xbintsc 0.7.8 → 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.
Files changed (116) hide show
  1. package/dist/src/cli/help.d.ts +1 -1
  2. package/dist/src/cli/help.js +1 -1
  3. package/dist/src/codegen/generator/context.d.ts +4 -3
  4. package/dist/src/codegen/generator/context.js +6 -5
  5. package/dist/src/codegen/generator/context.js.map +1 -1
  6. package/dist/src/codegen/generator/expressions/assignment.js +3 -3
  7. package/dist/src/driver/config.d.ts +1 -1
  8. package/dist/src/driver/options.d.ts +1 -1
  9. package/dist/src/driver/version.d.ts +1 -1
  10. package/dist/src/driver/version.js +1 -1
  11. package/dist/src/extensions/gui/features.d.ts +14 -6
  12. package/dist/src/extensions/gui/features.js +34 -6
  13. package/dist/src/extensions/gui/features.js.map +1 -1
  14. package/dist/src/extensions/gui/index.js +3 -3
  15. package/dist/tests/codegen/llvm.test.js +1 -1
  16. package/dist/tests/e2e/gui-audio.test.js +24 -0
  17. package/dist/tests/e2e/gui-audio.test.js.map +1 -1
  18. package/dist/tests/e2e/gui-draw-perf.test.js +9 -0
  19. package/dist/tests/e2e/gui-draw-perf.test.js.map +1 -1
  20. package/dist/tests/e2e/gui-font-face.test.d.ts +3 -1
  21. package/dist/tests/e2e/gui-font-face.test.js +99 -7
  22. package/dist/tests/e2e/gui-font-face.test.js.map +1 -1
  23. package/dist/tests/e2e/gui-frame-pacing.test.d.ts +15 -0
  24. package/dist/tests/e2e/gui-frame-pacing.test.js +68 -0
  25. package/dist/tests/e2e/gui-frame-pacing.test.js.map +1 -0
  26. package/dist/tests/e2e/gui-initial-url.test.d.ts +10 -0
  27. package/dist/tests/e2e/gui-initial-url.test.js +49 -0
  28. package/dist/tests/e2e/gui-initial-url.test.js.map +1 -0
  29. package/dist/tests/e2e/gui-webgl-depth-range.test.d.ts +18 -0
  30. package/dist/tests/e2e/gui-webgl-depth-range.test.js +140 -0
  31. package/dist/tests/e2e/gui-webgl-depth-range.test.js.map +1 -0
  32. package/dist/tests/extensions/gui-features.test.js +27 -2
  33. package/dist/tests/extensions/gui-features.test.js.map +1 -1
  34. package/doc/ai/extensions.md +10 -4
  35. package/doc/ai/zh-CN/extensions.md +8 -3
  36. package/doc/gui/api.md +35 -2
  37. package/doc/gui/build.md +34 -15
  38. package/doc/gui/known-issues.md +1 -1
  39. package/doc/gui/rendering.md +45 -8
  40. package/doc/gui/runtime.md +13 -0
  41. package/doc/gui/webgl.md +11 -2
  42. package/doc/gui.md +9 -4
  43. package/doc/implemented/codegen.md +3 -2
  44. package/doc/zh-CN/gui/api.md +25 -2
  45. package/doc/zh-CN/gui/build.md +20 -7
  46. package/doc/zh-CN/gui/known-issues.md +1 -1
  47. package/doc/zh-CN/gui/rendering.md +33 -8
  48. package/doc/zh-CN/gui/runtime.md +9 -0
  49. package/doc/zh-CN/gui/webgl.md +3 -2
  50. package/doc/zh-CN/gui.md +10 -5
  51. package/doc/zh-CN/implemented/codegen.md +3 -2
  52. package/package.json +7 -7
  53. package/runtime/ext_gui/audio_graph_commands.cpp +127 -0
  54. package/runtime/ext_gui/audio_mixer.cpp +84 -163
  55. package/runtime/ext_gui/audio_mixer.h +59 -3
  56. package/runtime/ext_gui/audio_mixer_extra.cpp +8 -0
  57. package/runtime/ext_gui/audio_param_apply.cpp +160 -0
  58. package/runtime/ext_gui/browser.cpp +6 -19
  59. package/runtime/ext_gui/browser_location.cpp +78 -0
  60. package/runtime/ext_gui/css_sheet.cpp +4 -2
  61. package/runtime/ext_gui/document_animation.cpp +7 -0
  62. package/runtime/ext_gui/dom_api_computed_style.cpp +3 -2
  63. package/runtime/ext_gui/dom_api_webgl.cpp +31 -0
  64. package/runtime/ext_gui/dom_api_webgl_internal.h +4 -0
  65. package/runtime/ext_gui/dom_api_webgl_shader.cpp +19 -4
  66. package/runtime/ext_gui/dom_api_webgl_state.cpp +6 -3
  67. package/runtime/ext_gui/gui.cpp +100 -1
  68. package/runtime/ext_gui/gui_events.cpp +8 -2
  69. package/runtime/ext_gui/perf.cpp +18 -6
  70. package/runtime/ext_gui/perf.h +37 -0
  71. package/runtime/ext_gui/style.cpp +27 -10
  72. package/runtime/ext_gui/style.h +10 -1
  73. package/runtime/ext_gui/text.cpp +4 -0
  74. package/runtime/ext_gui/text_font_bytes.cpp +117 -0
  75. package/runtime/ext_gui/text_font_bytes.h +65 -0
  76. package/runtime/ext_gui/text_font_db.cpp +5 -0
  77. package/runtime/ext_gui/text_font_sfnt.cpp +71 -24
  78. package/runtime/ext_gui/text_font_sfnt.h +5 -2
  79. package/runtime/ext_gui/text_hb.cpp +34 -5
  80. package/runtime/ext_gui/text_stb.cpp +7 -30
  81. package/runtime/ext_gui/text_webfont.cpp +217 -0
  82. package/runtime/ext_gui/warnings.cpp +26 -0
  83. package/runtime/ext_gui/warnings.h +8 -0
  84. package/runtime/ext_gui/webfont_bridge.c +23 -0
  85. package/runtime/ext_gui/webgl.h +34 -39
  86. package/runtime/ext_gui/webgl_blit_color.cpp +5 -0
  87. package/runtime/ext_gui/webgl_buffer_objects.cpp +19 -5
  88. package/runtime/ext_gui/webgl_draw.cpp +72 -12
  89. package/runtime/ext_gui/webgl_draw_setup.cpp +8 -2
  90. package/runtime/ext_gui/webgl_draw_state.h +13 -2
  91. package/runtime/ext_gui/webgl_objects.h +4 -0
  92. package/runtime/ext_gui/webgl_program.h +64 -0
  93. package/runtime/ext_gui/webgl_state.cpp +7 -0
  94. package/runtime/ext_gui/webgl_uniform.cpp +78 -0
  95. package/runtime/ext_gui/window.cpp +6 -0
  96. package/runtime/rt_object.h +6 -0
  97. package/runtime/rt_profile.h +6 -0
  98. package/runtime/xt_containers/access.inc +2 -47
  99. package/runtime/xt_containers/ic_get.inc +70 -0
  100. package/runtime/xt_containers/method_cache.inc +24 -10
  101. package/runtime/xt_containers/objects.inc +12 -1
  102. package/runtime/xt_containers/shapes.inc +78 -0
  103. package/runtime/xt_containers.c +1 -0
  104. package/runtime/xt_profile.c +3 -0
  105. package/scripts/build-gui.ts +43 -49
  106. package/scripts/gui-engine-build.ts +58 -0
  107. package/scripts/gui-vendor-source.ts +53 -0
  108. package/scripts/gui-webfont-build.ts +138 -0
  109. package/src/cli/help.ts +1 -1
  110. package/src/codegen/generator/context.ts +6 -6
  111. package/src/codegen/generator/expressions/assignment.ts +3 -3
  112. package/src/driver/config.ts +1 -1
  113. package/src/driver/options.ts +1 -1
  114. package/src/driver/version.ts +1 -1
  115. package/src/extensions/gui/features.ts +34 -6
  116. package/src/extensions/gui/index.ts +3 -3
@@ -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
- class FileReader {
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
- bool u16(uint64_t offset, uint16_t *out) const {
56
- unsigned char bytes[2];
57
- if (!read(offset, bytes, sizeof(bytes))) return false;
58
- *out = (uint16_t)((uint16_t)bytes[0] << 8 | bytes[1]);
59
- return true;
60
- }
83
+ private:
84
+ FILE *file_ = nullptr;
85
+ uint64_t size_ = 0;
86
+ };
61
87
 
62
- bool u32(uint64_t offset, uint32_t *out) const {
63
- unsigned char bytes[4];
64
- if (!read(offset, bytes, sizeof(bytes))) return false;
65
- *out = ((uint32_t)bytes[0] << 24) | ((uint32_t)bytes[1] << 16) | ((uint32_t)bytes[2] << 8) |
66
- (uint32_t)bytes[3];
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
- FILE *file_ = nullptr;
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 FileReader &reader, uint64_t face_offset, uint32_t *sfnt_tag,
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 FileReader &reader, const TableRecord &table, NameSet *names) {
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 FileReader &reader, uint64_t face_offset, int face_index, SfntFace *face) {
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
- } // namespace
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 both single faces and collections (`.ttc`/`.otc`).
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
- hb_blob_t *blob = hb_blob_create_from_file(path.c_str());
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 (FT_New_Face(e.ft, path.c_str(), face_index, &face) != 0) return nullptr;
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
- std::shared_ptr<const std::vector<unsigned char>> data = fontData(path);
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
@@ -0,0 +1,217 @@
1
+ /*
2
+ * xbintsc GUI engine — WOFF and WOFF2 decompression for author `@font-face`
3
+ * sources.
4
+ *
5
+ * Only `webfont_bridge.c` references `xt_webfont_feature_register`, so this
6
+ * translation unit (and the miniz/Brotli/woff2 members it calls) is extracted
7
+ * from `gui.a` only when the `webfonts` link feature is active.
8
+ *
9
+ * WOFF (W3C WOFF 1.0) is the sfnt table directory with each table individually
10
+ * zlib-compressed, so this unit rebuilds a plain sfnt from the decompressed
11
+ * tables. WOFF2 (W3C WOFF 2.0) stores one Brotli stream of
12
+ * *transformed* tables (glyf/loca and hmtx are re-encoded); reconstructing that
13
+ * is the woff2 reference decoder's job, and its output is already a complete
14
+ * sfnt file.
15
+ */
16
+
17
+ #include "text_font_bytes.h"
18
+
19
+ #include <cstdint>
20
+ #include <cstring>
21
+ #include <string>
22
+ #include <vector>
23
+
24
+ #include "miniz_tinfl.h"
25
+ #include <woff2/decode.h>
26
+
27
+ namespace xtgui {
28
+ namespace {
29
+
30
+ constexpr uint32_t tagOf(char a, char b, char c, char d) {
31
+ return ((uint32_t)(unsigned char)a << 24) | ((uint32_t)(unsigned char)b << 16) |
32
+ ((uint32_t)(unsigned char)c << 8) | (uint32_t)(unsigned char)d;
33
+ }
34
+
35
+ constexpr uint32_t kTagWoff = tagOf('w', 'O', 'F', 'F');
36
+ constexpr uint32_t kTagWoff2 = tagOf('w', 'O', 'F', '2');
37
+
38
+ /** Decompression-bomb guard: a real font is far below this. WOFF declares the
39
+ * uncompressed size up front, and the rebuilt table set is checked against the
40
+ * same bound. */
41
+ constexpr size_t kMaxSfntSize = 64u * 1024u * 1024u;
42
+
43
+ uint16_t be16(const unsigned char *data) {
44
+ return (uint16_t)((uint16_t)data[0] << 8 | data[1]);
45
+ }
46
+
47
+ uint32_t be32(const unsigned char *data) {
48
+ return ((uint32_t)data[0] << 24) | ((uint32_t)data[1] << 16) | ((uint32_t)data[2] << 8) |
49
+ (uint32_t)data[3];
50
+ }
51
+
52
+ void put16(unsigned char *data, uint16_t value) {
53
+ data[0] = (unsigned char)(value >> 8);
54
+ data[1] = (unsigned char)value;
55
+ }
56
+
57
+ void put32(unsigned char *data, uint32_t value) {
58
+ data[0] = (unsigned char)(value >> 24);
59
+ data[1] = (unsigned char)(value >> 16);
60
+ data[2] = (unsigned char)(value >> 8);
61
+ data[3] = (unsigned char)value;
62
+ }
63
+
64
+ size_t paddedSize(size_t length) { return (length + 3u) & ~(size_t)3u; }
65
+
66
+ /** One WOFF table directory entry. */
67
+ struct WoffTable {
68
+ uint32_t tag = 0;
69
+ uint32_t offset = 0;
70
+ uint32_t compressed_length = 0;
71
+ uint32_t original_length = 0;
72
+ uint32_t checksum = 0;
73
+ };
74
+
75
+ /** Inflate one WOFF table. WOFF compresses each table with zlib (RFC 1950),
76
+ * not a raw DEFLATE stream, so the 2-byte header and Adler-32 trailer are part
77
+ * of the input. */
78
+ bool inflateTable(const unsigned char *source, size_t source_length, uint32_t original_length,
79
+ std::vector<unsigned char> *out) {
80
+ size_t inflated = 0;
81
+ void *heap = tinfl_decompress_mem_to_heap(source, source_length, &inflated,
82
+ TINFL_FLAG_PARSE_ZLIB_HEADER);
83
+ if (heap == nullptr) return false;
84
+ const bool ok = inflated == original_length;
85
+ if (ok) out->assign((const unsigned char *)heap, (const unsigned char *)heap + inflated);
86
+ MZ_FREE(heap);
87
+ return ok;
88
+ }
89
+
90
+ /** Rebuild a plain sfnt file from the WOFF table set. Every table keeps the
91
+ * checksum the WOFF directory recorded, and the data is written 4-byte aligned
92
+ * in directory order, which is what an sfnt reader expects. */
93
+ bool buildSfnt(uint32_t flavor, const std::vector<WoffTable> &tables,
94
+ const std::vector<std::vector<unsigned char>> &data,
95
+ std::vector<unsigned char> *out) {
96
+ const size_t count = tables.size();
97
+ if (count == 0 || count > 0x0fffu) return false;
98
+ size_t total = 12 + count * 16;
99
+ for (size_t i = 0; i < count; i++) {
100
+ if (data[i].size() != tables[i].original_length) return false;
101
+ total += paddedSize(data[i].size());
102
+ if (total > kMaxSfntSize) return false;
103
+ }
104
+
105
+ out->assign(total, 0);
106
+ unsigned char *sfnt = out->data();
107
+ put32(sfnt, flavor);
108
+ put16(sfnt + 4, (uint16_t)count);
109
+ int entry_selector = 0;
110
+ while (((size_t)1 << (entry_selector + 1)) <= count) entry_selector++;
111
+ const uint16_t search_range = (uint16_t)(((size_t)1 << entry_selector) * 16);
112
+ put16(sfnt + 6, search_range);
113
+ put16(sfnt + 8, (uint16_t)entry_selector);
114
+ put16(sfnt + 10, (uint16_t)(count * 16 - search_range));
115
+
116
+ size_t offset = 12 + count * 16;
117
+ for (size_t i = 0; i < count; i++) {
118
+ unsigned char *record = sfnt + 12 + i * 16;
119
+ put32(record, tables[i].tag);
120
+ put32(record + 4, tables[i].checksum);
121
+ put32(record + 8, (uint32_t)offset);
122
+ put32(record + 12, tables[i].original_length);
123
+ if (!data[i].empty()) std::memcpy(sfnt + offset, data[i].data(), data[i].size());
124
+ offset += paddedSize(data[i].size());
125
+ }
126
+ return true;
127
+ }
128
+
129
+ bool decodeWoff(const std::vector<unsigned char> &source, std::vector<unsigned char> *out) {
130
+ /* Fixed 44-byte header, then one 20-byte record per table. */
131
+ constexpr size_t kHeaderSize = 44;
132
+ constexpr size_t kRecordSize = 20;
133
+ const unsigned char *bytes = source.data();
134
+ if (source.size() < kHeaderSize) return false;
135
+
136
+ const uint32_t flavor = be32(bytes + 4);
137
+ const uint32_t declared_length = be32(bytes + 8);
138
+ const uint16_t count = be16(bytes + 12);
139
+ const uint32_t total_sfnt_size = be32(bytes + 16);
140
+ if (count == 0 || count > 0x0fffu) return false;
141
+ if (total_sfnt_size == 0 || total_sfnt_size > kMaxSfntSize) return false;
142
+ if (declared_length != 0 && declared_length > source.size()) return false;
143
+ if (kHeaderSize + (size_t)count * kRecordSize > source.size()) return false;
144
+
145
+ std::vector<WoffTable> tables(count);
146
+ std::vector<std::vector<unsigned char>> data(count);
147
+ for (size_t i = 0; i < count; i++) {
148
+ const unsigned char *record = bytes + kHeaderSize + i * kRecordSize;
149
+ WoffTable &table = tables[i];
150
+ table.tag = be32(record);
151
+ table.offset = be32(record + 4);
152
+ table.compressed_length = be32(record + 8);
153
+ table.original_length = be32(record + 12);
154
+ table.checksum = be32(record + 16);
155
+ if (table.original_length == 0 || table.compressed_length == 0) return false;
156
+ if (table.original_length > kMaxSfntSize) return false;
157
+ /* A table is stored uncompressed when its compressed size is not smaller. */
158
+ if (table.compressed_length > table.original_length) return false;
159
+ if (table.offset > source.size() ||
160
+ table.compressed_length > source.size() - table.offset) {
161
+ return false;
162
+ }
163
+ if (table.compressed_length == table.original_length) {
164
+ data[i].assign(bytes + table.offset, bytes + table.offset + table.compressed_length);
165
+ continue;
166
+ }
167
+ if (!inflateTable(bytes + table.offset, table.compressed_length, table.original_length,
168
+ &data[i])) {
169
+ return false;
170
+ }
171
+ }
172
+ return buildSfnt(flavor, tables, data, out);
173
+ }
174
+
175
+ bool decodeWoff2(const std::vector<unsigned char> &source, std::vector<unsigned char> *out) {
176
+ if (source.size() < 48) return false;
177
+ std::string decoded;
178
+ woff2::WOFF2StringOut writer(&decoded);
179
+ if (!woff2::ConvertWOFF2ToTTF(source.data(), source.size(), &writer)) return false;
180
+ out->assign(decoded.begin(), decoded.end());
181
+ return true;
182
+ }
183
+
184
+ /** The sfnt version tags a rebuilt container may start with. WOFF/WOFF2 cannot
185
+ * hold a collection, but accepting `ttcf` keeps a decoder bug from turning into
186
+ * a "corrupt font" report when the source really was one. */
187
+ bool looksLikeSfnt(const std::vector<unsigned char> &bytes) {
188
+ if (bytes.size() < 4) return false;
189
+ const uint32_t tag = be32(bytes.data());
190
+ return tag == 0x00010000u || tag == tagOf('t', 'r', 'u', 'e') ||
191
+ tag == tagOf('t', 'y', 'p', '1') || tag == tagOf('O', 'T', 'T', 'O') ||
192
+ tag == tagOf('t', 't', 'c', 'f');
193
+ }
194
+
195
+ bool decode(const std::vector<unsigned char> &source, std::vector<unsigned char> *out) {
196
+ if (source.size() < 4) return false;
197
+ const uint32_t tag = be32(source.data());
198
+ if (tag == kTagWoff) {
199
+ if (!decodeWoff(source, out)) return false;
200
+ } else if (tag == kTagWoff2) {
201
+ if (!decodeWoff2(source, out)) return false;
202
+ } else {
203
+ return false;
204
+ }
205
+ return looksLikeSfnt(*out);
206
+ }
207
+
208
+ } // namespace
209
+ } // namespace xtgui
210
+
211
+ /** Entry point referenced by `webfont_bridge.c`; it installs the decoder and,
212
+ * through that reference, makes the linker extract this object (and the
213
+ * miniz/Brotli/woff2 members) from `gui.a`. */
214
+ extern "C" void xt_webfont_feature_register(void) {
215
+ static const xtgui::WebfontDecoder kDecoder = {xtgui::decode};
216
+ xtgui::xt_font_webfont_install(&kDecoder);
217
+ }
@@ -178,6 +178,22 @@ void warnSelectorOnce(const std::string &selector) {
178
178
  truncated(selector, 80).c_str());
179
179
  }
180
180
 
181
+ void warnWebfontUnlinkedOnce(const std::string &path) {
182
+ static SeenNames seen;
183
+ if (!firstUse(&seen, path)) return;
184
+ std::fprintf(stderr,
185
+ "xt_gui: warning: '%s' is a WOFF/WOFF2 font and this program was built without "
186
+ "the 'webfonts' link feature; rebuild with --feature webfonts\n",
187
+ truncated(path, 120).c_str());
188
+ }
189
+
190
+ void warnWebfontInvalidOnce(const std::string &path) {
191
+ static SeenNames seen;
192
+ if (!firstUse(&seen, path)) return;
193
+ std::fprintf(stderr, "xt_gui: warning: cannot decode the WOFF/WOFF2 font '%s'; it is ignored\n",
194
+ truncated(path, 120).c_str());
195
+ }
196
+
181
197
  void warnPseudoClassOnce(const std::string &name) {
182
198
  static SeenNames seen;
183
199
  if (!firstUse(&seen, name)) return;
@@ -270,6 +286,16 @@ void xt_warn_unsupported_selector(const std::string &selector) {
270
286
  warnSelectorOnce(text);
271
287
  }
272
288
 
289
+ void xt_warn_webfont_unlinked(const std::string &path) {
290
+ if (warningsSuppressed() || path.empty()) return;
291
+ warnWebfontUnlinkedOnce(path);
292
+ }
293
+
294
+ void xt_warn_webfont_invalid(const std::string &path) {
295
+ if (warningsSuppressed() || path.empty()) return;
296
+ warnWebfontInvalidOnce(path);
297
+ }
298
+
273
299
  void xt_warn_unsupported_pseudo_class(const std::string &name) {
274
300
  if (warningsSuppressed() || name.empty()) return;
275
301
  warnPseudoClassOnce(hasUpperAscii(name) ? lowerAscii(name) : name);
@@ -36,6 +36,14 @@ void xt_warn_unsupported_at_rule(const std::string &name);
36
36
  /** Report an ignored `@font-face` descriptor once per descriptor name. */
37
37
  void xt_warn_unsupported_descriptor(const std::string &name);
38
38
 
39
+ /** Report a `.woff`/`.woff2` source that cannot be decoded because the program
40
+ * was built without the `webfonts` link feature, once per path. */
41
+ void xt_warn_webfont_unlinked(const std::string &path);
42
+
43
+ /** Report a `.woff`/`.woff2` source whose container cannot be decoded, once per
44
+ * path. The source is skipped like an unreadable file. */
45
+ void xt_warn_webfont_invalid(const std::string &path);
46
+
39
47
  /** Report a selector the parser had to drop. */
40
48
  void xt_warn_unsupported_selector(const std::string &selector);
41
49
 
@@ -0,0 +1,23 @@
1
+ /*
2
+ * xbintsc GUI engine — `webfonts` link feature registration bridge.
3
+ *
4
+ * Compiled and linked only when the compiler detects that the program may use a
5
+ * WOFF/WOFF2 `@font-face` source. Its constructor installs the decompressor and
6
+ * references it, which pulls `text_webfont.cpp` (and the miniz/Brotli/woff2
7
+ * members it calls) out of `gui.a`; without it a compressed font source is
8
+ * skipped and the diagnostic names the missing feature.
9
+ */
10
+
11
+ extern void xt_webfont_feature_register(void);
12
+
13
+ #if defined(__GNUC__) || defined(__clang__)
14
+ __attribute__((constructor)) static void xt_webfont_feature_boot(void) {
15
+ xt_webfont_feature_register();
16
+ }
17
+ #elif defined(_MSC_VER)
18
+ void xt_webfont_feature_boot(void);
19
+ #pragma section(".CRT$XCU", read)
20
+ __declspec(allocate(".CRT$XCU")) static void (*const xt_webfont_feature_ctor)(void) =
21
+ xt_webfont_feature_boot;
22
+ void xt_webfont_feature_boot(void) { xt_webfont_feature_register(); }
23
+ #endif