@geastack/engine 0.1.32 → 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.32",
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
  }
@@ -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
+