@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,274 @@
|
|
|
1
|
+
#include "pango-text.h"
|
|
2
|
+
|
|
3
|
+
#include "fonts.h"
|
|
4
|
+
|
|
5
|
+
#include "ngn/surface-metrics.h"
|
|
6
|
+
|
|
7
|
+
#include <gtk/gtk.h>
|
|
8
|
+
#include <pango/pangofc-fontmap.h>
|
|
9
|
+
|
|
10
|
+
#include <react/renderer/graphics/Color.h>
|
|
11
|
+
|
|
12
|
+
#include <algorithm>
|
|
13
|
+
#include <cmath>
|
|
14
|
+
#include <optional>
|
|
15
|
+
#include <string>
|
|
16
|
+
|
|
17
|
+
using namespace facebook::react;
|
|
18
|
+
|
|
19
|
+
namespace ngn::gtk {
|
|
20
|
+
|
|
21
|
+
namespace {
|
|
22
|
+
|
|
23
|
+
float scaleOf(const TextAttributes &a) {
|
|
24
|
+
float multiplier = 1;
|
|
25
|
+
if (a.allowFontScaling.value_or(true) && !std::isnan(a.fontSizeMultiplier)) {
|
|
26
|
+
multiplier = a.fontSizeMultiplier;
|
|
27
|
+
if (!std::isnan(a.maxFontSizeMultiplier) && a.maxFontSizeMultiplier >= 1) {
|
|
28
|
+
multiplier = std::min<float>(multiplier, a.maxFontSizeMultiplier);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return multiplier;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// One context per thread, since a Pango cairo font map is per thread.
|
|
35
|
+
PangoContext *threadContext() {
|
|
36
|
+
thread_local PangoContext *context = [] {
|
|
37
|
+
PangoContext *c = pango_font_map_create_context(pango_cairo_font_map_get_default());
|
|
38
|
+
cairo_font_options_t *options = cairo_font_options_create();
|
|
39
|
+
cairo_font_options_set_hint_metrics(options, CAIRO_HINT_METRICS_OFF);
|
|
40
|
+
cairo_font_options_set_hint_style(options, CAIRO_HINT_STYLE_NONE);
|
|
41
|
+
pango_cairo_context_set_font_options(c, options);
|
|
42
|
+
cairo_font_options_destroy(options);
|
|
43
|
+
pango_context_set_round_glyph_positions(c, FALSE);
|
|
44
|
+
// Neutral, so each paragraph takes its direction from its own text (auto_dir), and Pango never
|
|
45
|
+
// swaps a left or right alignment for a paragraph that runs the other way from the context's:
|
|
46
|
+
// layoutText aligns by the layout's direction itself.
|
|
47
|
+
pango_context_set_base_dir(c, PANGO_DIRECTION_NEUTRAL);
|
|
48
|
+
return c;
|
|
49
|
+
}();
|
|
50
|
+
return context;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// The context a justified paragraph is laid out in: threadContext's, with a left-to-right base. A
|
|
54
|
+
// justified paragraph's lines fill the width but its last, which follows the paragraph's own
|
|
55
|
+
// direction, as AppKit's does (text-layout.mm): aligned left, Pango swaps it to the right for a
|
|
56
|
+
// paragraph that runs right to left.
|
|
57
|
+
PangoContext *justifiedContext() {
|
|
58
|
+
thread_local PangoContext *context = [] {
|
|
59
|
+
PangoContext *c = pango_font_map_create_context(pango_cairo_font_map_get_default());
|
|
60
|
+
pango_cairo_context_set_font_options(c, pango_cairo_context_get_font_options(threadContext()));
|
|
61
|
+
pango_context_set_round_glyph_positions(c, FALSE);
|
|
62
|
+
pango_context_set_base_dir(c, PANGO_DIRECTION_LTR);
|
|
63
|
+
return c;
|
|
64
|
+
}();
|
|
65
|
+
return context;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
void addAttribute(PangoAttrList *list, PangoAttribute *attribute, guint start, guint end) {
|
|
69
|
+
attribute->start_index = start;
|
|
70
|
+
attribute->end_index = end;
|
|
71
|
+
pango_attr_list_insert(list, attribute);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
guint16 channel(float value) {
|
|
75
|
+
return static_cast<guint16>(std::lround(std::clamp(value, 0.0f, 1.0f) * 65535));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
} // namespace
|
|
79
|
+
|
|
80
|
+
TextLayout::TextLayout(TextLayout &&other) noexcept : layout(other.layout), top(other.top), size(other.size) {
|
|
81
|
+
other.layout = nullptr;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
TextLayout &TextLayout::operator=(TextLayout &&other) noexcept {
|
|
85
|
+
if (this != &other) {
|
|
86
|
+
if (layout) {
|
|
87
|
+
g_object_unref(layout);
|
|
88
|
+
}
|
|
89
|
+
layout = other.layout;
|
|
90
|
+
top = other.top;
|
|
91
|
+
size = other.size;
|
|
92
|
+
other.layout = nullptr;
|
|
93
|
+
}
|
|
94
|
+
return *this;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
TextLayout::~TextLayout() {
|
|
98
|
+
if (layout) {
|
|
99
|
+
g_object_unref(layout);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
bool pangoHasLineHeight() {
|
|
104
|
+
#if PANGO_VERSION_CHECK(1, 50, 0)
|
|
105
|
+
return pango_version() >= PANGO_VERSION_ENCODE(1, 50, 0);
|
|
106
|
+
#else
|
|
107
|
+
return false;
|
|
108
|
+
#endif
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// The calling thread's font map looks again at fontconfig once fonts were added since it last did:
|
|
112
|
+
// a Pango cairo font map is per thread, and caches what fontconfig had.
|
|
113
|
+
void refreshFontMap() {
|
|
114
|
+
thread_local uint64_t seen = 0;
|
|
115
|
+
uint64_t added = fontsAdded();
|
|
116
|
+
if (added == seen) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
seen = added;
|
|
120
|
+
PangoFontMap *map = pango_cairo_font_map_get_default();
|
|
121
|
+
if (PANGO_IS_FC_FONT_MAP(map)) {
|
|
122
|
+
pango_fc_font_map_config_changed(PANGO_FC_FONT_MAP(map));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
PangoFontDescription *fontOf(const TextAttributes &a) {
|
|
127
|
+
refreshFontMap();
|
|
128
|
+
PangoFontDescription *font = pango_font_description_new();
|
|
129
|
+
const std::string &family = a.fontFamily;
|
|
130
|
+
if (family.empty() || family == "System" || family == "system-ui" || family == "sans-serif") {
|
|
131
|
+
pango_font_description_set_family(font, "sans-serif");
|
|
132
|
+
} else if (family == "monospace" || family == "Menlo" || family == "ui-monospace") {
|
|
133
|
+
pango_font_description_set_family(font, "monospace");
|
|
134
|
+
} else {
|
|
135
|
+
// A name a font was loaded under stands for that font's family, as on macOS.
|
|
136
|
+
auto library = fontLibrary();
|
|
137
|
+
auto loaded = library ? library->fontOf(family) : std::nullopt;
|
|
138
|
+
pango_font_description_set_family(font, (loaded ? loaded->family : family).c_str());
|
|
139
|
+
}
|
|
140
|
+
double size = (std::isnan(a.fontSize) ? 14 : a.fontSize) * scaleOf(a);
|
|
141
|
+
// Absolute: a React Native font size is in points, which are the surface's logical pixels.
|
|
142
|
+
pango_font_description_set_absolute_size(font, size * PANGO_SCALE);
|
|
143
|
+
int weight = static_cast<int>(a.fontWeight.value_or(FontWeight::Regular));
|
|
144
|
+
pango_font_description_set_weight(font, static_cast<PangoWeight>(weight <= 0 ? 400 : weight));
|
|
145
|
+
if (a.fontStyle.has_value() && *a.fontStyle != FontStyle::Normal) {
|
|
146
|
+
pango_font_description_set_style(font, PANGO_STYLE_ITALIC);
|
|
147
|
+
}
|
|
148
|
+
return font;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
TextLayout layoutText(const AttributedString &text, const ParagraphAttributes ¶graph, Float maxWidth, LineHeights lineHeights) {
|
|
152
|
+
bool pangoLineHeight = lineHeights == LineHeights::Auto && pangoHasLineHeight();
|
|
153
|
+
TextLayout result;
|
|
154
|
+
// The paragraph's alignment, its first run's that names one, as the runs are read below.
|
|
155
|
+
std::optional<TextAlignment> named;
|
|
156
|
+
for (const auto &fragment : text.getFragments()) {
|
|
157
|
+
if (!fragment.isAttachment() && fragment.textAttributes.alignment) {
|
|
158
|
+
named = fragment.textAttributes.alignment;
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
bool justified = named == TextAlignment::Justified;
|
|
163
|
+
result.layout = pango_layout_new(justified ? justifiedContext() : threadContext());
|
|
164
|
+
PangoLayout *layout = result.layout;
|
|
165
|
+
std::string utf8;
|
|
166
|
+
PangoAttrList *attrs = pango_attr_list_new();
|
|
167
|
+
std::optional<TextAlignment> alignment;
|
|
168
|
+
std::optional<LayoutDirection> layoutDirection;
|
|
169
|
+
// The paragraph's line height, the tallest any run asks for.
|
|
170
|
+
double lineHeight = 0;
|
|
171
|
+
for (const auto &fragment : text.getFragments()) {
|
|
172
|
+
if (fragment.isAttachment()) {
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const auto &a = fragment.textAttributes;
|
|
176
|
+
guint start = static_cast<guint>(utf8.size());
|
|
177
|
+
utf8 += fragment.string;
|
|
178
|
+
guint end = static_cast<guint>(utf8.size());
|
|
179
|
+
PangoFontDescription *font = fontOf(a);
|
|
180
|
+
addAttribute(attrs, pango_attr_font_desc_new(font), start, end);
|
|
181
|
+
pango_font_description_free(font);
|
|
182
|
+
if (a.foregroundColor) {
|
|
183
|
+
auto c = colorComponentsFromColor(a.foregroundColor);
|
|
184
|
+
addAttribute(attrs, pango_attr_foreground_new(channel(c.red), channel(c.green), channel(c.blue)), start, end);
|
|
185
|
+
addAttribute(attrs, pango_attr_foreground_alpha_new(channel(c.alpha)), start, end);
|
|
186
|
+
}
|
|
187
|
+
if (!std::isnan(a.letterSpacing)) {
|
|
188
|
+
addAttribute(attrs, pango_attr_letter_spacing_new(static_cast<int>(a.letterSpacing * PANGO_SCALE)), start, end);
|
|
189
|
+
}
|
|
190
|
+
if (!std::isnan(a.lineHeight) && a.lineHeight > 0) {
|
|
191
|
+
double height = a.lineHeight * scaleOf(a);
|
|
192
|
+
lineHeight = std::max(lineHeight, height);
|
|
193
|
+
#if PANGO_VERSION_CHECK(1, 50, 0)
|
|
194
|
+
if (pangoLineHeight) {
|
|
195
|
+
addAttribute(attrs, pango_attr_line_height_new_absolute(static_cast<int>(std::lround(height * PANGO_SCALE))), start, end);
|
|
196
|
+
}
|
|
197
|
+
#endif
|
|
198
|
+
}
|
|
199
|
+
if (!alignment && a.alignment) {
|
|
200
|
+
alignment = a.alignment;
|
|
201
|
+
}
|
|
202
|
+
if (!layoutDirection && a.layoutDirection) {
|
|
203
|
+
layoutDirection = a.layoutDirection;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
pango_layout_set_text(layout, utf8.c_str(), static_cast<int>(utf8.size()));
|
|
207
|
+
pango_layout_set_attributes(layout, attrs);
|
|
208
|
+
pango_attr_list_unref(attrs);
|
|
209
|
+
if (std::isfinite(maxWidth)) {
|
|
210
|
+
pango_layout_set_width(layout, static_cast<int>(maxWidth * PANGO_SCALE));
|
|
211
|
+
pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
|
|
212
|
+
}
|
|
213
|
+
// Aligned by the layout's direction, whatever the script's, as AppKit's ctAlignment and iOS do:
|
|
214
|
+
// natural and left are the layout's start, right and end its end. The context's neutral base
|
|
215
|
+
// (threadContext) keeps Pango from swapping them for a right-to-left paragraph.
|
|
216
|
+
bool rightToLeft = layoutDirection == LayoutDirection::RightToLeft;
|
|
217
|
+
PangoAlignment start = rightToLeft ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
|
|
218
|
+
PangoAlignment end = rightToLeft ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
|
|
219
|
+
switch (alignment.value_or(TextAlignment::Natural)) {
|
|
220
|
+
case TextAlignment::Center: pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER); break;
|
|
221
|
+
case TextAlignment::Right:
|
|
222
|
+
case TextAlignment::End: pango_layout_set_alignment(layout, end); break;
|
|
223
|
+
case TextAlignment::Justified:
|
|
224
|
+
// Left, which justifiedContext's base swaps for a right-to-left paragraph's last line.
|
|
225
|
+
pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
|
|
226
|
+
pango_layout_set_justify(layout, TRUE);
|
|
227
|
+
break;
|
|
228
|
+
default: pango_layout_set_alignment(layout, start); break;
|
|
229
|
+
}
|
|
230
|
+
if (paragraph.maximumNumberOfLines > 0) {
|
|
231
|
+
pango_layout_set_height(layout, -paragraph.maximumNumberOfLines);
|
|
232
|
+
pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
PangoRectangle logical;
|
|
236
|
+
pango_layout_get_extents(layout, nullptr, &logical);
|
|
237
|
+
result.size = {static_cast<Float>(logical.width) / PANGO_SCALE, static_cast<Float>(logical.height) / PANGO_SCALE};
|
|
238
|
+
if (lineHeight > 0 && !pangoLineHeight) {
|
|
239
|
+
// Each line as tall as the paragraph's line height: the space between lines makes up the
|
|
240
|
+
// difference from the lines' own height, and half of it goes above the first.
|
|
241
|
+
int lines = std::max(1, pango_layout_get_line_count(layout));
|
|
242
|
+
double natural = static_cast<double>(logical.height) / PANGO_SCALE / lines;
|
|
243
|
+
double extra = lineHeight - natural;
|
|
244
|
+
pango_layout_set_spacing(layout, static_cast<int>(std::lround(extra * PANGO_SCALE)));
|
|
245
|
+
result.top = extra / 2;
|
|
246
|
+
result.size.height = static_cast<Float>(lineHeight * lines);
|
|
247
|
+
}
|
|
248
|
+
return result;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
Size PangoTextMeasurer::measure(const AttributedString &text, const ParagraphAttributes ¶graph, Float maxWidth) const {
|
|
252
|
+
return layoutText(text, paragraph, maxWidth, lineHeights_).size;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
float desktopFontScale() {
|
|
256
|
+
int dpi = -1;
|
|
257
|
+
if (GtkSettings *settings = gtk_settings_get_default()) {
|
|
258
|
+
g_object_get(settings, "gtk-xft-dpi", &dpi, nullptr);
|
|
259
|
+
}
|
|
260
|
+
return xftFontScale(dpi);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
void watchFontScale(std::function<void()> changed) {
|
|
264
|
+
if (GtkSettings *settings = gtk_settings_get_default()) {
|
|
265
|
+
g_signal_connect_data(
|
|
266
|
+
settings, "notify::gtk-xft-dpi", G_CALLBACK(+[](GObject *, GParamSpec *, gpointer data) {
|
|
267
|
+
(*static_cast<std::function<void()> *>(data))();
|
|
268
|
+
}),
|
|
269
|
+
new std::function<void()>(std::move(changed)),
|
|
270
|
+
[](gpointer data, GClosure *) { delete static_cast<std::function<void()> *>(data); }, GConnectFlags(0));
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
} // namespace ngn::gtk
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// Text through Pango, shared by measurement (PangoTextMeasurer, on the JS thread) and drawing (a
|
|
2
|
+
// Paragraph's view, on the GTK thread), so both break the lines the same way.
|
|
3
|
+
//
|
|
4
|
+
// `lineHeight` is Pango's own attribute from Pango 1.50. Below it (el9 ships 1.48) the layout
|
|
5
|
+
// spaces its lines itself: each line takes the paragraph's line height, its extra split above and
|
|
6
|
+
// below, as CSS and Pango 1.50 both place it.
|
|
7
|
+
#pragma once
|
|
8
|
+
|
|
9
|
+
#include <functional>
|
|
10
|
+
|
|
11
|
+
#include "ngn/text-measurer.h"
|
|
12
|
+
|
|
13
|
+
#include <pango/pangocairo.h>
|
|
14
|
+
|
|
15
|
+
namespace ngn::gtk {
|
|
16
|
+
|
|
17
|
+
enum class LineHeights {
|
|
18
|
+
// Pango's attribute when the Pango running has it, our own spacing when not.
|
|
19
|
+
Auto,
|
|
20
|
+
// Our own spacing whatever Pango runs: what Pango below 1.50 gets, and what tests compare.
|
|
21
|
+
Own,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
struct TextLayout {
|
|
25
|
+
// Owned: freed with the TextLayout.
|
|
26
|
+
PangoLayout *layout{nullptr};
|
|
27
|
+
// Where the layout's top goes below the content box's: half of each line's extra height when
|
|
28
|
+
// the layout spaces its own lines.
|
|
29
|
+
double top{0};
|
|
30
|
+
facebook::react::Size size{};
|
|
31
|
+
|
|
32
|
+
TextLayout() = default;
|
|
33
|
+
TextLayout(const TextLayout &) = delete;
|
|
34
|
+
TextLayout &operator=(const TextLayout &) = delete;
|
|
35
|
+
TextLayout(TextLayout &&other) noexcept;
|
|
36
|
+
TextLayout &operator=(TextLayout &&other) noexcept;
|
|
37
|
+
~TextLayout();
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// The string laid out at most `maxWidth` wide (infinite for no limit), in a context of the calling
|
|
41
|
+
// thread's Pango cairo font map, with metrics unhinted so that a size does not depend on the scale
|
|
42
|
+
// it is drawn at.
|
|
43
|
+
TextLayout layoutText(
|
|
44
|
+
const facebook::react::AttributedString &text,
|
|
45
|
+
const facebook::react::ParagraphAttributes ¶graph,
|
|
46
|
+
facebook::react::Float maxWidth,
|
|
47
|
+
LineHeights lineHeights = LineHeights::Auto);
|
|
48
|
+
|
|
49
|
+
// Whether the Pango running has the line-height attribute.
|
|
50
|
+
bool pangoHasLineHeight();
|
|
51
|
+
|
|
52
|
+
// The text size multiplier the desktop asks for, from GtkSettings' `gtk-xft-dpi` (xftFontScale),
|
|
53
|
+
// and `changed` called each time it changes, for as long as the process runs.
|
|
54
|
+
float desktopFontScale();
|
|
55
|
+
void watchFontScale(std::function<void()> changed);
|
|
56
|
+
|
|
57
|
+
// The font a run of text attributes resolves to. The caller frees it.
|
|
58
|
+
PangoFontDescription *fontOf(const facebook::react::TextAttributes &attributes);
|
|
59
|
+
|
|
60
|
+
class PangoTextMeasurer final : public TextMeasurer {
|
|
61
|
+
public:
|
|
62
|
+
explicit PangoTextMeasurer(LineHeights lineHeights = LineHeights::Auto) : lineHeights_(lineHeights) {}
|
|
63
|
+
|
|
64
|
+
facebook::react::Size measure(
|
|
65
|
+
const facebook::react::AttributedString &text,
|
|
66
|
+
const facebook::react::ParagraphAttributes ¶graph,
|
|
67
|
+
facebook::react::Float maxWidth) const override;
|
|
68
|
+
|
|
69
|
+
private:
|
|
70
|
+
LineHeights lineHeights_;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
} // namespace ngn::gtk
|