|
|
@ -22,7 +22,14 @@ var kMaxWaitForFontFace = 1000; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
var Fonts = (function Fonts() { |
|
|
|
var Fonts = (function Fonts() { |
|
|
|
|
|
|
|
var kScalePrecision = 40; |
|
|
|
var fonts = []; |
|
|
|
var fonts = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!isWorker) { |
|
|
|
|
|
|
|
var ctx = document.createElement('canvas').getContext('2d'); |
|
|
|
|
|
|
|
ctx.scale(1 / kScalePrecision, 1); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var fontCount = 0; |
|
|
|
var fontCount = 0; |
|
|
|
|
|
|
|
|
|
|
|
function FontInfo(name, data, properties) { |
|
|
|
function FontInfo(name, data, properties) { |
|
|
@ -35,6 +42,7 @@ var Fonts = (function Fonts() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var current; |
|
|
|
var current; |
|
|
|
|
|
|
|
var measureCache; |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
return { |
|
|
|
registerFont: function fonts_registerFont(fontName, data, properties) { |
|
|
|
registerFont: function fonts_registerFont(fontName, data, properties) { |
|
|
@ -49,6 +57,28 @@ var Fonts = (function Fonts() { |
|
|
|
}, |
|
|
|
}, |
|
|
|
lookupById: function fonts_lookupById(id) { |
|
|
|
lookupById: function fonts_lookupById(id) { |
|
|
|
return fonts[id]; |
|
|
|
return fonts[id]; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
setActive: function fonts_setActive(fontName, fontObj, size) { |
|
|
|
|
|
|
|
// |current| can be null is fontName is a built-in font
|
|
|
|
|
|
|
|
// (e.g. "sans-serif")
|
|
|
|
|
|
|
|
if (fontObj && (current = fonts[fontObj.id])) { |
|
|
|
|
|
|
|
var sizes = current.sizes; |
|
|
|
|
|
|
|
if (!(measureCache = sizes[size])) |
|
|
|
|
|
|
|
measureCache = sizes[size] = Object.create(null); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
measureCache = null |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ctx.font = (size * kScalePrecision) + 'px "' + fontName + '"'; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
measureText: function fonts_measureText(text) { |
|
|
|
|
|
|
|
var width; |
|
|
|
|
|
|
|
if (measureCache && (width = measureCache[text])) |
|
|
|
|
|
|
|
return width; |
|
|
|
|
|
|
|
width = ctx.measureText(text).width / kScalePrecision; |
|
|
|
|
|
|
|
if (measureCache) |
|
|
|
|
|
|
|
measureCache[text] = width; |
|
|
|
|
|
|
|
return width; |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
})(); |
|
|
|
})(); |
|
|
@ -1080,7 +1110,7 @@ var Font = (function() { |
|
|
|
return rule; |
|
|
|
return rule; |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
charsToUnicode: function fonts_charsToUnicode(chars) { |
|
|
|
charsToUnicode: function fonts_chars2Unicode(chars) { |
|
|
|
var charsCache = this.charsCache; |
|
|
|
var charsCache = this.charsCache; |
|
|
|
|
|
|
|
|
|
|
|
// if we translated this string before, just grab it from the cache
|
|
|
|
// if we translated this string before, just grab it from the cache
|
|
|
|