Skip to content

Commit 3d992ae

Browse files
committed
temp
1 parent ba89686 commit 3d992ae

File tree

8 files changed

+11
-486
lines changed

8 files changed

+11
-486
lines changed

examples/small-pdf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ let y = 80
88
let x = 50
99

1010
ctx.font = '22px Helvetica'
11+
ctx.textDrawingMode = 'glyph'
1112
ctx.fillText('node-canvas pdf', x, y)
1213

1314
ctx.font = '10px Arial'

index.js

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ const CanvasPattern = require('./lib/pattern')
55
const parseFont = require('./lib/parse-font')
66
const packageJson = require('./package.json')
77
const bindings = require('./lib/bindings')
8-
const fs = require('fs')
98
const PNGStream = require('./lib/pngstream')
109
const PDFStream = require('./lib/pdfstream')
1110
const JPEGStream = require('./lib/jpegstream')
1211
const { DOMPoint, DOMMatrix } = require('./lib/DOMMatrix')
12+
const registeredFonts = require('./lib/registeredFonts')
1313

1414
bindings.setDOMMatrix(DOMMatrix)
1515
bindings.setParseFont(parseFont)
@@ -38,26 +38,6 @@ function loadImage (src) {
3838
})
3939
}
4040

41-
/**
42-
* Resolve paths for registerFont. Must be called *before* creating a Canvas
43-
* instance.
44-
* @param src {string} Path to font file.
45-
* @param fontFace {{family: string, weight?: string, style?: string}} Object
46-
* specifying font information. `weight` and `style` default to `"normal"`.
47-
*/
48-
function registerFont (src, fontFace) {
49-
// TODO this doesn't need to be on Canvas; it should just be a static method
50-
// of `bindings`.
51-
return Canvas._registerFont(fs.realpathSync(src), fontFace)
52-
}
53-
54-
/**
55-
* Unload all fonts from pango to free up memory
56-
*/
57-
function deregisterAllFonts () {
58-
return Canvas._deregisterAllFonts()
59-
}
60-
6141
exports.Canvas = Canvas
6242
exports.Context2d = CanvasRenderingContext2D // Legacy/compat export
6343
exports.CanvasRenderingContext2D = CanvasRenderingContext2D
@@ -71,8 +51,7 @@ exports.JPEGStream = JPEGStream
7151
exports.DOMMatrix = DOMMatrix
7252
exports.DOMPoint = DOMPoint
7353

74-
exports.registerFont = registerFont
75-
exports.deregisterAllFonts = deregisterAllFonts
54+
exports.fonts = registeredFonts
7655
exports.parseFont = parseFont
7756

7857
exports.createCanvas = createCanvas

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@
6060
"tsd": "^0.29.0",
6161
"typescript": "^4.2.2"
6262
},
63-
"engines": {
64-
"node": "^18.12.0 || >= 20.9.0"
65-
},
6663
"binary": {
6764
"napi_versions": [7]
6865
},

src/Canvas.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <ctime>
1414
#include <glib.h>
1515
#include "PNG.h"
16-
#include "register_font.h"
1716
#include <sstream>
1817
#include <stdlib.h>
1918
#include <string>

src/CanvasRenderingContext2d.cc

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2568,8 +2568,7 @@ Context2d::GetFont(const Napi::CallbackInfo& info) {
25682568
* Set font:
25692569
* - weight
25702570
* - style
2571-
* - size
2572-
* - unit
2571+
* - size (px)
25732572
* - family
25742573
*/
25752574

@@ -2591,37 +2590,12 @@ Context2d::SetFont(const Napi::CallbackInfo& info, const Napi::Value& value) {
25912590
Napi::String empty = Napi::String::New(env, "");
25922591
Napi::Number zero = Napi::Number::New(env, 0);
25932592

2594-
std::string weight = font.Get("weight").UnwrapOr(empty).ToString().UnwrapOr(empty).Utf8Value();
2595-
std::string style = font.Get("style").UnwrapOr(empty).ToString().UnwrapOr(empty).Utf8Value();
2596-
double size = font.Get("size").UnwrapOr(zero).ToNumber().UnwrapOr(zero).DoubleValue();
2597-
std::string unit = font.Get("unit").UnwrapOr(empty).ToString().UnwrapOr(empty).Utf8Value();
2593+
uint32_t weight = font.Get("weight").UnwrapOr(zero).ToNumber().UnwrapOr(empty).Uint32Value();
2594+
FontDescription::Style = font.Get("style").UnwrapOr(zero).ToNumber().UnwrapOr(empty).Uint32Value();
2595+
float size = font.Get("size").UnwrapOr(zero).ToNumber().UnwrapOr(zero).FloatValue();
25982596
std::string family = font.Get("family").UnwrapOr(empty).ToString().UnwrapOr(empty).Utf8Value();
25992597

2600-
PangoFontDescription *desc = pango_font_description_copy(state->fontDescription);
2601-
pango_font_description_free(state->fontDescription);
2602-
2603-
pango_font_description_set_style(desc, Canvas::GetStyleFromCSSString(style.c_str()));
2604-
pango_font_description_set_weight(desc, Canvas::GetWeightFromCSSString(weight.c_str()));
2605-
2606-
if (family.length() > 0) {
2607-
// See #1643 - Pango understands "sans" whereas CSS uses "sans-serif"
2608-
std::string s1(family);
2609-
std::string s2("sans-serif");
2610-
if (streq_casein(s1, s2)) {
2611-
pango_font_description_set_family(desc, "sans");
2612-
} else {
2613-
pango_font_description_set_family(desc, family.c_str());
2614-
}
2615-
}
2616-
2617-
PangoFontDescription *sys_desc = Canvas::ResolveFontDescription(desc);
2618-
pango_font_description_free(desc);
2619-
2620-
if (size > 0) pango_font_description_set_absolute_size(sys_desc, size * PANGO_SCALE);
2621-
2622-
state->fontDescription = sys_desc;
2623-
pango_layout_set_font_description(_layout, sys_desc);
2624-
2598+
state->fontDescription = FontDescription(family, weight, style, size);
26252599
state->font = value.As<Napi::String>().Utf8Value().c_str();
26262600
}
26272601

src/CanvasRenderingContext2d.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
#include "cairo.h"
66
#include "Canvas.h"
7+
#include "FontDescription.h"
78
#include "color.h"
89
#include "napi.h"
9-
#include <pango/pangocairo.h>
1010
#include <stack>
1111

1212
/*
@@ -26,7 +26,7 @@ struct canvas_state_t {
2626
cairo_pattern_t* strokePattern = nullptr;
2727
cairo_pattern_t* fillGradient = nullptr;
2828
cairo_pattern_t* strokeGradient = nullptr;
29-
PangoFontDescription* fontDescription = nullptr;
29+
FontDescription fontDescription;
3030
std::string font = "10px sans-serif";
3131
cairo_filter_t patternQuality = CAIRO_FILTER_GOOD;
3232
float globalAlpha = 1.f;
@@ -36,11 +36,6 @@ struct canvas_state_t {
3636
canvas_draw_mode_t textDrawingMode = TEXT_DRAW_PATHS;
3737
bool imageSmoothingEnabled = true;
3838

39-
canvas_state_t() {
40-
fontDescription = pango_font_description_from_string("sans");
41-
pango_font_description_set_absolute_size(fontDescription, 10 * PANGO_SCALE);
42-
}
43-
4439
canvas_state_t(const canvas_state_t& other) {
4540
fill = other.fill;
4641
stroke = other.stroke;
@@ -57,14 +52,10 @@ struct canvas_state_t {
5752
shadowOffsetX = other.shadowOffsetX;
5853
shadowOffsetY = other.shadowOffsetY;
5954
textDrawingMode = other.textDrawingMode;
60-
fontDescription = pango_font_description_copy(other.fontDescription);
55+
fontDescription = other.fontDescription;
6156
font = other.font;
6257
imageSmoothingEnabled = other.imageSmoothingEnabled;
6358
}
64-
65-
~canvas_state_t() {
66-
pango_font_description_free(fontDescription);
67-
}
6859
};
6960

7061
/*

0 commit comments

Comments
 (0)