Skip to content

Commit 1c78d8f

Browse files
committed
Fix more headers tables
1 parent 7ff6f51 commit 1c78d8f

File tree

1 file changed

+40
-32
lines changed

1 file changed

+40
-32
lines changed

fonts.js

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ 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");
302303
break;
303304

304305
case "TrueType":
@@ -485,9 +486,9 @@ var Font = (function () {
485486

486487
for (var i = 1; i < charset.length; i++) {
487488
var code = GlyphsUnicode[charset[i]];
488-
if (code < firstCharIndex || !firstCharIndex)
489+
if (firstCharIndex > code || !firstCharIndex)
489490
firstCharIndex = code;
490-
if (code > lastCharIndex)
491+
if (lastCharIndex < code)
491492
lastCharIndex = code;
492493

493494
var position = getUnicodeRangeFor(code);
@@ -509,7 +510,7 @@ var Font = (function () {
509510
"\x02\x24" + // xAvgCharWidth
510511
"\x01\xF4" + // usWeightClass
511512
"\x00\x05" + // usWidthClass
512-
"\x00\x00" + // fstype
513+
"\x00\x02" + // fstype
513514
"\x02\x8A" + // ySubscriptXSize
514515
"\x02\xBB" + // ySubscriptYSize
515516
"\x00\x00" + // ySubscriptXOffset
@@ -521,41 +522,41 @@ var Font = (function () {
521522
"\x00\x31" + // yStrikeOutSize
522523
"\x01\x02" + // yStrikeOutPosition
523524
"\x00\x00" + // sFamilyClass
524-
"\x02\x00\x06\x03\x00\x00\x00\x00\x00\x00" + // Panose
525+
"\x00\x00\x06" + String.fromCharCode(properties.fixedPitch ? 0x09 : 0x00) +
526+
"\x00\x00\x00\x00\x00\x00" + // Panose
525527
string32(ulUnicodeRange1) + // ulUnicodeRange1 (Bits 0-31)
526528
string32(ulUnicodeRange2) + // ulUnicodeRange2 (Bits 32-63)
527529
string32(ulUnicodeRange3) + // ulUnicodeRange3 (Bits 64-95)
528530
string32(ulUnicodeRange4) + // ulUnicodeRange4 (Bits 96-127)
529531
"\x2A\x32\x31\x2A" + // achVendID
530-
"\x00\x00" + // fsSelection
532+
string16(properties.italicAngle ? 1 : 0) + // fsSelection
531533
string16(firstCharIndex || properties.firstChar) + // usFirstCharIndex
532534
string16(lastCharIndex || properties.lastChar) + // usLastCharIndex
533-
"\x00\x20" + // sTypoAscender
534-
"\x00\x00" + // sTypeDescender
535-
"\x00\x00" + // sTypoLineGap
535+
string16(properties.ascent) + // sTypoAscender
536+
string16(properties.descent) + // sTypoDescender
537+
"\x00\x64" + // sTypoLineGap (7%-10% of the unitsPerEM value)
536538
string16(properties.ascent) + // usWinAscent
537-
string16(properties.descent) + // usWinDescent
539+
string16(-properties.descent) + // usWinDescent
538540
"\x00\x00\x00\x00" + // ulCodePageRange1 (Bits 0-31)
539541
"\x00\x00\x00\x00" + // ulCodePageRange2 (Bits 32-63)
540542
string16(properties.xHeight) + // sxHeight
541543
string16(properties.capHeight) + // sCapHeight
542544
string16(0) + // usDefaultChar
543545
string16(firstCharIndex || properties.firstChar) + // usBreakChar
544-
"\x00\x00"; // usMaxContext
546+
"\x00\x03"; // usMaxContext
545547
};
546548

547549
function createPostTable(properties) {
548-
TODO("Fill with real values from the font dict");
549-
550-
return "\x00\x03\x00\x00" + // Version number
551-
string32(properties.italicAngle) + // italicAngle
552-
"\x00\x00" + // underlinePosition
553-
"\x00\x00" + // underlineThickness
554-
"\x00\x00\x00\x00" + // isFixedPitch
555-
"\x00\x00\x00\x00" + // minMemType42
556-
"\x00\x00\x00\x00" + // maxMemType42
557-
"\x00\x00\x00\x00" + // minMemType1
558-
"\x00\x00\x00\x00"; // maxMemType1
550+
var angle = Math.floor(properties.italicAngle * (Math.pow(2, 16)));
551+
return "\x00\x03\x00\x00" + // Version number
552+
string32(angle) + // italicAngle
553+
"\x00\x00" + // underlinePosition
554+
"\x00\x00" + // underlineThickness
555+
string32(properties.fixedPitch) + // isFixedPitch
556+
"\x00\x00\x00\x00" + // minMemType42
557+
"\x00\x00\x00\x00" + // maxMemType42
558+
"\x00\x00\x00\x00" + // minMemType1
559+
"\x00\x00\x00\x00"; // maxMemType1
559560
};
560561

561562
constructor.prototype = {
@@ -844,6 +845,14 @@ var Font = (function () {
844845
nameTable += strings.join("") + stringsUnicode.join("");
845846
return nameTable;
846847
}
848+
849+
function isFixedPitch(glyphs) {
850+
for (var i = 0; i < glyphs.length - 1; i++) {
851+
if (glyphs[i] != glyphs[i+1])
852+
return false;
853+
}
854+
return true;
855+
};
847856
848857
// Required Tables
849858
var CFF =
@@ -874,11 +883,12 @@ var Font = (function () {
874883
createTableEntry(otf, offsets, "CFF ", CFF);
875884
876885
/** OS/2 */
886+
var charstrings = font.charstrings;
887+
properties.fixedPitch = isFixedPitch(charstrings);
877888
OS2 = stringToArray(createOS2Table(properties));
878889
createTableEntry(otf, offsets, "OS/2", OS2);
879890
880891
/** CMAP */
881-
var charstrings = font.charstrings;
882892
cmap = createCMapTable(charstrings.slice());
883893
createTableEntry(otf, offsets, "cmap", cmap);
884894
@@ -893,10 +903,10 @@ var Font = (function () {
893903
"\x00\x00\x00\x00\x9e\x0b\x7e\x27" + // creation date
894904
"\x00\x00\x00\x00\x9e\x0b\x7e\x27" + // modifification date
895905
"\x00\x00" + // xMin
896-
"\x00\x00" + // yMin
897-
"\x00\x00" + // xMax
898-
"\x00\x00" + // yMax
899-
string16(properties.italicAngle ? 1 : 0) + // macStyle
906+
string16(properties.descent) + // yMin
907+
"\x0F\xFF" + // xMax
908+
string16(properties.ascent) + // yMax
909+
string16(properties.italicAngle ? 2 : 0) + // macStyle
900910
"\x00\x11" + // lowestRecPPEM
901911
"\x00\x00" + // fontDirectionHint
902912
"\x00\x00" + // indexToLocFormat
@@ -910,12 +920,12 @@ var Font = (function () {
910920
string16(properties.ascent) + // Typographic Ascent
911921
string16(properties.descent) + // Typographic Descent
912922
"\x00\x00" + // Line Gap
913-
"\xFF\xFF" + // advanceWidthMax
923+
"\x00\xFF" + // advanceWidthMax
914924
"\x00\x00" + // minLeftSidebearing
915925
"\x00\x00" + // minRightSidebearing
916926
"\x00\x00" + // xMaxExtent
917-
"\x00\x00" + // caretSlopeRise
918-
string16(properties.italicAngle) + // caretSlopeRun
927+
string16(properties.capHeight) + // caretSlopeRise
928+
string16(Math.tan(properties.italicAngle) * properties.xHeight) + // caretSlopeRun
919929
"\x00\x00" + // caretOffset
920930
"\x00\x00" + // -reserved-
921931
"\x00\x00" + // -reserved-
@@ -933,10 +943,8 @@ var Font = (function () {
933943
* have to touch the 'hmtx' table
934944
*/
935945
hmtx = "\x00\x00\x00\x00"; // Fake .notdef
936-
var width = 0, lsb = 0;
937946
for (var i = 0; i < charstrings.length; i++) {
938-
var charstring = charstrings[i];
939-
hmtx += string16(charstring.width) + string16(0);
947+
hmtx += string16(charstrings[i].width) + string16(0);
940948
}
941949
hmtx = stringToArray(hmtx);
942950
createTableEntry(otf, offsets, "hmtx", hmtx);

0 commit comments

Comments
 (0)