@ng-native/desktop 0.26.1 → 0.27.0
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/README.md +6 -3
- package/bin/app-config.mjs +1 -0
- package/bin/archive.mjs +166 -0
- package/bin/autolink.mjs +61 -30
- package/bin/cli.mjs +127 -27
- package/bin/host.mjs +255 -58
- package/bin/package-linux.mjs +1394 -0
- package/bin/package.mjs +171 -35
- package/bin/plist.mjs +6 -2
- package/bin/png-icon.mjs +315 -0
- package/bin/prebuilt.mjs +147 -26
- package/bin/toolchain.mjs +258 -19
- package/native/CMakeLists.txt +64 -6
- package/native/appkit/CMakeLists.txt +2 -1
- package/native/appkit/appkit-backend.h +1 -0
- package/native/appkit/appkit-backend.mm +13 -0
- package/native/appkit/header-toolbar.mm +28 -25
- package/native/appkit/host-view.h +10 -0
- package/native/appkit/host-view.mm +146 -40
- package/native/appkit/input-replay.h +6 -0
- package/native/appkit/input-replay.mm +21 -12
- package/native/appkit/main.mm +11 -7
- package/native/appkit/native-controls.mm +13 -1
- package/native/appkit/split-view.mm +7 -13
- package/native/appkit/text-input-field.h +4 -0
- package/native/appkit/text-input-field.mm +20 -1
- package/native/cmake/curl.cmake +79 -0
- package/native/cmake/libraries/react-native-reanimated.cmake +5 -0
- package/native/cmake/libraries/react-native-worklets.cmake +5 -0
- package/native/cmake/linked-modules.cmake +20 -4
- package/native/cmake/react-native.cmake +18 -1
- package/native/core/include/ngn/accent-colors.h +38 -0
- package/native/core/include/ngn/accessibility-relations.h +65 -0
- package/native/core/include/ngn/accessibility.h +26 -0
- package/native/core/include/ngn/animated-frame-loop.h +56 -0
- package/native/core/include/ngn/app-info.h +24 -8
- package/native/core/include/ngn/app-menus.h +14 -1
- package/native/core/include/ngn/app-windows.h +15 -2
- package/native/core/include/ngn/aria-role.h +96 -0
- package/native/core/include/ngn/audio.h +2 -0
- package/native/core/include/ngn/camera.h +9 -0
- package/native/core/include/ngn/control-sizes.h +7 -2
- package/native/core/include/ngn/crash-report.h +31 -0
- package/native/core/include/ngn/curl-network.h +93 -0
- package/native/core/include/ngn/dbus-menu.h +39 -0
- package/native/core/include/ngn/default-menu.h +36 -0
- package/native/core/include/ngn/desktop-expo.h +7 -1
- package/native/core/include/ngn/desktop-modules.h +22 -0
- package/native/core/include/ngn/desktop-props.h +21 -1
- package/native/core/include/ngn/desktop-view-props.h +6 -0
- package/native/core/include/ngn/focus-order.h +10 -0
- package/native/core/include/ngn/header-bar.h +22 -0
- package/native/core/include/ngn/host-os.h +6 -0
- package/native/core/include/ngn/host-reload.h +21 -0
- package/native/core/include/ngn/host-state.h +23 -0
- package/native/core/include/ngn/image-source.h +26 -1
- package/native/core/include/ngn/input-script.h +3 -3
- package/native/core/include/ngn/input.h +4 -1
- package/native/core/include/ngn/launch-frame.h +21 -0
- package/native/core/include/ngn/launch-settings.h +23 -5
- package/native/core/include/ngn/mounting-backend.h +5 -0
- package/native/core/include/ngn/mounting-manager.h +1 -0
- package/native/core/include/ngn/native-controls.h +26 -5
- package/native/core/include/ngn/notification-schedule.h +74 -0
- package/native/core/include/ngn/notifications.h +2 -1
- package/native/core/include/ngn/openssl-update-crypto.h +15 -0
- package/native/core/include/ngn/photo-library.h +60 -20
- package/native/core/include/ngn/posix-locale.h +43 -0
- package/native/core/include/ngn/runtime.h +5 -0
- package/native/core/include/ngn/safe-areas.h +14 -2
- package/native/core/include/ngn/split-view.h +78 -0
- package/native/core/include/ngn/surface-metrics.h +7 -0
- package/native/core/include/ngn/system.h +14 -0
- package/native/core/include/ngn/text-input.h +13 -0
- package/native/core/include/ngn/text-units.h +38 -0
- package/native/core/include/ngn/updates.h +14 -2
- package/native/core/include/ngn/view-box.h +40 -0
- package/native/core/include/ngn/view-transform.h +15 -0
- package/native/core/include/ngn/web-browser.h +46 -0
- package/native/core/src/accent-colors.cpp +88 -0
- package/native/core/src/accessibility-relations.cpp +138 -0
- package/native/core/src/accessibility.cpp +50 -0
- package/native/core/src/animated-frame-loop.cpp +166 -0
- package/native/core/src/app-menus.cpp +26 -9
- package/native/core/src/app-windows.cpp +27 -6
- package/native/core/src/aria-role.cpp +127 -0
- package/native/core/src/control-sizes.cpp +9 -5
- package/native/core/src/crash-report.cpp +196 -0
- package/native/core/src/curl-network.cpp +1562 -0
- package/native/core/src/dbus-menu.cpp +71 -0
- package/native/core/src/default-menu.cpp +134 -0
- package/native/core/src/desktop-view-props.cpp +14 -0
- package/native/core/src/focus-order.cpp +19 -0
- package/native/core/src/header-bar.cpp +20 -0
- package/native/core/src/host-os.cpp +19 -0
- package/native/core/src/host-reload.cpp +45 -0
- package/native/core/src/host-state.cpp +8 -0
- package/native/core/src/image-source.cpp +90 -0
- package/native/core/src/input-dispatcher.cpp +27 -5
- package/native/core/src/input-script.cpp +3 -2
- package/native/core/src/launch-settings.cpp +76 -0
- package/native/core/src/libraries/react-native-reanimated.cpp +6 -1
- package/native/core/src/modules/app-info.cpp +55 -6
- package/native/core/src/modules/app-modules.cpp +1 -1
- package/native/core/src/modules/audio.cpp +4 -2
- package/native/core/src/modules/camera.cpp +8 -11
- package/native/core/src/modules/desktop-expo.cpp +10 -5
- package/native/core/src/modules/desktop-modules.cpp +106 -41
- package/native/core/src/modules/file-photo-picker.cpp +408 -0
- package/native/core/src/modules/notifications.cpp +13 -17
- package/native/core/src/modules/photo-library.cpp +42 -13
- package/native/core/src/modules/web-browser.cpp +89 -0
- package/native/core/src/mounting-manager.cpp +12 -0
- package/native/core/src/native-controls.cpp +42 -1
- package/native/core/src/notification-schedule.cpp +429 -0
- package/native/core/src/openssl-update-crypto.cpp +83 -0
- package/native/core/src/posix-locale.cpp +192 -0
- package/native/core/src/runtime.cpp +30 -1
- package/native/core/src/safe-areas.cpp +19 -4
- package/native/core/src/spawn-rules.cpp +4 -1
- package/native/core/src/split-view.cpp +93 -0
- package/native/core/src/system.cpp +19 -0
- package/native/core/src/text-input.cpp +18 -1
- package/native/core/src/text-units.cpp +99 -0
- package/native/core/src/updates.cpp +76 -17
- package/native/core/src/view-box.cpp +75 -40
- package/native/core/src/view-transform.cpp +28 -0
- package/native/docker/Dockerfile +38 -0
- package/native/docker/Dockerfile.fedora +36 -0
- package/native/docker/glibc-floor.sh +49 -0
- package/native/gtk/CMakeLists.txt +137 -0
- package/native/gtk/accent-colour.cpp +57 -0
- package/native/gtk/accent-colour.h +41 -0
- package/native/gtk/accessibility-settings.cpp +159 -0
- package/native/gtk/accessibility-settings.h +62 -0
- package/native/gtk/accessible-text.cpp +151 -0
- package/native/gtk/accessible-text.h +49 -0
- package/native/gtk/accessible.cpp +306 -0
- package/native/gtk/accessible.h +75 -0
- package/native/gtk/alerts.cpp +267 -0
- package/native/gtk/alerts.h +30 -0
- package/native/gtk/animation-frames.cpp +159 -0
- package/native/gtk/animation-frames.h +20 -0
- package/native/gtk/app-info.cpp +91 -0
- package/native/gtk/app-info.h +25 -0
- package/native/gtk/app-windows.cpp +439 -0
- package/native/gtk/app-windows.h +150 -0
- package/native/gtk/audio.cpp +370 -0
- package/native/gtk/audio.h +54 -0
- package/native/gtk/auth-session.cpp +73 -0
- package/native/gtk/auth-session.h +16 -0
- package/native/gtk/battery.cpp +110 -0
- package/native/gtk/battery.h +19 -0
- package/native/gtk/box-paint.cpp +262 -0
- package/native/gtk/box-paint.h +32 -0
- package/native/gtk/cairo-canvas.cpp +755 -0
- package/native/gtk/cairo-canvas.h +49 -0
- package/native/gtk/camera.cpp +909 -0
- package/native/gtk/camera.h +69 -0
- package/native/gtk/capture.cpp +46 -0
- package/native/gtk/capture.h +19 -0
- package/native/gtk/clock-format.cpp +151 -0
- package/native/gtk/clock-format.h +36 -0
- package/native/gtk/crypto.cpp +75 -0
- package/native/gtk/crypto.h +14 -0
- package/native/gtk/dbus-properties.cpp +131 -0
- package/native/gtk/dbus-properties.h +61 -0
- package/native/gtk/drag-and-drop.cpp +339 -0
- package/native/gtk/drag-and-drop.h +30 -0
- package/native/gtk/file-dialogs.cpp +239 -0
- package/native/gtk/file-dialogs.h +21 -0
- package/native/gtk/file-system.cpp +212 -0
- package/native/gtk/file-system.h +25 -0
- package/native/gtk/focus.cpp +282 -0
- package/native/gtk/focus.h +37 -0
- package/native/gtk/fonts.cpp +89 -0
- package/native/gtk/fonts.h +22 -0
- package/native/gtk/gst-frames.cpp +150 -0
- package/native/gtk/gst-frames.h +47 -0
- package/native/gtk/gst-playback.cpp +496 -0
- package/native/gtk/gst-playback.h +81 -0
- package/native/gtk/gtk-backend.cpp +690 -0
- package/native/gtk/gtk-backend.h +134 -0
- package/native/gtk/host-sources.cpp +354 -0
- package/native/gtk/host-sources.h +46 -0
- package/native/gtk/image-manipulator.cpp +254 -0
- package/native/gtk/image-manipulator.h +30 -0
- package/native/gtk/image-view.cpp +256 -0
- package/native/gtk/image-view.h +54 -0
- package/native/gtk/input-replay.cpp +299 -0
- package/native/gtk/input-replay.h +36 -0
- package/native/gtk/launcher-badge.cpp +29 -0
- package/native/gtk/launcher-badge.h +21 -0
- package/native/gtk/locales.cpp +131 -0
- package/native/gtk/locales.h +23 -0
- package/native/gtk/location.cpp +274 -0
- package/native/gtk/location.h +19 -0
- package/native/gtk/main.cpp +1092 -0
- package/native/gtk/menus.cpp +447 -0
- package/native/gtk/menus.h +100 -0
- package/native/gtk/native-controls.cpp +831 -0
- package/native/gtk/native-controls.h +58 -0
- package/native/gtk/network-state.cpp +72 -0
- package/native/gtk/network-state.h +24 -0
- package/native/gtk/notifications.cpp +235 -0
- package/native/gtk/notifications.h +30 -0
- package/native/gtk/pango-text.cpp +274 -0
- package/native/gtk/pango-text.h +73 -0
- package/native/gtk/portal.cpp +451 -0
- package/native/gtk/portal.h +71 -0
- package/native/gtk/printing.cpp +338 -0
- package/native/gtk/printing.h +34 -0
- package/native/gtk/proxy-resolver.cpp +74 -0
- package/native/gtk/proxy-resolver.h +20 -0
- package/native/gtk/screens.cpp +1461 -0
- package/native/gtk/screens.h +274 -0
- package/native/gtk/scroll-view.cpp +414 -0
- package/native/gtk/scroll-view.h +82 -0
- package/native/gtk/secret-store.cpp +120 -0
- package/native/gtk/secret-store.h +27 -0
- package/native/gtk/settings-portal.cpp +129 -0
- package/native/gtk/settings-portal.h +47 -0
- package/native/gtk/split-view.cpp +1105 -0
- package/native/gtk/split-view.h +195 -0
- package/native/gtk/status-notifier.cpp +487 -0
- package/native/gtk/status-notifier.h +43 -0
- package/native/gtk/surface-input.cpp +251 -0
- package/native/gtk/surface-input.h +57 -0
- package/native/gtk/system-access.cpp +295 -0
- package/native/gtk/system-access.h +37 -0
- package/native/gtk/text-field.cpp +827 -0
- package/native/gtk/text-field.h +78 -0
- package/native/gtk/video.cpp +527 -0
- package/native/gtk/video.h +47 -0
- package/native/gtk/view-widget.cpp +431 -0
- package/native/gtk/view-widget.h +105 -0
- package/native/gtk/window-scale.cpp +95 -0
- package/native/gtk/window-scale.h +35 -0
- package/native/scripts/atspi-check.py +310 -0
- package/native/scripts/atspi-check.test.py +48 -0
- package/native/scripts/build.sh +20 -3
- package/native/scripts/compilers.sh +17 -0
- package/native/scripts/deb-depends-check.sh +22 -0
- package/native/scripts/fetch.sh +23 -2
- package/native/scripts/launch-cell.sh +157 -0
- package/native/scripts/launch-probe.sh +14 -3
- package/native/scripts/make-prebuilt.mjs +133 -44
- package/native/scripts/prebuilt-host-cache.mjs +71 -40
- package/native/scripts/reset-for-compilers.sh +44 -0
- package/native/scripts/start-xvfb.sh +35 -0
- package/native/shim/paragraph-platform/react/renderer/components/text/HostPlatformParagraphProps.h +35 -0
- package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +5 -2
- package/native/shim/worklets/linux-reanimated-props.h +14 -0
- package/native/shim/worklets/linux-worklets.h +23 -0
- package/package.json +6 -3
- package/prebuilt.json +70 -6
|
@@ -0,0 +1,755 @@
|
|
|
1
|
+
#include "cairo-canvas.h"
|
|
2
|
+
|
|
3
|
+
#include "image-view.h"
|
|
4
|
+
#include "pango-text.h"
|
|
5
|
+
|
|
6
|
+
#include <react/renderer/attributedstring/TextAttributes.h>
|
|
7
|
+
|
|
8
|
+
#include <pango/pangocairo.h>
|
|
9
|
+
|
|
10
|
+
#include <algorithm>
|
|
11
|
+
#include <array>
|
|
12
|
+
#include <cmath>
|
|
13
|
+
#include <map>
|
|
14
|
+
#include <optional>
|
|
15
|
+
#include <set>
|
|
16
|
+
#include <string>
|
|
17
|
+
#include <utility>
|
|
18
|
+
#include <vector>
|
|
19
|
+
|
|
20
|
+
namespace ngn::gtk {
|
|
21
|
+
|
|
22
|
+
namespace {
|
|
23
|
+
|
|
24
|
+
void setColor(cairo_t *cr, uint32_t argb, double opacity) {
|
|
25
|
+
cairo_set_source_rgba(
|
|
26
|
+
cr, ((argb >> 16) & 0xff) / 255.0, ((argb >> 8) & 0xff) / 255.0, (argb & 0xff) / 255.0, ((argb >> 24) & 0xff) / 255.0 * opacity);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
cairo_matrix_t cairoMatrix(const SvgMatrix &m) {
|
|
30
|
+
cairo_matrix_t out;
|
|
31
|
+
cairo_matrix_init(&out, m.a, m.b, m.c, m.d, m.e, m.f);
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
void addPath(cairo_t *cr, const SvgPath &path) {
|
|
36
|
+
cairo_new_path(cr);
|
|
37
|
+
for (const auto &s : path.segments) {
|
|
38
|
+
switch (s.op) {
|
|
39
|
+
case SvgPath::Op::Move:
|
|
40
|
+
cairo_move_to(cr, s.points[0], s.points[1]);
|
|
41
|
+
break;
|
|
42
|
+
case SvgPath::Op::Line:
|
|
43
|
+
cairo_line_to(cr, s.points[0], s.points[1]);
|
|
44
|
+
break;
|
|
45
|
+
case SvgPath::Op::Cubic:
|
|
46
|
+
cairo_curve_to(cr, s.points[0], s.points[1], s.points[2], s.points[3], s.points[4], s.points[5]);
|
|
47
|
+
break;
|
|
48
|
+
case SvgPath::Op::Close:
|
|
49
|
+
cairo_close_path(cr);
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
void applyStroke(cairo_t *cr, const SvgStroke &stroke) {
|
|
56
|
+
cairo_set_line_width(cr, stroke.width);
|
|
57
|
+
cairo_set_line_cap(cr, stroke.cap == 1 ? CAIRO_LINE_CAP_ROUND : stroke.cap == 2 ? CAIRO_LINE_CAP_SQUARE : CAIRO_LINE_CAP_BUTT);
|
|
58
|
+
cairo_set_line_join(cr, stroke.join == 1 ? CAIRO_LINE_JOIN_ROUND : stroke.join == 2 ? CAIRO_LINE_JOIN_BEVEL : CAIRO_LINE_JOIN_MITER);
|
|
59
|
+
cairo_set_miter_limit(cr, stroke.miterLimit);
|
|
60
|
+
cairo_set_dash(cr, stroke.dashes.empty() ? nullptr : stroke.dashes.data(), static_cast<int>(stroke.dashes.size()), stroke.dashOffset);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// A gradient as a cairo pattern over user space, at `opacity`: its stops' offsets only grow, as
|
|
64
|
+
// SVG clamps a smaller one to the one before, and it pads past its ends. Null for a gradient whose
|
|
65
|
+
// transform cannot be inverted, which paints nothing.
|
|
66
|
+
cairo_pattern_t *gradientPattern(const SvgGradient &g, double opacity) {
|
|
67
|
+
cairo_matrix_t toGradient = cairoMatrix(g.transform);
|
|
68
|
+
if (cairo_matrix_invert(&toGradient) != CAIRO_STATUS_SUCCESS) {
|
|
69
|
+
return nullptr;
|
|
70
|
+
}
|
|
71
|
+
cairo_pattern_t *pattern = g.radial ? cairo_pattern_create_radial(g.x2, g.y2, 0, g.x1, g.y1, g.r)
|
|
72
|
+
: cairo_pattern_create_linear(g.x1, g.y1, g.x2, g.y2);
|
|
73
|
+
double last = 0;
|
|
74
|
+
bool first = true;
|
|
75
|
+
for (const auto &[offset, argb] : g.stops) {
|
|
76
|
+
last = std::clamp(first ? offset : std::max(offset, last), 0.0, 1.0);
|
|
77
|
+
first = false;
|
|
78
|
+
cairo_pattern_add_color_stop_rgba(
|
|
79
|
+
pattern, last, ((argb >> 16) & 0xff) / 255.0, ((argb >> 8) & 0xff) / 255.0, (argb & 0xff) / 255.0,
|
|
80
|
+
((argb >> 24) & 0xff) / 255.0 * opacity);
|
|
81
|
+
}
|
|
82
|
+
cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
|
|
83
|
+
cairo_pattern_set_matrix(pattern, &toGradient);
|
|
84
|
+
return pattern;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// The current path filled or stroked (`fill`) in `paint`: a colour, or a gradient.
|
|
88
|
+
void paintPath(cairo_t *cr, const SvgPaint &paint, bool fill) {
|
|
89
|
+
if (paint.gradient) {
|
|
90
|
+
cairo_pattern_t *pattern = gradientPattern(*paint.gradient, paint.opacity);
|
|
91
|
+
if (!pattern) {
|
|
92
|
+
cairo_new_path(cr);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
cairo_set_source(cr, pattern);
|
|
96
|
+
cairo_pattern_destroy(pattern);
|
|
97
|
+
} else {
|
|
98
|
+
setColor(cr, paint.argb, paint.opacity);
|
|
99
|
+
}
|
|
100
|
+
fill ? cairo_fill(cr) : cairo_stroke(cr);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// The first stop of a gradient, which text takes, as on macOS.
|
|
104
|
+
uint32_t solidOf(const SvgPaint &paint) {
|
|
105
|
+
return paint.gradient && !paint.gradient->stops.empty() ? paint.gradient->stops[0].second : paint.argb;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
PangoContext *measureContext() {
|
|
109
|
+
thread_local PangoContext *context = [] {
|
|
110
|
+
PangoContext *c = pango_font_map_create_context(pango_cairo_font_map_get_default());
|
|
111
|
+
cairo_font_options_t *options = cairo_font_options_create();
|
|
112
|
+
cairo_font_options_set_hint_metrics(options, CAIRO_HINT_METRICS_OFF);
|
|
113
|
+
pango_cairo_context_set_font_options(c, options);
|
|
114
|
+
cairo_font_options_destroy(options);
|
|
115
|
+
return c;
|
|
116
|
+
}();
|
|
117
|
+
return context;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// `text` laid out on one line in the font, as React Native's text would name it, a line break in it
|
|
121
|
+
// a space as SVG's white space has it. The caller frees it.
|
|
122
|
+
PangoLayout *lineOf(std::string text, double fontSize, const std::string &family, const std::string &weight) {
|
|
123
|
+
std::replace_if(text.begin(), text.end(), [](char c) { return c == '\n' || c == '\r'; }, ' ');
|
|
124
|
+
facebook::react::TextAttributes attributes;
|
|
125
|
+
attributes.fontSize = static_cast<facebook::react::Float>(fontSize);
|
|
126
|
+
attributes.fontFamily = family;
|
|
127
|
+
int w = weight == "bold" ? 700 : std::atoi(weight.c_str());
|
|
128
|
+
attributes.fontWeight = static_cast<facebook::react::FontWeight>(w > 0 ? w : 400);
|
|
129
|
+
PangoLayout *layout = pango_layout_new(measureContext());
|
|
130
|
+
PangoFontDescription *font = fontOf(attributes);
|
|
131
|
+
pango_layout_set_font_description(layout, font);
|
|
132
|
+
pango_font_description_free(font);
|
|
133
|
+
pango_layout_set_text(layout, text.c_str(), static_cast<int>(text.size()));
|
|
134
|
+
return layout;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// A cluster's glyphs as its run shaped them: the run's font and the cluster's glyphs, placed from
|
|
138
|
+
// the cluster's start.
|
|
139
|
+
struct ShapedGlyphs {
|
|
140
|
+
PangoFont *font{nullptr};
|
|
141
|
+
PangoGlyphString *glyphs{nullptr};
|
|
142
|
+
ShapedGlyphs() = default;
|
|
143
|
+
ShapedGlyphs(const ShapedGlyphs &) = delete;
|
|
144
|
+
ShapedGlyphs &operator=(const ShapedGlyphs &) = delete;
|
|
145
|
+
~ShapedGlyphs() {
|
|
146
|
+
if (font) {
|
|
147
|
+
g_object_unref(font);
|
|
148
|
+
}
|
|
149
|
+
if (glyphs) {
|
|
150
|
+
pango_glyph_string_free(glyphs);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
// Images an svg's <image> draws, by uri, loaded once for every svg, and the canvases waiting for one.
|
|
156
|
+
struct SvgImages {
|
|
157
|
+
std::map<std::string, cairo_surface_t *> loaded;
|
|
158
|
+
// Each with the svg view it is for (its SvgImageHold), or null.
|
|
159
|
+
std::map<std::string, std::vector<std::pair<const SvgImageHold *, std::function<void()>>>> waiting;
|
|
160
|
+
// The uris that failed to load or to become a cairo surface: not asked for again on each draw.
|
|
161
|
+
std::set<std::string> failed;
|
|
162
|
+
// The loaded ones in the order they loaded.
|
|
163
|
+
std::vector<std::pair<std::string, cairo_surface_t *>> order;
|
|
164
|
+
// How many svg views hold each uri held (SvgImageHold): a held image is not let go.
|
|
165
|
+
std::map<std::string, int> holders;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
SvgImages &svgImages() {
|
|
169
|
+
static SvgImages images;
|
|
170
|
+
return images;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// At most `keep` kept, the first loaded going first, as macOS lets its image cache go; one an svg
|
|
174
|
+
// view holds stays, past `keep` when need be.
|
|
175
|
+
void trimSvgImages(size_t keep) {
|
|
176
|
+
auto &images = svgImages();
|
|
177
|
+
while (images.loaded.size() > keep) {
|
|
178
|
+
auto unheld = std::find_if(images.order.begin(), images.order.end(), [&images](const auto &entry) {
|
|
179
|
+
return !images.holders.contains(entry.first);
|
|
180
|
+
});
|
|
181
|
+
if (unheld == images.order.end()) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
cairo_surface_destroy(unheld->second);
|
|
185
|
+
images.loaded.erase(unheld->first);
|
|
186
|
+
images.order.erase(unheld);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Null when cairo cannot hold the picture: larger than an image surface can be, or out of memory.
|
|
191
|
+
cairo_surface_t *surfaceOf(GdkTexture *texture) {
|
|
192
|
+
int width = gdk_texture_get_width(texture), height = gdk_texture_get_height(texture);
|
|
193
|
+
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
|
|
194
|
+
if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
|
|
195
|
+
cairo_surface_destroy(surface);
|
|
196
|
+
return nullptr;
|
|
197
|
+
}
|
|
198
|
+
cairo_surface_flush(surface);
|
|
199
|
+
gdk_texture_download(texture, cairo_image_surface_get_data(surface), static_cast<gsize>(cairo_image_surface_get_stride(surface)));
|
|
200
|
+
cairo_surface_mark_dirty(surface);
|
|
201
|
+
return surface;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
class CairoCanvas final : public SvgCanvas {
|
|
205
|
+
public:
|
|
206
|
+
CairoCanvas(cairo_t *cr, double width, double height, double scale, std::function<void()> imageLoaded, SvgImageHold *hold)
|
|
207
|
+
: cr_(cr), width_(width), height_(height), scale_(scale), imageLoaded_(std::move(imageLoaded)), hold_(hold) {
|
|
208
|
+
cairo_get_matrix(cr_, &base_);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
void save() override {
|
|
212
|
+
cairo_save(cr_);
|
|
213
|
+
saved_.push_back({matrix_, strokeClip_});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
void restore() override {
|
|
217
|
+
cairo_restore(cr_);
|
|
218
|
+
if (!saved_.empty()) {
|
|
219
|
+
matrix_ = saved_.back().matrix;
|
|
220
|
+
strokeClip_ = saved_.back().strokeClip;
|
|
221
|
+
saved_.pop_back();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// A transform that cannot be inverted, as scale(0), draws nothing until the restore: given to
|
|
226
|
+
// cairo it would put the context in an error state that no restore clears.
|
|
227
|
+
void concat(const SvgMatrix &m) override {
|
|
228
|
+
cairo_matrix_t transform = cairoMatrix(m), inverse = transform;
|
|
229
|
+
if (cairo_matrix_invert(&inverse) == CAIRO_STATUS_SUCCESS) {
|
|
230
|
+
cairo_transform(cr_, &transform);
|
|
231
|
+
} else {
|
|
232
|
+
cairo_new_path(cr_);
|
|
233
|
+
cairo_rectangle(cr_, 0, 0, 0, 0);
|
|
234
|
+
cairo_clip(cr_);
|
|
235
|
+
}
|
|
236
|
+
matrix_ = matrix_ * m;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
void clip(const SvgPath &path, bool evenOdd) override {
|
|
240
|
+
addPath(cr_, path);
|
|
241
|
+
cairo_set_fill_rule(cr_, evenOdd ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
|
|
242
|
+
cairo_clip(cr_);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
void fill(const SvgPath &path, bool evenOdd, const SvgPaint &paint) override {
|
|
246
|
+
addPath(cr_, path);
|
|
247
|
+
cairo_set_fill_rule(cr_, evenOdd ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
|
|
248
|
+
paintPath(cr_, paint, true);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
void stroke(const SvgPath &path, const SvgStroke &stroke, const SvgPaint &paint) override {
|
|
252
|
+
cairo_save(cr_);
|
|
253
|
+
applyStroke(cr_, stroke);
|
|
254
|
+
addPath(cr_, path);
|
|
255
|
+
paintPath(cr_, paint, false);
|
|
256
|
+
cairo_restore(cr_);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
void beginLayer(double opacity) override {
|
|
260
|
+
cairo_save(cr_);
|
|
261
|
+
cairo_push_group(cr_);
|
|
262
|
+
layers_.push_back(opacity);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
void endLayer() override {
|
|
266
|
+
if (layers_.empty()) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
cairo_pop_group_to_source(cr_);
|
|
270
|
+
cairo_paint_with_alpha(cr_, layers_.back());
|
|
271
|
+
layers_.pop_back();
|
|
272
|
+
cairo_restore(cr_);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// The mask is drawn in a bitmap of the view's pixels: a luminance mask in colour, turned into
|
|
276
|
+
// its luminance times its alpha, as SVG masks by; an alpha mask's alpha as it is.
|
|
277
|
+
void beginMask(bool alpha) override {
|
|
278
|
+
cairo_surface_t *surface = viewSurface();
|
|
279
|
+
if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
|
|
280
|
+
// No bitmap (a view past cairo's limit): the mask's own drawing goes to a scratch bitmap and
|
|
281
|
+
// is dropped, and the masked content then draws unmasked, as a filter's source does.
|
|
282
|
+
cairo_surface_destroy(surface);
|
|
283
|
+
cairo_surface_t *scratch = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);
|
|
284
|
+
cairo_t *mask = drawingInto(scratch);
|
|
285
|
+
cairo_surface_destroy(scratch);
|
|
286
|
+
masks_.push_back({.parent = cr_, .mask = mask, .surface = nullptr, .saved = saved_, .alpha = alpha});
|
|
287
|
+
cr_ = mask;
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
cairo_t *mask = drawingInto(surface);
|
|
291
|
+
masks_.push_back({.parent = cr_, .mask = mask, .surface = surface, .saved = saved_, .alpha = alpha});
|
|
292
|
+
cr_ = mask;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
void endMask() override {
|
|
296
|
+
if (masks_.empty()) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
Mask &frame = masks_.back();
|
|
300
|
+
cr_ = frame.parent;
|
|
301
|
+
saved_ = frame.saved;
|
|
302
|
+
cairo_destroy(frame.mask);
|
|
303
|
+
frame.mask = nullptr;
|
|
304
|
+
if (!frame.surface) {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
if (!frame.alpha) {
|
|
308
|
+
toLuminance(frame.surface);
|
|
309
|
+
}
|
|
310
|
+
// What follows is drawn in a group, which endMasked draws through the mask.
|
|
311
|
+
cairo_save(cr_);
|
|
312
|
+
cairo_push_group(cr_);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
void endMasked() override {
|
|
316
|
+
if (masks_.empty()) {
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
Mask frame = masks_.back();
|
|
320
|
+
masks_.pop_back();
|
|
321
|
+
if (!frame.surface) {
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
cairo_pop_group_to_source(cr_);
|
|
325
|
+
cairo_set_matrix(cr_, &base_);
|
|
326
|
+
cairo_mask_surface(cr_, frame.surface, 0, 0);
|
|
327
|
+
cairo_surface_destroy(frame.surface);
|
|
328
|
+
cairo_restore(cr_);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// A pattern stroke: the next pattern paints along this stroke, not over the clip.
|
|
332
|
+
void clipToStroke(const SvgPath &path, const SvgStroke &stroke) override {
|
|
333
|
+
strokeClip_ = std::make_pair(path, stroke);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// The tile is drawn once into a bitmap at the pixels it covers, which repeats over the clip, or
|
|
337
|
+
// along the stroke clipToStroke set.
|
|
338
|
+
void pattern(const SvgPatternTile &tile, double opacity, const std::function<void(SvgCanvas &)> &drawTile) override {
|
|
339
|
+
SvgMatrix toView = matrix_ * tile.transform;
|
|
340
|
+
double density = scale_ * std::sqrt(std::abs(toView.a * toView.d - toView.b * toView.c));
|
|
341
|
+
if (!(density > 0) || tile.tile.width <= 0 || tile.tile.height <= 0) {
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
// At most 2048 pixels a side: a larger tile is drawn coarser.
|
|
345
|
+
density = std::min({density, 2048 / tile.tile.width, 2048 / tile.tile.height});
|
|
346
|
+
int width = std::max(1, static_cast<int>(std::ceil(tile.tile.width * density)));
|
|
347
|
+
int height = std::max(1, static_cast<int>(std::ceil(tile.tile.height * density)));
|
|
348
|
+
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
|
|
349
|
+
cairo_surface_set_device_scale(surface, width / tile.tile.width, height / tile.tile.height);
|
|
350
|
+
{
|
|
351
|
+
cairo_t *cr = cairo_create(surface);
|
|
352
|
+
// At the bitmap's own pixels a point, which rounding up makes more than `density`: a mask's or
|
|
353
|
+
// filter's bitmap in the tile lines up with it.
|
|
354
|
+
double pixels = std::min(width / tile.tile.width, height / tile.tile.height);
|
|
355
|
+
CairoCanvas inner(cr, tile.tile.width, tile.tile.height, pixels, imageLoaded_, hold_);
|
|
356
|
+
drawTile(inner);
|
|
357
|
+
cairo_destroy(cr);
|
|
358
|
+
}
|
|
359
|
+
// The tile's place is in the pattern's own matrix, from user space to the tile's: a source takes
|
|
360
|
+
// the user space current where it is set, which is the outer one below.
|
|
361
|
+
cairo_pattern_t *repeat = cairo_pattern_create_for_surface(surface);
|
|
362
|
+
cairo_matrix_t toTile = cairoMatrix(tile.transform);
|
|
363
|
+
cairo_matrix_translate(&toTile, tile.tile.x, tile.tile.y);
|
|
364
|
+
if (cairo_matrix_invert(&toTile) != CAIRO_STATUS_SUCCESS) {
|
|
365
|
+
cairo_pattern_destroy(repeat);
|
|
366
|
+
cairo_surface_destroy(surface);
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
cairo_pattern_set_matrix(repeat, &toTile);
|
|
370
|
+
cairo_pattern_set_extend(repeat, CAIRO_EXTEND_REPEAT);
|
|
371
|
+
cairo_save(cr_);
|
|
372
|
+
if (strokeClip_) {
|
|
373
|
+
// Through a group, so the opacity applies once where the stroke crosses itself.
|
|
374
|
+
cairo_push_group(cr_);
|
|
375
|
+
cairo_set_source(cr_, repeat);
|
|
376
|
+
applyStroke(cr_, strokeClip_->second);
|
|
377
|
+
addPath(cr_, strokeClip_->first);
|
|
378
|
+
cairo_stroke(cr_);
|
|
379
|
+
cairo_pop_group_to_source(cr_);
|
|
380
|
+
} else {
|
|
381
|
+
cairo_set_source(cr_, repeat);
|
|
382
|
+
}
|
|
383
|
+
cairo_paint_with_alpha(cr_, opacity);
|
|
384
|
+
cairo_restore(cr_);
|
|
385
|
+
cairo_pattern_destroy(repeat);
|
|
386
|
+
cairo_surface_destroy(surface);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// The source is drawn in a bitmap of the filter region's pixels, which can reach past the view's,
|
|
390
|
+
// so an offset brings in what is drawn outside the view; the filter's result is drawn back in its
|
|
391
|
+
// place. With no bitmap (more pixels than cairo holds) the source is drawn as it is, unfiltered,
|
|
392
|
+
// as on macOS.
|
|
393
|
+
void beginFilter(const SvgFilter &filter) override {
|
|
394
|
+
auto [x, y, width, height] = regionPixels(filter);
|
|
395
|
+
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
|
|
396
|
+
if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
|
|
397
|
+
cairo_surface_destroy(surface);
|
|
398
|
+
filters_.push_back({.parent = cr_, .cr = nullptr, .surface = nullptr, .saved = saved_, .filter = filter});
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
cairo_surface_set_device_scale(surface, scale_, scale_);
|
|
402
|
+
cairo_surface_set_device_offset(surface, -x, -y);
|
|
403
|
+
cairo_t *cr = drawingInto(surface);
|
|
404
|
+
filters_.push_back({.parent = cr_, .cr = cr, .surface = surface, .saved = saved_, .filter = filter, .x = x, .y = y});
|
|
405
|
+
cr_ = cr;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
void endFilter() override {
|
|
409
|
+
if (filters_.empty()) {
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
Filter frame = std::move(filters_.back());
|
|
413
|
+
filters_.pop_back();
|
|
414
|
+
if (!frame.surface) {
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
cr_ = frame.parent;
|
|
418
|
+
saved_ = frame.saved;
|
|
419
|
+
cairo_destroy(frame.cr);
|
|
420
|
+
cairo_surface_flush(frame.surface);
|
|
421
|
+
int width = cairo_image_surface_get_width(frame.surface), height = cairo_image_surface_get_height(frame.surface);
|
|
422
|
+
int stride = cairo_image_surface_get_stride(frame.surface);
|
|
423
|
+
unsigned char *data = cairo_image_surface_get_data(frame.surface);
|
|
424
|
+
// cairo's pixels are premultiplied ARGB words; the filter's are premultiplied RGBA bytes.
|
|
425
|
+
SvgPixels source{.width = width, .height = height};
|
|
426
|
+
source.rgba.resize(static_cast<size_t>(width) * height * 4);
|
|
427
|
+
for (int y = 0; y < height; ++y) {
|
|
428
|
+
const auto *row = reinterpret_cast<const uint32_t *>(data + static_cast<size_t>(y) * stride);
|
|
429
|
+
for (int x = 0; x < width; ++x) {
|
|
430
|
+
uint8_t *p = &source.rgba[(static_cast<size_t>(y) * width + x) * 4];
|
|
431
|
+
p[0] = (row[x] >> 16) & 0xff;
|
|
432
|
+
p[1] = (row[x] >> 8) & 0xff;
|
|
433
|
+
p[2] = row[x] & 0xff;
|
|
434
|
+
p[3] = row[x] >> 24;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
auto result = runSvgFilter(frame.filter, source, SvgMatrix::translate(-frame.x, -frame.y) * SvgMatrix::scale(scale_, scale_) * matrix_);
|
|
438
|
+
for (int y = 0; y < height && y < result.height; ++y) {
|
|
439
|
+
auto *row = reinterpret_cast<uint32_t *>(data + static_cast<size_t>(y) * stride);
|
|
440
|
+
for (int x = 0; x < width && x < result.width; ++x) {
|
|
441
|
+
const uint8_t *p = &result.rgba[(static_cast<size_t>(y) * result.width + x) * 4];
|
|
442
|
+
row[x] = (uint32_t(p[3]) << 24) | (uint32_t(p[0]) << 16) | (uint32_t(p[1]) << 8) | p[2];
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
cairo_surface_mark_dirty(frame.surface);
|
|
446
|
+
cairo_save(cr_);
|
|
447
|
+
cairo_set_matrix(cr_, &base_);
|
|
448
|
+
cairo_set_source_surface(cr_, frame.surface, 0, 0);
|
|
449
|
+
cairo_paint(cr_);
|
|
450
|
+
cairo_restore(cr_);
|
|
451
|
+
cairo_surface_destroy(frame.surface);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
void image(const std::string &uri, const SvgRect &rect, const std::string &align, int meetOrSlice, double opacity) override {
|
|
455
|
+
auto &images = svgImages();
|
|
456
|
+
if (hold_) {
|
|
457
|
+
hold_->use(uri);
|
|
458
|
+
}
|
|
459
|
+
auto found = images.loaded.find(uri);
|
|
460
|
+
if (found == images.loaded.end()) {
|
|
461
|
+
if (!images.failed.contains(uri)) {
|
|
462
|
+
load(uri);
|
|
463
|
+
}
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
cairo_surface_t *surface = found->second;
|
|
467
|
+
double width = cairo_image_surface_get_width(surface), height = cairo_image_surface_get_height(surface);
|
|
468
|
+
if (width <= 0 || height <= 0) {
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
auto fit = svgViewBoxTransform({0, 0, width, height}, align, meetOrSlice, rect.width, rect.height);
|
|
472
|
+
cairo_save(cr_);
|
|
473
|
+
cairo_rectangle(cr_, rect.x, rect.y, rect.width, rect.height);
|
|
474
|
+
cairo_clip(cr_);
|
|
475
|
+
cairo_translate(cr_, rect.x, rect.y);
|
|
476
|
+
cairo_matrix_t transform = cairoMatrix(fit);
|
|
477
|
+
cairo_transform(cr_, &transform);
|
|
478
|
+
cairo_set_source_surface(cr_, surface, 0, 0);
|
|
479
|
+
cairo_paint_with_alpha(cr_, opacity);
|
|
480
|
+
cairo_restore(cr_);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
double measureText(const std::string &text, double fontSize, const std::string &fontFamily, const std::string &fontWeight) override {
|
|
484
|
+
PangoLayout *layout = lineOf(text, fontSize, fontFamily, fontWeight);
|
|
485
|
+
PangoRectangle logical;
|
|
486
|
+
pango_layout_get_extents(layout, nullptr, &logical);
|
|
487
|
+
g_object_unref(layout);
|
|
488
|
+
return static_cast<double>(logical.width) / PANGO_SCALE;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// Pango's clusters of `text` on one shaped line, each with its run's font and its own glyphs.
|
|
492
|
+
std::vector<SvgGlyphCluster> glyphClusters(
|
|
493
|
+
const std::string &text, double fontSize, const std::string &fontFamily, const std::string &fontWeight) override {
|
|
494
|
+
std::vector<SvgGlyphCluster> out;
|
|
495
|
+
std::vector<int> starts;
|
|
496
|
+
PangoLayout *layout = lineOf(text, fontSize, fontFamily, fontWeight);
|
|
497
|
+
PangoLayoutLine *line = pango_layout_get_line_readonly(layout, 0);
|
|
498
|
+
double runX = 0;
|
|
499
|
+
for (GSList *item = line ? line->runs : nullptr; item; item = item->next) {
|
|
500
|
+
auto *run = static_cast<PangoGlyphItem *>(item->data);
|
|
501
|
+
// Where each glyph starts in the run.
|
|
502
|
+
std::vector<int> glyphX(static_cast<size_t>(run->glyphs->num_glyphs) + 1, 0);
|
|
503
|
+
for (int g = 0; g < run->glyphs->num_glyphs; ++g) {
|
|
504
|
+
glyphX[g + 1] = glyphX[g] + run->glyphs->glyphs[g].geometry.width;
|
|
505
|
+
}
|
|
506
|
+
PangoGlyphItemIter iter;
|
|
507
|
+
for (bool more = pango_glyph_item_iter_init_start(&iter, run, pango_layout_get_text(layout)); more; more = pango_glyph_item_iter_next_cluster(&iter)) {
|
|
508
|
+
auto shaped = std::make_shared<ShapedGlyphs>();
|
|
509
|
+
shaped->font = PANGO_FONT(g_object_ref(run->item->analysis.font));
|
|
510
|
+
int first = std::min(iter.start_glyph, iter.end_glyph), last = std::max(iter.start_glyph, iter.end_glyph);
|
|
511
|
+
if (iter.start_glyph > iter.end_glyph) {
|
|
512
|
+
// A right-to-left run steps from its last glyph back: the cluster is the ones after end_glyph.
|
|
513
|
+
first = iter.end_glyph + 1;
|
|
514
|
+
last = iter.start_glyph + 1;
|
|
515
|
+
}
|
|
516
|
+
shaped->glyphs = pango_glyph_string_new();
|
|
517
|
+
pango_glyph_string_set_size(shaped->glyphs, last - first);
|
|
518
|
+
for (int g = first; g < last; ++g) {
|
|
519
|
+
shaped->glyphs->glyphs[g - first] = run->glyphs->glyphs[g];
|
|
520
|
+
shaped->glyphs->log_clusters[g - first] = 0;
|
|
521
|
+
}
|
|
522
|
+
int start = glyphX[first], advance = glyphX[last] - glyphX[first];
|
|
523
|
+
starts.push_back(iter.start_index);
|
|
524
|
+
out.push_back({
|
|
525
|
+
.text = text.substr(static_cast<size_t>(iter.start_index), static_cast<size_t>(iter.end_index - iter.start_index)),
|
|
526
|
+
.x = runX + static_cast<double>(start) / PANGO_SCALE,
|
|
527
|
+
.advance = static_cast<double>(advance) / PANGO_SCALE,
|
|
528
|
+
.glyphs = shaped,
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
runX += static_cast<double>(pango_glyph_string_get_width(run->glyphs)) / PANGO_SCALE;
|
|
532
|
+
}
|
|
533
|
+
g_object_unref(layout);
|
|
534
|
+
// In the order of the text: runs come in visual order.
|
|
535
|
+
std::vector<size_t> order(out.size());
|
|
536
|
+
for (size_t i = 0; i < order.size(); ++i) {
|
|
537
|
+
order[i] = i;
|
|
538
|
+
}
|
|
539
|
+
std::stable_sort(order.begin(), order.end(), [&starts](size_t a, size_t b) { return starts[a] < starts[b]; });
|
|
540
|
+
std::vector<SvgGlyphCluster> sorted;
|
|
541
|
+
for (size_t i : order) {
|
|
542
|
+
sorted.push_back(std::move(out[i]));
|
|
543
|
+
}
|
|
544
|
+
return sorted;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
void glyphCluster(const SvgGlyphCluster &cluster, double x, double y, const SvgPaint &fill) override {
|
|
548
|
+
auto shaped = std::static_pointer_cast<const ShapedGlyphs>(cluster.glyphs);
|
|
549
|
+
if (!shaped || !shaped->glyphs) {
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
cairo_save(cr_);
|
|
553
|
+
setColor(cr_, solidOf(fill), fill.opacity);
|
|
554
|
+
cairo_move_to(cr_, x, y);
|
|
555
|
+
pango_cairo_show_glyph_string(cr_, shaped->font, shaped->glyphs);
|
|
556
|
+
cairo_restore(cr_);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
void text(
|
|
560
|
+
const std::string &text,
|
|
561
|
+
double x,
|
|
562
|
+
double y,
|
|
563
|
+
double fontSize,
|
|
564
|
+
const std::string &fontFamily,
|
|
565
|
+
const std::string &fontWeight,
|
|
566
|
+
const SvgPaint &fill) override {
|
|
567
|
+
PangoLayout *layout = lineOf(text, fontSize, fontFamily, fontWeight);
|
|
568
|
+
double baseline = static_cast<double>(pango_layout_get_baseline(layout)) / PANGO_SCALE;
|
|
569
|
+
cairo_save(cr_);
|
|
570
|
+
setColor(cr_, solidOf(fill), fill.opacity);
|
|
571
|
+
cairo_move_to(cr_, x, y - baseline);
|
|
572
|
+
pango_cairo_show_layout(cr_, layout);
|
|
573
|
+
cairo_restore(cr_);
|
|
574
|
+
g_object_unref(layout);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
void views(Tag) override {}
|
|
578
|
+
|
|
579
|
+
private:
|
|
580
|
+
struct Saved {
|
|
581
|
+
SvgMatrix matrix;
|
|
582
|
+
std::optional<std::pair<SvgPath, SvgStroke>> strokeClip;
|
|
583
|
+
};
|
|
584
|
+
struct Mask {
|
|
585
|
+
cairo_t *parent;
|
|
586
|
+
cairo_t *mask;
|
|
587
|
+
cairo_surface_t *surface;
|
|
588
|
+
std::vector<Saved> saved;
|
|
589
|
+
bool alpha{false};
|
|
590
|
+
};
|
|
591
|
+
struct Filter {
|
|
592
|
+
cairo_t *parent;
|
|
593
|
+
cairo_t *cr;
|
|
594
|
+
cairo_surface_t *surface;
|
|
595
|
+
std::vector<Saved> saved;
|
|
596
|
+
SvgFilter filter;
|
|
597
|
+
// Where the bitmap's corner is in the view's pixels.
|
|
598
|
+
int x{0}, y{0};
|
|
599
|
+
};
|
|
600
|
+
|
|
601
|
+
// The filter region's bounds in the view's pixels: x, y, width and height. The filter keeps a
|
|
602
|
+
// float image of the region for each step and its two sources, so a region past the view is cut to
|
|
603
|
+
// the view's when those would take more than 64 MiB, or when it is wider or taller than the view
|
|
604
|
+
// and than 2048 pixels.
|
|
605
|
+
std::array<int, 4> regionPixels(const SvgFilter &filter) const {
|
|
606
|
+
const SvgRect ®ion = filter.region;
|
|
607
|
+
SvgMatrix toPixels = SvgMatrix::scale(scale_, scale_) * matrix_;
|
|
608
|
+
double minX = INFINITY, minY = INFINITY, maxX = -INFINITY, maxY = -INFINITY;
|
|
609
|
+
for (auto [rx, ry] : {std::pair{region.x, region.y}, {region.x + region.width, region.y}, {region.x, region.y + region.height},
|
|
610
|
+
{region.x + region.width, region.y + region.height}}) {
|
|
611
|
+
auto [px, py] = toPixels.apply(rx, ry);
|
|
612
|
+
minX = std::min(minX, px);
|
|
613
|
+
minY = std::min(minY, py);
|
|
614
|
+
maxX = std::max(maxX, px);
|
|
615
|
+
maxY = std::max(maxY, py);
|
|
616
|
+
}
|
|
617
|
+
double viewWidth = std::ceil(width_ * scale_), viewHeight = std::ceil(height_ * scale_);
|
|
618
|
+
minX = std::floor(minX), minY = std::floor(minY), maxX = std::ceil(maxX), maxY = std::ceil(maxY);
|
|
619
|
+
double images = static_cast<double>(filter.steps.size()) + 3;
|
|
620
|
+
double pixels = (64.0 * 1024 * 1024) / (images * 4 * sizeof(float));
|
|
621
|
+
bool fits = (maxX - minX) * (maxY - minY) <= std::max(viewWidth * viewHeight, pixels) &&
|
|
622
|
+
maxX - minX <= std::max(viewWidth, 2048.0) && maxY - minY <= std::max(viewHeight, 2048.0) &&
|
|
623
|
+
std::abs(minX) < 1e7 && std::abs(minY) < 1e7;
|
|
624
|
+
if (!fits) {
|
|
625
|
+
minX = std::max(minX, 0.0), minY = std::max(minY, 0.0), maxX = std::min(maxX, viewWidth), maxY = std::min(maxY, viewHeight);
|
|
626
|
+
}
|
|
627
|
+
if (!(maxX > minX && maxY > minY)) {
|
|
628
|
+
return {0, 0, 1, 1};
|
|
629
|
+
}
|
|
630
|
+
return {static_cast<int>(minX), static_cast<int>(minY), static_cast<int>(maxX - minX), static_cast<int>(maxY - minY)};
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
// A transparent bitmap of the view's pixels, in the view's points.
|
|
634
|
+
cairo_surface_t *viewSurface() const {
|
|
635
|
+
int width = std::max(1, static_cast<int>(std::ceil(width_ * scale_)));
|
|
636
|
+
int height = std::max(1, static_cast<int>(std::ceil(height_ * scale_)));
|
|
637
|
+
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
|
|
638
|
+
cairo_surface_set_device_scale(surface, scale_, scale_);
|
|
639
|
+
return surface;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
// Drawing on `surface` where the drawing is now: the view's points, then the user transform.
|
|
643
|
+
cairo_t *drawingInto(cairo_surface_t *surface) const {
|
|
644
|
+
cairo_t *cr = cairo_create(surface);
|
|
645
|
+
cairo_matrix_t transform = cairoMatrix(matrix_);
|
|
646
|
+
cairo_transform(cr, &transform);
|
|
647
|
+
return cr;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
// Each pixel's alpha becomes its luminance times its alpha (the premultiplied colour's luminance).
|
|
651
|
+
static void toLuminance(cairo_surface_t *surface) {
|
|
652
|
+
cairo_surface_flush(surface);
|
|
653
|
+
int width = cairo_image_surface_get_width(surface), height = cairo_image_surface_get_height(surface);
|
|
654
|
+
int stride = cairo_image_surface_get_stride(surface);
|
|
655
|
+
unsigned char *data = cairo_image_surface_get_data(surface);
|
|
656
|
+
for (int y = 0; y < height; ++y) {
|
|
657
|
+
auto *row = reinterpret_cast<uint32_t *>(data + static_cast<size_t>(y) * stride);
|
|
658
|
+
for (int x = 0; x < width; ++x) {
|
|
659
|
+
double r = (row[x] >> 16) & 0xff, g = (row[x] >> 8) & 0xff, b = row[x] & 0xff;
|
|
660
|
+
auto luminance = static_cast<uint32_t>(std::lround(std::clamp(0.2125 * r + 0.7154 * g + 0.0721 * b, 0.0, 255.0)));
|
|
661
|
+
row[x] = luminance << 24;
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
cairo_surface_mark_dirty(surface);
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
// Every canvas that asks for a uri while it loads is told when it has loaded or failed to.
|
|
668
|
+
void load(const std::string &uri) {
|
|
669
|
+
auto &images = svgImages();
|
|
670
|
+
bool started = images.waiting.contains(uri);
|
|
671
|
+
auto &waiters = images.waiting[uri];
|
|
672
|
+
// Once for each svg view, however often it draws the uri while it loads.
|
|
673
|
+
bool waiting = hold_ && std::ranges::any_of(waiters, [this](const auto &waiter) { return waiter.first == hold_; });
|
|
674
|
+
if (imageLoaded_ && !waiting) {
|
|
675
|
+
waiters.emplace_back(hold_, imageLoaded_);
|
|
676
|
+
}
|
|
677
|
+
if (started) {
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
facebook::react::ImageSource source;
|
|
681
|
+
source.uri = uri;
|
|
682
|
+
readImage(source, [uri](GdkTexture *texture, const std::string &) {
|
|
683
|
+
auto &images = svgImages();
|
|
684
|
+
auto waiters = std::move(images.waiting[uri]);
|
|
685
|
+
images.waiting.erase(uri);
|
|
686
|
+
cairo_surface_t *surface = texture ? surfaceOf(texture) : nullptr;
|
|
687
|
+
if (texture) {
|
|
688
|
+
g_object_unref(texture);
|
|
689
|
+
}
|
|
690
|
+
if (!surface) {
|
|
691
|
+
images.failed.insert(uri);
|
|
692
|
+
} else {
|
|
693
|
+
trimSvgImages(63);
|
|
694
|
+
images.loaded[uri] = surface;
|
|
695
|
+
images.order.emplace_back(uri, surface);
|
|
696
|
+
}
|
|
697
|
+
for (auto &waiter : waiters) {
|
|
698
|
+
waiter.second();
|
|
699
|
+
}
|
|
700
|
+
});
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
cairo_t *cr_;
|
|
704
|
+
double width_, height_, scale_;
|
|
705
|
+
std::function<void()> imageLoaded_;
|
|
706
|
+
SvgImageHold *hold_;
|
|
707
|
+
cairo_matrix_t base_;
|
|
708
|
+
SvgMatrix matrix_;
|
|
709
|
+
std::optional<std::pair<SvgPath, SvgStroke>> strokeClip_;
|
|
710
|
+
std::vector<Saved> saved_;
|
|
711
|
+
std::vector<double> layers_;
|
|
712
|
+
std::vector<Mask> masks_;
|
|
713
|
+
std::vector<Filter> filters_;
|
|
714
|
+
};
|
|
715
|
+
|
|
716
|
+
} // namespace
|
|
717
|
+
|
|
718
|
+
SvgImageHold::~SvgImageHold() {
|
|
719
|
+
// Its view waits for nothing more, and another hold made at this address waits for itself.
|
|
720
|
+
for (auto &[uri, waiters] : svgImages().waiting) {
|
|
721
|
+
std::erase_if(waiters, [this](const auto &waiter) { return waiter.first == this; });
|
|
722
|
+
}
|
|
723
|
+
next_.clear();
|
|
724
|
+
drawn();
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
void SvgImageHold::use(const std::string &uri) {
|
|
728
|
+
next_.insert(uri);
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
void SvgImageHold::drawn() {
|
|
732
|
+
auto &holders = svgImages().holders;
|
|
733
|
+
for (const auto &uri : next_) {
|
|
734
|
+
++holders[uri];
|
|
735
|
+
}
|
|
736
|
+
for (const auto &uri : held_) {
|
|
737
|
+
if (auto found = holders.find(uri); found != holders.end() && --found->second <= 0) {
|
|
738
|
+
holders.erase(found);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
held_ = std::move(next_);
|
|
742
|
+
next_.clear();
|
|
743
|
+
trimSvgImages(64);
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
size_t svgImagesKept() {
|
|
747
|
+
return svgImages().loaded.size();
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
std::unique_ptr<SvgCanvas> makeCairoCanvas(
|
|
751
|
+
cairo_t *cr, double width, double height, double scale, std::function<void()> imageLoaded, SvgImageHold *hold) {
|
|
752
|
+
return std::make_unique<CairoCanvas>(cr, width, height, scale, std::move(imageLoaded), hold);
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
} // namespace ngn::gtk
|