Merge pull request #929 from notmasteryet/tree-49
Fixing Type3 text layer creation and character spacing
This commit is contained in:
commit
14647735b9
@ -638,17 +638,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
|
|||||||
geometry.hScale = tr[0] - bl[0];
|
geometry.hScale = tr[0] - bl[0];
|
||||||
geometry.vScale = tr[1] - bl[1];
|
geometry.vScale = tr[1] - bl[1];
|
||||||
}
|
}
|
||||||
var spaceGlyph = font.charsToGlyphs(' ');
|
geometry.spaceWidth = font.spaceWidth;
|
||||||
|
|
||||||
// Hack (sometimes space is not encoded)
|
|
||||||
if (spaceGlyph.length === 0 || spaceGlyph[0].width === 0)
|
|
||||||
spaceGlyph = font.charsToGlyphs('i');
|
|
||||||
|
|
||||||
// Fallback
|
|
||||||
if (spaceGlyph.length === 0 || spaceGlyph[0].width === 0)
|
|
||||||
spaceGlyph = [{width: 0}];
|
|
||||||
|
|
||||||
geometry.spaceWidth = spaceGlyph[0].width;
|
|
||||||
return geometry;
|
return geometry;
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -687,13 +677,6 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
|
|||||||
var textSelection = textLayer && !skipTextSelection ? true : false;
|
var textSelection = textLayer && !skipTextSelection ? true : false;
|
||||||
var textRenderingMode = current.textRenderingMode;
|
var textRenderingMode = current.textRenderingMode;
|
||||||
|
|
||||||
if (textSelection) {
|
|
||||||
ctx.save();
|
|
||||||
this.applyTextTransforms();
|
|
||||||
text.geom = this.getTextGeometry();
|
|
||||||
ctx.restore();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Type3 fonts - each glyph is a "mini-PDF"
|
// Type3 fonts - each glyph is a "mini-PDF"
|
||||||
if (font.coded) {
|
if (font.coded) {
|
||||||
ctx.save();
|
ctx.save();
|
||||||
@ -701,6 +684,13 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
|
|||||||
ctx.translate(current.x, current.y);
|
ctx.translate(current.x, current.y);
|
||||||
|
|
||||||
ctx.scale(textHScale, 1);
|
ctx.scale(textHScale, 1);
|
||||||
|
|
||||||
|
if (textSelection) {
|
||||||
|
this.save();
|
||||||
|
ctx.scale(1, -1);
|
||||||
|
text.geom = this.getTextGeometry();
|
||||||
|
this.restore();
|
||||||
|
}
|
||||||
for (var i = 0; i < glyphsLength; ++i) {
|
for (var i = 0; i < glyphsLength; ++i) {
|
||||||
|
|
||||||
var glyph = glyphs[i];
|
var glyph = glyphs[i];
|
||||||
@ -720,7 +710,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
|
|||||||
var width = transformed[0] * fontSize + charSpacing;
|
var width = transformed[0] * fontSize + charSpacing;
|
||||||
|
|
||||||
ctx.translate(width, 0);
|
ctx.translate(width, 0);
|
||||||
current.x += width * textHScale2;
|
current.x += width * textHScale;
|
||||||
|
|
||||||
text.str += glyph.unicode;
|
text.str += glyph.unicode;
|
||||||
text.length++;
|
text.length++;
|
||||||
@ -730,6 +720,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
|
|||||||
} else {
|
} else {
|
||||||
ctx.save();
|
ctx.save();
|
||||||
this.applyTextTransforms();
|
this.applyTextTransforms();
|
||||||
|
if (textSelection)
|
||||||
|
text.geom = this.getTextGeometry();
|
||||||
|
|
||||||
var width = 0;
|
var width = 0;
|
||||||
for (var i = 0; i < glyphsLength; ++i) {
|
for (var i = 0; i < glyphsLength; ++i) {
|
||||||
@ -780,17 +772,25 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
|
|||||||
showSpacedText: function canvasGraphicsShowSpacedText(arr) {
|
showSpacedText: function canvasGraphicsShowSpacedText(arr) {
|
||||||
var ctx = this.ctx;
|
var ctx = this.ctx;
|
||||||
var current = this.current;
|
var current = this.current;
|
||||||
|
var font = current.font;
|
||||||
var fontSize = current.fontSize;
|
var fontSize = current.fontSize;
|
||||||
var textHScale2 = current.textHScale *
|
var textHScale = current.textHScale;
|
||||||
(current.font.fontMatrix || IDENTITY_MATRIX)[0];
|
if (!font.coded)
|
||||||
|
textHScale *= (font.fontMatrix || IDENTITY_MATRIX)[0];
|
||||||
var arrLength = arr.length;
|
var arrLength = arr.length;
|
||||||
var textLayer = this.textLayer;
|
var textLayer = this.textLayer;
|
||||||
var font = current.font;
|
|
||||||
var text = {str: '', length: 0, canvasWidth: 0, geom: {}};
|
var text = {str: '', length: 0, canvasWidth: 0, geom: {}};
|
||||||
var textSelection = textLayer ? true : false;
|
var textSelection = textLayer ? true : false;
|
||||||
|
|
||||||
if (textSelection) {
|
if (textSelection) {
|
||||||
ctx.save();
|
ctx.save();
|
||||||
|
// Type3 fonts - each glyph is a "mini-PDF" (see also showText)
|
||||||
|
if (font.coded) {
|
||||||
|
ctx.transform.apply(ctx, current.textMatrix);
|
||||||
|
ctx.scale(1, -1);
|
||||||
|
ctx.translate(current.x, -1 * current.y);
|
||||||
|
ctx.scale(textHScale, 1);
|
||||||
|
} else
|
||||||
this.applyTextTransforms();
|
this.applyTextTransforms();
|
||||||
text.geom = this.getTextGeometry();
|
text.geom = this.getTextGeometry();
|
||||||
ctx.restore();
|
ctx.restore();
|
||||||
@ -799,7 +799,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
|
|||||||
for (var i = 0; i < arrLength; ++i) {
|
for (var i = 0; i < arrLength; ++i) {
|
||||||
var e = arr[i];
|
var e = arr[i];
|
||||||
if (isNum(e)) {
|
if (isNum(e)) {
|
||||||
var spacingLength = -e * 0.001 * fontSize * textHScale2;
|
var spacingLength = -e * 0.001 * fontSize * textHScale;
|
||||||
current.x += spacingLength;
|
current.x += spacingLength;
|
||||||
|
|
||||||
if (textSelection) {
|
if (textSelection) {
|
||||||
@ -807,9 +807,10 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
|
|||||||
text.canvasWidth += spacingLength;
|
text.canvasWidth += spacingLength;
|
||||||
if (e < 0 && text.geom.spaceWidth > 0) { // avoid div by zero
|
if (e < 0 && text.geom.spaceWidth > 0) { // avoid div by zero
|
||||||
var numFakeSpaces = Math.round(-e / text.geom.spaceWidth);
|
var numFakeSpaces = Math.round(-e / text.geom.spaceWidth);
|
||||||
for (var j = 0; j < numFakeSpaces; ++j)
|
if (numFakeSpaces > 0) {
|
||||||
text.str += ' ';
|
text.str += ' ';
|
||||||
text.length += numFakeSpaces > 0 ? 1 : 0;
|
text.length++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (isString(e)) {
|
} else if (isString(e)) {
|
||||||
|
31
src/fonts.js
31
src/fonts.js
@ -2101,6 +2101,37 @@ var Font = (function FontClosure() {
|
|||||||
return rule;
|
return rule;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
get spaceWidth() {
|
||||||
|
// trying to estimate space character width
|
||||||
|
var possibleSpaceReplacements = ['space', 'minus', 'one', 'i'];
|
||||||
|
var width;
|
||||||
|
for (var i = 0, ii = possibleSpaceReplacements.length; i < ii; i++) {
|
||||||
|
var glyphName = possibleSpaceReplacements[i];
|
||||||
|
// if possible, getting width by glyph name
|
||||||
|
if (glyphName in this.widths) {
|
||||||
|
width = this.widths[glyphName];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
var glyphUnicode = GlyphsUnicode[glyphName];
|
||||||
|
// finding the charcode via unicodeToCID map
|
||||||
|
var charcode = 0;
|
||||||
|
if (this.composite)
|
||||||
|
charcode = this.unicodeToCID[glyphUnicode];
|
||||||
|
// ... via toUnicode map
|
||||||
|
if (!charcode && 'toUnicode' in this)
|
||||||
|
charcode = this.toUnicode.indexOf(glyphUnicode);
|
||||||
|
// setting it to unicode if negative or undefined
|
||||||
|
if (!(charcode > 0))
|
||||||
|
charcode = glyphUnicode;
|
||||||
|
// trying to get width via charcode
|
||||||
|
width = this.widths[charcode];
|
||||||
|
if (width)
|
||||||
|
break; // the non-zero width found
|
||||||
|
}
|
||||||
|
width = (width || this.defaultWidth) * this.widthMultiplier;
|
||||||
|
return shadow(this, 'spaceWidth', width);
|
||||||
|
},
|
||||||
|
|
||||||
charToGlyph: function fonts_charToGlyph(charcode) {
|
charToGlyph: function fonts_charToGlyph(charcode) {
|
||||||
var unicode, width, codeIRQueue;
|
var unicode, width, codeIRQueue;
|
||||||
|
|
||||||
|
1
test/pdfs/.gitignore
vendored
1
test/pdfs/.gitignore
vendored
@ -19,3 +19,4 @@
|
|||||||
!issue840.pdf
|
!issue840.pdf
|
||||||
!scan-bad.pdf
|
!scan-bad.pdf
|
||||||
!freeculture.pdf
|
!freeculture.pdf
|
||||||
|
!issue918.pdf
|
||||||
|
BIN
test/pdfs/issue918.pdf
Normal file
BIN
test/pdfs/issue918.pdf
Normal file
Binary file not shown.
@ -348,5 +348,11 @@
|
|||||||
"link": true,
|
"link": true,
|
||||||
"pageLimit": 3,
|
"pageLimit": 3,
|
||||||
"type": "eq"
|
"type": "eq"
|
||||||
|
},
|
||||||
|
{ "id": "issue918",
|
||||||
|
"file": "pdfs/issue918.pdf",
|
||||||
|
"md5": "d582cc0f2592ae82936589ced2a47e55",
|
||||||
|
"rounds": 1,
|
||||||
|
"type": "eq"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user