Skip to content

Commit 08d7a10

Browse files
committed
only add glyphs if RenderSDF succeeds
1 parent 2803a51 commit 08d7a10

File tree

5 files changed

+26
-47
lines changed

5 files changed

+26
-47
lines changed

src/glyphs.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -313,21 +313,21 @@ void RangeAsync(uv_work_t* req) {
313313
if (!char_index) continue;
314314

315315
glyph.glyph_index = char_index;
316-
RenderSDF(glyph, 24, 3, 0.25, ft_face);
317-
318-
// Add glyph to fontstack.
319-
llmr::glyphs::glyph *mutable_glyph = mutable_fontstack->add_glyphs();
320-
mutable_glyph->set_id(char_code);
321-
mutable_glyph->set_width(glyph.width);
322-
mutable_glyph->set_height(glyph.height);
323-
mutable_glyph->set_left(glyph.left);
324-
mutable_glyph->set_top(glyph.top - glyph.ascender);
325-
mutable_glyph->set_advance(glyph.advance);
326-
327-
if (glyph.width > 0) {
328-
mutable_glyph->set_bitmap(glyph.bitmap);
329-
}
330316

317+
if(RenderSDF(glyph, 24, 3, 0.25, ft_face)) {
318+
// Add glyph to fontstack.
319+
llmr::glyphs::glyph *mutable_glyph = mutable_fontstack->add_glyphs();
320+
mutable_glyph->set_id(char_code);
321+
mutable_glyph->set_width(glyph.width);
322+
mutable_glyph->set_height(glyph.height);
323+
mutable_glyph->set_left(glyph.left);
324+
mutable_glyph->set_top(glyph.top - glyph.ascender);
325+
mutable_glyph->set_advance(glyph.advance);
326+
327+
if (glyph.width > 0) {
328+
mutable_glyph->set_bitmap(glyph.bitmap);
329+
}
330+
}
331331
}
332332
if (ft_face) {
333333
FT_Done_Face(ft_face);
@@ -525,15 +525,15 @@ double MinDistanceToLineSegment(const Tree &tree,
525525
return std::sqrt(sqaured_distance);
526526
}
527527

528-
void RenderSDF(glyph_info &glyph,
528+
bool RenderSDF(glyph_info &glyph,
529529
int size,
530530
int buffer,
531531
float cutoff,
532532
FT_Face ft_face)
533533
{
534534

535535
if (FT_Load_Glyph (ft_face, glyph.glyph_index, FT_LOAD_NO_HINTING)) {
536-
return;
536+
return false;
537537
}
538538

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

564564
if (!user.ring.empty()) {
565565
CloseRing(user.ring);
566566
user.rings.push_back(user.ring);
567567
}
568568

569569
if (user.rings.empty()) {
570-
return;
570+
return false;
571571
}
572572
} else {
573-
return;
573+
return false;
574574
}
575575

576576
// Calculate the real glyph bbox.
@@ -602,7 +602,7 @@ void RenderSDF(glyph_info &glyph,
602602
}
603603
}
604604

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

607607
glyph.left = bbox_xmin;
608608
glyph.top = bbox_ymax;
@@ -666,6 +666,8 @@ void RenderSDF(glyph_info &glyph,
666666
glyph.bitmap[i] = static_cast<char>(255 - n);
667667
}
668668
}
669+
670+
return true;
669671
}
670672

671673
} // ns node_fontnik

src/glyphs.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ NAN_METHOD(Range);
2929
void RangeAsync(uv_work_t* req);
3030
void AfterRange(uv_work_t* req);
3131
struct glyph_info;
32-
void RenderSDF(glyph_info &glyph,
32+
bool RenderSDF(glyph_info &glyph,
3333
int size,
3434
int buffer,
3535
float cutoff,

test/expected/range.json

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22
"stacks": {
33
"Open Sans Regular": {
44
"glyphs": {
5-
"32": {
6-
"id": 32,
7-
"width": 0,
8-
"height": 0,
9-
"left": 0,
10-
"top": -26,
11-
"advance": 6
12-
},
135
"33": {
146
"id": 33,
157
"width": 3,
@@ -762,14 +754,6 @@
762754
"top": -16,
763755
"advance": 13
764756
},
765-
"160": {
766-
"id": 160,
767-
"width": 0,
768-
"height": 0,
769-
"left": 0,
770-
"top": -26,
771-
"advance": 6
772-
},
773757
"161": {
774758
"id": 161,
775759
"width": 3,

test/fixtures/range.0.256.pbf

-29 Bytes
Binary file not shown.

test/fontnik.test.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var test = require('tape');
77
var fs = require('fs');
88
var path = require('path');
99
var zlib = require('zlib');
10-
var zdata = fs.readFileSync(__dirname + '/fixtures/range.0.256.pbf');
1110
var Protobuf = require('pbf');
1211
var Glyphs = require('./format/glyphs');
1312
var UPDATE = process.env.UPDATE;
@@ -99,14 +98,8 @@ test('load', function(t) {
9998
});
10099

101100
test('range', function(t) {
102-
var data;
103-
zlib.inflate(zdata, function(err, d) {
104-
if (err) throw err;
105-
data = d;
106-
});
107-
108101
t.test('ranges', function(t) {
109-
fontnik.range({font: opensans, start: 0, end: 256}, function(err, res) {
102+
fontnik.range({font: opensans, start: 0, end: 256}, function(err, data) {
110103
t.error(err);
111104
t.ok(data);
112105

@@ -146,9 +139,9 @@ test('range', function(t) {
146139
});
147140

148141
t.test('shortrange', function(t) {
149-
fontnik.range({font: opensans, start: 34, end: 38}, function(err, res) {
142+
fontnik.range({font: opensans, start: 34, end: 38}, function(err, data) {
150143
t.error(err);
151-
var vt = new Glyphs(new Protobuf(new Uint8Array(res)));
144+
var vt = new Glyphs(new Protobuf(new Uint8Array(data)));
152145
t.equal(vt.stacks.hasOwnProperty('Open Sans Regular'), true);
153146
var codes = Object.keys(vt.stacks['Open Sans Regular'].glyphs);
154147
t.deepEqual(codes, ['34','35','36','37','38']);

0 commit comments

Comments
 (0)