Skip to content

Commit 64f8e79

Browse files
committed
Make windows table header more compatible with windows
1 parent 2334399 commit 64f8e79

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

fonts.js

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ var Font = (function () {
299299

300300
// Wrap the CFF data inside an OTF font file
301301
data = this.convert(name, cff, properties);
302-
writeToFile(data, "/tmp/file." + fontName + "-" + fontCount + ".otf");
303302
break;
304303

305304
case "TrueType":
@@ -373,7 +372,7 @@ var Font = (function () {
373372

374373
// length
375374
var length = data.length;
376-
375+
377376
// Per spec tables must be 4-bytes align so add padding as needed
378377
while (data.length & 3)
379378
data.push(0x00);
@@ -435,7 +434,7 @@ var Font = (function () {
435434
"\x00\x00\x00\x0C" + // start of the table record
436435
"\x00\x04" + // format
437436
string16(headerSize) + // length
438-
"\x00\x00" + // languages
437+
"\x04\x09" + // languages
439438
string16(segCount2) +
440439
string16(searchRange) +
441440
string16(searchEntry) +
@@ -458,7 +457,7 @@ var Font = (function () {
458457
startCount += string16(start);
459458
endCount += string16(end);
460459
idDeltas += string16(delta);
461-
idRangeOffsets += string16(0);
460+
idRangeOffsets += string16(0);
462461

463462
for (var j = 0; j < range.length; j++)
464463
glyphsIds += String.fromCharCode(range[j]);
@@ -479,10 +478,19 @@ var Font = (function () {
479478
var ulUnicodeRange3 = 0;
480479
var ulUnicodeRange4 = 0;
481480

482-
var charset = properties.charset;
481+
var charset = properties.charset;
483482
if (charset && charset.length) {
483+
var firstCharIndex = null;
484+
var lastCharIndex = 0;
485+
484486
for (var i = 1; i < charset.length; i++) {
485-
var position = getUnicodeRangeFor(GlyphsUnicode[charset[i]]);
487+
var code = GlyphsUnicode[charset[i]];
488+
if (code < firstCharIndex || !firstCharIndex)
489+
firstCharIndex = code;
490+
if (code > lastCharIndex)
491+
lastCharIndex = code;
492+
493+
var position = getUnicodeRangeFor(code);
486494
if (position < 32) {
487495
ulUnicodeRange1 |= 1 << position;
488496
} else if (position < 64) {
@@ -520,8 +528,8 @@ var Font = (function () {
520528
string32(ulUnicodeRange4) + // ulUnicodeRange4 (Bits 96-127)
521529
"\x2A\x32\x31\x2A" + // achVendID
522530
"\x00\x00" + // fsSelection
523-
string16(properties.firstChar) + // usFirstCharIndex
524-
string16(properties.lastChar) + // usLastCharIndex
531+
string16(firstCharIndex || properties.firstChar) + // usFirstCharIndex
532+
string16(lastCharIndex || properties.lastChar) + // usLastCharIndex
525533
"\x00\x20" + // sTypoAscender
526534
"\x00\x00" + // sTypeDescender
527535
"\x00\x00" + // sTypoLineGap
@@ -532,7 +540,7 @@ var Font = (function () {
532540
string16(properties.xHeight) + // sxHeight
533541
string16(properties.capHeight) + // sCapHeight
534542
string16(0) + // usDefaultChar
535-
string16(0) + // usBreakChar
543+
string16(firstCharIndex || properties.firstChar) + // usBreakChar
536544
"\x00\x00"; // usMaxContext
537545
};
538546

@@ -780,7 +788,7 @@ var Font = (function () {
780788
name = name.slice(0, name.length - 1);
781789

782790
var strings = [
783-
"Original licence",// 0.Copyright
791+
"Original licence", // 0.Copyright
784792
name, // 1.Font family
785793
"Unknown", // 2.Font subfamily (font weight)
786794
"uniqueID", // 3.Unique ID
@@ -792,14 +800,9 @@ var Font = (function () {
792800
"Unknown" // 9.Designer
793801
];
794802
795-
var platforms = ["\x00\x01", "\x00\x03"];
796-
var encodings = ["\x00\x00", "\x00\x01"];
797-
var languages = ["\x00\x00", "\x04\x09"];
798-
799803
// Mac want 1-byte per character strings while Windows want
800804
// 2-bytes per character, so duplicate the names table
801805
var stringsUnicode = [];
802-
var names = [strings, stringsUnicode];
803806
for (var i = 0; i < strings.length; i++) {
804807
var str = strings[i];
805808
@@ -809,7 +812,12 @@ var Font = (function () {
809812
stringsUnicode.push(strUnicode);
810813
}
811814
812-
var namesRecordCount = platforms.length * names.length;
815+
var names = [strings, stringsUnicode];
816+
var platforms = ["\x00\x01", "\x00\x03"];
817+
var encodings = ["\x00\x00", "\x00\x01"];
818+
var languages = ["\x00\x00", "\x04\x09"];
819+
820+
var namesRecordCount = strings.length * platforms.length;
813821
var nameTable =
814822
"\x00\x00" + // format
815823
string16(namesRecordCount) + // Number of names Record
@@ -818,9 +826,9 @@ var Font = (function () {
818826
// Build the name records field
819827
var strOffset = 0;
820828
for (var i = 0; i < platforms.length; i++) {
821-
var names = strings[i];
822-
for (var j = 0; j < names.length; j++) {
823-
var str = names[j];
829+
var strs = names[i];
830+
for (var j = 0; j < strs.length; j++) {
831+
var str = strs[j];
824832
var nameRecord =
825833
platforms[i] + // platform ID
826834
encodings[i] + // encoding ID
@@ -833,7 +841,8 @@ var Font = (function () {
833841
}
834842
}
835843
836-
return nameTable + strings.join("") + stringsUnicode.join("");
844+
nameTable += strings.join("") + stringsUnicode.join("");
845+
return nameTable;
837846
}
838847
839848
// Required Tables
@@ -870,13 +879,13 @@ var Font = (function () {
870879
871880
/** CMAP */
872881
var charstrings = font.charstrings;
873-
cmap = createCMapTable(charstrings);
882+
cmap = createCMapTable(charstrings.slice());
874883
createTableEntry(otf, offsets, "cmap", cmap);
875884
876885
/** HEAD */
877886
head = stringToArray(
878887
"\x00\x01\x00\x00" + // Version number
879-
"\x00\x00\x50\x00" + // fontRevision
888+
"\x00\x00\x10\x00" + // fontRevision
880889
"\x00\x00\x00\x00" + // checksumAdjustement
881890
"\x5F\x0F\x3C\xF5" + // magicNumber
882891
"\x00\x00" + // Flags
@@ -923,7 +932,7 @@ var Font = (function () {
923932
* while Windows use this data. So be careful if you hack on Linux and
924933
* have to touch the 'hmtx' table
925934
*/
926-
hmtx = "\x00\x8B\x00\x00"; // Fake .notdef
935+
hmtx = "\x00\x00\x00\x00"; // Fake .notdef
927936
var width = 0, lsb = 0;
928937
for (var i = 0; i < charstrings.length; i++) {
929938
var charstring = charstrings[i];

0 commit comments

Comments
 (0)