Skip to content

Commit 9278abe

Browse files
committed
Forces to measure "standard" fonts
1 parent a081c2d commit 9278abe

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/canvas.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
10301030
continue;
10311031
}
10321032

1033+
var restoreNeeded = false;
10331034
var character = glyph.fontChar;
10341035
var vmetric = glyph.vmetric || defaultVMetrics;
10351036
if (vertical) {
@@ -1055,6 +1056,22 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
10551056
scaledAccentX = scaledX + accent.offset.x / fontSizeScale;
10561057
scaledAccentY = scaledY - accent.offset.y / fontSizeScale;
10571058
}
1059+
1060+
if (font.remeasure && width > 0) {
1061+
// some standard fonts may not have the exact width, trying to
1062+
// rescale per character
1063+
var measuredWidth = ctx.measureText(character).width * 1000 /
1064+
current.fontSize * current.fontSizeScale;
1065+
var characterScaleX = width / measuredWidth;
1066+
restoreNeeded = true;
1067+
ctx.save();
1068+
ctx.scale(characterScaleX, 1);
1069+
scaledX /= characterScaleX;
1070+
if (accent) {
1071+
scaledAccentX /= characterScaleX;
1072+
}
1073+
}
1074+
10581075
switch (textRenderingMode) {
10591076
default: // other unsupported rendering modes
10601077
case TextRenderingMode.FILL:
@@ -1096,6 +1113,10 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
10961113
x += charWidth;
10971114

10981115
canvasWidth += charWidth;
1116+
1117+
if (restoreNeeded) {
1118+
ctx.restore();
1119+
}
10991120
}
11001121
if (vertical) {
11011122
current.y -= x * textHScale;

src/fonts.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,6 +2443,9 @@ var Font = (function FontClosure() {
24432443
// name ArialBlack for example will be replaced by Helvetica.
24442444
this.black = (name.search(/Black/g) != -1);
24452445

2446+
// if at least one width is present, remeasure all chars when exists
2447+
this.remeasure = Object.keys(this.widths).length > 0;
2448+
24462449
this.encoding = properties.baseEncoding;
24472450
this.noUnicodeAdaptation = true;
24482451
this.loadedName = fontName.split('-')[0];
@@ -4656,6 +4659,7 @@ var Font = (function FontClosure() {
46564659
}
46574660
fontCharCode = this.toFontChar[charcode] || charcode;
46584661
break;
4662+
case 'MMType1': // XXX at the moment only "standard" fonts are supported
46594663
case 'Type1':
46604664
var glyphName = this.differences[charcode] || this.encoding[charcode];
46614665
if (!isNum(width))

0 commit comments

Comments
 (0)