Skip to content

Commit

Permalink
add C++ parser for the font shorthand
Browse files Browse the repository at this point in the history
This is the first part needed for the new font stack, which will
look like the FontFace-based API the browsers have. FontFace uses
a parser for font-family, font-size, etc., so I will need deeper
control over the parser.

FontFace will be implemented in C++ and I didn't want to carry
over the awkward (and slow) switching between JS and C++. So here
it is. I used Claude to generate initial classes and busy work,
but it's been heavily examined and heavily modified.

Caching aside, this is 3x faster in the benchmarks, but in some
cases was 400x faster, which IIRC is why we cached it. I expect
the C++ version to have a much more stable performance profile, so
not going to cache for now.

It's also far more correct than what we had!
  • Loading branch information
chearon committed Dec 29, 2024
1 parent f22223c commit 93b4be1
Show file tree
Hide file tree
Showing 12 changed files with 1,130 additions and 210 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ project adheres to [Semantic Versioning](http://semver.org/).
(Unreleased)
==================
### Changed
* `ctx.font` has a new C++ parser and is 2x-400x faster. Please file an issue if you experience different results, as caching has been removed.

### Added
### Fixed

Expand Down
3 changes: 2 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
'src/Image.cc',
'src/ImageData.cc',
'src/init.cc',
'src/register_font.cc'
'src/register_font.cc',
'src/FontParser.cc'
],
'conditions': [
['OS=="win"', {
Expand Down
3 changes: 0 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const Canvas = require('./lib/canvas')
const Image = require('./lib/image')
const CanvasRenderingContext2D = require('./lib/context2d')
const CanvasPattern = require('./lib/pattern')
const parseFont = require('./lib/parse-font')
const packageJson = require('./package.json')
const bindings = require('./lib/bindings')
const fs = require('fs')
Expand All @@ -12,7 +11,6 @@ const JPEGStream = require('./lib/jpegstream')
const { DOMPoint, DOMMatrix } = require('./lib/DOMMatrix')

bindings.setDOMMatrix(DOMMatrix)
bindings.setParseFont(parseFont)

function createCanvas (width, height, type) {
return new Canvas(width, height, type)
Expand Down Expand Up @@ -73,7 +71,6 @@ exports.DOMPoint = DOMPoint

exports.registerFont = registerFont
exports.deregisterAllFonts = deregisterAllFonts
exports.parseFont = parseFont

exports.createCanvas = createCanvas
exports.createImageData = createImageData
Expand Down
110 changes: 0 additions & 110 deletions lib/parse-font.js

This file was deleted.

39 changes: 38 additions & 1 deletion src/Canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "Util.h"
#include <vector>
#include "node_buffer.h"
#include "FontParser.h"

#ifdef HAVE_JPEG
#include "JPEGStream.h"
Expand Down Expand Up @@ -68,7 +69,8 @@ Canvas::Initialize(Napi::Env& env, Napi::Object& exports) {
StaticValue("PNG_FILTER_PAETH", Napi::Number::New(env, PNG_FILTER_PAETH), napi_default_jsproperty),
StaticValue("PNG_ALL_FILTERS", Napi::Number::New(env, PNG_ALL_FILTERS), napi_default_jsproperty),
StaticMethod<&Canvas::RegisterFont>("_registerFont", napi_default_method),
StaticMethod<&Canvas::DeregisterAllFonts>("_deregisterAllFonts", napi_default_method)
StaticMethod<&Canvas::DeregisterAllFonts>("_deregisterAllFonts", napi_default_method),
StaticMethod<&Canvas::ParseFont>("parseFont", napi_default_method)
});

data->CanvasCtor = Napi::Persistent(ctor);
Expand Down Expand Up @@ -694,6 +696,7 @@ Canvas::RegisterFont(const Napi::CallbackInfo& info) {
// now check the attrs, there are many ways to be wrong
Napi::Object js_user_desc = info[1].As<Napi::Object>();

// TODO: use FontParser on these values just like the FontFace API works
char *family = str_value(js_user_desc.Get("family"), NULL, false);
char *weight = str_value(js_user_desc.Get("weight"), "normal", true);
char *style = str_value(js_user_desc.Get("style"), "normal", false);
Expand Down Expand Up @@ -749,6 +752,40 @@ Canvas::DeregisterAllFonts(const Napi::CallbackInfo& info) {
if (!success) Napi::Error::New(env, "Could not deregister one or more fonts").ThrowAsJavaScriptException();
}

/*
* Do not use! This is only exported for testing
*/
Napi::Value
Canvas::ParseFont(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();

if (info.Length() != 1) return env.Undefined();

Napi::String str;
if (!info[0].ToString().UnwrapTo(&str)) return env.Undefined();

bool ok;
auto props = FontParser::parse(str, &ok);
if (!ok) return env.Undefined();

Napi::Object obj = Napi::Object::New(env);
obj.Set("size", Napi::Number::New(env, props.fontSize));
Napi::Array families = Napi::Array::New(env);
obj.Set("families", families);

unsigned int index = 0;

for (auto& family : props.fontFamily) {
families[index++] = Napi::String::New(env, family);
}

obj.Set("weight", Napi::Number::New(env, props.fontWeight));
obj.Set("variant", Napi::Number::New(env, static_cast<int>(props.fontVariant)));
obj.Set("style", Napi::Number::New(env, static_cast<int>(props.fontStyle)));

return obj;
}

/*
* Get a PangoStyle from a CSS string (like "italic")
*/
Expand Down
1 change: 1 addition & 0 deletions src/Canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Canvas : public Napi::ObjectWrap<Canvas> {
void StreamJPEGSync(const Napi::CallbackInfo& info);
static void RegisterFont(const Napi::CallbackInfo& info);
static void DeregisterAllFonts(const Napi::CallbackInfo& info);
static Napi::Value ParseFont(const Napi::CallbackInfo& info);
Napi::Error CairoError(cairo_status_t status);
static void ToPngBufferAsync(Closure* closure);
static void ToJpegBufferAsync(Closure* closure);
Expand Down
40 changes: 18 additions & 22 deletions src/CanvasRenderingContext2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "CanvasGradient.h"
#include "CanvasPattern.h"
#include "InstanceData.h"
#include "FontParser.h"
#include <cmath>
#include <cstdlib>
#include "Image.h"
Expand Down Expand Up @@ -2575,34 +2576,29 @@ Context2d::GetFont(const Napi::CallbackInfo& info) {

void
Context2d::SetFont(const Napi::CallbackInfo& info, const Napi::Value& value) {
InstanceData* data = env.GetInstanceData<InstanceData>();

if (!value.IsString()) return;

if (!value.As<Napi::String>().Utf8Value().length()) return;

Napi::Value mparsed;
std::string str = value.As<Napi::String>().Utf8Value();
if (!str.length()) return;

// parseFont returns undefined for invalid CSS font strings
if (!data->parseFont.Call({ value }).UnwrapTo(&mparsed) || mparsed.IsUndefined()) return;

Napi::Object font = mparsed.As<Napi::Object>();

Napi::String empty = Napi::String::New(env, "");
Napi::Number zero = Napi::Number::New(env, 0);

std::string weight = font.Get("weight").UnwrapOr(empty).ToString().UnwrapOr(empty).Utf8Value();
std::string style = font.Get("style").UnwrapOr(empty).ToString().UnwrapOr(empty).Utf8Value();
double size = font.Get("size").UnwrapOr(zero).ToNumber().UnwrapOr(zero).DoubleValue();
std::string unit = font.Get("unit").UnwrapOr(empty).ToString().UnwrapOr(empty).Utf8Value();
std::string family = font.Get("family").UnwrapOr(empty).ToString().UnwrapOr(empty).Utf8Value();
bool success;
auto props = FontParser::parse(str, &success);
if (!success) return;

PangoFontDescription *desc = pango_font_description_copy(state->fontDescription);
pango_font_description_free(state->fontDescription);

pango_font_description_set_style(desc, Canvas::GetStyleFromCSSString(style.c_str()));
pango_font_description_set_weight(desc, Canvas::GetWeightFromCSSString(weight.c_str()));
PangoStyle style = props.fontStyle == FontStyle::Italic ? PANGO_STYLE_ITALIC
: props.fontStyle == FontStyle::Oblique ? PANGO_STYLE_OBLIQUE
: PANGO_STYLE_NORMAL;
pango_font_description_set_style(desc, style);

pango_font_description_set_weight(desc, static_cast<PangoWeight>(props.fontWeight));

std::string family = props.fontFamily.empty() ? "" : props.fontFamily[0];
for (size_t i = 1; i < props.fontFamily.size(); i++) {
family += "," + props.fontFamily[i];
}
if (family.length() > 0) {
// See #1643 - Pango understands "sans" whereas CSS uses "sans-serif"
std::string s1(family);
Expand All @@ -2617,12 +2613,12 @@ Context2d::SetFont(const Napi::CallbackInfo& info, const Napi::Value& value) {
PangoFontDescription *sys_desc = Canvas::ResolveFontDescription(desc);
pango_font_description_free(desc);

if (size > 0) pango_font_description_set_absolute_size(sys_desc, size * PANGO_SCALE);
if (props.fontSize > 0) pango_font_description_set_absolute_size(sys_desc, props.fontSize * PANGO_SCALE);

state->fontDescription = sys_desc;
pango_layout_set_font_description(_layout, sys_desc);

state->font = value.As<Napi::String>().Utf8Value().c_str();
state->font = str;
}

/*
Expand Down
Loading

0 comments on commit 93b4be1

Please sign in to comment.