Skip to content

Commit f06aa6a

Browse files
committed
Merge pull request mozilla#6391 from CodingFabian/fix-vertical-kernings-6387
Fix text spacing with vertical fonts (mozilla#6387)
2 parents e68a5c0 + 2564827 commit f06aa6a

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

src/core/evaluator.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -731,11 +731,12 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
731731
var arr = args[0];
732732
var combinedGlyphs = [];
733733
var arrLength = arr.length;
734+
var state = stateManager.state;
734735
for (i = 0; i < arrLength; ++i) {
735736
var arrItem = arr[i];
736737
if (isString(arrItem)) {
737738
Array.prototype.push.apply(combinedGlyphs,
738-
self.handleText(arrItem, stateManager.state));
739+
self.handleText(arrItem, state));
739740
} else if (isNum(arrItem)) {
740741
combinedGlyphs.push(arrItem);
741742
}
@@ -1131,17 +1132,26 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
11311132
if (typeof items[j] === 'string') {
11321133
buildTextGeometry(items[j], textChunk);
11331134
} else {
1134-
var val = items[j] / 1000;
1135-
if (!textState.font.vertical) {
1136-
offset = -val * textState.fontSize * textState.textHScale *
1137-
textState.textMatrix[0];
1138-
textState.translateTextMatrix(offset, 0);
1139-
textChunk.width += offset;
1140-
} else {
1141-
offset = -val * textState.fontSize *
1142-
textState.textMatrix[3];
1135+
// PDF Specification 5.3.2 states:
1136+
// The number is expressed in thousandths of a unit of text
1137+
// space.
1138+
// This amount is subtracted from the current horizontal or
1139+
// vertical coordinate, depending on the writing mode.
1140+
// In the default coordinate system, a positive adjustment
1141+
// has the effect of moving the next glyph painted either to
1142+
// the left or down by the given amount.
1143+
var val = items[j] * textState.fontSize / 1000;
1144+
if (textState.font.vertical) {
1145+
offset = val * textState.textMatrix[3];
11431146
textState.translateTextMatrix(0, offset);
1147+
// Value needs to be added to height to paint down.
11441148
textChunk.height += offset;
1149+
} else {
1150+
offset = val * textState.textHScale *
1151+
textState.textMatrix[0];
1152+
textState.translateTextMatrix(offset, 0);
1153+
// Value needs to be subtracted from width to paint left.
1154+
textChunk.width -= offset;
11451155
}
11461156
if (items[j] < 0 && textState.font.spaceWidth > 0) {
11471157
var fakeSpaces = -items[j] / textState.font.spaceWidth;

src/display/canvas.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
13601360
var textHScale = current.textHScale * fontDirection;
13611361
var glyphsLength = glyphs.length;
13621362
var vertical = font.vertical;
1363+
var spacingDir = vertical ? 1 : -1;
13631364
var defaultVMetrics = font.defaultVMetrics;
13641365
var widthAdvanceScale = fontSize * current.fontMatrix[0];
13651366

@@ -1406,7 +1407,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
14061407
x += fontDirection * wordSpacing;
14071408
continue;
14081409
} else if (isNum(glyph)) {
1409-
x += -glyph * fontSize * 0.001;
1410+
x += spacingDir * glyph * fontSize / 1000;
14101411
continue;
14111412
}
14121413

@@ -1476,14 +1477,15 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
14761477
var font = current.font;
14771478
var fontSize = current.fontSize;
14781479
var fontDirection = current.fontDirection;
1480+
var spacingDir = font.vertical ? 1 : -1;
14791481
var charSpacing = current.charSpacing;
14801482
var wordSpacing = current.wordSpacing;
14811483
var textHScale = current.textHScale * fontDirection;
14821484
var fontMatrix = current.fontMatrix || FONT_IDENTITY_MATRIX;
14831485
var glyphsLength = glyphs.length;
14841486
var isTextInvisible =
14851487
current.textRenderingMode === TextRenderingMode.INVISIBLE;
1486-
var i, glyph, width;
1488+
var i, glyph, width, spacingLength;
14871489

14881490
if (isTextInvisible || fontSize === 0) {
14891491
return;
@@ -1504,7 +1506,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
15041506
current.x += wordSpacing * textHScale;
15051507
continue;
15061508
} else if (isNum(glyph)) {
1507-
var spacingLength = -glyph * 0.001 * fontSize;
1509+
spacingLength = spacingDir * glyph * fontSize / 1000;
15081510
this.ctx.translate(spacingLength, 0);
15091511
current.x += spacingLength * textHScale;
15101512
continue;

test/pdfs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
!issue5747.pdf
101101
!issue6099.pdf
102102
!issue6336.pdf
103+
!issue6387.pdf
103104
!issue6410.pdf
104105
!gradientfill.pdf
105106
!bug903856.pdf

test/pdfs/issue6387.pdf

64.6 KB
Binary file not shown.

test/test_manifest.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,19 @@
15741574
"lastPage": 1,
15751575
"type": "eq"
15761576
},
1577+
{ "id": "issue6387-canvas",
1578+
"file": "pdfs/issue6387.pdf",
1579+
"md5": "08c39ac6d0aab1596e6e59793eaf3ee4",
1580+
"rounds": 1,
1581+
"type": "eq"
1582+
},
1583+
{ "id": "issue6387-text",
1584+
"file": "pdfs/issue6387.pdf",
1585+
"md5": "08c39ac6d0aab1596e6e59793eaf3ee4",
1586+
"rounds": 1,
1587+
"type": "text",
1588+
"about": "Note that the text layer seems to be off to the right."
1589+
},
15771590
{ "id": "issue4890",
15781591
"file": "pdfs/issue4890.pdf",
15791592
"md5": "1666feb4cd26318c2bdbea6a175dce87",

0 commit comments

Comments
 (0)