ctx.font = rule;
current = font;
},
- measureText: function fonts_measureText(text) {
+ measureText: function fonts_measureText(text, encoding, size) {
var width;
if (measureCache && (width = measureCache[text]))
return width;
- width = ctx.measureText(text).width / kScalePrecision;
+
+ try {
+ width = 0.0;
+ for (var i = 0; i < text.length; i++) {
+ var charWidth = encoding[text.charCodeAt(i)].width;
+ width += parseFloat(charWidth);
+ }
+ width = width * size / 1000;
+ } catch(e) {
+ width = ctx.measureText(text).width / kScalePrecision;
+ }
if (measureCache)
measureCache[text] = width;
return width;
(fontName.indexOf('Italic') != -1);
// Use 'name' instead of 'fontName' here because the original
- // name ArialNarrow for example will be replaced by Helvetica.
- this.narrow = (name.indexOf("Narrow") != -1)
+ // name ArialBlack for example will be replaced by Helvetica.
this.black = (name.indexOf("Black") != -1)
this.loadedName = fontName.split('-')[0];
var index = firstCode;
for (var j = start; j <= end; j++) {
var code = j - firstCode - 1;
- encoding[index++] = { unicode: glyphs[code].unicode };
+ var mapping = encoding[index + 1] || {};
+ mapping.unicode = glyphs[code].unicode;
+ encoding[index++] = mapping;
}
return cmap.data = createCMapTable(glyphs);
}
}
}
- if (code == -1) {
- var mapping = properties.glyphs[glyph] || {};
+ var mapping = properties.glyphs[glyph] || {};
+ if (code == -1)
index = code = mapping.unicode || index;
- }
- var width = widths[code] || defaultWidth;
+ var width = mapping.width || defaultWidth;
if (code <= 0x1f || (code >= 127 && code <= 255))
code += kCmapGlyphOffset;
var glyphs = {};
for (var i = firstChar; i <= lastChar; i++) {
var glyph = differences[i] || baseEncoding[i];
- if (glyph) {
- var index = GlyphsUnicode[glyph] || i;
- glyphs[glyph] = map[i] = {
- unicode: index,
- width: properties.widths[i - firstChar] || properties.defaultWidth
- };
-
- // If there is no file, the character mapping can't be modified
- // but this is unlikely that there is any standard encoding with
- // chars below 0x1f, so that's fine.
- if (!properties.file)
- continue;
+ var index = GlyphsUnicode[glyph] || i;
+ map[i] = {
+ unicode: index,
+ width: properties.widths[i] || properties.defaultWidth
+ };
- if (index <= 0x1f || (index >= 127 && index <= 255))
- map[i].unicode += kCmapGlyphOffset;
- }
+ if (glyph)
+ glyphs[glyph] = map[i];
+
+ // If there is no file, the character mapping can't be modified
+ // but this is unlikely that there is any standard encoding with
+ // chars below 0x1f, so that's fine.
+ if (!properties.file)
+ continue;
+
+ if (index <= 0x1f || (index >= 127 && index <= 255))
+ map[i].unicode += kCmapGlyphOffset;
}
if (type == 'TrueType' && dict.has('ToUnicode') && differences) {
var endRange = tokens[j + 1];
var code = tokens[j + 2];
while (startRange < endRange) {
- map[startRange] = {
- unicode: code++,
- width: 0
- }
+ var mapping = map[startRange] || {};
+ mapping.unicode = code++;
+ map[startRange] = mapping;
++startRange;
}
}
for (var j = 0; j < tokens.length; j += 2) {
var index = tokens[j];
var code = tokens[j + 1];
- map[index] = {
- unicode: code,
- width: 0
- };
+ var mapping = map[index] || {};
+ mapping.unicode = code;
+ map[index] = mapping;
}
break;
descent: descriptor.get('Descent'),
xHeight: descriptor.get('XHeight'),
capHeight: descriptor.get('CapHeight'),
- defaultWidth: descriptor.get('MissingWidth') || 0,
+ defaultWidth: parseFloat(descriptor.get('MissingWidth')) || 0,
flags: descriptor.get('Flags'),
italicAngle: descriptor.get('ItalicAngle'),
differences: [],
widths: (function() {
var glyphWidths = {};
- for (var i = 0; i <= widths.length; i++)
+ for (var i = 0; i < widths.length; i++)
glyphWidths[firstChar++] = widths[i];
return glyphWidths;
})(),
var scaleFactorX = 1, scaleFactorY = 1;
var font = current.font;
+ var baseText= text;
if (font) {
if (current.fontSize <= kRasterizerMin) {
scaleFactorX = scaleFactorY = kScalePrecision;
text = font.charsToUnicode(text);
}
+ var encoding = current.font.encoding;
+ var size = current.fontSize;
var charSpacing = current.charSpacing;
var wordSpacing = current.wordSpacing;
var textHScale = current.textHScale;
- // This is a poor simulation for Arial Narrow while font-stretch
- // is not implemented (bug 3512)
- if (current.font.narrow) {
- textHScale += 0.2;
- charSpacing -= (0.09 * current.fontSize);
- }
-
if (charSpacing != 0 || wordSpacing != 0 || textHScale != 1) {
scaleFactorX *= textHScale;
ctx.scale(1 / textHScale, 1);
var width = 0;
for (var i = 0, ii = text.length; i < ii; ++i) {
- var c = text.charAt(i);
+ var c = baseText.charAt(i);
ctx.fillText(c, 0, 0);
- var charWidth = FontMeasure.measureText(c) + charSpacing;
+ var charWidth = FontMeasure.measureText(c, encoding, size);
+ charWidth += charSpacing;
if (c.charCodeAt(0) == 32)
charWidth += wordSpacing;
ctx.translate(charWidth * scaleFactorX, 0);
current.x += width;
} else {
ctx.fillText(text, 0, 0);
- current.x += FontMeasure.measureText(text);
+ current.x += FontMeasure.measureText(baseText, encoding, size);
}
this.ctx.restore();