@geastack/engine 0.1.31 → 0.1.33

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/image_store.cpp CHANGED
@@ -4,7 +4,11 @@
4
4
  #include "pixel.h"
5
5
  #include "gea_perf_config.h"
6
6
 
7
+ #include <cmath>
7
8
  #include <cstring>
9
+ #include <memory>
10
+ #include <string>
11
+ #include <string_view>
8
12
  #include <limits>
9
13
 
10
14
  #if GEA_EMBEDDED_PERF && defined(ESP_PLATFORM)
@@ -87,6 +91,60 @@ void *gea_stbi_realloc_sized(void *ptr, std::size_t oldSize, std::size_t newSize
87
91
  #define STB_IMAGE_IMPLEMENTATION
88
92
  #include "stb_image.h"
89
93
 
94
+ #if defined(__APPLE__) || defined(_WIN32) || (defined(__linux__) && !defined(ESP_PLATFORM))
95
+ #define GEA_HOST_SVG_IMAGES 1
96
+ #define NANOSVG_IMPLEMENTATION
97
+ #include "vendor/nanosvg/nanosvg.h"
98
+ #define NANOSVGRAST_IMPLEMENTATION
99
+ #include "vendor/nanosvg/nanosvgrast.h"
100
+
101
+ namespace {
102
+ bool isSvgImage(const std::uint8_t *data, int length)
103
+ {
104
+ std::string_view xml(reinterpret_cast<const char *>(data), length);
105
+ if (xml.starts_with("\xef\xbb\xbf")) xml.remove_prefix(3);
106
+ while (true) {
107
+ const auto start = xml.find_first_not_of(" \t\n\r");
108
+ if (start == xml.npos) return false;
109
+ xml.remove_prefix(start);
110
+ const bool declaration = xml.starts_with("<?");
111
+ if (!declaration && !xml.starts_with("<!--")) break;
112
+ const auto close = declaration ? std::string_view("?>") : std::string_view("-->");
113
+ const auto end = xml.find(close);
114
+ if (end == xml.npos) return false;
115
+ xml.remove_prefix(end + close.size());
116
+ }
117
+ return xml.starts_with("<svg") && xml.size() > 4 &&
118
+ std::string_view(" \t\n\r>/").find(xml[4]) != xml.npos;
119
+ }
120
+
121
+ stbi_uc *rasterizeSvgImage(const std::uint8_t *data, int length, int &width, int &height)
122
+ {
123
+ constexpr int maxPixels = 4 * 1024 * 1024;
124
+ if (length > 1024 * 1024) return nullptr;
125
+ std::string markup(reinterpret_cast<const char *>(data), length);
126
+ std::unique_ptr<NSVGimage, decltype(&nsvgDelete)> image(nsvgParse(markup.data(), "px", 96), nsvgDelete);
127
+ if (!image || !(image->width > 0 && image->width <= 8192 &&
128
+ image->height > 0 && image->height <= 8192)) return nullptr;
129
+
130
+ width = static_cast<int>(std::ceil(image->width));
131
+ height = static_cast<int>(std::ceil(image->height));
132
+ const int pixels = width * height;
133
+ if (pixels > maxPixels) return nullptr;
134
+
135
+ std::unique_ptr<NSVGrasterizer, decltype(&nsvgDeleteRasterizer)> rasterizer(
136
+ nsvgCreateRasterizer(), nsvgDeleteRasterizer);
137
+ if (!rasterizer) return nullptr;
138
+
139
+ auto *rgba = static_cast<stbi_uc *>(gea_stbi_malloc(static_cast<std::size_t>(pixels) * 4));
140
+ if (!rgba) return nullptr;
141
+ nsvgRasterize(rasterizer.get(), image.get(), 0, 0, 1, rgba, width, height, width * 4);
142
+ return rgba;
143
+ }
144
+ } // namespace
145
+ #endif
146
+
147
+
90
148
  #if defined(GEA_EMBEDDED_ROM_TJPGD) && GEA_EMBEDDED_ROM_TJPGD
91
149
  // The ROM emits one RGB888 MCU at a time, not a full-frame RGB(A) buffer.
92
150
  #if defined(CONFIG_IDF_TARGET_ESP32S3)
@@ -293,6 +351,9 @@ ImageFormat ImageStore::detectFormat(const std::uint8_t *data, int length) const
293
351
  if (data[0] == 0xFF && data[1] == 0xD8) return ImageFormat::Jpeg;
294
352
  if (data[0] == 0x89 && data[1] == 'P' && data[2] == 'N' && data[3] == 'G') return ImageFormat::Png;
295
353
  if (data[0] == 'G' && data[1] == 'I' && data[2] == 'F') return ImageFormat::Gif;
354
+ #if GEA_HOST_SVG_IMAGES
355
+ if (isSvgImage(data, length)) return ImageFormat::Svg;
356
+ #endif
296
357
  return ImageFormat::Unknown;
297
358
  }
298
359
 
@@ -915,7 +976,11 @@ int ImageStore::decodeStatic(ImageSlot &image, const std::uint8_t *data, int len
915
976
  }
916
977
  #endif
917
978
 
918
- stbi_uc *rgba = stbi_load_from_memory(data, length, &width, &height, &components, 4);
979
+ stbi_uc *rgba =
980
+ #if GEA_HOST_SVG_IMAGES
981
+ image.format == ImageFormat::Svg ? rasterizeSvgImage(data, length, width, height) :
982
+ #endif
983
+ stbi_load_from_memory(data, length, &width, &height, &components, 4);
919
984
  if (!rgba) return -1;
920
985
 
921
986
  const int pixelCount = width * height;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geastack/engine",
3
- "version": "0.1.31",
3
+ "version": "0.1.33",
4
4
  "description": "GeaStack rendering engine, intrinsic JSX primitives, and native engine backends.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -57,6 +57,6 @@
57
57
  },
58
58
  "sideEffects": false,
59
59
  "dependencies": {
60
- "@geastack/core": "^0.1.63"
60
+ "@geastack/core": "^0.1.66"
61
61
  }
62
62
  }
package/ui/style.cpp CHANGED
@@ -11941,6 +11941,61 @@ void replayInlineStyles(int node)
11941
11941
  #endif
11942
11942
  }
11943
11943
 
11944
+ #if GEA_UI_IMAGE_NODES
11945
+ struct ImageDimensionHint {
11946
+ double value = -1;
11947
+ bool percentage = false;
11948
+ };
11949
+
11950
+ ImageDimensionHint parseImageDimensionHint(const char *value)
11951
+ {
11952
+ if (!value) return {};
11953
+ while (*value == ' ' || *value == '\t' || *value == '\r' ||
11954
+ *value == '\n' || *value == '\f') ++value;
11955
+ if (*value < '0' || *value > '9') return {};
11956
+
11957
+ double number = 0;
11958
+ while (*value >= '0' && *value <= '9') {
11959
+ number = number * 10 + (*value++ - '0');
11960
+ }
11961
+ if (*value == '.') {
11962
+ ++value;
11963
+ for (double place = 0.1; *value >= '0' && *value <= '9'; place *= 0.1) {
11964
+ number += (*value++ - '0') * place;
11965
+ }
11966
+ }
11967
+ return {number, *value == '%'};
11968
+ }
11969
+
11970
+ void applyImageDimensionHints(int node)
11971
+ {
11972
+ auto &tree = Tree::instance();
11973
+ const auto width = parseImageDimensionHint(tree.getAttribute(node, "width"));
11974
+ const auto height = parseImageDimensionHint(tree.getAttribute(node, "height"));
11975
+
11976
+ const auto apply = [&](ImageDimensionHint hint, Property length, Property percentage) {
11977
+ if (hint.value < 0) return;
11978
+ if (hint.percentage) {
11979
+ tree.setStyleFromClass(node, percentage, roundToInt(std::min(32767.0, hint.value * 10)));
11980
+ } else {
11981
+ tree.setStyleFromClass(node, length, roundToInt(std::min(32767.0, hint.value * g_device_pixel_ratio)));
11982
+ }
11983
+ };
11984
+ apply(width, Property::Width, Property::WidthPercent);
11985
+ apply(height, Property::Height, Property::HeightPercent);
11986
+ #if GEA_CSS_ASPECT_RATIO
11987
+ if (!width.percentage && !height.percentage && width.value > 0 && height.value > 0) {
11988
+ // A negative ratio encodes CSS 'auto <ratio>': use the natural ratio
11989
+ // once the image is available, otherwise use the attribute pair.
11990
+ const float ratio = static_cast<float>(-width.value / height.value);
11991
+ int encoded;
11992
+ std::memcpy(&encoded, &ratio, sizeof(encoded));
11993
+ tree.setStyleFromClass(node, Property::AspectRatio, encoded);
11994
+ }
11995
+ #endif
11996
+ }
11997
+ #endif
11998
+
11944
11999
  void applyDefaultStyleOverrides(int node)
11945
12000
  {
11946
12001
  auto &state = treeState();
@@ -11950,6 +12005,9 @@ void applyDefaultStyleOverrides(int node)
11950
12005
  const NodeStyleOverride &entry = rd->defaultStyles.at(i);
11951
12006
  Tree::instance().setStyleFromClass(node, entry.property, entry.value);
11952
12007
  }
12008
+ #if GEA_UI_IMAGE_NODES
12009
+ if (state.nodes[node].type == NodeType::Image) applyImageDimensionHints(node);
12010
+ #endif
11953
12011
  }
11954
12012
 
11955
12013
  #if GEA_CSS_PSEUDO_ELEMENTS
@@ -36,6 +36,11 @@ bool sameName(const char *a, const char *b)
36
36
  return a && b && std::strcmp(a, b) == 0;
37
37
  }
38
38
 
39
+ bool isImageDimensionAttribute(const Node &node, const char *name)
40
+ {
41
+ return node.type == NodeType::Image && (sameName(name, "width") || sameName(name, "height"));
42
+ }
43
+
39
44
  bool audioBooleanAttributeEnabled(const char *value)
40
45
  {
41
46
  if (!value) return false;
@@ -963,6 +968,9 @@ void Tree::setAttribute(int node, const char *name, const char *value)
963
968
  if (state.nodes[node].type == NodeType::Image && sameName(name, "fit")) {
964
969
  Style(node).imageFit(imageFitAttributeValue(value));
965
970
  }
971
+ if (isImageDimensionAttribute(state.nodes[node], name)) {
972
+ StyleSheet::instance().recomputeSubtree(node);
973
+ }
966
974
  applyAudioAttribute(*this, node, name, value ? value : "");
967
975
  if (state.nodes[node].type == NodeType::VirtualList) VirtualListRenderer::configureAttribute(node, name, value);
968
976
  // Tree::setText marks render.dirty + non_scroll_dirty so the
@@ -998,6 +1006,9 @@ void Tree::removeAttribute(int node, const char *name)
998
1006
  if (!rd) return; // no rare-data block → no attributes to remove
999
1007
  const bool hadAttribute = rd->attributes.remove(name);
1000
1008
  if (!hadAttribute) return;
1009
+ if (isImageDimensionAttribute(state.nodes[node], name)) {
1010
+ StyleSheet::instance().recomputeSubtree(node);
1011
+ }
1001
1012
  if (state.nodes[node].type == NodeType::Image && sameName(name, "src")) {
1002
1013
  setStyleFromClass(node, Property::ImageId, -1);
1003
1014
  markDisplayListDirty();
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2013-14 Mikko Mononen memon@inside.org
2
+
3
+ This software is provided 'as-is', without any express or implied
4
+ warranty. In no event will the authors be held liable for any damages
5
+ arising from the use of this software.
6
+
7
+ Permission is granted to anyone to use this software for any purpose,
8
+ including commercial applications, and to alter it and redistribute it
9
+ freely, subject to the following restrictions:
10
+
11
+ 1. The origin of this software must not be misrepresented; you must not
12
+ claim that you wrote the original software. If you use this software
13
+ in a product, an acknowledgment in the product documentation would be
14
+ appreciated but is not required.
15
+ 2. Altered source versions must be plainly marked as such, and must not be
16
+ misrepresented as being the original software.
17
+ 3. This notice may not be removed or altered from any source distribution.
18
+