Merge pull request #249 from sbarman/shading
Cleaned up shading colorspace code
This commit is contained in:
		
						commit
						24e6fdbf97
					
				
							
								
								
									
										94
									
								
								pdf.js
									
									
									
									
									
								
							
							
						
						
									
										94
									
								
								pdf.js
									
									
									
									
									
								
							@ -4148,9 +4148,13 @@ var CanvasGraphics = (function() {
 | 
				
			|||||||
      this.setStrokeRGBColor.apply(this, color);
 | 
					      this.setStrokeRGBColor.apply(this, color);
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    setStrokeColorN: function(/*...*/) {
 | 
					    setStrokeColorN: function(/*...*/) {
 | 
				
			||||||
      // TODO real impl
 | 
					      var cs = this.getStrokeColorSpace();
 | 
				
			||||||
      TODO('check for special color spaces');
 | 
					
 | 
				
			||||||
      this.setStrokeColor.apply(this, arguments);
 | 
					      if (cs.name == 'Pattern') {
 | 
				
			||||||
 | 
					        this.ctx.strokeStyle = this.getPattern(cs, arguments);
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        this.setStrokeColor.apply(this, arguments);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    setFillColor: function(/*...*/) {
 | 
					    setFillColor: function(/*...*/) {
 | 
				
			||||||
      var cs = this.getFillColorSpace();
 | 
					      var cs = this.getFillColorSpace();
 | 
				
			||||||
@ -4161,28 +4165,25 @@ var CanvasGraphics = (function() {
 | 
				
			|||||||
      var cs = this.getFillColorSpace();
 | 
					      var cs = this.getFillColorSpace();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if (cs.name == 'Pattern') {
 | 
					      if (cs.name == 'Pattern') {
 | 
				
			||||||
        var length = arguments.length;
 | 
					        this.ctx.fillStyle = this.getPattern(cs, arguments);
 | 
				
			||||||
        var base = cs.base;
 | 
					 | 
				
			||||||
        if (base) {
 | 
					 | 
				
			||||||
          var baseComps = base.numComps;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
          if (baseComps != length - 1)
 | 
					 | 
				
			||||||
            error("invalid base color for pattern colorspace");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
          var color = [];
 | 
					 | 
				
			||||||
          for (var i = 0; i < baseComps; ++i)
 | 
					 | 
				
			||||||
            color.push(arguments[i]);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
          color = base.getRgb(color);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        var patternName = arguments[length - 1];
 | 
					 | 
				
			||||||
        this.setFillPattern(patternName, base, color);
 | 
					 | 
				
			||||||
      } else {
 | 
					      } else {
 | 
				
			||||||
        // TODO real impl
 | 
					 | 
				
			||||||
        this.setFillColor.apply(this, arguments);
 | 
					        this.setFillColor.apply(this, arguments);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    setFillPattern: function(patternName, baseCS, color) {
 | 
					    getPattern: function(cs, args) {
 | 
				
			||||||
 | 
					      var length = args.length;
 | 
				
			||||||
 | 
					      var base = cs.base;
 | 
				
			||||||
 | 
					      if (base) {
 | 
				
			||||||
 | 
					        var baseComps = base.numComps;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        var color = [];
 | 
				
			||||||
 | 
					        for (var i = 0; i < baseComps; ++i)
 | 
				
			||||||
 | 
					          color.push(args[i]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        color = base.getRgb(color);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      var patternName = args[length - 1];
 | 
				
			||||||
      if (!IsName(patternName))
 | 
					      if (!IsName(patternName))
 | 
				
			||||||
        error("Bad args to getPattern");
 | 
					        error("Bad args to getPattern");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -4194,36 +4195,26 @@ var CanvasGraphics = (function() {
 | 
				
			|||||||
      var pattern = xref.fetchIfRef(patternRes.get(patternName.name));
 | 
					      var pattern = xref.fetchIfRef(patternRes.get(patternName.name));
 | 
				
			||||||
      var dict = IsStream(pattern) ? pattern.dict : pattern;
 | 
					      var dict = IsStream(pattern) ? pattern.dict : pattern;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      var types = [null, this.setTilingPattern, this.setShadingPattern];
 | 
					      var types = [null, this.getTilingPattern, this.getShadingPattern];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      var typeNum = dict.get("PatternType");
 | 
					      var typeNum = dict.get("PatternType");
 | 
				
			||||||
      var patternFn = types[typeNum];
 | 
					      var patternFn = types[typeNum];
 | 
				
			||||||
      if (!patternFn)
 | 
					      if (!patternFn)
 | 
				
			||||||
        error("Unhandled pattern type");
 | 
					        error("Unhandled pattern type");
 | 
				
			||||||
      patternFn.call(this, pattern, dict, baseCS, color);
 | 
					      return patternFn.call(this, pattern, dict, color);
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    setShadingPattern: function(pattern, dict) {
 | 
					    getShadingPattern: function(pattern, dict) {
 | 
				
			||||||
      var matrix = dict.get("Matrix");
 | 
					      var matrix = dict.get("Matrix");
 | 
				
			||||||
      
 | 
					      
 | 
				
			||||||
      var inv = [0,0,0,0,0,0];
 | 
					      this.save();
 | 
				
			||||||
      var det = 1 / (matrix[0] * matrix[3] - matrix[1] * matrix[2]);
 | 
					 | 
				
			||||||
      inv[0] = matrix[3] * det;
 | 
					 | 
				
			||||||
      inv[1] = -matrix[1] * det;
 | 
					 | 
				
			||||||
      inv[2] = -matrix[2] * det;
 | 
					 | 
				
			||||||
      inv[3] = matrix[0] * det;
 | 
					 | 
				
			||||||
      inv[4] = det * (matrix[2] * matrix[5] - matrix[3] * matrix[4]);
 | 
					 | 
				
			||||||
      inv[5] = det * (matrix[1] * matrix[4] - matrix[0] * matrix[5]);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      this.transform.apply(this, matrix);
 | 
					      this.transform.apply(this, matrix);
 | 
				
			||||||
      var shading = this.getShading(pattern.get("Shading"));
 | 
					      var shading = this.getShading(pattern.get("Shading"));
 | 
				
			||||||
      this.ctx.fillStyle = shading;
 | 
					      this.restore();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // HACK to get the gradient to show at the right location. If
 | 
					      TODO('store transform so it can be applied before every fill');
 | 
				
			||||||
      // removed, the gradient will show at the pre-transform coordinates.
 | 
					      return shading;
 | 
				
			||||||
      this.ctx.fillRect(0,0,0,0);
 | 
					 | 
				
			||||||
      this.transform.apply(this, inv);
 | 
					 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    setTilingPattern: function(pattern, dict, baseCS, color) {
 | 
					    getTilingPattern: function(pattern, dict, color) {
 | 
				
			||||||
      function multiply(m, tm) {
 | 
					      function multiply(m, tm) {
 | 
				
			||||||
        var a = m[0] * tm[0] + m[1] * tm[2];
 | 
					        var a = m[0] * tm[0] + m[1] * tm[2];
 | 
				
			||||||
        var b = m[0] * tm[1] + m[1] * tm[3];
 | 
					        var b = m[0] * tm[1] + m[1] * tm[3];
 | 
				
			||||||
@ -4257,8 +4248,10 @@ var CanvasGraphics = (function() {
 | 
				
			|||||||
      var height = botRight[1] - topLeft[1];
 | 
					      var height = botRight[1] - topLeft[1];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // TODO: hack to avoid OOM, remove then pattern code is fixed
 | 
					      // TODO: hack to avoid OOM, remove then pattern code is fixed
 | 
				
			||||||
      if (Math.abs(width) > 8192 || Math.abs(height) > 8192)
 | 
					      if (Math.abs(width) > 8192 || Math.abs(height) > 8192) {
 | 
				
			||||||
        return false;
 | 
					        this.restore();
 | 
				
			||||||
 | 
					        return 'hotpink';
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      var tmpCanvas = new this.ScratchCanvas(width, height);
 | 
					      var tmpCanvas = new this.ScratchCanvas(width, height);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -4270,13 +4263,13 @@ var CanvasGraphics = (function() {
 | 
				
			|||||||
      var paintType = dict.get('PaintType');
 | 
					      var paintType = dict.get('PaintType');
 | 
				
			||||||
      switch (paintType) {
 | 
					      switch (paintType) {
 | 
				
			||||||
      case PAINT_TYPE_COLORED:
 | 
					      case PAINT_TYPE_COLORED:
 | 
				
			||||||
        // should go to default for color space
 | 
					        tmpCtx.fillStyle = savedCtx.fillStyle;
 | 
				
			||||||
        tmpCtx.fillStyle = this.makeCssRgb(1, 1, 1);
 | 
					        tmpCtx.strokeStyle = savedCtx.strokeStyle;
 | 
				
			||||||
        tmpCtx.strokeStyle = this.makeCssRgb(0, 0, 0);
 | 
					 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
      case PAINT_TYPE_UNCOLORED:
 | 
					      case PAINT_TYPE_UNCOLORED:
 | 
				
			||||||
        tmpCtx.fillStyle = this.makeCssRgb.apply(this, baseCS.getRgb(color));
 | 
					        color = this.makeCssRgb.apply(this, color);
 | 
				
			||||||
        tmpCtx.strokeStyle = this.makeCssRgb.apply(this, baseCS.getRgb(color));
 | 
					        tmpCtx.fillStyle = color;
 | 
				
			||||||
 | 
					        tmpCtx.strokeStyle = color;
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
      default:
 | 
					      default:
 | 
				
			||||||
        error('Unsupported paint type');
 | 
					        error('Unsupported paint type');
 | 
				
			||||||
@ -4310,9 +4303,7 @@ var CanvasGraphics = (function() {
 | 
				
			|||||||
      this.ctx = savedCtx;
 | 
					      this.ctx = savedCtx;
 | 
				
			||||||
      this.restore();
 | 
					      this.restore();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      TODO('Inverse pattern is painted');
 | 
					      return this.ctx.createPattern(tmpCanvas, 'repeat');
 | 
				
			||||||
      pattern = this.ctx.createPattern(tmpCanvas, 'repeat');
 | 
					 | 
				
			||||||
      this.ctx.fillStyle = pattern;
 | 
					 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    setStrokeGray: function(gray) {
 | 
					    setStrokeGray: function(gray) {
 | 
				
			||||||
      this.setStrokeRGBColor(gray, gray, gray);
 | 
					      this.setStrokeRGBColor(gray, gray, gray);
 | 
				
			||||||
@ -4382,6 +4373,8 @@ var CanvasGraphics = (function() {
 | 
				
			|||||||
      this.restore();
 | 
					      this.restore();
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    getShading: function(shading) {
 | 
					    getShading: function(shading) {
 | 
				
			||||||
 | 
					      this.save();
 | 
				
			||||||
 | 
					      
 | 
				
			||||||
      shading = this.xref.fetchIfRef(shading);
 | 
					      shading = this.xref.fetchIfRef(shading);
 | 
				
			||||||
      var dict = IsStream(shading) ? shading.dict : shading;
 | 
					      var dict = IsStream(shading) ? shading.dict : shading;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -4407,6 +4400,8 @@ var CanvasGraphics = (function() {
 | 
				
			|||||||
      var typeNum = dict.get('ShadingType');
 | 
					      var typeNum = dict.get('ShadingType');
 | 
				
			||||||
      var shadingFn = types[typeNum];
 | 
					      var shadingFn = types[typeNum];
 | 
				
			||||||
      
 | 
					      
 | 
				
			||||||
 | 
					      this.restore();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // Most likely we will not implement other types of shading
 | 
					      // Most likely we will not implement other types of shading
 | 
				
			||||||
      // unless the browser supports them
 | 
					      // unless the browser supports them
 | 
				
			||||||
      if (!shadingFn) {
 | 
					      if (!shadingFn) {
 | 
				
			||||||
@ -4498,7 +4493,6 @@ var CanvasGraphics = (function() {
 | 
				
			|||||||
        gradient.addColorStop((i - t0) / diff, 
 | 
					        gradient.addColorStop((i - t0) / diff, 
 | 
				
			||||||
            this.makeCssRgb.apply(this, rgbColor));
 | 
					            this.makeCssRgb.apply(this, rgbColor));
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					 | 
				
			||||||
      return gradient;
 | 
					      return gradient;
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user