-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
23 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,33 @@ | ||
'use strict'; | ||
|
||
module.exports = Glyphs; | ||
function Glyphs(buffer, end) { | ||
// Public | ||
this.stacks = {}; | ||
// Private | ||
this._buffer = buffer; | ||
|
||
var val, tag; | ||
if (typeof end === 'undefined') end = buffer.length; | ||
while (buffer.pos < end) { | ||
val = buffer.readVarint(); | ||
tag = val >> 3; | ||
if (tag == 1) { | ||
var fontstack = this.readFontstack(); | ||
this.stacks[fontstack.name] = fontstack; | ||
} else { | ||
// console.warn('skipping tile tag ' + tag); | ||
buffer.skip(val); | ||
} | ||
} | ||
function Glyphs(pbf, end) { | ||
this.stacks = pbf.readFields(readFontstacks, {}, end); | ||
} | ||
|
||
Glyphs.prototype.readFontstack = function() { | ||
var buffer = this._buffer; | ||
var fontstack = { glyphs: {} }; | ||
|
||
var bytes = buffer.readVarint(); | ||
var val, tag; | ||
var end = buffer.pos + bytes; | ||
while (buffer.pos < end) { | ||
val = buffer.readVarint(); | ||
tag = val >> 3; | ||
|
||
if (tag == 1) { | ||
fontstack.name = buffer.readString(); | ||
} else if (tag == 2) { | ||
var range = buffer.readString(); | ||
fontstack.range = range; | ||
} else if (tag == 3) { | ||
var glyph = this.readGlyph(); | ||
fontstack.glyphs[glyph.id] = glyph; | ||
} else { | ||
buffer.skip(val); | ||
} | ||
function readFontstacks(tag, stacks, pbf) { | ||
if (tag == 1) { | ||
var fontstack = pbf.readMessage(readFontstack, {glyphs: {}}); | ||
stacks[fontstack.name] = fontstack; | ||
} | ||
} | ||
|
||
return fontstack; | ||
}; | ||
|
||
Glyphs.prototype.readGlyph = function() { | ||
var buffer = this._buffer; | ||
var glyph = {}; | ||
|
||
var bytes = buffer.readVarint(); | ||
var val, tag; | ||
var end = buffer.pos + bytes; | ||
while (buffer.pos < end) { | ||
val = buffer.readVarint(); | ||
tag = val >> 3; | ||
|
||
if (tag == 1) { | ||
glyph.id = buffer.readVarint(); | ||
} else if (tag == 2) { | ||
glyph.bitmap = buffer.readBuffer(); | ||
} else if (tag == 3) { | ||
glyph.width = buffer.readVarint(); | ||
} else if (tag == 4) { | ||
glyph.height = buffer.readVarint(); | ||
} else if (tag == 5) { | ||
glyph.left = buffer.readSVarint(); | ||
} else if (tag == 6) { | ||
glyph.top = buffer.readSVarint(); | ||
} else if (tag == 7) { | ||
glyph.advance = buffer.readVarint(); | ||
} else { | ||
buffer.skip(val); | ||
} | ||
function readFontstack(tag, fontstack, pbf) { | ||
if (tag == 1) fontstack.name = pbf.readString(); | ||
else if (tag == 2) fontstack.range = pbf.readString(); | ||
else if (tag == 3) { | ||
var glyph = pbf.readMessage(readGlyph, {}); | ||
fontstack.glyphs[glyph.id] = glyph; | ||
} | ||
} | ||
|
||
return glyph; | ||
}; | ||
function readGlyph(tag, glyph, pbf) { | ||
if (tag == 1) glyph.id = pbf.readVarint(); | ||
else if (tag == 2) glyph.bitmap = pbf.readpbf(); | ||
else if (tag == 3) glyph.width = pbf.readVarint(); | ||
else if (tag == 4) glyph.height = pbf.readVarint(); | ||
else if (tag == 5) glyph.left = pbf.readSVarint(); | ||
else if (tag == 6) glyph.top = pbf.readSVarint(); | ||
else if (tag == 7) glyph.advance = pbf.readVarint(); | ||
} |