xbintsc 0.7.7 → 0.7.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/cli/help.js +1 -1
- package/dist/src/codegen/generator/calls/invocation.js +6 -4
- package/dist/src/codegen/generator/calls/invocation.js.map +1 -1
- package/dist/src/codegen/generator/context.d.ts +4 -3
- package/dist/src/codegen/generator/context.js +6 -5
- package/dist/src/codegen/generator/context.js.map +1 -1
- package/dist/src/codegen/generator/expressions/assignment.js +3 -3
- package/dist/src/driver/config.d.ts +1 -1
- package/dist/src/driver/options.d.ts +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/extensions/gui/features.d.ts +14 -6
- package/dist/src/extensions/gui/features.js +34 -6
- package/dist/src/extensions/gui/features.js.map +1 -1
- package/dist/src/extensions/gui/index.js +3 -3
- package/dist/tests/codegen/llvm.test.js +1 -1
- package/dist/tests/e2e/gui-audio.test.js +24 -0
- package/dist/tests/e2e/gui-audio.test.js.map +1 -1
- package/dist/tests/e2e/gui-draw-perf.test.js +9 -0
- package/dist/tests/e2e/gui-draw-perf.test.js.map +1 -1
- package/dist/tests/e2e/gui-font-face.test.d.ts +3 -1
- package/dist/tests/e2e/gui-font-face.test.js +99 -7
- package/dist/tests/e2e/gui-font-face.test.js.map +1 -1
- package/dist/tests/e2e/gui-frame-pacing.test.d.ts +15 -0
- package/dist/tests/e2e/gui-frame-pacing.test.js +68 -0
- package/dist/tests/e2e/gui-frame-pacing.test.js.map +1 -0
- package/dist/tests/e2e/gui-initial-url.test.d.ts +10 -0
- package/dist/tests/e2e/gui-initial-url.test.js +49 -0
- package/dist/tests/e2e/gui-initial-url.test.js.map +1 -0
- package/dist/tests/e2e/gui-layout.test.js +85 -0
- package/dist/tests/e2e/gui-layout.test.js.map +1 -1
- package/dist/tests/e2e/gui-webgl-depth-init.test.d.ts +13 -0
- package/dist/tests/e2e/gui-webgl-depth-init.test.js +144 -0
- package/dist/tests/e2e/gui-webgl-depth-init.test.js.map +1 -0
- package/dist/tests/e2e/gui-webgl-depth-range.test.d.ts +18 -0
- package/dist/tests/e2e/gui-webgl-depth-range.test.js +140 -0
- package/dist/tests/e2e/gui-webgl-depth-range.test.js.map +1 -0
- package/dist/tests/e2e/gui-webgl.test.js +49 -0
- package/dist/tests/e2e/gui-webgl.test.js.map +1 -1
- package/dist/tests/e2e/gui-webgpu-attachment.test.d.ts +19 -0
- package/dist/tests/e2e/gui-webgpu-attachment.test.js +258 -0
- package/dist/tests/e2e/gui-webgpu-attachment.test.js.map +1 -0
- package/dist/tests/e2e/method-call-cache.test.js +27 -0
- package/dist/tests/e2e/method-call-cache.test.js.map +1 -1
- package/dist/tests/extensions/gui-features.test.js +27 -2
- package/dist/tests/extensions/gui-features.test.js.map +1 -1
- package/doc/ai/extensions.md +10 -4
- package/doc/ai/zh-CN/extensions.md +8 -3
- package/doc/gui/animation.md +1 -1
- package/doc/gui/api.md +35 -2
- package/doc/gui/build.md +34 -15
- package/doc/gui/dom.md +3 -2
- package/doc/gui/known-issues.md +1 -1
- package/doc/gui/layout.md +9 -3
- package/doc/gui/rendering.md +45 -8
- package/doc/gui/runtime.md +13 -0
- package/doc/gui/webgl.md +29 -3
- package/doc/gui/webgpu.md +20 -0
- package/doc/gui.md +9 -4
- package/doc/implemented/codegen.md +4 -3
- package/doc/zh-CN/gui/animation.md +1 -1
- package/doc/zh-CN/gui/api.md +25 -2
- package/doc/zh-CN/gui/build.md +20 -7
- package/doc/zh-CN/gui/dom.md +3 -2
- package/doc/zh-CN/gui/known-issues.md +1 -1
- package/doc/zh-CN/gui/layout.md +7 -4
- package/doc/zh-CN/gui/rendering.md +33 -8
- package/doc/zh-CN/gui/runtime.md +9 -0
- package/doc/zh-CN/gui/webgl.md +7 -4
- package/doc/zh-CN/gui/webgpu.md +18 -0
- package/doc/zh-CN/gui.md +10 -5
- package/doc/zh-CN/implemented/codegen.md +4 -3
- package/package.json +7 -7
- package/runtime/ext_gui/audio_graph_commands.cpp +127 -0
- package/runtime/ext_gui/audio_mixer.cpp +84 -163
- package/runtime/ext_gui/audio_mixer.h +59 -3
- package/runtime/ext_gui/audio_mixer_extra.cpp +8 -0
- package/runtime/ext_gui/audio_param_apply.cpp +160 -0
- package/runtime/ext_gui/browser.cpp +6 -19
- package/runtime/ext_gui/browser_location.cpp +78 -0
- package/runtime/ext_gui/css.cpp +1 -0
- package/runtime/ext_gui/css.h +4 -0
- package/runtime/ext_gui/css_sheet.cpp +4 -2
- package/runtime/ext_gui/document_animation.cpp +7 -0
- package/runtime/ext_gui/dom_api_computed_style.cpp +3 -2
- package/runtime/ext_gui/dom_api_webgl.cpp +39 -2
- package/runtime/ext_gui/dom_api_webgl_extensions.cpp +134 -0
- package/runtime/ext_gui/dom_api_webgl_internal.h +4 -0
- package/runtime/ext_gui/dom_api_webgl_shader.cpp +28 -4
- package/runtime/ext_gui/dom_api_webgl_state.cpp +6 -103
- package/runtime/ext_gui/gui.cpp +100 -1
- package/runtime/ext_gui/gui_events.cpp +8 -2
- package/runtime/ext_gui/layout_flex.cpp +5 -2
- package/runtime/ext_gui/layout_flow.cpp +24 -1
- package/runtime/ext_gui/layout_flow_internal.h +11 -2
- package/runtime/ext_gui/media_query.cpp +3 -0
- package/runtime/ext_gui/perf.cpp +18 -6
- package/runtime/ext_gui/perf.h +37 -0
- package/runtime/ext_gui/style.cpp +28 -10
- package/runtime/ext_gui/style.h +10 -1
- package/runtime/ext_gui/style_length.cpp +4 -0
- package/runtime/ext_gui/style_print.cpp +7 -1
- package/runtime/ext_gui/style_values.cpp +6 -5
- package/runtime/ext_gui/text.cpp +4 -0
- package/runtime/ext_gui/text_font_bytes.cpp +117 -0
- package/runtime/ext_gui/text_font_bytes.h +65 -0
- package/runtime/ext_gui/text_font_db.cpp +5 -0
- package/runtime/ext_gui/text_font_sfnt.cpp +71 -24
- package/runtime/ext_gui/text_font_sfnt.h +5 -2
- package/runtime/ext_gui/text_hb.cpp +34 -5
- package/runtime/ext_gui/text_stb.cpp +7 -30
- package/runtime/ext_gui/text_webfont.cpp +217 -0
- package/runtime/ext_gui/warnings.cpp +26 -0
- package/runtime/ext_gui/warnings.h +8 -0
- package/runtime/ext_gui/webfont_bridge.c +23 -0
- package/runtime/ext_gui/webgl.h +36 -39
- package/runtime/ext_gui/webgl_blit_color.cpp +5 -0
- package/runtime/ext_gui/webgl_buffer_objects.cpp +19 -5
- package/runtime/ext_gui/webgl_context.cpp +2 -0
- package/runtime/ext_gui/webgl_draw.cpp +72 -12
- package/runtime/ext_gui/webgl_draw_setup.cpp +8 -2
- package/runtime/ext_gui/webgl_draw_state.h +13 -2
- package/runtime/ext_gui/webgl_framebuffer.cpp +8 -0
- package/runtime/ext_gui/webgl_objects.h +19 -0
- package/runtime/ext_gui/webgl_program.h +64 -0
- package/runtime/ext_gui/webgl_state.cpp +7 -0
- package/runtime/ext_gui/webgl_targets.cpp +31 -7
- package/runtime/ext_gui/webgl_uniform.cpp +78 -0
- package/runtime/ext_gui/webgpu.h +10 -0
- package/runtime/ext_gui/webgpu_buffer.cpp +3 -1
- package/runtime/ext_gui/webgpu_canvas.cpp +1 -0
- package/runtime/ext_gui/webgpu_command.cpp +51 -11
- package/runtime/ext_gui/webgpu_convert.cpp +7 -2
- package/runtime/ext_gui/webgpu_pass.cpp +12 -0
- package/runtime/ext_gui/webgpu_queue.cpp +3 -0
- package/runtime/ext_gui/webgpu_resources.cpp +1 -0
- package/runtime/ext_gui/window.cpp +6 -0
- package/runtime/rt_object.h +6 -0
- package/runtime/rt_profile.h +6 -0
- package/runtime/xt_containers/access.inc +2 -47
- package/runtime/xt_containers/ic_get.inc +70 -0
- package/runtime/xt_containers/method_cache.inc +42 -11
- package/runtime/xt_containers/objects.inc +12 -1
- package/runtime/xt_containers/shapes.inc +78 -0
- package/runtime/xt_containers.c +1 -0
- package/runtime/xt_profile.c +3 -0
- package/scripts/build-gui.ts +43 -49
- package/scripts/gui-engine-build.ts +58 -0
- package/scripts/gui-vendor-source.ts +53 -0
- package/scripts/gui-webfont-build.ts +138 -0
- package/src/cli/help.ts +1 -1
- package/src/codegen/generator/calls/invocation.ts +6 -4
- package/src/codegen/generator/context.ts +6 -6
- package/src/codegen/generator/expressions/assignment.ts +3 -3
- package/src/driver/config.ts +1 -1
- package/src/driver/options.ts +1 -1
- package/src/driver/version.ts +1 -1
- package/src/extensions/gui/features.ts +34 -6
- package/src/extensions/gui/index.ts +3 -3
|
@@ -9,11 +9,9 @@
|
|
|
9
9
|
#include <cstdlib>
|
|
10
10
|
#include <cstring>
|
|
11
11
|
#include <vector>
|
|
12
|
-
|
|
13
12
|
namespace xtgui {
|
|
14
13
|
|
|
15
14
|
/* -- small helpers -------------------------------------------------------- */
|
|
16
|
-
|
|
17
15
|
static void setEdge(Edges *edges, const std::string &side, Length value) {
|
|
18
16
|
if (side == "top") edges->top = value;
|
|
19
17
|
else if (side == "right") edges->right = value;
|
|
@@ -448,9 +446,12 @@ bool xt_style_apply_declaration(XtStyle *style, const std::string &rawProperty,
|
|
|
448
446
|
if (parseEnum(value, names, 6, &index)) style->align_content = (AlignContent)index;
|
|
449
447
|
return true;
|
|
450
448
|
}
|
|
451
|
-
if (property == "gap") {
|
|
452
|
-
|
|
453
|
-
|
|
449
|
+
if (property == "gap") { /* `gap: <row> <column>`; one value means both axes. */
|
|
450
|
+
std::vector<std::string> parts = splitEdgeTokens(value);
|
|
451
|
+
const std::string &first = parts.empty() ? value : parts[0];
|
|
452
|
+
assignLength(&style->row_gap, first);
|
|
453
|
+
assignLength(&style->column_gap, parts.size() > 1 ? parts[1] : first);
|
|
454
|
+
style->gap = style->row_gap;
|
|
454
455
|
return true;
|
|
455
456
|
}
|
|
456
457
|
if (property == "row-gap") { assignLength(&style->row_gap, value); return true; }
|
package/runtime/ext_gui/text.cpp
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
#include "text_api.h"
|
|
12
12
|
|
|
13
13
|
#include "perf.h"
|
|
14
|
+
#include "text_font_bytes.h"
|
|
14
15
|
#include "text_fonts.h"
|
|
15
16
|
|
|
16
17
|
namespace xtgui {
|
|
@@ -65,6 +66,9 @@ void xt_text_shutdown() {
|
|
|
65
66
|
xt_text_glyph_cache_clear();
|
|
66
67
|
xt_text_api()->shutdown();
|
|
67
68
|
xt_font_db_shutdown();
|
|
69
|
+
/* The decoded font bytes are shared by the database and the backend faces,
|
|
70
|
+
* so they are the last thing released. */
|
|
71
|
+
xt_font_bytes_shutdown();
|
|
68
72
|
}
|
|
69
73
|
|
|
70
74
|
} // namespace xtgui
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — font file loading and webfont decoding (see
|
|
3
|
+
* `text_font_bytes.h`).
|
|
4
|
+
*
|
|
5
|
+
* The process-wide byte cache is keyed by path and holds the *uncompressed*
|
|
6
|
+
* sfnt bytes, so a `.woff2` source is decompressed once and then shared by the
|
|
7
|
+
* metadata reader and whichever text backend draws it. A failed read or decode
|
|
8
|
+
* is cached as null: a page that points at a missing or corrupt font must not
|
|
9
|
+
* retry it on every restyle.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
#include "text_font_bytes.h"
|
|
13
|
+
|
|
14
|
+
#include <cstdio>
|
|
15
|
+
#include <unordered_map>
|
|
16
|
+
|
|
17
|
+
#include "warnings.h"
|
|
18
|
+
|
|
19
|
+
namespace xtgui {
|
|
20
|
+
|
|
21
|
+
namespace {
|
|
22
|
+
|
|
23
|
+
struct ByteCache {
|
|
24
|
+
std::unordered_map<std::string, std::shared_ptr<const std::vector<unsigned char>>> files;
|
|
25
|
+
/** Whether a path is a WOFF/WOFF2 container (its first four bytes). */
|
|
26
|
+
std::unordered_map<std::string, bool> compressed;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
ByteCache &byteCache() {
|
|
30
|
+
static ByteCache instance;
|
|
31
|
+
return instance;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const WebfontDecoder *g_decoder = nullptr;
|
|
35
|
+
|
|
36
|
+
std::shared_ptr<std::vector<unsigned char>> readFile(const std::string &path) {
|
|
37
|
+
FILE *file = std::fopen(path.c_str(), "rb");
|
|
38
|
+
if (file == nullptr) return nullptr;
|
|
39
|
+
if (std::fseek(file, 0, SEEK_END) != 0) {
|
|
40
|
+
std::fclose(file);
|
|
41
|
+
return nullptr;
|
|
42
|
+
}
|
|
43
|
+
const long length = std::ftell(file);
|
|
44
|
+
if (length <= 0 || std::fseek(file, 0, SEEK_SET) != 0) {
|
|
45
|
+
std::fclose(file);
|
|
46
|
+
return nullptr;
|
|
47
|
+
}
|
|
48
|
+
auto bytes = std::make_shared<std::vector<unsigned char>>((size_t)length);
|
|
49
|
+
const size_t read = std::fread(bytes->data(), 1, bytes->size(), file);
|
|
50
|
+
std::fclose(file);
|
|
51
|
+
if (read != bytes->size()) return nullptr;
|
|
52
|
+
return bytes;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
} // namespace
|
|
56
|
+
|
|
57
|
+
void xt_font_webfont_install(const WebfontDecoder *decoder) { g_decoder = decoder; }
|
|
58
|
+
|
|
59
|
+
bool xt_font_webfont_available() { return g_decoder != nullptr; }
|
|
60
|
+
|
|
61
|
+
bool xt_font_is_webfont(const unsigned char *data, size_t size) {
|
|
62
|
+
if (data == nullptr || size < 4) return false;
|
|
63
|
+
const uint32_t tag = ((uint32_t)data[0] << 24) | ((uint32_t)data[1] << 16) |
|
|
64
|
+
((uint32_t)data[2] << 8) | (uint32_t)data[3];
|
|
65
|
+
return tag == kWebfontWoffTag || tag == kWebfontWoff2Tag;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
bool xt_font_file_is_compressed(const std::string &path) {
|
|
69
|
+
ByteCache &cache = byteCache();
|
|
70
|
+
const auto found = cache.compressed.find(path);
|
|
71
|
+
if (found != cache.compressed.end()) return found->second;
|
|
72
|
+
bool result = false;
|
|
73
|
+
FILE *file = std::fopen(path.c_str(), "rb");
|
|
74
|
+
if (file != nullptr) {
|
|
75
|
+
unsigned char header[4];
|
|
76
|
+
result = std::fread(header, 1, sizeof(header), file) == sizeof(header) &&
|
|
77
|
+
xt_font_is_webfont(header, sizeof(header));
|
|
78
|
+
std::fclose(file);
|
|
79
|
+
}
|
|
80
|
+
cache.compressed.emplace(path, result);
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
std::shared_ptr<const std::vector<unsigned char>> xt_font_file_bytes(const std::string &path) {
|
|
85
|
+
ByteCache &cache = byteCache();
|
|
86
|
+
const auto found = cache.files.find(path);
|
|
87
|
+
if (found != cache.files.end()) return found->second;
|
|
88
|
+
|
|
89
|
+
std::shared_ptr<const std::vector<unsigned char>> result;
|
|
90
|
+
const std::shared_ptr<std::vector<unsigned char>> raw = readFile(path);
|
|
91
|
+
if (raw != nullptr) {
|
|
92
|
+
if (xt_font_is_webfont(raw->data(), raw->size())) {
|
|
93
|
+
if (g_decoder == nullptr) {
|
|
94
|
+
xt_warn_webfont_unlinked(path);
|
|
95
|
+
} else {
|
|
96
|
+
auto decoded = std::make_shared<std::vector<unsigned char>>();
|
|
97
|
+
if (g_decoder->decode(*raw, decoded.get()) && !decoded->empty()) {
|
|
98
|
+
result = decoded;
|
|
99
|
+
} else {
|
|
100
|
+
xt_warn_webfont_invalid(path);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
result = raw;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
cache.files.emplace(path, result);
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
void xt_font_bytes_shutdown() {
|
|
112
|
+
ByteCache &cache = byteCache();
|
|
113
|
+
cache.files.clear();
|
|
114
|
+
cache.compressed.clear();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
} // namespace xtgui
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — font file bytes shared by the metadata reader and both
|
|
3
|
+
* text backends.
|
|
4
|
+
*
|
|
5
|
+
* A `.ttf`/`.ttc`/`.otf`/`.otc` file is already sfnt, so its bytes are handed
|
|
6
|
+
* out as read. A `.woff`/`.woff2` file stores the same tables deflated (WOFF) or
|
|
7
|
+
* Brotli-compressed behind a transformed table set (WOFF2), so it is decoded to
|
|
8
|
+
* a plain sfnt by the optional `webfonts` link feature before anyone parses it.
|
|
9
|
+
* The decoder is a function table installed by `webfont_bridge.c`:
|
|
10
|
+
* `text_webfont.cpp` is the only place that references miniz/Brotli/woff2, and
|
|
11
|
+
* without the bridge those members are never extracted from `gui.a`.
|
|
12
|
+
*
|
|
13
|
+
* Every path is decoded at most once per process: the font database parses the
|
|
14
|
+
* same bytes for its CSS metadata that the selected backend loads for outlines.
|
|
15
|
+
*/
|
|
16
|
+
#ifndef XT_GUI_TEXT_FONT_BYTES_H
|
|
17
|
+
#define XT_GUI_TEXT_FONT_BYTES_H
|
|
18
|
+
|
|
19
|
+
#include <cstdint>
|
|
20
|
+
#include <memory>
|
|
21
|
+
#include <string>
|
|
22
|
+
#include <vector>
|
|
23
|
+
|
|
24
|
+
namespace xtgui {
|
|
25
|
+
|
|
26
|
+
/** The two compressed container signatures (`'wOFF'`, `'wOF2'`). */
|
|
27
|
+
constexpr uint32_t kWebfontWoffTag = 0x774f4646u;
|
|
28
|
+
constexpr uint32_t kWebfontWoff2Tag = 0x774f4632u;
|
|
29
|
+
|
|
30
|
+
/** The WOFF/WOFF2 decompressor installed by the `webfonts` link feature. */
|
|
31
|
+
struct WebfontDecoder {
|
|
32
|
+
/** Decode a compressed font file into a plain sfnt file in `out`. */
|
|
33
|
+
bool (*decode)(const std::vector<unsigned char> &source,
|
|
34
|
+
std::vector<unsigned char> *out);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** Install the decoder. Called by `webfont_bridge.c`'s constructor; a program
|
|
38
|
+
* built without the feature leaves it null and compressed sources are skipped
|
|
39
|
+
* with a diagnostic. */
|
|
40
|
+
void xt_font_webfont_install(const WebfontDecoder *decoder);
|
|
41
|
+
|
|
42
|
+
/** True when a WOFF/WOFF2 decoder is installed (the `webfonts` feature is
|
|
43
|
+
* linked). Font-directory scans consult it so an undecodable file is not even
|
|
44
|
+
* indexed, and the compressed-source diagnostic stays with the author rule. */
|
|
45
|
+
bool xt_font_webfont_available();
|
|
46
|
+
|
|
47
|
+
/** True when `path` is a WOFF/WOFF2 file. A backend that can stream an sfnt file
|
|
48
|
+
* (FreeType and HarfBuzz both can) uses this to keep a large system font out of
|
|
49
|
+
* memory, while a compressed source always has to be decoded into a buffer. */
|
|
50
|
+
bool xt_font_file_is_compressed(const std::string &path);
|
|
51
|
+
|
|
52
|
+
/** The uncompressed sfnt bytes for `path`, decoded once per process. Returns
|
|
53
|
+
* null when the file cannot be read or a compressed container cannot be
|
|
54
|
+
* decoded; the failure is cached too, so a missing source is reported once. */
|
|
55
|
+
std::shared_ptr<const std::vector<unsigned char>> xt_font_file_bytes(const std::string &path);
|
|
56
|
+
|
|
57
|
+
/** True when `data` starts with a WOFF/WOFF2 signature. */
|
|
58
|
+
bool xt_font_is_webfont(const unsigned char *data, size_t size);
|
|
59
|
+
|
|
60
|
+
/** Drop the cached bytes (after the text backends released their faces). */
|
|
61
|
+
void xt_font_bytes_shutdown();
|
|
62
|
+
|
|
63
|
+
} // namespace xtgui
|
|
64
|
+
|
|
65
|
+
#endif /* XT_GUI_TEXT_FONT_BYTES_H */
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
#include "text_fonts_internal.h"
|
|
11
11
|
|
|
12
|
+
#include "text_font_bytes.h"
|
|
12
13
|
#include "text_font_paths.h"
|
|
13
14
|
#include "text_font_sfnt.h"
|
|
14
15
|
|
|
@@ -130,6 +131,10 @@ bool hasFontExtension(const std::string &path) {
|
|
|
130
131
|
const size_t dot = path.find_last_of('.');
|
|
131
132
|
if (dot == std::string::npos) return false;
|
|
132
133
|
const std::string ext = lowerAscii(path.substr(dot));
|
|
134
|
+
/* `.woff`/`.woff2` are only indexed when the `webfonts` feature can decode
|
|
135
|
+
* them: without it a scan would report every compressed file it met, while an
|
|
136
|
+
* author rule over an undecodable source still gets its own diagnostic. */
|
|
137
|
+
if (ext == ".woff" || ext == ".woff2") return xt_font_webfont_available();
|
|
133
138
|
return ext == ".ttf" || ext == ".ttc" || ext == ".otf" || ext == ".otc";
|
|
134
139
|
}
|
|
135
140
|
|
|
@@ -7,9 +7,12 @@
|
|
|
7
7
|
#include <algorithm>
|
|
8
8
|
#include <cstdint>
|
|
9
9
|
#include <cstdio>
|
|
10
|
+
#include <memory>
|
|
10
11
|
#include <string>
|
|
11
12
|
#include <vector>
|
|
12
13
|
|
|
14
|
+
#include "text_font_bytes.h"
|
|
15
|
+
|
|
13
16
|
namespace xtgui {
|
|
14
17
|
namespace {
|
|
15
18
|
|
|
@@ -25,10 +28,35 @@ constexpr uint32_t kTagOs2 = tagOf('O', 'S', '/', '2');
|
|
|
25
28
|
constexpr uint32_t kTagHead = tagOf('h', 'e', 'a', 'd');
|
|
26
29
|
constexpr uint32_t kTagPost = tagOf('p', 'o', 's', 't');
|
|
27
30
|
|
|
28
|
-
|
|
31
|
+
/** Random access to an sfnt container. A plain font file is read on demand (a
|
|
32
|
+
* large CJK face is never held in memory just to match its family name), while
|
|
33
|
+
* a decompressed WOFF/WOFF2 container is already a byte vector. */
|
|
34
|
+
class Reader {
|
|
35
|
+
public:
|
|
36
|
+
virtual ~Reader() = default;
|
|
37
|
+
virtual uint64_t size() const = 0;
|
|
38
|
+
virtual bool read(uint64_t offset, void *destination, size_t length) const = 0;
|
|
39
|
+
|
|
40
|
+
bool u16(uint64_t offset, uint16_t *out) const {
|
|
41
|
+
unsigned char bytes[2];
|
|
42
|
+
if (!read(offset, bytes, sizeof(bytes))) return false;
|
|
43
|
+
*out = (uint16_t)((uint16_t)bytes[0] << 8 | bytes[1]);
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
bool u32(uint64_t offset, uint32_t *out) const {
|
|
48
|
+
unsigned char bytes[4];
|
|
49
|
+
if (!read(offset, bytes, sizeof(bytes))) return false;
|
|
50
|
+
*out = ((uint32_t)bytes[0] << 24) | ((uint32_t)bytes[1] << 16) | ((uint32_t)bytes[2] << 8) |
|
|
51
|
+
(uint32_t)bytes[3];
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
class FileReader : public Reader {
|
|
29
57
|
public:
|
|
30
58
|
FileReader() = default;
|
|
31
|
-
~FileReader() {
|
|
59
|
+
~FileReader() override {
|
|
32
60
|
if (file_ != nullptr) std::fclose(file_);
|
|
33
61
|
}
|
|
34
62
|
FileReader(const FileReader &) = delete;
|
|
@@ -44,31 +72,33 @@ class FileReader {
|
|
|
44
72
|
return true;
|
|
45
73
|
}
|
|
46
74
|
|
|
47
|
-
uint64_t size() const { return size_; }
|
|
75
|
+
uint64_t size() const override { return size_; }
|
|
48
76
|
|
|
49
|
-
bool read(uint64_t offset, void *destination, size_t length) const {
|
|
77
|
+
bool read(uint64_t offset, void *destination, size_t length) const override {
|
|
50
78
|
if (offset > size_ || length > size_ - offset) return false;
|
|
51
79
|
if (std::fseek(file_, (long)offset, SEEK_SET) != 0) return false;
|
|
52
80
|
return std::fread(destination, 1, length, file_) == length;
|
|
53
81
|
}
|
|
54
82
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return true;
|
|
60
|
-
}
|
|
83
|
+
private:
|
|
84
|
+
FILE *file_ = nullptr;
|
|
85
|
+
uint64_t size_ = 0;
|
|
86
|
+
};
|
|
61
87
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
88
|
+
class MemoryReader : public Reader {
|
|
89
|
+
public:
|
|
90
|
+
MemoryReader(const std::vector<unsigned char> &bytes) : data_(bytes.data()), size_(bytes.size()) {}
|
|
91
|
+
|
|
92
|
+
uint64_t size() const override { return size_; }
|
|
93
|
+
|
|
94
|
+
bool read(uint64_t offset, void *destination, size_t length) const override {
|
|
95
|
+
if (offset > size_ || length > size_ - offset) return false;
|
|
96
|
+
std::copy(data_ + offset, data_ + offset + length, static_cast<unsigned char *>(destination));
|
|
67
97
|
return true;
|
|
68
98
|
}
|
|
69
99
|
|
|
70
100
|
private:
|
|
71
|
-
|
|
101
|
+
const unsigned char *data_ = nullptr;
|
|
72
102
|
uint64_t size_ = 0;
|
|
73
103
|
};
|
|
74
104
|
|
|
@@ -78,7 +108,7 @@ struct TableRecord {
|
|
|
78
108
|
uint32_t length = 0;
|
|
79
109
|
};
|
|
80
110
|
|
|
81
|
-
bool readTableRecords(const
|
|
111
|
+
bool readTableRecords(const Reader &reader, uint64_t face_offset, uint32_t *sfnt_tag,
|
|
82
112
|
std::vector<TableRecord> *tables) {
|
|
83
113
|
if (!reader.u32(face_offset, sfnt_tag)) return false;
|
|
84
114
|
uint16_t count = 0;
|
|
@@ -216,7 +246,7 @@ int namePriority(uint16_t platform, uint16_t encoding, uint16_t language) {
|
|
|
216
246
|
return 5;
|
|
217
247
|
}
|
|
218
248
|
|
|
219
|
-
bool readNames(const
|
|
249
|
+
bool readNames(const Reader &reader, const TableRecord &table, NameSet *names) {
|
|
220
250
|
uint16_t format = 0;
|
|
221
251
|
uint16_t count = 0;
|
|
222
252
|
uint16_t string_offset = 0;
|
|
@@ -303,7 +333,7 @@ std::vector<std::string> uniqueNames(const std::vector<std::string> &names) {
|
|
|
303
333
|
return out;
|
|
304
334
|
}
|
|
305
335
|
|
|
306
|
-
bool parseFace(const
|
|
336
|
+
bool parseFace(const Reader &reader, uint64_t face_offset, int face_index, SfntFace *face) {
|
|
307
337
|
uint32_t sfnt_tag = 0;
|
|
308
338
|
std::vector<TableRecord> tables;
|
|
309
339
|
if (!readTableRecords(reader, face_offset, &sfnt_tag, &tables)) return false;
|
|
@@ -364,11 +394,8 @@ bool parseFace(const FileReader &reader, uint64_t face_offset, int face_index, S
|
|
|
364
394
|
return true;
|
|
365
395
|
}
|
|
366
396
|
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
bool xt_sfnt_read_faces(const std::string &path, std::vector<SfntFace> &out) {
|
|
370
|
-
FileReader reader;
|
|
371
|
-
if (!reader.open(path)) return false;
|
|
397
|
+
/** Append every face of one sfnt container (single face or collection). */
|
|
398
|
+
bool readFaces(const Reader &reader, std::vector<SfntFace> &out) {
|
|
372
399
|
uint32_t first_tag = 0;
|
|
373
400
|
if (!reader.u32(0, &first_tag)) return false;
|
|
374
401
|
if (first_tag != kTagTtcf) {
|
|
@@ -393,4 +420,24 @@ bool xt_sfnt_read_faces(const std::string &path, std::vector<SfntFace> &out) {
|
|
|
393
420
|
return any;
|
|
394
421
|
}
|
|
395
422
|
|
|
423
|
+
} // namespace
|
|
424
|
+
|
|
425
|
+
bool xt_sfnt_read_faces(const std::string &path, std::vector<SfntFace> &out) {
|
|
426
|
+
uint32_t first_tag = 0;
|
|
427
|
+
{
|
|
428
|
+
FileReader probe;
|
|
429
|
+
if (!probe.open(path) || !probe.u32(0, &first_tag)) return false;
|
|
430
|
+
/* A plain font file is parsed straight from disk, without loading it. */
|
|
431
|
+
if (first_tag != kWebfontWoffTag && first_tag != kWebfontWoff2Tag) {
|
|
432
|
+
return readFaces(probe, out);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
/* A WOFF/WOFF2 file has no sfnt table directory of its own: decode it once
|
|
436
|
+
* (the same bytes the text backend will draw from) and parse that. */
|
|
437
|
+
const std::shared_ptr<const std::vector<unsigned char>> bytes = xt_font_file_bytes(path);
|
|
438
|
+
if (bytes == nullptr || bytes->size() < 12) return false;
|
|
439
|
+
MemoryReader reader(*bytes);
|
|
440
|
+
return readFaces(reader, out);
|
|
441
|
+
}
|
|
442
|
+
|
|
396
443
|
} // namespace xtgui
|
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
* `text_font_sfnt.cpp` reads only the tables needed to match a CSS family and
|
|
5
5
|
* weight: `name` (family/style names), `OS/2` (usWeightClass/fsSelection),
|
|
6
6
|
* `head` (macStyle) and `post` (isFixedPitch). It does not decode outlines or
|
|
7
|
-
* shape text, and it accepts
|
|
7
|
+
* shape text, and it accepts single faces, collections (`.ttc`/`.otc`) and —
|
|
8
|
+
* through the optional `webfonts` link feature — `.woff`/`.woff2` sources,
|
|
9
|
+
* which are decompressed first (`text_font_bytes.h`).
|
|
8
10
|
*
|
|
9
11
|
* The reader is deliberately dependency-free: the default text backend links
|
|
10
12
|
* stb_truetype and the optional `shaping` backend links FreeType/HarfBuzz, but
|
|
@@ -36,7 +38,8 @@ struct SfntFace {
|
|
|
36
38
|
|
|
37
39
|
/** Read every face in `path`, appending them to `out` in collection order.
|
|
38
40
|
* Returns false when the file cannot be opened or is not an sfnt container;
|
|
39
|
-
* `out` may still contain the faces read before an error.
|
|
41
|
+
* `out` may still contain the faces read before an error. A WOFF/WOFF2 file is
|
|
42
|
+
* decompressed first, so its metadata is read exactly like a plain sfnt's. */
|
|
40
43
|
bool xt_sfnt_read_faces(const std::string &path, std::vector<SfntFace> &out);
|
|
41
44
|
|
|
42
45
|
} // namespace xtgui
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
24
|
#include "text_api.h"
|
|
25
|
+
#include "text_font_bytes.h"
|
|
25
26
|
#include "text_fonts.h"
|
|
26
27
|
|
|
27
28
|
#include <ft2build.h>
|
|
@@ -46,6 +47,9 @@ namespace xtgui {
|
|
|
46
47
|
struct HbFont {
|
|
47
48
|
FT_Face ft_face = nullptr;
|
|
48
49
|
hb_font_t *hb_font = nullptr;
|
|
50
|
+
/** FreeType and HarfBuzz keep pointers into these bytes (a WOFF/WOFF2 source
|
|
51
|
+
* arrives here already decompressed), so the face owns the buffer. */
|
|
52
|
+
std::shared_ptr<const std::vector<unsigned char>> data;
|
|
49
53
|
std::string path;
|
|
50
54
|
int face_index = 0;
|
|
51
55
|
float pixel_size = 16.0f;
|
|
@@ -128,14 +132,24 @@ void ensureInitialized() {
|
|
|
128
132
|
e.ft_ok = true;
|
|
129
133
|
}
|
|
130
134
|
|
|
131
|
-
hb_face_t *hbFaceForPath(const std::string &path, int face_index
|
|
135
|
+
hb_face_t *hbFaceForPath(const std::string &path, int face_index,
|
|
136
|
+
const std::shared_ptr<const std::vector<unsigned char>> &bytes) {
|
|
132
137
|
Engine &e = engine();
|
|
133
138
|
std::string key = path;
|
|
134
139
|
key += '#';
|
|
135
140
|
key += std::to_string(face_index);
|
|
136
141
|
auto it = e.hb_faces.find(key);
|
|
137
142
|
if (it != e.hb_faces.end()) return it->second;
|
|
138
|
-
|
|
143
|
+
/* A decoded WOFF/WOFF2 source is a process-wide buffer that outlives every
|
|
144
|
+
* `hb_face_t` (it is only dropped after this backend's shutdown), so a
|
|
145
|
+
* read-only, reference-counted blob is enough — nothing is copied. A plain
|
|
146
|
+
* sfnt file keeps the old file blob, so a large unused fallback face is never
|
|
147
|
+
* pulled into memory. */
|
|
148
|
+
hb_blob_t *blob = bytes != nullptr
|
|
149
|
+
? hb_blob_create(reinterpret_cast<const char *>(bytes->data()),
|
|
150
|
+
(unsigned)bytes->size(), HB_MEMORY_MODE_READONLY, nullptr,
|
|
151
|
+
nullptr)
|
|
152
|
+
: hb_blob_create_from_file(path.c_str());
|
|
139
153
|
hb_face_t *face = nullptr;
|
|
140
154
|
if (blob != nullptr && hb_blob_get_length(blob) > 0) {
|
|
141
155
|
face = hb_face_create(blob, (unsigned)face_index);
|
|
@@ -149,12 +163,25 @@ std::unique_ptr<HbFont> createFont(const std::string &path, int face_index, floa
|
|
|
149
163
|
Engine &e = engine();
|
|
150
164
|
if (!e.ft_ok || path.empty()) return nullptr;
|
|
151
165
|
|
|
166
|
+
/* A compressed source has to be decoded into memory, which both FreeType and
|
|
167
|
+
* HarfBuzz then read from; a plain sfnt file is opened by path so its bytes
|
|
168
|
+
* are demand-paged rather than copied. */
|
|
169
|
+
std::shared_ptr<const std::vector<unsigned char>> bytes;
|
|
152
170
|
FT_Face face = nullptr;
|
|
153
|
-
if (
|
|
171
|
+
if (xt_font_file_is_compressed(path)) {
|
|
172
|
+
bytes = xt_font_file_bytes(path);
|
|
173
|
+
if (bytes == nullptr || bytes->empty()) return nullptr;
|
|
174
|
+
if (FT_New_Memory_Face(e.ft, bytes->data(), (FT_Long)bytes->size(), face_index, &face) != 0) {
|
|
175
|
+
return nullptr;
|
|
176
|
+
}
|
|
177
|
+
} else if (FT_New_Face(e.ft, path.c_str(), face_index, &face) != 0) {
|
|
178
|
+
return nullptr;
|
|
179
|
+
}
|
|
154
180
|
FT_Set_Pixel_Sizes(face, 0, (FT_UInt)(pixel_size + 0.5f));
|
|
155
181
|
|
|
156
182
|
auto font = std::make_unique<HbFont>();
|
|
157
183
|
font->ft_face = face;
|
|
184
|
+
font->data = bytes;
|
|
158
185
|
font->path = path;
|
|
159
186
|
font->face_index = face_index;
|
|
160
187
|
font->pixel_size = pixel_size;
|
|
@@ -164,7 +191,7 @@ std::unique_ptr<HbFont> createFont(const std::string &path, int face_index, floa
|
|
|
164
191
|
font->descent = -metrics.descender / 64.0f;
|
|
165
192
|
font->line_height = metrics.height / 64.0f;
|
|
166
193
|
|
|
167
|
-
hb_face_t *hb_face = hbFaceForPath(path, face_index);
|
|
194
|
+
hb_face_t *hb_face = hbFaceForPath(path, face_index, bytes);
|
|
168
195
|
if (hb_face != nullptr) {
|
|
169
196
|
font->hb_font = hb_font_create(hb_face);
|
|
170
197
|
hb_ot_font_set_funcs(font->hb_font);
|
|
@@ -373,7 +400,9 @@ bool xt_text_hb_ready() {
|
|
|
373
400
|
|
|
374
401
|
void xt_text_hb_shutdown() {
|
|
375
402
|
Engine &e = engine();
|
|
376
|
-
/* Cached shaped runs point at faces, so drop them before the faces go.
|
|
403
|
+
/* Cached shaped runs point at faces, so drop them before the faces go. The
|
|
404
|
+
* HarfBuzz faces and FreeType faces reference the cached font bytes, which
|
|
405
|
+
* `xt_font_bytes_shutdown` releases last. */
|
|
377
406
|
e.primary.clear();
|
|
378
407
|
e.fallbacks.clear();
|
|
379
408
|
e.cache.clear();
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
#include "text_api.h"
|
|
20
|
+
#include "text_font_bytes.h"
|
|
20
21
|
#include "text_fonts.h"
|
|
21
22
|
|
|
22
23
|
#define STB_TRUETYPE_IMPLEMENTATION
|
|
@@ -51,7 +52,6 @@ struct Font {
|
|
|
51
52
|
namespace {
|
|
52
53
|
|
|
53
54
|
struct Engine {
|
|
54
|
-
std::unordered_map<std::string, std::shared_ptr<const std::vector<unsigned char>>> data;
|
|
55
55
|
std::unordered_map<std::string, std::unique_ptr<Font>> cache; // key -> face (may be null)
|
|
56
56
|
std::unordered_map<std::string, std::vector<Font *>> fallbacks; // face+size -> faces
|
|
57
57
|
std::unordered_map<std::string, Font *> primary; // spec -> face (may be null)
|
|
@@ -100,35 +100,11 @@ uint32_t decodeUtf8(const std::string &text, size_t &index) {
|
|
|
100
100
|
return code_point;
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
-
/* Read a font file once and share the bytes across every pixel size. */
|
|
104
|
-
std::shared_ptr<const std::vector<unsigned char>> fontData(const std::string &path) {
|
|
105
|
-
Engine &e = engine();
|
|
106
|
-
auto found = e.data.find(path);
|
|
107
|
-
if (found != e.data.end()) return found->second;
|
|
108
|
-
|
|
109
|
-
FILE *file = fopen(path.c_str(), "rb");
|
|
110
|
-
if (file == nullptr) return nullptr;
|
|
111
|
-
if (fseek(file, 0, SEEK_END) != 0) {
|
|
112
|
-
fclose(file);
|
|
113
|
-
return nullptr;
|
|
114
|
-
}
|
|
115
|
-
long length = ftell(file);
|
|
116
|
-
if (length <= 0 || fseek(file, 0, SEEK_SET) != 0) {
|
|
117
|
-
fclose(file);
|
|
118
|
-
return nullptr;
|
|
119
|
-
}
|
|
120
|
-
auto bytes = std::make_shared<std::vector<unsigned char>>((size_t)length);
|
|
121
|
-
size_t read = fread(bytes->data(), 1, bytes->size(), file);
|
|
122
|
-
fclose(file);
|
|
123
|
-
if (read != bytes->size()) return nullptr;
|
|
124
|
-
std::shared_ptr<const std::vector<unsigned char>> shared = bytes;
|
|
125
|
-
e.data.emplace(path, shared);
|
|
126
|
-
return shared;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
103
|
std::unique_ptr<Font> createFont(const std::string &path, int face_index, float pixel_size) {
|
|
130
104
|
if (path.empty()) return nullptr;
|
|
131
|
-
|
|
105
|
+
/* The shared cache decompresses a WOFF/WOFF2 source and returns the sfnt
|
|
106
|
+
* bytes; stbtt keeps pointers into them, so `Font` holds the buffer. */
|
|
107
|
+
std::shared_ptr<const std::vector<unsigned char>> data = xt_font_file_bytes(path);
|
|
132
108
|
if (data == nullptr || data->empty()) return nullptr;
|
|
133
109
|
|
|
134
110
|
const int offset = stbtt_GetFontOffsetForIndex(data->data(), face_index);
|
|
@@ -342,11 +318,12 @@ bool xt_text_stb_ready() {
|
|
|
342
318
|
|
|
343
319
|
void xt_text_stb_shutdown() {
|
|
344
320
|
Engine &e = engine();
|
|
345
|
-
/* Cached shaped runs point at faces, so drop them before the faces go.
|
|
321
|
+
/* Cached shaped runs point at faces, so drop them before the faces go. The
|
|
322
|
+
* font bytes outlive this call; they are released by `xt_font_bytes_shutdown`
|
|
323
|
+
* after both backends dropped their faces. */
|
|
346
324
|
e.primary.clear();
|
|
347
325
|
e.fallbacks.clear();
|
|
348
326
|
e.cache.clear();
|
|
349
|
-
e.data.clear();
|
|
350
327
|
}
|
|
351
328
|
|
|
352
329
|
} // namespace xtgui
|