From 3a2f13d4cfc96ad9a13c18280f158e26ca89de7f Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Wed, 21 Aug 2019 09:44:15 -0700 Subject: [PATCH 1/4] port to protozero from @joto - refs #143 --- binding.gyp | 31 ++++------------------------- scripts/install_deps.sh | 8 ++------ scripts/setup.sh | 2 +- src/glyphs.cpp | 43 ++++++++++++++++++++--------------------- src/glyphs.hpp | 1 - 5 files changed, 28 insertions(+), 57 deletions(-) diff --git a/binding.gyp b/binding.gyp index 3acdaa0fe..a37102eb6 100644 --- a/binding.gyp +++ b/binding.gyp @@ -9,8 +9,7 @@ 'system_includes': [ "-isystem <(module_root_dir)/ // node #include #include @@ -291,7 +291,7 @@ void RangeAsync(uv_work_t* req) { /* LCOV_EXCL_END */ } - llmr::glyphs::glyphs glyphs; + protozero::pbf_writer pbf_writer{baton->message}; FT_Face ft_face = 0; FT_Long num_faces = 0; for (int i = 0; ft_face == 0 || i < num_faces; ++i) { @@ -307,14 +307,13 @@ void RangeAsync(uv_work_t* req) { } if (ft_face->family_name) { - llmr::glyphs::fontstack* mutable_fontstack = glyphs.add_stacks(); + protozero::pbf_writer fontstack_writer{pbf_writer, 1}; if (ft_face->style_name) { - mutable_fontstack->set_name(std::string(ft_face->family_name) + " " + std::string(ft_face->style_name)); + fontstack_writer.add_string(1,std::string(ft_face->family_name) + " " + std::string(ft_face->style_name)); } else { - mutable_fontstack->set_name(std::string(ft_face->family_name)); + fontstack_writer.add_string(1,std::string(ft_face->family_name)); } - - mutable_fontstack->set_range(std::to_string(baton->start) + "-" + std::to_string(baton->end)); + fontstack_writer.add_string(2,std::to_string(baton->start) + "-" + std::to_string(baton->end)); const double scale_factor = 1.0; @@ -335,47 +334,47 @@ void RangeAsync(uv_work_t* req) { sdf_glyph_foundry::RenderSDF(glyph, 24, 3, 0.25, ft_face); // Add glyph to fontstack. - llmr::glyphs::glyph* mutable_glyph = mutable_fontstack->add_glyphs(); - - // direct type conversions, no need for checking or casting - mutable_glyph->set_width(glyph.width); - mutable_glyph->set_height(glyph.height); - mutable_glyph->set_left(glyph.left); - - // conversions requiring checks, for safety and correctness + protozero::pbf_writer glyph_writer{fontstack_writer, 3}; // shortening conversion if (char_code > std::numeric_limits::max()) { throw std::runtime_error("Invalid value for char_code: too large"); } else { - mutable_glyph->set_id(static_cast(char_code)); + glyph_writer.add_uint32(1,static_cast(char_code)); } + if (glyph.width > 0) { + glyph_writer.add_bytes(2,glyph.bitmap); + } + + // direct type conversions, no need for checking or casting + glyph_writer.add_uint32(3,glyph.width); + glyph_writer.add_uint32(4,glyph.width); + glyph_writer.add_sint32(5,glyph.width); + + // conversions requiring checks, for safety and correctness + // double to int double top = static_cast(glyph.top) - glyph.ascender; if (top < std::numeric_limits::min() || top > std::numeric_limits::max()) { throw std::runtime_error("Invalid value for glyph.top-glyph.ascender"); } else { - mutable_glyph->set_top(static_cast(top)); + glyph_writer.add_sint32(6,static_cast(top)); } // double to uint if (glyph.advance < std::numeric_limits::min() || glyph.advance > std::numeric_limits::max()) { throw std::runtime_error("Invalid value for glyph.top-glyph.ascender"); } else { - mutable_glyph->set_advance(static_cast(glyph.advance)); + glyph_writer.add_uint32(7,static_cast(glyph.advance)); } - if (glyph.width > 0) { - mutable_glyph->set_bitmap(glyph.bitmap); - } } } else { baton->error_name = std::string("font does not have family_name"); return; } } - baton->message = glyphs.SerializeAsString(); } catch (std::exception const& ex) { baton->error_name = ex.what(); } diff --git a/src/glyphs.hpp b/src/glyphs.hpp index ecfc8cb94..173872cf0 100644 --- a/src/glyphs.hpp +++ b/src/glyphs.hpp @@ -1,7 +1,6 @@ #ifndef NODE_FONTNIK_GLYPHS_HPP #define NODE_FONTNIK_GLYPHS_HPP -#include "glyphs.pb.h" #include namespace node_fontnik { From 06d0cb4529ec62d95520a6f0a25d24c15ba00d41 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Sat, 24 Aug 2019 14:30:38 -0700 Subject: [PATCH 2/4] try to work around travis node_modules caching --- Makefile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index a2f661376..065f488cc 100644 --- a/Makefile +++ b/Makefile @@ -5,16 +5,16 @@ export WERROR ?= false default: release -node_modules: +./node_modules/.bin/node-pre-gyp: # install deps but for now ignore our own install script # so that we can run it directly in either debug or release npm install --ignore-scripts -release: node_modules +release: ./node_modules/.bin/node-pre-gyp V=1 ./node_modules/.bin/node-pre-gyp configure build --error_on_warnings=$(WERROR) --loglevel=error @echo "run 'make clean' for full rebuild" -debug: node_modules +debug: ./node_modules/.bin/node-pre-gyp V=1 ./node_modules/.bin/node-pre-gyp configure build --error_on_warnings=$(WERROR) --loglevel=error --debug @echo "run 'make clean' for full rebuild" @@ -46,7 +46,7 @@ distclean: clean rm -rf .toolchain rm -f local.env -xcode: node_modules +xcode: ./node_modules/.bin/node-pre-gyp ./node_modules/.bin/node-pre-gyp configure -- -f xcode @# If you need more targets, e.g. to run other npm scripts, duplicate the last line and change NPM_ARGUMENT From f2c5462ca3e8d5bcbbf6c521c1538ed40b511648 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Mon, 26 Aug 2019 11:00:45 -0700 Subject: [PATCH 3/4] make format --- src/glyphs.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/glyphs.cpp b/src/glyphs.cpp index 8f620244f..9746c0908 100644 --- a/src/glyphs.cpp +++ b/src/glyphs.cpp @@ -309,11 +309,11 @@ void RangeAsync(uv_work_t* req) { if (ft_face->family_name) { protozero::pbf_writer fontstack_writer{pbf_writer, 1}; if (ft_face->style_name) { - fontstack_writer.add_string(1,std::string(ft_face->family_name) + " " + std::string(ft_face->style_name)); + fontstack_writer.add_string(1, std::string(ft_face->family_name) + " " + std::string(ft_face->style_name)); } else { - fontstack_writer.add_string(1,std::string(ft_face->family_name)); + fontstack_writer.add_string(1, std::string(ft_face->family_name)); } - fontstack_writer.add_string(2,std::to_string(baton->start) + "-" + std::to_string(baton->end)); + fontstack_writer.add_string(2, std::to_string(baton->start) + "-" + std::to_string(baton->end)); const double scale_factor = 1.0; @@ -340,17 +340,17 @@ void RangeAsync(uv_work_t* req) { if (char_code > std::numeric_limits::max()) { throw std::runtime_error("Invalid value for char_code: too large"); } else { - glyph_writer.add_uint32(1,static_cast(char_code)); + glyph_writer.add_uint32(1, static_cast(char_code)); } if (glyph.width > 0) { - glyph_writer.add_bytes(2,glyph.bitmap); + glyph_writer.add_bytes(2, glyph.bitmap); } // direct type conversions, no need for checking or casting - glyph_writer.add_uint32(3,glyph.width); - glyph_writer.add_uint32(4,glyph.width); - glyph_writer.add_sint32(5,glyph.width); + glyph_writer.add_uint32(3, glyph.width); + glyph_writer.add_uint32(4, glyph.width); + glyph_writer.add_sint32(5, glyph.width); // conversions requiring checks, for safety and correctness @@ -359,16 +359,15 @@ void RangeAsync(uv_work_t* req) { if (top < std::numeric_limits::min() || top > std::numeric_limits::max()) { throw std::runtime_error("Invalid value for glyph.top-glyph.ascender"); } else { - glyph_writer.add_sint32(6,static_cast(top)); + glyph_writer.add_sint32(6, static_cast(top)); } // double to uint if (glyph.advance < std::numeric_limits::min() || glyph.advance > std::numeric_limits::max()) { throw std::runtime_error("Invalid value for glyph.top-glyph.ascender"); } else { - glyph_writer.add_uint32(7,static_cast(glyph.advance)); + glyph_writer.add_uint32(7, static_cast(glyph.advance)); } - } } else { baton->error_name = std::string("font does not have family_name"); From fcd980e4466c6f32703bac3ef386aabe1cc224dc Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Wed, 28 Aug 2019 08:10:56 -0700 Subject: [PATCH 4/4] fix copypaste bug - why did this not trigger test failures? --- src/glyphs.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/glyphs.cpp b/src/glyphs.cpp index 9746c0908..cf38112c2 100644 --- a/src/glyphs.cpp +++ b/src/glyphs.cpp @@ -349,8 +349,8 @@ void RangeAsync(uv_work_t* req) { // direct type conversions, no need for checking or casting glyph_writer.add_uint32(3, glyph.width); - glyph_writer.add_uint32(4, glyph.width); - glyph_writer.add_sint32(5, glyph.width); + glyph_writer.add_uint32(4, glyph.height); + glyph_writer.add_sint32(5, glyph.left); // conversions requiring checks, for safety and correctness