Skip to content

Commit 386d65b

Browse files
committed
backup
1 parent a1752ae commit 386d65b

33 files changed

+4077
-85
lines changed

build.zig

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn build(b: *std.Build) void {
1111
.use_glib = false,
1212
.use_dwrite = false,
1313
.use_fontconfig = false,
14-
.use_freetype = false,
14+
.use_freetype = true,
1515
.use_quartz = false,
1616
.target = target,
1717
.optimize = optimize,
@@ -31,6 +31,34 @@ pub fn build(b: *std.Build) void {
3131
.target = target,
3232
.optimize = optimize,
3333
}).artifact("png");
34+
35+
const sheenbidi = b.dependency("sheenbidi", .{
36+
.target = target,
37+
.optimize = optimize,
38+
}).artifact("sheenbidi");
39+
40+
const zg = b.dependency("zg", .{
41+
.target = target,
42+
.optimize = optimize,
43+
});
44+
45+
const harfbuzz = b.dependency("harfbuzz", .{
46+
.target = target,
47+
.optimize = optimize,
48+
}).artifact("harfbuzz");
49+
50+
const unicode = b.addLibrary(.{
51+
.name = "unicode",
52+
.linkage = .static,
53+
.root_module = b.createModule(.{
54+
.target = target,
55+
.optimize = optimize,
56+
.root_source_file = b.path("src/unicode.zig"),
57+
})
58+
});
59+
60+
unicode.root_module.addImport("Scripts", zg.module("Scripts"));
61+
unicode.root_module.addImport("Graphemes", zg.module("Graphemes"));
3462

3563
const canvas = b.addLibrary(.{
3664
.name = "canvas",
@@ -58,11 +86,18 @@ pub fn build(b: *std.Build) void {
5886
"src/Image.cc",
5987
"src/ImageData.cc",
6088
"src/init.cc",
61-
"src/FontParser.cc"
89+
"src/itemize.cc",
90+
"src/FontManager.cc",
91+
"src/FontManagerMacos.cc",
92+
"src/FontFace.cc",
93+
"src/FontFaceSet.cc",
94+
"src/FontParser.cc",
95+
"src/FontLayout.cc"
6296
} ,
6397
.flags = &.{
6498
"-DNAPI_DISABLE_CPP_EXCEPTIONS",
6599
"-DNODE_ADDON_API_ENABLE_MAYBE",
100+
"-std=c++20",
66101
if (target.result.os.tag == .windows) "-DCAIRO_WIN32_STATIC_BUILD" else "",
67102
}
68103
});
@@ -80,6 +115,9 @@ pub fn build(b: *std.Build) void {
80115
canvas.linkLibrary(libjpeg_turbo);
81116
canvas.linkLibrary(libpng);
82117
canvas.linkLibrary(giflib);
118+
canvas.linkLibrary(sheenbidi);
119+
canvas.linkLibrary(unicode);
120+
canvas.linkLibrary(harfbuzz);
83121

84122
const move = b.addInstallFile(canvas.getEmittedBin(), "../bin/canvas.node");
85123
move.step.dependOn(&canvas.step);

build.zig.zon

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
.hash = "node_api-0.0.0-AAAAAAQgAAB-W9HyzJg5Vhja3uNMMcDiFaCJvBLt41GD",
1111
},
1212
.cairo = .{
13-
.url = "https://github.com/chearon/cairo.zig/archive/6b4c3e269483200576b17c160d2511e1e75e0c26.tar.gz",
14-
.hash = "cairo_zig-1.18.4-GbxI8fJhAQD27f7GkMyojP0v6K8DlNxetysEyAcu1qDD",
13+
.url = "https://github.com/chearon/cairo.zig/archive/9e251c4de5b9cc327043c2b7bc970f2909df6b47.tar.gz",
14+
.hash = "cairo_zig-1.18.4-GbxI8cBhAQBTj4mtJt12p34Tyxcdh-r3qnmkXqQRkCNq",
1515
},
16+
// TODO: this needs to be kept in sync with freetype's build.zig.zon
1617
.libpng = .{
1718
.url = "https://github.com/allyourcodebase/libpng/archive/d512607515687aa60b975d6a191aef9a692dac87.zip",
1819
.hash = "libpng-1.6.50-oiaFGt4qAAB_vzrtvz9hA7_UiBW4arSaMVGc7ekKMCZ2",
@@ -25,5 +26,17 @@
2526
.url = "https://github.com/chearon/giflib/archive/986b2816254c3cebf7f9f735eb32564e4600cc66.tar.gz",
2627
.hash = "giflib-5.2.2-TakyOnIKIwCYvNoi8kEaGj5KYEc9hZ900YBld700u-PM",
2728
},
29+
.zg = .{
30+
.url = "https://codeberg.org/atman/zg/archive/d9f596626e8ec05a9f3e47f7bc83aedd5bd2f989.tar.gz",
31+
.hash = "zg-0.15.3-oGqU3P5ItAJHmwDLfzX6sg6Xc5MhSJU2TctHfWu_sKLv",
32+
},
33+
// TODO: this needs to be kept in sync with freetype's build.zig.zon
34+
.zlib = .{
35+
.url = "git+https://github.com/allyourcodebase/zlib#3599c16d41dbe749ae51b0ff7ab864c61adc779a",
36+
.hash = "zlib-1.3.1-ZZQ7lc8NAABUbHzDe_cSWboCqMbrLkVwvFkKnojgeiT2",
37+
},
38+
.sheenbidi = .{ .path = "./pkg/sheenbidi" },
39+
.harfbuzz = .{ .path = "./pkg/harfbuzz" },
40+
.freetype = .{ .path = "./pkg/freetype" },
2841
},
2942
}

examples/font.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fs = require('fs')
22
const path = require('path')
3-
const Canvas = require('..')
3+
const {createCanvas, FontFace, fonts} = require('..')
44

55
function fontFile (name) {
66
return path.join(__dirname, '/pfennigFont/', name)
@@ -10,12 +10,12 @@ function fontFile (name) {
1010
// `registerFont`. When you set `ctx.font`, refer to the styles and the family
1111
// name as it is embedded in the TTF. If you aren't sure, open the font in
1212
// FontForge and visit Element -> Font Information and copy the Family Name
13-
Canvas.registerFont(fontFile('Pfennig.ttf'), { family: 'pfennigFont' })
14-
Canvas.registerFont(fontFile('PfennigBold.ttf'), { family: 'pfennigFont', weight: 'bold' })
15-
Canvas.registerFont(fontFile('PfennigItalic.ttf'), { family: 'pfennigFont', style: 'italic' })
16-
Canvas.registerFont(fontFile('PfennigBoldItalic.ttf'), { family: 'pfennigFont', weight: 'bold', style: 'italic' })
13+
fonts.add(new FontFace('pfennigFont', fontFile('Pfennig.ttf')));
14+
fonts.add(new FontFace('pfennigFont', fontFile('PfennigBold.ttf'), { weight: 'bold' }))
15+
fonts.add(new FontFace('pfennigFont', fontFile('PfennigItalic.ttf'), { style: 'italic' }))
16+
fonts.add(new FontFace('pfennigFont', fontFile('PfennigBoldItalic.ttf'), { weight: 'bold', style: 'italic' }))
1717

18-
const canvas = Canvas.createCanvas(320, 320)
18+
const canvas = createCanvas(320, 320)
1919
const ctx = canvas.getContext('2d')
2020

2121
ctx.font = 'normal normal 50px Helvetica'

index.d.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -377,19 +377,35 @@ export function createImageData(width: number, height: number): ImageData
377377
*/
378378
export function loadImage(src: string|Buffer, options?: any): Promise<Image>
379379

380-
/**
381-
* Registers a font that is not installed as a system font. This must be used
382-
* before creating Canvas instances.
383-
* @param path Path to local font file.
384-
* @param fontFace Description of the font face, corresponding to CSS properties
385-
* used in `@font-face` rules.
386-
*/
387-
export function registerFont(path: string, fontFace: {family: string, weight?: string, style?: string}): void
380+
interface FontFaceDescriptors {
381+
weight?: number;
382+
/** Currently italic, bold, and oblique are supported */
383+
style?: string;
384+
}
388385

389-
/**
390-
* Unloads all fonts
391-
*/
392-
export function deregisterAllFonts(): void;
386+
export class FontFace {
387+
constructor(
388+
family: string,
389+
url: string | ArrayBuffer | TypedArray,
390+
descriptors?: FontFaceDescriptors
391+
);
392+
393+
family: string;
394+
style: string;
395+
weight: number;
396+
status: 'loaded' | 'unloaded' | 'error';
397+
}
398+
399+
class FontFaceSet {
400+
add(face: FontFace): void;
401+
has(face: FontFace): boolean;
402+
clear(): void;
403+
delete(face: FontFace): boolean;
404+
[Symbol.iterator](): Iterator<FontFace>;
405+
size: number;
406+
}
407+
408+
export const fonts: FontFaceSet;
393409

394410
/** This class must not be constructed directly; use `canvas.createPNGStream()`. */
395411
export class PNGStream extends Readable {}

index.js

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const CanvasRenderingContext2D = require('./lib/context2d')
44
const CanvasPattern = require('./lib/pattern')
55
const packageJson = require('./package.json')
66
const bindings = require('./lib/bindings')
7-
const fs = require('fs')
87
const PNGStream = require('./lib/pngstream')
98
const PDFStream = require('./lib/pdfstream')
109
const JPEGStream = require('./lib/jpegstream')
@@ -36,31 +35,12 @@ function loadImage (src) {
3635
})
3736
}
3837

39-
/**
40-
* Resolve paths for registerFont. Must be called *before* creating a Canvas
41-
* instance.
42-
* @param src {string} Path to font file.
43-
* @param fontFace {{family: string, weight?: string, style?: string}} Object
44-
* specifying font information. `weight` and `style` default to `"normal"`.
45-
*/
46-
function registerFont (src, fontFace) {
47-
// TODO this doesn't need to be on Canvas; it should just be a static method
48-
// of `bindings`.
49-
return Canvas._registerFont(fs.realpathSync(src), fontFace)
50-
}
51-
52-
/**
53-
* Unload all fonts from pango to free up memory
54-
*/
55-
function deregisterAllFonts () {
56-
return Canvas._deregisterAllFonts()
57-
}
58-
5938
exports.Canvas = Canvas
6039
exports.Context2d = CanvasRenderingContext2D // Legacy/compat export
6140
exports.CanvasRenderingContext2D = CanvasRenderingContext2D
6241
exports.CanvasGradient = bindings.CanvasGradient
6342
exports.CanvasPattern = CanvasPattern
43+
exports.FontFace = bindings.FontFace
6444
exports.Image = Image
6545
exports.ImageData = bindings.ImageData
6646
exports.PNGStream = PNGStream
@@ -69,8 +49,7 @@ exports.JPEGStream = JPEGStream
6949
exports.DOMMatrix = DOMMatrix
7050
exports.DOMPoint = DOMPoint
7151

72-
exports.registerFont = registerFont
73-
exports.deregisterAllFonts = deregisterAllFonts
52+
exports.fonts = bindings.fonts;
7453

7554
exports.createCanvas = createCanvas
7655
exports.createImageData = createImageData

lib/bindings.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,8 @@ Object.defineProperty(bindings.CanvasRenderingContext2d.prototype, Symbol.toStri
4141
value: 'CanvasRenderingContext2d',
4242
configurable: true
4343
})
44+
45+
Object.defineProperty(bindings.FontFace.prototype, Symbol.toStringTag, {
46+
value: 'FontFace',
47+
configurable: true
48+
})

package.json

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

src/Canvas.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -654,24 +654,23 @@ Canvas::ParseFont(const Napi::CallbackInfo& info) {
654654
if (!ok) return env.Undefined();
655655

656656
Napi::Object obj = Napi::Object::New(env);
657-
obj.Set("size", Napi::Number::New(env, props.fontSize));
657+
obj.Set("size", Napi::Number::New(env, props.size));
658658
Napi::Array families = Napi::Array::New(env);
659659
obj.Set("families", families);
660660

661661
unsigned int index = 0;
662662

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

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

671671
return obj;
672672
}
673673

674-
675674
// This returns an approximate value only, suitable for
676675
// Napi::MemoryManagement:: AdjustExternalMemory.
677676
// The formats that don't map to intrinsic types (RGB30, A1) round up.

src/Canvas.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,28 @@ typedef enum {
2424
CANVAS_TYPE_SVG
2525
} canvas_type_t;
2626

27+
enum text_baseline_t : uint8_t {
28+
TEXT_BASELINE_ALPHABETIC = 0,
29+
TEXT_BASELINE_TOP = 1,
30+
TEXT_BASELINE_BOTTOM = 2,
31+
TEXT_BASELINE_MIDDLE = 3,
32+
TEXT_BASELINE_IDEOGRAPHIC = 4,
33+
TEXT_BASELINE_HANGING = 5
34+
};
35+
36+
enum text_align_t : int8_t {
37+
TEXT_ALIGNMENT_LEFT = -1,
38+
TEXT_ALIGNMENT_CENTER = 0,
39+
TEXT_ALIGNMENT_RIGHT = 1,
40+
TEXT_ALIGNMENT_START = -2,
41+
TEXT_ALIGNMENT_END = 2
42+
};
43+
44+
enum canvas_draw_mode_t : uint8_t {
45+
TEXT_DRAW_PATHS,
46+
TEXT_DRAW_GLYPHS
47+
};
48+
2749
/*
2850
* Canvas.
2951
*/

0 commit comments

Comments
 (0)