Skip to content

Commit

Permalink
only add glyphs if RenderSDF succeeds
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemorris committed Nov 8, 2016
1 parent e3ca9e4 commit 7b1e582
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 47 deletions.
42 changes: 22 additions & 20 deletions src/glyphs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,21 +313,21 @@ void RangeAsync(uv_work_t* req) {
if (!char_index) continue;

glyph.glyph_index = char_index;
RenderSDF(glyph, 24, 3, 0.25, ft_face);

// Add glyph to fontstack.
llmr::glyphs::glyph *mutable_glyph = mutable_fontstack->add_glyphs();
mutable_glyph->set_id(char_code);
mutable_glyph->set_width(glyph.width);
mutable_glyph->set_height(glyph.height);
mutable_glyph->set_left(glyph.left);
mutable_glyph->set_top(glyph.top - glyph.ascender);
mutable_glyph->set_advance(glyph.advance);

if (glyph.width > 0) {
mutable_glyph->set_bitmap(glyph.bitmap);
}

if(RenderSDF(glyph, 24, 3, 0.25, ft_face)) {
// Add glyph to fontstack.
llmr::glyphs::glyph *mutable_glyph = mutable_fontstack->add_glyphs();
mutable_glyph->set_id(char_code);
mutable_glyph->set_width(glyph.width);
mutable_glyph->set_height(glyph.height);
mutable_glyph->set_left(glyph.left);
mutable_glyph->set_top(glyph.top - glyph.ascender);
mutable_glyph->set_advance(glyph.advance);

if (glyph.width > 0) {
mutable_glyph->set_bitmap(glyph.bitmap);
}
}
}
if (ft_face) {
FT_Done_Face(ft_face);
Expand Down Expand Up @@ -525,15 +525,15 @@ double MinDistanceToLineSegment(const Tree &tree,
return std::sqrt(sqaured_distance);
}

void RenderSDF(glyph_info &glyph,
bool RenderSDF(glyph_info &glyph,
int size,
int buffer,
float cutoff,
FT_Face ft_face)
{

if (FT_Load_Glyph (ft_face, glyph.glyph_index, FT_LOAD_NO_HINTING)) {
return;
return false;
}

int advance = ft_face->glyph->metrics.horiAdvance / 64;
Expand All @@ -559,18 +559,18 @@ void RenderSDF(glyph_info &glyph,
if (ft_face->glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
// Decompose outline into bezier curves and line segments
FT_Outline outline = ft_face->glyph->outline;
if (FT_Outline_Decompose(&outline, &func_interface, &user)) return;
if (FT_Outline_Decompose(&outline, &func_interface, &user)) return false;

if (!user.ring.empty()) {
CloseRing(user.ring);
user.rings.push_back(user.ring);
}

if (user.rings.empty()) {
return;
return false;
}
} else {
return;
return false;
}

// Calculate the real glyph bbox.
Expand Down Expand Up @@ -602,7 +602,7 @@ void RenderSDF(glyph_info &glyph,
}
}

if (bbox_xmax - bbox_xmin == 0 || bbox_ymax - bbox_ymin == 0) return;
if (bbox_xmax - bbox_xmin == 0 || bbox_ymax - bbox_ymin == 0) return false;

glyph.left = bbox_xmin;
glyph.top = bbox_ymax;
Expand Down Expand Up @@ -666,6 +666,8 @@ void RenderSDF(glyph_info &glyph,
glyph.bitmap[i] = static_cast<char>(255 - n);
}
}

return true;
}

} // ns node_fontnik
2 changes: 1 addition & 1 deletion src/glyphs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ NAN_METHOD(Range);
void RangeAsync(uv_work_t* req);
void AfterRange(uv_work_t* req);
struct glyph_info;
void RenderSDF(glyph_info &glyph,
bool RenderSDF(glyph_info &glyph,
int size,
int buffer,
float cutoff,
Expand Down
16 changes: 0 additions & 16 deletions test/expected/range.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@
"stacks": {
"Open Sans Regular": {
"glyphs": {
"32": {
"id": 32,
"width": 0,
"height": 0,
"left": 0,
"top": -26,
"advance": 6
},
"33": {
"id": 33,
"width": 3,
Expand Down Expand Up @@ -762,14 +754,6 @@
"top": -16,
"advance": 13
},
"160": {
"id": 160,
"width": 0,
"height": 0,
"left": 0,
"top": -26,
"advance": 6
},
"161": {
"id": 161,
"width": 3,
Expand Down
Binary file modified test/fixtures/range.0.256.pbf
Binary file not shown.
13 changes: 3 additions & 10 deletions test/fontnik.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var test = require('tape');
var fs = require('fs');
var path = require('path');
var zlib = require('zlib');
var zdata = fs.readFileSync(__dirname + '/fixtures/range.0.256.pbf');
var Protobuf = require('pbf');
var Glyphs = require('./format/glyphs');
var UPDATE = process.env.UPDATE;
Expand Down Expand Up @@ -99,14 +98,8 @@ test('load', function(t) {
});

test('range', function(t) {
var data;
zlib.inflate(zdata, function(err, d) {
if (err) throw err;
data = d;
});

t.test('ranges', function(t) {
fontnik.range({font: opensans, start: 0, end: 256}, function(err, res) {
fontnik.range({font: opensans, start: 0, end: 256}, function(err, data) {
t.error(err);
t.ok(data);

Expand Down Expand Up @@ -146,9 +139,9 @@ test('range', function(t) {
});

t.test('shortrange', function(t) {
fontnik.range({font: opensans, start: 34, end: 38}, function(err, res) {
fontnik.range({font: opensans, start: 34, end: 38}, function(err, data) {
t.error(err);
var vt = new Glyphs(new Protobuf(new Uint8Array(res)));
var vt = new Glyphs(new Protobuf(new Uint8Array(data)));
t.equal(vt.stacks.hasOwnProperty('Open Sans Regular'), true);
var codes = Object.keys(vt.stacks['Open Sans Regular'].glyphs);
t.deepEqual(codes, ['34','35','36','37','38']);
Expand Down

0 comments on commit 7b1e582

Please sign in to comment.