Clean up Fonts.convert()
This commit is contained in:
parent
b390a03a1d
commit
f58983f4e5
144
fonts.js
144
fonts.js
@ -944,8 +944,6 @@ var Font = (function () {
|
|||||||
},
|
},
|
||||||
|
|
||||||
convert: function font_convert(fontName, font, properties) {
|
convert: function font_convert(fontName, font, properties) {
|
||||||
var otf = new Uint8Array(kMaxFontFileSize);
|
|
||||||
|
|
||||||
function createNameTable(name) {
|
function createNameTable(name) {
|
||||||
// All the strings of the name table should be an odd number of bytes
|
// All the strings of the name table should be an odd number of bytes
|
||||||
if (name.length % 2)
|
if (name.length % 2)
|
||||||
@ -1008,7 +1006,7 @@ var Font = (function () {
|
|||||||
nameTable += strings.join("") + stringsUnicode.join("");
|
nameTable += strings.join("") + stringsUnicode.join("");
|
||||||
return nameTable;
|
return nameTable;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isFixedPitch(glyphs) {
|
function isFixedPitch(glyphs) {
|
||||||
for (var i = 0; i < glyphs.length - 1; i++) {
|
for (var i = 0; i < glyphs.length - 1; i++) {
|
||||||
if (glyphs[i] != glyphs[i+1])
|
if (glyphs[i] != glyphs[i+1])
|
||||||
@ -1017,46 +1015,34 @@ var Font = (function () {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Required Tables
|
|
||||||
var CFF =
|
|
||||||
font.data, // PostScript Font Program
|
|
||||||
OS2, // OS/2 and Windows Specific metrics
|
|
||||||
cmap, // Character to glyphs mapping
|
|
||||||
head, // Font header
|
|
||||||
hhea, // Horizontal header
|
|
||||||
hmtx, // Horizontal metrics
|
|
||||||
maxp, // Maximum profile
|
|
||||||
name, // Naming tables
|
|
||||||
post; // PostScript informations
|
|
||||||
var tables = [CFF, OS2, cmap, head, hhea, hmtx, maxp, name, post];
|
|
||||||
|
|
||||||
// The offsets object holds at the same time a representation of where
|
// The offsets object holds at the same time a representation of where
|
||||||
// to write the table entry information about a table and another offset
|
// to write the table entry information about a table and another offset
|
||||||
// representing the offset where to draw the actual data of a particular
|
// representing the offset where to draw the actual data of a particular
|
||||||
// table
|
// table
|
||||||
|
var kRequiredTablesCount = 9;
|
||||||
var offsets = {
|
var offsets = {
|
||||||
currentOffset: 0,
|
currentOffset: 0,
|
||||||
virtualOffset: tables.length * (4 * 4)
|
virtualOffset: 9 * (4 * 4)
|
||||||
};
|
};
|
||||||
|
|
||||||
// It there is only one font, offset table is the first bytes of the file
|
var otf = new Uint8Array(kMaxFontFileSize);
|
||||||
createOpenTypeHeader("\x4F\x54\x54\x4F", otf, offsets, tables.length);
|
createOpenTypeHeader("\x4F\x54\x54\x4F", otf, offsets, 9);
|
||||||
|
|
||||||
/** CFF */
|
|
||||||
createTableEntry(otf, offsets, "CFF ", CFF);
|
|
||||||
|
|
||||||
/** OS/2 */
|
|
||||||
var charstrings = font.charstrings;
|
var charstrings = font.charstrings;
|
||||||
properties.fixedPitch = isFixedPitch(charstrings);
|
properties.fixedPitch = isFixedPitch(charstrings);
|
||||||
OS2 = stringToArray(createOS2Table(properties));
|
var fields = {
|
||||||
createTableEntry(otf, offsets, "OS/2", OS2);
|
// PostScript Font Program
|
||||||
|
"CFF ": font.data,
|
||||||
|
|
||||||
/** CMAP */
|
// OS/2 and Windows Specific metrics
|
||||||
cmap = createCMapTable(charstrings.slice());
|
"OS/2": stringToArray(createOS2Table(properties)),
|
||||||
createTableEntry(otf, offsets, "cmap", cmap);
|
|
||||||
|
|
||||||
/** HEAD */
|
// Character to glyphs mapping
|
||||||
head = stringToArray(
|
"cmap": createCMapTable(charstrings.slice()),
|
||||||
|
|
||||||
|
// Font header
|
||||||
|
"head": (function() {
|
||||||
|
return stringToArray(
|
||||||
"\x00\x01\x00\x00" + // Version number
|
"\x00\x01\x00\x00" + // Version number
|
||||||
"\x00\x00\x10\x00" + // fontRevision
|
"\x00\x00\x10\x00" + // fontRevision
|
||||||
"\x00\x00\x00\x00" + // checksumAdjustement
|
"\x00\x00\x00\x00" + // checksumAdjustement
|
||||||
@ -1073,63 +1059,59 @@ var Font = (function () {
|
|||||||
"\x00\x11" + // lowestRecPPEM
|
"\x00\x11" + // lowestRecPPEM
|
||||||
"\x00\x00" + // fontDirectionHint
|
"\x00\x00" + // fontDirectionHint
|
||||||
"\x00\x00" + // indexToLocFormat
|
"\x00\x00" + // indexToLocFormat
|
||||||
"\x00\x00" // glyphDataFormat
|
"\x00\x00"); // glyphDataFormat
|
||||||
);
|
})(),
|
||||||
createTableEntry(otf, offsets, "head", head);
|
|
||||||
|
|
||||||
/** HHEA */
|
// Horizontal header
|
||||||
hhea = stringToArray(
|
"hhea": (function() {
|
||||||
"\x00\x01\x00\x00" + // Version number
|
return stringToArray(
|
||||||
string16(properties.ascent) + // Typographic Ascent
|
"\x00\x01\x00\x00" + // Version number
|
||||||
string16(properties.descent) + // Typographic Descent
|
string16(properties.ascent) + // Typographic Ascent
|
||||||
"\x00\x00" + // Line Gap
|
string16(properties.descent) + // Typographic Descent
|
||||||
"\xFF\xFF" + // advanceWidthMax
|
"\x00\x00" + // Line Gap
|
||||||
"\x00\x00" + // minLeftSidebearing
|
"\xFF\xFF" + // advanceWidthMax
|
||||||
"\x00\x00" + // minRightSidebearing
|
"\x00\x00" + // minLeftSidebearing
|
||||||
"\x00\x00" + // xMaxExtent
|
"\x00\x00" + // minRightSidebearing
|
||||||
string16(properties.capHeight) + // caretSlopeRise
|
"\x00\x00" + // xMaxExtent
|
||||||
string16(Math.tan(properties.italicAngle) * properties.xHeight) + // caretSlopeRun
|
string16(properties.capHeight) + // caretSlopeRise
|
||||||
"\x00\x00" + // caretOffset
|
string16(Math.tan(properties.italicAngle) * properties.xHeight) + // caretSlopeRun
|
||||||
"\x00\x00" + // -reserved-
|
"\x00\x00" + // caretOffset
|
||||||
"\x00\x00" + // -reserved-
|
"\x00\x00" + // -reserved-
|
||||||
"\x00\x00" + // -reserved-
|
"\x00\x00" + // -reserved-
|
||||||
"\x00\x00" + // -reserved-
|
"\x00\x00" + // -reserved-
|
||||||
"\x00\x00" + // metricDataFormat
|
"\x00\x00" + // -reserved-
|
||||||
string16(charstrings.length + 1) // Number of HMetrics
|
"\x00\x00" + // metricDataFormat
|
||||||
);
|
string16(charstrings.length + 1)); // Number of HMetrics
|
||||||
createTableEntry(otf, offsets, "hhea", hhea);
|
})(),
|
||||||
|
|
||||||
/** HMTX */
|
// Horizontal metrics
|
||||||
/* For some reasons, probably related to how the backend handle fonts,
|
"hmtx": (function() {
|
||||||
* Linux seems to ignore this file and prefer the data from the CFF itself
|
var hmtx = "\x00\x00\x00\x00"; // Fake .notdef
|
||||||
* while Windows use this data. So be careful if you hack on Linux and
|
for (var i = 0; i < charstrings.length; i++) {
|
||||||
* have to touch the 'hmtx' table
|
hmtx += string16(charstrings[i].width) + string16(0);
|
||||||
*/
|
}
|
||||||
hmtx = "\x00\x00\x00\x00"; // Fake .notdef
|
return stringToArray(hmtx);
|
||||||
for (var i = 0; i < charstrings.length; i++) {
|
})(),
|
||||||
hmtx += string16(charstrings[i].width) + string16(0);
|
|
||||||
}
|
|
||||||
hmtx = stringToArray(hmtx);
|
|
||||||
createTableEntry(otf, offsets, "hmtx", hmtx);
|
|
||||||
|
|
||||||
/** MAXP */
|
// Maximum profile
|
||||||
maxp = "\x00\x00\x50\x00" + // Version number
|
"maxp": (function() {
|
||||||
string16(charstrings.length + 1); // Num of glyphs
|
return stringToArray(
|
||||||
maxp = stringToArray(maxp);
|
"\x00\x00\x50\x00" + // Version number
|
||||||
createTableEntry(otf, offsets, "maxp", maxp);
|
string16(charstrings.length + 1)); // Num of glyphs
|
||||||
|
})(),
|
||||||
|
|
||||||
/** NAME */
|
// Naming tables
|
||||||
name = stringToArray(createNameTable(fontName));
|
"name": stringToArray(createNameTable(fontName)),
|
||||||
createTableEntry(otf, offsets, "name", name);
|
|
||||||
|
|
||||||
/** POST */
|
// PostScript informations
|
||||||
post = stringToArray(createPostTable(properties));
|
"post": stringToArray(createPostTable(properties))
|
||||||
createTableEntry(otf, offsets, "post", post);
|
};
|
||||||
|
|
||||||
// Once all the table entries header are written, dump the data!
|
for (var field in fields)
|
||||||
var tables = [CFF, OS2, cmap, head, hhea, hmtx, maxp, name, post];
|
createTableEntry(otf, offsets, field, fields[field]);
|
||||||
for (var i = 0; i < tables.length; i++) {
|
|
||||||
var table = tables[i];
|
for (var field in fields) {
|
||||||
|
var table = fields[field];
|
||||||
otf.set(table, offsets.currentOffset);
|
otf.set(table, offsets.currentOffset);
|
||||||
offsets.currentOffset += table.length;
|
offsets.currentOffset += table.length;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user