Generic build of PDF.js library.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
7.3 KiB

/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* Copyright 2012 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals ColorSpace, PDFFunction, Util, error, warn, info, isArray, isStream,
isPDFFunction, UnsupportedManager, UNSUPPORTED_FEATURES */
'use strict';
var PatternType = {
AXIAL: 2,
RADIAL: 3
};
var Pattern = (function PatternClosure() {
// Constructor should define this.getPattern
function Pattern() {
error('should not call Pattern constructor');
}
Pattern.prototype = {
// Input: current Canvas context
// Output: the appropriate fillStyle or strokeStyle
getPattern: function Pattern_getPattern(ctx) {
error('Should not call Pattern.getStyle: ' + ctx);
}
};
Pattern.parseShading = function Pattern_parseShading(shading, matrix, xref,
res) {
var dict = isStream(shading) ? shading.dict : shading;
var type = dict.get('ShadingType');
switch (type) {
case PatternType.AXIAL:
case PatternType.RADIAL:
// Both radial and axial shadings are handled by RadialAxial shading.
return new Shadings.RadialAxial(dict, matrix, xref, res);
default:
UnsupportedManager.notify(UNSUPPORTED_FEATURES.shadingPattern);
return new Shadings.Dummy();
}
};
return Pattern;
})();
var Shadings = {};
// A small number to offset the first/last color stops so we can insert ones to
// support extend. Number.MIN_VALUE appears to be too small and breaks the
// extend. 1e-7 works in FF but chrome seems to use an even smaller sized number
// internally so we have to go bigger.
Shadings.SMALL_NUMBER = 1e-2;
// Radial and axial shading have very similar implementations
// If needed, the implementations can be broken into two classes
Shadings.RadialAxial = (function RadialAxialClosure() {
function RadialAxial(dict, matrix, xref, res) {
this.matrix = matrix;
this.coordsArr = dict.get('Coords');
this.shadingType = dict.get('ShadingType');
this.type = 'Pattern';
var cs = dict.get('ColorSpace', 'CS');
cs = ColorSpace.parse(cs, xref, res);
this.cs = cs;
var t0 = 0.0, t1 = 1.0;
if (dict.has('Domain')) {
var domainArr = dict.get('Domain');
t0 = domainArr[0];
t1 = domainArr[1];
}
var extendStart = false, extendEnd = false;
if (dict.has('Extend')) {
var extendArr = dict.get('Extend');
extendStart = extendArr[0];
extendEnd = extendArr[1];
}
if (this.shadingType === PatternType.RADIAL &&
(!extendStart || !extendEnd)) {
// Radial gradient only currently works if either circle is fully within
// the other circle.
var x1 = this.coordsArr[0];
var y1 = this.coordsArr[1];
var r1 = this.coordsArr[2];
var x2 = this.coordsArr[3];
var y2 = this.coordsArr[4];
var r2 = this.coordsArr[5];
var distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
if (r1 <= r2 + distance &&
r2 <= r1 + distance) {
warn('Unsupported radial gradient.');
}
}
this.extendStart = extendStart;
this.extendEnd = extendEnd;
var fnObj = dict.get('Function');
var fn;
if (isArray(fnObj)) {
var fnArray = [];
for (var j = 0, jj = fnObj.length; j < jj; j++) {
var obj = xref.fetchIfRef(fnObj[j]);
if (!isPDFFunction(obj)) {
error('Invalid function');
}
fnArray.push(PDFFunction.parse(xref, obj));
}
fn = function radialAxialColorFunction(arg) {
var out = [];
for (var i = 0, ii = fnArray.length; i < ii; i++) {
out.push(fnArray[i](arg)[0]);
}
return out;
};
} else {
if (!isPDFFunction(fnObj)) {
error('Invalid function');
}
fn = PDFFunction.parse(xref, fnObj);
}
// 10 samples seems good enough for now, but probably won't work
// if there are sharp color changes. Ideally, we would implement
// the spec faithfully and add lossless optimizations.
var diff = t1 - t0;
var step = diff / 10;
var colorStops = this.colorStops = [];
// Protect against bad domains so we don't end up in an infinte loop below.
if (t0 >= t1 || step <= 0) {
// Acrobat doesn't seem to handle these cases so we'll ignore for
// now.
info('Bad shading domain.');
return;
}
for (var i = t0; i <= t1; i += step) {
var rgbColor = cs.getRgb(fn([i]), 0);
var cssColor = Util.makeCssRgb(rgbColor);
colorStops.push([(i - t0) / diff, cssColor]);
}
var background = 'transparent';
if (dict.has('Background')) {
var rgbColor = cs.getRgb(dict.get('Background'), 0);
background = Util.makeCssRgb(rgbColor);
}
if (!extendStart) {
// Insert a color stop at the front and offset the first real color stop
// so it doesn't conflict with the one we insert.
colorStops.unshift([0, background]);
colorStops[1][0] += Shadings.SMALL_NUMBER;
}
if (!extendEnd) {
// Same idea as above in extendStart but for the end.
colorStops[colorStops.length - 1][0] -= Shadings.SMALL_NUMBER;
colorStops.push([1, background]);
}
this.colorStops = colorStops;
}
RadialAxial.prototype = {
getIR: function RadialAxial_getIR() {
var coordsArr = this.coordsArr;
var shadingType = this.shadingType;
var type, p0, p1, r0, r1;
if (shadingType == PatternType.AXIAL) {
p0 = [coordsArr[0], coordsArr[1]];
p1 = [coordsArr[2], coordsArr[3]];
r0 = null;
r1 = null;
type = 'axial';
} else if (shadingType == PatternType.RADIAL) {
p0 = [coordsArr[0], coordsArr[1]];
p1 = [coordsArr[3], coordsArr[4]];
r0 = coordsArr[2];
r1 = coordsArr[5];
type = 'radial';
} else {
error('getPattern type unknown: ' + shadingType);
}
var matrix = this.matrix;
if (matrix) {
p0 = Util.applyTransform(p0, matrix);
p1 = Util.applyTransform(p1, matrix);
}
return ['RadialAxial', type, this.colorStops, p0, p1, r0, r1];
}
};
return RadialAxial;
})();
Shadings.Dummy = (function DummyClosure() {
function Dummy() {
this.type = 'Pattern';
}
Dummy.prototype = {
getIR: function Dummy_getIR() {
return ['Dummy'];
}
};
return Dummy;
})();
function getTilingPatternIR(operatorList, dict, args) {
var matrix = dict.get('Matrix');
var bbox = dict.get('BBox');
var xstep = dict.get('XStep');
var ystep = dict.get('YStep');
var paintType = dict.get('PaintType');
var tilingType = dict.get('TilingType');
return [
'TilingPattern', args, operatorList, matrix, bbox, xstep, ystep,
paintType, tilingType
];
}