Skip to content

Commit c24a669

Browse files
committed
backup
1 parent e04f759 commit c24a669

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+4360
-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
@@ -379,19 +379,35 @@ export function createImageData(width: number, height: number): ImageData
379379
*/
380380
export function loadImage(src: string|Buffer, options?: any): Promise<Image>
381381

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

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

396412
/** This class must not be constructed directly; use `canvas.createPNGStream()`. */
397413
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

pkg/freetype/build.zig

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) !void {
4+
const target = b.standardTargetOptions(.{});
5+
const optimize = b.standardOptimizeOption(.{});
6+
7+
const upstream = b.dependency("freetype", .{});
8+
const lib = b.addLibrary(.{
9+
.name = "freetype",
10+
.linkage = .static,
11+
.root_module = b.createModule(.{
12+
.target = target,
13+
.optimize = optimize,
14+
.link_libc = true,
15+
}),
16+
});
17+
18+
lib.addIncludePath(upstream.path("include"));
19+
lib.installHeadersDirectory(upstream.path("include"), "", .{});
20+
21+
// Dependencies
22+
const zlib_dep = b.dependency("zlib", .{ .target = target, .optimize = optimize });
23+
lib.root_module.linkLibrary(zlib_dep.artifact("z"));
24+
25+
const libpng_dep = b.dependency("libpng", .{ .target = target, .optimize = optimize });
26+
lib.root_module.linkLibrary(libpng_dep.artifact("png"));
27+
28+
var flags: std.ArrayList([]const u8) = .empty;
29+
defer flags.deinit(b.allocator);
30+
31+
try flags.appendSlice(b.allocator, &.{
32+
"-DFT2_BUILD_LIBRARY",
33+
34+
"-DFT_CONFIG_OPTION_SYSTEM_ZLIB",
35+
"-DFT_CONFIG_OPTION_USE_PNG",
36+
37+
"-DHAVE_UNISTD_H",
38+
"-DHAVE_FCNTL_H",
39+
40+
"-fno-sanitize=undefined",
41+
});
42+
43+
lib.root_module.addCSourceFiles(.{
44+
.root = upstream.path(""),
45+
.files = srcs,
46+
.flags = flags.items,
47+
});
48+
49+
switch (target.result.os.tag) {
50+
.linux => lib.root_module.addCSourceFile(.{
51+
.file = upstream.path("builds/unix/ftsystem.c"),
52+
.flags = flags.items,
53+
}),
54+
.windows => lib.root_module.addCSourceFile(.{
55+
.file = upstream.path("builds/windows/ftsystem.c"),
56+
.flags = flags.items,
57+
}),
58+
else => lib.root_module.addCSourceFile(.{
59+
.file = upstream.path("src/base/ftsystem.c"),
60+
.flags = flags.items,
61+
}),
62+
}
63+
switch (target.result.os.tag) {
64+
.windows => {
65+
lib.root_module.addCSourceFile(.{
66+
.file = upstream.path("builds/windows/ftdebug.c"),
67+
.flags = flags.items,
68+
});
69+
lib.root_module.addWin32ResourceFile(.{
70+
.file = upstream.path("src/base/ftver.rc"),
71+
});
72+
},
73+
else => lib.root_module.addCSourceFile(.{
74+
.file = upstream.path("src/base/ftdebug.c"),
75+
.flags = flags.items,
76+
}),
77+
}
78+
79+
b.installArtifact(lib);
80+
}
81+
82+
const srcs: []const []const u8 = &.{
83+
"src/autofit/autofit.c",
84+
"src/base/ftbase.c",
85+
"src/base/ftbbox.c",
86+
"src/base/ftbdf.c",
87+
"src/base/ftbitmap.c",
88+
"src/base/ftcid.c",
89+
"src/base/ftfstype.c",
90+
"src/base/ftgasp.c",
91+
"src/base/ftglyph.c",
92+
"src/base/ftgxval.c",
93+
"src/base/ftinit.c",
94+
"src/base/ftmm.c",
95+
"src/base/ftotval.c",
96+
"src/base/ftpatent.c",
97+
"src/base/ftpfr.c",
98+
"src/base/ftstroke.c",
99+
"src/base/ftsynth.c",
100+
"src/base/fttype1.c",
101+
"src/base/ftwinfnt.c",
102+
"src/bdf/bdf.c",
103+
"src/bzip2/ftbzip2.c",
104+
"src/cache/ftcache.c",
105+
"src/cff/cff.c",
106+
"src/cid/type1cid.c",
107+
"src/gzip/ftgzip.c",
108+
"src/lzw/ftlzw.c",
109+
"src/pcf/pcf.c",
110+
"src/pfr/pfr.c",
111+
"src/psaux/psaux.c",
112+
"src/pshinter/pshinter.c",
113+
"src/psnames/psnames.c",
114+
"src/raster/raster.c",
115+
"src/sdf/sdf.c",
116+
"src/sfnt/sfnt.c",
117+
"src/smooth/smooth.c",
118+
"src/svg/svg.c",
119+
"src/truetype/truetype.c",
120+
"src/type1/type1.c",
121+
"src/type42/type42.c",
122+
"src/winfonts/winfnt.c",
123+
};

pkg/freetype/build.zig.zon

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.{
2+
.name = .freetype,
3+
.version = "0.0.0",
4+
.dependencies = .{
5+
.freetype = .{
6+
.url = "https://github.com/freetype/freetype/archive/refs/tags/VER-2-14-1.tar.gz",
7+
.hash = "N-V-__8AAJx1qgDwI7KTM6yDZ4ArrZU6qmKKuvDHRVrxUkTw",
8+
},
9+
// TODO: this needs to be kept in sync with the root build.zig.zon
10+
.zlib = .{
11+
.url = "git+https://github.com/allyourcodebase/zlib#3599c16d41dbe749ae51b0ff7ab864c61adc779a",
12+
.hash = "zlib-1.3.1-ZZQ7lc8NAABUbHzDe_cSWboCqMbrLkVwvFkKnojgeiT2",
13+
},
14+
// TODO: this needs to be kept in sync with the root build.zig.zon
15+
.libpng = .{
16+
.url = "https://github.com/allyourcodebase/libpng/archive/d512607515687aa60b975d6a191aef9a692dac87.zip",
17+
.hash = "libpng-1.6.50-oiaFGt4qAAB_vzrtvz9hA7_UiBW4arSaMVGc7ekKMCZ2",
18+
},
19+
},
20+
}

0 commit comments

Comments
 (0)