diff --git a/.serena/memories/frontend/core.md b/.serena/memories/frontend/core.md index 139859bc98..c9268fe453 100644 --- a/.serena/memories/frontend/core.md +++ b/.serena/memories/frontend/core.md @@ -37,6 +37,7 @@ See `mem:scripts/paren-repair`. UI and packages: - App UI components, SCSS modules, style-system boundaries, accessibility, i18n, and render performance: `mem:frontend/ui-conventions-and-style-system`. - JS/TS packages, shared UI package, text editor, Storybook, and package builds: `mem:frontend/ui-packages-text-editor-workflow`. +- IME composition in the WASM text editor (capture surface rules, candidate window anchoring, Linux Chrome caret-bounds limit, real-IME debugging): `mem:frontend/text-editor-ime`. - PO translation workflow and per-locale conventions: `mem:frontend/translations`. Workspace behavior: diff --git a/.serena/memories/frontend/text-editor-ime.md b/.serena/memories/frontend/text-editor-ime.md new file mode 100644 index 0000000000..816c4ef39b --- /dev/null +++ b/.serena/memories/frontend/text-editor-ime.md @@ -0,0 +1,28 @@ +# WASM Text Editor IME Input + +IME (Japanese/CJK composition) in the v3 WASM text editor, `app.main.ui.workspace.shapes.text.v3-editor`. The WASM editor (`app.render-wasm.text-editor`, main-thread `_text_editor_*` exports) owns text, caret, and the composition preview; the DOM only captures input. + +## Capture surface model + +- Input lands in a hidden contenteditable (`#text-editor-wasm-input`, opacity 0, `pointer-events:none`) inside the editor `foreignObject`, wrapped by a positioned `.ime-anchor` div. Native IMEs anchor their candidate window to the DOM caret inside it. +- Idle: the surface is placed ON the WASM caret (`update-ime-caret!`) after every caret-affecting operation, collapsed along the inline axis (`scaleX`/`scaleY` ≈ 0.001) so retained text never moves the DOM caret. Vertical text uses `writing-mode:vertical-rl` and sits one column right of the caret column (macOS Chrome/Firefox: on the column). +- Mid-composition: NEVER write the composing element (style, text, selection) and never dispatch to the store; real IMEs (mozc on ibus/fcitx) abort and commit on any such mutation, including style writes on the keydown-229 before `compositionstart`. Only the wrapper moves (`follow-ime-caret!`: transform, or left/top on macOS vertical), by the WASM caret delta from the idle anchor. +- The WASM preview lives in WASM state (`text-editor-composition-update`); the store syncs on `compositionend` only. Preview fallback fonts must be requested explicitly (`load-composition-fonts!`), or uncommitted glyphs render as squares. +- Composition caret placement: on Linux+Chromium (`follow-ime-cursor?`) it tracks the IME's own cursor (the DOM selection, read on `input` with `insertCompositionText`); elsewhere it goes to the end of the changed text (`changed-span-end`) and later cursor-only moves mirror the DOM selection. +- Chromium: programmatic `.focus()` on a contenteditable inside SVG `foreignObject` does not fire focus/focusin; establish WASM focus explicitly (`focus-editor!`). +- If the anchored caret falls outside the `foreignObject` box, Chrome scrolls it (`scrollLeft`) and cancels the offset; vertical text widens the foreignObject, not the clip rect. + +## Platform IME behavior + +- Chrome on Linux X11 (GTK IM path, `ui/gtk/input_method_context_impl_gtk.cc`) passes caret bounds to the IME ONLY inside `DispatchKeyEvent` (key press and release); `SetCursorLocation` just stores them. An anchor move caused by the IME's reply that arrives after the key release (mozc TAB prediction is slow) leaves the candidate window stale until the next key. No page-side workaround: moving the anchor by transform or by layout makes no difference, and synthetic key events never reach the browser process. Keep the anchor still between keys instead. +- Blink side is not the bottleneck: the renderer recomputes selection bounds every main frame (`WidgetBase::WillBeginMainFrame` → `UpdateSelectionBounds`), and a layout-tree update invalidates its cache. +- mozc cursor: end of the preedit while typing; start of the active clause during conversion/prediction (stays there while cycling candidates). The first TAB/Space of a conversion moves it back to the clause start, so that one key lags on Linux Chrome, exactly as in a plain Chrome textarea. +- Synthetic input (`cljs_repl`, CDP `Input.imeSetComposition`) bypasses the OS IME: it validates event flow and WASM preview/commit, but cannot reproduce aborts, candidate-window position, or long-lived uncommitted preedits (real users often leave the preedit uncommitted for seconds). + +## Debugging + +- Geometry overlay: `debug.toggle_debug('ime')` (no reload). Blue = WASM caret, yellow = DOM caret, pink = DOM composition range (what Chromium reports to the IME); values are viewport CSS px. The native candidate window position is not visible to the page. +- When only the user reproduces: install capture-phase document listeners (keydown/keyup/composition*/input) logging `Date.now()`, `getSelection().focusOffset`, the wrapper style and the DOM caret rect; with the page `screenX/screenY`, `devicePixelRatio`, `outerHeight - innerHeight*dpr` (browser chrome height) to map viewport → screen px. Page listeners and `set!` patches vanish on reload; reinstall. +- Real candidate window position on X11/fcitx5: find `Fcitx5 Input Window` with `xdotool search --class fcitx`, then poll `xwininfo -id ` (absolute X/Y, size, Map State) every ~30 ms in a background loop, logging only changes with timestamps; correlate with the page log. Pressing a lone modifier (Ctrl) that snaps a stale window to the right place confirms a key-event-only bounds update. +- Screen recordings (e.g. Peek) of a user repro: extract timestamped contact sheets with `ffmpeg -vf "fps=2,scale=606:-1,drawtext=text='%{pts\:hms}':x=5:y=5:fontsize=20:fontcolor=yellow:box=1:boxcolor=black,tile=2x3"` and read them with the overlay panel visible. +- Driving the real IME from scripts on X11: `fcitx5-remote -s mozc` / restore the previous IM (`-n` to query), `xdotool windowactivate --sync`, `xdotool type --delay 250`, `xdotool key Tab|space|Return`. Check the screen is unlocked and on first, and get the user's consent: it takes over their keyboard. diff --git a/frontend/packages/draft-js/index.js b/frontend/packages/draft-js/index.js index 5372d7ee40..97c4518f70 100644 --- a/frontend/packages/draft-js/index.js +++ b/frontend/packages/draft-js/index.js @@ -34,6 +34,10 @@ function mergeBlockData(block, newData) { let data = block.getData(); for (let key of Object.keys(newData)) { + if (newData[key] == null) { + data = data.delete(key); + continue; + } const oldVal = data.get(key); if (oldVal === newData[key]) { data = data.delete(key); diff --git a/frontend/src/app/main/data/workspace/drawing/common.cljs b/frontend/src/app/main/data/workspace/drawing/common.cljs index bb54665da3..e07efaaa06 100644 --- a/frontend/src/app/main/data/workspace/drawing/common.cljs +++ b/frontend/src/app/main/data/workspace/drawing/common.cljs @@ -18,6 +18,7 @@ [app.main.data.workspace.shapes :as dwsh] [app.main.data.workspace.undo :as dwu] [app.main.worker :as mw] + [app.util.text.writing-mode :as wm] [beicon.v2.core :as rx] [potok.v2.core :as ptk])) @@ -46,6 +47,15 @@ (assoc :points (grc/rect->points selrect)) (assoc :grow-type :fixed)))) +(defn click-draw-text + "Place the start of an auto-width text at the clicked point." + [shape] + (-> shape + (assoc :height 17 :width 4 :grow-type :auto-width :selrect nil :points nil) + (cts/setup-shape) + (cond-> (wm/vertical-text-content? (:content shape)) + (gsh/transform-shape (ctm/move-modifiers -4 0))))) + (defn clear-drawing ([] (clear-drawing nil)) ([{:keys [preserve-tool?]}] @@ -103,8 +113,7 @@ (click-draw-path) (and click-draw? text?) - (-> (assoc :height 17 :width 4 :grow-type :auto-width) - (cts/setup-shape)) + (click-draw-text) :always (dissoc :initialized? :click-draw?))] diff --git a/frontend/src/app/main/data/workspace/modifiers.cljs b/frontend/src/app/main/data/workspace/modifiers.cljs index 82e3d9cc72..65a3522dde 100644 --- a/frontend/src/app/main/data/workspace/modifiers.cljs +++ b/frontend/src/app/main/data/workspace/modifiers.cljs @@ -35,6 +35,7 @@ [app.render-wasm.api :as wasm.api] [app.render-wasm.gesture :as wasm-gesture] [app.render-wasm.shape :as wasm.shape] + [app.util.text.writing-mode :as wm] [beicon.v2.core :as rx] [potok.v2.core :as ptk])) @@ -1102,21 +1103,32 @@ (rx/of (dwu/commit-undo-transaction undo-id)) (rx/empty)))))))) +(defn vertical-text-shape? + [shape] + (wm/vertical-text-content? (get shape :content))) + ;; Pure function to determine next grow-type for text layers (defn next-grow-type - [current-grow-type scalev] - (cond - (= current-grow-type :fixed) - :fixed + ([current-grow-type scalev] + (next-grow-type current-grow-type scalev false)) - (and (not (mth/close? (:y scalev) 1.0)) - (or (= current-grow-type :auto-width) - (= current-grow-type :auto-height))) - :fixed + ([current-grow-type scalev vertical?] + ;; Reuse the horizontal state machine with swapped vertical axes. + (let [scalev (if vertical? + (gpt/point (:y scalev) (:x scalev)) + scalev)] + (cond + (= current-grow-type :fixed) + :fixed - (and (not (mth/close? (:x scalev) 1.0)) - (= current-grow-type :auto-width)) - :auto-height + (and (not (mth/close? (:y scalev) 1.0)) + (or (= current-grow-type :auto-width) + (= current-grow-type :auto-height))) + :fixed - :else - current-grow-type)) + (and (not (mth/close? (:x scalev) 1.0)) + (= current-grow-type :auto-width)) + :auto-height + + :else + current-grow-type)))) diff --git a/frontend/src/app/main/data/workspace/texts.cljs b/frontend/src/app/main/data/workspace/texts.cljs index 9d727e4c9b..c1e4eba7de 100644 --- a/frontend/src/app/main/data/workspace/texts.cljs +++ b/frontend/src/app/main/data/workspace/texts.cljs @@ -24,6 +24,7 @@ [app.common.types.shape.layout :as ctl] [app.common.types.shape.text :as cts] [app.common.types.text :as txt] + [app.common.types.text.cjk-layout :as cjk] [app.common.uuid :as uuid] [app.main.data.changes :as dch] [app.main.data.helpers :as dsh] @@ -446,6 +447,12 @@ [{:keys [attrs shape]}] (shape-current-values shape txt/is-root-node? attrs)) +(defn current-ruby-values + [{:keys [attrs shape]}] + (shape-current-values shape + #(and (txt/is-text-node? %) (cjk/ruby-span? %)) + attrs)) + (defn v3-current-text-values [{:keys [editor-styles attrs]}] (let [result (-> editor-styles @@ -496,7 +503,8 @@ (defn current-paragraph-values [{:keys [editor-styles editor-state editor-instance attrs shape] :as options}] (cond - (some? editor-styles) (v3-current-text-values options) + (some? editor-styles) (merge (shape-current-values shape txt/is-paragraph-node? attrs) + (select-keys editor-styles attrs)) (v2-editor-live? editor-instance) (v2-current-text-values options) (some? editor-state) (v1-current-paragraph-values options) :else (shape-current-values shape txt/is-paragraph-node? attrs))) @@ -534,26 +542,24 @@ (count (:text node))))) - (defn decorate-range-info - "Adds information about ranges inside the metadata of the text nodes" + "Adds information about ranges inside the metadata of the text nodes. + Offsets run top-down so every node counts from its parent's start." [content] - (->> (with-meta content {:start 0 :end (count-node-chars content)}) - (txt/transform-nodes - (fn [node] - (d/update-when - node - :children - (fn [children] - (let [start (-> node meta (:start 0))] - (->> children - (reduce (fn [[result start] node] - (let [end (+ start (count-node-chars node))] - [(-> result - (conj (with-meta node {:start start :end end}))) - end])) - [[] start]) - (first))))))))) + (letfn [(decorate-children [node] + (let [start (-> node meta (:start 0))] + (d/update-when + node + :children + (fn [children] + (->> children + (reduce (fn [[result start] child] + (let [end (+ start (count-node-chars child)) + child (with-meta child {:start start :end end})] + [(conj result (decorate-children child)) end])) + [[] start]) + (first))))))] + (decorate-children (with-meta content {:start 0 :end (count-node-chars content)})))) (defn split-content-at [content position] @@ -588,13 +594,66 @@ (<= (-> node meta :end) end)))) #(d/patch-object % attrs)))) -(defn- update-text-range-attrs +(defn- overlapping-text-nodes + "Text nodes of a range-decorated content with characters in [start, end)." + [content start end] + (->> (txt/node-seq txt/is-text-node? content) + (filter (fn [node] + (let [{node-start :start node-end :end} (meta node)] + (and (< node-start end) (< start node-end))))))) + +(defn- annotated-span-bounds + "The range [start, end) widened to cover every ruby or warichu span it + cuts, so a reading or note is never split between two spans." + [content start end] + (->> (overlapping-text-nodes content start end) + (filter cjk/annotated-span?) + (reduce (fn [[start end] node] + (let [{node-start :start node-end :end} (meta node)] + [(min start node-start) (max end node-end)])) + [start end]))) + +(defn single-span-range? + "True when the characters [start, end) of `content`, widened to whole ruby + and warichu spans, all belong to one text span." + [content start end] + (or (nil? content) + (let [content (decorate-range-info content) + [start end] (annotated-span-bounds content start end)] + (<= (count (overlapping-text-nodes content start end)) 1)))) + +;; Paragraph attrs a text range sets on the whole paragraphs it touches. +(def ^:private range-paragraph-attrs + [:text-language]) + +(defn- update-paragraph-range + "Applies `attrs` to the paragraphs of a range-decorated content with + characters in [start, end); a nil value removes the attr." + [content start end attrs] + (txt/transform-nodes + (fn [node] + (and (txt/is-paragraph-node? node) + (let [{node-start :start node-end :end} (meta node)] + (and (< node-start end) (< start node-end))))) + #(d/patch-object % attrs) + content)) + +(defn update-text-range-attrs + "Applies `attrs` to the characters [start, end) of a text shape. A range + that cuts a ruby or warichu span covers the whole span. Spans left with + the same style as their neighbour are joined back. Paragraph attrs such + as the language go to the whole paragraphs the range touches." [shape start end attrs] - (let [new-content (-> (:content shape) - (decorate-range-info) - (split-content-at start) - (split-content-at end) - (update-content-range start end attrs))] + (let [content (decorate-range-info (:content shape)) + [start end] (annotated-span-bounds content start end) + span-attrs (apply dissoc attrs range-paragraph-attrs) + new-content (->> (-> content + (update-paragraph-range start end (select-keys attrs range-paragraph-attrs)) + (split-content-at start) + (split-content-at end) + (update-content-range start end span-attrs)) + (txt/transform-nodes txt/is-paragraph-node? + #(d/update-when % :children txt/merge-equal-spans)))] (assoc shape :content new-content))) (defn update-text-range @@ -657,30 +716,32 @@ (defn update-paragraph-attrs [{:keys [id attrs]}] - (let [attrs (d/without-nils attrs)] - (ptk/reify ::update-paragraph-attrs - ptk/UpdateEvent - (update [_ state] - (d/update-in-when state [:workspace-editor-state id] ted/update-editor-current-block-data attrs)) + (ptk/reify ::update-paragraph-attrs + ptk/UpdateEvent + (update [_ state] + (d/update-in-when state [:workspace-editor-state id] ted/update-editor-current-block-data attrs)) - ptk/WatchEvent - (watch [_ state _] - (when-not (some? (get-in state [:workspace-editor-state id])) - (let [objects (dsh/lookup-page-objects state) - shape (get objects id) + ptk/WatchEvent + (watch [_ state _] + (when-not (some? (get-in state [:workspace-editor-state id])) + (let [objects (dsh/lookup-page-objects state) + shape (get objects id) - merge-fn (fn [node attrs] - (reduce-kv - (fn [node k v] (assoc node k v)) - node - attrs)) + merge-fn (fn [node attrs] + (reduce-kv + (fn [node k v] + (if (nil? v) + (dissoc node k) + (assoc node k v))) + node + attrs)) - update-fn #(txt/update-text-content % txt/is-paragraph-node? merge-fn attrs) - shape-ids (cond - (cfh/text-shape? shape) [id] - (cfh/group-shape? shape) (cfh/get-children-ids objects id))] + update-fn #(txt/update-text-content % txt/is-paragraph-node? merge-fn attrs) + shape-ids (cond + (cfh/text-shape? shape) [id] + (cfh/group-shape? shape) (cfh/get-children-ids objects id))] - (rx/of (dwsh/update-shapes shape-ids update-fn)))))))) + (rx/of (dwsh/update-shapes shape-ids update-fn))))))) (defn update-text-attrs [{:keys [id attrs]}] @@ -712,6 +773,47 @@ updated-shape))] (rx/of (dwsh/update-shapes shape-ids merge-shape))))))) +(defn update-ruby-presentation-attrs + [shape attrs] + (txt/update-text-content + shape + #(and (txt/is-text-node? %) (cjk/ruby-span? %)) + d/txt-merge + attrs)) + +(defn update-all-ruby-presentation + "Apply ruby presentation `attrs` to the ruby spans of the text shapes in + `ids`, and of the texts inside the groups among them, in one update." + [ids attrs] + (ptk/reify ::update-all-ruby-presentation + ptk/WatchEvent + (watch [_ state _] + (let [objects (dsh/lookup-page-objects state) + wasm? (features/active-feature? state "render-wasm/v1") + shape-ids (into [] + (comp (mapcat (fn [id] + (let [shape (get objects id)] + (cond + (cfh/text-shape? shape) [id] + (cfh/group-shape? shape) (cfh/get-children-ids objects id))))) + (distinct)) + ids) + update-fn (fn [shape] + (let [updated-shape (update-ruby-presentation-attrs shape attrs)] + (when (and wasm? (cfh/text-shape? updated-shape)) + (wasm.text-editor/cache-shape-text-content! + (:id updated-shape) + (:content updated-shape))) + updated-shape)) + undo-id (js/Symbol)] + (rx/concat + (rx/of (dwu/start-undo-transaction undo-id) + (dwsh/update-shapes shape-ids update-fn)) + (if wasm? + (rx/of (dwwt/resize-wasm-text-all shape-ids)) + (rx/empty)) + (rx/of (dwu/commit-undo-transaction undo-id))))))) + (defn migrate-node [node] (let [color-attrs (not-empty (select-keys node types.fills/fill-attrs))] @@ -909,6 +1011,17 @@ (not multiple?) (update :workspace-global assoc :default-font font)))))) +(defn remember-text-language + "Remember `language` for the next new texts of the file; nil forgets it, + so they take the file language (see `tc/v2-default-text-content`)." + [language] + (ptk/reify ::remember-text-language + ptk/UpdateEvent + (update [_ state] + (if (some? language) + (assoc-in state [:workspace-global :default-text-language] language) + (update state :workspace-global dissoc :default-text-language))))) + (defn apply-text-modifier [shape text-modifier] diff --git a/frontend/src/app/main/data/workspace/transforms.cljs b/frontend/src/app/main/data/workspace/transforms.cljs index 3e5f8ced3a..7aad8bcfab 100644 --- a/frontend/src/app/main/data/workspace/transforms.cljs +++ b/frontend/src/app/main/data/workspace/transforms.cljs @@ -266,7 +266,9 @@ ;; Calculate new grow-type for text layers new-grow-type (when (cfh/text-shape? shape) - (dwm/next-grow-type (dm/get-prop shape :grow-type) scalev)) + (dwm/next-grow-type (dm/get-prop shape :grow-type) + scalev + (dwm/vertical-text-shape? shape))) ;; When the horizontal/vertical scale a flex children with auto/fill ;; we change it too fixed @@ -559,7 +561,9 @@ new-height (if (= attr :height) value sr-height) scalev (gpt/point (/ new-width sr-width) (/ new-height sr-height)) current-grow-type (dm/get-prop shape :grow-type) - new-grow-type (dwm/next-grow-type current-grow-type scalev)] + new-grow-type (dwm/next-grow-type current-grow-type + scalev + (dwm/vertical-text-shape? shape))] (cond-> modifiers (not= new-grow-type current-grow-type) (ctm/change-property :grow-type new-grow-type))) diff --git a/frontend/src/app/main/data/workspace/wasm_text.cljs b/frontend/src/app/main/data/workspace/wasm_text.cljs index 9ff79af325..a1fb742135 100644 --- a/frontend/src/app/main/data/workspace/wasm_text.cljs +++ b/frontend/src/app/main/data/workspace/wasm_text.cljs @@ -24,11 +24,19 @@ [app.main.data.workspace.undo :as dwu] [app.render-wasm.api :as wasm.api] [app.render-wasm.api.fonts :as wasm.fonts] + [app.util.text.geometry :as text.geom] [beicon.v2.core :as rx] [potok.v2.core :as ptk])) (def debounce-resize-text-time 40) +(defn resolve-text-size + "Selects the axes controlled by WASM for a text grow type. Vertical + auto-height keeps the physical height as its wrap budget and grows width." + [selrect grow-type content dimension] + (when (or (= :fixed grow-type) (some? dimension)) + (text.geom/resolve-size selrect grow-type content dimension))) + (defn get-wasm-text-new-size "Computes the new {width, height} for a text shape from WASM text layout. For :fixed grow-type, updates WASM content and returns current dimensions (no resize)." @@ -51,25 +59,18 @@ (wasm.api/set-shape-text-images id content)) (let [dimension (when (not= :fixed grow-type) (wasm.api/get-text-dimensions))] - ;; nil dimension = shape not present in WASM state; skip the resize. - (when (or (= :fixed grow-type) (some? dimension)) - {:width (if (#{:fixed :auto-height} grow-type) - (:width selrect) - (:width dimension)) - :height (if (= :fixed grow-type) - (:height selrect) - (:height dimension))}))))) + (resolve-text-size selrect grow-type content dimension))))) (defn resize-wasm-text-modifiers ([shape] (resize-wasm-text-modifiers shape (:content shape))) - ([{:keys [id points selrect] :as shape} content] + ([{:keys [id selrect] :as shape} content] (when-let [new-size (get-wasm-text-new-size shape content)] (let [width-scale (/ (:width new-size) (:width selrect)) height-scale (/ (:height new-size) (:height selrect)) resize-v (gpt/point width-scale height-scale) - origin (first points)] + origin (text.geom/resize-origin (assoc shape :content content))] {id {:modifiers (ctm/resize-modifiers @@ -275,3 +276,5 @@ (rx/empty) resize-stream))))) resize-stream)))))) + + diff --git a/frontend/src/app/main/ui/css_cursors.cljs b/frontend/src/app/main/ui/css_cursors.cljs index 8541d7bedf..91f30c1941 100644 --- a/frontend/src/app/main/ui/css_cursors.cljs +++ b/frontend/src/app/main/ui/css_cursors.cljs @@ -17,6 +17,11 @@ [name rotation] (dm/str "cursor-" name "-" (mod (* (.floor js/Math (/ rotation angle-step)) angle-step) 360))) +(defn get-text + "Returns the text cursor class, rotating the I-beam with vertical text flow." + [rotation vertical?] + (get-dynamic "text" (+ (or rotation 0) (if vertical? 90 0)))) + (defn init-static-cursor-style "Initializes a static cursor style" [style name value] diff --git a/frontend/src/app/main/ui/workspace/shapes/text/editor.cljs b/frontend/src/app/main/ui/workspace/shapes/text/editor.cljs index f7e55cd8be..84f1996cd0 100644 --- a/frontend/src/app/main/ui/workspace/shapes/text/editor.cljs +++ b/frontend/src/app/main/ui/workspace/shapes/text/editor.cljs @@ -25,6 +25,7 @@ [app.util.keyboard :as kbd] [app.util.object :as obj] [app.util.text-editor :as ted] + [app.util.text.writing-mode :as wm] [goog.events :as events] [rumext.v2 :as mf])) @@ -36,7 +37,7 @@ (let [bprops (obj/get props "blockProps") data (obj/get bprops "data") style (sts/generate-paragraph-styles (obj/get bprops "shape") - (obj/get bprops "data")) + (wm/renderable-paragraph data)) dir (:text-direction data "auto")] @@ -247,7 +248,7 @@ :opacity (when @blurred 0)} :on-pointer-down on-pointer-down :class (dom/classnames - (cur/get-dynamic "text" (:rotation shape)) true + (cur/get-text (:rotation shape) (wm/vertical-text-content? content)) true :align-top (= (:vertical-align content "top") "top") :align-center (= (:vertical-align content) "center") :align-bottom (= (:vertical-align content) "bottom"))} diff --git a/frontend/src/app/main/ui/workspace/shapes/text/ime_debug.cljs b/frontend/src/app/main/ui/workspace/shapes/text/ime_debug.cljs new file mode 100644 index 0000000000..d66c8927d5 --- /dev/null +++ b/frontend/src/app/main/ui/workspace/shapes/text/ime_debug.cljs @@ -0,0 +1,155 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns app.main.ui.workspace.shapes.text.ime-debug + "Read-only IME geometry overlay. Enable with debug.toggle_debug('ime')." + (:require-macros [app.main.style :as stl]) + (:require + [app.config :as cf] + [app.render-wasm.text-editor :as text-editor] + [app.util.debug :as dbg] + [rumext.v2 :as mf])) + +(defn- screen-rect + [{:keys [x y width height]} ^js matrix] + (let [points (for [[px py] [[x y] [(+ x width) y] + [x (+ y height)] [(+ x width) (+ y height)]]] + [(+ (* (.-a matrix) px) (* (.-c matrix) py) (.-e matrix)) + (+ (* (.-b matrix) px) (* (.-d matrix) py) (.-f matrix))]) + xs (map first points) + ys (map second points) + left (apply min xs) + top (apply min ys)] + {:x left :y top :width (- (apply max xs) left) :height (- (apply max ys) top)})) + +(defn- dom-rect + [^js rect] + {:x (.-x rect) :y (.-y rect) :width (.-width rect) :height (.-height rect)}) + +(defn- text-position + "Resolve a UTF-16 offset, keeping range edges inside the composing text." + [nodes offset edge] + (loop [[node & remaining] nodes + offset offset] + (when node + (if (or (< offset (.-length ^js node)) + (and (= offset (.-length ^js node)) + (or (= edge :end) (empty? remaining)))) + [node offset] + (recur remaining (- offset (.-length ^js node))))))) + +(defn- composition-range + [^js node {:keys [start text]}] + (let [walker (.createTreeWalker js/document node js/NodeFilter.SHOW_TEXT) + nodes (loop [nodes []] + (if-let [text-node (.nextNode walker)] + (recur (conj nodes text-node)) + nodes)) + from (text-position nodes start :start) + to (text-position nodes (+ start (count text)) :end)] + (when (and from to) + (let [range (.createRange js/document)] + (.setStart range (first from) (second from)) + (.setEnd range (first to) (second to)) + range)))) + +(defn- browser-caret + [^js node] + (let [selection (.getSelection js/window)] + (when (and selection (.contains node (.-focusNode selection))) + (let [range (.createRange js/document)] + (.setStart range (.-focusNode selection) (.-focusOffset selection)) + (.collapse range true) + (dom-rect (.getBoundingClientRect range)))))) + +(defn- snapshot + "All bounds use viewport CSS pixels, matching the fixed debug overlay." + [^js node {:keys [active text] :as composition}] + (let [matrix (some-> (.closest node "g.text-editor") (.getScreenCTM)) + caret (text-editor/text-editor-get-cursor-rect) + range (when (and active (seq text)) (composition-range node composition))] + {:platform cf/platform + :browser cf/browser + :writing-mode (.-writingMode (.getComputedStyle js/window node)) + :active active + :text text + :start (:start composition) + :scroll {:x (.-scrollLeft node) :y (.-scrollTop node)} + :wasm-caret (when (and matrix caret) (screen-rect caret matrix)) + :dom-caret (browser-caret node) + :composition (when range (mapv dom-rect (array-seq (.getClientRects range))))})) + +(defn- format-rect + [{:keys [x y width height] :as rect}] + (if rect + (str "x=" (.toFixed x 1) " y=" (.toFixed y 1) + " w=" (.toFixed width 2) " h=" (.toFixed height 2)) + "unavailable")) + +(mf/defc bounds* + {::mf/private true} + [{:keys [rect class]}] + (let [{:keys [x y width height]} rect] + [:g {:class class} + ;; Keep zero-size bounds visible without changing the measured values. + [:rect {:x x :y y :width (max 1 width) :height (max 1 height) + :fill "none" :stroke "currentColor" :stroke-width 1}] + [:path {:d (str "M " (- x 4) " " y " h 8 M " x " " (- y 4) " v 8") + :stroke "currentColor" :stroke-width 1}]])) + +(mf/defc overlay* + [{:keys [input-ref composing-ref composition-text-ref composition-base-ref]}] + (let [options (mf/deref dbg/state) + enabled (contains? options :ime) + sample* (mf/use-state nil)] + ;; A separate component owns debug state. Sampling never changes the input, + ;; its selection, or the editor component's state during composition. + (mf/use-effect + (mf/deps enabled) + (fn [] + (when enabled + (let [frame (atom nil)] + (letfn [(tick [] + (when-let [node (mf/ref-val input-ref)] + (let [composition {:active (mf/ref-val composing-ref) + :text (mf/ref-val composition-text-ref) + :start (mf/ref-val composition-base-ref)} + next-sample (snapshot node composition)] + (swap! sample* #(if (= % next-sample) % next-sample)))) + (reset! frame (js/requestAnimationFrame tick)))] + (tick) + (fn [] (js/cancelAnimationFrame @frame))))))) + + (when enabled + (let [{:keys [platform browser writing-mode active text start scroll + wasm-caret dom-caret composition]} @sample*] + ;; A body portal avoids the text shape's SVG clip and transforms. + (mf/portal + (mf/html + [:div {:class (stl/css :ime-debug) :data-testid "ime-debug" :aria-hidden true} + [:svg {:class (stl/css :geometry)} + (when wasm-caret + [:> bounds* {:rect wasm-caret :class (stl/css :wasm-caret)}]) + (when dom-caret + [:> bounds* {:rect dom-caret :class (stl/css :dom-caret)}]) + (for [[index rect] (map-indexed vector composition)] + [:> bounds* {:key index :rect rect :class (stl/css :composition)}])] + [:div {:class (stl/css :panel)} + [:div (str "IME · " (name (or platform cf/platform)) + " · " (name (or browser cf/browser)) " · " writing-mode + " · " (if active "composing" "idle"))] + [:div {:class (stl/css :wasm-caret)} (str "Penpot caret: " (format-rect wasm-caret))] + [:div {:class (stl/css :dom-caret)} (str "Browser caret: " (format-rect dom-caret))] + [:div {:class (stl/css :composition)} + (str "Browser composition: " (format-rect (first composition)) + " · " (count composition) " rectangle(s)")] + [:div (str "Input scroll: " (:x scroll) ", " (:y scroll) + " · composition start: " start " UTF-16 units")] + (when active + [:div {:class (stl/css :composition-text)} (str "Text: " text)]) + [:div {:class (stl/css :note)} + "Bounds are in viewport CSS pixels. Native candidate window bounds are unavailable."]]]) + (.-body js/document)))))) diff --git a/frontend/src/app/main/ui/workspace/shapes/text/ime_debug.scss b/frontend/src/app/main/ui/workspace/shapes/text/ime_debug.scss new file mode 100644 index 0000000000..1212086c6d --- /dev/null +++ b/frontend/src/app/main/ui/workspace/shapes/text/ime_debug.scss @@ -0,0 +1,48 @@ +.ime-debug { + position: fixed; + inset: 0; + z-index: 10000; + pointer-events: none; +} + +.geometry { + width: 100%; + height: 100%; + overflow: visible; +} + +.panel { + position: absolute; + bottom: 48px; + left: 64px; + max-width: 480px; + padding: 8px 12px; + border: 1px solid #697582; + border-radius: 4px; + background: #17212b; + color: #fff; + font: 12px / 18px monospace; +} + +.wasm-caret { + color: #36bdff; +} + +.dom-caret { + color: #ffbe55; +} + +.composition { + color: #ff73ce; +} + +.composition-text { + max-height: 72px; + overflow: hidden; + overflow-wrap: anywhere; + white-space: pre-wrap; +} + +.note { + color: #c0ccd8; +} diff --git a/frontend/src/app/main/ui/workspace/shapes/text/text_edition_outline.cljs b/frontend/src/app/main/ui/workspace/shapes/text/text_edition_outline.cljs index 43d2b492d0..32e2dd4be7 100644 --- a/frontend/src/app/main/ui/workspace/shapes/text/text_edition_outline.cljs +++ b/frontend/src/app/main/ui/workspace/shapes/text/text_edition_outline.cljs @@ -13,6 +13,7 @@ [app.main.refs :as refs] [app.main.store :as st] [app.render-wasm.api :as wasm.api] + [app.util.text.geometry :as text.geom] [rumext.v2 :as mf])) (mf/defc text-edition-outline* @@ -21,14 +22,23 @@ (let [selrect-transform (mf/deref refs/workspace-selrect) [selrect transform] (dsh/get-selrect selrect-transform shape) - ;; While editing, the committed selrect lags the text (geometry is - ;; finalize-only), so measure the live WASM text for the growing axes. - live (wasm.api/get-text-dimensions (:id shape)) - {:keys [width height]} (dwt/merge-live-text-dimensions - (:grow-type shape) selrect live)] + ;; IME previews never enter the store. Subscribe here so only the + ;; outline updates; re-rendering the capture surface aborts real IMEs. + dimension* (mf/use-state nil) + dimension (or @dimension* (wasm.api/get-text-dimensions (:id shape))) + {:keys [x y width height]} + (text.geom/live-rect selrect (:grow-type shape) (:content shape) dimension)] + (mf/use-effect + (mf/deps (:id shape)) + (fn [] + (let [update-bounds! (fn [_] + (reset! dimension* (wasm.api/get-text-dimensions (:id shape))))] + (.addEventListener js/document "penpot:wasm:render" update-bounds!) + (update-bounds! nil) + #(.removeEventListener js/document "penpot:wasm:render" update-bounds!)))) [:rect.main.viewport-selrect - {:x (:x selrect) - :y (:y selrect) + {:x x + :y y :width width :height height :transform transform diff --git a/frontend/src/app/main/ui/workspace/shapes/text/v3_editor.cljs b/frontend/src/app/main/ui/workspace/shapes/text/v3_editor.cljs index dfb2f3a556..55ec8fa6e0 100644 --- a/frontend/src/app/main/ui/workspace/shapes/text/v3_editor.cljs +++ b/frontend/src/app/main/ui/workspace/shapes/text/v3_editor.cljs @@ -17,10 +17,13 @@ [app.main.refs :as refs] [app.main.store :as st] [app.main.ui.css-cursors :as cur] + [app.main.ui.workspace.shapes.text.ime-debug :as ime-debug] [app.render-wasm.api :as wasm.api] [app.render-wasm.text-editor :as text-editor] [app.util.dom :as dom] [app.util.keyboard :as kbd] + [app.util.text.geometry :as text.geom] + [app.util.text.writing-mode :as wm] [app.util.timers :as ts] [beicon.v2.core :as rx] [cuerdas.core :as str] @@ -183,12 +186,163 @@ Mousetrap's `stopCallback` drops every keystroke whose target is contentEditable, so without the `mousetrap` class (as in V1/V2) the text - shortcuts (Ctrl+B, Ctrl+I, …) never reach the dispatcher." - [rotation] - (dm/str "mousetrap " - (cur/get-dynamic "text" rotation) - " " - (stl/css :text-editor-container))) + shortcuts (Ctrl+B, Ctrl+I, …) never reach the dispatcher. The hover cursor + lives on the overlay: the surface is pointer-events:none." + [] + (dm/str "mousetrap " (stl/css :text-editor-container))) + +(def ^:private ime-surface-collapse + "Inline-axis scale of the capture surface to limit DOM caret drift." + 0.001) + +(defn- macos-vertical-ime-scale + "Keep composition characters nonempty after Chromium rounds screen bounds." + [^js node font-size] + (let [matrix (some-> node (.closest "foreignObject") (.getScreenCTM)) + zoom (if matrix (js/Math.hypot (.-c matrix) (.-d matrix)) 1)] + ;; Chromium derives vertical character height from integer caret positions. + ;; scaleY(0.001) makes most of those heights zero, leaving the native macOS + ;; IME without usable character bounds. Allow 1.5 screen pixels per character, + ;; including a margin for fractional layout/zoom rounding. + (max ime-surface-collapse (/ 1.5 (max 0.001 (* font-size zoom)))))) + +(defn- update-ime-caret! + "Move the hidden capture surface onto the WASM caret so the IME candidate + window opens next to the edited text. Returns the caret rect with its + writing direction, or nil when there is none yet. + + Call only while idle, never during or right before a composition: real + IMEs (ibus-mozc) abort and commit on any mutation of the composing + element, including style writes on the keydown-229 before + compositionstart. So the surface follows the caret after every + caret-affecting operation, and during a composition only its wrapper + moves (see `follow-ime-caret!`). + + The IME anchors its window to the DOM caret inside the surface. The + surface is compressed along the inline axis to limit the advance of its + retained text (see `keep-input-alive`) and composition; the block axis + keeps the caret cell size. macOS vertical characters retain nonempty + screen bounds so its native IME can anchor to them. In vertical text the + surface (`vertical-rl`, `line-height: 1`) aligns its caret cell with the + text column on Windows and on macOS outside Safari. Safari has been observed + to show horizontal candidates for vertical text, so it keeps the surface + right of the column, like Linux where candidates open below the caret. + + `origin` holds the pointer overlay's top-left and width, in the + same page space as the caret rect." + [^js node ^js wrapper origin] + (when (and (some? node) (some? origin)) + (when-let [{:keys [x y width height] :as rect} (text-editor/text-editor-get-cursor-rect)] + (let [style (.-style node) + macos-vertical? (and (:vertical? origin) (cf/check-platform? :macos))] + ;; Unlike overflow:hidden, clip is not a scroll container. Long + ;; compositions must not scroll the capture surface away from its anchor. + (set! (.-overflow style) (if macos-vertical? "clip" "")) + (if (:vertical? origin) + (do + (set! (.-writingMode style) "vertical-rl") + (set! (.-fontSize style) (dm/str width "px")) + (set! (.-lineHeight style) "1") + (set! (.-transformOrigin style) "100% 0") + (set! (.-transform style) + (dm/str "scaleY(" (if macos-vertical? + (macos-vertical-ime-scale node width) + ime-surface-collapse) ")")) + ;; vertical-rl puts the DOM caret in the surface's rightmost cell. + ;; Safari's horizontal candidate window needs an anchor beside + ;; the column. Windows and non-Safari macOS use the column itself. + (let [right (if (or (cf/check-platform? :windows) + (and macos-vertical? (not (cf/check-browser? :safari)))) + (+ x width) + (+ x (* 2.15 width)))] + (set! (.-left style) (dm/str (- right (:x origin) (:width origin)) "px")))) + (do + (set! (.-writingMode style) "") + (set! (.-fontSize style) (dm/str height "px")) + (set! (.-lineHeight style) "") + (set! (.-transformOrigin style) "0 0") + (set! (.-transform style) (dm/str "scaleX(" ime-surface-collapse ")")) + (set! (.-left style) (dm/str (- x (:x origin)) "px")))) + (set! (.-top style) (dm/str (- y (:y origin)) "px")) + (when (some? wrapper) + (set! (.. wrapper -style -transform) "") + (set! (.. wrapper -style -left) "") + (set! (.. wrapper -style -top) "")) + (assoc rect :vertical? (:vertical? origin)))))) + +(defn- ime-overlay-padding + "Space around the text overlay for the hidden vertical IME anchor." + [vertical? viewport-width] + (cond + (not vertical?) {:left 0 :right 0} + (cf/check-platform? :macos) {:left viewport-width + :right (if (cf/check-browser? :safari) viewport-width 0)} + :else {:left 0 :right viewport-width})) + +(defn- text-before-selection + "UTF-16 length of the surface text before the DOM selection focus, or nil + when the selection is not inside `node`. Reading it mutates nothing." + [^js node] + (let [sel (.getSelection js/window)] + (when (and (some? node) (some? sel) (pos? (.-rangeCount sel)) + (.contains node (.-focusNode sel))) + (let [range (.createRange js/document)] + (.setStart range node 0) + (.setEnd range (.-focusNode sel) (.-focusOffset sel)) + (.-length (.toString range)))))) + +(defn changed-span-end + "Code point index, in `new-text`, where the part that differs from + `old-text` ends: a newly typed kana or a newly picked candidate." + [old-text new-text] + (let [old (js/Array.from old-text) + new (js/Array.from new-text) + n-old (.-length old) + n-new (.-length new) + prefix (loop [i 0] + (if (and (< i n-old) (< i n-new) (= (aget old i) (aget new i))) + (recur (inc i)) + i)) + suffix (loop [i 0] + (if (and (< i (- n-old prefix)) + (< i (- n-new prefix)) + (= (aget old (- n-old 1 i)) (aget new (- n-new 1 i)))) + (recur (inc i)) + i))] + (- n-new suffix))) + +(def ^:private follow-ime-cursor? + "True where the composition caret tracks the IME cursor (the DOM selection) + instead of the end of the changed text. + + Chromium on Linux hands the caret bounds to GTK input methods only while + dispatching a key event. A caret move caused by the IME's reply arrives + after the key release when the IME is slow (mozc prediction on Tab), so + the candidate window keeps its old position until the next key. The IME + cursor stays on the active clause while cycling candidates, so the window + never needs that late update." + (and (cf/check-platform? :linux) + (or (cf/check-browser? :chrome) + (cf/check-browser? :edge)))) + +(defn- follow-ime-caret! + "Move the surface wrapper by how far the WASM caret moved from `anchor`, + the caret rect the surface was placed on. Writes only the wrapper, never the + composing element, so it is safe mid-composition. Returns nil when the caret + rect is not available yet." + [^js wrapper anchor] + (when (and (some? wrapper) (some? anchor)) + (when-let [{:keys [x y]} (text-editor/text-editor-get-cursor-rect)] + (let [dx (- x (:x anchor)) + dy (- y (:y anchor))] + (if (and (:vertical? anchor) (cf/check-platform? :macos)) + (do + ;; Move the wrapper in layout to update the composition bounds + ;; that macOS uses to position the native candidate window. + (set! (.. wrapper -style -left) (dm/str dx "px")) + (set! (.. wrapper -style -top) (dm/str dy "px"))) + (set! (.. wrapper -style -transform) + (dm/str "translate(" dx "px, " dy "px)"))))))) (mf/defc text-editor* "Contenteditable element positioned over the text shape to capture input events." @@ -197,16 +351,10 @@ clip-id (dm/str "text-edition-clip" shape-id) + vertical? (wm/vertical-text-content? (:content shape)) + contenteditable-ref (mf/use-ref nil) - ;; True between compositionstart and compositionend: history restores - ;; and shortcuts wait until the IME commits or cancels. - composing-ref (mf/use-ref false) - - ;; Native IMEs can end composition before the cancelling key's key-up. - ;; Remember its key-down so that Escape only cancels the preedit. - ime-key-ref (mf/use-ref false) - ;; Number of characters the browser is about to replace via marked text ;; (macOS press-and-hold accent menu). Set on `beforeinput`, consumed on ;; `input`. See on-before-input / on-input. @@ -219,55 +367,195 @@ deferred-press-ref (mf/use-ref nil) + ;; Pointer overlay top-left and width in page space (see `update-ime-caret!`). + origin-ref (mf/use-ref nil) + + ;; True between compositionstart and compositionend; the surface must not move then. + composing-ref (mf/use-ref false) + + ;; Native IMEs can end composition before the cancelling key's key-up. + ;; Remember its key-down so that Escape only cancels the preedit. + ime-key-ref (mf/use-ref false) + + ;; Surface wrapper, moved during a composition (see `follow-ime-caret!`). + wrapper-ref (mf/use-ref nil) + + ;; Caret rectangle the surface was last placed on while idle. + anchor-ref (mf/use-ref nil) + + ;; Surface UTF-16 length before the composition, and the composition text. + composition-base-ref (mf/use-ref 0) + composition-text-ref (mf/use-ref "") + + ;; IME cursor offset last read from the DOM selection; nil after a text change. + synced-offset-ref (mf/use-ref nil) + fallback-fonts (wasm.api/fonts-from-text-content (:content shape) false) fallback-families (map (fn [font] (font-family-from-font-id (:font-id font))) fallback-fonts) - [{:keys [x y width height]} transform] - (let [{:keys [width height]} (wasm.api/get-text-dimensions shape-id) + [{:keys [x y width height ime-width ime-left]} transform] + (let [dimension (wasm.api/get-text-dimensions shape-id) selrect-transform (mf/deref refs/workspace-selrect) vbox (mf/deref refs/vbox) [selrect transform] (dsh/get-selrect selrect-transform shape) - selrect-height (:height selrect) - selrect-width (:width selrect) - max-width (max width selrect-width) - max-height (max height selrect-height) - ;; During auto-width editing the shape width is trimmed to the content, so an - ;; empty text box ends up only a few pixels wide. That is not enough room for - ;; the caret and the contenteditable overlay may fail to receive input when it - ;; is that small. Expand the overlay by one viewport width for auto-width texts - ;; (mirroring the v2 editor) so typing works and the caret is not clipped. + live-rect (text.geom/live-rect selrect (:grow-type shape) (:content shape) dimension) + ;; Reserve room for composition without changing the capture DOM + ;; mid-IME. Vertical columns grow leftward; their inline axis is y. + auto-width? (= (:grow-type shape) :auto-width) + auto-grow? (not= (:grow-type shape) :fixed) viewport-width (or (:width vbox) 0) - overlay-width (if (= (:grow-type shape) :auto-width) - (+ max-width viewport-width) - max-width) - valign (-> shape :content :vertical-align) - y (:y selrect) - y (case valign - "bottom" (+ y (- selrect-height height)) - "center" (+ y (/ (- selrect-height height) 2)) - y)] - [(assoc selrect :y y :width overlay-width :height max-height) transform]) + viewport-height (or (:height vbox) 0) + left (min (:x selrect) (:x live-rect)) + top (min (:y selrect) (:y live-rect)) + right (max (+ (:x selrect) (:width selrect)) + (+ (:x live-rect) (:width live-rect))) + bottom (max (+ (:y selrect) (:height selrect)) + (+ (:y live-rect) (:height live-rect))) + x (if (and vertical? auto-grow?) (- left viewport-width) left) + overlay-width (+ (- right x) (if (and (not vertical?) auto-width?) viewport-width 0)) + overlay-height (+ (- bottom top) + (if (if vertical? auto-width? auto-grow?) viewport-height 0)) + ime-padding (ime-overlay-padding vertical? viewport-width) + y top] + ;; Widen the foreignObject (not the clip) toward the IME anchor, or + ;; the browser scrolls to reveal the caret and cancels the offset. + [(assoc selrect + :x x :y y :width overlay-width :height overlay-height + :ime-width (+ overlay-width (:right ime-padding)) + :ime-left (:left ime-padding)) + transform]) + + schedule-ime-caret! + (mf/use-fn + (fn [] + ;; Wait two frames for the pending WASM render to rebuild the layout + ;; the caret rect reads, then retry: after mount the rect can stay + ;; unavailable until the first full layout. Skip while composing. + (letfn [(attempt [tries] + (when-not (mf/ref-val composing-ref) + (if-let [rect (update-ime-caret! + (mf/ref-val contenteditable-ref) + (mf/ref-val wrapper-ref) + (mf/ref-val origin-ref))] + (mf/set-ref-val! anchor-ref rect) + (when (pos? tries) + (js/requestAnimationFrame #(attempt (dec tries)))))))] + (js/requestAnimationFrame + (fn [] + (js/requestAnimationFrame #(attempt 30))))))) + + schedule-ime-follow! + (mf/use-fn + (fn [] + ;; Same wait and retries as `schedule-ime-caret!`: a preview update + ;; clears the layout the caret rect reads until the next render. + ;; Runs only mid-composition. + (letfn [(attempt [tries] + (when (and (mf/ref-val composing-ref) + (nil? (follow-ime-caret! + (mf/ref-val wrapper-ref) + (mf/ref-val anchor-ref))) + (pos? tries)) + (js/requestAnimationFrame #(attempt (dec tries)))))] + (js/requestAnimationFrame + (fn [] + (js/requestAnimationFrame #(attempt 30))))))) + + ;; Moves the WASM caret inside the preview, and the IME window with it. + ;; Synchronous: the browser reports caret bounds to the IME right after + ;; the update that triggered this. + place-composition-caret! + (mf/use-fn + (fn [offset] + (text-editor/text-editor-set-composition-cursor offset) + (when (nil? (follow-ime-caret! (mf/ref-val wrapper-ref) (mf/ref-val anchor-ref))) + (schedule-ime-follow!)))) + + ;; Mirrors the IME cursor (the DOM selection) on the WASM caret when only + ;; the cursor moves, e.g. switching clauses. The first read after a text + ;; change only sets the baseline (see on-composition-update), except + ;; where the caret always tracks the IME cursor (see `follow-ime-cursor?`). + sync-composition-cursor! + (mf/use-fn + (fn [] + (when (mf/ref-val composing-ref) + (when-let [focus (text-before-selection (mf/ref-val contenteditable-ref))] + (let [text (mf/ref-val composition-text-ref) + units (-> (- focus (mf/ref-val composition-base-ref)) + (max 0) + (min (count text))) + cursor (.-length (js/Array.from (subs text 0 units))) + previous (mf/ref-val synced-offset-ref)] + (mf/set-ref-val! synced-offset-ref cursor) + (when (if (some? previous) + (not= cursor previous) + follow-ime-cursor?) + (place-composition-caret! cursor) + (wasm.api/render-text-editor-overlay!))))))) + + ;; In Chromium, .focus() on a contenteditable inside an SVG foreignObject + ;; does not reliably fire focus/focusin, so React's on-focus never runs + ;; and every caret-rect read is null. This sets the WASM focus itself. + focus-editor! + (mf/use-fn + (mf/deps shape-id) + (fn [] + (when-let [node (mf/ref-val contenteditable-ref)] + (when (not= (.-activeElement js/document) node) + (.focus node))) + ;; Re-focusing resets transient WASM editor state (overtype mode, + ;; pending events), so only do it when this shape lacks the focus. + (when-not (and (text-editor/text-editor-has-focus?) + (= shape-id (text-editor/text-editor-get-active-shape-id))) + (wasm.api/text-editor-focus shape-id)) + (schedule-ime-caret!))) on-composition-start (mf/use-fn (fn [_event] (mf/set-ref-val! composing-ref true) + (mf/set-ref-val! composition-text-ref "") + (mf/set-ref-val! synced-offset-ref nil) + (mf/set-ref-val! composition-base-ref + (let [node (mf/ref-val contenteditable-ref)] + (or (text-before-selection node) + (count (.-textContent node))))) ;; IME composition supplies its own text; drop any pending caret style. (text-editor/clear-pending-caret-styles!) (text-editor/text-editor-composition-start))) on-composition-update (mf/use-fn + (mf/deps shape-id) (fn [event] - ;; IME cancel (e.g. Escape on Linux ibus-mozc) fires compositionupdate - ;; with an empty string; that must reach WASM to clear the preview text. + ;; IME cancel (e.g. Escape in ibus-mozc) fires compositionupdate with + ;; an empty string; WASM needs it to clear the preview. + ;; + ;; The browser owns the capture surface mid-composition, so this + ;; handler must not touch it: no clearing, no style writes, no store + ;; dispatch. A store sync re-renders this component, and that makes + ;; a real IME commit the pending kana and restart (typing "ni" + ;; commits ん then い, not に). WASM keeps the preview in its own + ;; state; the store syncs on compositionend. Only the surface wrapper + ;; moves, so the IME window follows the WASM caret. + ;; + ;; The caret goes to the end of the changed part: a typed kana, a + ;; converted word, or a candidate picked in any clause. Where it + ;; tracks the IME cursor instead, `on-input` places it once the DOM + ;; selection holds that cursor. (let [data (.-data event)] (when (some? data) - (text-editor/text-editor-composition-update data) - (sync-wasm-text-editor-content!) - (wasm.api/request-render-preserving-target "text-composition")) - (reset-input-node (mf/ref-val contenteditable-ref))))) + (let [previous (mf/ref-val composition-text-ref)] + (mf/set-ref-val! composition-text-ref data) + (mf/set-ref-val! synced-offset-ref nil) + (text-editor/text-editor-composition-update data) + ;; The preview never reaches the store, so request its + ;; fallback faces here or it shows missing glyphs until commit. + (wasm.api/load-composition-fonts! shape-id data) + (when-not follow-ime-cursor? + (place-composition-caret! (changed-span-end previous data))) + (wasm.api/request-render-preserving-target "text-composition")))))) on-composition-end (mf/use-fn @@ -277,7 +565,9 @@ (wasm.api/finish-text-editor-composition data) (sync-wasm-text-editor-content!) (wasm.api/request-render-preserving-target "text-composition")) - (reset-input-node (mf/ref-val contenteditable-ref)))) + (reset-input-node (mf/ref-val contenteditable-ref)) + ;; Move the surface onto the caret, now past the committed text. + (schedule-ime-caret!))) on-paste (mf/use-fn @@ -286,7 +576,8 @@ (when-let [data (.-clipboardData event)] (st/emit! (dwt/v3-paste (.getData data "text/html") (.getData data "text/plain")))) - (reset-input-node (mf/ref-val contenteditable-ref)))) + (reset-input-node (mf/ref-val contenteditable-ref)) + (schedule-ime-caret!))) on-copy (mf/use-fn @@ -306,7 +597,8 @@ (text-editor/text-editor-delete-backward) (sync-wasm-text-editor-content!) (wasm.api/request-render-preserving-target "text-cut")) - (reset-input-node (mf/ref-val contenteditable-ref))))) + (reset-input-node (mf/ref-val contenteditable-ref)) + (schedule-ime-caret!)))) on-key-down (mf/use-fn @@ -314,6 +606,10 @@ (mf/set-ref-val! ime-key-ref (or (mf/ref-val composing-ref) (composing-event? event))) + ;; IME keydowns (keyCode 229) must not touch the surface: the IME + ;; already owns it, and even a style write before compositionstart + ;; makes ibus abort and commit. The surface already sits on the + ;; caret (see `update-ime-caret!`). (when (and (text-editor/text-editor-has-focus?) (not (mf/ref-val composing-ref)) (not (composing-event? event))) @@ -417,10 +713,12 @@ (text-editor/text-editor-move-cursor 5 ctrl? shift?) (wasm.api/render-text-editor-overlay!)) - ;; Let contenteditable handle text input via on-input, which - ;; needs an editable browser selection. - :else - (ensure-input-caret (mf/ref-val contenteditable-ref))))))) + ;; Let contenteditable handle text input via on-input with an editable selection. + :else (ensure-input-caret (mf/ref-val contenteditable-ref))) + ;; A handled key may move the caret; keep the surface on it for + ;; the next IME sequence. Character keys also reschedule from + ;; on-input after the insert. + (schedule-ime-caret!))))) ;; Native `beforeinput` listener (see the use-effect that registers it). ;; We use the native event, not React's synthetic `onBeforeInput`, because @@ -459,6 +757,9 @@ (let [native-event (.-nativeEvent event) input-type (.-inputType native-event) data (.-data native-event)] + ;; The DOM now holds the updated composition and its IME cursor. + (when (= input-type "insertCompositionText") + (sync-composition-cursor!)) ;; Skip composition-related input events - composition-end handles those (when (and (not (composing-event? event)) (not= input-type "insertCompositionText")) @@ -481,11 +782,17 @@ ;; IMPORTANT: do NOT clear the surface here (see keep-input-alive): ;; the browser must retain the just-typed character so the macOS ;; accent menu can replace it on the next input. - (keep-input-alive (mf/ref-val contenteditable-ref)))))) + (keep-input-alive (mf/ref-val contenteditable-ref)) + (schedule-ime-caret!))))) on-pointer-down (mf/use-fn (fn [^js event] + ;; The capture surface is pointer-events:none, so focus it here. + ;; preventDefault stops mousedown from moving focus to the body, + ;; which would blur the surface. + (dom/prevent-default event) + (focus-editor!) (when-not (secondary-button? event) (let [native-event (dom/event->native-event event) off-pt (dom/get-offset-position native-event)] @@ -530,7 +837,8 @@ ;; Without a drag there is no pointer selection to close; the ;; caret is placed by `on-click`. (when dragging? - (wasm.api/render-text-editor-overlay!)))))) + (wasm.api/render-text-editor-overlay!)) + (schedule-ime-caret!))))) on-click (mf/use-fn @@ -554,7 +862,8 @@ :else (do (wasm.api/text-editor-set-cursor-from-offset off-pt) - (wasm.api/render-text-editor-overlay!))))))) + (wasm.api/render-text-editor-overlay!))) + (schedule-ime-caret!))))) on-double-click (mf/use-fn @@ -563,7 +872,8 @@ off-pt (dom/get-offset-position native-event)] (wasm.api/text-editor-select-word-boundary off-pt) (ensure-input-caret (mf/ref-val contenteditable-ref)) - (wasm.api/render-text-editor-overlay!)))) + (wasm.api/render-text-editor-overlay!) + (schedule-ime-caret!)))) on-context-menu (mf/use-fn @@ -582,7 +892,8 @@ ;; on every other path that moves it. (text-editor/clear-pending-caret-styles!) (wasm.api/text-editor-set-cursor-from-offset off-pt) - (wasm.api/render-text-editor-overlay!))) + (wasm.api/render-text-editor-overlay!) + (schedule-ime-caret!))) ;; Deferred: the dropdown closes itself on a document `contextmenu`, ;; which would close the menu this very event is opening. (ts/schedule @@ -594,19 +905,8 @@ on-focus (mf/use-fn (fn [^js _event] - (wasm.api/text-editor-focus shape-id))) - - ;; Focus the input and the WASM editor if something took the focus - ;; while the edition stays open (e.g. a workspace menu). - focus-editor! - (mf/use-fn - (mf/deps shape-id) - (fn [] - (when-let [node (mf/ref-val contenteditable-ref)] - (when (not= (.-activeElement js/document) node) - (.focus node))) - (when-not (text-editor/text-editor-has-focus?) - (wasm.api/text-editor-focus shape-id)))) + (wasm.api/text-editor-focus shape-id) + (schedule-ime-caret!))) on-blur (mf/use-fn @@ -619,10 +919,18 @@ (wasm.api/text-editor-blur)))) style #js {:pointerEvents "all" + ;; Keep pointer offsets relative to the text shape when + ;; the foreignObject extends left for the macOS IME anchor. + :position "relative" + :left (dm/str ime-left "px") + :width (dm/str ime-width "px") "--editor-container-width" (dm/str width "px") "--editor-container-height" (dm/str height "px") "--fallback-families" (if (seq fallback-families) (dm/str (str/join ", " fallback-families)) "sourcesanspro")}] + ;; Keep the caret-translation origin in sync with the current render. + (mf/set-ref-val! origin-ref {:x x :y y :width ime-width :vertical? vertical?}) + ;; The workspace leaves undo to the active editor. Handle the same events ;; for keyboard shortcuts, native history input and workspace menu actions. ;; A menu takes the focus and blurs the WASM editor, which then ignores the @@ -638,7 +946,8 @@ (focus-editor!) (st/emit! (dwt/v3-restore-history (if (ptk/type? ::dwu/undo event) :undo :redo))) - (reset-input-node (mf/ref-val contenteditable-ref))))))] + (reset-input-node (mf/ref-val contenteditable-ref)) + (schedule-ime-caret!)))))] #(rx/dispose! subscription)))) ;; Exit on Escape via a document key-up listener (like v2). On key-down the trailing @@ -660,6 +969,14 @@ (.addEventListener js/document "keyup" on-key-up) #(.removeEventListener js/document "keyup" on-key-up)))) + ;; The IME cursor can move inside the composition without changing its + ;; text (e.g. switching clauses), which only shows as a selection change. + (mf/use-effect + (mf/deps sync-composition-cursor!) + (fn [] + (.addEventListener js/document "selectionchange" sync-composition-cursor!) + #(.removeEventListener js/document "selectionchange" sync-composition-cursor!))) + ;; Register the native `beforeinput` listener. React's synthetic ;; `onBeforeInput` does not expose `getTargetRanges()`, even with ;; nativeEvent (it's fully synthetic, composed of other two events). @@ -680,9 +997,10 @@ ;; Group the whole editing session (edits, reflow resizes, finalize) into a single ;; undo entry. Nested transactions (e.g. style shortcuts) are ref-counted and fold in. (st/emit! (dwu/start-undo-transaction shape-id :timeout nil)) - (when-let [node (mf/ref-val contenteditable-ref)] - ;; Focus and select all text on mount (this will trigger on-focus) - (.focus node) + (when (some? (mf/ref-val contenteditable-ref)) + ;; Focus and select all text on mount. `focus-editor!` establishes the + ;; WASM focus explicitly; select-all is a no-op without it. + (focus-editor!) (text-editor/text-editor-select-all) (wasm.api/request-render-preserving-target "text-editor-select-all-on-mount")) ;; On unmount, finalize the editor content and then dispose the WASM editor. @@ -721,40 +1039,46 @@ [:g.text-editor {:clip-path (dm/fmt "url(#%)" clip-id) :transform (dm/str transform) :data-testid "text-editor"} + [:> ime-debug/overlay* {:input-ref contenteditable-ref + :composing-ref composing-ref + :composition-text-ref composition-text-ref + :composition-base-ref composition-base-ref}] [:defs [:clipPath {:id clip-id} [:rect {:x x :y y :width width :height height}]]] - [:foreignObject {:x x :y y :width width :height height} + [:foreignObject {:x (- x ime-left) :y y :width (+ ime-width ime-left) :height height} [:div {:on-click on-click :on-double-click on-double-click :on-pointer-down on-pointer-down :on-pointer-move on-pointer-move :on-pointer-up on-pointer-up :on-context-menu on-context-menu - :class (stl/css :text-editor) + ;; The hover cursor lives here: the capture surface is pointer-events:none. + :class (dm/str (cur/get-text (:rotation shape) vertical?) + " " + (stl/css :text-editor)) :style style} - [:div - {:ref contenteditable-ref - :contentEditable true - :suppressContentEditableWarning true - ;; The surface retains typed text between keystrokes (see - ;; keep-input-alive), so disable text assistance that would otherwise - ;; rewrite that retained text and desync the WASM editor. - ;; NOTE: this was already not working in v1/v2 - :spellCheck false - :autoCorrect "off" - :autoCapitalize "off" - :on-composition-start on-composition-start - :on-composition-update on-composition-update - :on-composition-end on-composition-end - :on-key-down on-key-down - :on-input on-input - :on-paste on-paste - :on-copy on-copy - :on-cut on-cut - :on-focus on-focus - :on-blur on-blur - :id "text-editor-wasm-input" - :class (input-surface-class (:rotation shape)) - :data-testid "text-editor-container"}]]]])) + [:div {:ref wrapper-ref + :class (stl/css :ime-anchor)} + [:div + {:ref contenteditable-ref + :contentEditable true + :suppressContentEditableWarning true + ;; Text assistance would rewrite the retained text (see keep-input-alive). + :spellCheck false + :autoCorrect "off" + :autoCapitalize "off" + :on-composition-start on-composition-start + :on-composition-update on-composition-update + :on-composition-end on-composition-end + :on-key-down on-key-down + :on-input on-input + :on-paste on-paste + :on-copy on-copy + :on-cut on-cut + :on-focus on-focus + :on-blur on-blur + :id "text-editor-wasm-input" + :class (input-surface-class) + :data-testid "text-editor-container"}]]]]])) diff --git a/frontend/src/app/main/ui/workspace/shapes/text/v3_editor.scss b/frontend/src/app/main/ui/workspace/shapes/text/v3_editor.scss index 5659b0646e..a28d4ddcea 100644 --- a/frontend/src/app/main/ui/workspace/shapes/text/v3_editor.scss +++ b/frontend/src/app/main/ui/workspace/shapes/text/v3_editor.scss @@ -9,4 +9,20 @@ opacity: 0; overflow: hidden; white-space: pre; + + // The surface is positioned on the caret while idle, so clicks must always + // hit the outer overlay (keeping offset coordinates stable); focus is given + // to the surface programmatically from the overlay's pointer-down. + pointer-events: none; +} + +// Positioned wrapper of the capture surface, translated during an IME +// composition so the candidate window follows the WASM caret. +.ime-anchor { + position: absolute; + top: 0; + left: 0; + width: 100%; + height: 100%; + pointer-events: none; } diff --git a/frontend/src/app/main/ui/workspace/sidebar/debug.cljs b/frontend/src/app/main/ui/workspace/sidebar/debug.cljs index 51435e5779..3a6ab4b60d 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/debug.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/debug.cljs @@ -22,6 +22,7 @@ "Debug options that don't require a page reload to take effect. These options are handled reactively via okulary subscriptions." #{:shape-panel + :ime :show-ids :show-touched :components-debugger}) diff --git a/frontend/src/app/main/ui/workspace/viewport/selection.cljs b/frontend/src/app/main/ui/workspace/viewport/selection.cljs index a4c378fa8d..d064e717ec 100644 --- a/frontend/src/app/main/ui/workspace/viewport/selection.cljs +++ b/frontend/src/app/main/ui/workspace/viewport/selection.cljs @@ -31,6 +31,7 @@ [app.util.debug :as dbg] [app.util.dom :as dom] [app.util.object :as obj] + [app.util.text.writing-mode :as wm] [potok.v2.core :as ptk] [rumext.v2 :as mf])) @@ -563,23 +564,31 @@ (dom/stop-propagation event) (let [target (dom/get-current-target event) position (-> (dom/get-data target "position") - (keyword))] + (keyword)) + horizontal-resize? (contains? #{:right :left} position) + vertical-resize? (contains? #{:top :bottom} position) + vertical-text? (wm/vertical-text-content? (get shape :content)) + wrap-axis-resize? (if vertical-text? + vertical-resize? + horizontal-resize?) + resize-direction (if horizontal-resize? :horizontal :vertical)] (cond - ;; If text and in auto-width and the resize is horizontal, switch to auto-height and mark direction + ;; Resizing auto-width along the wrap axis (vertical under + ;; vertical writing) switches to auto-height. (and (= shape-type :text) (= grow-type :auto-width) - (or (= position :right) (= position :left))) - (st/emit! (dwsh/update-shapes [shape-id] #(-> % (assoc :grow-type :auto-height) (assoc :last-resize-direction :horizontal)))) - ;; If text and in auto-height and the resize is horizontal, mark direction but do not change grow-type + wrap-axis-resize?) + (st/emit! (dwsh/update-shapes [shape-id] + #(assoc % + :grow-type :auto-height + :last-resize-direction resize-direction))) + (and (= shape-type :text) (= grow-type :auto-height) - (or (= position :right) (= position :left))) - (st/emit! (dwsh/update-shapes [shape-id] #(assoc % :last-resize-direction :horizontal))) - ;; If text and in auto-height and the resize is vertical, mark direction - (and (= shape-type :text) - (= grow-type :auto-height) - (or (= position :top) (= position :bottom))) - (st/emit! (dwsh/update-shapes [shape-id] #(assoc % :last-resize-direction :vertical))) + (or horizontal-resize? vertical-resize?)) + (st/emit! (dwsh/update-shapes [shape-id] + #(assoc % :last-resize-direction resize-direction))) + :else nil) (st/emit! (dw/start-resize position #{shape-id} shape)))))) diff --git a/frontend/src/app/render_wasm/text_editor.cljs b/frontend/src/app/render_wasm/text_editor.cljs index e0a32e5371..2cf0de85ee 100644 --- a/frontend/src/app/render_wasm/text_editor.cljs +++ b/frontend/src/app/render_wasm/text_editor.cljs @@ -14,6 +14,7 @@ [app.common.render-wasm.wasm :as wasm] [app.common.types.fills.impl :as types.fills.impl] [app.common.types.text :as txt] + [app.common.types.text.cjk-layout :as cjk] [app.common.uuid :as uuid] [app.main.fonts :as main-fonts] ;; Required for side effects: binds the generated enums. @@ -21,7 +22,8 @@ [app.render-wasm.api.fonts :as fonts] [app.render-wasm.text-editor-history :as history] [app.util.color :as uc] - [app.util.dom :as dom])) + [app.util.dom :as dom] + [cuerdas.core :as str])) (def multiple-state-multiple (sr/translate-multiple-state :multiple)) @@ -267,7 +269,7 @@ caret/selection overlay on top, submitting one atomic frame. Pixel identical to the last full render at any zoom, so the blink does not flash." [] - (when wasm/context-initialized? + (when (wasm/ready?) (h/call wasm/internal-module "_text_editor_render_caret"))) (defn text-editor-poll-event @@ -389,6 +391,42 @@ (mem/free) result))))) +(defn text-editor-get-cursor-rect + "Read the caret rectangle from the WASM editor. + + Coordinates are in the shape's local selrect space (the same space as the + text overlay's foreignObject). Returns a map with :x :y :width :height, or + nil when there is no active caret." + [] + (when (wasm/ready?) + (let [ptr (h/call wasm/internal-module "_text_editor_get_cursor_rect")] + (when (and ptr (not (zero? ptr))) + (let [heap-f32 (mem/get-heap-f32) + offset (mem/->offset-32 ptr) + x (aget heap-f32 offset) + y (aget heap-f32 (+ offset 1)) + width (aget heap-f32 (+ offset 2)) + height (aget heap-f32 (+ offset 3))] + (mem/free) + {:x x :y y :width width :height height}))))) + +(defn text-editor-get-selection-rect + "Read the box around the selected text from the WASM editor, in page + coordinates: a map with :x :y :width :height that encloses the text as + drawn, rotated or not. Nil when the selection is collapsed." + [] + (when (wasm/ready?) + (let [ptr (h/call wasm/internal-module "_text_editor_get_selection_rect")] + (when (and ptr (not (zero? ptr))) + (let [heap-f32 (mem/get-heap-f32) + offset (mem/->offset-32 ptr) + x (aget heap-f32 offset) + y (aget heap-f32 (+ offset 1)) + width (aget heap-f32 (+ offset 2)) + height (aget heap-f32 (+ offset 3))] + (mem/free) + {:x x :y y :width width :height height}))))) + (defn text-editor-encode-text-pre [text] (when (and (not (empty? text)) @@ -419,6 +457,12 @@ (h/call wasm/internal-module "_text_editor_composition_update") (text-editor-encode-text-post text))) +(defn text-editor-set-composition-cursor + "Place the WASM caret `offset` characters into the composition preview." + [offset] + (when (wasm/ready?) + (h/call wasm/internal-module "_text_editor_set_composition_cursor" offset))) + (defn text-editor-composition-end [text] (when (wasm/ready?) @@ -679,7 +723,10 @@ styling is taken from there; a span WASM never saw falls back to its position and then to the last existing span. - exported vector of paragraphs, each a vector of `{:p 0 :s 0 :t \"text\"}` + `:pp` tracks paragraph attrs independently of span styling, including + when a merge removes all the surviving paragraph's original characters. + + exported vector of paragraphs, each a vector of `{:pp 0 :p 0 :s 0 :t \"text\"}` content existing Penpot content map (root -> paragraph-set -> …)" [content exported] (let [para-set (first (get content :children)) @@ -690,7 +737,8 @@ styling-para (fn [para-idx spans] - (or (get orig-paras (get (first spans) :p)) + (or (get orig-paras (get (first spans) :pp)) + (get orig-paras (get (first spans) :p)) (get orig-paras para-idx) last-orig-para)) @@ -791,46 +839,43 @@ (normalize-selection {:anchor-para (:para before) :anchor-offset (:offset before) :focus-para (:para after) :focus-offset (:offset after)}))) -(defn apply-attrs-to-paragraph - "Apply `styles` (attrs map, or a fn per span) within [sel-start, sel-end), splitting spans." - [para sel-start sel-end styles] - (let [spans (:children para) - - result (loop [spans spans - pos 0 - acc []] - (if (empty? spans) - acc - (let [span (first spans) - text (:text span) - span-len (count text) - span-end (+ pos span-len) - ol-start (max pos sel-start) - ol-end (min span-end sel-end) - ;; An empty span has no range to overlap, but an empty - ;; line inside the selection still has to be restyled. - has-overlap? (or (< ol-start ol-end) - (and (zero? span-len) - (<= sel-start pos sel-end)))] - (if (not has-overlap?) - (recur (rest spans) span-end (conj acc span)) - (let [before (when (> ol-start pos) - (assoc span :text (subs text 0 (- ol-start pos)))) - selected (-> (if (fn? styles) - (styles span) - (merge span styles)) - (assoc :text (subs text (- ol-start pos) (- ol-end pos)))) - after (when (< ol-end span-end) - (assoc span :text (subs text (- ol-end pos))))] - (recur (rest spans) span-end - (-> acc - (into (keep identity [before selected after])))))))))] - (assoc para :children result))) - (defn- para-char-count [para] (apply + (map (fn [span] (count (:text span))) (:children para)))) +(defn- span-cjk-styles + "CJK span attributes with their defaults filled in. Every selection + snapshot carries all of them, so moving onto an unstyled span resets the + controls." + [span] + (reduce-kv + (fn [styles attr default] + (assoc styles attr (or (get span attr) default))) + {} + cjk/span-attr-defaults)) + +(defn- merge-selection-styles + [result styles] + (reduce-kv + (fn [result attr value] + (update result attr #(if (or (nil? %) (= % value)) value :multiple))) + result + styles)) + +(defn- span-at-offset + "Span the WASM editor uses for a collapsed caret. At a span boundary the + preceding span wins, as in find_text_span_at_offset in render-wasm." + [paragraph offset] + (let [spans (:children paragraph)] + (loop [remaining-spans spans + accumulated 0] + (if-let [span (first remaining-spans)] + (let [span-end (+ accumulated (count (:text span)))] + (if (<= offset span-end) + span + (recur (rest remaining-spans) span-end))) + (last spans))))) + (defn- paragraph-selected-spans "Return the spans of `para` that overlap the [sel-start, sel-end) char range." [para sel-start sel-end] @@ -844,20 +889,200 @@ overlap? (< (max pos sel-start) (min span-end sel-end))] (recur (rest spans) span-end (cond-> acc overlap? (conj span))))))) +(defn- selected-spans + "Spans of `paragraphs` that overlap a normalized selection range." + [paragraphs {:keys [start-para start-offset end-para end-offset]}] + (mapcat (fn [idx] + (when-let [para (nth paragraphs idx nil)] + (paragraph-selected-spans para + (if (= idx start-para) start-offset 0) + (if (= idx end-para) end-offset (para-char-count para))))) + (range start-para (inc end-para)))) + +(defn- span-bounds + "Char range `[start end)` of each span of `paragraph`." + [paragraph] + (->> (:children paragraph) + (reductions (fn [[_ end] span] [end (+ end (count (:text span)))]) [0 0]) + (rest) + (vec))) + +(defn- annotation-bases + "Char ranges `[start end)` of the annotated text of `paragraph`: each + reading (`cjk/ruby-groups`, which can cross spans) and each warichu note." + [paragraph] + (let [spans (:children paragraph) + bounds (span-bounds paragraph)] + (into (mapv (fn [[start end]] [(first (nth bounds start)) (second (nth bounds (dec end)))]) + (cjk/ruby-groups spans)) + (keep-indexed (fn [index span] + (when (= "warichu" (:warichu span)) (nth bounds index)))) + spans))) + +(defn reading-selection? + "True for text that can take one reading: characters of one paragraph + without a reading or warichu note, whatever their styles (the reading + joins their spans into one base), or the whole base of an existing one." + [content selection] + (boolean + (when (and content selection) + (let [{:keys [start-para start-offset end-para end-offset]} (normalize-selection selection) + paragraph (get-in content [:children 0 :children start-para])] + (and (= start-para end-para) + (some? paragraph) + (< start-offset end-offset) + (or (not-any? cjk/annotated-span? + (paragraph-selected-spans paragraph start-offset end-offset)) + (some #(= [start-offset end-offset] %) (annotation-bases paragraph)))))))) + +(defn single-span-selection? + "True for characters of one span, including the whole of an annotated one: + the text a warichu note can take, as it never crosses spans." + [content selection] + (boolean + (when (and content selection) + (let [{:keys [start-para start-offset end-para end-offset] :as range} + (normalize-selection selection) + paragraphs (-> content :children first :children) + spans (selected-spans paragraphs range) + span (first spans)] + (and (= start-para end-para) + (= 1 (count spans)) + (or (not (cjk/annotated-span? span)) + (= (- end-offset start-offset) (count (:text span))))))))) + +(defn zhuyin-selection? + "True when the reading selection is exactly one Han base." + [content selection] + (and (reading-selection? content selection) + (let [{:keys [start-para start-offset end-offset]} (normalize-selection selection) + paragraph (get-in content [:children 0 :children start-para]) + text (apply str (map :text (:children paragraph)))] + (cjk/zhuyin-base? (subs text start-offset end-offset))))) + +(defn selection-cjk-styles + "CJK span styles of a WASM selection, read from the cached content. + A range with differing values reports :multiple; a caret reports its + span's style." + [content selection] + (when (and content selection) + (let [{:keys [start-para start-offset end-para end-offset] :as sel-range} + (normalize-selection selection) + paragraphs (-> content :children first :children) + collapsed? (and (= start-para end-para) + (= start-offset end-offset)) + spans + (if collapsed? + (some-> (get paragraphs start-para) + (span-at-offset start-offset) + vector) + (selected-spans paragraphs sel-range))] + (when (seq spans) + (reduce (fn [result span] + (merge-selection-styles result (span-cjk-styles span))) + {} + spans))))) + +(defn selection-paragraph-styles + "Language attrs of the selected paragraphs, including unset and mixed values." + [content selection] + (when (and content selection) + (let [{:keys [start-para end-para]} (normalize-selection selection) + paragraphs (-> content :children first :children)] + (reduce + (fn [result paragraph] + (reduce + (fn [result attr] + (let [value (get paragraph attr)] + (assoc result attr + (if (and (contains? result attr) + (not= (get result attr) value)) + :multiple + value)))) + result + cjk/language-paragraph-attrs)) + {} + (subvec paragraphs start-para (inc end-para)))))) + +(defn text-editor-get-current-cjk-styles + "CJK span styles and paragraph language attrs of the active editor selection." + [] + (when (wasm/ready?) + (let [shape-id (text-editor-get-active-shape-id) + selection (text-editor-get-selection) + content (get-cached-content shape-id)] + (merge (selection-cjk-styles content selection) + (selection-paragraph-styles content selection))))) + +(defn- whole-base-span? + "True for an annotated span that a decoration edit must cover whole: a + warichu note, or a reading without `:ruby-group`, which splitting would + repeat over each piece. A grouped reading keeps one base when split." + [span] + (and (cjk/annotated-span? span) + (or (= "warichu" (:warichu span)) + (str/blank? (:ruby-group span))))) + +(defn- annotation-style-range + [paragraph start end styles] + (if (and (< start end) + (map? styles) + (some #(contains? styles %) [:text-emphasis :text-emphasis-position + :text-decoration :text-underline-position :annotation-clearance])) + (let [[start end] + (reduce (fn [[from to offset] span] + (let [span-end (+ offset (count (:text span)))] + (if (and (< offset end) (< start span-end) (whole-base-span? span)) + [(min from offset) (max to span-end) span-end] + [from to span-end]))) + [start end 0] + (:children paragraph))] + [start end]) + [start end])) + +(defn apply-attrs-to-paragraph + "Apply `styles` (attrs map, or a fn per span) within [sel-start, sel-end), + splitting spans and joining back the ones left with their neighbour's style." + [para sel-start sel-end styles] + (let [[sel-start sel-end] (annotation-style-range para sel-start sel-end styles) + spans (:children para) + + result (loop [spans spans + pos 0 + acc []] + (if (empty? spans) + acc + (let [span (first spans) + text (:text span) + span-len (count text) + span-end (+ pos span-len) + ol-start (max pos sel-start) + ol-end (min span-end sel-end) + ;; An empty span has no range to overlap, but an empty + ;; line inside the selection still has to be restyled. + has-overlap? (or (< ol-start ol-end) + (and (zero? span-len) + (<= sel-start pos sel-end)))] + (if (not has-overlap?) + (recur (rest spans) span-end (conj acc span)) + (let [before (when (> ol-start pos) + (assoc span :text (subs text 0 (- ol-start pos)))) + selected (-> (if (fn? styles) + (styles span) + (merge span styles)) + (assoc :text (subs text (- ol-start pos) (- ol-end pos)))) + after (when (< ol-end span-end) + (assoc span :text (subs text (- ol-end pos))))] + (recur (rest spans) span-end + (-> acc + (into (keep identity [before selected after])))))))))] + (assoc para :children (txt/merge-equal-spans result)))) + (defn selection-fills "The selection's fills: shared vector if all spans match, `:multiple` if not, nil if empty." - [content {:keys [start-para start-offset end-para end-offset]}] - (let [paragraphs (:children (first (:children content))) - selected (mapcat (fn [idx para] - (cond - (or (< idx start-para) (> idx end-para)) nil - (= start-para end-para) (paragraph-selected-spans para start-offset end-offset) - (= idx start-para) (paragraph-selected-spans para start-offset (para-char-count para)) - (= idx end-para) (paragraph-selected-spans para 0 end-offset) - :else (paragraph-selected-spans para 0 (para-char-count para)))) - (range (count paragraphs)) - paragraphs) - fills-set (into #{} (map :fills) selected)] + [content selection] + (let [selected (selected-spans (:children (first (:children content))) selection) + fills-set (into #{} (map :fills) selected)] (cond (empty? selected) nil (= 1 (count fills-set)) (first fills-set) @@ -1013,11 +1238,19 @@ (= k :text-decoration) [k "none"]))) styles))) +(defn- with-ruby-group + "A reading set on a selection joins its spans into one base with a fresh + `:ruby-group`, whatever their styles; removing the reading drops it." + [styles] + (if (and (map? styles) (contains? styles :ruby) (not (contains? styles :ruby-group))) + (assoc styles :ruby-group (if (str/blank? (:ruby styles)) "" (str (uuid/next)))) + styles)) + (defn apply-styles-to-selection "Apply `styles` (attrs map, or a fn per span) to the selected spans; `:with-fills?` also returns `:fills`." [styles use-shape-fn set-shape-text-content-fn & [{:keys [with-fills?]}]] (when (wasm/ready?) - (let [styles (clean-styles styles) + (let [styles (-> styles clean-styles with-ruby-group) shape-id (text-editor-get-active-shape-id) selection (text-editor-get-selection)] @@ -1059,26 +1292,40 @@ {:shape-id shape-id :content new-content}))))) +(defn apply-paragraph-attrs-to-range + "Apply paragraph attrs to the paragraphs `selection` touches (a caret + touches one). Whole-text attrs (writing mode, orientation, language) go + to every paragraph; a nil value removes them." + [content selection attrs] + (let [{:keys [start-para end-para]} (normalize-selection selection) + whole-attrs (select-keys attrs cjk/whole-text-paragraph-attrs) + range-attrs (apply dissoc attrs cjk/whole-text-paragraph-attrs) + apply-whole (fn [para] + (reduce-kv (fn [para attr value] + (if (some? value) + (assoc para attr value) + (dissoc para attr))) + para + whole-attrs)) + paragraph-set (first (:children content)) + new-paragraphs (into [] + (map-indexed (fn [idx para] + (cond-> (apply-whole para) + (<= start-para idx end-para) + (merge range-attrs)))) + (:children paragraph-set))] + (assoc content :children [(assoc paragraph-set :children new-paragraphs)]))) + (defn apply-paragraph-attrs-to-selection - "Apply paragraph level attrs (text-align, text-direction) to the whole - paragraphs the editor selection touches; a collapsed caret means just the one - it sits in." + "Apply paragraph level attrs to the active editor selection (see + `apply-paragraph-attrs-to-range`)." [attrs use-shape-fn set-shape-text-content-fn] (when (wasm/ready?) (let [shape-id (text-editor-get-active-shape-id) selection (text-editor-get-selection)] (when (and shape-id selection) (when-let [content (get-cached-content shape-id)] - (let [{:keys [start-para end-para]} (normalize-selection selection) - paragraph-set (first (:children content)) - new-paragraphs (into [] - (map-indexed (fn [idx para] - (if (<= start-para idx end-para) - (merge para attrs) - para))) - (:children paragraph-set)) - new-content (assoc content :children - [(assoc paragraph-set :children new-paragraphs)])] + (let [new-content (apply-paragraph-attrs-to-range content selection attrs)] (update-cached-content! shape-id new-content) (use-shape-fn shape-id) (set-shape-text-content-fn shape-id new-content) diff --git a/frontend/src/app/util/debug.cljs b/frontend/src/app/util/debug.cljs index 3340e040e2..045ced95a2 100644 --- a/frontend/src/app/util/debug.cljs +++ b/frontend/src/app/util/debug.cljs @@ -104,6 +104,12 @@ ;; Show viewbox. :wasm-viewbox + ;; Draw the jlreq-style character-frame grid over Japanese vertical text. + :wasm-text-grid + + ;; Show browser composition bounds and the WASM/DOM carets during text input. + :ime + ;; Makes the GL context to fail on initialization. :wasm-gl-context-init-error diff --git a/frontend/src/debug.cljs b/frontend/src/debug.cljs index 7a1abb89af..4c2af2b90c 100644 --- a/frontend/src/debug.cljs +++ b/frontend/src/debug.cljs @@ -92,7 +92,8 @@ (set! st/*debug-events-time* true) nil) - (js* "app.main.reinit()")) + (when-not (= option :ime) + (js* "app.main.reinit()"))) (defn disable! [option] @@ -105,7 +106,8 @@ (set! st/*debug-events-time* false) nil) - (js* "app.main.reinit()")) + (when-not (= option :ime) + (js* "app.main.reinit()"))) (defn ^:export toggle-debug [name] @@ -1153,4 +1155,4 @@ (dw/delete-shapes page-id ids)) unrelated-items-by-page) [(dwu/commit-undo-transaction undo-id)])))) - nil)) \ No newline at end of file + nil)) diff --git a/frontend/test/frontend_tests/data/workspace_texts_test.cljs b/frontend/test/frontend_tests/data/workspace_texts_test.cljs index d2772ebd36..84e2fe137a 100644 --- a/frontend/test/frontend_tests/data/workspace_texts_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_texts_test.cljs @@ -6,21 +6,220 @@ (ns frontend-tests.data.workspace-texts-test (:require + [app.common.geom.point :as gpt] [app.common.geom.rect :as grc] + [app.common.geom.shapes :as gsh] [app.common.test-helpers.files :as cthf] [app.common.test-helpers.shapes :as cths] [app.common.types.modifiers :as ctm] [app.common.types.shape :as cts] [app.common.types.text :as txt] [app.common.uuid :as uuid] + [app.main.data.workspace.drawing.common :as drawing] + [app.main.data.workspace.modifiers :as dwm] + [app.main.data.workspace.shapes :as dwsh] [app.main.data.workspace.texts :as dwt] [app.main.data.workspace.texts-events :as dwte] + [app.main.data.workspace.wasm-text :as dwwt] + [app.main.ui.shapes.text.styles :as text.styles] [app.main.ui.workspace.shapes.text.viewport-texts-html :as vth] + [app.util.text.geometry :as text.geom] [beicon.v2.core :as rx] [cljs.test :as t :include-macros true] [frontend-tests.helpers.state :as ths] [potok.v2.core :as ptk])) +(defn- writing-mode-content + "Japanese text content with `writing-mode`: vertical writing needs a CJK + language." + [writing-mode] + {:children [{:children [{:writing-mode writing-mode :text-language "ja"}]}]}) + +(t/deftest vertical-auto-height-grows-width + (with-redefs [cts/wasm-enabled? true] + (let [selrect {:width 100 :height 200} + dimension {:width 240 :height 360} + content (writing-mode-content "vertical-rl")] + (t/is (= {:width 240 :height 200} + (dwwt/resolve-text-size selrect :auto-height content dimension)))))) + +(t/deftest vertical-writing-without-language-grows-as-horizontal + (with-redefs [cts/wasm-enabled? true] + (let [content {:children [{:children [{:writing-mode "vertical-rl"}]}]}] + (t/is (= {:width 100 :height 360} + (dwwt/resolve-text-size {:width 100 :height 200} :auto-height + content {:width 240 :height 360})))))) + +(t/deftest horizontal-auto-height-grows-height + (let [selrect {:width 100 :height 200} + dimension {:width 240 :height 360} + content (writing-mode-content "horizontal-tb")] + (t/is (= {:width 100 :height 360} + (dwwt/resolve-text-size selrect :auto-height content dimension))))) + +(t/deftest vertical-auto-grow-keeps-the-block-anchor + (with-redefs [cts/wasm-enabled? true] + (doseq [[align expected-x] [["top" -90] ["center" -40] ["bottom" 10]]] + (let [shape (cts/setup-shape + {:type :text :x 10 :y 20 :width 100 :height 200 + :grow-type :auto-height + :content (assoc (writing-mode-content "vertical-rl") :vertical-align align)}) + resized (with-redefs [dwwt/get-wasm-text-new-size + (fn ([_] {:width 200 :height 200}) + ([_ _] {:width 200 :height 200}))] + (let [modifiers (dwwt/resize-wasm-text-modifiers shape)] + (gsh/transform-shape shape (get-in modifiers [(:id shape) :modifiers]))))] + (t/is (= expected-x (:x resized)) align) + (t/is (= 200 (:height resized)) align))))) + +(t/deftest live-vertical-bounds-grow-left-and-keep-the-wrap-height + (with-redefs [cts/wasm-enabled? true] + (let [selrect {:x 10 :y 20 :width 100 :height 200} + content (writing-mode-content "vertical-rl")] + (t/is (= {:x -130 :y 20 :width 240 :height 200} + (text.geom/live-rect selrect :auto-height content {:width 240 :height 360}))) + (t/is (= {:x -130 :y 20 :width 240 :height 360} + (text.geom/live-rect selrect :auto-width content {:width 240 :height 360}))) + (t/is (= selrect (text.geom/live-rect selrect :fixed content {:width 240 :height 360}))) + (t/is (= selrect (text.geom/live-rect selrect :auto-width content nil)))))) + +(t/deftest live-horizontal-bounds-grow-down-and-keep-the-wrap-width + (let [selrect {:x 10 :y 20 :width 100 :height 200} + content (writing-mode-content "horizontal-tb")] + (t/is (= {:x 10 :y 20 :width 100 :height 360} + (text.geom/live-rect selrect :auto-height content {:width 240 :height 360}))) + (t/is (= {:x 10 :y 20 :width 240 :height 360} + (text.geom/live-rect selrect :auto-width content {:width 240 :height 360}))))) + +(t/deftest live-bounds-keep-committed-axes-when-the-measure-is-missing + (let [selrect {:x 10 :y 20 :width 100 :height 200} + content (writing-mode-content "horizontal-tb")] + (t/is (= selrect (text.geom/live-rect selrect :auto-width content {:width 0 :height 0}))) + (t/is (= selrect (text.geom/live-rect selrect :auto-width content {:width nil :height nil}))) + (t/is (= {:x 10 :y 20 :width 100 :height 360} + (text.geom/live-rect selrect :auto-width content {:width 0 :height 360}))))) + +(t/deftest vertical-auto-grow-keeps-the-anchor-after-rotation + (with-redefs [cts/wasm-enabled? true] + (let [base (cts/setup-shape + {:type :text :x 10 :y 20 :width 100 :height 200 + :grow-type :auto-height :content (writing-mode-content "vertical-rl")}) + shape (gsh/transform-shape base (ctm/rotation-modifiers base (gpt/point 60 120) 45)) + resized (with-redefs [dwwt/get-wasm-text-new-size + (fn ([_] {:width 200 :height 200}) + ([_ _] {:width 200 :height 200}))] + (let [modifiers (dwwt/resize-wasm-text-modifiers shape)] + (gsh/transform-shape shape (get-in modifiers [(:id shape) :modifiers]))))] + (t/is (gpt/almost-zero? (gpt/subtract (nth (:points shape) 1) (nth (:points resized) 1))))))) + +(t/deftest clicking-vertical-text-anchors-its-right-edge-at-the-click + (with-redefs [cts/wasm-enabled? true] + (let [shape (drawing/click-draw-text + (cts/setup-shape {:type :text :x 100 :y 50 :width 1 :height 1 + :content (writing-mode-content "vertical-rl")}))] + (t/is (= 100 (+ (:x shape) (:width shape)))) + (t/is (= 50 (:y shape))) + (t/is (= [4 17] [(:width shape) (:height shape)])) + (t/is (= :auto-width (:grow-type shape)))))) + +(t/deftest clicking-horizontal-text-keeps-its-left-edge-at-the-click + (let [shape (drawing/click-draw-text + (cts/setup-shape {:type :text :x 100 :y 50 :width 1 :height 1 + :content (writing-mode-content "horizontal-tb")}))] + (t/is (= 100 (:x shape))) + (t/is (= 50 (:y shape))) + (t/is (= [4 17] [(:width shape) (:height shape)])))) + +(t/deftest horizontal-auto-grow-keeps-the-block-anchor + (doseq [[align expected-y] [["top" 20] ["center" -60] ["bottom" -140]]] + (let [shape (cts/setup-shape + {:type :text :x 10 :y 20 :width 100 :height 200 + :grow-type :auto-height + :content (assoc (writing-mode-content "horizontal-tb") :vertical-align align)}) + resized (with-redefs [dwwt/get-wasm-text-new-size + (fn ([_] {:width 100 :height 360}) + ([_ _] {:width 100 :height 360}))] + (let [modifiers (dwwt/resize-wasm-text-modifiers shape)] + (gsh/transform-shape shape (get-in modifiers [(:id shape) :modifiers]))))] + (t/is (= expected-y (:y resized)) align)))) + +(t/deftest vertical-grow-type-resize-axes-are-remapped + (t/is (= :auto-height + (dwm/next-grow-type :auto-width (gpt/point 1 2) true))) + (t/is (= :fixed + (dwm/next-grow-type :auto-height (gpt/point 2 1) true))) + (t/is (= :auto-height + (dwm/next-grow-type :auto-height (gpt/point 1 2) true)))) + +(t/deftest distributed-alignment-exports-valid-css + (let [style (text.styles/generate-paragraph-styles nil {:text-align "distribute"}) + normal (text.styles/generate-paragraph-styles nil {:text-align "justify"})] + (t/is (= "justify" (aget style "textAlign"))) + (t/is (= "justify" (aget style "textAlignLast"))) + (t/is (= "inter-character" (aget style "textJustify"))) + (t/is (nil? (aget normal "textAlignLast"))))) + +(t/deftest vertical-export-styles-enable-inter-script-spacing + (let [vertical (text.styles/generate-paragraph-styles + nil + {:writing-mode "vertical-rl" + :text-orientation "upright"}) + horizontal (text.styles/generate-paragraph-styles + nil + {:writing-mode "horizontal-tb"})] + (t/is (= "vertical-rl" (aget vertical "writingMode"))) + (t/is (= "upright" (aget vertical "textOrientation"))) + (t/is (= "normal" (aget vertical "textAutospace"))) + (t/is (nil? (aget horizontal "textAutospace"))))) + +(t/deftest text-export-styles-emit-font-features + (let [palt (text.styles/generate-text-styles + {:grow-type :fixed} + {:font-features "palt" + :font-size "20" + :fills [{:fill-color "#000000" :fill-opacity 1}]}) + none (text.styles/generate-text-styles + {:grow-type :fixed} + {:font-features "none" + :font-size "20" + :fills [{:fill-color "#000000" :fill-opacity 1}]})] + (t/is (= "\"palt\"" (aget palt "fontFeatureSettings"))) + (t/is (nil? (aget none "fontFeatureSettings"))))) + +(t/deftest text-export-styles-emit-annotation-clearance + (let [style (text.styles/generate-text-styles + {:grow-type :fixed} + {:annotation-clearance "auto" + :line-height "1.2" + :ruby "かんじ" + :text-emphasis "filled-dot" + :font-size "20" + :fills [{:fill-color "#000000" :fill-opacity 1}]})] + (t/is (nil? (aget style "--annotation-clearance"))) + (t/is (= "calc(max(var(--paragraph-line-height, 1.2), 1.2) + 1)" + (aget style "lineHeight"))))) + +(t/deftest annotation-clearance-adds-the-ruby-size-to-the-paragraph-line-height + (let [style (text.styles/generate-text-styles + {:grow-type :fixed} + {:annotation-clearance "auto" + :ruby "かんじ" + :ruby-size "quarter" + :font-size "20" + :fills [{:fill-color "#000000" :fill-opacity 1}]}) + paragraph (text.styles/generate-paragraph-styles nil {:line-height "2"})] + (t/is (= "calc(var(--paragraph-line-height, 1.2) + 0.25)" (aget style "lineHeight"))) + (t/is (= "2" (aget paragraph "--paragraph-line-height"))))) + +(t/deftest annotation-clearance-none-keeps-the-line-height + (let [style (text.styles/generate-text-styles + {:grow-type :fixed} + {:annotation-clearance "none" + :ruby "かんじ" + :font-size "20" + :fills [{:fill-color "#000000" :fill-opacity 1}]})] + (t/is (nil? (aget style "lineHeight"))))) + ;; --------------------------------------------------------------------------- ;; Helpers ;; --------------------------------------------------------------------------- @@ -725,3 +924,121 @@ (t/deftest same-styled-text-sees-a-text-change (t/is (not (dwt/same-styled-text? (text-content {:text "Hello" :font-weight "400"}) (text-content {:text "Hello!" :font-weight "400"}))))) + +(defn- spans-content + [& spans] + {:type "root" + :children [{:type "paragraph-set" + :children [{:type "paragraph" + :children (vec spans)}]}]}) + +(defn- content-spans + [shape] + (txt/node-seq txt/is-text-node? (:content shape))) + +(t/deftest range-style-over-part-of-a-ruby-span-styles-the-whole-span + (let [shape {:type :text + :content (spans-content {:text "あ" :font-size "14"} + {:text "漢字" :font-size "14" :ruby "かんじ"} + {:text "い" :font-size "14"})} + result (dwt/update-text-range-attrs shape 1 2 {:font-size "30"})] + (t/is (= [["あ" "14" nil] ["漢字" "30" "かんじ"] ["い" "14" nil]] + (mapv (juxt :text :font-size :ruby) (content-spans result)))))) + +(t/deftest range-style-over-part-of-a-warichu-span-styles-the-whole-span + (let [shape {:type :text + :content (spans-content {:text "本文" :font-size "14"} + {:text "割注" :font-size "14" :warichu "warichu"})} + result (dwt/update-text-range-attrs shape 0 3 {:font-size "30"})] + (t/is (= [["本文" "30" nil] ["割注" "30" "warichu"]] + (mapv (juxt :text :font-size :warichu) (content-spans result)))))) + +(t/deftest single-span-range-is-false-across-span-boundaries + (let [content (spans-content {:text "漢" :font-weight "700"} + {:text "字かな" :font-weight "400"})] + (t/is (true? (dwt/single-span-range? content 1 3))) + (t/is (false? (dwt/single-span-range? content 0 2))))) + +(defn- paragraphs-content + [& texts] + {:type "root" + :children [{:type "paragraph-set" + :children (mapv (fn [text] + {:type "paragraph" + :children [{:text text :font-size "14"}]}) + texts)}]}) + +(t/deftest range-style-in-a-later-paragraph-uses-document-offsets + (let [shape {:type :text :content (paragraphs-content "東京" "京都")} + result (dwt/update-text-range-attrs shape 3 5 {:font-size "30"})] + (t/is (= [["東京" "14"] ["京都" "30"]] + (mapv (juxt :text :font-size) (content-spans result)))))) + +(t/deftest range-style-in-the-first-paragraph-leaves-later-paragraphs-alone + (let [shape {:type :text :content (paragraphs-content "東京" "京都")} + result (dwt/update-text-range-attrs shape 0 2 {:font-size "30"})] + (t/is (= [["東京" "30"] ["京都" "14"]] + (mapv (juxt :text :font-size) (content-spans result)))))) + +(t/deftest single-span-range-counts-each-paragraph-at-its-own-offset + (let [content (paragraphs-content "東京" "京都")] + (t/is (true? (dwt/single-span-range? content 0 2))) + (t/is (true? (dwt/single-span-range? content 3 5))) + (t/is (false? (dwt/single-span-range? content 1 4))))) + +(t/deftest ruby-presentation-of-several-texts-is-one-shape-update + (let [file-id (uuid/next) + page-id (uuid/next) + ids [(uuid/next) (uuid/next) (uuid/next)] + objects (into {} (map (fn [id] [id {:id id :type :text}])) ids) + state {:current-file-id file-id + :current-page-id page-id + :features #{} + :files {file-id {:data {:pages-index {page-id {:objects objects}}}}}} + events (atom [])] + (->> (ptk/watch (dwt/update-all-ruby-presentation ids {:ruby-size "third"}) state nil) + (rx/subs! #(swap! events conj %))) + (let [updates (filter #(= ::dwsh/update-shapes (ptk/type %)) @events)] + (t/is (= 1 (count updates)))))) + +(defn- spans-of + [shape] + (->> (:content shape) (txt/node-seq txt/is-text-node?) (mapv #(select-keys % [:text :ruby :text-combine-upright])))) + +(defn- one-span-text + [text] + {:type "root" + :children [{:type "paragraph-set" + :children [{:type "paragraph" + :children [{:text text :font-size "20"}]}]}]}) + +(t/deftest update-text-range-joins-spans-left-with-the-same-style + (t/testing "the same style set over overlapping ranges ends in one span" + (let [shape (-> {:content (one-span-text "ab123c")} + (dwt/update-text-range-attrs 3 5 {:text-combine-upright "all"}) + (dwt/update-text-range-attrs 2 5 {:text-combine-upright "all"}))] + (t/is (= [{:text "ab"} {:text "123" :text-combine-upright "all"} {:text "c"}] + (spans-of shape))))) + + (t/testing "removing per-character readings lets a group reading cover the word" + (let [shape (reduce (fn [shape [idx reading]] + (dwt/update-text-range-attrs shape idx (inc idx) {:ruby reading})) + {:content (one-span-text "一盅兩件")} + (map-indexed vector ["jat1" "zung1" "loeng5" "gin6"])) + shape (dwt/update-text-range-attrs shape 0 4 {:ruby nil})] + (t/is (= [{:text "一盅兩件"}] (spans-of shape))) + (t/is (dwt/single-span-range? (:content shape) 0 4))))) + +(t/deftest update-text-range-sets-the-language-on-the-paragraphs-it-touches + (let [content {:type "root" + :children [{:type "paragraph-set" + :children [{:type "paragraph" :children [{:text "ab" :font-size "20"}]} + {:type "paragraph" :children [{:text "cd" :font-size "20"}]}]}]} + languages (fn [shape] + (->> (:content shape) (txt/node-seq txt/is-paragraph-node?) (mapv :text-language))) + shape (dwt/update-text-range-attrs {:content content} 3 4 {:text-language "ja"})] + (t/is (= [nil "ja"] (languages shape))) + (t/is (= [{:text "ab"} {:text "cd"}] (spans-of shape)) + "spans are neither split nor given the language") + (t/is (= [nil nil] + (languages (dwt/update-text-range-attrs shape 0 5 {:text-language nil})))))) diff --git a/frontend/test/frontend_tests/render_wasm/text_editor_apply_styles_test.cljs b/frontend/test/frontend_tests/render_wasm/text_editor_apply_styles_test.cljs index f9681063b0..3b00aec641 100644 --- a/frontend/test/frontend_tests/render_wasm/text_editor_apply_styles_test.cljs +++ b/frontend/test/frontend_tests/render_wasm/text_editor_apply_styles_test.cljs @@ -100,3 +100,66 @@ (t/testing "a collapsed selection has no selected spans" (let [c (content [(para [(span "hello" [red])])])] (t/is (nil? (text-editor/selection-fills c (selection 0 2 0 2))))))) + +(t/deftest apply-attrs-joins-spans-left-with-the-same-style + (t/testing "extending a style over its neighbour leaves one span" + (let [para {:children [{:text "ab"} {:text "12" :text-combine-upright "all"} {:text "3c"}]} + result (apply-attrs-to-paragraph para 4 5 {:text-combine-upright "all"})] + (t/is (= [{:text "ab"} {:text "123" :text-combine-upright "all"} {:text "c"}] + (:children result))))) + + (t/testing "resetting a style to its default rejoins the text" + (let [para {:children [{:text "ab"} {:text "cd" :text-emphasis "filled-dot"} {:text "ef"}]} + result (apply-attrs-to-paragraph para 2 4 {:text-emphasis "none"})] + (t/is (= [{:text "abcdef"}] (:children result)))))) + +(defn- editor-selection + "A WASM editor selection from `start` to `end` in the first paragraph." + [start end] + {:anchor-para 0 :anchor-offset start :focus-para 0 :focus-offset end}) + +(def ^:private reading-text + "日本 has one reading over two spans, the second with emphasis; 語 has + none; 天 has a reading of its own, without a group." + (content [(para [{:text "日" :ruby "にほん" :ruby-group "g"} + {:text "本" :ruby "にほん" :ruby-group "g" :text-emphasis "filled-sesame"} + {:text "語"} + {:text "を" :text-emphasis "filled-dot"} + {:text "天" :ruby "てん"}])])) + +(t/deftest a-reading-takes-text-of-several-styles + (t/is (text-editor/reading-selection? + (content [(para [{:text "日"} {:text "本" :text-emphasis "filled-dot"}])]) + (editor-selection 0 2)) + "emphasis on part of the text does not block a reading")) + +(t/deftest a-reading-edits-a-whole-base-only + (t/is (text-editor/reading-selection? reading-text (editor-selection 0 2)) + "the whole base of a reading over two spans") + (t/is (not (text-editor/reading-selection? reading-text (editor-selection 1 2))) + "part of a base") + (t/is (not (text-editor/reading-selection? reading-text (editor-selection 1 3))) + "a base and the text after it") + (t/is (text-editor/reading-selection? reading-text (editor-selection 2 4)) + "text of two styles without readings") + (t/is (text-editor/reading-selection? reading-text (editor-selection 4 5)) + "the whole of a reading without a group")) + +(t/deftest a-warichu-note-takes-one-span + (t/is (text-editor/single-span-selection? reading-text (editor-selection 2 3))) + (t/is (not (text-editor/single-span-selection? reading-text (editor-selection 2 4))))) + +(t/deftest emphasis-on-part-of-a-grouped-reading-keeps-one-base + (let [paragraph {:children [{:text "日本" :ruby "にほん" :ruby-group "g"} {:text "語"}]} + result (apply-attrs-to-paragraph paragraph 1 2 {:text-emphasis "filled-dot"})] + (t/is (= [{:text "日" :ruby "にほん" :ruby-group "g"} + {:text "本" :ruby "にほん" :ruby-group "g" :text-emphasis "filled-dot"} + {:text "語"}] + (:children result))))) + +(t/deftest emphasis-on-part-of-a-reading-without-a-group-covers-its-base + (let [paragraph {:children [{:text "日本" :ruby "にほん"} {:text "語"}]} + result (apply-attrs-to-paragraph paragraph 1 2 {:text-emphasis "filled-dot"})] + (t/is (= [{:text "日本" :ruby "にほん" :text-emphasis "filled-dot"} {:text "語"}] + (:children result)) + "splitting it would repeat the reading"))) diff --git a/frontend/test/frontend_tests/render_wasm/texts_test.cljs b/frontend/test/frontend_tests/render_wasm/texts_test.cljs new file mode 100644 index 0000000000..5632e3c82d --- /dev/null +++ b/frontend/test/frontend_tests/render_wasm/texts_test.cljs @@ -0,0 +1,648 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS INC Sucursal en España SL + +(ns frontend-tests.render-wasm.texts-test + "Japanese text support in the WASM bridge: span serialization, editor + selection styles, IME composition caret placement, and the CJK script + classification used to pick Noto fallback fonts." + (:require + [app.common.fonts :as cfnt] + [app.common.render-wasm.serializers :as sr] + [app.common.render-wasm.text-content :as tc] + [app.common.render-wasm.wasm :as wasm] + [app.common.types.text.cjk-layout :as cjk] + [app.config :as cfg] + [app.main.ui.workspace.shapes.text.ime-debug :as ime-debug] + [app.main.ui.workspace.shapes.text.v3-editor :as v3-editor] + [app.render-wasm.api :as api] + [app.render-wasm.api.texts :as texts] + [app.render-wasm.text-editor :as text-editor] + [app.util.i18n :as i18n] + [app.util.text.content.styles :as content-styles] + [cljs.test :as t :include-macros true] + [frontend-tests.helpers.mock :as mock])) + +(defn- write-spans + [offset dview spans paragraph] + (@#'tc/write-spans offset dview spans paragraph cfnt/font-id->uuid)) + +(defn- langs [text] + (cfnt/collect-used-languages #{} text)) + +(defn- font-ids + [fonts] + (into #{} (map :font-id) fonts)) + +(defn- editor-content + [& paragraphs] + {:type "root" + :children [{:type "paragraph-set" + :children (mapv (fn [spans] + {:type "paragraph" + :children spans}) + paragraphs)}]}) + +(defn- selection + [anchor-para anchor-offset focus-para focus-offset] + {:anchor-para anchor-para + :anchor-offset anchor-offset + :focus-para focus-para + :focus-offset focus-offset}) + +(t/deftest reading-selection-stays-with-one-base-group + (let [content (editor-content [{:text "你好"} {:text "世界" :font-weight "700"}] + [{:text "中国" :ruby "zhōng guó"}])] + ;; A reading joins text of several styles into one base. + (doseq [range [(selection 0 0 0 1) (selection 0 2 0 0) + (selection 0 2 0 4) (selection 1 0 1 2) + (selection 0 1 0 3)]] + (t/is (true? (text-editor/reading-selection? content range)))) + (doseq [range [(selection 0 0 1 2) + (selection 0 1 0 1) (selection 1 0 1 1)]] + (t/is (false? (text-editor/reading-selection? content range)))) + (t/is (false? (text-editor/reading-selection? nil nil))))) + +(t/deftest cjk-styles-follow-the-selected-span + (let [content (editor-content + [{:text "日" :ruby "にち" :text-emphasis "filled-dot"} + {:text "本"}])] + (t/is (= {:text-combine-upright "none" + :text-emphasis "filled-dot" + :text-emphasis-position "auto" + :text-underline-position "auto" + :ruby "にち" + :ruby-group "" + :ruby-hidden false + :ruby-zhuyin false + :ruby-size "half" + :ruby-align "space-around" + :ruby-overhang "auto" + :ruby-side "over" + :warichu "none" + :font-features "none" + :annotation-clearance "none"} + (text-editor/selection-cjk-styles + content (selection 0 0 0 1)))) + (t/is (= {:text-combine-upright "none" + :text-emphasis "none" + :text-emphasis-position "auto" + :text-underline-position "auto" + :ruby "" + :ruby-group "" + :ruby-hidden false + :ruby-zhuyin false + :ruby-size "half" + :ruby-align "space-around" + :ruby-overhang "auto" + :ruby-side "over" + :warichu "none" + :font-features "none" + :annotation-clearance "none"} + (text-editor/selection-cjk-styles + content (selection 0 1 0 2)))))) + +(t/deftest paragraph-profiles-follow-the-editor-selection + (let [content {:type "root" + :children [{:type "paragraph-set" + :children [{:type "paragraph" :children [{:text "Plain"}]} + {:type "paragraph" :text-language "ko" + :korean-wrap "spaces-only" + :children [{:text "한국어"}]}]}]}] + (t/is (= {:text-language nil :korean-wrap nil} + (text-editor/selection-paragraph-styles content (selection 0 0 0 0)))) + (t/is (= {:text-language "ko" :korean-wrap "spaces-only"} + (text-editor/selection-paragraph-styles content (selection 1 1 1 1)))) + (t/is (= {:text-language :multiple :korean-wrap :multiple} + (text-editor/selection-paragraph-styles content (selection 0 0 1 1)))) + (t/is (= {:text-language :multiple :korean-wrap :multiple} + (text-editor/selection-paragraph-styles content (selection 1 1 0 0)))))) + +(t/deftest cjk-styles-report-mixed-ranges-and-follow-the-caret + (let [content (editor-content + [{:text "平成" + :text-combine-upright "digits2" + :text-emphasis "filled-dot" + :text-emphasis-position "under-left" + :text-underline-position "right" + :ruby "へいせい" + :ruby-hidden true + :ruby-size "third" + :ruby-align "center" + :ruby-overhang "none" + :ruby-side "under" + :annotation-clearance "auto"} + {:text "年" + :ruby-hidden false + :ruby-zhuyin false + :warichu "warichu" + :font-features "vpal"}])] + (t/is (= {:text-combine-upright :multiple + :text-emphasis :multiple + :text-emphasis-position :multiple + :text-underline-position :multiple + :ruby :multiple + :ruby-group "" + :ruby-hidden :multiple + :ruby-zhuyin false + :ruby-size :multiple + :ruby-align :multiple + :ruby-overhang :multiple + :ruby-side :multiple + :warichu :multiple + :font-features :multiple + :annotation-clearance :multiple} + (text-editor/selection-cjk-styles + content (selection 0 0 0 3)))) + ;; A caret at the boundary belongs to the preceding span, as in WASM. + (t/is (= "digits2" + (:text-combine-upright + (text-editor/selection-cjk-styles + content (selection 0 2 0 2))))) + (t/is (= "warichu" + (:warichu + (text-editor/selection-cjk-styles + content (selection 0 3 0 3))))))) + +(t/deftest japanese-selection-offsets-are-utf16-code-units + (let [content (editor-content + [{:text "😀"} + {:text "日" :ruby "にち"}])] + (t/is (= "にち" + (:ruby + (text-editor/selection-cjk-styles + content (selection 0 2 0 3))))))) + +(t/deftest composition-caret-goes-to-the-end-of-the-changed-part + ;; Typing appends, converting replaces all, and a candidate for a middle + ;; clause changes only that clause. + (t/is (= 1 (v3-editor/changed-span-end "" "に"))) + (t/is (= 2 (v3-editor/changed-span-end "に" "にほ"))) + (t/is (= 3 (v3-editor/changed-span-end "にほんご" "日本語"))) + (t/is (= 5 (v3-editor/changed-span-end "日本語を書く" "日本語を掻く"))) + (t/is (= 3 (v3-editor/changed-span-end "ああ" "あああ"))) + (t/is (= 0 (v3-editor/changed-span-end "にほ" ""))) + (t/is (= 2 (v3-editor/changed-span-end "😀" "😀😀")))) + +(t/deftest ime-debug-caret-bounds-follow-viewport-zoom-and-pan + (t/is (= {:x 210 :y 380 :width 48 :height 2} + (@#'ime-debug/screen-rect + {:x 100 :y 200 :width 24 :height 1} + #js {:a 2 :b 0 :c 0 :d 2 :e 10 :f -20})))) + +(t/deftest ime-debug-caret-bounds-follow-shape-rotation + (t/is (= {:x 119 :y 140 :width 1 :height 24} + (@#'ime-debug/screen-rect + {:x 100 :y 200 :width 24 :height 1} + #js {:a 0 :b 1 :c -1 :d 0 :e 320 :f 40})))) + +(t/deftest ime-debug-composition-offsets-use-dom-utf16-units + (let [prefix #js {:length 3} + kana #js {:length 2} + suffix #js {:length 1} + nodes [prefix kana suffix]] + ;; A prefix such as "a😀" takes three DOM units, not two code points. + ;; The range must exclude zero-length rectangles from adjacent text nodes. + (t/is (= [kana 0] (@#'ime-debug/text-position nodes 3 :start))) + (t/is (= [kana 2] (@#'ime-debug/text-position nodes 5 :end))) + (t/is (nil? (@#'ime-debug/text-position nodes 7 :end))))) + +(defn- ime-surface-style + ([platform vertical? rect] + (ime-surface-style platform vertical? rect 1)) + ([platform vertical? rect zoom] + (ime-surface-style platform vertical? rect zoom :chrome)) + ([platform vertical? rect zoom browser] + (mock/with-mocks* + {cfg/browser browser + cfg/check-platform? (fn [candidate] (= platform candidate)) + text-editor/text-editor-get-cursor-rect (fn [] rect)} + (let [node #js {:style #js {} + :closest (fn [_] #js {:getScreenCTM (fn [] #js {:c 0 :d zoom})})} + wrapper #js {:style #js {:transform "translate(10px, 20px)" + :left "-36px" :top "72px"}} + origin {:x 100 :y 80 :width 900 :vertical? vertical?} + anchor (@#'v3-editor/update-ime-caret! node wrapper origin)] + {:left (+ (:x origin) (js/parseFloat (.. node -style -left))) + :right (+ (:x origin) (:width origin) (js/parseFloat (.. node -style -left))) + :top (+ (:y origin) (js/parseFloat (.. node -style -top))) + :writing-mode (.. node -style -writingMode) + :transform (.. node -style -transform) + :overflow (.. node -style -overflow) + :padding (@#'v3-editor/ime-overlay-padding vertical? 900) + :anchor anchor + :wrapper-left (.. wrapper -style -left) + :wrapper-top (.. wrapper -style -top) + :wrapper-transform (.. wrapper -style -transform)})))) + +(t/deftest ^:async macos-vertical-ime-anchor-matches-the-text-column + (doseq [width [12 24 72] + browser [:chrome :firefox :edge]] + (let [rect {:x 400 :y 180 :width width :height 1} + result (await (ime-surface-style :macos true rect 1 browser))] + ;; vertical-rl puts the caret cell immediately left of the surface's + ;; right edge. Match the WASM column without a horizontal offset. + (t/is (= (+ (:x rect) (:width rect)) (:right result))) + (t/is (= (:y rect) (:top result))) + (t/is (= "vertical-rl" (:writing-mode result))) + (t/is (= {:left 900 :right 0} (:padding result))) + (t/is (= "clip" (:overflow result))) + (t/is (true? (:vertical? (:anchor result)))) + (t/is (= "" (:wrapper-left result))) + (t/is (= "" (:wrapper-top result))) + (t/is (= "" (:wrapper-transform result)))))) + +(t/deftest ^:async macos-safari-vertical-ime-anchor-clears-the-text-column + ;; Safari's native candidate window is horizontal even for vertical text. + ;; Its anchor needs room to the right, including at the text shape's edge. + (doseq [browser [:safari :safari-16 :safari-17 :safari-18 :safari-26] + width [12 24 72]] + (let [rect {:x 400 :y 180 :width width :height 1} + result (await (ime-surface-style :macos true rect 1 browser))] + (t/is (> (- (:right result) width) (+ (:x rect) width))) + (t/is (= (:y rect) (:top result))) + (t/is (= {:left 900 :right 900} (:padding result))) + (t/is (= "vertical-rl" (:writing-mode result))) + (t/is (= "clip" (:overflow result)))))) + +(t/deftest ^:async macos-vertical-ime-character-bounds-stay-nonempty + ;; Chromium derives a vertical character rectangle from integer caret y + ;; positions. Subpixel advances become empty rectangles, which macOS uses + ;; to position its candidate window. Exercise long compositions and zoom. + (doseq [width [12 24 72] + zoom [0.25 1 4] + browser [:chrome :firefox :safari-26]] + (let [rect {:x 400 :y 180 :width width :height 1} + result (await (ime-surface-style :macos true rect zoom browser)) + scale (js/parseFloat (subs (:transform result) 7)) + step (* width scale zoom) + ys (map #(js/Math.floor (+ 180 (* % step))) (range 81))] + (t/is (every? pos? (map - (rest ys) ys)))))) + +(t/deftest ^:async windows-vertical-ime-anchor-matches-the-text-column + (doseq [width [12 24 72] + browser [:chrome :edge :firefox]] + (let [rect {:x 400 :y 180 :width width :height 1} + result (await (ime-surface-style :windows true rect 1 browser))] + ;; Windows opens vertical candidates to the left of the composition + ;; range. Keep that range in the text column so candidates clear it. + (t/is (= (+ (:x rect) width) (:right result))) + (t/is (= (:y rect) (:top result))) + (t/is (= "vertical-rl" (:writing-mode result))) + (t/is (= "scaleY(0.001)" (:transform result)))))) + +(t/deftest ^:async linux-vertical-ime-anchor-stays-right-of-the-text-column + (doseq [browser [:chrome :firefox :safari]] + (let [rect {:x 400 :y 180 :width 24 :height 1} + result (await (ime-surface-style :linux true rect 1 browser))] + ;; vertical-rl places the DOM caret in the rightmost cell of the surface. + (t/is (> (- (:right result) (:width rect)) (+ (:x rect) (:width rect)))) + (t/is (= (:y rect) (:top result))) + (t/is (= {:left 0 :right 900} (:padding result))) + (t/is (= "vertical-rl" (:writing-mode result))) + (t/is (= "scaleY(0.001)" (:transform result))) + (t/is (= "" (:overflow result)))))) + +(t/deftest ^:async horizontal-ime-anchor-stays-on-the-caret + (doseq [platform [:macos :linux :windows] + browser [:chrome :firefox :safari]] + (let [rect {:x 400 :y 180 :width 1 :height 24} + result (await (ime-surface-style platform false rect 1 browser))] + (t/is (= (:x rect) (:left result))) + (t/is (= (:y rect) (:top result))) + (t/is (= {:left 0 :right 0} (:padding result))) + (t/is (= "" (:writing-mode result))) + (t/is (= "scaleX(0.001)" (:transform result)))))) + +(t/deftest ^:async macos-vertical-ime-follows-the-caret-in-layout + (let [anchor {:x 400 :y 180 :width 24 :height 1 :vertical? true} + current (atom {:x 400 :y 252 :width 24 :height 1}) + wrapper #js {:style #js {:transform ""}}] + (await + (mock/with-mocks* + {cfg/check-platform? (fn [candidate] (= candidate :macos)) + text-editor/text-editor-get-cursor-rect (fn [] @current)} + (@#'v3-editor/follow-ime-caret! wrapper anchor) + (t/is (= "72px" (.. wrapper -style -top))) + (t/is (= "0px" (.. wrapper -style -left))) + (t/is (= "" (.. wrapper -style -transform))) + ;; Moving into another vertical column resets the inline position. + (reset! current {:x 364 :y 180 :width 24 :height 1}) + (@#'v3-editor/follow-ime-caret! wrapper anchor) + (t/is (= "0px" (.. wrapper -style -top))) + (t/is (= "-36px" (.. wrapper -style -left))))))) + +(t/deftest ^:async other-ime-directions-keep-following-with-a-transform + (doseq [[platform vertical?] [[:linux true] [:linux false] [:macos false] + [:windows true] [:windows false]]] + (let [anchor {:x 400 :y 180 :vertical? vertical?} + wrapper #js {:style #js {}}] + (await + (mock/with-mocks* + {cfg/check-platform? (fn [candidate] (= candidate platform)) + text-editor/text-editor-get-cursor-rect (fn [] {:x 424 :y 252})} + (@#'v3-editor/follow-ime-caret! wrapper anchor) + (t/is (= "translate(24px, 72px)" (.. wrapper -style -transform)))))))) + +(t/deftest vertical-writing-attrs-apply-to-every-paragraph + (let [content (-> (editor-content [{:text "一"}] [{:text "二"}] [{:text "三"}]) + (assoc-in [:children 0 :children 2 :text-orientation] "upright")) + result (text-editor/apply-paragraph-attrs-to-range + content + (selection 1 0 1 0) + {:writing-mode "vertical-rl" + :text-orientation nil + :text-align "center"}) + paragraphs (-> result :children first :children)] + (t/is (every? #(= "vertical-rl" (:writing-mode %)) paragraphs)) + (t/is (not-any? #(contains? % :text-orientation) paragraphs)) + (t/is (= [nil "center" nil] (mapv :text-align paragraphs))))) + +(t/deftest language-applies-to-the-whole-text-and-wrapping-to-selected-paragraphs + (let [content (editor-content [{:text "日本"}] [{:text "한국어"}] [{:text "中文"}]) + result (text-editor/apply-paragraph-attrs-to-range + content (selection 1 0 1 0) + {:text-language "ko" :korean-wrap "spaces-only"}) + paragraphs (-> result :children first :children)] + (t/is (= ["ko" "ko" "ko"] (mapv :text-language paragraphs))) + (t/is (= [nil "spaces-only" nil] (mapv :korean-wrap paragraphs))) + (t/is (= result + (text-editor/merge-exported-texts-into-content + result [[{:p 0 :s 0 :t "日本"}] + [{:p 1 :s 0 :t "한국어"}] + [{:p 2 :s 0 :t "中文"}]])))) + (let [content (assoc-in (editor-content [{:text "한글"}]) + [:children 0 :children 0 :text-language] "ko") + result (text-editor/merge-exported-texts-into-content + content [[{:p 0 :s 0 :t "한"}] [{:p 0 :s 0 :t "글"}]])] + (t/is (= ["ko" "ko"] (mapv :text-language (-> result :children first :children)))))) + +(t/deftest paragraph-language-binary-header-matches-rust + (let [dview (js/DataView. (js/ArrayBuffer. tc/PARAGRAPH-ATTR-U8-SIZE)) + end (@#'tc/write-paragraph 0 dview + {:text-language "zh-Hant-TW" :korean-wrap "spaces-only" + :line-height "1.5" :letter-spacing "2"})] + (t/is (= 20 end)) + (t/is (= 1.5 (.getFloat32 dview 4 true))) + (t/is (= 2 (.getFloat32 dview 8 true))) + (t/is (= 1 (.getUint8 dview 15))) + (t/is (= 10 (.getUint32 dview 16 true))))) + +(t/deftest merging-away-original-spans-keeps-the-surviving-paragraph-profile + (let [content (-> (editor-content [{:text "日"}] [{:text "한"}]) + (assoc-in [:children 0 :children 0 :text-language] "ja") + (assoc-in [:children 0 :children 1 :text-language] "ko") + (assoc-in [:children 0 :children 1 :korean-wrap] "spaces-only")) + result (text-editor/merge-exported-texts-into-content + content [[{:pp 0 :p 1 :s 0 :t "한"}]]) + para (get-in result [:children 0 :children 0])] + (t/is (= "ja" (:text-language para))) + (t/is (not (contains? para :korean-wrap))) + (t/is (= "한" (get-in para [:children 0 :text]))))) + +;; Byte offsets inside one span's attribute block (see `write-spans` and +;; RawTextSpan in render-wasm). +(def ^:private span-attr-offset + {:text-combine-upright 5 + :font-features 8 + :annotation-clearance 9 + :ruby-size 10 + :ruby-align 11 + :ruby-overhang 12 + :ruby-side 13 + :text-emphasis-position 14 + :text-underline-position 15 + :padding [14 15] + :ruby-length 72}) + +(def ^:private span-paragraph + {:font-size "16" :font-weight "400" :line-height "1"}) + +(defn- serialized-span + "DataView over the attribute block `write-spans` produces for `span`." + [span] + (let [dview (js/DataView. (js/ArrayBuffer. 256))] + (write-spans 0 dview [(merge {:font-size "16" :font-weight "400"} span)] span-paragraph) + dview)) + +(defn- span-byte + [dview attr] + (.getUint8 dview (get span-attr-offset attr))) + +(defn- padding-bytes + [dview] + (mapv #(.getUint8 dview %) (:padding span-attr-offset))) + +(t/deftest paragraph-wrap-modes-have-stable-distinct-protocol-bytes + (doseq [[mode expected] [[nil 0] ["syllable" 0] ["spaces-only" 1] ["keep-words" 2] ["invalid" 0]]] + (let [dview (js/DataView. (js/ArrayBuffer. tc/PARAGRAPH-ATTR-U8-SIZE))] + (@#'tc/write-paragraph 0 dview {:text-language "ko" :korean-wrap mode}) + (t/is (= expected (.getUint8 dview 15))) + (t/is (= 2 (.getUint32 dview 16 true)))))) + +(t/deftest write-spans-serializes-font-features + (let [dview (serialized-span {:text "日本語" :font-features "vpal"})] + (t/is (= 2 (span-byte dview :font-features))) + (t/is (= 0 (span-byte dview :annotation-clearance))) + (t/is (= [0 0] (padding-bytes dview))))) + +(t/deftest write-spans-serializes-annotation-clearance + (let [dview (serialized-span {:text "漢字" :annotation-clearance "auto"})] + (t/is (= 1 (span-byte dview :annotation-clearance))) + (t/is (= [0 0] (padding-bytes dview))))) + +(t/deftest write-spans-serializes-ruby-customization + (let [dview (serialized-span {:text "漢字" + :ruby-size "quarter" + :ruby-align "space-between" + :ruby-overhang "none" + :ruby-side "under"})] + (t/is (= [2 3 1 1] + (mapv #(span-byte dview %) [:ruby-size :ruby-align :ruby-overhang :ruby-side]))) + (t/is (= [0 0] (padding-bytes dview))))) + +(t/deftest write-spans-omits-hidden-ruby + (let [ruby-length #(.getInt32 % (:ruby-length span-attr-offset) true)] + (t/is (= 0 (ruby-length (serialized-span {:text "日" :ruby "にち" :ruby-hidden true})))) + (t/is (pos? (ruby-length (serialized-span {:text "日" :ruby "にち" :ruby-hidden false})))) + ;; A stored nil reading sends no bytes, as an absent one does. + (t/is (= 0 (ruby-length (serialized-span {:text "日" :ruby nil})))))) + +(t/deftest write-spans-serializes-counted-digits-tcy + (let [dview (serialized-span {:text "平成31年" :text-combine-upright "digits2"})] + (t/is (= 3 (span-byte dview :text-combine-upright))))) + +(t/deftest ruby-text-participates-in-live-and-reload-fallback-discovery + (let [content {:children + [{:children + [{:children + [{:text "Penpot" + :ruby "ぺんぽっと😀"}]}]}]} + expected #{"gfont-noto-sans-jp" "gfont-noto-color-emoji"}] + (with-redefs [texts/write-shape-text (fn [& _])] + (t/is (every? (font-ids (api/fonts-from-text-content content true)) expected))) + (t/is (every? (font-ids (api/fonts-from-text-content content false)) expected)))) + +(t/deftest classification-kana + ;; Hiragana/katakana are unambiguously Japanese. + (t/is (= #{:japanese} (langs "ひらがなとカタカナ"))) + ;; Half-width katakana too. + (t/is (= #{:japanese} (langs "デザイン")))) + +(t/deftest classification-han-is-ambiguous + ;; Kanji-only text is ambiguous Han, not a concrete language. + (t/is (= #{:han} (langs "東京都渋谷区神南一丁目")))) + +(t/deftest classification-cjk-punctuation + ;; CJK punctuation and full-width forms match the shared class. + (t/is (= #{:cjk-punctuation} (langs "、。「」『』()"))) + (t/is (= #{:cjk-punctuation} (langs "!?:;123ABC")))) + +(t/deftest classification-mixed-japanese + (t/is (= #{:japanese :han :cjk-punctuation} + (langs "「こんにちは」と彼は言った。")))) + +(t/deftest classification-korean + (t/is (= #{:korean} (langs "안녕하세요")))) + +(t/deftest resolve-kana-wins-over-locale + ;; Kana in the same content implies Japanese regardless of locale. + (t/is (= #{:japanese} + (cfnt/resolve-ambiguous-cjk #{:japanese :han :cjk-punctuation} nil "zh"))) + (t/is (= #{:japanese} + (cfnt/resolve-ambiguous-cjk #{:japanese :han} nil "en")))) + +(t/deftest resolve-hangul-wins-over-locale + (t/is (= #{:korean} + (cfnt/resolve-ambiguous-cjk #{:korean :han} nil "ja")))) + +(t/deftest resolve-han-only-uses-locale + (t/is (= #{:japanese} (cfnt/resolve-ambiguous-cjk #{:han} nil "ja"))) + (t/is (= #{:japanese} (cfnt/resolve-ambiguous-cjk #{:han} nil "ja_paid"))) + (t/is (= #{:korean} (cfnt/resolve-ambiguous-cjk #{:han} nil "ko"))) + (t/is (= #{:chinese} (cfnt/resolve-ambiguous-cjk #{:han} nil "zh_cn")))) + +(t/deftest resolve-han-only-defaults-to-chinese + ;; Without kana, hangul or a CJK locale, Han resolves to Chinese (Noto Sans SC). + (t/is (= #{:chinese} (cfnt/resolve-ambiguous-cjk #{:han} nil "en"))) + (t/is (= #{:chinese} (cfnt/resolve-ambiguous-cjk #{:han} nil nil)))) + +(t/deftest resolve-punctuation-only + (t/is (= #{:japanese} (cfnt/resolve-ambiguous-cjk #{:cjk-punctuation} nil "ja"))) + (t/is (= #{:chinese} (cfnt/resolve-ambiguous-cjk #{:cjk-punctuation} nil "en")))) + +(t/deftest resolve-paragraph-language-wins + ;; The paragraph language decides Han, whatever the content or locale. + (t/is (= #{:japanese} (cfnt/resolve-ambiguous-cjk #{:han} "ja" "en"))) + (t/is (= #{:chinese :japanese} + (cfnt/resolve-ambiguous-cjk #{:japanese :han} "zh-Hans" "ja"))) + (t/is (= #{:korean} (cfnt/resolve-ambiguous-cjk #{:han} "ko" nil))) + ;; A non-CJK paragraph language leaves the content and locale to decide. + (t/is (= #{:japanese} (cfnt/resolve-ambiguous-cjk #{:han} "en-GB" "ja")))) + +(t/deftest resolve-regional-chinese + (t/is (= #{:chinese-traditional} (cfnt/resolve-ambiguous-cjk #{:han} "zh-Hant-TW" nil))) + (t/is (= #{:chinese-traditional} (cfnt/resolve-ambiguous-cjk #{:han} "zh-TW" nil))) + (t/is (= #{:chinese-hongkong} (cfnt/resolve-ambiguous-cjk #{:han} "zh-Hant-HK" nil))) + (t/is (= #{:chinese} (cfnt/resolve-ambiguous-cjk #{:han} "zh-CN" nil))) + ;; App locales use underscores. + (t/is (= #{:chinese-traditional} (cfnt/resolve-ambiguous-cjk #{:han} nil "zh_hant")))) + +(t/deftest paragraph-language-selects-the-han-fallback-font + (let [content (fn [language] + {:children + [{:children + [{:text-language language + :children [{:text "東京都"}]}]}]}) + fonts #(font-ids (api/fonts-from-text-content (content %) false))] + (with-redefs [i18n/locale (atom "en")] + (t/is (contains? (fonts "ja") "gfont-noto-sans-jp")) + (t/is (not (contains? (fonts "ja") "gfont-noto-sans-sc"))) + (t/is (contains? (fonts "zh-Hant-TW") "gfont-noto-sans-tc")) + (t/is (contains? (fonts nil) "gfont-noto-sans-sc"))))) + +(t/deftest resolve-leaves-unambiguous-sets-alone + (t/is (= #{:latin-ext} (cfnt/resolve-ambiguous-cjk #{:latin-ext} nil "ja"))) + (t/is (= #{} (cfnt/resolve-ambiguous-cjk #{} nil "ja"))) + ;; Other detected languages survive resolution. + (t/is (= #{:japanese :cyrillic} + (cfnt/resolve-ambiguous-cjk #{:han :cyrillic} nil "ja")))) + +(t/deftest resolve-han-unification + ;; A kanji-only Japanese address resolves to Japanese under a ja locale. + (t/is (= #{:japanese} + (cfnt/resolve-ambiguous-cjk (langs "東京都渋谷区神南一丁目") nil "ja")))) + +(t/deftest wasm-enum-exports-match-the-cjk-value-table + (doseq [[attr values] cjk/enum-values] + (let [exported (js->clj (unchecked-get wasm/serializers (name attr)))] + (t/is (= values (mapv key (sort-by val exported))) (name attr))))) + +(t/deftest unset-or-unknown-cjk-values-serialize-as-the-default + (t/is (= 0 (sr/translate-text-layout-enum :ruby-side nil))) + (t/is (= 0 (sr/translate-text-layout-enum :ruby-side ""))) + (t/is (= 0 (sr/translate-text-layout-enum :ruby-side "left"))) + (t/is (= 1 (sr/translate-text-layout-enum :ruby-side "under"))) + (t/is (= 1 (sr/translate-text-layout-enum :ruby-side :under)))) + + +(t/deftest decoration-positions-use-the-former-padding-bytes + (let [view (serialized-span {:text "中文" :text-emphasis-position "under-left" + :text-underline-position "under-right"})] + (t/is (= 4 (span-byte view :text-emphasis-position))) + (t/is (= 5 (span-byte view :text-underline-position))) + (t/is (= 16 (.getFloat32 view 16 true))))) + + +(t/deftest decoration-edits-preserve-an-existing-reading-as-one-group + (let [paragraph {:type "paragraph" :children [{:text "你好" :ruby "nǐ hǎo"} + {:text "世界" :font-weight "700"}]} + result (text-editor/apply-attrs-to-paragraph paragraph 1 3 {:text-emphasis-position "under-left"})] + (t/is (= [{:text "你好" :ruby "nǐ hǎo" :text-emphasis-position "under-left"} + {:text "世" :font-weight "700" :text-emphasis-position "under-left"} + {:text "界" :font-weight "700"}] + (:children result))))) + + +(t/deftest dom-editor-style-data-keeps-decoration-positions + (let [attrs {:text-emphasis-position "under-left" :text-underline-position "under-right"} + styles (content-styles/attrs->styles attrs)] + (t/is (= "under-left" (aget styles "--text-emphasis-position"))) + (t/is (= "under-right" (aget styles "--text-underline-position"))) + (t/is (= attrs (content-styles/styles->attrs (js->clj styles)))))) + + +(t/deftest a-collapsed-decoration-range-does-not-restyle-a-reading + (let [paragraph {:type "paragraph" :children [{:text "你好" :ruby "nǐ hǎo"}]}] + (t/is (= paragraph (text-editor/apply-attrs-to-paragraph paragraph 1 1 {:text-emphasis-position "under-left"}))))) + +(t/deftest zhuyin-mode-round-trips-in-existing-span-flags + (doseq [[attrs expected] [[{} 0] [{:ruby-side "under"} 1] + [{:ruby-zhuyin true} 2] + [{:ruby-zhuyin true :ruby-side "under"} 3]]] + (t/is (= expected (span-byte (serialized-span attrs) :ruby-side))))) + +(t/deftest zhuyin-selection-requires-a-whole-single-han-base + (let [content (editor-content [{:text "你好"} {:text "𠀀"}] [{:text "中" :ruby "ㄓㄨㄥ" :ruby-zhuyin true}])] + (t/is (true? (text-editor/zhuyin-selection? content (selection 0 0 0 1)))) + (t/is (true? (text-editor/zhuyin-selection? content (selection 0 2 0 4)))) + (t/is (true? (text-editor/zhuyin-selection? content (selection 1 0 1 1)))) + (t/is (false? (text-editor/zhuyin-selection? content (selection 0 0 0 2)))) + (t/is (false? (text-editor/zhuyin-selection? content (selection 0 2 0 3)))))) + +(t/deftest dom-style-data-keeps-zhuyin-as-a-boolean + (let [attrs {:ruby-zhuyin true :ruby "ㄋㄧˇ"} + encoded (content-styles/attrs->styles attrs) + decoded (content-styles/styles->attrs (js->clj encoded))] + (t/is (true? (:ruby-zhuyin decoded))) + (t/is (= "ㄋㄧˇ" (:ruby decoded))))) + +(t/deftest zhuyin-export-retains-default-fonts-when-optional-span-fonts-are-absent + (let [entry (@#'api/zhuyin-glyph-entry + {:text "中" :ruby "ㄓ" :ruby-zhuyin true} + {:start-pos 0 :end-pos 1 :x 10 :y 20 :width 6 :height 6})] + (t/is (= "sourcesanspro" (:font-family entry))) + (t/is (= "ㄓ" (:text entry))) + (t/is (= 26 (:y entry))))) diff --git a/frontend/test/frontend_tests/ui/css_cursors_test.cljs b/frontend/test/frontend_tests/ui/css_cursors_test.cljs new file mode 100644 index 0000000000..5478bca52c --- /dev/null +++ b/frontend/test/frontend_tests/ui/css_cursors_test.cljs @@ -0,0 +1,9 @@ +(ns frontend-tests.ui.css-cursors-test + (:require + [app.main.ui.css-cursors :as cursors] + [cljs.test :as t])) + +(t/deftest text-cursor-follows-writing-direction-and-shape-rotation + (t/is (= "cursor-text-0" (cursors/get-text 0 false))) + (t/is (= "cursor-text-90" (cursors/get-text 0 true))) + (t/is (= "cursor-text-10" (cursors/get-text 280 true)))) diff --git a/render-wasm/src/render/text_editor.rs b/render-wasm/src/render/text_editor.rs index fdf9adb037..0c29a0fab1 100644 --- a/render-wasm/src/render/text_editor.rs +++ b/render-wasm/src/render/text_editor.rs @@ -1,4 +1,6 @@ use crate::render::options::RenderOptions; +use crate::shapes::text_paragraph::LayoutParagraph; +use crate::shapes::text_vertical; use crate::shapes::{vertical_align_offset, Shape, TextContent, Type}; use crate::state::{TextEditorState, TextSelection}; use crate::view::Viewbox; @@ -47,21 +49,37 @@ fn render_cursor( text_content: &TextContent, shape: &Shape, ) { - let Some(rect) = calculate_cursor_rect(editor_state, text_content, shape) else { + let Some(rect) = cursor_rect(editor_state, text_content, shape) else { return; }; + // In vertical writing the caret is a thin horizontal bar across the + // column; in horizontal writing a thin vertical bar. + let thin = editor_state.theme.cursor_width / zoom * dpr; let mut cursor_rect = Rect::new_empty(); - cursor_rect.set_xywh( - rect.x(), - rect.y(), - if editor_state.is_overtype_mode { - rect.width() - } else { - editor_state.theme.cursor_width / zoom * dpr - }, - rect.height(), - ); + if text_content.is_vertical() { + cursor_rect.set_xywh( + rect.x(), + rect.y(), + rect.width(), + if editor_state.is_overtype_mode && rect.height() > 0.0 { + rect.height() + } else { + thin + }, + ); + } else { + cursor_rect.set_xywh( + rect.x(), + rect.y(), + if editor_state.is_overtype_mode { + rect.width() + } else { + thin + }, + rect.height(), + ); + } let mut paint = Paint::default(); paint.set_anti_alias(false); @@ -112,10 +130,7 @@ fn render_selection( canvas.restore(); } -fn paragraphs_vertical_offset( - shape: &Shape, - layout_paragraphs: &[&skia_safe::textlayout::Paragraph], -) -> f32 { +fn paragraphs_vertical_offset(shape: &Shape, layout_paragraphs: &[&LayoutParagraph]) -> f32 { let total_height: f32 = layout_paragraphs.iter().map(|p| p.height()).sum(); vertical_align_offset( shape.selrect().height(), @@ -124,7 +139,82 @@ fn paragraphs_vertical_offset( ) } -fn calculate_cursor_rect( +fn vertical_layout_for_shape( + text_content: &TextContent, + shape: &Shape, +) -> (std::rc::Rc, f32) { + let selrect = shape.selrect(); + let layout = text_content.vertical_layout(&selrect); + let origin_x = + text_vertical::block_axis_offset(selrect.width(), layout.width, shape.vertical_align()); + (layout, origin_x) +} + +fn calculate_vertical_cursor_rect( + text_content: &TextContent, + shape: &Shape, + paragraph: usize, + offset: usize, +) -> Option { + let (layout, origin_x) = vertical_layout_for_shape(text_content, shape); + let rect = text_vertical::caret_rect(&layout, paragraph, offset)?; + Some(Rect::from_xywh( + origin_x + rect.x(), + rect.y(), + rect.width(), + rect.height(), + )) +} + +/// Vertical-layout boxes, shape-local, of the text between `start` and `end` +/// (paragraph, char offset). +fn vertical_range_rects( + start: (usize, usize), + end: (usize, usize), + text_content: &TextContent, + shape: &Shape, +) -> Vec { + let (start_paragraph, start_offset) = start; + let (end_paragraph, end_offset) = end; + let paragraphs = text_content.paragraphs(); + let (layout, origin_x) = vertical_layout_for_shape(text_content, shape); + let mut rects = Vec::new(); + + for (para_idx, paragraph) in paragraphs + .iter() + .enumerate() + .take(end_paragraph + 1) + .skip(start_paragraph) + { + let para_char_count: usize = paragraph + .children() + .iter() + .map(|span| span.text.chars().count()) + .sum(); + let range_start = if para_idx == start_paragraph { + start_offset + } else { + 0 + }; + let range_end = if para_idx == end_paragraph { + end_offset + } else { + para_char_count + }; + for rect in text_vertical::range_rects(&layout, para_idx, range_start, range_end) { + rects.push(Rect::from_xywh( + origin_x + rect.x(), + rect.y(), + rect.width(), + rect.height(), + )); + } + } + rects +} + +/// Caret rectangle relative to the shape's top-left corner. +pub(crate) fn cursor_rect( editor_state: &TextEditorState, text_content: &TextContent, shape: &Shape, @@ -135,92 +225,103 @@ fn calculate_cursor_rect( return None; } + if text_content.is_vertical() { + return calculate_vertical_cursor_rect( + text_content, + shape, + cursor.paragraph, + cursor.offset, + ); + } + let layout_paragraphs: Vec<_> = text_content.layout.paragraphs.iter().flatten().collect(); + let laid_out_para = layout_paragraphs.get(cursor.paragraph)?; + let y_offset = paragraphs_vertical_offset(shape, &layout_paragraphs) + + layout_paragraphs[..cursor.paragraph] + .iter() + .map(|paragraph| paragraph.height()) + .sum::(); + let plans = text_content.horizontal_plans(); + let plan = plans.get(cursor.paragraph)?; + let model = ¶graphs[cursor.paragraph]; + let rect = + horizontal_caret_rect_with_offsets(model, laid_out_para, cursor.offset, &plan.offsets); + Some(rect.with_offset((plan.x_offset(model, shape.selrect.width()), y_offset))) +} - if cursor.paragraph >= layout_paragraphs.len() { - return None; +/// Caret rect at the source character offset `char_pos` of a laid-out +/// horizontal paragraph, in the paragraph's coordinates. It covers the +/// character at the offset (overtype carets use its width), or follows the +/// last character at the paragraph end. +#[cfg(test)] +fn horizontal_caret_rect( + para: &crate::shapes::Paragraph, + laid_out_para: &LayoutParagraph, + char_pos: usize, +) -> Rect { + horizontal_caret_rect_with_offsets( + para, + laid_out_para, + char_pos, + &crate::shapes::HorizontalOffsets::new(para), + ) +} + +fn horizontal_caret_rect_with_offsets( + para: &crate::shapes::Paragraph, + laid_out_para: &LayoutParagraph, + char_pos: usize, + offsets: &crate::shapes::HorizontalOffsets, +) -> Rect { + if let Some(rect) = crate::shapes::horizontal_warichu_caret_rect(para, laid_out_para, char_pos) + { + return rect; + } + let para_char_count: usize = para + .children() + .iter() + .map(|span| span.text.chars().count()) + .sum(); + let default_rect = || Rect::from_xywh(0.0, 0.0, 1.0, laid_out_para.height()); + if para_char_count == 0 { + return default_rect(); } - let mut y_offset = paragraphs_vertical_offset(shape, &layout_paragraphs); - for (idx, laid_out_para) in layout_paragraphs.iter().enumerate() { - if idx == cursor.paragraph { - let char_pos = cursor.offset; - // For cursor, we get a zero-width range at the position - // We need to handle edge cases: - // - At start of paragraph: use position 0 - // - At end of paragraph: use last position - let para = ¶graphs[cursor.paragraph]; - let para_char_count: usize = para - .children() - .iter() - .map(|span| span.text.chars().count()) - .sum(); - - // Skia ranges are UTF-16 code units, not characters. - let (cursor_x, cursor_y, cursor_width, cursor_height) = if para_char_count == 0 { - // Empty paragraph - use default height - (0.0, 0.0, 1.0, laid_out_para.height()) - } else if char_pos == 0 { - let rects = laid_out_para.get_rects_for_range( - 0..para.char_offset_to_utf16(para.next_grapheme_offset(0)), - RectHeightStyle::Max, - RectWidthStyle::Tight, - ); - if !rects.is_empty() { - let r = &rects[0].rect; - (r.left(), r.top(), r.width(), r.height()) - } else { - (0.0, 0.0, 1.0, laid_out_para.height()) - } - } else if char_pos >= para_char_count { - let last_start = - para.char_offset_to_utf16(para.prev_grapheme_offset(para_char_count)); - let rects = laid_out_para.get_rects_for_range( - last_start..para.char_offset_to_utf16(para_char_count), - RectHeightStyle::Max, - RectWidthStyle::Tight, - ); - if !rects.is_empty() { - let r = &rects[0].rect; - (r.right(), r.top(), r.width(), r.height()) - } else if let Some(line) = laid_out_para.get_line_metrics().last() { - ( - line.left as f32 + line.width as f32, - 0.0, - 1.0, - laid_out_para.height(), - ) - } else { - (0.0, 0.0, 1.0, laid_out_para.height()) - } - } else { - let utf16_pos = para.char_offset_to_utf16(char_pos); - let rects = laid_out_para.get_rects_for_range( - utf16_pos..para.char_offset_to_utf16(para.next_grapheme_offset(char_pos)), - RectHeightStyle::Max, - RectWidthStyle::Tight, - ); - if !rects.is_empty() { - let r = &rects[0].rect; - (r.left(), r.top(), r.width(), r.height()) - } else { - // Fallback: use glyph position - let pos = laid_out_para.get_glyph_position_at_coordinate((0.0, 0.0)); - (pos.position as f32, 0.0, 1.0, laid_out_para.height()) - } - }; - - return Some(Rect::from_xywh( - cursor_x, - y_offset + cursor_y, - cursor_width, // cursor_width - cursor_height, - )); + // Cursor offsets count source characters; the laid-out paragraph indexes + // the transformed, kinsoku-shifted builder text. + let first_rect = |start: usize, end: usize| { + laid_out_para + .get_rects_for_range(start..end, RectHeightStyle::Max, RectWidthStyle::Tight) + .first() + .map(|textbox| textbox.rect) + }; + if char_pos >= para_char_count { + let last_start = offsets.source_to_builder(para.prev_grapheme_offset(para_char_count)); + let last_end = offsets.source_to_builder(para_char_count); + return match first_rect(last_start, last_end) { + Some(r) => Rect::from_xywh(r.right(), r.top(), r.width(), r.height()), + None => match laid_out_para.get_line_metrics().last() { + Some(line) => Rect::from_xywh( + line.left as f32 + line.width as f32, + 0.0, + 1.0, + laid_out_para.height(), + ), + None => default_rect(), + }, + }; + } + let start = offsets.source_to_builder(char_pos); + let end = offsets.source_to_builder(para.next_grapheme_offset(char_pos)); + match first_rect(start, end) { + Some(rect) => rect, + None if char_pos == 0 => default_rect(), + None => { + // Fallback: use glyph position + let pos = laid_out_para.get_glyph_position_at_coordinate((0.0, 0.0)); + Rect::from_xywh(pos.position as f32, 0.0, 1.0, laid_out_para.height()) } - y_offset += laid_out_para.height(); } - - None } fn calculate_selection_rects( @@ -251,6 +352,27 @@ pub(crate) fn range_origin( .map(|rect| Point::new(rect.left(), rect.top())) } +/// Box in page coordinates around the line boxes of the text between `start` +/// and `end` (paragraph, char offset), mapped through the shape transform: on +/// a rotated text it encloses the text as drawn. `None` when the range covers +/// no text. +pub(crate) fn range_page_bounds( + start: (usize, usize), + end: (usize, usize), + text_content: &TextContent, + shape: &Shape, +) -> Option { + let matrix = shape.get_matrix(); + let bounds = range_rects(start, end, text_content, shape) + .iter() + .map(|rect| matrix.map_rect(rect).0) + .reduce(|mut bounds, rect| { + bounds.join(rect); + bounds + })?; + (!bounds.is_empty()).then_some(bounds) +} + /// Line boxes, shape-local, of the text between `start` and `end` (paragraph, /// char offset). fn range_rects( @@ -259,9 +381,14 @@ fn range_rects( text_content: &TextContent, shape: &Shape, ) -> Vec { + if text_content.is_vertical() { + return vertical_range_rects(start, end, text_content, shape); + } + let mut rects = Vec::new(); let (start_paragraph, start_offset) = start; let (end_paragraph, end_offset) = end; + let plans = text_content.horizontal_plans(); let paragraphs = text_content.paragraphs(); let layout_paragraphs: Vec<_> = text_content.layout.paragraphs.iter().flatten().collect(); @@ -279,6 +406,8 @@ fn range_rects( // Calculate character range for this paragraph let para = ¶graphs[para_idx]; + let plan = &plans[para_idx]; + let x_offset = plan.x_offset(para, shape.selrect.width()); let para_char_count: usize = para .children() .iter() @@ -298,22 +427,41 @@ fn range_rects( }; if range_start < range_end { - use skia_safe::textlayout::{RectHeightStyle, RectWidthStyle}; - let text_boxes = laid_out_para.get_rects_for_range( - para.char_offset_to_utf16(range_start)..para.char_offset_to_utf16(range_end), - RectHeightStyle::Max, - RectWidthStyle::Tight, + // Selection offsets live in original text space; the + // laid-out paragraph indexes the kinsoku-shifted text. + let warichu_rects = crate::shapes::horizontal_warichu_range_rects( + para, + laid_out_para, + range_start, + range_end, ); - - for text_box in text_boxes { - let r = text_box.rect; + for r in warichu_rects { rects.push(Rect::from_xywh( - r.left(), + r.left() + x_offset, y_offset + r.top(), r.width(), r.height(), )); } + use skia_safe::textlayout::{RectHeightStyle, RectWidthStyle}; + for builder_range in plan + .offsets + .normal_selection_ranges(para, range_start, range_end) + { + for text_box in laid_out_para.get_rects_for_range( + builder_range, + RectHeightStyle::Max, + RectWidthStyle::Tight, + ) { + let r = text_box.rect; + rects.push(Rect::from_xywh( + r.left() + x_offset, + y_offset + r.top(), + r.width(), + r.height(), + )); + } + } } y_offset += para_height; @@ -331,6 +479,10 @@ mod tests { FontFamily, FontStyle, GrowType, Paragraph, TextAlign, TextDirection, TextSpan, }; use crate::uuid::Uuid; + use skia_safe::textlayout::{ + FontCollection, ParagraphBuilder, ParagraphStyle, TextStyle, TypefaceFontProvider, + }; + use skia_safe::FontMgr; /// A laid-out fixed text, `width` wide, with a single span of `text`. fn text_shape(text: &str, width: f32) -> Shape { @@ -400,6 +552,54 @@ mod tests { assert!(world.y > 10.0, "{world:?}"); } + fn page_bounds_of(shape: &Shape, start: usize, end: usize) -> Option { + let Type::Text(content) = &shape.shape_type else { + unreachable!() + }; + range_page_bounds((0, start), (0, end), content, shape) + } + + #[test] + fn range_page_bounds_are_offset_by_the_shape_position() { + let mut resources = RenderResources::try_new_headless().expect("headless resources"); + let _guard = TestRenderResourcesGuard::install(&mut resources); + let mut shape = text_shape("hello world", 500.0); + let local = page_bounds_of(&shape, 6, 11).expect("world has text"); + shape.set_selrect(100.0, 50.0, 600.0, 150.0); + + let page = page_bounds_of(&shape, 6, 11).expect("world has text"); + + assert!( + (page.left() - (local.left() + 100.0)).abs() < 0.01, + "{page:?}" + ); + assert!((page.top() - (local.top() + 50.0)).abs() < 0.01, "{page:?}"); + assert!((page.width() - local.width()).abs() < 0.01, "{page:?}"); + } + + #[test] + fn range_page_bounds_of_wrapped_text_cover_every_line() { + let mut resources = RenderResources::try_new_headless().expect("headless resources"); + let _guard = TestRenderResourcesGuard::install(&mut resources); + // Too narrow for both words on one line. + let shape = text_shape("hello world", 45.0); + + let hello = page_bounds_of(&shape, 0, 5).expect("hello has text"); + let both = page_bounds_of(&shape, 0, 11).expect("the text has text"); + + assert!(both.top() <= hello.top() + 0.01, "{both:?}"); + assert!(both.height() > hello.height() * 1.5, "{both:?}"); + } + + #[test] + fn range_page_bounds_are_none_for_an_empty_range() { + let mut resources = RenderResources::try_new_headless().expect("headless resources"); + let _guard = TestRenderResourcesGuard::install(&mut resources); + let shape = text_shape("hello", 500.0); + + assert!(page_bounds_of(&shape, 2, 2).is_none()); + } + #[test] fn range_origin_is_none_for_an_empty_range() { let mut resources = RenderResources::try_new_headless().expect("headless resources"); @@ -408,4 +608,302 @@ mod tests { assert!(origin_of(&shape, 2, 2).is_none()); } + + fn paragraph(text: &str) -> Paragraph { + crate::globals::design_init(); + let span = TextSpan { + text: text.to_string(), + font_size: 16.0, + ..TextSpan::default() + }; + Paragraph::new( + TextAlign::Left, + TextDirection::LTR, + None, + None, + 1.0, + 0.0, + vec![span], + ) + } + + fn laid_out(text: &str) -> LayoutParagraph { + let mut provider = TypefaceFontProvider::new(); + let typeface = FontMgr::new() + .new_from_data( + skia_safe::Data::new_copy(include_bytes!("../fonts/sourcesanspro-regular.ttf")), + None, + ) + .expect("test font"); + provider.register_typeface(typeface, Some("test")); + let mut fonts = FontCollection::new(); + fonts.set_default_font_manager(FontMgr::from(provider), None); + let mut style = TextStyle::new(); + style.set_font_families(&["test"]); + style.set_font_size(16.0); + let mut builder = ParagraphBuilder::new(&ParagraphStyle::default(), &fonts); + builder.push_style(&style); + builder.add_text(text); + let mut paragraph = LayoutParagraph::build(&mut builder); + paragraph.layout(1000.0); + paragraph + } + + fn glyph_rect(laid_out: &LayoutParagraph, range: std::ops::Range) -> Rect { + laid_out.get_rects_for_range(range, RectHeightStyle::Max, RectWidthStyle::Tight)[0].rect + } + + #[test] + fn caret_inside_the_text_covers_the_character_at_the_offset() { + let laid_out = laid_out("abc"); + let caret = horizontal_caret_rect(¶graph("abc"), &laid_out, 1); + assert_eq!(caret, glyph_rect(&laid_out, 1..2)); + } + + #[test] + fn caret_at_the_end_follows_the_last_character() { + let laid_out = laid_out("abc"); + let caret = horizontal_caret_rect(¶graph("abc"), &laid_out, 3); + let last = glyph_rect(&laid_out, 2..3); + assert_eq!(caret.left, last.right); + assert_eq!(caret.width(), last.width()); + } + + #[test] + fn caret_in_an_empty_paragraph_spans_its_height() { + let laid_out = laid_out(""); + let caret = horizontal_caret_rect(¶graph(""), &laid_out, 0); + assert_eq!(caret, Rect::from_xywh(0.0, 0.0, 1.0, laid_out.height())); + } + #[test] + fn narrow_cjk_layout_shares_alignment_with_carets_selection_hit_testing_and_exports() { + use crate::shapes::calculate_text_layout_data; + use crate::shapes::text_paths::TextPaths; + use crate::shapes::text_vertical::test_support::provider; + use crate::shapes::{GrowType, TextPositionWithAffinity, VerticalAlign}; + crate::globals::design_init(); + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let mut fonts = resources.fonts.font_collection().clone(); + fonts.set_asset_font_manager(Some( + provider(include_bytes!("../fonts/cjk-locl-test.ttf")).into(), + )); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + for align in [ + TextAlign::Left, + TextAlign::Right, + TextAlign::Center, + TextAlign::Justify, + ] { + for grow in [GrowType::Fixed, GrowType::AutoHeight] { + let spans = ["文文", "文", "。", "文文"] + .iter() + .map(|text| TextSpan { + text: text.to_string(), + font_size: 20.0, + line_height: 1.0, + ..TextSpan::default() + }) + .collect(); + let mut model = + Paragraph::new(align, TextDirection::LTR, None, None, 1.0, 0.0, spans); + model.set_language("zh-Hant-TW".to_owned()); + let mut content = + TextContent::new(crate::math::Rect::from_xywh(0.0, 0.0, 20.0, 300.0), grow); + content.add_paragraph(model.clone()); + content.add_paragraph(model); + let mut shape = Shape::new(crate::Uuid::nil()); + shape.set_selrect(0.0, 0.0, 20.0, 300.0); + content.update_layout(shape.selrect); + let plans = content.horizontal_plans(); + let mut editor = TextEditorState::new(); + let data = calculate_text_layout_data( + &shape, + &content, + &mut content.paragraph_builder_group_from_text(None), + false, + ); + let mut paragraph_y = 0.0; + for paragraph_index in 0..2 { + let laid_out = &content.layout.paragraphs[paragraph_index][0]; + let plan = &plans[paragraph_index]; + let x_offset = plan.x_offset(&content.paragraphs()[paragraph_index], 20.0); + let lines = laid_out.get_line_metrics(); + assert_eq!(lines.len(), 5, "{align:?} {grow:?}"); + let atomic_left = lines[2].left as f32 + x_offset; + let expected_left = match align { + TextAlign::Right => -20.0, + TextAlign::Center => -10.0, + _ => 0.0, + }; + assert!( + (atomic_left - expected_left).abs() < 0.02, + "{align:?}: {atomic_left}" + ); + assert!( + (lines[0].left as f32 + x_offset).abs() < 0.02, + "ordinary text stays within its box" + ); + for (offset, line_index) in [0, 1, 2, 2, 3, 4].into_iter().enumerate() { + let position = TextPositionWithAffinity::new_downstream_affinity( + paragraph_index, + offset, + ); + editor.selection.set_caret(position); + let caret = cursor_rect(&editor, &content, &shape).expect("caret"); + let line_top = paragraph_y + + lines[..line_index] + .iter() + .map(|line| line.height as f32) + .sum::(); + assert!( + (caret.top() - line_top).abs() < 0.1, + "caret {offset} in {align:?}: {caret:?}" + ); + let hit = content + .get_caret_position_from_shape_coords( + &crate::math::Point::new( + caret.left() + caret.width() * 0.1, + caret.center_y(), + ), + &shape.selrect, + VerticalAlign::Top, + ) + .expect("hit"); + assert_eq!( + (hit.paragraph, hit.offset), + (paragraph_index, offset), + "{align:?}, caret={caret:?}" + ); + } + editor.selection.anchor = + TextPositionWithAffinity::new_downstream_affinity(paragraph_index, 2); + editor.selection.focus = + TextPositionWithAffinity::new_downstream_affinity(paragraph_index, 4); + let selected = calculate_selection_rects(&editor.selection, &content, &shape); + assert!(!selected.is_empty()); + assert!(selected.iter().all(|rect| (rect.top() + - paragraph_y + - lines[..2] + .iter() + .map(|line| line.height as f32) + .sum::()) + .abs() + < 0.1)); + assert!((selected.iter().map(|r| r.width()).sum::() - 40.0).abs() < 0.1); + let exported = &data.paragraphs[paragraph_index]; + assert!((exported.x - x_offset).abs() < 0.01); + assert!((exported.y - paragraph_y).abs() < 0.01); + assert_eq!(exported.paragraph.get_line_metrics().len(), 5); + paragraph_y += laid_out.height(); + } + let paths = TextPaths::new(content.clone()).get_paths(VerticalAlign::Top); + assert!(!paths.is_empty()); + let mut bounds = skia_safe::Rect::new_empty(); + for path in paths { + bounds.join(path.bounds()); + } + assert!( + bounds.height() > paragraph_y - 30.0, + "paths include both paragraphs" + ); + assert!( + bounds.width() > 20.0, + "paths include the overflowing punctuation unit: {align:?} {bounds:?}" + ); + assert!( + content.get_height(160.0) < paragraph_y / 2.0, + "prospective width uses a fresh plan" + ); + let before: Vec<_> = plans.iter().map(|plan| plan.texts.clone()).collect(); + content.update_layout(crate::math::Rect::from_xywh(0.0, 0.0, 160.0, 300.0)); + assert!(content + .layout + .paragraphs + .iter() + .all(|group| group[0].get_line_metrics().len() == 1)); + assert!(content.horizontal_plans().iter().all(|plan| !plan + .texts + .concat() + .contains(crate::shapes::line_breaks::FORCED_LINE_BREAK))); + content.update_layout(shape.selrect); + assert_eq!( + content + .horizontal_plans() + .iter() + .map(|plan| plan.texts.clone()) + .collect::>(), + before, + "narrow/wide/narrow restores the plan" + ); + assert_eq!( + content.paragraphs()[0] + .children() + .iter() + .map(|s| s.text.as_str()) + .collect::(), + "文文文。文文" + ); + } + } + } + #[test] + fn narrow_cjk_fractional_strokes_share_the_fill_breaks_and_advances() { + use crate::shapes::text_vertical::test_support::provider; + use crate::shapes::{GrowType, Stroke, StrokeStyle}; + crate::globals::design_init(); + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let mut fonts = resources.fonts.font_collection().clone(); + fonts.set_asset_font_manager(Some( + provider(include_bytes!("../fonts/cjk-locl-test.ttf")).into(), + )); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + let spans = ["文文", "文", "。", "文文"] + .iter() + .map(|text| TextSpan { + text: text.to_string(), + font_size: 20.125, + line_height: 1.0, + ..TextSpan::default() + }) + .collect(); + let mut model = Paragraph::new( + TextAlign::Justify, + TextDirection::LTR, + None, + None, + 1.0, + 0.0, + spans, + ); + model.set_language("zh-Hant-TW".to_owned()); + let mut content = TextContent::new( + crate::math::Rect::from_xywh(0.0, 0.0, 32.25, 300.0), + GrowType::Fixed, + ); + content.add_paragraph(model); + content.update_layout(content.bounds()); + let stroke = Stroke::new_center_stroke(1.0, StrokeStyle::Solid, None, None, None, None); + let (mut builders, _) = crate::render::text::stroke_paragraph_builder_group_from_text( + &content, + &stroke, + &content.bounds(), + None, + ); + assert!(!builders[0].is_empty()); + let mut stroked = builders[0][0].build(); + stroked.layout(content.horizontal_plans()[0].layout_width(32.25)); + let fill_lines = content.layout.paragraphs[0][0].get_line_metrics(); + let stroke_lines = stroked.get_line_metrics(); + assert_eq!(fill_lines.len(), stroke_lines.len()); + for (fill, stroke) in fill_lines.iter().zip(&stroke_lines) { + assert_eq!( + (fill.start_index, fill.end_index), + (stroke.start_index, stroke.end_index) + ); + assert!((fill.width - stroke.width).abs() < 0.01); + assert!((fill.left - stroke.left).abs() < 0.01); + } + } } diff --git a/render-wasm/src/state/text_editor.rs b/render-wasm/src/state/text_editor.rs index 9857cb134f..44dbe7496c 100644 --- a/render-wasm/src/state/text_editor.rs +++ b/render-wasm/src/state/text_editor.rs @@ -284,6 +284,8 @@ pub struct TextComposition { pub previous: String, pub current: String, pub is_composing: bool, + /// Preview start: each update replaces `[start, start + previous]`. + pub start: Option, } impl TextComposition { @@ -292,6 +294,7 @@ impl TextComposition { previous: String::new(), current: String::new(), is_composing: false, + start: None, } } @@ -302,6 +305,7 @@ impl TextComposition { self.is_composing = true; self.previous = String::new(); self.current = String::new(); + self.start = None; true } @@ -319,22 +323,26 @@ impl TextComposition { return false; } self.is_composing = false; + self.start = None; true } + /// Range the next preview replaces: the previous preview, or `selection` + /// (the pre-composition selection) when there is none yet. pub fn get_selection(&self, selection: &TextSelection) -> TextSelection { - if self.previous.is_empty() { + let Some(start) = self.start.filter(|_| !self.previous.is_empty()) else { return *selection; - } + }; - let focus = selection.focus; - let previous_len = self.previous.chars().count(); let anchor = TextPositionWithAffinity::new_downstream_affinity( - focus.paragraph, - focus.offset + previous_len, + start.paragraph, + start.offset + self.previous.chars().count(), ); - TextSelection { anchor, focus } + TextSelection { + anchor, + focus: start, + } } } @@ -925,8 +933,23 @@ impl TextEditorState { TextDirection::LTR }; + // In vertical-rl, Up/Down move by character along the column and + // Left/Right cross columns (columns advance right-to-left). + let is_vertical = text_content.is_vertical(); + let direction = if is_vertical { + match direction { + CursorDirection::Backward => CursorDirection::LineAfter, // Left -> next column + CursorDirection::Forward => CursorDirection::LineBefore, // Right -> prev column + CursorDirection::LineBefore => CursorDirection::Backward, // Up -> prev char + CursorDirection::LineAfter => CursorDirection::Forward, // Down -> next char + other => other, + } + } else { + direction + }; + // For horizontal navigation, swap Backward/Forward when in RTL text - let adjusted_direction = if text_span_text_direction == TextDirection::RTL { + let adjusted_direction = if !is_vertical && text_span_text_direction == TextDirection::RTL { match direction { CursorDirection::Backward => CursorDirection::Forward, CursorDirection::Forward => CursorDirection::Backward, @@ -1259,4 +1282,31 @@ mod tests { assert!(editor.selection.is_collapsed()); assert_eq!(editor.selection.focus.offset, 2); } + + fn caret(paragraph: usize, offset: usize) -> TextSelection { + TextSelection::from_position_with_affinity( + TextPositionWithAffinity::new_downstream_affinity(paragraph, offset), + ) + } + + #[test] + fn composition_replaces_the_previous_preview_wherever_the_caret_is() { + let mut composition = TextComposition::new(); + composition.start(); + + // First update: the pre-composition caret is the replaced range. + composition.update("に"); + let replaced = composition.get_selection(&caret(0, 2)); + assert_eq!((replaced.start().offset, replaced.end().offset), (2, 2)); + composition.start = Some(replaced.start()); + + // The caret sits after the preview; the next update still replaces + // the preview itself. + composition.update("にほ"); + let replaced = composition.get_selection(&caret(0, 3)); + assert_eq!((replaced.start().offset, replaced.end().offset), (2, 3)); + + composition.end(); + assert!(composition.start.is_none()); + } } diff --git a/render-wasm/src/wasm/text_editor.rs b/render-wasm/src/wasm/text_editor.rs index 518a6271ff..645e5bed17 100644 --- a/render-wasm/src/wasm/text_editor.rs +++ b/render-wasm/src/wasm/text_editor.rs @@ -3,7 +3,7 @@ use macros::{wasm_error, ToJs}; use crate::globals::{get_render_state, get_text_editor_state}; use crate::math::{Matrix, Point}; use crate::mem; -use crate::render::text_editor::range_origin; +use crate::render::text_editor::{range_origin, range_page_bounds}; use crate::shapes::{TextAlign, TextContent, TextPositionWithAffinity, Type, VerticalAlign}; use crate::state::{State, TextEditorEvent, TextEditorState}; use crate::utils::uuid_from_u32_quartet; @@ -155,7 +155,11 @@ where }; let point = Point::new(x, y); - if let Some(position) = text_content.get_caret_position_from_shape_coords(&point) { + if let Some(position) = text_content.get_caret_position_from_shape_coords( + &point, + &shape.selrect(), + shape.vertical_align(), + ) { apply(get_text_editor_state(), text_content, &position); } }) @@ -209,7 +213,11 @@ pub extern "C" fn text_editor_pointer_down(x: f32, y: f32) { }; let point = Point::new(x, y); get_text_editor_state().start_pointer_selection(); - if let Some(position) = text_content.get_caret_position_from_shape_coords(&point) { + if let Some(position) = text_content.get_caret_position_from_shape_coords( + &point, + &shape.selrect(), + shape.vertical_align(), + ) { get_text_editor_state().place_caret_from_position(text_content, &position); } }); @@ -234,7 +242,11 @@ pub extern "C" fn text_editor_pointer_down_extend(x: f32, y: f32) { }; let point = Point::new(x, y); get_text_editor_state().start_pointer_selection(); - if let Some(position) = text_content.get_caret_position_from_shape_coords(&point) { + if let Some(position) = text_content.get_caret_position_from_shape_coords( + &point, + &shape.selrect(), + shape.vertical_align(), + ) { get_text_editor_state().extend_selection_from_position(&position); // The click after pointerup would collapse the caret and drop the // selection we just extended. @@ -268,7 +280,11 @@ pub extern "C" fn text_editor_pointer_move(x: f32, y: f32) { return; }; - if let Some(position) = text_content.get_caret_position_from_shape_coords(&point) { + if let Some(position) = text_content.get_caret_position_from_shape_coords( + &point, + &shape.selrect(), + shape.vertical_align(), + ) { get_text_editor_state().extend_selection_from_position(&position); // We need this flag to prevent handling the click behavior // just after a pointerup event. @@ -297,7 +313,11 @@ pub extern "C" fn text_editor_pointer_up(x: f32, y: f32) { let Type::Text(text_content) = &shape.shape_type else { return; }; - if let Some(position) = text_content.get_caret_position_from_shape_coords(&point) { + if let Some(position) = text_content.get_caret_position_from_shape_coords( + &point, + &shape.selrect(), + shape.vertical_align(), + ) { get_text_editor_state().extend_selection_from_position(&position); get_text_editor_state().update_styles(text_content); } @@ -332,7 +352,11 @@ pub extern "C" fn text_editor_set_cursor_from_offset(x: f32, y: f32) { return; }; - if let Some(position) = text_content.get_caret_position_from_shape_coords(&point) { + if let Some(position) = text_content.get_caret_position_from_shape_coords( + &point, + &shape.selrect(), + shape.vertical_align(), + ) { get_text_editor_state().place_caret_from_position(text_content, &position); } }); @@ -357,9 +381,13 @@ pub extern "C" fn text_editor_set_cursor_from_point(x: f32, y: f32) { let Type::Text(text_content) = &shape.shape_type else { return; }; - if let Some(position) = - text_content.get_caret_position_from_screen_coords(&point, &view_matrix, &shape_matrix) - { + if let Some(position) = text_content.get_caret_position_from_screen_coords( + &point, + &view_matrix, + &shape_matrix, + &shape.selrect(), + shape.vertical_align(), + ) { get_text_editor_state().place_caret_from_position(text_content, &position); } }); @@ -369,6 +397,21 @@ pub extern "C" fn text_editor_set_cursor_from_point(x: f32, y: f32) { // TEXT OPERATIONS // ============================================================================ +fn text_content_for_edit(state: &mut State, shape_id: Uuid) -> Option<&mut TextContent> { + if !matches!(state.shapes.get_raw(&shape_id)?.shape_type, Type::Text(_)) { + return None; + } + // Capture old tile coverage before changing the preview, then drop the + // shape snapshot and bounds so new glyphs and erased previews get indexed. + state.touch_shape(shape_id); + state.shapes.invalidate_shape_extrect(&shape_id); + let shape = state.shapes.get_mut(&shape_id)?; + match &mut shape.shape_type { + Type::Text(content) => Some(content), + _ => None, + } +} + #[no_mangle] #[wasm_error] pub extern "C" fn text_editor_composition_start() -> Result<()> { @@ -397,11 +440,7 @@ pub extern "C" fn text_editor_composition_end() -> Result<()> { return Ok(()); }; - let Some(shape) = state.shapes.get_mut(&shape_id) else { - return Ok(()); - }; - - let Type::Text(text_content) = &mut shape.shape_type else { + let Some(text_content) = text_content_for_edit(state, shape_id) else { return Ok(()); }; @@ -412,11 +451,16 @@ pub extern "C" fn text_editor_composition_end() -> Result<()> { .get_selection(&get_text_editor_state().selection); text_helpers::delete_selection_range(text_content, &selection); - let cursor = get_text_editor_state().selection.focus; + // Insert at the start of the replaced range (the pre-composition + // selection or the previous preview), not at a possibly stale + // selection focus, and advance the caret past the committed text. + let cursor = selection.start(); if let Some(new_cursor) = text_helpers::insert_text_with_newlines(text_content, &cursor, &text) { get_text_editor_state().selection.set_caret(new_cursor); + } else { + get_text_editor_state().selection.set_caret(cursor); } text_content.layout.clear(); @@ -425,8 +469,6 @@ pub extern "C" fn text_editor_composition_end() -> Result<()> { get_text_editor_state().push_event(crate::state::TextEditorEvent::ContentChanged); get_text_editor_state().push_event(crate::state::TextEditorEvent::NeedsLayout); - get_render_state().mark_touched(shape_id); - get_text_editor_state().composition.end(); }); @@ -452,11 +494,7 @@ pub extern "C" fn text_editor_composition_update() -> Result<()> { return Ok(()); }; - let Some(shape) = state.shapes.get_mut(&shape_id) else { - return Ok(()); - }; - - let Type::Text(text_content) = &mut shape.shape_type else { + let Some(text_content) = text_content_for_edit(state, shape_id) else { return Ok(()); }; @@ -467,22 +505,47 @@ pub extern "C" fn text_editor_composition_update() -> Result<()> { .get_selection(&get_text_editor_state().selection); text_helpers::delete_selection_range(text_content, &selection); - let cursor = get_text_editor_state().selection.focus; - text_helpers::insert_text_with_newlines(text_content, &cursor, &text); + // The preview always starts where the replaced range did; the caret + // goes after it, as in a native text field. + let cursor = selection.start(); + get_text_editor_state().composition.start = Some(cursor); + let caret = + text_helpers::insert_text_with_newlines(text_content, &cursor, &text).unwrap_or(cursor); + get_text_editor_state().selection.set_caret(caret); text_content.layout.clear(); get_text_editor_state().reset_blink(); get_text_editor_state().push_event(crate::state::TextEditorEvent::ContentChanged); get_text_editor_state().push_event(crate::state::TextEditorEvent::NeedsLayout); - - get_render_state().mark_touched(shape_id); }); crate::mem::free_bytes()?; Ok(()) } +/// Place the caret `offset` characters into the composition preview, where +/// the IME's own cursor is (clause conversion, cursor moves inside the +/// preedit). Clamped to the preview; ignored outside a composition. +#[no_mangle] +pub extern "C" fn text_editor_set_composition_cursor(offset: u32) { + let editor_state = get_text_editor_state(); + if !editor_state.has_focus || !editor_state.composition.is_composing { + return; + } + let Some(start) = editor_state.composition.start else { + return; + }; + let offset = (offset as usize).min(editor_state.composition.current.chars().count()); + editor_state + .selection + .set_caret(TextPositionWithAffinity::new_downstream_affinity( + start.paragraph, + start.offset + offset, + )); + editor_state.reset_blink(); +} + #[no_mangle] #[wasm_error] pub extern "C" fn text_editor_toggle_overtype_mode() -> Result<()> { @@ -509,11 +572,7 @@ pub extern "C" fn text_editor_insert_text() -> Result<()> { return Ok(()); }; - let Some(shape) = state.shapes.get_mut(&shape_id) else { - return Ok(()); - }; - - let Type::Text(text_content) = &mut shape.shape_type else { + let Some(text_content) = text_content_for_edit(state, shape_id) else { return Ok(()); }; @@ -543,8 +602,6 @@ pub extern "C" fn text_editor_insert_text() -> Result<()> { get_text_editor_state().reset_blink(); get_text_editor_state().push_event(TextEditorEvent::ContentChanged); get_text_editor_state().push_event(TextEditorEvent::NeedsLayout); - - get_render_state().mark_touched(shape_id); }); crate::mem::free_bytes()?; @@ -562,16 +619,11 @@ pub extern "C" fn text_editor_delete_backward(word_boundary: bool) { return; }; - let Some(shape) = state.shapes.get_mut(&shape_id) else { - return; - }; - - let Type::Text(text_content) = &mut shape.shape_type else { + let Some(text_content) = text_content_for_edit(state, shape_id) else { return; }; get_text_editor_state().delete_backward(text_content, word_boundary); - get_render_state().mark_touched(shape_id); }); } @@ -586,16 +638,11 @@ pub extern "C" fn text_editor_delete_forward(word_boundary: bool) { return; }; - let Some(shape) = state.shapes.get_mut(&shape_id) else { - return; - }; - - let Type::Text(text_content) = &mut shape.shape_type else { + let Some(text_content) = text_content_for_edit(state, shape_id) else { return; }; get_text_editor_state().delete_forward(text_content, word_boundary); - get_render_state().mark_touched(shape_id); }); } @@ -610,16 +657,11 @@ pub extern "C" fn text_editor_insert_paragraph() { return; }; - let Some(shape) = state.shapes.get_mut(&shape_id) else { - return; - }; - - let Type::Text(text_content) = &mut shape.shape_type else { + let Some(text_content) = text_content_for_edit(state, shape_id) else { return; }; get_text_editor_state().insert_paragraph(text_content); - get_render_state().mark_touched(shape_id); }); } @@ -663,6 +705,94 @@ pub extern "C" fn text_editor_move_cursor( // RENDERING & EXPORT // ============================================================================ +/// Caret rectangle in page coordinates (the unrotated selrect space of the +/// text overlay), regardless of the blink phase. The frontend keeps the IME +/// capture surface on it. +#[no_mangle] +pub extern "C" fn text_editor_get_cursor_rect() -> *mut u8 { + with_state!(state, { + let editor_state = get_text_editor_state(); + if !editor_state.has_focus { + return std::ptr::null_mut(); + } + + let Some(shape_id) = editor_state.active_shape_id else { + return std::ptr::null_mut(); + }; + + // A preview update clears the layout; rebuild it so the rect is + // current before the next render. + update_text_layout_if_needed(state, shape_id); + + let Some(shape) = state.shapes.get(&shape_id) else { + return std::ptr::null_mut(); + }; + + let Type::Text(text_content) = &shape.shape_type else { + return std::ptr::null_mut(); + }; + + let Some(rect) = crate::render::text_editor::cursor_rect(editor_state, text_content, shape) + else { + return std::ptr::null_mut(); + }; + + let selrect = shape.selrect(); + let mut bytes = vec![0u8; 16]; + bytes[0..4].copy_from_slice(&(selrect.x() + rect.left()).to_le_bytes()); + bytes[4..8].copy_from_slice(&(selrect.y() + rect.top()).to_le_bytes()); + bytes[8..12].copy_from_slice(&rect.width().to_le_bytes()); + bytes[12..16].copy_from_slice(&rect.height().to_le_bytes()); + mem::write_bytes(bytes) + }) +} + +/// Box in page coordinates around the selected text (x, y, width, height), +/// enclosing it as drawn on a rotated text. Null when the selection is +/// collapsed or covers no text. +#[no_mangle] +pub extern "C" fn text_editor_get_selection_rect() -> *mut u8 { + with_state!(state, { + let editor_state = get_text_editor_state(); + if !editor_state.selection.is_selection() { + return std::ptr::null_mut(); + } + + let Some(shape_id) = editor_state.active_shape_id else { + return std::ptr::null_mut(); + }; + + update_text_layout_if_needed(state, shape_id); + + let Some(shape) = state.shapes.get(&shape_id) else { + return std::ptr::null_mut(); + }; + + let Type::Text(text_content) = &shape.shape_type else { + return std::ptr::null_mut(); + }; + + let selection = &editor_state.selection; + let start = selection.start(); + let end = selection.end(); + let Some(rect) = range_page_bounds( + (start.paragraph, start.offset), + (end.paragraph, end.offset), + text_content, + shape, + ) else { + return std::ptr::null_mut(); + }; + + let mut bytes = vec![0u8; 16]; + bytes[0..4].copy_from_slice(&rect.x().to_le_bytes()); + bytes[4..8].copy_from_slice(&rect.y().to_le_bytes()); + bytes[8..12].copy_from_slice(&rect.width().to_le_bytes()); + bytes[12..16].copy_from_slice(&rect.height().to_le_bytes()); + mem::write_bytes(bytes) + }) +} + #[no_mangle] pub extern "C" fn text_editor_get_current_styles() -> *mut u8 { with_state!(state, { @@ -701,6 +831,7 @@ pub extern "C" fn text_editor_get_current_styles() -> *mut u8 { TextAlign::Right => RawTextAlign::Right as u32, TextAlign::End => RawTextAlign::Right as u32, TextAlign::Justify => RawTextAlign::Justify as u32, + TextAlign::Distribute => RawTextAlign::Distribute as u32, }) .unwrap_or(0); @@ -910,8 +1041,11 @@ pub extern "C" fn text_editor_export_content() -> *mut u8 { .replace('\r', "\\r") .replace('\t', "\\t"); span_parts.push(format!( - "{{\"p\":{},\"s\":{},\"t\":\"{}\"}}", - span.paragraph_position, span.span_position, escaped_text + "{{\"pp\":{},\"p\":{},\"s\":{},\"t\":\"{}\"}}", + para.source_position(), + span.paragraph_position, + span.span_position, + escaped_text )); } json_parts.push(format!("[{}]", span_parts.join(","))); @@ -1164,28 +1298,38 @@ pub extern "C" fn text_editor_restore_selection( #[cfg(test)] mod tests { use super::*; - use crate::globals::TestRenderResourcesGuard; - use crate::math::Rect; - use crate::render::RenderResources; - use crate::shapes::{FontFamily, FontStyle, GrowType, Paragraph, TextDirection, TextSpan}; + use crate::shapes::{Group, GrowType, Paragraph, TextDirection, TextSpan, WritingMode}; + use crate::tiles; - fn text_content(text: &str) -> TextContent { - let span = TextSpan::new( - text.to_string(), - FontFamily::new(Uuid::nil(), 400, FontStyle::Normal), - 14.0, - 1.2, - 0.0, - None, - None, - TextDirection::LTR, - 400, - Uuid::nil(), - vec![], - ); - let mut content = - TextContent::new(Rect::from_xywh(0.0, 0.0, 200.0, 100.0), GrowType::Fixed); - content.add_paragraph(Paragraph::new( + #[test] + fn history_selection_restores_utf16_ranges_without_splitting_surrogates() { + let mut resources = crate::render::RenderResources::try_new_headless().expect("resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + let (mut state, id) = preview_state(GrowType::Fixed, WritingMode::VerticalRl); + let content = text_content_for_edit(&mut state, id).unwrap(); + content.paragraphs_mut()[0].children_mut()[0].text = "A😀한B".into(); + let mut editor = TextEditorState::new(); + assert!(restore_editor_selection(&mut editor, content, 0, 1, 0, 3)); + assert_eq!(editor.selection.anchor.offset, 1); + assert_eq!(editor.selection.focus.offset, 2); + assert!(restore_editor_selection(&mut editor, content, 0, 2, 9, 99)); + assert_eq!(editor.selection.anchor.offset, 1); + assert_eq!(editor.selection.focus.paragraph, 0); + assert_eq!(editor.selection.focus.offset, 4); + } + + fn preview_state(grow_type: GrowType, writing_mode: WritingMode) -> (State, Uuid) { + let id = Uuid::new_v4(); + let mut state = State::new(); + let shape = state.shapes.add_shape(id); + shape.set_selrect(250.0, 250.0, 274.0, 310.0); + let mut content = TextContent::new(shape.selrect(), grow_type); + let span = TextSpan { + text: "あ".to_string(), + font_size: 20.0, + ..TextSpan::default() + }; + let mut paragraph = Paragraph::new( TextAlign::Left, TextDirection::LTR, None, @@ -1193,22 +1337,108 @@ mod tests { 1.2, 0.0, vec![span], - )); - content + ); + paragraph.set_writing_mode(writing_mode); + content.add_paragraph(paragraph); + content.update_layout(shape.selrect()); + shape.set_shape_type(Type::Text(content)); + (state, id) + } + + fn replace_preview(state: &mut State, id: Uuid, editor: &mut TextEditorState, text: &str) { + let content = text_content_for_edit(state, id).expect("text shape"); + editor.composition.update(text); + let selection = editor.composition.get_selection(&editor.selection); + text_helpers::delete_selection_range(content, &selection); + let cursor = selection.start(); + editor.composition.start = Some(cursor); + let caret = + text_helpers::insert_text_with_newlines(content, &cursor, text).unwrap_or(cursor); + editor.selection.set_caret(caret); + content.layout.clear(); + update_text_layout_if_needed(state, id); } #[test] - fn history_selection_restores_utf16_ranges_without_splitting_surrogates() { - let mut resources = RenderResources::try_new_headless().expect("resources"); - let _guard = TestRenderResourcesGuard::install(&mut resources); - let content = text_content("A😀한B"); + fn composition_preview_reindexes_tiles_as_text_grows_and_shrinks() { + let mut resources = crate::render::RenderResources::try_new_headless().expect("resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + let long_preview = "あいうえおかきくけこさしすせそたちつてとなにぬねの".repeat(4); + for grow_type in [GrowType::AutoWidth, GrowType::AutoHeight] { + for writing_mode in [WritingMode::HorizontalTb, WritingMode::VerticalRl] { + let (mut state, id) = preview_state(grow_type, writing_mode); + let original = state.shapes.get(&id).unwrap().extrect(&state.shapes, 1.0); + let mut editor = TextEditorState::new(); + editor.composition.start(); + for text in [long_preview.as_str(), "日本語", ""] { + replace_preview(&mut state, id, &mut editor, text); + let shape = state.shapes.get(&id).unwrap(); + let mut uncached = shape.clone(); + uncached.invalidate_extrect(); + let expected = uncached.extrect(&state.shapes, 1.0); + let actual = shape.extrect(&state.shapes, 1.0); + assert_eq!( + actual, expected, + "{grow_type:?} {writing_mode:?} preview {text}" + ); + let current_tiles = tiles::get_tiles_for_rect(actual, tiles::TILE_SIZE); + let original_tiles = tiles::get_tiles_for_rect(original, tiles::TILE_SIZE); + if text.chars().count() > 20 { + assert_ne!(current_tiles, original_tiles, "preview reaches new tiles"); + } else if text.is_empty() { + assert_eq!( + current_tiles, original_tiles, + "cancellation restores coverage" + ); + } + } + assert_eq!( + state.shapes.get(&id).unwrap().extrect(&state.shapes, 1.0), + original + ); + } + } + } + + #[test] + fn composition_preview_updates_cached_group_bounds() { + let mut resources = crate::render::RenderResources::try_new_headless().expect("resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + let (mut state, id) = preview_state(GrowType::AutoWidth, WritingMode::HorizontalTb); + let parent_id = Uuid::new_v4(); + let parent = state.shapes.add_shape(parent_id); + parent.set_shape_type(Type::Group(Group::default())); + parent.clip_content = false; + parent.children.push(id); + state.shapes.get_mut(&id).unwrap().set_parent(parent_id); + let original = state + .shapes + .get(&parent_id) + .unwrap() + .extrect(&state.shapes, 1.0); let mut editor = TextEditorState::new(); - assert!(restore_editor_selection(&mut editor, &content, 0, 1, 0, 3)); - assert_eq!(editor.selection.anchor.offset, 1); - assert_eq!(editor.selection.focus.offset, 2); - assert!(restore_editor_selection(&mut editor, &content, 0, 2, 9, 99)); - assert_eq!(editor.selection.anchor.offset, 1); - assert_eq!(editor.selection.focus.paragraph, 0); - assert_eq!(editor.selection.focus.offset, 4); + editor.composition.start(); + replace_preview(&mut state, id, &mut editor, "あいうえおかきくけこ"); + let parent = state.shapes.get(&parent_id).unwrap(); + assert!(parent.extrect(&state.shapes, 1.0).right() > original.right()); + } + + #[test] + fn composition_preview_updates_modified_shape_snapshot() { + let mut resources = crate::render::RenderResources::try_new_headless().expect("resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + let (mut state, id) = preview_state(GrowType::AutoWidth, WritingMode::HorizontalTb); + state + .shapes + .set_modifiers(std::collections::HashMap::from([( + id, + Matrix::translate((20.0, 30.0)), + )])); + let original = state.shapes.get(&id).unwrap().extrect(&state.shapes, 1.0); + let mut editor = TextEditorState::new(); + editor.composition.start(); + replace_preview(&mut state, id, &mut editor, "あいうえおかきくけこ"); + let shape = state.shapes.get(&id).unwrap(); + assert!(shape.extrect(&state.shapes, 1.0).right() > original.right()); } }