Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to protozero #162

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 4 additions & 27 deletions binding.gyp
Expand Up @@ -9,8 +9,7 @@
'system_includes': [
"-isystem <(module_root_dir)/<!(node -e \"require('nan')\")",
"-isystem <(module_root_dir)/mason_packages/.link/include/",
"-isystem <(module_root_dir)/mason_packages/.link/include/freetype2",
'-isystem <(SHARED_INTERMEDIATE_DIR)'
"-isystem <(module_root_dir)/mason_packages/.link/include/freetype2"
],
# Flags we pass to the compiler to ensure the compiler
# warns us about potentially buggy or dangerous code
Expand Down Expand Up @@ -53,46 +52,24 @@
}
]
},
{
'target_name': 'action_before_build2',
'type': 'none',
'dependencies': [
'action_before_build'
],
'hard_dependency': 1,
'actions': [
{
'action_name': 'run_protoc_glyphs',
'inputs': [
'./proto/glyphs.proto'
],
'outputs': [
"<(SHARED_INTERMEDIATE_DIR)/glyphs.pb.cc"
],
'action': ['./mason_packages/.link/bin/protoc','-Iproto/','--cpp_out=<(SHARED_INTERMEDIATE_DIR)/','./proto/glyphs.proto']
}
]
},
{
# module_name and module_path are both variables passed by node-pre-gyp from package.json
'target_name': '<(module_name)', # sets the name of the binary file
'product_dir': '<(module_path)', # controls where the node binary file gets copied to (./lib/binding/module.node)
'type': 'loadable_module',
'dependencies': [ 'action_before_build2' ],
'dependencies': [ 'action_before_build' ],
# "make" only watches files specified here, and will sometimes cache these files after the first compile.
# This cache can sometimes cause confusing errors when removing/renaming/adding new files.
# Running "make clean" helps to prevent this "mysterious error by cache" scenario
# This also is where the benefits of using a "glob" come into play...
# See: https://github.com/mapbox/node-cpp-skel/pull/44#discussion_r122050205
'sources': [
'src/node_fontnik.cpp',
'src/glyphs.cpp',
'<(SHARED_INTERMEDIATE_DIR)/glyphs.pb.cc'
'src/glyphs.cpp'
],
"link_settings": {
"libraries": [
"-lfreetype",
"-lprotobuf-lite"
"-lfreetype"
],
"library_dirs": [
"<(module_root_dir)/mason_packages/.link/lib"
Expand Down
8 changes: 2 additions & 6 deletions scripts/install_deps.sh
Expand Up @@ -13,10 +13,6 @@ function install() {
source local.env

install boost 1.63.0
#link boost 1.63.0 # is linking needed?
install freetype 2.7.1
#link freetype 2.7.1
install protobuf 3.2.0
#link protobuf 3.2.0
install sdf-glyph-foundry 0.1.1
#link sdf-glyph-foundry 0.1.1
install protozero 1.6.8
install sdf-glyph-foundry 0.1.1
2 changes: 1 addition & 1 deletion scripts/setup.sh
Expand Up @@ -3,7 +3,7 @@
set -eu
set -o pipefail

export MASON_RELEASE="${MASON_RELEASE:-eeba3b5}"
export MASON_RELEASE="${MASON_RELEASE:-master}"
export MASON_LLVM_RELEASE="${MASON_LLVM_RELEASE:-5.0.0}"

PLATFORM=$(uname | tr A-Z a-z)
Expand Down
44 changes: 21 additions & 23 deletions src/glyphs.cpp
@@ -1,6 +1,6 @@
// fontnik
#include "glyphs.hpp"

#include <protozero/pbf_writer.hpp>
// node
#include <limits>
#include <nan.h>
Expand Down Expand Up @@ -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) {
Expand All @@ -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;

Expand All @@ -335,47 +334,46 @@ 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<FT_ULong>::max()) {
throw std::runtime_error("Invalid value for char_code: too large");
} else {
mutable_glyph->set_id(static_cast<std::uint32_t>(char_code));
glyph_writer.add_uint32(1, static_cast<std::uint32_t>(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.height);
glyph_writer.add_sint32(5, glyph.left);

// conversions requiring checks, for safety and correctness

// double to int
double top = static_cast<double>(glyph.top) - glyph.ascender;
if (top < std::numeric_limits<std::int32_t>::min() || top > std::numeric_limits<std::int32_t>::max()) {
throw std::runtime_error("Invalid value for glyph.top-glyph.ascender");
} else {
mutable_glyph->set_top(static_cast<std::int32_t>(top));
glyph_writer.add_sint32(6, static_cast<std::int32_t>(top));
}

// double to uint
if (glyph.advance < std::numeric_limits<std::uint32_t>::min() || glyph.advance > std::numeric_limits<std::uint32_t>::max()) {
throw std::runtime_error("Invalid value for glyph.top-glyph.ascender");
} else {
mutable_glyph->set_advance(static_cast<std::uint32_t>(glyph.advance));
}

if (glyph.width > 0) {
mutable_glyph->set_bitmap(glyph.bitmap);
glyph_writer.add_uint32(7, static_cast<std::uint32_t>(glyph.advance));
}
}
} 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();
}
Expand Down
1 change: 0 additions & 1 deletion src/glyphs.hpp
@@ -1,7 +1,6 @@
#ifndef NODE_FONTNIK_GLYPHS_HPP
#define NODE_FONTNIK_GLYPHS_HPP

#include "glyphs.pb.h"
#include <nan.h>

namespace node_fontnik {
Expand Down