Skip to content

Commit 6129ccf

Browse files
committed
linux
1 parent c24a669 commit 6129ccf

File tree

10 files changed

+423
-12
lines changed

10 files changed

+423
-12
lines changed

build.zig

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub fn build(b: *std.Build) void {
1010
.use_png = true,
1111
.use_glib = false,
1212
.use_dwrite = false,
13-
.use_fontconfig = false,
13+
.use_fontconfig = target.result.os.tag == .linux,
1414
.use_freetype = true,
1515
.use_quartz = false,
1616
.target = target,
@@ -54,6 +54,7 @@ pub fn build(b: *std.Build) void {
5454
.target = target,
5555
.optimize = optimize,
5656
.root_source_file = b.path("src/unicode.zig"),
57+
.link_libc = true,
5758
})
5859
});
5960

@@ -88,7 +89,11 @@ pub fn build(b: *std.Build) void {
8889
"src/init.cc",
8990
"src/itemize.cc",
9091
"src/FontManager.cc",
91-
"src/FontManagerMacos.cc",
92+
switch (target.result.os.tag) {
93+
.macos => "src/FontManagerMacos.cc",
94+
.linux => "src/FontManagerLinux.cc",
95+
else => @panic("unrecognized OS")
96+
},
9297
"src/FontFace.cc",
9398
"src/FontFaceSet.cc",
9499
"src/FontParser.cc",
@@ -97,6 +102,11 @@ pub fn build(b: *std.Build) void {
97102
.flags = &.{
98103
"-DNAPI_DISABLE_CPP_EXCEPTIONS",
99104
"-DNODE_ADDON_API_ENABLE_MAYBE",
105+
switch (target.result.os.tag) {
106+
.macos => "-DCANVAS_MACOS",
107+
.linux => "-DCANVAS_LINUX",
108+
else => @panic("unrecognized OS")
109+
},
100110
"-std=c++20",
101111
if (target.result.os.tag == .windows) "-DCAIRO_WIN32_STATIC_BUILD" else "",
102112
}

src/Font.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct FontDescriptor : FontBase {
3232
// Collection (ttc). macOS CTFontDescriptors don't tell you which index
3333
// they represent in a ttc, so when we begin to draw with a ttc match, we
3434
// select which index to draw with based on which one has this name.
35-
std::unique_ptr<char[]> postscript;
35+
std::unique_ptr<char[]> postscript = nullptr;
3636
std::unique_ptr<char[]> url = nullptr;
3737
std::unique_ptr<uint8_t[]> data = nullptr;
3838
size_t data_len = 0;

src/FontLayout.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,9 @@ layoutText(
434434
nullptr
435435
);
436436
size_t count = hb_face_count(hbblob);
437-
if (count > 1) {
437+
// If there's a postscript name, the backend (macOS) requires us to
438+
// initialize the TTC index
439+
if (count > 1 && matches[matchIndex]->postscript) {
438440
for (size_t index = 0; index < count; index++) {
439441
char buf[128];
440442
unsigned int len = sizeof(buf);

src/FontManager.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ compareFamilyNames(const char* str1, size_t len1, const char* str2, size_t len2)
2929
return true;
3030
}
3131

32-
3332
void
3433
FontManager::narrowByStretch(
3534
std::vector<FontDescriptor*>& fonts,
@@ -230,7 +229,7 @@ FontManager::query(
230229
for (const std::string& family : properties.families) {
231230
auto genericFamilies = getGenericList(family);
232231
if (genericFamilies) {
233-
for (const std::string& family : **genericFamilies) {
232+
for (const std::string& family : *genericFamilies) {
234233
for (FontDescriptor& desc : system_fonts) {
235234
maybeAdd(family, &desc);
236235
}

src/FontManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class FontManager {
1919
script_t script
2020
) = 0;
2121

22-
virtual std::optional<const std::vector<std::string>*> getGenericList(
22+
virtual std::optional<const std::vector<std::string>> getGenericList(
2323
const std::string& generic
2424
) = 0;
2525

src/FontManagerLinux.cc

Lines changed: 380 additions & 0 deletions
Large diffs are not rendered by default.

src/FontManagerLinux.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) 2025 Caleb Hearon <caleb@chearon.net>
2+
3+
#pragma once
4+
5+
#include "FontManager.h"
6+
7+
class FontManagerLinux : public FontManager {
8+
public:
9+
void readSystemFonts(std::vector<FontDescriptor>& properties) override;
10+
void populateFallbackFonts(std::vector<std::string>& families, script_t script) override;
11+
std::optional<const std::vector<std::string>> getGenericList(const std::string& generic) override;
12+
};

src/FontManagerMacos.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <vector>
99
#include <array>
1010
#include <cstring>
11-
#include <iostream>
1211
#include <cmath>
1312

1413
#include "FontManagerMacos.h"
@@ -64,7 +63,7 @@ convertWeight(float aCTWeight) {
6463
return round(prev->second * (1.0 - t) + m->second * t);
6564
}
6665

67-
void
66+
static void
6867
create_font_descriptor(
6968
std::vector<FontDescriptor>& results,
7069
CTFontDescriptorRef descriptor
@@ -654,7 +653,7 @@ const std::vector<std::string> monospace_fonts = {"Menlo"};
654653
const std::vector<std::string> cursive_fonts = {"Apple Chancery"};
655654
const std::vector<std::string> fantasy_fonts = {"Papyrus"};
656655

657-
std::optional<const std::vector<std::string>*>
656+
std::optional<const std::vector<std::string>>
658657
FontManagerMacos::getGenericList(const std::string& generic) {
659658
if (generic == "serif") {
660659
return &serif_fonts;

src/FontManagerMacos.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ class FontManagerMacos : public FontManager {
77
public:
88
void readSystemFonts(std::vector<FontDescriptor>& properties) override;
99
void populateFallbackFonts(std::vector<std::string>& families, script_t script) override;
10-
std::optional<const std::vector<std::string>*> getGenericList(const std::string& generic) override;
10+
std::optional<const std::vector<std::string>> getGenericList(const std::string& generic) override;
1111
};

src/InstanceData.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
#include <napi.h>
44
#include <freetype/freetype.h>
55
#include "FontFaceSet.h"
6-
#include "FontManagerMacos.h" // TODO per-platform
6+
7+
#if defined(CANVAS_LINUX)
8+
#include "FontManagerLinux.h"
9+
#elif defined(CANVAS_MACOS)
10+
#include "FontManagerMacos.h"
11+
#endif
712

813
struct InstanceData {
914
Napi::FunctionReference CanvasCtor;
@@ -18,7 +23,11 @@ struct InstanceData {
1823
Napi::ObjectReference jsFontSet;
1924
FontFaceSet* cppFontSet;
2025
FT_Library ft;
26+
#if defined(CANVAS_LINUX)
27+
FontManagerLinux fontManager;
28+
#elif defined(CANVAS_MACOS)
2129
FontManagerMacos fontManager;
30+
#endif
2231

2332
InstanceData() {
2433
FT_Init_FreeType(&ft);

0 commit comments

Comments
 (0)