return rule;
},
- charsToUnicode: function fonts_chars2Unicode(chars) {
+ charsToGlyphs: function fonts_chars2Glyphs(chars) {
var charsCache = this.charsCache;
var str;
var encoding = this.encoding;
if (!encoding)
return chars;
- str = '';
+ var glyphs = [];
if (this.composite) {
// composite fonts have multi-byte strings convert the string from
var unicode = encoding[charcode];
if ('undefined' == typeof(unicode)) {
warn('Unencoded charcode ' + charcode);
- unicode = charcode;
- } else {
- unicode = unicode.unicode;
+ unicode = { unicode: charcode };
}
- str += String.fromCharCode(unicode);
+ glyphs.push(unicode);
}
}
else {
var unicode = encoding[charcode];
if ('undefined' == typeof(unicode)) {
warn('Unencoded charcode ' + charcode);
- unicode = charcode;
- } else {
- unicode = unicode.unicode;
- }
-
- // Handle surrogate pairs
- if (unicode > 0xFFFF) {
- str += String.fromCharCode(unicode & 0xFFFF);
- unicode >>= 16;
+ unicode = { unicode: charcode };
}
- str += String.fromCharCode(unicode);
+ glyphs.push(unicode);
}
}
// Enter the translated string into the cache
- return charsCache[chars] = str;
+ return charsCache[chars] = glyphs;
}
};
showText: function(text) {
var ctx = this.ctx;
var current = this.current;
- var originalText = text;
ctx.save();
ctx.transform.apply(ctx, current.textMatrix);
ctx.translate(current.x, -1 * current.y);
var font = current.font;
+ var glyphs = [];
if (font) {
ctx.transform.apply(ctx, font.textMatrix || IDENTITY_MATRIX);
- text = font.charsToUnicode(text);
+ glyphs = font.charsToGlyphs(text);
+ } else {
+ // fallback to simple glyphs
+ glyphs = [];
+ for (var i = 0; i < text.length; ++i)
+ glyphs.push({unicode: text.charCodeAt(i)});
}
var composite = font.composite;
ctx.scale(1 / textHScale, 1);
var width = 0;
- for (var i = 0; i < text.length; i++) {
- if (composite) {
- var position = i * 2 + 1;
- var charcode = (originalText.charCodeAt(position - 1) << 8) +
- originalText.charCodeAt(position);
- } else {
- var charcode = originalText.charCodeAt(i);
- }
-
- var charWidth = font.encoding[charcode].width * fontSize * 0.001;
+ for (var i = 0; i < glyphs.length; i++) {
+ var glyph = glyphs[i];
+ var unicode = glyph.unicode;
+ var char = unicode >= 0x10000 ?
+ String.fromCharCode(0xD800 | ((unicode - 0x10000) >> 10),
+ 0xDC00 | (unicode & 0x3FF)) : String.fromCharCode(unicode);
+
+ var charWidth = glyph.width * fontSize * 0.001;
charWidth += charSpacing;
- if (charcode == 32)
+ if (unicode == 32)
charWidth += wordSpacing;
- ctx.fillText(text.charAt(i), 0, 0);
+ ctx.fillText(char, 0, 0);
ctx.translate(charWidth, 0);
width += charWidth;
}