From 52e1631030ce6494fca5f3818bc4030172e4acdf Mon Sep 17 00:00:00 2001 From: Andreas Gal Date: Mon, 20 Jun 2011 03:33:52 -0400 Subject: [PATCH] use Array.sort to calculate glyph ranges instead of large typed arrays --- fonts.js | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/fonts.js b/fonts.js index 2ee8a0d3c..49a78cb53 100644 --- a/fonts.js +++ b/fonts.js @@ -8,11 +8,6 @@ */ var kMaxFontFileSize = 40000; -/** - * Maximum number of glyphs per font. -*/ -var kMaxGlyphsCount = 65526; - /** * Maximum time to wait for a font to be loaded by @font-face */ @@ -310,24 +305,30 @@ var Font = (function () { aOffsets.currentOffset += tableEntry.length; aOffsets.virtualOffset += aData.length; } - - function createCMAPTable(aGlyphs) { - var characters = new Uint16Array(kMaxGlyphsCount); - for (var i = 0; i < aGlyphs.length; i++) - characters[aGlyphs[i].unicode] = i + 1; - // Separate the glyphs into continuous range of codes, aka segment. + function getRanges(glyphs) { + // Array.sort() sorts by characters, not numerically, so convert to an + // array of characters. + var codes = []; + var length = glyphs.length; + for (var n = 0; n < length; ++n) + codes.push(String.fromCharCode(glyphs[n].unicode)) + codes.sort(); + var ranges = []; - var range = []; - var count = characters.length; - for (var i = 0; i < count; i++) { - if (characters[i]) { - range.push(i); - } else if (range.length) { - ranges.push(range.slice()); - range = []; - } + for (var n = 0; n < length; ) { + var range = []; + do { + var current = codes[n++].charCodeAt(0); + range.push(current); + } while (n < length && current + 1 == codes[n].charCodeAt(0)); + ranges.push(range); } + return ranges; + } + + function createCMAPTable(aGlyphs) { + var ranges = getRanges(aGlyphs); // The size in bytes of the header is equal to the size of the // different fields * length of a short + (size of the 4 parallels arrays