Skip to content

Commit db30cc6

Browse files
committed
Start converting CFF class to use strings instead of arrays
1 parent 5cbc687 commit db30cc6

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

fonts.js

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,42 +1483,47 @@ CFF.prototype = {
14831483
// If there is no object, just create an array saying that with another
14841484
// offset byte.
14851485
if (count == 0)
1486-
return [0x00, 0x00, 0x00];
1486+
return "\x00\x00\x00";
14871487

1488-
var data = [];
1488+
var data = "";
14891489
var bytes = FontsUtils.integerToBytes(count, 2);
14901490
for (var i = 0; i < bytes.length; i++)
1491-
data.push(bytes[i]);
1491+
data += String.fromCharCode(bytes[i]);
14921492

14931493
// Next byte contains the offset size use to reference object in the file
14941494
// Actually we're using 0x04 to be sure to be able to store everything
14951495
// without thinking of it while coding.
1496-
data.push(0x04);
1496+
data += "\x04";
14971497

14981498
// Add another offset after this one because we need a new offset
14991499
var relativeOffset = 1;
15001500
for (var i = 0; i < count + 1; i++) {
15011501
var bytes = FontsUtils.integerToBytes(relativeOffset, 4);
15021502
for (var j = 0; j < bytes.length; j++)
1503-
data.push(bytes[j]);
1503+
data += String.fromCharCode(bytes[j]);
15041504

15051505
if (objects[i])
15061506
relativeOffset += objects[i].length;
15071507
}
15081508

15091509
for (var i =0; i < count; i++) {
15101510
for (var j = 0; j < objects[i].length; j++)
1511-
data.push(isByte ? objects[i][j] : objects[i].charCodeAt(j));
1511+
data += isByte ? String.fromCharCode(objects[i][j]) : objects[i][j];
15121512
}
15131513
return data;
15141514
},
15151515

15161516
encodeNumber: function cff_encodeNumber(value) {
1517-
var x = 0;
15181517
if (value >= -32768 && value <= 32767) {
1519-
return [ 28, value >> 8, value & 0xFF ];
1518+
return "\x1c" +
1519+
String.fromCharCode(value >> 8) +
1520+
String.fromCharCode(value & 0xFF);
15201521
} else if (value >= (-2147483647-1) && value <= 2147483647) {
1521-
return [ 0xFF, value >> 24, Value >> 16, value >> 8, value & 0xFF ];
1522+
return "\xff" +
1523+
String.fromCharCode(value >> 24) +
1524+
String.fromCharCode(value >> 16) +
1525+
String.fromCharCode(value >> 8) +
1526+
String.fromCharCode(value & 0xFF);
15221527
}
15231528
error("Value: " + value + " is not allowed");
15241529
return null;
@@ -1626,16 +1631,24 @@ CFF.prototype = {
16261631
},
16271632

16281633
wrap: function wrap(name, glyphs, charstrings, subrs, properties) {
1634+
function stringToArray(str) {
1635+
var array = [];
1636+
for (var i = 0; i < str.length; ++i)
1637+
array[i] = str.charCodeAt(i);
1638+
1639+
return array;
1640+
};
1641+
16291642
var cff = new Uint8Array(kMaxFontFileSize);
16301643
var currentOffset = 0;
16311644

16321645
// Font header (major version, minor version, header size, offset size)
1633-
var header = [0x01, 0x00, 0x04, 0x04];
1646+
var header = "\x01\x00\x04\x04";
16341647
currentOffset += header.length;
1635-
cff.set(header);
1648+
cff.set(stringToArray(header));
16361649

16371650
// Names Index
1638-
var nameIndex = this.createCFFIndexHeader([name]);
1651+
var nameIndex = stringToArray(this.createCFFIndexHeader([name]));
16391652
cff.set(nameIndex, currentOffset);
16401653
currentOffset += nameIndex.length;
16411654

@@ -1649,11 +1662,11 @@ CFF.prototype = {
16491662
var weight = "";
16501663
var strings = [version, notice, fullName,
16511664
familyName, weight];
1652-
var stringsIndex = this.createCFFIndexHeader(strings);
1665+
var stringsIndex = stringToArray(this.createCFFIndexHeader(strings));
16531666
var stringsDataLength = stringsIndex.length;
16541667

16551668
// Create the global subroutines index
1656-
var globalSubrsIndex = this.createCFFIndexHeader([]);
1669+
var globalSubrsIndex = stringToArray(this.createCFFIndexHeader([]));
16571670

16581671
// Fill the charset header (first byte is the encoding)
16591672
var charset = [0x00];
@@ -1668,7 +1681,7 @@ CFF.prototype = {
16681681
charset.push(bytes[1]);
16691682
}
16701683

1671-
var charstringsIndex = this.createCFFIndexHeader([[0x8B, 0x0E]].concat(glyphs), true);
1684+
var charstringsIndex = stringToArray(this.createCFFIndexHeader([[0x8B, 0x0E]].concat(glyphs), true));
16721685

16731686
//Top Dict Index
16741687
var topDictIndex = [
@@ -1682,25 +1695,25 @@ CFF.prototype = {
16821695

16831696
var fontBBox = properties.bbox;
16841697
for (var i = 0; i < fontBBox.length; i++)
1685-
topDictIndex = topDictIndex.concat(this.encodeNumber(fontBBox[i]));
1698+
topDictIndex = topDictIndex.concat(stringToArray(this.encodeNumber(fontBBox[i])));
16861699
topDictIndex.push(5) // FontBBox;
16871700

16881701
var charsetOffset = currentOffset +
16891702
(topDictIndex.length + (4 + 4 + 4 + 7)) +
16901703
stringsIndex.length +
16911704
globalSubrsIndex.length;
1692-
topDictIndex = topDictIndex.concat(this.encodeNumber(charsetOffset));
1705+
topDictIndex = topDictIndex.concat(stringToArray(this.encodeNumber(charsetOffset)));
16931706
topDictIndex.push(15); // charset
16941707

16951708
topDictIndex = topDictIndex.concat([28, 0, 0, 16]) // Encoding
16961709

16971710
var charstringsOffset = charsetOffset + (glyphsCount * 2) + 1;
1698-
topDictIndex = topDictIndex.concat(this.encodeNumber(charstringsOffset));
1711+
topDictIndex = topDictIndex.concat(stringToArray(this.encodeNumber(charstringsOffset)));
16991712
topDictIndex.push(17); // charstrings
17001713

17011714
topDictIndex = topDictIndex.concat([28, 0, 55])
17021715
var privateOffset = charstringsOffset + charstringsIndex.length;
1703-
topDictIndex = topDictIndex.concat(this.encodeNumber(privateOffset));
1716+
topDictIndex = topDictIndex.concat(stringToArray(this.encodeNumber(privateOffset)));
17041717
topDictIndex.push(18); // Private
17051718

17061719
var indexes = [
@@ -1716,7 +1729,7 @@ CFF.prototype = {
17161729
}
17171730

17181731
// Private Data
1719-
var defaultWidth = this.encodeNumber(0);
1732+
var defaultWidth = stringToArray(this.encodeNumber(0));
17201733
var privateData = [].concat(
17211734
defaultWidth, [20],
17221735
[139, 21], // nominalWidth
@@ -1736,7 +1749,7 @@ CFF.prototype = {
17361749

17371750

17381751
// Local subrs
1739-
var subrsData = this.createCFFIndexHeader(subrs, true);
1752+
var subrsData = stringToArray(this.createCFFIndexHeader(subrs, true));
17401753
cff.set(subrsData, currentOffset);
17411754
currentOffset += subrsData.length;
17421755

0 commit comments

Comments
 (0)