deno.land / x / skia_canvas@0.5.8 / native / src / font.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "include/font.hpp"
int systemFontsLoaded = -1;sk_sp<SkFontMgr> fontMgr = nullptr;sk_sp<skia::textlayout::FontCollection> fontCollection = nullptr;sk_sp<skia::textlayout::TypefaceFontProvider> assets = nullptr;
extern "C" { void setup_font_collection() { if (fontCollection == nullptr) { fontMgr = SkFontMgr::RefDefault(); fontCollection = sk_sp(new skia::textlayout::FontCollection()); assets = sk_sp(new skia::textlayout::TypefaceFontProvider()); fontCollection->setDefaultFontManager(fontMgr); fontCollection->setAssetFontManager(assets); } } int fonts_register_path(const char* path, char* alias) { auto tf = fontMgr->makeFromFile(path); auto result = assets->registerTypeface(tf); if (alias != nullptr) assets->registerTypeface(tf, SkString(alias)); return (int) result; } int fonts_register_memory(const void* data, size_t length, char* alias) { auto tf = fontMgr->makeFromData(sk_sp<SkData>(SkData::MakeWithoutCopy(data, length))); auto result = assets->registerTypeface(tf); if (alias != nullptr) assets->registerTypeface(tf, SkString(alias)); return (int) result; } int fonts_register_dir(char* path) { // Recursively register all fonts in a directory int count = 0; for (std::filesystem::recursive_directory_iterator i(path), end; i != end; ++i) { if (!std::filesystem::is_directory(i->path())) { auto ext = i->path().extension(); if (ext == ".ttf" || ext == ".otf" || ext == ".ttc" || ext == ".pfb" || ext == ".woff" || ext == ".woff2") { fonts_register_path((const char*) i->path().c_str(), nullptr); count++; } } } return count; } int load_system_fonts() { if (systemFontsLoaded == -1) { systemFontsLoaded = 0; #if defined(__APPLE__) systemFontsLoaded = fonts_register_dir("/System/Library/Fonts"); #endif #if defined(__linux__) systemFontsLoaded = fonts_register_dir("/usr/share/fonts"); #endif #if defined(_WIN32) systemFontsLoaded = fonts_register_dir("C:\\Windows\\Fonts"); #endif } return systemFontsLoaded; } void fonts_set_alias(char* alias, char* family) { auto style = SkFontStyle(); auto typeface = assets->matchFamilyStyle(family, style); assets->registerTypeface(sk_sp(typeface), SkString(alias)); } int fonts_count() { return assets->countFamilies(); } char* fonts_family(int index) { auto family = new SkString(); assets->getFamilyName(index, family); return (char*) family->c_str(); }}
skia_canvas

Version Info

Tagged at
8 months ago