From 894408ff7be6c3a2aba55a42eab50296eb498f58 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Mon, 16 Nov 2026 16:43:10 +0100 Subject: [PATCH] :sparkles: Add CJK text attributes and language profiles to the data model Texts get the attributes of CJK layout: the language of each paragraph (a BCP 47 tag), the writing mode and orientation, readings (ruby, pinyin and zhuyin), emphasis marks, warichu, tate-chu-yoko, line breaking rules and distributed alignment. `cjk-layout` holds their values and defaults and tells which language family a tag belongs to. Files keep a language profile in their options: the language new text layers take when the user has not picked one. Profiles keep their own default, and new files start with it. A new change type sets the file profile, and the `cjk-support` feature gates the whole work. Range styling joins spans left with the same style, since tate-chu-yoko and readings work per span. AI-assisted-by: claude-opus-5-5 --- backend/src/app/rpc/commands/files_create.clj | 15 +- backend/src/app/rpc/commands/profile.clj | 3 + backend/test/backend_tests/rpc_file_test.clj | 37 ++ common/src/app/common/attrs.cljc | 11 +- common/src/app/common/features.cljc | 3 + common/src/app/common/files/changes.cljc | 10 + .../src/app/common/files/changes_builder.cljc | 12 + common/src/app/common/flags.cljc | 1 + common/src/app/common/types/file.cljc | 20 +- common/src/app/common/types/shape.cljc | 8 +- common/src/app/common/types/shape/text.cljc | 52 ++- common/src/app/common/types/text.cljc | 52 ++- .../src/app/common/types/text/cjk_layout.cljc | 390 ++++++++++++++++++ common/test/common_tests/attrs_test.cljc | 9 + .../test/common_tests/files_changes_test.cljc | 17 + common/test/common_tests/types/text_test.cljc | 297 +++++++++++++ frontend/src/app/worker/index.cljs | 3 +- library/test/builder.test.js | 38 ++ 18 files changed, 965 insertions(+), 13 deletions(-) create mode 100644 common/src/app/common/types/text/cjk_layout.cljc diff --git a/backend/src/app/rpc/commands/files_create.clj b/backend/src/app/rpc/commands/files_create.clj index 3d1fac497a..1998eca213 100644 --- a/backend/src/app/rpc/commands/files_create.clj +++ b/backend/src/app/rpc/commands/files_create.clj @@ -18,6 +18,7 @@ [app.loggers.audit :as-alias audit] [app.loggers.webhooks :as-alias webhooks] [app.rpc :as-alias rpc] + [app.rpc.commands.profile :as profile] [app.rpc.commands.projects :as projects] [app.rpc.commands.teams :as teams] [app.rpc.doc :as-alias doc] @@ -38,7 +39,7 @@ [{:keys [::db/conn] :as cfg} {:keys [id name project-id is-shared revn modified-at deleted-at create-page page-id - ignore-sync-until features] + ignore-sync-until features text-language] :or {is-shared false revn 0 create-page true} :as params}] @@ -57,7 +58,10 @@ :created-at modified-at :deleted-at deleted-at} {:create-page create-page - :page-id page-id})] + :page-id page-id}) + file (cond-> file + (some? text-language) + (update :data ctf/set-default-text-language text-language))] (bfc/insert-file! cfg file) @@ -105,9 +109,14 @@ (set/difference cfeat/frontend-only-features) (set/union features)) + ;; New files start with the text language the profile picked. + language (-> (profile/get-profile conn profile-id) + (get-in [:props :default-text-language])) + params (-> params (assoc :profile-id profile-id) - (assoc :features features))] + (assoc :features features) + (assoc :text-language language))] (quotes/check! cfg {::quotes/id ::quotes/files-per-project ::quotes/team-id team-id diff --git a/backend/src/app/rpc/commands/profile.clj b/backend/src/app/rpc/commands/profile.clj index 35b4eff985..3735d22c1b 100644 --- a/backend/src/app/rpc/commands/profile.clj +++ b/backend/src/app/rpc/commands/profile.clj @@ -14,6 +14,7 @@ [app.common.time :as ct] [app.common.transit :as t] [app.common.types.plugins :as ctp] + [app.common.types.shape.text :as ctsx] [app.common.uuid :as uuid] [app.config :as cf] [app.db :as db] @@ -62,6 +63,8 @@ [:map {:title "ProfileProps" :closed true} [:plugins {:optional true} ctp/schema:plugin-registry] [:renderer {:optional true} [::sm/one-of #{:svg :wasm}]] + ;; Text language (BCP 47 tag) the files the profile creates start with. + [:default-text-language {:optional true} [:maybe ctsx/schema:text-language]] [:mcp-enabled {:optional true} ::sm/boolean] [:newsletter-updates {:optional true} ::sm/boolean] [:newsletter-news {:optional true} ::sm/boolean] diff --git a/backend/test/backend_tests/rpc_file_test.clj b/backend/test/backend_tests/rpc_file_test.clj index e45794a739..b3a4f98b25 100644 --- a/backend/test/backend_tests/rpc_file_test.clj +++ b/backend/test/backend_tests/rpc_file_test.clj @@ -147,6 +147,43 @@ (let [result (:result out)] (t/is (= 0 (count result)))))))) +(t/deftest new-files-take-the-profile-text-language + (let [prof (th/create-profile* 1 {:is-active true}) + proj-id (:default-project-id prof) + create (fn [] + (let [out (th/command! {::th/type :create-file + ::rpc/profile-id (:id prof) + :project-id proj-id + :name "foobar"})] + (t/is (nil? (:error out))) + (let [out (th/command! {::th/type :get-file + ::rpc/profile-id (:id prof) + :id (:id (:result out))})] + (t/is (nil? (:error out))) + (get-in out [:result :data :options :text-language])))) + set-language (fn [language] + (let [out (th/command! {::th/type :update-profile-props + ::rpc/profile-id (:id prof) + :props {:default-text-language language}})] + (t/is (nil? (:error out)))))] + + (t/testing "without a profile language new files have none" + (t/is (nil? (create)))) + + (t/testing "new files start with the profile language" + (set-language "zh-Hant-TW") + (t/is (= "zh-Hant-TW" (create)))) + + (t/testing "removing the profile language stops applying it" + (set-language nil) + (t/is (nil? (create)))) + + (t/testing "the profile language must be a language tag" + (let [out (th/command! {::th/type :update-profile-props + ::rpc/profile-id (:id prof) + :props {:default-text-language "not a tag!"}})] + (t/is (th/ex-of-type? (:error out) :validation)))))) + (t/deftest file-gc-with-fragments (let [profile (th/create-profile* 1) file (th/create-file* 1 {:profile-id (:id profile) diff --git a/common/src/app/common/attrs.cljc b/common/src/app/common/attrs.cljc index 95e43d268c..3ec034b375 100644 --- a/common/src/app/common/attrs.cljc +++ b/common/src/app/common/attrs.cljc @@ -8,7 +8,11 @@ (:require [app.common.geom.shapes :as gsh] [app.common.math :as mth] - [app.common.types.text :as txt])) + [app.common.types.text :as txt] + [app.common.types.text.cjk-layout :as cjk])) + +(def ^:private language-paragraph-attrs + (set cjk/language-paragraph-attrs)) (defn- get-attr [obj attr] @@ -42,6 +46,11 @@ :multiple) (get obj attr ::unset))) + ;; A text without language differs from one with a language, so an + ;; absent value must count when reporting mixed text selections. + (contains? language-paragraph-attrs attr) + (get obj attr) + :else (get obj attr ::unset)))) diff --git a/common/src/app/common/features.cljc b/common/src/app/common/features.cljc index 281ed4c9bb..cb4fe42642 100644 --- a/common/src/app/common/features.cljc +++ b/common/src/app/common/features.cljc @@ -57,6 +57,7 @@ "text-editor/v2" "text-editor-wasm/v1" "text-editor-wasm/v1-html-paste" + "cjk-support" "render-wasm/v1" "variants/v1"}) @@ -84,6 +85,7 @@ "text-editor-wasm/v1" "text-editor-wasm/v1-html-paste" "tokens/numeric-input" + "cjk-support" "render-wasm/v1"}) ;; Features that are mainly backend only or there are a proper @@ -134,6 +136,7 @@ :feature-text-editor-v2-html-paste "text-editor/v2-html-paste" :feature-text-editor-wasm "text-editor-wasm/v1" :feature-text-editor-wasm-html-paste "text-editor-wasm/v1-html-paste" + :cjk-support "cjk-support" :feature-render-wasm "render-wasm/v1" :feature-variants "variants/v1" :feature-token-input "tokens/numeric-input" diff --git a/common/src/app/common/files/changes.cljc b/common/src/app/common/files/changes.cljc index b2b0e6de60..d5562e69a3 100644 --- a/common/src/app/common/files/changes.cljc +++ b/common/src/app/common/files/changes.cljc @@ -28,6 +28,7 @@ [app.common.types.path :as path] [app.common.types.shape :as cts] [app.common.types.shape-tree :as ctst] + [app.common.types.shape.text :as ctsx] [app.common.types.token :as cto] [app.common.types.tokens-lib :as ctob] [app.common.types.tokens-status :as ctos] @@ -435,6 +436,11 @@ [:before-path [:maybe [:vector :string]]] [:before-group [:maybe :boolean]]]] + [:set-default-text-language + [:map {:title "SetDefaultTextLanguage"} + [:type [:= :set-default-text-language]] + [:language [:maybe ctsx/schema:text-language]]]] + [:set-base-font-size [:map {:title "ModBaseFontSize"} [:type [:= :set-base-font-size]] @@ -1153,6 +1159,10 @@ ;; === Design Tokens configuration +(defmethod process-change :set-default-text-language + [data {:keys [language]}] + (ctf/set-default-text-language data language)) + (defmethod process-change :set-base-font-size [data {:keys [base-font-size]}] (ctf/set-base-font-size data base-font-size)) diff --git a/common/src/app/common/files/changes_builder.cljc b/common/src/app/common/files/changes_builder.cljc index 94f50fccef..b09d903217 100644 --- a/common/src/app/common/files/changes_builder.cljc +++ b/common/src/app/common/files/changes_builder.cljc @@ -1153,6 +1153,18 @@ :base-font-size previous-font-size}) (apply-changes-local)))) +(defn set-default-text-language + [changes language] + (assert-file-data! changes) + (let [file-data (::file-data (meta changes)) + previous (ctf/get-default-text-language file-data)] + (-> changes + (update :redo-changes conj {:type :set-default-text-language + :language language}) + (update :undo-changes conj {:type :set-default-text-language + :language previous}) + (apply-changes-local)))) + (defn set-tokens-source [changes library-id] (assert-library! changes) diff --git a/common/src/app/common/flags.cljc b/common/src/app/common/flags.cljc index 6012c702bf..d80cf7a9ef 100644 --- a/common/src/app/common/flags.cljc +++ b/common/src/app/common/flags.cljc @@ -160,6 +160,7 @@ ;; Show WASM renderer info label (hidden by default). :render-wasm-info :render-switch + :cjk-support :hide-release-modal :subscriptions :inspect-styles diff --git a/common/src/app/common/types/file.cljc b/common/src/app/common/types/file.cljc index 78fa9cf3b4..bdfba1d57b 100644 --- a/common/src/app/common/types/file.cljc +++ b/common/src/app/common/types/file.cljc @@ -28,6 +28,7 @@ [app.common.types.plugins :refer [schema:plugin-data]] [app.common.types.shape :as cts] [app.common.types.shape-tree :as ctst] + [app.common.types.shape.text :as ctsx] [app.common.types.text :as txt] [app.common.types.tokens-lib :as ctob] [app.common.types.tokens-status :as ctos] @@ -77,7 +78,9 @@ (def schema:options [:map {:title "FileOptions"} [:components-v2 {:optional true} ::sm/boolean] - [:base-font-size {:optional true} :string]]) + [:base-font-size {:optional true} :string] + ;; Language new text layers take (see `get-default-text-language`). + [:text-language {:optional true} ctsx/schema:text-language]]) (def schema:data [:map {:title "FileData"} @@ -1261,6 +1264,21 @@ [file-data base-font-size] (assoc-in file-data [:options :base-font-size] base-font-size)) +;; Default text language + +(defn get-default-text-language + "Language (a BCP 47 tag) the new text layers of the file take, or nil. + Existing texts store their own language and do not follow it." + [file-data] + (get-in file-data [:options :text-language])) + +(defn set-default-text-language + "Set the file text language; nil removes it." + [file-data language] + (if (some? language) + (assoc-in file-data [:options :text-language] language) + (d/update-in-when file-data [:options] dissoc :text-language))) + ;; Ref Chains (defn get-ref-chain-until-target-ref diff --git a/common/src/app/common/types/shape.cljc b/common/src/app/common/types/shape.cljc index b7bf5f2961..7df6c4db71 100644 --- a/common/src/app/common/types/shape.cljc +++ b/common/src/app/common/types/shape.cljc @@ -104,7 +104,7 @@ #{:top :bottom :topbottom :center :scale}) (def text-align-types - #{"left" "right" "center" "justify"}) + #{"left" "right" "center" "justify" "distribute"}) (def bool-types #{:union @@ -876,7 +876,7 @@ :r4}) (def ^:private text-extract-props - (into #{} cat [txt/root-attrs txt/paragraph-attrs txt/text-node-attrs])) + (into #{} cat [txt/root-attrs txt/paragraph-attrs txt/text-node-style-attrs])) (def ^:private layout-extract-props (set ctsl/layout-attrs)) @@ -927,7 +927,7 @@ (assoc-props node txt/paragraph-attrs) (txt/is-text-node? node) - (assoc-props node txt/text-node-attrs))) + (assoc-props node txt/text-node-style-attrs))) props))) (extract-layout-attrs [props shape] @@ -970,7 +970,7 @@ (d/patch-object (select-keys props txt/paragraph-attrs)) (txt/is-text-node? node) - (d/patch-object (select-keys props txt/text-node-attrs)))))))))) + (d/patch-object (select-keys props txt/text-node-style-attrs)))))))))) (patch-layout-props [shape props] (let [shape (d/patch-object shape (select-keys props ctsl/layout-attrs))] diff --git a/common/src/app/common/types/shape/text.cljc b/common/src/app/common/types/shape/text.cljc index 122aae80a8..d7e7206988 100644 --- a/common/src/app/common/types/shape/text.cljc +++ b/common/src/app/common/types/shape/text.cljc @@ -7,7 +7,8 @@ (ns app.common.types.shape.text (:require [app.common.schema :as sm] - [app.common.types.fills :refer [schema:fills]])) + [app.common.types.fills :refer [schema:fills]] + [app.common.types.text.cjk-layout :as cjk])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; SCHEMA @@ -15,10 +16,22 @@ (def node-types #{"root" "paragraph-set" "paragraph"}) +;; Values of a CJK layout enum attr; paragraphs set the orientation +;; of the whole shape and spans carry a copy. +(defn- schema:cjk-enum + [attr] + (into [:enum] (get cjk/enum-values attr))) + +;; BCP 47 shaped language tag of a paragraph, as the HTML `lang` attribute. +(def schema:text-language + [:re {:gen/elements ["ja" "zh-Hans" "zh-Hant-TW" "ko" "en-GB"]} + #"^(?:[A-Za-z]{2,8}(?:-[A-Za-z0-9]{1,8})*|[xXiI](?:-[A-Za-z0-9]{1,8})+)$"]) + (def schema:content [:map [:type [:= "root"]] [:key {:optional true} :string] + [:line-adjustment {:optional true} (schema:cjk-enum :line-adjustment)] [:children [:vector {:min 1 :gen/max 2 :gen/min 1} [:map @@ -36,6 +49,12 @@ [:font-style {:optional true} ::sm/text] [:font-weight {:optional true} ::sm/text] [:direction {:optional true} ::sm/text] + ;; Paragraph language selects typography; it never follows the UI locale. + [:text-language {:optional true} schema:text-language] + ;; Word mode keeps punctuation rules; retain the old POC value on reload. + [:korean-wrap {:optional true} (schema:cjk-enum :korean-wrap)] + [:writing-mode {:optional true} (schema:cjk-enum :writing-mode)] + [:text-orientation {:optional true} (schema:cjk-enum :text-orientation)] [:text-decoration {:optional true} ::sm/text] [:text-transform {:optional true} ::sm/text] [:typography-ref-id {:optional true} [:maybe ::sm/uuid]] @@ -52,6 +71,22 @@ [:font-style {:optional true} ::sm/text] [:font-weight {:optional true} ::sm/text] [:direction {:optional true} ::sm/text] + [:text-combine-upright {:optional true} (schema:cjk-enum :text-combine-upright)] + [:text-emphasis {:optional true} (schema:cjk-enum :text-emphasis)] + [:text-emphasis-position {:optional true} (schema:cjk-enum :text-emphasis-position)] + [:text-underline-position {:optional true} (schema:cjk-enum :text-underline-position)] + [:ruby {:optional true} :string] + [:ruby-group {:optional true} :string] + [:ruby-hidden {:optional true} :boolean] + [:ruby-zhuyin {:optional true} :boolean] + [:ruby-size {:optional true} (schema:cjk-enum :ruby-size)] + [:ruby-align {:optional true} (schema:cjk-enum :ruby-align)] + [:ruby-overhang {:optional true} (schema:cjk-enum :ruby-overhang)] + [:ruby-side {:optional true} (schema:cjk-enum :ruby-side)] + [:warichu {:optional true} (schema:cjk-enum :warichu)] + [:font-features {:optional true} (schema:cjk-enum :font-features)] + [:annotation-clearance {:optional true} (schema:cjk-enum :annotation-clearance)] + [:text-orientation {:optional true} (schema:cjk-enum :text-orientation)] [:text-decoration {:optional true} ::sm/text] [:text-transform {:optional true} ::sm/text] [:typography-ref-id {:optional true} [:maybe ::sm/uuid]] @@ -73,6 +108,21 @@ [:font-style {:optional true} ::sm/text] [:font-weight {:optional true} ::sm/text] [:rtl {:optional true} :boolean] + [:writing-mode {:optional true} ::sm/text] + [:text-orientation {:optional true} ::sm/text] + [:text-combine-upright {:optional true} ::sm/text] + [:emphasis-mark {:optional true} :boolean] + [:zhuyin-glyph {:optional true} :boolean] + [:ruby {:optional true} :string] + ;; A horizontal reading over several strips of its base spans them all. + [:ruby-x {:optional true} ::sm/safe-number] + [:ruby-width {:optional true} ::sm/safe-number] + [:ruby-size {:optional true} ::sm/text] + [:ruby-align {:optional true} ::sm/text] + [:ruby-overhang {:optional true} ::sm/text] + [:ruby-side {:optional true} ::sm/text] + [:warichu {:optional true} ::sm/text] + [:font-features {:optional true} ::sm/text] [:text {:optional true} :string] [:text-decoration {:optional true} ::sm/text] [:text-transform {:optional true} ::sm/text]]]) diff --git a/common/src/app/common/types/text.cljc b/common/src/app/common/types/text.cljc index fbeb80695c..4ebf7aef3c 100644 --- a/common/src/app/common/types/text.cljc +++ b/common/src/app/common/types/text.cljc @@ -12,6 +12,7 @@ [app.common.math :as mth] [app.common.types.color :as clr] [app.common.types.fills :as types.fills] + [app.common.types.text.cjk-layout :as cjk] [clojure.set :as set] [clojure.walk :as walk] [cuerdas.core :as str])) @@ -53,6 +54,10 @@ (def text-valign-attrs [:vertical-align]) +;; How the shape fits lines slightly too long (see `cjk/enum-values`). +(def text-line-adjustment-attrs + [:line-adjustment]) + (def text-decoration-attrs [:text-decoration]) @@ -100,23 +105,31 @@ [:grow-type]) (def root-attrs - text-valign-attrs) + (d/concat-vec text-valign-attrs text-line-adjustment-attrs)) (def paragraph-attrs (d/concat-vec text-align-attrs text-direction-attrs - text-list-attrs)) + text-list-attrs + cjk/language-paragraph-attrs + cjk/vertical-writing-attrs)) (def text-node-attrs (d/concat-vec text-typography-attrs text-font-attrs text-spacing-attrs + cjk/span-attrs text-decoration-attrs text-transform-attrs text-fills)) +(def text-node-style-attrs + "Text node attrs that style characters, without the attrs the characters + own (ruby reading, warichu)." + (vec (remove (set cjk/text-content-attrs) text-node-attrs))) + (def text-span-attrs "Inline text span attrs. Line-height is paragraph-level in the DOM editor; it may still be stored redundantly on span nodes." @@ -247,6 +260,41 @@ item)) root))) +(defn- span-style + "The text node attrs of `span` that differ from their default value, with + legacy numeric values as strings." + [span] + (reduce (fn [style attr] + (let [value (get span attr) + value (if (number? value) (str value) value)] + (if (or (nil? value) + (and (or (string? value) (coll? value)) (empty? value)) + (= value (get default-text-attrs attr)) + (= value (get cjk/span-attr-defaults attr))) + style + (assoc style attr value)))) + {} + text-node-attrs)) + +(defn- mergeable-spans? + [a b] + (and (not (cjk/annotated-span? a)) + (not (cjk/annotated-span? b)) + (= (span-style a) (span-style b)))) + +(defn merge-equal-spans + "Joins neighbouring `spans` of a paragraph that have the same style, so + span boundaries only exist where the style changes. Spans with a ruby + reading or a warichu note are never joined: each one is its own group." + [spans] + (reduce (fn [result span] + (let [prev (peek result)] + (if (and (some? prev) (mergeable-spans? prev span)) + (conj (pop result) (update prev :text str (:text span))) + (conj result span)))) + [] + spans)) + (defn update-text-content [shape pred-fn update-fn attrs] (let [update-attrs-fn #(update-fn % attrs) diff --git a/common/src/app/common/types/text/cjk_layout.cljc b/common/src/app/common/types/text/cjk_layout.cljc new file mode 100644 index 0000000000..dee58de95e --- /dev/null +++ b/common/src/app/common/types/text/cjk_layout.cljc @@ -0,0 +1,390 @@ +;; 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 app.common.types.text.cjk-layout + (:require + [app.common.data :as d] + [app.common.data.macros :as dm] + [cuerdas.core :as str])) + +;; Values of the enum attrs, in the order of the renderer's discriminants +;; (`Raw*` enums in render-wasm). The first value is the default. +(def enum-values + {;; Vertical writing (tategaki). + :writing-mode ["horizontal-tb" "vertical-rl"] + :text-orientation ["mixed" "upright"] + :text-combine-upright ["none" "all" "digits" "digits2" "digits3"] + ;; Emphasis mark (圏点 / bouten) applied per span. + :text-emphasis ["none" "filled-dot" "open-dot" "filled-circle" + "open-circle" "filled-sesame" "open-sesame"] + :text-emphasis-position ["auto" "over-right" "over-left" "under-right" "under-left"] + :text-underline-position ["auto" "under" "left" "right" "under-left" "under-right"] + ;; Warichu (割注): two half-size lines in one line position. + :warichu ["none" "warichu"] + :font-features ["none" "palt" "vpal"] + ;; "auto" adds the ruby (at its size) and emphasis marks to the line + ;; height; "none" keeps it, so annotations sit in the line gap. + :annotation-clearance ["none" "auto"] + :ruby-size ["half" "third" "quarter"] + :ruby-align ["space-around" "center" "start" "space-between"] + :ruby-overhang ["auto" "none"] + :ruby-side ["over" "under"] + ;; Root attr: how lines slightly too long are fitted (Adobe's Kinsoku + ;; Type). It decides squeezing; alignment decides the space left over. + :line-adjustment ["push-in-first" "push-out-first" "push-out-only"] + ;; Korean line breaking: between syllables, or between words only. + ;; "spaces-only" is the former name of "keep-words", kept on reload. + :korean-wrap ["syllable" "spaces-only" "keep-words"]}) + +(def ^:private enum-value-sets + (update-vals enum-values set)) + +(defn enum-default + "Default value of the enum `attr`." + [attr] + (first (get enum-values attr))) + +(defn valid-enum-value? + "True when `value` is one of the values of the enum `attr`." + [attr value] + (contains? (get enum-value-sets attr) value)) + +;; Vertical writing paragraph attrs. They apply to the whole shape: every +;; paragraph carries the first one's value. +(def vertical-writing-attrs + [:writing-mode :text-orientation]) + +;; Paragraph attrs the text options write to every paragraph, whatever the +;; editor selection: a text has one writing mode and one language. +(def whole-text-paragraph-attrs + (conj vertical-writing-attrs :text-language)) + +;; Paragraph attrs of the language. A text has one language, but Korean +;; wrapping can change per paragraph. +(def language-paragraph-attrs + [:text-language :korean-wrap]) + +;; Ruby attrs that change how a reading looks, not the reading itself. +(def ruby-presentation-attrs + [:ruby-hidden :ruby-zhuyin :ruby-size :ruby-align :ruby-overhang :ruby-side]) + +;; Span attrs; `:ruby` holds the reading (furigana) of the span and +;; `:ruby-group` joins it to the spans of the same reading (`ruby-groups`). +(def span-attrs + (into [:text-combine-upright :text-emphasis :text-emphasis-position :text-underline-position + :ruby :ruby-group] + (concat ruby-presentation-attrs + [:warichu :font-features :annotation-clearance]))) + +;; Span attrs owned by the span's characters, not its style: a reading or a +;; warichu note annotates its own text and is never copied to other text. +(def text-content-attrs + [:ruby :ruby-group :warichu]) + +;; Values of the span attrs above when a span does not store them. +(def span-attr-defaults + (into {:ruby "" :ruby-group "" :ruby-hidden false :ruby-zhuyin false} + (comp (filter #(contains? enum-values %)) + (map (juxt identity enum-default))) + span-attrs)) + +;; Values of the paragraph attrs when a paragraph does not store them. +(def paragraph-attr-defaults + (into {} (map (juxt identity enum-default)) vertical-writing-attrs)) + +;; Glyph of each emphasis style, per CSS `text-emphasis-style`. +(def ^:private emphasis-mark-chars + {"filled-dot" "•" + "open-dot" "◦" + "filled-circle" "●" + "open-circle" "○" + "filled-sesame" "﹅" + "open-sesame" "﹆"}) + +(defn emphasis-mark-char + "Mark glyph of a text-emphasis value, or nil for none." + [text-emphasis] + (get emphasis-mark-chars text-emphasis)) + +;; Emphasis mark font size relative to the base font size. +(def emphasis-font-scale 0.5) + +;; Warichu sub-line font size relative to the base font size. +(def warichu-font-scale 0.5) + +(defn ruby-font-scale + [ruby-size] + (case ruby-size + "third" (/ 1 3) + "quarter" 0.25 + 0.5)) + +(defn visible-ruby + "Ruby annotation text of a text node, or nil when absent, hidden or on a + warichu note, which shows no ruby." + [node] + (let [ruby (:ruby node)] + (when (and (string? ruby) + (not (str/blank? ruby)) + (not (true? (:ruby-hidden node))) + (not= "warichu" (:warichu node))) + ruby))) + +(defn western-reading? + "True when a ruby reading holds no CJK, kana or full-width character: it is + set as words, sideways in vertical text, and never spread letter by + letter." + [ruby] + (and (string? ruby) + (not (str/blank? ruby)) + (not (re-find #"[\u2E80-\u9FFF\uF900-\uFAFF\uFF00-\uFFEF]" ruby)))) + +(def ^:private zhuyin-base-pattern + #?(:clj (re-pattern "[\\u3400-\\u9FFF\\uF900-\\uFAFF\\x{20000}-\\x{323AF}][\\uFE00-\\uFE0F\\x{E0100}-\\x{E01EF}]?") + :cljs (js/RegExp. "[\\u3400-\\u9FFF\\uF900-\\uFAFF\\u{20000}-\\u{323AF}][\\uFE00-\\uFE0F\\u{E0100}-\\u{E01EF}]?" "u"))) + +(defn zhuyin-base? + "One Han character, optionally followed by a variation selector." + [text] + (boolean (and (string? text) (re-matches zhuyin-base-pattern text)))) + +(defn zhuyin-reading? + "One Mandarin syllable: one to three symbols and an optional tone." + [reading] + (boolean (and (string? reading) + (re-matches #"(?:[\u3105-\u3129]{1,3}[ˉˊˇˋ˙]?|˙[\u3105-\u3129]{1,3})" (str/trim reading))))) + +(defn zhuyin-slots + "Upright symbol ink centres in base em for HTML handoff. Input is one valid + Mandarin syllable. The canvas positions font ink around the same centres." + [reading] + (let [chars (vec (str/trim reading)) + symbols (vec (keep-indexed (fn [idx ch] (when (re-matches #"[\u3105-\u3129]" (str ch)) idx)) chars)) + neutral? (some #(= "˙" (str %)) chars) + lead (if neutral? (/ 1 15) 0) + top (/ (- 1 (* (count symbols) 0.3) lead) 2)] + (mapv (fn [idx ch] + (if-let [symbol (first (keep-indexed #(when (= idx %2) %1) symbols))] + {:text (str ch) :x 0.15 :y (+ top lead (* (+ symbol 0.5) 0.3))} + {:text (str ch) :x (if neutral? 0.15 0.45) + :y (+ top (if neutral? (/ lead 2) (* (dec (count symbols)) 0.3)))})) + (range (count chars)) chars))) + +(defn ruby-span? + "True when a text node carries a ruby reading, hidden or not." + [node] + (not (str/blank? (:ruby node)))) + +(defn ruby-joins-previous? + "True when text node `node` continues the reading of `previous`, the node + before it: both carry the same reading in the same non-blank + `:ruby-group`, so their text is one base whose first node sets the + presentation. Without a group, neighbours with the same reading stay + apart, as in 天天 (tiān tiān). Zhuyin annotates one character, so its + nodes stand alone." + [previous node] + (let [group (:ruby-group node)] + (boolean + (and (some? previous) + (string? group) + (not (str/blank? group)) + (= group (:ruby-group previous)) + (ruby-span? node) + (ruby-span? previous) + (not= "warichu" (:warichu node)) + (not= "warichu" (:warichu previous)) + (not (true? (:ruby-zhuyin node))) + (not (true? (:ruby-zhuyin previous))) + (= (str/trim (:ruby node)) (str/trim (:ruby previous))))))) + +(defn ruby-groups + "Index ranges `[start end)` of the text `nodes` of a paragraph whose text is + one ruby base: a node with a reading and the nodes after it that join it + (`ruby-joins-previous?`). A reading over characters of several styles, + such as some with emphasis marks, is one base." + [nodes] + (let [nodes (vec nodes)] + (reduce (fn [groups index] + (let [node (nth nodes index)] + (cond + (not (ruby-span? node)) + groups + + (and (seq groups) + (= index (second (peek groups))) + (ruby-joins-previous? (nth nodes (dec index)) node)) + (conj (pop groups) [(first (peek groups)) (inc index)]) + + :else + (conj groups [index (inc index)])))) + [] + (range (count nodes))))) + +(defn ruby-base-runs + "Index ranges `[start end)` that cover the `nodes` of a paragraph in order: + each reading over several nodes (`ruby-groups`) as one range, every other + node alone. Renderers draw a range as one reading over its nodes." + [nodes] + (let [nodes (vec nodes) + starts (into {} (comp (filter (fn [[start end]] (> (- end start) 1))) + (map (juxt first identity))) + (ruby-groups nodes))] + (loop [index 0 + runs []] + (if (>= index (count nodes)) + runs + (let [[_ end :as run] (get starts index [index (inc index)])] + (recur end (conj runs run))))))) + +(defn annotated-span? + "True when a text node carries a ruby reading or is a warichu note." + [node] + (or (ruby-span? node) + (= "warichu" (:warichu node)))) + +(defn- digit? + [c] + (let [code #?(:clj (int c) :cljs (.charCodeAt c 0))] + (or (<= 48 code 57) (<= 0xFF10 code 0xFF19)))) + +;; Longest digit run each `digits` tate-chu-yoko value combines. +(def ^:private digit-combine-max-lengths + {"digits" 4 "digits2" 2 "digits3" 3}) + +(defn digit-combine? + "True for the tate-chu-yoko values that combine digit runs only." + [value] + (contains? digit-combine-max-lengths value)) + +(defn digit-combine-segments + "Text split for a `digits` tate-chu-yoko value: `[text combine?]` pairs, + where runs of up to the value's maximum ASCII or full-width digits + combine, a lone digit included as in CSS `digits`, and longer runs stay + ordinary text. Nil for other values." + [text value] + (when-let [max-len (get digit-combine-max-lengths value)] + (->> (partition-by digit? (seq text)) + (mapv (fn [chars] + (let [run (apply str chars)] + [run (and (digit? (first chars)) (<= (count chars) max-len))])))))) + +(defn warichu-text? + "True when a text node renders as warichu, which needs at least two + characters for its two sub-lines." + [node] + (let [text (:text node)] + (and (= "warichu" (:warichu node)) + (string? text) + (>= (count text) 2)))) + +(defn whole-shape-attr + "Value of a whole-shape paragraph `attr` (see `whole-text-paragraph-attrs`) + in a text content: stored per paragraph, the first paragraph's value + decides. Nil when unset." + [content attr] + (dm/get-in content [:children 0 :children 0 attr])) + +(defn stored-writing-mode + "Writing mode stored in a text content, whatever its language, or nil + when unset. `active-writing-mode` is the one the renderer uses." + [content] + (whole-shape-attr content :writing-mode)) + +(defn stored-vertical? + "True when the text content stores vertical writing (vertical-rl), + whatever its language." + [content] + (= "vertical-rl" (stored-writing-mode content))) + +;; --- Active language + +(defn language-family + "CJK language of a BCP 47 `tag` (`:japanese`, `:chinese` or `:korean`), + or nil for any other value." + [tag] + (when (string? tag) + (case (-> tag (str/split "-") first str/lower) + "ja" :japanese + "zh" :chinese + "ko" :korean + nil))) + +(def ^:private ruby-attrs + (into #{:ruby :ruby-group} ruby-presentation-attrs)) + +;; CJK attrs each language renders; the others stay in the content and come +;; back when the text takes that language again. +(def ^:private language-attrs + {:japanese {:root #{:line-adjustment} + :paragraph (set vertical-writing-attrs) + :span (into #{:text-combine-upright :warichu :text-emphasis + :annotation-clearance :font-features} + (disj ruby-attrs :ruby-zhuyin))} + :chinese {:root #{} + :paragraph (set vertical-writing-attrs) + :span (into #{:text-emphasis :text-emphasis-position + :text-underline-position :annotation-clearance} + ruby-attrs)} + :korean {:root #{} + :paragraph (conj (set vertical-writing-attrs) :korean-wrap) + :span #{}}}) + +;; Attrs some language renders, per level: the ones without language drop. +(def ^:private language-dependent-attrs + (reduce-kv (fn [result _ levels] (merge-with into result levels)) + {:root #{} :paragraph #{} :span #{}} + language-attrs)) + +(defn content-language + "Language of a text content: the first paragraph's `:text-language`, as a + text has one language. Nil when unset." + [content] + (whole-shape-attr content :text-language)) + +(defn- keep-language-attrs + "`node` without the CJK attrs that its language `family` does not render + at `level` (`:root`, `:paragraph` or `:span`)." + [node family level] + (let [kept (get-in language-attrs [family level] #{})] + (reduce (fn [node attr] + (if (contains? kept attr) node (dissoc node attr))) + node + (get language-dependent-attrs level)))) + +(defn- active-paragraph + [paragraph] + (let [language (:text-language paragraph) + family (language-family language)] + (cond-> (-> paragraph + (d/update-when :children + (partial mapv #(keep-language-attrs % family :span))) + (keep-language-attrs family :paragraph)) + (some? language) + (assoc :text-language language) + + (and (nil? family) (= "distribute" (:text-align paragraph))) + (assoc :text-align "left")))) + +(defn active-content + "Text `content` as the renderer lays it out: the CJK attrs its language + does not use are left out, so without a CJK language a text is horizontal + and not distributed. The stored content keeps every attr." + [content] + (let [family (language-family (content-language content))] + (-> content + (d/update-when :children + (partial mapv + (fn [paragraph-set] + (d/update-when paragraph-set :children + (partial mapv active-paragraph))))) + (keep-language-attrs family :root)))) + +(defn active-writing-mode + "Writing mode the renderer lays `content` out with (see `active-content`): + nil when unset or when the text has no CJK language." + [content] + (when (some? (language-family (content-language content))) + (stored-writing-mode content))) diff --git a/common/test/common_tests/attrs_test.cljc b/common/test/common_tests/attrs_test.cljc index 8d06d1126b..a1da876edc 100644 --- a/common/test/common_tests/attrs_test.cljc +++ b/common/test/common_tests/attrs_test.cljc @@ -171,3 +171,12 @@ (t/testing "a bullet paragraph next to one without list style is mixed" (let [shape (text-shape-with-paragraphs {:list-style-type "disc"} {})] (t/is (= :multiple (:list-style-type (attrs/get-text-attrs-multi shape {} [:list-style-type]))))))) + +(t/deftest unset-language-profile-counts-as-a-distinct-value + (t/is (= {:text-language :multiple :korean-wrap :multiple} + (attrs/get-attrs-multi [{} {:text-language "ko" :korean-wrap "spaces-only"}] + [:text-language :korean-wrap]))) + (t/is (= {:text-language :multiple} + (attrs/get-attrs-multi [{:text-language "ko"} {}] [:text-language]))) + (t/is (= {:text-language nil :korean-wrap nil} + (attrs/get-attrs-multi [{} {}] [:text-language :korean-wrap])))) diff --git a/common/test/common_tests/files_changes_test.cljc b/common/test/common_tests/files_changes_test.cljc index 3cf8d4f93b..f690fab490 100644 --- a/common/test/common_tests/files_changes_test.cljc +++ b/common/test/common_tests/files_changes_test.cljc @@ -252,6 +252,23 @@ "The parent must use the updated bounds of both children") (assert-idempotent data changes)))))) +(t/deftest default-text-language-round-trips-through-undo + (let [data (assoc (make-file-data (uuid/custom 2 2) (uuid/custom 1 1)) + :options {:base-font-size "16"})] + (doseq [[before after] [[nil "ja"] ["ja" "zh-Hant-TW"] ["ko" nil]]] + (let [data (ctf/set-default-text-language data before) + changes (-> (pcb/empty-changes) + (pcb/with-file-data data) + (pcb/set-default-text-language after)) + edited (ch/process-changes data (:redo-changes changes)) + restored (ch/process-changes edited (:undo-changes changes))] + (t/is (every? ch/valid-change? (:redo-changes changes))) + (t/is (= after (ctf/get-default-text-language edited))) + (t/is (= data restored)) + (t/is (= "16" (get-in edited [:options :base-font-size]))) + (t/is (sm/validate ctf/schema:options (:options edited)))))) + (t/is (not (ch/valid-change? {:type :set-default-text-language :language "not a tag!"})))) + (t/deftest add-obj (let [file-id (uuid/custom 2 2) page-id (uuid/custom 1 1) diff --git a/common/test/common_tests/types/text_test.cljc b/common/test/common_tests/types/text_test.cljc index e0027b7b16..e63bd3aedb 100644 --- a/common/test/common_tests/types/text_test.cljc +++ b/common/test/common_tests/types/text_test.cljc @@ -8,7 +8,9 @@ (:require [app.common.types.shape :as cts] + [app.common.types.shape.text :as ctst] [app.common.types.text :as cttx] + [app.common.types.text.cjk-layout :as cjk] [clojure.test :as t :include-macros true])) (def content-base @@ -16,6 +18,13 @@ (get :content) (cttx/change-text "hello world"))) +(t/deftest distributed-alignment-is-a-persisted-paragraph-style + (t/is (contains? cts/text-align-types "distribute")) + (let [content (assoc-in content-base [:children 0 :children 0 :text-align] "distribute")] + (t/is (ctst/valid-content? content)) + (t/is (= "distribute" + (get-in (cttx/change-text content "한글") [:children 0 :children 0 :text-align]))))) + ;; Normal happy-path changes (def content-changed-text @@ -373,3 +382,291 @@ (t/is (true? (cttx/list-paragraph? {:type "paragraph" :list-style-type "disc"}))) (t/is (false? (cttx/list-paragraph? {:type "paragraph" :list-style-type "none"}))) (t/is (false? (cttx/list-paragraph? {:type "paragraph"})))) + +(defn- with-span-attrs + [attrs] + (update-in content-base [:children 0 :children 0 :children 0] merge attrs)) + +(defn- with-paragraph-attrs + [attrs] + (update-in content-base [:children 0 :children 0] merge attrs)) + +(t/deftest paragraph-language-and-experimental-korean-wrap + (doseq [language ["ja" "zh-Hans" "zh-Hant-TW" "ko" "en-GB"]] + (t/is (ctst/valid-content? + (with-paragraph-attrs {:text-language language + :korean-wrap "spaces-only"})))) + (doseq [attrs [{:text-language 42} + {:text-language "zh_Hans"} + {:text-language ""} + {:korean-wrap "keep-all"}]] + (t/is (not (ctst/valid-content? (with-paragraph-attrs attrs))) (pr-str attrs))) + (t/is (contains? (set cttx/paragraph-attrs) :text-language)) + (t/is (not (contains? (set cjk/vertical-writing-attrs) :text-language)))) + +(t/deftest japanese-span-attrs-accept-supported-values + (t/is (ctst/valid-content? + (with-span-attrs {:text-combine-upright "digits2" + :text-emphasis "open-sesame" + :ruby "かんじ" + :ruby-hidden true + :ruby-zhuyin true + :ruby-size "quarter" + :ruby-align "space-between" + :ruby-overhang "none" + :ruby-side "under" + :warichu "warichu" + :font-features "vpal" + :annotation-clearance "auto" + :text-orientation "upright"}))) + (t/is (ctst/valid-content? + (with-paragraph-attrs {:writing-mode "vertical-rl" :text-orientation "mixed"})))) + +(t/deftest japanese-attrs-reject-unknown-values + (doseq [attrs [{:text-combine-upright "digits5"} + {:text-emphasis "dot"} + {:ruby-hidden "yes"} + {:ruby-zhuyin "yes"} + {:ruby-size "full"} + {:ruby-align "end"} + {:ruby-overhang "always"} + {:ruby-side "left"} + {:warichu "yes"} + {:font-features "kern"} + {:annotation-clearance "max"} + {:text-orientation "sideways"}]] + (t/is (not (ctst/valid-content? (with-span-attrs attrs))) (pr-str attrs))) + (t/is (not (ctst/valid-content? (with-paragraph-attrs {:writing-mode "vertical-lr"}))))) + +(t/deftest digit-combine-segments-combine-short-digit-runs + (t/is (= [["平成" false] ["31" true] ["年" false]] + (cjk/digit-combine-segments "平成31年" "digits2"))) + (t/is (= [["第" false] ["123" true]] + (cjk/digit-combine-segments "第123" "digits3"))) + (t/is (= [["12345" false]] (cjk/digit-combine-segments "12345" "digits"))) + (t/is (= [["1" true]] (cjk/digit-combine-segments "1" "digits"))) + (t/is (= [["2" true] ["月" false] ["5" true] ["日" false]] + (cjk/digit-combine-segments "2月5日" "digits2"))) + (t/is (nil? (cjk/digit-combine-segments "31" "all")))) + +(t/deftest western-reading-test + (t/is (true? (cjk/western-reading? "editor"))) + (t/is (true? (cjk/western-reading? "gross domestic product"))) + (t/is (false? (cjk/western-reading? "エディター"))) + (t/is (false? (cjk/western-reading? "GDP"))) + (t/is (false? (cjk/western-reading? "かnji"))) + (t/is (false? (cjk/western-reading? ""))) + (t/is (false? (cjk/western-reading? nil)))) + +(t/deftest schema-accepts-every-japanese-enum-value + (doseq [[attr values] (dissoc cjk/enum-values :writing-mode :line-adjustment) + value values] + (t/is (ctst/valid-content? (with-span-attrs {attr value})) (pr-str attr value))) + (doseq [value (:writing-mode cjk/enum-values)] + (t/is (ctst/valid-content? (with-paragraph-attrs {:writing-mode value})) value)) + (doseq [value (:line-adjustment cjk/enum-values)] + (t/is (ctst/valid-content? (assoc content-base :line-adjustment value)) value)) + (t/is (not (ctst/valid-content? (assoc content-base :line-adjustment "squeeze"))))) + +(t/deftest japanese-enum-defaults-are-the-first-values + (t/is (= "horizontal-tb" (cjk/enum-default :writing-mode))) + (t/is (= "space-around" (cjk/enum-default :ruby-align))) + (t/is (= "auto" (cjk/enum-default :ruby-overhang))) + (t/is (= "push-in-first" (cjk/enum-default :line-adjustment))) + (t/is (= {:writing-mode "horizontal-tb" :text-orientation "mixed"} + cjk/paragraph-attr-defaults))) + +(t/deftest span-attr-defaults-cover-every-span-attr + (t/is (= (set cjk/span-attrs) (set (keys cjk/span-attr-defaults)))) + (t/is (= "" (:ruby cjk/span-attr-defaults))) + (t/is (false? (:ruby-hidden cjk/span-attr-defaults))) + (t/is (= "half" (:ruby-size cjk/span-attr-defaults)))) + +(t/deftest valid-enum-value-checks-the-attr-values + (t/is (cjk/valid-enum-value? :ruby-side "under")) + (t/is (not (cjk/valid-enum-value? :ruby-side "left"))) + (t/is (not (cjk/valid-enum-value? :ruby-side nil)))) + +(t/deftest digit-combine-covers-the-digits-values-only + (t/is (every? cjk/digit-combine? ["digits" "digits2" "digits3"])) + (t/is (not-any? cjk/digit-combine? ["none" "all" nil]))) + +(t/deftest warichu-notes-show-no-ruby + (t/is (= "かん" (cjk/visible-ruby {:text "漢字" :ruby "かん"}))) + (t/is (nil? (cjk/visible-ruby {:text "漢字" :ruby "かん" :warichu "warichu"}))) + (t/is (nil? (cjk/visible-ruby {:text "漢字" :ruby "かん" :ruby-hidden true})))) + +(t/deftest ruby-span-is-true-for-hidden-readings + (t/is (cjk/ruby-span? {:ruby "かん" :ruby-hidden true})) + (t/is (not (cjk/ruby-span? {:ruby " "}))) + (t/is (not (cjk/ruby-span? {})))) + +(t/deftest whole-shape-attr-reads-the-first-paragraph + (let [content {:type "root" + :children [{:type "paragraph-set" + :children [{:type "paragraph" :writing-mode "vertical-rl" + :children [{:text "a"}]} + {:type "paragraph" :writing-mode "horizontal-tb" + :children [{:text "b"}]}]}]}] + (t/is (= "vertical-rl" (cjk/whole-shape-attr content :writing-mode))) + (t/is (nil? (cjk/whole-shape-attr content :text-orientation))))) + +(t/deftest regional-chinese-and-korean-word-profiles-validate + (doseq [language ["zh-Hans-CN" "zh-Hant-CN" "zh-Hant-TW" "zh-Hant-HK" "ko-KR"]] + (t/is (ctst/valid-content? + (with-paragraph-attrs {:text-language language :korean-wrap "keep-words"}))))) + + +(t/deftest annotation-position-enums-are-independent-span-styles + (doseq [[attr values] [[:text-emphasis-position ["auto" "over-right" "over-left" "under-right" "under-left"]] + [:text-underline-position ["auto" "under" "left" "right" "under-left" "under-right"]]]] + (t/is (= "auto" (cjk/enum-default attr))) + (t/is (some #{attr} cjk/span-attrs)) + (doseq [value values] + (t/is (cjk/valid-enum-value? attr value)) + (t/is (ctst/valid-content? (with-span-attrs {attr value})))) + (t/is (not (ctst/valid-content? (with-span-attrs {attr "invalid"})))) + (t/is (not (cjk/valid-enum-value? attr "invalid"))))) + +(t/deftest zhuyin-validates-one-han-base-and-one-mandarin-syllable + (doseq [base ["你" "中" "𠀀" "中︀"]] + (t/is (cjk/zhuyin-base? base))) + (doseq [base [nil "" "你好" "a" "한" "ㄋ"]] + (t/is (not (cjk/zhuyin-base? base)))) + (doseq [reading ["ㄓ" "ㄋㄧˇ" "ㄓㄨㄤˋ" "˙ㄉㄜ" "ㄉㄜ˙" "ㄇㄚˉ"]] + (t/is (cjk/zhuyin-reading? reading))) + (doseq [reading [nil "" "nǐ" "ˇ" "ㄋˇㄧ" "ㄋㄧˇˋ" "ㄋ ㄧ" "ㄅㄆㄇㄈ"]] + (t/is (not (cjk/zhuyin-reading? reading)))) + (t/is (false? (:ruby-zhuyin cjk/span-attr-defaults)))) + +(t/deftest zhuyin-neutral-tone-is-above-and-other-tones-are-outside-the-last-symbol + (let [[neutral first-symbol second-symbol] (cjk/zhuyin-slots "˙ㄉㄜ") + [first-normal last-normal tone] (cjk/zhuyin-slots "ㄋㄧˇ")] + (t/is (< (:y neutral) (:y first-symbol) (:y second-symbol))) + (t/is (= (:x neutral) (:x first-symbol) (:x second-symbol))) + (t/is (< (:y first-normal) (:y last-normal))) + (t/is (> (:x tone) (:x last-normal))) + (t/is (< (:y tone) (:y last-normal))))) + +(t/deftest merge-equal-spans-joins-neighbours-with-the-same-style + (t/testing "a missing attr equals its default, CJK defaults included" + (t/is (= [{:text "2027" :text-combine-upright "all"}] + (cttx/merge-equal-spans [{:text "2" :text-combine-upright "all"} + {:text "027" :text-combine-upright "all"}]))) + (t/is (= [{:text "abcd" :font-size 14}] + (cttx/merge-equal-spans [{:text "ab" :font-size 14} + {:text "cd" :text-combine-upright "none" :ruby nil}])))) + + (t/testing "spans with different styles stay apart" + (t/is (= [{:text "ab"} {:text "cd" :font-weight "700"} {:text "ef"}] + (cttx/merge-equal-spans [{:text "ab"} {:text "cd" :font-weight "700"} {:text "ef"}])))) + + (t/testing "readings and warichu notes are never joined, even when equal" + (let [spans [{:text "人" :ruby "hito"} {:text "々" :ruby "hito"} {:text "x"}]] + (t/is (= spans (cttx/merge-equal-spans spans)))) + (let [spans [{:text "注" :warichu "warichu"} {:text "釈" :warichu "warichu"}]] + (t/is (= spans (cttx/merge-equal-spans spans))))) + + (t/testing "no spans give no spans" + (t/is (= [] (cttx/merge-equal-spans []))))) + +(defn- cjk-content + "One paragraph with a span carrying the CJK attrs of every language." + [paragraph-attrs] + {:type "root" + :line-adjustment "push-out-first" + :children + [{:type "paragraph-set" + :children + [(merge {:type "paragraph" + :writing-mode "vertical-rl" + :text-orientation "upright" + :text-align "distribute" + :korean-wrap "keep-words" + :children [{:text "日本" + :ruby "にほん" + :ruby-zhuyin true + :text-combine-upright "digits" + :text-emphasis-position "over-left" + :warichu "warichu" + :font-features "palt"}]} + paragraph-attrs)]}]}) + +(defn- first-paragraph + [content] + (get-in content [:children 0 :children 0])) + +(t/deftest language-family-follows-the-primary-subtag + (t/is (= :japanese (cjk/language-family "ja-JP"))) + (t/is (= :chinese (cjk/language-family "zh-Hant-TW"))) + (t/is (= :korean (cjk/language-family "KO-kr"))) + (t/is (nil? (cjk/language-family "en-GB"))) + (t/is (nil? (cjk/language-family :multiple))) + (t/is (nil? (cjk/language-family nil)))) + +(t/deftest text-without-language-renders-horizontal-and-plain + (let [content (cjk-content {}) + active (cjk/active-content content) + paragraph (first-paragraph active)] + (t/is (not (contains? paragraph :writing-mode))) + (t/is (not (contains? paragraph :text-orientation))) + (t/is (= "left" (:text-align paragraph))) + (t/is (not (contains? paragraph :korean-wrap))) + (t/is (= {:text "日本"} (first (:children paragraph)))) + (t/is (not (contains? active :line-adjustment))) + (t/is (nil? (cjk/active-writing-mode content))) + (t/is (= content (cjk-content {})) "the stored content keeps every attr"))) + +(t/deftest each-language-renders-only-its-own-attrs + (let [span #(first (:children (first-paragraph %)))] + (let [active (cjk/active-content (cjk-content {:text-language "ja"}))] + (t/is (= {:text "日本" :ruby "にほん" :text-combine-upright "digits" + :warichu "warichu" :font-features "palt"} + (span active))) + (t/is (= "push-out-first" (:line-adjustment active))) + (t/is (= "vertical-rl" (:writing-mode (first-paragraph active)))) + (t/is (= "distribute" (:text-align (first-paragraph active)))) + (t/is (not (contains? (first-paragraph active) :korean-wrap)))) + (let [active (cjk/active-content (cjk-content {:text-language "zh-Hant-TW"}))] + (t/is (= {:text "日本" :ruby "にほん" :ruby-zhuyin true :text-emphasis-position "over-left"} + (span active))) + (t/is (not (contains? active :line-adjustment)))) + (let [active (cjk/active-content (cjk-content {:text-language "ko"}))] + (t/is (= {:text "日本"} (span active))) + (t/is (= "keep-words" (:korean-wrap (first-paragraph active)))) + (t/is (= "vertical-rl" (:writing-mode (first-paragraph active))))))) + +(defn- reading + ([text ruby] {:text text :ruby ruby}) + ([text ruby group] {:text text :ruby ruby :ruby-group group})) + +(t/deftest a-reading-joins-the-spans-of-its-group + (let [nodes [(reading "日" "にほん" "g") + (assoc (reading "本" "にほん" "g") :text-emphasis "filled-sesame") + {:text "語"}]] + (t/is (cjk/ruby-joins-previous? (first nodes) (second nodes))) + (t/is (= [[0 2]] (cjk/ruby-groups nodes))) + (t/is (= [[0 2] [2 3]] (cjk/ruby-base-runs nodes))))) + +(t/deftest neighbours-with-the-same-reading-stay-apart-without-a-group + (let [nodes [(reading "天" "tiān") (reading "天" "tiān")]] + (t/is (not (cjk/ruby-joins-previous? (first nodes) (second nodes)))) + (t/is (= [[0 1] [1 2]] (cjk/ruby-groups nodes))) + (t/is (= [[0 1] [1 2]] (cjk/ruby-base-runs nodes))))) + +(t/deftest a-group-joins-only-the-same-reading + (t/is (= [[0 1] [1 2]] + (cjk/ruby-groups [(reading "日" "に" "g") (reading "本" "ほん" "g")]))) + (t/is (= [[0 1] [1 2]] + (cjk/ruby-groups [(reading "日" "にほん" "a") (reading "本" "にほん" "b")])))) + +(t/deftest zhuyin-and-warichu-spans-never-join + (t/is (= [[0 1] [1 2]] + (cjk/ruby-groups [(assoc (reading "天" "ㄊㄧㄢ" "g") :ruby-zhuyin true) + (assoc (reading "天" "ㄊㄧㄢ" "g") :ruby-zhuyin true)]))) + (t/is (not (cjk/ruby-joins-previous? (reading "割" "わり" "g") + (assoc (reading "注" "わり" "g") :warichu "warichu"))))) + +(t/deftest the-ruby-group-is-text-content-not-style + (t/is (some #{:ruby-group} cjk/text-content-attrs)) + (t/is (= "" (:ruby-group cjk/span-attr-defaults)))) diff --git a/frontend/src/app/worker/index.cljs b/frontend/src/app/worker/index.cljs index 910fa74fae..e81ac70226 100644 --- a/frontend/src/app/worker/index.cljs +++ b/frontend/src/app/worker/index.cljs @@ -39,7 +39,8 @@ :add-typography :mod-typography :del-typography :set-tokens-lib :set-token :set-token-set :set-token-theme :set-tokens-status :set-own-tokens-status :rename-token-set-group :move-token-set - :move-token-set-group :set-base-font-size :set-tokens-source}) + :move-token-set-group :set-base-font-size :set-tokens-source + :set-default-text-language}) (defn- index-change? [{:keys [type]}] diff --git a/library/test/builder.test.js b/library/test/builder.test.js index e4c7bf8a37..5dce3340d7 100644 --- a/library/test/builder.test.js +++ b/library/test/builder.test.js @@ -110,6 +110,44 @@ test("create context with file and page", () => { assert.equal(rootShape.id, "00000000-0000-0000-0000-000000000000"); }); +test("create vertical text", () => { + const context = penpot.createBuildContext(); + + const fileId = context.addFile({name: "file 1"}); + const pageId = context.addPage({name: "page 1"}); + const textId = context.addText({ + name: "Vertical text", + x: 10, + y: 20, + width: 100, + height: 200, + content: { + type: "root", + children: [ + { + type: "paragraph-set", + children: [ + { + type: "paragraph", + writingMode: "vertical-rl", + textOrientation: "upright", + children: [{text: "縦書き"}], + }, + ], + }, + ], + }, + }); + + const internalState = context.getInternalState(); + const paragraph = + internalState.files[fileId].data.pagesIndex[pageId].objects[textId].content + .children[0].children[0]; + + assert.equal(paragraph.writingMode, "vertical-rl"); + assert.equal(paragraph.textOrientation, "upright"); +}); + test("create context with color", () => { const context = penpot.createBuildContext();