Skip to content

Commit e13164e

Browse files
committed
Read the text matrix from the Type1 font ascii header
1 parent ae2d130 commit e13164e

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

fonts.js

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var isWorker = (typeof window == "undefined");
88
/**
99
* Maximum file size of the font.
1010
*/
11-
var kMaxFontFileSize = 200000;
11+
var kMaxFontFileSize = 40000;
1212

1313
/**
1414
* Maximum time to wait for a font to be loaded by @font-face
@@ -77,6 +77,9 @@ var Fonts = (function Fonts() {
7777
measureCache = sizes[size] = Object.create(null);
7878
ctx.font = (size * kScalePrecision) + 'px "' + fontName + '"';
7979
},
80+
getActive: function fonts_getActive() {
81+
return current;
82+
},
8083
charsToUnicode: function fonts_chars2Unicode(chars) {
8184
if (!charsCache)
8285
return chars;
@@ -1386,7 +1389,57 @@ var Type1Parser = function() {
13861389
}
13871390

13881391
return extracted;
1389-
}
1392+
},
1393+
1394+
this.extractFontHeader = function t1_extractFontProgram(stream) {
1395+
var headerString = "";
1396+
for (var i = 0; i < stream.length; i++)
1397+
headerString += String.fromCharCode(stream[i]);
1398+
1399+
var info = {
1400+
textMatrix: null
1401+
};
1402+
1403+
function readNumberArray(str, index) {
1404+
var start = ++index;
1405+
var count = 0;
1406+
while ((c = str[index++]) != "]")
1407+
count++;
1408+
1409+
var array = str.substr(start, count).split(" ");
1410+
for (var i = 0; i < array.length; i++)
1411+
array[i] = parseFloat(array[i]);
1412+
return array;
1413+
};
1414+
1415+
var token = "";
1416+
var count = headerString.length;
1417+
for (var i = 0; i < count; i++) {
1418+
var c = headerString[i];
1419+
if (c == " " || c == "\n") {
1420+
switch (token) {
1421+
case "/FontMatrix":
1422+
var matrix = readNumberArray(headerString, i + 1);
1423+
1424+
// The FontMatrix is in unitPerEm, so make it pixels
1425+
for (var j = 0; j < matrix.length; j++)
1426+
matrix[j] *= 1000;
1427+
1428+
// Make the angle into the right direction
1429+
matrix[2] *= -1;
1430+
1431+
info.textMatrix = matrix;
1432+
break;
1433+
}
1434+
token = "";
1435+
} else {
1436+
token += c;
1437+
}
1438+
}
1439+
1440+
return info;
1441+
};
1442+
13901443
};
13911444

13921445
/**
@@ -1457,7 +1510,12 @@ var CFF = function(name, file, properties) {
14571510
// Get the data block containing glyphs and subrs informations
14581511
var length1 = file.dict.get("Length1");
14591512
var length2 = file.dict.get("Length2");
1460-
file.skip(length1);
1513+
1514+
var headerBlock = file.getBytes(length1);
1515+
var header = type1Parser.extractFontHeader(headerBlock);
1516+
for (var info in header) {
1517+
properties[info] = header[info];
1518+
}
14611519

14621520
// Decrypt the data blocks and retrieve it's content
14631521
var eexecBlock = file.getBytes(length2);
@@ -1700,6 +1758,7 @@ CFF.prototype = {
17001758
"charstrings": this.createCFFIndexHeader([[0x8B, 0x0E]].concat(glyphs), true),
17011759
17021760
"private": (function(self) {
1761+
log(properties.stemSnapH);
17031762
var data =
17041763
"\x8b\x14" + // defaultWidth
17051764
"\x8b\x15" + // nominalWidth

pdf.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3544,7 +3544,8 @@ var CanvasGraphics = (function() {
35443544
capHeight: descriptor.get("CapHeight"),
35453545
flags: descriptor.get("Flags"),
35463546
italicAngle: descriptor.get("ItalicAngle"),
3547-
fixedPitch: false
3547+
fixedPitch: false,
3548+
textMatrix: IDENTITY_MATRIX.slice()
35483549
};
35493550

35503551
return {
@@ -3861,14 +3862,15 @@ var CanvasGraphics = (function() {
38613862
// TODO: apply charSpacing, wordSpacing, textHScale
38623863

38633864
this.ctx.save();
3864-
this.ctx.transform.apply(this.ctx, this.current.textMatrix);
38653865
this.ctx.scale(1, -1);
38663866

38673867
if (this.ctx.$showText) {
38683868
this.ctx.$showText(this.current.y, Fonts.charsToUnicode(text));
38693869
} else {
38703870
text = Fonts.charsToUnicode(text);
38713871
this.ctx.translate(this.current.x, -1 * this.current.y);
3872+
var matrix = Fonts.lookup(this.current.fontName).properties.textMatrix;
3873+
this.ctx.transform.apply(this.ctx, matrix);
38723874
this.ctx.fillText(text, 0, 0);
38733875
this.current.x += Fonts.measureText(text);
38743876
}

0 commit comments

Comments
 (0)