From 8dbbbf41d68ed74fddaf976d6e6dc8c8fa633987 Mon Sep 17 00:00:00 2001 From: tristen Date: Mon, 5 Aug 2019 12:25:32 -0400 Subject: [PATCH 01/11] Expose metadata in protobuf Pull what's applicable from https://github.com/mapbox/node-fontnik/pull/97 into this commit. --- proto/glyphs.proto | 24 +++++++++++++++++++----- src/glyphs.cpp | 22 +++++++++++++++++----- src/glyphs.hpp | 16 +++++++++++++--- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/proto/glyphs.proto b/proto/glyphs.proto index 24c2826b8..bf2fa332d 100644 --- a/proto/glyphs.proto +++ b/proto/glyphs.proto @@ -10,7 +10,7 @@ option optimize_for = LITE_RUNTIME; message glyph { required uint32 id = 1; - // A signed distance field of the glyph with a border of 3 pixels. + // A signed distance field of a glyph with buffer documented in metadata. optional bytes bitmap = 2; // Glyph metrics. @@ -21,11 +21,25 @@ message glyph { required uint32 advance = 7; } -// Stores fontstack information and a list of faces. +// Stores a face with glyphs and optional metadata. message fontstack { - required string name = 1; - required string range = 2; - repeated glyph glyphs = 3; + repeated Glyph glyphs = 1; + + // Store SDF metadata. + message Metadata { + optional uint32 size = 1; + optional uint32 buffer = 2; + optional float cutoff = 3; + optional float scale = 4; + } + + optional Metadata metadata = 2; + + optional string family_name = 3; + optional string style_name = 4; + optional double ascender = 5; + optional double descender = 6; + optional double line_height = 7; } message glyphs { diff --git a/src/glyphs.cpp b/src/glyphs.cpp index 9037aec44..73c27e446 100644 --- a/src/glyphs.cpp +++ b/src/glyphs.cpp @@ -309,17 +309,29 @@ void RangeAsync(uv_work_t* req) { if (ft_face->family_name) { llmr::glyphs::fontstack* mutable_fontstack = glyphs.add_stacks(); if (ft_face->style_name) { - mutable_fontstack->set_name(std::string(ft_face->family_name) + " " + std::string(ft_face->style_name)); + mutable_fontstack->set_family_name(std::string(ft_face->family_name) + " " + std::string(ft_face->style_name)); } else { - mutable_fontstack->set_name(std::string(ft_face->family_name)); + mutable_fontstack->set_family_name(std::string(ft_face->family_name)); } - mutable_fontstack->set_range(std::to_string(baton->start) + "-" + std::to_string(baton->end)); + // TODO mutable_fontstack->set_range(std::to_string(baton->start) + "-" + std::to_string(baton->end)); + mutable_fontstack->set_family_name(ft_face->family_name); + mutable_fontstack->set_style_name(ft_face->style_name); + mutable_fontstack->set_ascender(ft_face->ascender); + mutable_fontstack->set_descender(ft_face->descender); + mutable_fontstack->set_line_height(ft_face->height); + + // Add metadata to face. + mbgl::glyphs::Face::Metadata mutable_metadata = mutable_face->metadata(); + mutable_metadata.set_size(char_size); + mutable_metadata.set_buffer(buffer_size); + mutable_metadata.set_cutoff(cutoff_size); + mutable_metadata.set_scale(scale_factor); const double scale_factor = 1.0; // Set character sizes. - double size = 24 * scale_factor; + double size = char_size * scale_factor; FT_Set_Char_Size(ft_face, 0, static_cast(size * (1 << 6)), 0, 0); for (std::vector::size_type x = 0; x != baton->chars.size(); x++) { @@ -332,7 +344,7 @@ void RangeAsync(uv_work_t* req) { if (!char_index) continue; glyph.glyph_index = char_index; - sdf_glyph_foundry::RenderSDF(glyph, 24, 3, 0.25, ft_face); + sdf_glyph_foundry::RenderSDF(glyph, char_size, buffer_size, cutoff_size, ft_face); // Add glyph to fontstack. llmr::glyphs::glyph* mutable_glyph = mutable_fontstack->add_glyphs(); diff --git a/src/glyphs.hpp b/src/glyphs.hpp index ecfc8cb94..bf6646e84 100644 --- a/src/glyphs.hpp +++ b/src/glyphs.hpp @@ -1,5 +1,4 @@ -#ifndef NODE_FONTNIK_GLYPHS_HPP -#define NODE_FONTNIK_GLYPHS_HPP +#pragma once #include "glyphs.pb.h" #include @@ -15,4 +14,15 @@ void AfterRange(uv_work_t* req); } // namespace node_fontnik -#endif // NODE_FONTNIK_GLYPHS_HPP +void RenderSDF(glyph_info &glyph, + int size, + int buffer, + float cutoff, + FT_Face ft_face); + +const static int char_size = 24; +const static int buffer_size = 3; +const static float cutoff_size = 0.25; +const static float scale_factor = 1.0; + +} // namespace node_fontnik From a861ae1fb484ddbbf2a4a00cb149a21c2b69709a Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Wed, 7 Aug 2019 12:15:19 -0700 Subject: [PATCH 02/11] get the code compiling --- proto/glyphs.proto | 2 +- src/glyphs.cpp | 4 +--- src/glyphs.hpp | 8 -------- 3 files changed, 2 insertions(+), 12 deletions(-) diff --git a/proto/glyphs.proto b/proto/glyphs.proto index bf2fa332d..38f02ff80 100644 --- a/proto/glyphs.proto +++ b/proto/glyphs.proto @@ -23,7 +23,7 @@ message glyph { // Stores a face with glyphs and optional metadata. message fontstack { - repeated Glyph glyphs = 1; + repeated glyph glyphs = 1; // Store SDF metadata. message Metadata { diff --git a/src/glyphs.cpp b/src/glyphs.cpp index 73c27e446..a08086750 100644 --- a/src/glyphs.cpp +++ b/src/glyphs.cpp @@ -322,14 +322,12 @@ void RangeAsync(uv_work_t* req) { mutable_fontstack->set_line_height(ft_face->height); // Add metadata to face. - mbgl::glyphs::Face::Metadata mutable_metadata = mutable_face->metadata(); + llmr::glyphs::fontstack::Metadata mutable_metadata = mutable_fontstack->metadata(); mutable_metadata.set_size(char_size); mutable_metadata.set_buffer(buffer_size); mutable_metadata.set_cutoff(cutoff_size); mutable_metadata.set_scale(scale_factor); - const double scale_factor = 1.0; - // Set character sizes. double size = char_size * scale_factor; FT_Set_Char_Size(ft_face, 0, static_cast(size * (1 << 6)), 0, 0); diff --git a/src/glyphs.hpp b/src/glyphs.hpp index bf6646e84..736e40161 100644 --- a/src/glyphs.hpp +++ b/src/glyphs.hpp @@ -12,14 +12,6 @@ NAN_METHOD(Range); void RangeAsync(uv_work_t* req); void AfterRange(uv_work_t* req); -} // namespace node_fontnik - -void RenderSDF(glyph_info &glyph, - int size, - int buffer, - float cutoff, - FT_Face ft_face); - const static int char_size = 24; const static int buffer_size = 3; const static float cutoff_size = 0.25; From 26cd52973683c1054820c33994fae4d2626b65b1 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Wed, 7 Aug 2019 12:29:08 -0700 Subject: [PATCH 03/11] workaround travis caching issue --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 2830a00e8..ace54cf5a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,6 +17,7 @@ install: - which node - clang++ -v - which clang++ + - make distclean - make ${BUILDTYPE} # *Here we run tests* From aec7d7c259fcfbdd9ee2ec34117aeff67331f5e5 Mon Sep 17 00:00:00 2001 From: tristen Date: Wed, 7 Aug 2019 16:39:13 -0400 Subject: [PATCH 04/11] Simplify and add tests --- proto/glyphs.proto | 25 +++++++------------------ src/glyphs.cpp | 20 ++++++-------------- src/glyphs.hpp | 10 ++++------ test/fontnik.test.js | 2 ++ test/format/glyphs.js | 6 ++++++ 5 files changed, 25 insertions(+), 38 deletions(-) diff --git a/proto/glyphs.proto b/proto/glyphs.proto index 38f02ff80..e72037fa8 100644 --- a/proto/glyphs.proto +++ b/proto/glyphs.proto @@ -21,25 +21,14 @@ message glyph { required uint32 advance = 7; } -// Stores a face with glyphs and optional metadata. +// Stores a face with glyphs and metadata. message fontstack { - repeated glyph glyphs = 1; - - // Store SDF metadata. - message Metadata { - optional uint32 size = 1; - optional uint32 buffer = 2; - optional float cutoff = 3; - optional float scale = 4; - } - - optional Metadata metadata = 2; - - optional string family_name = 3; - optional string style_name = 4; - optional double ascender = 5; - optional double descender = 6; - optional double line_height = 7; + required string name = 1; + required string range = 2; + repeated glyph glyphs = 3; + + optional double ascender = 4; + optional double descender = 5; } message glyphs { diff --git a/src/glyphs.cpp b/src/glyphs.cpp index a08086750..c85cf2689 100644 --- a/src/glyphs.cpp +++ b/src/glyphs.cpp @@ -309,27 +309,19 @@ void RangeAsync(uv_work_t* req) { if (ft_face->family_name) { llmr::glyphs::fontstack* mutable_fontstack = glyphs.add_stacks(); if (ft_face->style_name) { - mutable_fontstack->set_family_name(std::string(ft_face->family_name) + " " + std::string(ft_face->style_name)); + mutable_fontstack->set_name(std::string(ft_face->family_name) + " " + std::string(ft_face->style_name)); } else { - mutable_fontstack->set_family_name(std::string(ft_face->family_name)); + mutable_fontstack->set_name(std::string(ft_face->family_name)); } - // TODO mutable_fontstack->set_range(std::to_string(baton->start) + "-" + std::to_string(baton->end)); - mutable_fontstack->set_family_name(ft_face->family_name); - mutable_fontstack->set_style_name(ft_face->style_name); + mutable_fontstack->set_range(std::to_string(baton->start) + "-" + std::to_string(baton->end)); mutable_fontstack->set_ascender(ft_face->ascender); mutable_fontstack->set_descender(ft_face->descender); - mutable_fontstack->set_line_height(ft_face->height); - // Add metadata to face. - llmr::glyphs::fontstack::Metadata mutable_metadata = mutable_fontstack->metadata(); - mutable_metadata.set_size(char_size); - mutable_metadata.set_buffer(buffer_size); - mutable_metadata.set_cutoff(cutoff_size); - mutable_metadata.set_scale(scale_factor); + const double scale_factor = 1.0; // Set character sizes. - double size = char_size * scale_factor; + double size = 24 * scale_factor; FT_Set_Char_Size(ft_face, 0, static_cast(size * (1 << 6)), 0, 0); for (std::vector::size_type x = 0; x != baton->chars.size(); x++) { @@ -342,7 +334,7 @@ void RangeAsync(uv_work_t* req) { if (!char_index) continue; glyph.glyph_index = char_index; - sdf_glyph_foundry::RenderSDF(glyph, char_size, buffer_size, cutoff_size, ft_face); + sdf_glyph_foundry::RenderSDF(glyph, 24, 3, 0.25, ft_face); // Add glyph to fontstack. llmr::glyphs::glyph* mutable_glyph = mutable_fontstack->add_glyphs(); diff --git a/src/glyphs.hpp b/src/glyphs.hpp index 736e40161..ecfc8cb94 100644 --- a/src/glyphs.hpp +++ b/src/glyphs.hpp @@ -1,4 +1,5 @@ -#pragma once +#ifndef NODE_FONTNIK_GLYPHS_HPP +#define NODE_FONTNIK_GLYPHS_HPP #include "glyphs.pb.h" #include @@ -12,9 +13,6 @@ NAN_METHOD(Range); void RangeAsync(uv_work_t* req); void AfterRange(uv_work_t* req); -const static int char_size = 24; -const static int buffer_size = 3; -const static float cutoff_size = 0.25; -const static float scale_factor = 1.0; - } // namespace node_fontnik + +#endif // NODE_FONTNIK_GLYPHS_HPP diff --git a/test/fontnik.test.js b/test/fontnik.test.js index a1505c3ce..fda381f33 100644 --- a/test/fontnik.test.js +++ b/test/fontnik.test.js @@ -241,6 +241,8 @@ test('range', function(t) { var vt = new Glyphs(new Protobuf(new Uint8Array(data))); t.equal(vt.stacks.hasOwnProperty('?'), true); t.equal(vt.stacks['?'].hasOwnProperty('name'), true); + t.equal(vt.stacks['?'].hasOwnProperty('ascender'), true); + t.equal(vt.stacks['?'].hasOwnProperty('descender'), true); t.equal(vt.stacks['?'].name, '?'); t.end(); }); diff --git a/test/format/glyphs.js b/test/format/glyphs.js index e119d654f..9494e8426 100644 --- a/test/format/glyphs.js +++ b/test/format/glyphs.js @@ -41,6 +41,12 @@ Glyphs.prototype.readFontstack = function() { } else if (tag == 3) { var glyph = this.readGlyph(); fontstack.glyphs[glyph.id] = glyph; + } else if (tag == 4) { + var ascender = buffer.readDouble(); + fontstack.ascender = ascender; + } else if (tag == 5) { + var descender = buffer.readDouble(); + fontstack.descender = descender; } else { buffer.skip(val); } From f065493618e0829c4e6e39a613bb93af2aca61a5 Mon Sep 17 00:00:00 2001 From: tristen Date: Wed, 7 Aug 2019 16:52:03 -0400 Subject: [PATCH 05/11] Test output of stack containing name, descender, ascender properties --- test/fontnik.test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/fontnik.test.js b/test/fontnik.test.js index fda381f33..37fe40173 100644 --- a/test/fontnik.test.js +++ b/test/fontnik.test.js @@ -253,6 +253,12 @@ test('range', function(t) { t.error(err); var vt = new Glyphs(new Protobuf(new Uint8Array(data))); var glyphs = vt.stacks['Osaka Regular'].glyphs; + var stack = vt.stacks['Osaka Regular']; + + t.equal(stack.name, 'Osaka Regular'); + t.equal(stack.ascender, 256); + t.equal(stack.descender, -64); + var keys = Object.keys(glyphs); var glyph; From f7ad63f5aded721c9687a6a280eaf7efd2a1082d Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Wed, 7 Aug 2019 14:13:46 -0700 Subject: [PATCH 06/11] Add file to allow NODE_PATH to work --- fontnik.js | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 fontnik.js diff --git a/fontnik.js b/fontnik.js new file mode 100644 index 000000000..aa2db1533 --- /dev/null +++ b/fontnik.js @@ -0,0 +1,3 @@ +"use strict"; + +module.exports = require('./index.js'); From 15c124190e37148700e1acc1f6d95cbe265499c2 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Mon, 26 Aug 2019 10:39:14 -0700 Subject: [PATCH 07/11] no longer need this workaround --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index ace54cf5a..2830a00e8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,6 @@ install: - which node - clang++ -v - which clang++ - - make distclean - make ${BUILDTYPE} # *Here we run tests* From d3608d4d7ba231ef8b695edb71294bb9b2670365 Mon Sep 17 00:00:00 2001 From: zmiao Date: Tue, 17 Sep 2019 10:30:56 +0300 Subject: [PATCH 08/11] Change ascender and descender in 26.6 fractional pixels. --- src/glyphs.cpp | 6 ++++-- test/fontnik.test.js | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/glyphs.cpp b/src/glyphs.cpp index c85cf2689..4e8d78dfc 100644 --- a/src/glyphs.cpp +++ b/src/glyphs.cpp @@ -315,8 +315,6 @@ void RangeAsync(uv_work_t* req) { } mutable_fontstack->set_range(std::to_string(baton->start) + "-" + std::to_string(baton->end)); - mutable_fontstack->set_ascender(ft_face->ascender); - mutable_fontstack->set_descender(ft_face->descender); const double scale_factor = 1.0; @@ -324,6 +322,10 @@ void RangeAsync(uv_work_t* req) { double size = 24 * scale_factor; FT_Set_Char_Size(ft_face, 0, static_cast(size * (1 << 6)), 0, 0); + // Set ascender and descender in 26.6 fractional pixels. + mutable_fontstack->set_ascender(ft_face->size->metrics.ascender / 64) ; + mutable_fontstack->set_descender(ft_face->size->metrics.descender / 64); + for (std::vector::size_type x = 0; x != baton->chars.size(); x++) { FT_ULong char_code = baton->chars[x]; sdf_glyph_foundry::glyph_info glyph; diff --git a/test/fontnik.test.js b/test/fontnik.test.js index 37fe40173..74c9d7a04 100644 --- a/test/fontnik.test.js +++ b/test/fontnik.test.js @@ -256,8 +256,8 @@ test('range', function(t) { var stack = vt.stacks['Osaka Regular']; t.equal(stack.name, 'Osaka Regular'); - t.equal(stack.ascender, 256); - t.equal(stack.descender, -64); + t.equal(stack.ascender, 24); + t.equal(stack.descender, -6); var keys = Object.keys(glyphs); From 42dc7bac4d1db7c1387e8cd170534fdd9eed3e65 Mon Sep 17 00:00:00 2001 From: zmiao Date: Tue, 17 Sep 2019 10:57:58 +0300 Subject: [PATCH 09/11] fix format --- src/glyphs.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/glyphs.cpp b/src/glyphs.cpp index 4e8d78dfc..c49996b51 100644 --- a/src/glyphs.cpp +++ b/src/glyphs.cpp @@ -323,7 +323,7 @@ void RangeAsync(uv_work_t* req) { FT_Set_Char_Size(ft_face, 0, static_cast(size * (1 << 6)), 0, 0); // Set ascender and descender in 26.6 fractional pixels. - mutable_fontstack->set_ascender(ft_face->size->metrics.ascender / 64) ; + mutable_fontstack->set_ascender(ft_face->size->metrics.ascender / 64); mutable_fontstack->set_descender(ft_face->size->metrics.descender / 64); for (std::vector::size_type x = 0; x != baton->chars.size(); x++) { From bad6f46185e030e26e33a82239254fedc696744d Mon Sep 17 00:00:00 2001 From: zmiao Date: Thu, 26 Sep 2019 16:22:17 +0300 Subject: [PATCH 10/11] Change ascender and descender type to signed int --- proto/glyphs.proto | 4 ++-- src/glyphs.cpp | 4 ++-- test/format/glyphs.js | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/proto/glyphs.proto b/proto/glyphs.proto index e72037fa8..bbbf9391f 100644 --- a/proto/glyphs.proto +++ b/proto/glyphs.proto @@ -27,8 +27,8 @@ message fontstack { required string range = 2; repeated glyph glyphs = 3; - optional double ascender = 4; - optional double descender = 5; + optional sint32 ascender = 4; + optional sint32 descender = 5; } message glyphs { diff --git a/src/glyphs.cpp b/src/glyphs.cpp index c49996b51..2b20b2013 100644 --- a/src/glyphs.cpp +++ b/src/glyphs.cpp @@ -323,8 +323,8 @@ void RangeAsync(uv_work_t* req) { FT_Set_Char_Size(ft_face, 0, static_cast(size * (1 << 6)), 0, 0); // Set ascender and descender in 26.6 fractional pixels. - mutable_fontstack->set_ascender(ft_face->size->metrics.ascender / 64); - mutable_fontstack->set_descender(ft_face->size->metrics.descender / 64); + mutable_fontstack->set_ascender(static_cast(ft_face->size->metrics.ascender / 64)); + mutable_fontstack->set_descender(static_cast(ft_face->size->metrics.descender / 64)); for (std::vector::size_type x = 0; x != baton->chars.size(); x++) { FT_ULong char_code = baton->chars[x]; diff --git a/test/format/glyphs.js b/test/format/glyphs.js index 9494e8426..4a99ed742 100644 --- a/test/format/glyphs.js +++ b/test/format/glyphs.js @@ -42,10 +42,10 @@ Glyphs.prototype.readFontstack = function() { var glyph = this.readGlyph(); fontstack.glyphs[glyph.id] = glyph; } else if (tag == 4) { - var ascender = buffer.readDouble(); + var ascender = buffer.readSVarint(); fontstack.ascender = ascender; } else if (tag == 5) { - var descender = buffer.readDouble(); + var descender = buffer.readSVarint(); fontstack.descender = descender; } else { buffer.skip(val); From 535f3dcf71a8f21342fd08b33785425981ad358a Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Thu, 4 Jun 2020 10:47:11 -0700 Subject: [PATCH 11/11] update against latest master --- src/glyphs.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/glyphs.cpp b/src/glyphs.cpp index cf5e2deeb..3f6d946f9 100644 --- a/src/glyphs.cpp +++ b/src/glyphs.cpp @@ -548,8 +548,8 @@ void RangeAsync(uv_work_t* req) { FT_Set_Char_Size(ft_face, 0, static_cast(size * (1 << 6)), 0, 0); // Set ascender and descender in 26.6 fractional pixels. - mutable_fontstack->set_ascender(static_cast(ft_face->size->metrics.ascender / 64)); - mutable_fontstack->set_descender(static_cast(ft_face->size->metrics.descender / 64)); + fontstack_writer.add_sint32(4, static_cast(ft_face->size->metrics.ascender / 64)); + fontstack_writer.add_sint32(5, static_cast(ft_face->size->metrics.descender / 64)); for (std::vector::size_type x = 0; x != baton->chars.size(); x++) { FT_ULong char_code = baton->chars[x];