pdfkit
Version:
A PDF generation library for Node.js
150 lines (121 loc) • 4.27 kB
JavaScript
(function() {
var CompoundGlyph, Data, GlyfTable, SimpleGlyph, Table;
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; }, __slice = Array.prototype.slice;
Table = require('../table');
Data = require('../../data');
GlyfTable = (function() {
__extends(GlyfTable, Table);
function GlyfTable() {
GlyfTable.__super__.constructor.apply(this, arguments);
}
GlyfTable.prototype.parse = function(data) {
return this.cache = {};
};
GlyfTable.prototype.glyphFor = function(id) {
var data, index, length, loca, numberOfContours, raw, xMax, xMin, yMax, yMin;
if (id in this.cache) return this.cache[id];
loca = this.file.loca;
data = this.file.contents;
index = loca.indexOf(id);
length = loca.lengthOf(id);
if (length === 0) return this.cache[id] = null;
data.pos = this.offset + index;
raw = new Data(data.read(length));
numberOfContours = raw.readShort();
xMin = raw.readShort();
yMin = raw.readShort();
xMax = raw.readShort();
yMax = raw.readShort();
if (numberOfContours === -1) {
this.cache[id] = new CompoundGlyph(raw, xMin, yMin, xMax, yMax);
} else {
this.cache[id] = new SimpleGlyph(raw, numberOfContours, xMin, yMin, xMax, yMax);
}
return this.cache[id];
};
GlyfTable.prototype.encode = function(glyphs, mapping, old2new) {
var glyph, id, offsets, table, _i, _len;
table = [];
offsets = [];
for (_i = 0, _len = mapping.length; _i < _len; _i++) {
id = mapping[_i];
glyph = glyphs[id];
offsets.push(table.length);
if (glyph) table = table.concat(glyph.encode(old2new));
}
offsets.push(table.length);
return {
table: table,
offsets: offsets
};
};
return GlyfTable;
})();
SimpleGlyph = (function() {
function SimpleGlyph(raw, numberOfContours, xMin, yMin, xMax, yMax) {
this.raw = raw;
this.numberOfContours = numberOfContours;
this.xMin = xMin;
this.yMin = yMin;
this.xMax = xMax;
this.yMax = yMax;
this.compound = false;
}
SimpleGlyph.prototype.encode = function() {
return this.raw.data;
};
return SimpleGlyph;
})();
CompoundGlyph = (function() {
var ARG_1_AND_2_ARE_WORDS, MORE_COMPONENTS, WE_HAVE_AN_X_AND_Y_SCALE, WE_HAVE_A_SCALE, WE_HAVE_A_TWO_BY_TWO, WE_HAVE_INSTRUCTIONS;
ARG_1_AND_2_ARE_WORDS = 0x0001;
WE_HAVE_A_SCALE = 0x0008;
MORE_COMPONENTS = 0x0020;
WE_HAVE_AN_X_AND_Y_SCALE = 0x0040;
WE_HAVE_A_TWO_BY_TWO = 0x0080;
WE_HAVE_INSTRUCTIONS = 0x0100;
function CompoundGlyph(raw, xMin, yMin, xMax, yMax) {
var data, flags;
this.raw = raw;
this.xMin = xMin;
this.yMin = yMin;
this.xMax = xMax;
this.yMax = yMax;
this.compound = true;
this.glyphIDs = [];
this.glyphOffsets = [];
data = this.raw;
while (true) {
flags = data.readShort();
this.glyphOffsets.push(data.pos);
this.glyphIDs.push(data.readShort());
if (!(flags & MORE_COMPONENTS)) break;
if (flags & ARG_1_AND_2_ARE_WORDS) {
data.pos += 4;
} else {
data.pos += 2;
}
if (flags & WE_HAVE_A_TWO_BY_TWO) {
data.pos += 8;
} else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) {
data.pos += 4;
} else if (flags & WE_HAVE_A_SCALE) {
data.pos += 2;
}
}
}
CompoundGlyph.prototype.encode = function(mapping) {
var i, id, result, _len, _ref;
result = new Data(__slice.call(this.raw.data));
_ref = this.glyphIDs;
for (i = 0, _len = _ref.length; i < _len; i++) {
id = _ref[i];
result.pos = this.glyphOffsets[i];
result.writeShort(mapping[id]);
}
return result.data;
};
return CompoundGlyph;
})();
module.exports = GlyfTable;
}).call(this);