🐛 Fix CJK text readings and HTML language, and test the CJK API

Assigning `characters` gave the new text the style of the first span,
its reading and warichu note included, so one reading spread to every
character. The new text now drops them: they annotate their own
characters, not a style.

Removing a reading with `''` left an empty string that plugins read as
`ruby: ""`. A blank reading now reads `null`.

The HTML text renderers, used by generated code, `generateMarkup` and
the browser text, did not mark paragraphs with their language. Each
paragraph with one now carries `lang`, so browsers pick the localized
glyph forms and line breaking.

The Plugin API test suite covers every CJK member of Text and
TextRange: defaults, language tags, paragraph-wide range attrs,
`distribute`, Chinese annotations, mixed values, adding and removing
readings, the single-span rejection and invalid values. A live-only test
flattens a vertical CJK text. `api-surface.json` lists the CJK members.

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-26 11:19:35 +01:00
parent f0646be8e2
commit bdcc561eb9
10 changed files with 597 additions and 18 deletions

View File

@ -572,7 +572,8 @@
(defn change-text
"Changes the content of the text shape to use the text as argument. Will use the styles of the
first paragraph and text that is present in the shape (and override the rest)"
first paragraph and text that is present in the shape (and override the rest). A reading or
warichu note annotates its own characters, so the new text drops them."
[content text & {:as styles}]
(let [root-styles (select-keys content root-attrs)
@ -580,13 +581,17 @@
(merge
default-text-attrs
styles
(select-keys (->> content (node-seq is-paragraph-node?) first) text-all-attrs))
(-> (->> content (node-seq is-paragraph-node?) first)
(select-keys text-all-attrs)
(d/without-keys cjk/text-content-attrs)))
text-style
(merge
default-text-attrs
styles
(select-keys (->> content (node-seq is-text-node?) first) text-all-attrs))
(-> (->> content (node-seq is-text-node?) first)
(select-keys text-all-attrs)
(d/without-keys cjk/text-content-attrs)))
paragraph-texts
(str/split text "\n")

View File

@ -25,6 +25,18 @@
(t/is (= "distribute"
(get-in (cttx/change-text content "한글") [:children 0 :children 0 :text-align])))))
(t/deftest change-text-drops-the-annotations-of-the-first-span
(let [span (get-in content-base [:children 0 :children 0 :children 0])
content (assoc-in content-base [:children 0 :children 0 :children]
[(assoc span :text "紅葉" :ruby "もみじ" :ruby-group "g1"
:warichu "warichu" :text-emphasis "filled-dot")
(assoc span :text "の山")])
nodes (->> (cttx/change-text content "秋の空\n二行目")
(cttx/node-seq cttx/is-text-node?))]
(t/is (= ["秋の空" "二行目"] (map :text nodes)))
(t/is (every? #(not-any? (partial contains? %) cjk/text-content-attrs) nodes))
(t/is (every? #(= "filled-dot" (:text-emphasis %)) nodes))))
;; Normal happy-path changes
(def content-changed-text

View File

@ -64,8 +64,9 @@
(mf/defc render-paragraph*
[{:keys [node children shape]}]
(let [style (sts/generate-paragraph-styles shape node)
dir (:text-direction node "auto")]
[:p.paragraph {:style style :dir dir} children]))
dir (:text-direction node "auto")
lang (not-empty (:text-language node))]
[:p.paragraph {:style style :dir dir :lang lang} children]))
;; -- Text nodes
(mf/defc render-node*

View File

@ -95,8 +95,9 @@
[{:keys [node children shape is-code]}]
(let [style (when-not is-code (sts/generate-paragraph-styles shape node))
class (when is-code (:$id node))
dir (:text-direction node "auto")]
[:p.paragraph {:style style :dir dir :class class} children]))
dir (:text-direction node "auto")
lang (not-empty (:text-language node))]
[:p.paragraph {:style style :dir dir :lang lang :class class} children]))
(declare render-node*)

View File

@ -96,15 +96,21 @@
;; A range without ruby reports null to plugins.
(assoc cjk/span-attr-defaults :ruby nil))
(defn- span-cjk-value
"Value of the CJK span `attr` of text node `node`, or its default when
unset. A blank reading is no reading."
[node attr]
(let [value (get node attr (get cjk-range-defaults attr))]
(if (= attr :ruby) (not-empty value) value)))
(defn- shape-cjk-value
"Value of a CJK span `attr` over a whole text shape proxy, with unset
spans at their default: 'mixed' when spans differ."
[shape-proxy attr]
(let [default (get cjk-range-defaults attr)
values (->> (-> shape-proxy u/proxy->shape :content)
(txt/node-seq txt/is-text-node?)
(map #(get % attr default)))]
(if (seq values) (u/mixed-value values) default)))
(let [values (->> (-> shape-proxy u/proxy->shape :content)
(txt/node-seq txt/is-text-node?)
(map #(span-cjk-value % attr)))]
(if (seq values) (u/mixed-value values) (get cjk-range-defaults attr))))
(defn- whole-shape-value
"Value of a whole-shape paragraph `attr` (writing mode, orientation): the
@ -115,10 +121,9 @@
(defn- range-cjk-value
"Value of a CJK span `attr` over a text range proxy's characters."
[range-proxy start end attr]
(let [default (get cjk-range-defaults attr)]
(->> (-> range-proxy u/proxy->shape :content (content-range->text+styles start end))
(map #(get % attr default))
(u/mixed-value))))
(->> (-> range-proxy u/proxy->shape :content (content-range->text+styles start end))
(map #(span-cjk-value % attr))
(u/mixed-value)))
(defn- range-paragraph-value
"Value of a paragraph `attr` over the paragraphs a text range proxy

View File

@ -315,6 +315,23 @@
(mf/element fo-text/text-shape* #js {:shape text :grow-type :fixed}))]
(is (str/includes? markup "font-feature-settings:"palt"")))))
(deftest text-markup-marks-each-paragraph-language
(testing "generated markup tags each paragraph with its language"
(let [content (update-in ruby-text-content [:children 0 :children] conj
{:type "paragraph"
:children [{:text "Hi" :font-size "20"}]})
text (text-shape content)
markup (html/generate-markup (objects text) [text])]
(is (re-find #"<p[^>]* lang=\"ja\"" markup))
(is (= 1 (count (re-seq #" lang=" markup)))))))
(deftest foreign-object-text-marks-each-paragraph-language
(testing "browser/foreignObject render tags each paragraph with its language"
(let [content (assoc-in palt-text-content [:children 0 :children 0 :text-language] "zh-Hant-HK")
markup (rds/renderToStaticMarkup
(mf/element fo-text/text-shape* #js {:shape (text-shape content) :grow-type :fixed}))]
(is (re-find #"<p[^>]* lang=\"zh-Hant-HK\"" markup)))))
(deftest text-markup-emits-ruby-annotations
(testing "generated text markup keeps ruby annotations beside the base text"
(let [text (text-shape ruby-text-content)

View File

@ -29,6 +29,9 @@
### 🩹 Fixes
- **plugins-runtime:** Text range `align` and `direction` apply to the whole paragraphs the range touches and read their values from them. They used to set the range's spans, which nothing draws, and `direction` set a property no renderer reads.
- **plugins-runtime:** Assigning `characters` no longer copies the reading (`ruby`) or warichu note of the first text span to all the new text.
- **plugins-runtime:** A text or text range whose reading was removed with `''` reads `ruby` as `null`, not as an empty string.
- **plugins-runtime:** `generateMarkup` with `type: 'html'` tags each paragraph with its language (`lang`), so browsers pick the localized CJK glyph forms and line-breaking rules.
- **plugins-runtime:** `flatten()` outlines a text whose font is still loading once the font is in, instead of tracing a fallback font. Until then the returned shape is still a text; `penpot.waitForLayoutUpdate()` waits for the conversion.
- **plugins-runtime:** Methods that rejected an invalid value with a plain string (`connectLibrary`, file `restore`, `remove`, `pin`, `export`, `findVersions`, `saveVersion`) now reject with an `Error` carrying the same message.
- **plugin-runtime:** `LibraryComponent.instance()`, `createShapeFromSvg()` and `createShapeFromSvgWithImages()` now create shapes at the page root instead of inside a board at the page origin. Closes #12086.

View File

@ -546,10 +546,12 @@
],
"Text": [
"align",
"annotationClearance",
"applyTypography",
"characters",
"direction",
"fontFamily",
"fontFeatures",
"fontId",
"fontSize",
"fontStyle",
@ -557,32 +559,63 @@
"fontWeight",
"getRange",
"growType",
"lang",
"letterSpacing",
"lineAdjustment",
"lineHeight",
"ruby",
"rubyAlign",
"rubyHidden",
"rubyOverhang",
"rubySide",
"rubySize",
"rubyZhuyin",
"textBounds",
"textCombineUpright",
"textDecoration",
"textEmphasis",
"textEmphasisPosition",
"textOrientation",
"textTransform",
"textUnderlinePosition",
"type",
"verticalAlign"
"verticalAlign",
"warichu",
"writingMode"
],
"TextRange": [
"align",
"annotationClearance",
"applyTypography",
"characters",
"direction",
"fills",
"fontFamily",
"fontFeatures",
"fontId",
"fontSize",
"fontStyle",
"fontVariantId",
"fontWeight",
"lang",
"letterSpacing",
"lineHeight",
"ruby",
"rubyAlign",
"rubyHidden",
"rubyOverhang",
"rubySide",
"rubySize",
"rubyZhuyin",
"shape",
"textCombineUpright",
"textDecoration",
"textEmphasis",
"textEmphasisPosition",
"textTransform",
"verticalAlign"
"textUnderlinePosition",
"verticalAlign",
"warichu"
],
"ToggleOverlay": ["type"],
"TokenBase": [
@ -8986,6 +9019,114 @@
"type": null,
"array": false
},
"lineAdjustment": {
"decl": "Text",
"kind": "getset",
"type": null,
"array": false
},
"writingMode": {
"decl": "Text",
"kind": "getset",
"type": null,
"array": false
},
"textOrientation": {
"decl": "Text",
"kind": "getset",
"type": null,
"array": false
},
"textCombineUpright": {
"decl": "Text",
"kind": "getset",
"type": null,
"array": false
},
"textEmphasis": {
"decl": "Text",
"kind": "getset",
"type": null,
"array": false
},
"warichu": {
"decl": "Text",
"kind": "getset",
"type": null,
"array": false
},
"textEmphasisPosition": {
"decl": "Text",
"kind": "getset",
"type": null,
"array": false
},
"textUnderlinePosition": {
"decl": "Text",
"kind": "getset",
"type": null,
"array": false
},
"fontFeatures": {
"decl": "Text",
"kind": "getset",
"type": null,
"array": false
},
"annotationClearance": {
"decl": "Text",
"kind": "getset",
"type": null,
"array": false
},
"ruby": {
"decl": "Text",
"kind": "getset",
"type": null,
"array": false
},
"rubyHidden": {
"decl": "Text",
"kind": "getset",
"type": null,
"array": false
},
"rubySize": {
"decl": "Text",
"kind": "getset",
"type": null,
"array": false
},
"rubyAlign": {
"decl": "Text",
"kind": "getset",
"type": null,
"array": false
},
"rubyOverhang": {
"decl": "Text",
"kind": "getset",
"type": null,
"array": false
},
"rubyZhuyin": {
"decl": "Text",
"kind": "getset",
"type": null,
"array": false
},
"rubySide": {
"decl": "Text",
"kind": "getset",
"type": null,
"array": false
},
"lang": {
"decl": "Text",
"kind": "getset",
"type": null,
"array": false
},
"textBounds": {
"decl": "Text",
"kind": "get",
@ -9540,6 +9681,96 @@
"type": null,
"array": false
},
"textCombineUpright": {
"decl": "TextRange",
"kind": "getset",
"type": null,
"array": false
},
"textEmphasis": {
"decl": "TextRange",
"kind": "getset",
"type": null,
"array": false
},
"warichu": {
"decl": "TextRange",
"kind": "getset",
"type": null,
"array": false
},
"textEmphasisPosition": {
"decl": "TextRange",
"kind": "getset",
"type": null,
"array": false
},
"textUnderlinePosition": {
"decl": "TextRange",
"kind": "getset",
"type": null,
"array": false
},
"fontFeatures": {
"decl": "TextRange",
"kind": "getset",
"type": null,
"array": false
},
"annotationClearance": {
"decl": "TextRange",
"kind": "getset",
"type": null,
"array": false
},
"ruby": {
"decl": "TextRange",
"kind": "getset",
"type": null,
"array": false
},
"rubyHidden": {
"decl": "TextRange",
"kind": "getset",
"type": null,
"array": false
},
"rubySize": {
"decl": "TextRange",
"kind": "getset",
"type": null,
"array": false
},
"rubyAlign": {
"decl": "TextRange",
"kind": "getset",
"type": null,
"array": false
},
"rubyOverhang": {
"decl": "TextRange",
"kind": "getset",
"type": null,
"array": false
},
"rubyZhuyin": {
"decl": "TextRange",
"kind": "getset",
"type": null,
"array": false
},
"rubySide": {
"decl": "TextRange",
"kind": "getset",
"type": null,
"array": false
},
"lang": {
"decl": "TextRange",
"kind": "getset",
"type": null,
"array": false
},
"direction": {
"decl": "TextRange",
"kind": "getset",

View File

@ -239,6 +239,26 @@ describe('Shapes', () => {
expect(paths).toHaveLength(1);
expect(paths[0].type).toBe('path');
});
// Outlining a text needs its laid-out glyphs, which the mocked backend
// run does not produce.
test.skipIfMocked(
'flatten outlines a vertical CJK text once layout settles',
async (ctx) => {
const t = ctx.penpot.createText('縦書きの文');
if (!t) throw new Error('createText returned null');
ctx.board.appendChild(t);
t.lang = 'ja';
t.writingMode = 'vertical-rl';
await ctx.penpot.waitForLayoutUpdate();
const paths = ctx.penpot.flatten([t]);
await ctx.penpot.waitForLayoutUpdate();
expect(paths).toHaveLength(1);
expect(paths[0].id).toBe(t.id);
expect(paths[0].type).toBe('path');
},
);
});
describe('Align & distribute', () => {

View File

@ -470,4 +470,288 @@ describe('Text', () => {
t.characters = 'first line\nsecond line';
expect(t.characters).toBe('first line\nsecond line');
});
describe('CJK', () => {
test('a new text reports the CJK defaults', (ctx) => {
const t = text(ctx, '日本語');
expect(t.lang).toBeNull();
expect(t.writingMode).toBe('horizontal-tb');
expect(t.textOrientation).toBe('mixed');
expect(t.lineAdjustment).toBe('push-in-first');
expect(t.textCombineUpright).toBe('none');
expect(t.textEmphasis).toBe('none');
expect(t.textEmphasisPosition).toBe('auto');
expect(t.textUnderlinePosition).toBe('auto');
expect(t.warichu).toBe('none');
expect(t.fontFeatures).toBe('none');
expect(t.annotationClearance).toBe('none');
expect(t.ruby).toBeNull();
expect(t.rubyHidden).toBe(false);
expect(t.rubySize).toBe('half');
expect(t.rubyAlign).toBe('space-around');
expect(t.rubyOverhang).toBe('auto');
expect(t.rubyZhuyin).toBe(false);
expect(t.rubySide).toBe('over');
});
test('a new range reports the CJK defaults', (ctx) => {
const range = text(ctx, '日本語').getRange(0, 2);
expect(range.lang).toBeNull();
expect(range.textCombineUpright).toBe('none');
expect(range.textEmphasis).toBe('none');
expect(range.textEmphasisPosition).toBe('auto');
expect(range.textUnderlinePosition).toBe('auto');
expect(range.warichu).toBe('none');
expect(range.fontFeatures).toBe('none');
expect(range.annotationClearance).toBe('none');
expect(range.ruby).toBeNull();
expect(range.rubyHidden).toBe(false);
expect(range.rubySize).toBe('half');
expect(range.rubyAlign).toBe('space-around');
expect(range.rubyOverhang).toBe('auto');
expect(range.rubyZhuyin).toBe(false);
expect(range.rubySide).toBe('over');
});
test('lang round-trips and is removed with an empty string or null', (ctx) => {
const t = text(ctx, '香港的夜景');
t.lang = 'zh-Hant-HK';
expect(t.lang).toBe('zh-Hant-HK');
t.lang = '';
expect(t.lang).toBeNull();
t.lang = 'ja';
t.lang = null;
expect(t.lang).toBeNull();
});
test('lang rejects a value that is not a language tag', (ctx) => {
const t = text(ctx, '日本語');
expect(() => {
t.lang = 'not a tag';
}).toThrow();
expect(() => {
t.getRange(0, 1).lang = 'ja_JP';
}).toThrow();
expect(t.lang).toBeNull();
});
test('range lang applies to the paragraphs it touches', (ctx) => {
const t = text(ctx, '日本\n한국\n中文');
t.getRange(3, 5).lang = 'ko';
expect(t.getRange(0, 2).lang).toBeNull();
expect(t.getRange(3, 4).lang).toBe('ko');
expect(t.getRange(4, 5).lang).toBe('ko');
expect(t.lang).toBe('mixed');
t.getRange(0, t.characters.length).lang = 'ja';
expect(t.lang).toBe('ja');
});
test('range align and direction apply to the paragraphs they touch', (ctx) => {
const t = text(ctx, '日本語の文\n二行目');
t.getRange(1, 2).align = 'right';
t.getRange(1, 2).direction = 'rtl';
expect(t.getRange(3, 5).align).toBe('right');
expect(t.getRange(3, 5).direction).toBe('rtl');
expect(t.getRange(6, 8).align).not.toBe('right');
expect(t.align).toBe('mixed');
});
test('align accepts distribute on texts and ranges', (ctx) => {
const t = text(ctx, '均等割付\n二行目');
t.align = 'distribute';
expect(t.align).toBe('distribute');
t.getRange(5, 7).align = 'left';
expect(t.getRange(0, 2).align).toBe('distribute');
expect(t.getRange(5, 7).align).toBe('left');
});
test('Chinese annotation properties round-trip on texts', (ctx) => {
const t = text(ctx, '美');
t.lang = 'zh-Hant-TW';
t.textEmphasisPosition = 'under-left';
t.textUnderlinePosition = 'left';
t.ruby = 'ㄇㄟˇ';
t.rubyZhuyin = true;
t.rubyHidden = true;
expect(t.textEmphasisPosition).toBe('under-left');
expect(t.textUnderlinePosition).toBe('left');
expect(t.ruby).toBe('ㄇㄟˇ');
expect(t.rubyZhuyin).toBe(true);
expect(t.rubyHidden).toBe(true);
});
test('Chinese annotation properties round-trip on ranges', (ctx) => {
const t = text(ctx, '香港的夜景很美');
t.lang = 'zh-Hant-HK';
const base = t.getRange(6, 7);
base.ruby = 'ㄇㄟˇ';
base.rubyZhuyin = true;
base.rubyHidden = true;
const marks = t.getRange(3, 5);
marks.textEmphasis = 'filled-dot';
marks.textEmphasisPosition = 'over-right';
marks.textUnderlinePosition = 'under-right';
expect(base.ruby).toBe('ㄇㄟˇ');
expect(base.rubyZhuyin).toBe(true);
expect(base.rubyHidden).toBe(true);
expect(marks.textEmphasisPosition).toBe('over-right');
expect(marks.textUnderlinePosition).toBe('under-right');
expect(t.getRange(0, 2).rubyZhuyin).toBe(false);
});
test('every textCombineUpright value round-trips', (ctx) => {
const t = text(ctx, '令和7年');
for (const value of [
'all',
'digits',
'digits2',
'digits3',
'none',
] as const) {
t.textCombineUpright = value;
expect(t.textCombineUpright).toBe(value);
t.getRange(2, 3).textCombineUpright = value;
expect(t.getRange(2, 3).textCombineUpright).toBe(value);
}
});
test('a span property set on part of a text reads mixed', (ctx) => {
const t = text(ctx, '日本語の文');
t.getRange(0, 3).textEmphasis = 'open-sesame';
t.getRange(0, 3).fontFeatures = 'palt';
t.getRange(0, 3).rubySize = 'third';
expect(t.textEmphasis).toBe('mixed');
expect(t.fontFeatures).toBe('mixed');
expect(t.rubySize).toBe('mixed');
expect(t.getRange(0, 5).textEmphasis).toBe('mixed');
expect(t.getRange(3, 5).textEmphasis).toBe('none');
});
test('textOrientation stays a value, not a mixed report', (ctx) => {
const t = text(ctx, '縦書き\n二行目');
t.writingMode = 'vertical-rl';
t.textOrientation = 'upright';
expect(t.writingMode).toBe('vertical-rl');
expect(t.textOrientation).toBe('upright');
t.textOrientation = 'mixed';
expect(t.textOrientation).toBe('mixed');
});
test('a reading on part of a text makes the text ruby mixed', (ctx) => {
const t = text(ctx, '紅葉の山');
t.getRange(0, 2).ruby = 'もみじ';
expect(t.getRange(0, 2).ruby).toBe('もみじ');
expect(t.getRange(1, 2).ruby).toBe('もみじ');
expect(t.getRange(2, 4).ruby).toBeNull();
expect(t.ruby).toBe('mixed');
});
test('several readings can be added one after another', (ctx) => {
const t = text(ctx, '紅葉の山');
t.getRange(0, 2).ruby = 'もみじ';
t.getRange(3, 4).ruby = 'やま';
expect(t.getRange(0, 2).ruby).toBe('もみじ');
expect(t.getRange(3, 4).ruby).toBe('やま');
expect(t.getRange(2, 3).ruby).toBeNull();
});
test('a reading is changed or removed on the range it covers', (ctx) => {
const t = text(ctx, '日本の山');
const base = t.getRange(0, 2);
base.ruby = 'にっぽん';
base.ruby = 'にほん';
expect(base.ruby).toBe('にほん');
base.ruby = null;
expect(base.ruby).toBeNull();
const whole = text(ctx, '日本');
whole.ruby = 'にほん';
expect(whole.ruby).toBe('にほん');
whole.ruby = '';
expect(whole.ruby).toBeNull();
});
test('a reading or warichu note over several spans is rejected', (ctx) => {
const t = text(ctx, '日本語の文');
t.getRange(2, 3).textEmphasis = 'filled-dot';
expect(() => {
t.ruby = 'にほんごのぶん';
}).toThrow('single text span');
expect(() => {
t.warichu = 'warichu';
}).toThrow('single text span');
expect(() => {
t.getRange(0, 4).ruby = 'にほんごの';
}).toThrow('single text span');
expect(() => {
t.getRange(1, 3).warichu = 'warichu';
}).toThrow('single text span');
expect(t.ruby).toBeNull();
expect(t.warichu).toBe('none');
});
test('removing warichu or a reading is allowed over several spans', (ctx) => {
const t = text(ctx, '日本語の文');
t.getRange(0, 2).warichu = 'warichu';
t.getRange(3, 5).ruby = 'のぶん';
t.warichu = 'none';
t.ruby = null;
expect(t.warichu).toBe('none');
expect(t.ruby).toBeNull();
});
test('assigning characters drops the readings and notes', (ctx) => {
const t = text(ctx, '紅葉');
t.ruby = 'もみじ';
t.characters = '秋の空\n二行目';
expect(t.ruby).toBeNull();
const note = text(ctx, '割注');
note.warichu = 'warichu';
note.characters = '新しい文';
expect(note.warichu).toBe('none');
});
test('CJK properties reject unknown values', (ctx) => {
const t = text(ctx, '日本語');
const range = t.getRange(0, 1);
const spanProps = [
'textCombineUpright',
'textEmphasis',
'textEmphasisPosition',
'textUnderlinePosition',
'warichu',
'fontFeatures',
'annotationClearance',
'rubySize',
'rubyAlign',
'rubyOverhang',
'rubySide',
'rubyHidden',
'rubyZhuyin',
];
const textProps = [
'writingMode',
'textOrientation',
'lineAdjustment',
...spanProps,
];
for (const prop of textProps) {
expect(() => {
(t as unknown as Record<string, unknown>)[prop] = 'bogus';
}).toThrow();
}
for (const prop of spanProps) {
expect(() => {
(range as unknown as Record<string, unknown>)[prop] = 'bogus';
}).toThrow();
}
expect(() => {
(range as unknown as Record<string, unknown>)['ruby'] = 42;
}).toThrow();
expect(t.writingMode).toBe('horizontal-tb');
expect(t.textEmphasis).toBe('none');
expect(range.ruby).toBeNull();
});
});
});