Merge pull request #1051 from notmasteryet/fonts-3

Using the encoding instead of predefined  MacRoman in TrueType fonts;
This commit is contained in:
Brendan Dahl 2012-01-11 10:38:36 -08:00
commit be2f973be8
3 changed files with 47 additions and 15 deletions

View File

@ -557,7 +557,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
(fontObj.bold ? 'bold' : 'normal');
var italic = fontObj.italic ? 'italic' : 'normal';
var serif = fontObj.serif ? 'serif' : 'sans-serif';
var serif = fontObj.isSerifFont ? 'serif' : 'sans-serif';
var typeface = '"' + name + '", ' + serif;
var rule = italic + ' ' + bold + ' ' + size + 'px ' + typeface;
this.ctx.font = rule;

View File

@ -758,10 +758,17 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
baseFontName = baseFontName.name.replace(/[,_]/g, '-');
var metrics = this.getBaseFontMetrics(baseFontName);
// Simulating descriptor flags attribute
var fontNameWoStyle = baseFontName.split('-')[0];
var flags = (serifFonts[fontNameWoStyle] ||
(fontNameWoStyle.search(/serif/gi) != -1) ? 2 : 0) |
(symbolsFonts[fontNameWoStyle] ? 4 : 32);
var properties = {
type: type.name,
widths: metrics.widths,
defaultWidth: metrics.defaultWidth,
flags: flags,
firstChar: 0,
lastChar: maxCharIndex
};

View File

@ -386,6 +386,10 @@ var serifFonts = {
'Wide Latin': true, 'Windsor': true, 'XITS': true
};
var symbolsFonts = {
'Dingbats': true, 'Symbol': true, 'ZapfDingbats': true
};
var FontLoader = {
listeningForFontLoad: false,
@ -742,7 +746,8 @@ var Font = (function FontClosure() {
var names = name.split('+');
names = names.length > 1 ? names[1] : names[0];
names = names.split(/[-,_]/g)[0];
this.serif = serifFonts[names] || (name.search(/serif/gi) != -1);
this.isSerifFont = !!(properties.flags & 2);
this.isSymbolicFont = !!(properties.flags & 4);
var type = properties.type;
this.type = type;
@ -750,7 +755,7 @@ var Font = (function FontClosure() {
// If the font is to be ignored, register it like an already loaded font
// to avoid the cost of waiting for it be be loaded by the platform.
if (properties.ignore) {
this.loadedName = this.serif ? 'serif' : 'sans-serif';
this.loadedName = this.isSerifFont ? 'serif' : 'sans-serif';
this.loading = false;
return;
}
@ -830,7 +835,6 @@ var Font = (function FontClosure() {
this.widthMultiplier = !properties.fontMatrix ? 1.0 :
1.0 / properties.fontMatrix[0];
this.encoding = properties.baseEncoding;
this.hasShortCmap = properties.hasShortCmap;
this.loadedName = getUniqueName();
this.loading = true;
};
@ -1800,7 +1804,32 @@ var Font = (function FontClosure() {
this.useToUnicode = true;
}
}
properties.hasShortCmap = hasShortCmap;
if (hasShortCmap && this.hasEncoding && !this.isSymbolicFont) {
// Re-encode short map encoding to unicode -- that simplifies the
// resolution of MacRoman encoded glyphs logic for TrueType fonts:
// copying all characters to private use area, all mapping all known
// glyphs to the unicodes. The glyphs and ids arrays will grow.
var usedUnicodes = [];
for (var i = 0, ii = glyphs.length; i < ii; i++) {
var code = glyphs[i].unicode;
glyphs[i].unicode += kCmapGlyphOffset;
var glyphName = properties.baseEncoding[code];
if (glyphName in GlyphsUnicode) {
var unicode = GlyphsUnicode[glyphName];
if (unicode in usedUnicodes)
continue;
usedUnicodes[unicode] = true;
glyphs.push({
unicode: unicode,
code: glyphs[i].code
});
ids.push(ids[i]);
}
}
}
// remove glyph references outside range of avaialable glyphs
for (var i = 0, ii = ids.length; i < ii; i++) {
@ -2184,19 +2213,15 @@ var Font = (function FontClosure() {
unicode = GlyphsUnicode[glyphName] || charcode;
break;
}
if (!this.hasEncoding) {
if (!this.hasEncoding || this.isSymbolicFont) {
unicode = this.useToUnicode ? this.toUnicode[charcode] : charcode;
break;
}
if (this.hasShortCmap && false) {
var j = Encodings.MacRomanEncoding.indexOf(glyphName);
unicode = j >= 0 ? j :
this.glyphNameMap[glyphName];
} else {
unicode = glyphName in GlyphsUnicode ?
GlyphsUnicode[glyphName] :
this.glyphNameMap[glyphName];
}
// MacRoman encoding address by re-encoding the cmap table
unicode = glyphName in GlyphsUnicode ?
GlyphsUnicode[glyphName] :
this.glyphNameMap[glyphName];
break;
default:
warn('Unsupported font type: ' + this.type);