this.data = data;
this.type = properties.type;
this.textMatrix = properties.textMatrix;
+ this.defaultWidth = properties.defaultWidth;
this.loadedName = getUniqueName();
this.composite = properties.composite;
this.loading = true;
charsToGlyphs: function fonts_chars2Glyphs(chars) {
var charsCache = this.charsCache;
- var str;
+ var glyphs;
// if we translated this string before, just grab it from the cache
if (charsCache) {
- str = charsCache[chars];
- if (str)
- return str;
+ glyphs = charsCache[chars];
+ if (glyphs)
+ return glyphs;
}
// lazily create the translation cache
var encoding = this.encoding;
if (!encoding)
return chars;
- var glyphs = [];
+
+ glyphs = [];
if (this.composite) {
// composite fonts have multi-byte strings convert the string from
// loop should never end on the last byte
for (var i = 0; i < length; i++) {
var charcode = int16([chars.charCodeAt(i++), chars.charCodeAt(i)]);
- var unicode = encoding[charcode];
- if ('undefined' == typeof(unicode)) {
+ var glyph = encoding[charcode];
+ if ('undefined' == typeof(glyph)) {
warn('Unencoded charcode ' + charcode);
- unicode = { unicode: charcode };
+ glyph = { unicode: charcode };
}
- glyphs.push(unicode);
+ glyphs.push(glyph);
+ // placing null after each word break charcode (ASCII SPACE)
+ if (charcode == 0x20)
+ glyphs.push(null);
}
}
else {
for (var i = 0; i < chars.length; ++i) {
var charcode = chars.charCodeAt(i);
- var unicode = encoding[charcode];
- if ('undefined' == typeof(unicode)) {
+ var glyph = encoding[charcode];
+ if ('undefined' == typeof(glyph)) {
warn('Unencoded charcode ' + charcode);
- unicode = { unicode: charcode };
+ glyph = { unicode: charcode };
}
- glyphs.push(unicode);
+ glyphs.push(glyph);
+ if (charcode == 0x20)
+ glyphs.push(null);
}
}
};
}
} else if (type == 'CIDFontType0') {
- encoding = xref.fetchIfRef(dict.get('Encoding'));
if (IsName(encoding)) {
// Encoding is a predefined CMap
if (encoding.name == 'Identity-H') {
}
}
- // TODO implement default widths for standard fonts metrics
- var defaultWidth = 1000;
+ var defaultWidth = 0;
var widths = Metrics[stdFontMap[baseFontName] || baseFontName];
if (IsNum(widths)) {
defaultWidth = widths;
var width = 0;
for (var i = 0; i < glyphs.length; i++) {
var glyph = glyphs[i];
+ if (glyph === null) {
+ // word break
+ width += wordSpacing;
+ continue;
+ }
+
var unicode = glyph.unicode;
var char = unicode >= 0x10000 ?
String.fromCharCode(0xD800 | ((unicode - 0x10000) >> 10),
var charWidth = (glyph.width || defaultCharWidth) * fontSize * 0.001;
charWidth += charSpacing;
- if (unicode == 32)
- charWidth += wordSpacing;
- ctx.fillText(char, 0, 0);
- ctx.translate(charWidth, 0);
+ ctx.fillText(char, width, 0);
width += charWidth;
}
current.x += width;