diff --git a/common/src/app/common/fonts.cljs b/common/src/app/common/fonts.cljs index ee96b6c9df..9c30aee95c 100644 --- a/common/src/app/common/fonts.cljs +++ b/common/src/app/common/fonts.cljs @@ -163,7 +163,10 @@ (def ^:private unicode-ranges {:japanese #"[\u3040-\u30FF\u31F0-\u31FF\uFF66-\uFF9F]" - :chinese #"[\u4E00-\u9FFF\u3400-\u4DBF]" + ;; Han ideographs, shared by all CJK languages; see `resolve-ambiguous-cjk`. + :han #"[\u4E00-\u9FFF\u3400-\u4DBF\uF900-\uFAFF]" + ;; CJK punctuation and half/full-width forms, shared; see `resolve-ambiguous-cjk`. + :cjk-punctuation #"[\u3000-\u303F\uFF01-\uFF65\uFFE0-\uFFEE]" :korean #"[\uAC00-\uD7AF]" :arabic #"[\u0600-\u06FF\u0750-\u077F\u0870-\u089F\u08A0-\u08FF]" :cyrillic #"[\u0400-\u04FF\u0500-\u052F\u2DE0-\u2DFF\uA640-\uA69F]" @@ -241,6 +244,41 @@ used unicode-ranges)) +(defn- language->cjk-font + "CJK font class implied by a BCP 47 language tag or an app locale + (`ja`, `zh-Hant-TW`, `zh_hant`), or nil for a non-CJK language." + [language] + (let [[primary & subtags] (str/split (str/lower (str language)) #"[-_]") + subtags (set subtags)] + (case primary + "ja" :japanese + "ko" :korean + "zh" (cond + (some subtags ["hk" "mo"]) :chinese-hongkong + (some subtags ["tw" "hant"]) :chinese-traditional + :else :chinese) + nil))) + +(defn resolve-ambiguous-cjk + "Resolves the ambiguous CJK classes (:han, :cjk-punctuation) to a + concrete font class: a CJK paragraph `language` wins, then an + unambiguous script in the same content (kana implies Japanese, hangul + implies Korean), then the app `locale`; Chinese is the final default. + `language` and `locale` are nil when unset." + [langs language locale] + (if (or (contains? langs :han) + (contains? langs :cjk-punctuation)) + (let [resolved (or (language->cjk-font language) + (cond + (contains? langs :japanese) :japanese + (contains? langs :korean) :korean) + (language->cjk-font locale) + :chinese)] + (-> langs + (disj :han :cjk-punctuation) + (conj resolved))) + langs)) + (defn add-emoji-font [fonts] (conj fonts {:font-id "gfont-noto-color-emoji" @@ -253,6 +291,8 @@ (def noto-fonts {:japanese {:font-id "gfont-noto-sans-jp" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true} :chinese {:font-id "gfont-noto-sans-sc" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true} + :chinese-traditional {:font-id "gfont-noto-sans-tc" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true} + :chinese-hongkong {:font-id "gfont-noto-sans-hk" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true} :korean {:font-id "gfont-noto-sans-kr" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true} :arabic {:font-id "gfont-noto-sans-arabic" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true} :cyrillic {:font-id "gfont-noto-sans" :font-variant-id "regular" :style 0 :weight 400 :is-fallback true} diff --git a/common/src/app/common/render_wasm/enums.clj b/common/src/app/common/render_wasm/enums.clj index 0e73f76142..3e3284fcb8 100644 --- a/common/src/app/common/render_wasm/enums.clj +++ b/common/src/app/common/render_wasm/enums.clj @@ -36,6 +36,21 @@ ["fill-data" "RawFillData"] ["text-align" "RawTextAlign"] ["text-direction" "RawTextDirection"] + ["writing-mode" "RawWritingMode"] + ["text-orientation" "RawTextOrientation"] + ["text-combine-upright" "RawTextCombineUpright"] + ["text-emphasis" "RawTextEmphasis"] + ["text-emphasis-position" "RawTextEmphasisPosition"] + ["text-underline-position" "RawTextUnderlinePosition"] + ["warichu" "RawWarichu"] + ["font-features" "RawFontFeatures"] + ["annotation-clearance" "RawAnnotationClearance"] + ["ruby-size" "RawRubySize"] + ["ruby-align" "RawRubyAlign"] + ["ruby-overhang" "RawRubyOverhang"] + ["ruby-side" "RawRubySide"] + ["korean-wrap" "RawKoreanWrap"] + ["line-adjustment" "RawLineAdjustment"] ["text-decoration" "RawTextDecoration"] ["text-transform" "RawTextTransform"] ["list-style-type" "RawListStyleType"] diff --git a/common/src/app/common/render_wasm/serializers.cljs b/common/src/app/common/render_wasm/serializers.cljs index b3db247e10..cbd9a4ef12 100644 --- a/common/src/app/common/render_wasm/serializers.cljs +++ b/common/src/app/common/render_wasm/serializers.cljs @@ -9,10 +9,12 @@ [app.common.data :as d] [app.common.data.macros :as dm] [app.common.files.helpers :as cfh] + [app.common.logging :as log] [app.common.render-wasm.serializers.color :as sr-clr] [app.common.render-wasm.wasm :as wasm] [app.common.types.color :as clr] [app.common.types.shape-tree :as ctst] + [app.common.types.text.cjk-layout :as cjk] [app.common.uuid :as uuid] [cuerdas.core :as str])) @@ -312,6 +314,21 @@ [list-style-type] (untranslate "list-style-type" list-style-type "none")) +(defn translate-text-layout-enum + "Wasm discriminant of the value of the CJK text layout enum `attr`. An + unset value takes the default; an unknown one also takes it, with a + warning, since content validation should have rejected it. 0 when the + enum is not exported." + [attr value] + (let [default (cjk/enum-default attr) + value (some-> value d/name) + value (if (str/blank? value) default value)] + (if-let [values (unchecked-get wasm/serializers (name attr))] + (if-let [discriminant (unchecked-get values value)] + discriminant + (do (log/wrn :hint "unknown text layout value" :attr attr :value value) + (unchecked-get values default))) + 0))) (defn translate-font-style [font-style] diff --git a/common/src/app/common/render_wasm/text_content.cljs b/common/src/app/common/render_wasm/text_content.cljs index a19fb47c47..60d3384cd3 100644 --- a/common/src/app/common/render_wasm/text_content.cljs +++ b/common/src/app/common/render_wasm/text_content.cljs @@ -6,8 +6,9 @@ (ns app.common.render-wasm.text-content "Single source of truth for writing a text shape's content into the WASM design - state. The binary layout ([num-spans][paragraph attrs][span attrs][text]) is - identical for the workspace and the headless exporter, and so is the font-id + state. The binary layout is [num-spans][paragraph attrs][span attrs][text] + [ruby-text][language], identical for the workspace and headless exporter. + Both hosts also share the font-id -> uuid mapping (`cfnt/font-id->uuid`). Only *variant* resolution differs — the workspace has a loaded fonts DB, the exporter does not — so that part is injected via the `opts` map passed to `write-shape-text!`. @@ -21,11 +22,12 @@ [app.common.render-wasm.serializers :as sr] [app.common.render-wasm.wasm :as wasm] [app.common.types.fills.impl :as types.fills.impl] + [app.common.types.text.cjk-layout :as cjk] [app.common.uuid :as uuid] [cuerdas.core :as str])) -(def ^:const PARAGRAPH-ATTR-U8-SIZE 16) -(def ^:const SPAN-ATTR-U8-SIZE 64) +(def ^:const PARAGRAPH-ATTR-U8-SIZE 20) +(def ^:const SPAN-ATTR-U8-SIZE 80) (def ^:const MAX-TEXT-FILLS types.fills.impl/MAX-FILLS) (def ^:private default-font-size 14) @@ -75,6 +77,13 @@ ;; --- binary writers -------------------------------------------------------- +(defn- span-ruby + "Ruby annotation text sent to WASM; hidden ruby is omitted." + [span] + (if (true? (:ruby-hidden span)) + "" + (d/nilv (:ruby span) ""))) + (defn- encode-text "Into an UTF8 buffer. Returns an ArrayBuffer instance." [text] @@ -104,13 +113,16 @@ (defn- write-paragraph [offset dview paragraph] - (let [text-align (sr/translate-text-align (get paragraph :text-align)) - text-direction (sr/translate-text-direction (get paragraph :text-direction)) - text-decoration (sr/translate-text-decoration (get paragraph :text-decoration)) - text-transform (sr/translate-text-transform (get paragraph :text-transform)) - line-height (serialize-line-height (get paragraph :line-height)) - letter-spacing (serialize-letter-spacing (get paragraph :letter-spacing)) - list-style-type (sr/translate-list-style-type (get paragraph :list-style-type))] + (let [text-align (sr/translate-text-align (get paragraph :text-align)) + text-direction (sr/translate-text-direction (get paragraph :text-direction)) + text-decoration (sr/translate-text-decoration (get paragraph :text-decoration)) + text-transform (sr/translate-text-transform (get paragraph :text-transform)) + writing-mode (sr/translate-text-layout-enum :writing-mode (get paragraph :writing-mode)) + text-orientation (sr/translate-text-layout-enum :text-orientation (get paragraph :text-orientation)) + korean-wrap (sr/translate-text-layout-enum :korean-wrap (get paragraph :korean-wrap)) + line-height (serialize-line-height (get paragraph :line-height)) + letter-spacing (serialize-letter-spacing (get paragraph :letter-spacing)) + list-style-type (sr/translate-list-style-type (get paragraph :list-style-type))] (-> offset (mem/write-u8 dview text-align) (mem/write-u8 dview text-direction) @@ -119,10 +131,10 @@ (mem/write-f32 dview line-height) (mem/write-f32 dview letter-spacing) (mem/write-u8 dview list-style-type) - ;; padding - (mem/write-u8 dview 0) - (mem/write-u8 dview 0) - (mem/write-u8 dview 0) + (mem/write-u8 dview writing-mode) + (mem/write-u8 dview text-orientation) + (mem/write-u8 dview korean-wrap) + (mem/write-u32 dview (mem/size (encode-text (d/nilv (:text-language paragraph) "")))) (mem/assert-written offset PARAGRAPH-ATTR-U8-SIZE)))) (defn- write-spans @@ -131,7 +143,7 @@ paragraph-font-weight (-> paragraph :font-weight serialize-font-weight) paragraph-line-height (serialize-line-height (get paragraph :line-height))] (reduce - (fn [offset span] + (fn [offset [previous span]] (let [font-style (sr/translate-font-style (get span :font-style "normal")) font-size (serialize-font-size (get span :font-size paragraph-font-size)) line-height (serialize-line-height (get span :line-height) paragraph-line-height) @@ -141,6 +153,8 @@ font-family (hash (get span :font-family "sourcesanspro")) text-buffer (encode-text (get span :text "")) text-length (mem/size text-buffer) + ruby-buffer (encode-text (span-ruby span)) + ruby-length (mem/size ruby-buffer) fills (take MAX-TEXT-FILLS (get span :fills [])) font-variant-id (get span :font-variant-id) font-variant-id (if (uuid? font-variant-id) font-variant-id uuid/zero) @@ -152,12 +166,43 @@ (sr/translate-text-transform "none")) text-direction (or (sr/translate-text-direction (:text-direction span)) (sr/translate-text-direction (:text-direction paragraph)) - (sr/translate-text-direction "ltr"))] + (sr/translate-text-direction "ltr")) + text-orientation (sr/translate-text-layout-enum + :text-orientation + (get span :text-orientation (get paragraph :text-orientation))) + text-combine-upright (sr/translate-text-layout-enum :text-combine-upright (get span :text-combine-upright)) + text-emphasis (sr/translate-text-layout-enum :text-emphasis (get span :text-emphasis)) + warichu (sr/translate-text-layout-enum :warichu (get span :warichu)) + font-features (sr/translate-text-layout-enum :font-features (get span :font-features)) + annotation-clearance (sr/translate-text-layout-enum :annotation-clearance (get span :annotation-clearance)) + ruby-size (sr/translate-text-layout-enum :ruby-size (get span :ruby-size)) + ruby-align (sr/translate-text-layout-enum :ruby-align (get span :ruby-align)) + ruby-overhang (sr/translate-text-layout-enum :ruby-overhang (get span :ruby-overhang)) + ruby-side (sr/translate-text-layout-enum :ruby-side (get span :ruby-side)) + emphasis-position (sr/translate-text-layout-enum :text-emphasis-position (get span :text-emphasis-position)) + underline-position (sr/translate-text-layout-enum :text-underline-position (get span :text-underline-position))] (-> offset (mem/write-u8 dview font-style) (mem/write-u8 dview text-decoration) (mem/write-u8 dview text-transform) (mem/write-u8 dview text-direction) + (mem/write-u8 dview text-orientation) + (mem/write-u8 dview text-combine-upright) + (mem/write-u8 dview text-emphasis) + (mem/write-u8 dview warichu) + (mem/write-u8 dview font-features) + (mem/write-u8 dview annotation-clearance) + (mem/write-u8 dview ruby-size) + (mem/write-u8 dview ruby-align) + (mem/write-u8 dview ruby-overhang) + ;; Bit 1 selects Zhuyin; bit 2 joins the span to the reading of + ;; the span before it (`cjk/ruby-groups`). + (mem/write-u8 dview (bit-or ruby-side + (if (true? (:ruby-zhuyin span)) 2 0) + (if (cjk/ruby-joins-previous? previous span) 4 0))) + ;; Use the former padding bytes; the span header stays the same size. + (mem/write-u8 dview emphasis-position) + (mem/write-u8 dview underline-position) (mem/write-f32 dview font-size) (mem/write-f32 dview line-height) (mem/write-f32 dview letter-spacing) @@ -166,15 +211,18 @@ (mem/write-i32 dview font-family) (mem/write-uuid dview (d/nilv font-variant-id uuid/zero)) (mem/write-i32 dview text-length) + (mem/write-i32 dview ruby-length) (mem/write-i32 dview (count fills)) (mem/assert-written offset SPAN-ATTR-U8-SIZE) (write-span-fills dview fills)))) offset - spans))) + (map vector (cons nil spans) spans)))) (defn write-shape-text! "Writes one paragraph's spans + text into WASM and appends it to the current - shape via `_set_shape_text_content`. + shape via `_set_shape_text_content`. Ruby annotations are concatenated per + span, in span order, after the base text; the reader splits them using the + per-span byte lengths. `opts` injects host-specific font handling: - `:normalize-font-id` (string font-id -> wasm uuid) defaults to the shared @@ -187,21 +235,26 @@ :or {normalize-font-id cfnt/font-id->uuid normalize-paragraph identity normalize-span (fn [span _paragraph] span)}}] - (let [paragraph (normalize-paragraph paragraph) - spans (map #(normalize-span % paragraph) spans) - num-spans (count spans) - fills-size (* types.fills.impl/FILL-U8-SIZE MAX-TEXT-FILLS) - metadata-size (+ PARAGRAPH-ATTR-U8-SIZE - (* num-spans (+ SPAN-ATTR-U8-SIZE fills-size))) - text-buffer (encode-text text) - text-size (mem/size text-buffer) - total-size (+ 4 metadata-size text-size) - offset (mem/alloc total-size) - heapu8 (mem/get-heap-u8) - dview (mem/get-data-view)] + (let [paragraph (normalize-paragraph paragraph) + spans (map #(normalize-span % paragraph) spans) + num-spans (count spans) + fills-size (* types.fills.impl/FILL-U8-SIZE MAX-TEXT-FILLS) + metadata-size (+ PARAGRAPH-ATTR-U8-SIZE + (* num-spans (+ SPAN-ATTR-U8-SIZE fills-size))) + text-buffer (encode-text text) + text-size (mem/size text-buffer) + ruby-buffer (encode-text (apply str (map span-ruby spans))) + ruby-size (mem/size ruby-buffer) + language-buffer (encode-text (d/nilv (:text-language paragraph) "")) + total-size (+ 4 metadata-size text-size ruby-size (mem/size language-buffer)) + offset (mem/alloc total-size) + heapu8 (mem/get-heap-u8) + dview (mem/get-data-view)] (-> offset (mem/write-u32 dview num-spans) (write-paragraph dview paragraph) (write-spans dview spans paragraph normalize-font-id) - (mem/write-buffer heapu8 text-buffer)) + (mem/write-buffer heapu8 text-buffer) + (mem/write-buffer heapu8 ruby-buffer) + (mem/write-buffer heapu8 language-buffer)) (h/call wasm/internal-module "_set_shape_text_content"))) diff --git a/exporter/src/app/wasm/render.cljs b/exporter/src/app/wasm/render.cljs index 7889a6e3c5..d3becb8d33 100644 --- a/exporter/src/app/wasm/render.cljs +++ b/exporter/src/app/wasm/render.cljs @@ -298,14 +298,24 @@ several languages map to one noto family and provisioning is concurrent — otherwise they all miss the byte cache at once and refetch the same TTF." [scene] - (let [texts (for [shape (vals scene) - :when (= :text (:type shape)) - node (or (some->> (:content shape) (tree-seq :children :children)) []) - :let [text (:text node)] - :when (string? text)] - text) - emoji? (boolean (some cfnt/contains-emoji? texts)) - langs (reduce cfnt/collect-used-languages #{} texts)] + (let [paragraphs (for [shape (vals scene) + :when (= :text (:type shape)) + node (or (some->> (:content shape) (tree-seq :children :children)) []) + :when (= "paragraph" (:type node))] + node) + ;; Base text and ruby readings of each paragraph, which share its language. + texts (map (fn [paragraph] + (apply str (mapcat (juxt :text :ruby) (:children paragraph)))) + paragraphs) + emoji? (boolean (some cfnt/contains-emoji? texts)) + ;; The exporter has no user locale: Han follows the paragraph language, + ;; else the content, else Chinese. + langs (reduce into #{} + (map (fn [paragraph text] + (-> (cfnt/collect-used-languages #{} text) + (cfnt/resolve-ambiguous-cjk (:text-language paragraph) nil))) + paragraphs + texts))] (distinct (cond-> (cfnt/add-noto-fonts [] langs) emoji? (cfnt/add-emoji-font))))) diff --git a/exporter/src/app/wasm/text.cljs b/exporter/src/app/wasm/text.cljs index 13b037652b..33dd4ad470 100644 --- a/exporter/src/app/wasm/text.cljs +++ b/exporter/src/app/wasm/text.cljs @@ -16,7 +16,8 @@ (defn set-shape-text! "Serializes a text shape's content into the current WASM shape. Mirrors the - editor's sequence: clear -> vertical-align -> append each paragraph -> layout. + editor's sequence: clear -> vertical-align and line adjustment -> append each + paragraph -> layout. Byte writing and font resolution are the shared `text-content/write-shape-text!` defaults; the exporter has no fonts DB, so it injects no variant normalization." @@ -25,6 +26,8 @@ (h/call wasm/internal-module "_clear_shape_text") (h/call wasm/internal-module "_set_shape_vertical_align" (sr/translate-vertical-align (get content :vertical-align))) + (h/call wasm/internal-module "_set_shape_line_adjustment" + (sr/translate-text-layout-enum :line-adjustment (get content :line-adjustment))) (let [paragraph-set (first (get content :children)) paragraphs (get paragraph-set :children)] (doseq [paragraph paragraphs] diff --git a/frontend/src/app/main/ui/shapes/text/fo_text.cljs b/frontend/src/app/main/ui/shapes/text/fo_text.cljs index 13927d2fce..41cd9153dd 100644 --- a/frontend/src/app/main/ui/shapes/text/fo_text.cljs +++ b/frontend/src/app/main/ui/shapes/text/fo_text.cljs @@ -10,7 +10,9 @@ [app.common.data.macros :as dm] [app.common.geom.shapes :as gsh] [app.common.types.color :as cc] + [app.common.types.text.cjk-layout :as cjk] [app.main.ui.shapes.text.styles :as sts] + [app.util.text.writing-mode :as wm] [cuerdas.core :as str] [rumext.v2 :as mf])) @@ -19,9 +21,32 @@ (let [text (:text node) style (if (= text "") (sts/generate-text-styles shape parent) - (sts/generate-text-styles shape node))] - [:span.text-node {:style style} - (if (= text "") "\u00A0" text)])) + (sts/generate-text-styles shape node)) + ruby (cjk/visible-ruby node)] + (if (some? ruby) + [:ruby.ruby-node {:style (sts/generate-ruby-container-styles node)} + [:span.text-node {:style style} (sts/text-children text node)] + [:rt {:style (sts/generate-ruby-styles shape node)} ruby]] + [:span.text-node {:style style} + (if (= text "") "\u00A0" (sts/text-children text node))]))) + +(mf/defc render-ruby-group* + "One reading over the text `nodes` of its base (`cjk/ruby-groups`), each + node with its own style. The first node holds the reading." + {::mf/private true} + [{:keys [nodes shape]}] + (let [node (first nodes) + ruby (cjk/visible-ruby node)] + (if (some? ruby) + [:ruby.ruby-node {:style (sts/generate-ruby-container-styles node)} + (for [[index node] (d/enumerate nodes)] + [:span.text-node {:key index :style (sts/generate-text-styles shape node)} + (sts/text-children (:text node) node)]) + [:rt {:style (sts/generate-ruby-styles shape node)} ruby]] + [:* + (for [[index node] (d/enumerate nodes)] + [:span.text-node {:key index :style (sts/generate-text-styles shape node)} + (sts/text-children (:text node) node)])]))) (mf/defc render-root* [{:keys [node children shape]}] @@ -55,10 +80,15 @@ "paragraph" render-paragraph* nil)] (when component - [:> component props - (for [[index child-node] (d/enumerate children)] - [:> render-node* - (mf/spread-props props {:node child-node :index index :key index})])]))))) + (let [children (vec children)] + [:> component props + (for [[start end] (cjk/ruby-base-runs children)] + (if (= (inc start) end) + [:> render-node* + (mf/spread-props props {:node (nth children start) :index start :key start})] + [:> render-ruby-group* {:key start + :nodes (subvec children start end) + :shape (unchecked-get props "shape")}]))])))))) (defn- next-color "Given a set of colors try to get a color not yet used" @@ -164,7 +194,10 @@ y (dm/get-prop shape :y) width (dm/get-prop shape :width) height (dm/get-prop shape :height) - content (get shape :content) + content (wm/renderable-content (get shape :content)) + + ;; Vertical text anchors columns to the box edges, so it skips the oversized auto-grow box. + vertical? (wm/vertical-text-content? content) [colors _color-mapping color-mapping-inverse] (retrieve-colors shape)] @@ -175,8 +208,8 @@ :data-colors (str/join "," colors) :data-mapping (-> color-mapping-inverse clj->js js/JSON.stringify) :transform transform - :width (if (#{:auto-width} grow-type) 100000 width) - :height (if (#{:auto-height :auto-width} grow-type) 100000 height) + :width (if (and (not vertical?) (#{:auto-width} grow-type)) 100000 width) + :height (if (and (not vertical?) (#{:auto-height :auto-width} grow-type)) 100000 height) :ref ref} ;; We use a class here because react has a bug that won't use the appropriate selector for ;; `background-clip` diff --git a/frontend/src/app/main/ui/shapes/text/html_text.cljs b/frontend/src/app/main/ui/shapes/text/html_text.cljs index 7ffd80c38c..680d19c288 100644 --- a/frontend/src/app/main/ui/shapes/text/html_text.cljs +++ b/frontend/src/app/main/ui/shapes/text/html_text.cljs @@ -10,7 +10,9 @@ [app.common.data.macros :as dm] [app.common.text :as legacy.txt] [app.common.types.text :as txt] + [app.common.types.text.cjk-layout :as cjk] [app.main.ui.shapes.text.styles :as sts] + [app.util.text.writing-mode :as wm] [rumext.v2 :as mf])) (mf/defc render-text* @@ -19,9 +21,60 @@ style (if (= text "") (sts/generate-text-styles shape parent) (sts/generate-text-styles shape node)) - class (when is-code (:$id node))] - [:span.text-node {:style style :class class} - (if (= text "") "\u00A0" text)])) + class (when is-code (:$id node)) + ruby (cjk/visible-ruby node)] + (cond + (and (true? (:ruby-zhuyin node)) (cjk/zhuyin-base? text) (cjk/zhuyin-reading? ruby)) + [:span {:style #js {:display "inline-block" :position "relative" + :width "1.6em" :height "1em" :verticalAlign "middle" + :writingMode "horizontal-tb" :direction "ltr" + :fontSize (.-fontSize style) :fontFamily (.-fontFamily style) + :fontWeight (.-fontWeight style) :fontStyle (.-fontStyle style)} + :class (when is-code (dm/str class "-ruby"))} + [:span.text-node {:style style :class class} (sts/text-children text node)] + (for [[idx slot] (d/enumerate (cjk/zhuyin-slots ruby))] + [:span {:key idx :style #js {:position "absolute" :writingMode "horizontal-tb" + :left (dm/str (+ 1 (:x slot)) "em") + :top (dm/str (:y slot) "em") + :transform "translate(-50%, -50%)" :lineHeight 1}} + [:span {:style #js {:fontSize "0.3em"}} (:text slot)]])] + + (some? ruby) + [:ruby.ruby-node {:style (sts/generate-ruby-container-styles node) + :class (when is-code (dm/str class "-ruby"))} + [:span.text-node {:style style :class class} (sts/text-children text node)] + [:rt {:style (sts/generate-ruby-styles shape node) + :class (when is-code (dm/str class "-rt"))} + ruby]] + :else + [:span.text-node {:style style :class class} + (if (= text "") "\u00A0" (sts/text-children text node))]))) + +(mf/defc render-ruby-group* + "One reading over the text `nodes` of its base (`cjk/ruby-groups`), each + node with its own style. The first node holds the reading." + {::mf/private true} + [{:keys [nodes shape is-code]}] + (let [node (first nodes) + class (when is-code (:$id node)) + ruby (cjk/visible-ruby node)] + (if (some? ruby) + [:ruby.ruby-node {:style (sts/generate-ruby-container-styles node) + :class (when is-code (dm/str class "-ruby"))} + (for [[index node] (d/enumerate nodes)] + [:span.text-node {:key index + :style (sts/generate-text-styles shape node) + :class (when is-code (:$id node))} + (sts/text-children (:text node) node)]) + [:rt {:style (sts/generate-ruby-styles shape node) + :class (when is-code (dm/str class "-rt"))} + ruby]] + [:* + (for [[index node] (d/enumerate nodes)] + [:span.text-node {:key index + :style (sts/generate-text-styles shape node) + :class (when is-code (:$id node))} + (sts/text-children (:text node) node)])]))) (mf/defc render-root* [{:keys [node children shape is-code]}] @@ -50,6 +103,26 @@ (declare render-node*) +(defn- render-ruby-runs + "Text nodes of `paragraph`, with the nodes of a ruby base spanning several + of them (`cjk/ruby-base-runs`) drawn under one reading." + [props paragraph is-code] + (let [children (vec (:children paragraph))] + (for [[start end] (cjk/ruby-base-runs children)] + (if (= (inc start) end) + (mf/html + [:> render-node* + (mf/spread-props props + {:node (nth children start) + :parent paragraph + :index start + :key start})]) + (mf/html + [:> render-ruby-group* {:key start + :nodes (subvec children start end) + :shape (unchecked-get props "shape") + :is-code is-code}]))))) + (defn- render-children "Child elements of `parent`. In code mode, runs of list paragraphs are grouped into a `