mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-26 10:08:06 +02:00
Refactors font matrix operations
This commit is contained in:
parent
ccfa0e1972
commit
10bb6c9ec0
3 changed files with 54 additions and 56 deletions
43
src/fonts.js
43
src/fonts.js
|
@ -29,6 +29,8 @@ var PDF_GLYPH_SPACE_UNITS = 1000;
|
|||
// Until hinting is fully supported this constant can be used
|
||||
var HINTING_ENABLED = false;
|
||||
|
||||
var FONT_IDENTITY_MATRIX = [0.001, 0, 0, 0.001, 0, 0];
|
||||
|
||||
var FontFlags = {
|
||||
FixedPitch: 1,
|
||||
Serif: 2,
|
||||
|
@ -2214,6 +2216,19 @@ function fontCharsToUnicode(charCodes, font) {
|
|||
return result;
|
||||
}
|
||||
|
||||
function adjustWidths(properties) {
|
||||
if (properties.fontMatrix[0] === FONT_IDENTITY_MATRIX[0]) {
|
||||
return;
|
||||
}
|
||||
// adjusting width to fontMatrix scale
|
||||
var scale = 0.001 / properties.fontMatrix[0];
|
||||
var glyphsWidths = properties.widths;
|
||||
for (var glyph in glyphsWidths) {
|
||||
glyphsWidths[glyph] *= scale;
|
||||
}
|
||||
properties.defaultWidth *= scale;
|
||||
}
|
||||
|
||||
/**
|
||||
* 'Font' is the class the outside world should use, it encapsulate all the font
|
||||
* decoding logics whatever type it is (assuming the font type is supported).
|
||||
|
@ -2260,7 +2275,6 @@ var Font = (function FontClosure() {
|
|||
this.hasEncoding = properties.hasEncoding;
|
||||
|
||||
this.fontMatrix = properties.fontMatrix;
|
||||
this.widthMultiplier = 1.0;
|
||||
if (properties.type == 'Type3') {
|
||||
this.encoding = properties.baseEncoding;
|
||||
return;
|
||||
|
@ -2318,6 +2332,8 @@ var Font = (function FontClosure() {
|
|||
var cff = (subtype == 'Type1C' || subtype == 'CIDFontType0C') ?
|
||||
new CFFFont(file, properties) : new Type1Font(name, file, properties);
|
||||
|
||||
adjustWidths(properties);
|
||||
|
||||
// Wrap the CFF data inside an OTF font file
|
||||
data = this.convert(name, cff, properties);
|
||||
break;
|
||||
|
@ -2337,10 +2353,13 @@ var Font = (function FontClosure() {
|
|||
}
|
||||
|
||||
this.data = data;
|
||||
|
||||
// Transfer some properties again that could change during font conversion
|
||||
this.fontMatrix = properties.fontMatrix;
|
||||
this.widthMultiplier = !properties.fontMatrix ? 1.0 :
|
||||
1.0 / properties.fontMatrix[0];
|
||||
this.widths = properties.widths;
|
||||
this.defaultWidth = properties.defaultWidth;
|
||||
this.encoding = properties.baseEncoding;
|
||||
|
||||
this.loading = true;
|
||||
};
|
||||
|
||||
|
@ -3931,7 +3950,7 @@ var Font = (function FontClosure() {
|
|||
}
|
||||
this.toFontChar = toFontChar;
|
||||
}
|
||||
var unitsPerEm = properties.unitsPerEm || 1000; // defaulting to 1000
|
||||
var unitsPerEm = 1 / (properties.fontMatrix || FONT_IDENTITY_MATRIX)[0];
|
||||
|
||||
var fields = {
|
||||
// PostScript Font Program
|
||||
|
@ -4170,7 +4189,7 @@ var Font = (function FontClosure() {
|
|||
if (width)
|
||||
break; // the non-zero width found
|
||||
}
|
||||
width = (width || this.defaultWidth) * this.widthMultiplier;
|
||||
width = width || this.defaultWidth;
|
||||
// Do not shadow the property here. See discussion:
|
||||
// https://github.com/mozilla/pdf.js/pull/2127#discussion_r1662280
|
||||
this._shadowWidth = width;
|
||||
|
@ -4251,7 +4270,7 @@ var Font = (function FontClosure() {
|
|||
if (typeof unicodeChars === 'number')
|
||||
unicodeChars = String.fromCharCode(unicodeChars);
|
||||
|
||||
width = (isNum(width) ? width : this.defaultWidth) * this.widthMultiplier;
|
||||
width = isNum(width) ? width : this.defaultWidth;
|
||||
disabled = this.unicodeIsEnabled ?
|
||||
!this.unicodeIsEnabled[fontCharCode] : false;
|
||||
|
||||
|
@ -4902,14 +4921,6 @@ var Type1Parser = function type1Parser() {
|
|||
switch (token) {
|
||||
case '/FontMatrix':
|
||||
var matrix = readNumberArray(headerString, i + 1);
|
||||
|
||||
// The FontMatrix is in unitPerEm, so make it pixels
|
||||
for (var j = 0, jj = matrix.length; j < jj; j++)
|
||||
matrix[j] *= 1000;
|
||||
|
||||
// Make the angle into the right direction
|
||||
matrix[2] *= -1;
|
||||
|
||||
properties.fontMatrix = matrix;
|
||||
break;
|
||||
case '/Encoding':
|
||||
|
@ -5185,6 +5196,7 @@ Type1Font.prototype = {
|
|||
topDict.setByName('FamilyName', 3);
|
||||
topDict.setByName('Weight', 4);
|
||||
topDict.setByName('Encoding', null); // placeholder
|
||||
topDict.setByName('FontMatrix', properties.fontMatrix);
|
||||
topDict.setByName('FontBBox', properties.bbox);
|
||||
topDict.setByName('charset', null); // placeholder
|
||||
topDict.setByName('CharStrings', null); // placeholder
|
||||
|
@ -5459,8 +5471,7 @@ var CFFParser = (function CFFParserClosure() {
|
|||
|
||||
var fontMatrix = topDict.getByName('FontMatrix');
|
||||
if (fontMatrix) {
|
||||
// estimating unitsPerEM for the font
|
||||
properties.unitsPerEm = 1 / fontMatrix[0];
|
||||
properties.fontMatrix = fontMatrix;
|
||||
}
|
||||
|
||||
var fontBBox = topDict.getByName('FontBBox');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue