Skip to content

Commit

Permalink
Merge pull request #618 from bstell/master
Browse files Browse the repository at this point in the history
Move the getCmapMapping function to the end of the file
  • Loading branch information
bstell committed Apr 21, 2015
2 parents d1c2d1b + 477a85e commit bc46b52
Showing 1 changed file with 84 additions and 83 deletions.
167 changes: 84 additions & 83 deletions run_time/src/gae_server/www/js/tachyfont/incrementalfontutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,88 +58,6 @@ tachyfont.IncrementalFontUtils.STYLESHEET_ID =
'Incremental\u00A0Font\u00A0Utils';


/**
* Get the character to glyphId mapping.
* @param {DataView} baseFont Base font.
* @param {Object} headerInfo Header information.
* @return {Object.<string, tachyfont.CharCmapInfo>} Map of chars to glyphId,
* format4Seg, format12Seg.
*/
tachyfont.IncrementalFontUtils.getCmapMapping = function(headerInfo) {
var cmapMapping = {};
var charCmapInfo;
// Parse format 4.
if (headerInfo.compact_gos.cmap4) {
var segments = headerInfo.compact_gos.cmap4.segments;
var glyphIdArray = headerInfo.compact_gos.cmap4.glyphIdArray;
var glyphIdIndex = 0;
for (var i = 0; i < segments.length; i++) {
var startCode = segments[i][0];
var endCode = segments[i][1];
var idDelta = segments[i][2];
var idRangeOffset = segments[i][3];
var length = endCode - startCode + 1;
for (var j = 0; j < length; j++) {
var code = startCode + j;
var glyphId = null;
if (idRangeOffset == 0) {
glyphId = (code + idDelta) % 65536;
} else {
if (goog.DEBUG) {
// TODO(bstell): verify this code.
debugger;
}
glyphId = glyphIdArray[glyphIdIndex++];
if (glyphId == 0) {
// This code is not mapped in the font.
if (goog.DEBUG) {
// TODO(bstell): verify this code.
debugger;
}
continue;
}
}
charCmapInfo = new tachyfont.CharCmapInfo(glyphId, i, null);
cmapMapping[code] = charCmapInfo;
}
}
}


if (!headerInfo.compact_gos.cmap12) {
if (goog.Debug) {
debugger; // TODO(bstell): need to handle this.
}
return cmapMapping;
}
var n12Groups = headerInfo.cmap12.nGroups;
var segments = headerInfo.compact_gos.cmap12.segments;
for (var i = 0; i < n12Groups; i++) {
var startCode = segments[i][0];
var length = segments[i][1];
var startGlyphId = segments[i][2];
for (var j = 0; j < length; j++) {
var code = startCode + j;
charCmapInfo = cmapMapping[code];
var glyphId = startGlyphId + j;
if (goog.DEBUG) {
if (charCmapInfo) {
goog.asserts.assert(charCmapInfo.glyphId == glyphId,
'format 4/12 glyphId mismatch');
}
}
if (!charCmapInfo) {
charCmapInfo = new tachyfont.CharCmapInfo(glyphId, null, null);
cmapMapping[code] = charCmapInfo;
}
charCmapInfo.format12Seg = i;
}
}
return cmapMapping;
};



/**
* Parses base font header, set properties.
* @param {DataView} baseFont Base font with header.
Expand Down Expand Up @@ -170,7 +88,8 @@ tachyfont.IncrementalFontUtils.writeCharsetFormat2 =
* @param {DataView} baseFont Base font as DataView
* @return {DataView} Sanitized base font
*/
tachyfont.IncrementalFontUtils.sanitizeBaseFont = function(headerInfo, baseFont) {
tachyfont.IncrementalFontUtils.sanitizeBaseFont =
function(headerInfo, baseFont) {

if (headerInfo.isTtf) {
headerInfo.dirty = true;
Expand Down Expand Up @@ -399,3 +318,85 @@ tachyfont.IncrementalFontUtils.setCssFontRule =
tachyfont.IncrementalFontUtils.deleteCssRule(ruleToDelete, sheet);
sheet.insertRule(rule_str, sheet.cssRules.length);
};


/**
* Get the character to glyphId mapping.
* @param {Object} headerInfo Header information.
* @return {Object.<string, tachyfont.CharCmapInfo>} Map of chars to glyphId,
* format4Seg, format12Seg.
*/
tachyfont.IncrementalFontUtils.getCmapMapping = function(headerInfo) {
var cmapMapping = {};
var charCmapInfo;
// Parse format 4.
if (headerInfo.compact_gos.cmap4) {
var segments = headerInfo.compact_gos.cmap4.segments;
var glyphIdArray = headerInfo.compact_gos.cmap4.glyphIdArray;
var glyphIdIndex = 0;
for (var i = 0; i < segments.length; i++) {
var startCode = segments[i][0];
var endCode = segments[i][1];
var idDelta = segments[i][2];
var idRangeOffset = segments[i][3];
var length = endCode - startCode + 1;
for (var j = 0; j < length; j++) {
var code = startCode + j;
var glyphId = null;
if (idRangeOffset == 0) {
glyphId = (code + idDelta) % 65536;
} else {
if (goog.DEBUG) {
// TODO(bstell): verify this code.
debugger;
}
glyphId = glyphIdArray[glyphIdIndex++];
if (glyphId == 0) {
// This code is not mapped in the font.
if (goog.DEBUG) {
// TODO(bstell): verify this code.
debugger;
}
continue;
}
}
charCmapInfo = new tachyfont.CharCmapInfo(glyphId, i, null);
cmapMapping[code] = charCmapInfo;
}
}
}


if (!headerInfo.compact_gos.cmap12) {
if (goog.Debug) {
debugger; // TODO(bstell): need to handle this.
}
return cmapMapping;
}
var n12Groups = headerInfo.cmap12.nGroups;
var segments = headerInfo.compact_gos.cmap12.segments;
for (var i = 0; i < n12Groups; i++) {
var startCode = segments[i][0];
var length = segments[i][1];
var startGlyphId = segments[i][2];
for (var j = 0; j < length; j++) {
var code = startCode + j;
charCmapInfo = cmapMapping[code];
var glyphId = startGlyphId + j;
if (goog.DEBUG) {
if (charCmapInfo) {
goog.asserts.assert(charCmapInfo.glyphId == glyphId,
'format 4/12 glyphId mismatch');
}
}
if (!charCmapInfo) {
charCmapInfo = new tachyfont.CharCmapInfo(glyphId, null, null);
cmapMapping[code] = charCmapInfo;
}
charCmapInfo.format12Seg = i;
}
}
return cmapMapping;
};


0 comments on commit bc46b52

Please sign in to comment.