Browse Source

Merge pull request #466 from notmasteryet/charstoglyphs

Refactoring charsToUnicode into charsToGlyphs
vingtetun 14 years ago
parent
commit
7a81e79138
  1. 54
      fonts.js
  2. 46
      pdf.js
  3. 1
      test/pdfs/fips197.pdf.link
  4. 6
      test/test_manifest.json

54
fonts.js

@ -440,6 +440,7 @@ var Font = (function Font() {
// name ArialBlack for example will be replaced by Helvetica. // name ArialBlack for example will be replaced by Helvetica.
this.black = (name.search(/Black/g) != -1); this.black = (name.search(/Black/g) != -1);
this.defaultWidth = properties.defaultWidth;
this.loadedName = fontName.split('-')[0]; this.loadedName = fontName.split('-')[0];
this.loading = false; this.loading = false;
return; return;
@ -476,6 +477,7 @@ var Font = (function Font() {
this.data = data; this.data = data;
this.type = properties.type; this.type = properties.type;
this.textMatrix = properties.textMatrix; this.textMatrix = properties.textMatrix;
this.defaultWidth = properties.defaultWidth;
this.loadedName = getUniqueName(); this.loadedName = getUniqueName();
this.composite = properties.composite; this.composite = properties.composite;
this.loading = true; this.loading = true;
@ -1424,15 +1426,15 @@ var Font = (function Font() {
return rule; return rule;
}, },
charsToUnicode: function fonts_chars2Unicode(chars) { charsToGlyphs: function fonts_chars2Glyphs(chars) {
var charsCache = this.charsCache; var charsCache = this.charsCache;
var str; var glyphs;
// if we translated this string before, just grab it from the cache // if we translated this string before, just grab it from the cache
if (charsCache) { if (charsCache) {
str = charsCache[chars]; glyphs = charsCache[chars];
if (str) if (glyphs)
return str; return glyphs;
} }
// lazily create the translation cache // lazily create the translation cache
@ -1443,7 +1445,8 @@ var Font = (function Font() {
var encoding = this.encoding; var encoding = this.encoding;
if (!encoding) if (!encoding)
return chars; return chars;
str = '';
glyphs = [];
if (this.composite) { if (this.composite) {
// composite fonts have multi-byte strings convert the string from // composite fonts have multi-byte strings convert the string from
@ -1454,38 +1457,39 @@ var Font = (function Font() {
// loop should never end on the last byte // loop should never end on the last byte
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
var charcode = int16([chars.charCodeAt(i++), chars.charCodeAt(i)]); var charcode = int16([chars.charCodeAt(i++), chars.charCodeAt(i)]);
var unicode = encoding[charcode]; var glyph = encoding[charcode];
if ('undefined' == typeof(unicode)) { if ('undefined' == typeof(glyph)) {
warn('Unencoded charcode ' + charcode); warn('Unencoded charcode ' + charcode);
unicode = charcode; glyph = {
} else { unicode: charcode,
unicode = unicode.unicode; width: this.defaultWidth
};
} }
str += String.fromCharCode(unicode); glyphs.push(glyph);
// placing null after each word break charcode (ASCII SPACE)
if (charcode == 0x20)
glyphs.push(null);
} }
} }
else { else {
for (var i = 0; i < chars.length; ++i) { for (var i = 0; i < chars.length; ++i) {
var charcode = chars.charCodeAt(i); var charcode = chars.charCodeAt(i);
var unicode = encoding[charcode]; var glyph = encoding[charcode];
if ('undefined' == typeof(unicode)) { if ('undefined' == typeof(glyph)) {
warn('Unencoded charcode ' + charcode); warn('Unencoded charcode ' + charcode);
unicode = charcode; glyph = {
} else { unicode: charcode,
unicode = unicode.unicode; width: this.defaultWidth
} };
// Handle surrogate pairs
if (unicode > 0xFFFF) {
str += String.fromCharCode(unicode & 0xFFFF);
unicode >>= 16;
} }
str += String.fromCharCode(unicode); glyphs.push(glyph);
if (charcode == 0x20)
glyphs.push(null);
} }
} }
// Enter the translated string into the cache // Enter the translated string into the cache
return (charsCache[chars] = str); return (charsCache[chars] = glyphs);
} }
}; };

46
pdf.js

@ -4301,7 +4301,6 @@ var PartialEvaluator = (function() {
}; };
} }
} else if (type == 'CIDFontType0') { } else if (type == 'CIDFontType0') {
encoding = xref.fetchIfRef(dict.get('Encoding'));
if (IsName(encoding)) { if (IsName(encoding)) {
// Encoding is a predefined CMap // Encoding is a predefined CMap
if (encoding.name == 'Identity-H') { if (encoding.name == 'Identity-H') {
@ -4362,6 +4361,7 @@ var PartialEvaluator = (function() {
// merge in the differences // merge in the differences
var firstChar = properties.firstChar; var firstChar = properties.firstChar;
var lastChar = properties.lastChar; var lastChar = properties.lastChar;
var widths = properties.widths || [];
var glyphs = {}; var glyphs = {};
for (var i = firstChar; i <= lastChar; i++) { for (var i = firstChar; i <= lastChar; i++) {
var glyph = differences[i]; var glyph = differences[i];
@ -4372,7 +4372,7 @@ var PartialEvaluator = (function() {
continue; continue;
} }
var index = GlyphsUnicode[glyph] || i; var index = GlyphsUnicode[glyph] || i;
var width = properties.widths[i] || properties.widths[glyph]; var width = widths[i] || widths[glyph];
map[i] = { map[i] = {
unicode: index, unicode: index,
width: IsNum(width) ? width : properties.defaultWidth width: IsNum(width) ? width : properties.defaultWidth
@ -4543,7 +4543,7 @@ var PartialEvaluator = (function() {
type: type.name, type: type.name,
encoding: map, encoding: map,
differences: [], differences: [],
widths: widths, widths: widths || {},
defaultWidth: defaultWidth, defaultWidth: defaultWidth,
firstChar: 0, firstChar: 0,
lastChar: 256 lastChar: 256
@ -4934,7 +4934,7 @@ var CanvasGraphics = (function() {
font = font.get(fontRef.name); font = font.get(fontRef.name);
font = this.xref.fetchIfRef(font); font = this.xref.fetchIfRef(font);
if (!font) if (!font)
return; error('Referenced font is not found');
var fontObj = font.fontObj; var fontObj = font.fontObj;
this.current.font = fontObj; this.current.font = fontObj;
@ -4986,22 +4986,15 @@ var CanvasGraphics = (function() {
showText: function(text) { showText: function(text) {
var ctx = this.ctx; var ctx = this.ctx;
var current = this.current; var current = this.current;
var originalText = text; var font = current.font;
ctx.save(); ctx.save();
ctx.transform.apply(ctx, current.textMatrix); ctx.transform.apply(ctx, current.textMatrix);
ctx.scale(1, -1); ctx.scale(1, -1);
ctx.translate(current.x, -1 * current.y); ctx.translate(current.x, -1 * current.y);
ctx.transform.apply(ctx, font.textMatrix || IDENTITY_MATRIX);
var font = current.font; var glyphs = font.charsToGlyphs(text);
if (font) {
ctx.transform.apply(ctx, font.textMatrix || IDENTITY_MATRIX);
text = font.charsToUnicode(text);
}
var composite = font.composite;
var encoding = font.encoding;
var fontSize = current.fontSize; var fontSize = current.fontSize;
var charSpacing = current.charSpacing; var charSpacing = current.charSpacing;
var wordSpacing = current.wordSpacing; var wordSpacing = current.wordSpacing;
@ -5009,22 +5002,23 @@ var CanvasGraphics = (function() {
ctx.scale(1 / textHScale, 1); ctx.scale(1 / textHScale, 1);
var width = 0; var width = 0;
for (var i = 0; i < text.length; i++) { for (var i = 0; i < glyphs.length; i++) {
if (composite) { var glyph = glyphs[i];
var position = i * 2 + 1; if (glyph === null) {
var charcode = (originalText.charCodeAt(position - 1) << 8) + // word break
originalText.charCodeAt(position); width += wordSpacing;
} else { continue;
var charcode = originalText.charCodeAt(i);
} }
var charWidth = font.encoding[charcode].width * fontSize * 0.001; 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; charWidth += charSpacing;
if (charcode == 32)
charWidth += wordSpacing;
ctx.fillText(text.charAt(i), 0, 0); ctx.fillText(char, width, 0);
ctx.translate(charWidth, 0);
width += charWidth; width += charWidth;
} }
current.x += width; current.x += width;

1
test/pdfs/fips197.pdf.link

@ -0,0 +1 @@
http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf

6
test/test_manifest.json

@ -127,5 +127,11 @@
"link": true, "link": true,
"rounds": 1, "rounds": 1,
"type": "eq" "type": "eq"
},
{ "id": "fips197",
"file": "pdfs/fips197.pdf",
"link": true,
"rounds": 1,
"type": "load"
} }
] ]

Loading…
Cancel
Save