mirror of
https://github.com/penpot/penpot.git
synced 2026-11-17 07:30:35 +00:00
✨ Lay out horizontal CJK text in the WASM renderer
Add the CJK layout engine and use it for horizontal text: - Line breaking with kinsoku rules that depend on the language: Japanese JLREQ, Chinese per region (Mainland, Hong Kong, Taiwan) and Korean word or syllable wrapping. Indivisible units stay together in narrow boxes. - Justification and squeezing that follow JLREQ, and distributed alignment that keeps lines inside the box. - Readings over the text (ruby, pinyin and zhuyin), emphasis marks, warichu and CJK underlines, attached to the ideographic em box and included in the export bounds. - Shared glyph shaping, font tables and CJK fallback fonts. `docs/cjk-layout.md` describes the design. Test fonts cover the locl, vertical metrics and pinyin cases. AI-assisted-by: claude-opus-5-5
This commit is contained in:
parent
894408ff7b
commit
028a36c656
248
render-wasm/docs/cjk-layout.md
Normal file
248
render-wasm/docs/cjk-layout.md
Normal file
@ -0,0 +1,248 @@
|
||||
# Chinese and Korean core layout
|
||||
|
||||
Paragraph `text-language` selects a layout policy. Writing mode remains a
|
||||
whole-shape value. UI availability never changes either attribute.
|
||||
|
||||
## Named Chinese presets
|
||||
|
||||
| Tags | Preset |
|
||||
| --- | --- |
|
||||
| `zh`, `zh-Hans`, `zh-Hant`, unknown regions | Original basic punctuation policy |
|
||||
| `zh-Hans-CN`, `zh-Hant-CN`, `zh-CN`, `zh-SG` | Mainland: adjacent punctuation compression; quarter-em Han/Latin gaps |
|
||||
| `zh-Hant-TW`, `zh-TW` | Taiwan: solid punctuation; quarter-em Han/Latin gaps |
|
||||
| `zh-Hant-HK`, `zh-HK`, `zh-MO` | Hong Kong: centered punctuation spacing; quarter-em Han/Latin gaps |
|
||||
|
||||
Script does not imply region. Changing a profile does not convert characters.
|
||||
These presets select a limited set of styles; they do not claim that every
|
||||
publication in a region uses the same settings.
|
||||
|
||||
Compression applies to eligible adjacent full-width punctuation. Vertical
|
||||
colon, semicolon, question and exclamation marks keep their full frame.
|
||||
Proportional horizontal punctuation is not compressed again. Vertical cells
|
||||
retain at least a half-em frame. The Taiwan and Hong Kong presets allow a
|
||||
single comma or full stop to hang by at most half an em vertically; adjacent
|
||||
punctuation and horizontal text do not hang.
|
||||
|
||||
Chinese justification expands eligible character gaps by at most half an em.
|
||||
The final line/column remains unexpanded. These bounds are explicit POC
|
||||
choices, not numeric requirements imposed by a regional standard.
|
||||
|
||||
## Korean modes
|
||||
|
||||
- Missing `korean-wrap`, or `syllable`: syllable wrapping with punctuation rules.
|
||||
- `keep-words`: keep adjoining letters and digits together across scripts and
|
||||
style runs. Punctuation keeps its own opportunities and restrictions.
|
||||
- `spaces-only`: preserve the earlier experiment. It remains readable in the
|
||||
UI for saved text but is not offered for new selections by default.
|
||||
|
||||
Fitting words move whole. Overlong words may use safe grapheme boundaries.
|
||||
Nonbreaking spaces/joiners, numeric prefixes/suffixes, paired dashes/ellipses,
|
||||
annotations and punctuation restrictions remain separate from letter ties.
|
||||
Korean justification first expands spaces, at most half an em per gap.
|
||||
Syllable mode may then expand eligible character gaps by at most one eighth
|
||||
of an em. Word mode never expands inside a word. Original spaces remain in
|
||||
source text; no Japanese word-space substitution applies.
|
||||
|
||||
## Japanese line adjustment
|
||||
|
||||
Japanese justification uses the JLREQ §3.8.4 ordered expansion in both
|
||||
orientations (`jlreq_ordered_expansion`). Gaps widen by pair-table stage:
|
||||
Western word spaces, then Japanese/Western gaps up to half an em, then
|
||||
solid kana and kanji up to a quarter em. Slack left after the three stages
|
||||
spreads evenly over the same gaps. Punctuation aki, gaps beside a ruby base,
|
||||
gaps inside a Western word and gaps across a warichu note take none. The
|
||||
final line or column stays unexpanded; distribution keeps its own path.
|
||||
|
||||
Squeezing (oikomi) follows the line adjustment setting in both orientations.
|
||||
Push-in first squeezes whenever the aki on the line can hold the next unit.
|
||||
Push-out first squeezes only when pushing out cannot resolve a kinsoku
|
||||
break, which horizontally means the break would move back over more than
|
||||
four kinsoku pairs. Horizontal text keeps a unit wider than the box whole
|
||||
on its own line instead. Push-out only never squeezes.
|
||||
|
||||
## Horizontal emergency wrapping and offsets
|
||||
|
||||
SkParagraph can ignore word joins during emergency wrapping. The horizontal
|
||||
plan measures candidate units and replaces joins with zero-width break
|
||||
opportunities only inside overlong units, at safe grapheme boundaries.
|
||||
Both marker characters occupy three UTF-8 bytes and one UTF-16 code unit.
|
||||
Their offset maps therefore stay identical for drawing, hit testing, carets,
|
||||
selection, text paths and export. Width participates in the plan cache key;
|
||||
resizing a box restores whole-word joins when the word fits again.
|
||||
|
||||
If an indivisible Chinese or Korean punctuation/annotation unit exceeds the
|
||||
box width, the plan lets it overflow together. It first measures surrounding
|
||||
text at the original width, then records those line starts with layout-only
|
||||
line separators before giving Skia enough physical width for the unit.
|
||||
Ordinary text keeps its original wrap budget. Source text stays unchanged;
|
||||
the plan's offset map accounts for every inserted separator.
|
||||
|
||||
Painting, strokes, caret/selection geometry, hit testing, annotations, paths
|
||||
and exports share the plan and its alignment offset. The width cache also
|
||||
rebuilds these breaks on resize. Legacy Korean `spaces-only` keeps its earlier
|
||||
behavior; the supported modes do not claim complete CSS `keep-all` conformance.
|
||||
|
||||
## Pinyin readings
|
||||
|
||||
The Chinese layout panel offers typed Pinyin readings when the selected
|
||||
paragraph has an explicit Chinese profile. In the WASM text editor, select
|
||||
one character or a group, in any text styles, then enter its reading. A
|
||||
reading over spans of several styles, such as characters with and without
|
||||
emphasis marks, gives them one `ruby-group`; the renderer draws one base
|
||||
over the spans of a group (`Paragraph::ruby_groups`), whose first span holds
|
||||
the reading and its presentation. To edit an existing reading, select its
|
||||
whole base. Selections across paragraphs, or that cut a reading or warichu
|
||||
note, do not offer a reading field, which prevents repeated readings. For
|
||||
character-by-character readings, annotate each character separately:
|
||||
neighbours with the same reading but no shared group stay separate bases.
|
||||
|
||||
Readings use the saved `ruby` attributes and the shared annotation renderer.
|
||||
They support size, alignment, overhang, side, hiding and annotation clearance.
|
||||
Adjacent reading spans recover the style covering each base even when Skia
|
||||
merges their styles. Emphasis marks retain their per-base placement across
|
||||
wraps; text bounds include the marks so individual exports keep them whole.
|
||||
Without a text selection, presentation changes affect existing readings.
|
||||
Horizontal readings form one Latin group above or below the base; vertical
|
||||
readings form one sideways Latin group beside it. A reading longer than its
|
||||
base overhangs an adjacent comma, full stop or middle dot (or kana) without
|
||||
its own reading, by at most one ruby character and half the neighbour, in
|
||||
both orientations. Han neighbours take no overhang. What overhang cannot
|
||||
absorb, or all of it under ruby-overhang `none`, spreads the base. Pronunciation is entered
|
||||
by the author; the editor does not generate or distribute syllables.
|
||||
|
||||
Composed and combining tone marks retain their source text and shaping.
|
||||
Glyphs sharing a cluster retain the same complete source range. Tests use
|
||||
`src/fonts/pinyin-test.ttf`, an 8 KB subset of the repository's Work Sans
|
||||
Regular font, with letters, umlauts and combining tone marks. The subset
|
||||
retains its copyright and SIL Open Font License metadata; its family name
|
||||
is Pinyin Test. FontTools generated it from the Pinyin test strings.
|
||||
|
||||
## Emphasis and underline positions
|
||||
|
||||
Chinese profiles expose emphasis shape, emphasis position, underline position
|
||||
and annotation clearance independently of Pinyin. Position controls do not
|
||||
turn on underlining; use the existing underline control for that.
|
||||
|
||||
Optional span `text-emphasis-position` stores `auto`, `over-right`,
|
||||
`over-left`, `under-right` or `under-left`. The first part selects the
|
||||
horizontal side; the second selects the vertical side. Auto uses below/right
|
||||
for Chinese and preserves above/right for Japanese, Korean and plain text.
|
||||
|
||||
Optional `text-underline-position` stores `auto`, `under`, `left`, `right`,
|
||||
`under-left` or `under-right`. Under places horizontal underlines below the
|
||||
base rect; left/right select the vertical side. Chinese auto uses below/left.
|
||||
Other auto values preserve the earlier baseline offset and vertical left
|
||||
side. Explicit values can select other styles without changing the language.
|
||||
These are supported POC choices, not a claim of complete decoration support.
|
||||
|
||||
Automatic clearance reserves each occupied side. Stacked annotations retain
|
||||
the largest reading and mark sizes across mixed-font spans, preserving the
|
||||
prior Japanese column widths. Underlines remain next to
|
||||
the base; readings move outside an underline on their side, and emphasis
|
||||
moves outside a reading on its side. The underline band reserves 0.18 em,
|
||||
a POC bound around the existing 1/9-em gap and stroke width. Disabling
|
||||
clearance retains fixed line-height behavior and can allow overlap/clipping.
|
||||
|
||||
Horizontal marks are shaped once per span and placed by their measured
|
||||
box (advance by ascent and descent at half the base size). Paint, text
|
||||
bounds and selection use that one box, so bounds cover exactly what paints.
|
||||
Position data carries the mark's em box centred on it, because SVG export
|
||||
draws the mark at a font size of that box's width. Vertical marks already
|
||||
centre the glyph and their position-data box on the same point.
|
||||
|
||||
Decoration edits that intersect a note, or a reading without a group, apply
|
||||
to its whole base, so changing a side cannot duplicate its reading on split
|
||||
spans. A grouped reading keeps one base when an edit splits its spans. Other text
|
||||
styles remain unchanged. Selection snapshots, Plugin API properties,
|
||||
CSS handoff and saved content retain the independent positions. CSS auto
|
||||
positions inherit the Chinese paragraph defaults; explicit positions emit
|
||||
the corresponding CSS keyword pairs. The binary writer uses former span
|
||||
header bytes 14/15; header size and prior offsets stay unchanged. Unknown
|
||||
bytes decode to auto. Both WASM consumers share that writer.
|
||||
|
||||
Defaults and positions were checked on 2026-10-20 against
|
||||
[CSS Text Decoration, emphasis](https://www.w3.org/TR/css-text-decor-3/#text-emphasis-position-property)
|
||||
and [underline positions](https://www.w3.org/TR/css-text-decor-3/#text-underline-position-property).
|
||||
Japanese/Korean vertical auto underlines retain this branch's existing left
|
||||
side rather than adopting the spec's suggested right-side style.
|
||||
|
||||
## Evidence and release checks
|
||||
|
||||
Renderer tests cover regional geometry, horizontal/vertical wrapping,
|
||||
style boundaries, decomposed Jamo, punctuation, justification, stable offsets
|
||||
and distinct wrap protocol bytes. Editor splits preserve the complete wrap
|
||||
mode. Both WASM consumers use the same writer and byte values: 0 syllable,
|
||||
1 experiment, 2 keep-words; unknown values fall back to syllable wrapping.
|
||||
The renderer decodes the byte through the exported `RawKoreanWrap` enum.
|
||||
|
||||
Real Pinyin, Zhuyin and Korean IME checks and native-reader review remain
|
||||
release checks. Font glyph coverage and localized forms still depend on the
|
||||
chosen fonts. Dedicated Zhuyin layout is described below.
|
||||
|
||||
References checked on 2026-10-19:
|
||||
|
||||
- [CLREQ draft, 2026-10-07](https://www.w3.org/TR/2026/DNOTE-clreq-20261007/),
|
||||
sections 6.1 and 6.3.2: punctuation boundaries, compression and hanging.
|
||||
- [KLREQ draft, 2026-03-21](https://www.w3.org/TR/2026/DNOTE-klreq-20260321/),
|
||||
sections 7.1 and 7.2: syllable/word wrapping and justification restrictions.
|
||||
- [CSS Text 3](https://www.w3.org/TR/css-text-3/#word-break-property):
|
||||
letter-breaking policy is distinct from punctuation and overflow handling.
|
||||
|
||||
### Mandarin Zhuyin annotations
|
||||
|
||||
An optional span flag, `ruby-zhuyin`, selects dedicated right-side Zhuyin.
|
||||
Absent or false retains normal ruby. Each span maps one Han character
|
||||
(optionally with one variation selector) to one typed Mandarin syllable in
|
||||
`ruby`. Enter 1–3 U+3105–U+3129 symbols and at most one tone: U+02C9, U+02CA,
|
||||
U+02C7, U+02CB or U+02D9. Neutral tone may precede or follow the symbols;
|
||||
other tones must follow them. The editor validates this syntax; it does not
|
||||
check pronunciation, infer mappings or generate readings. Unsupported API
|
||||
content keeps its text and falls back to ordinary ruby.
|
||||
|
||||
Symbols remain upright to the base's right in horizontal and vertical text.
|
||||
Their size is 0.3 em. A 0.6 em strip holds symbols and tones, including an
|
||||
empty tone lane for unmarked first tone. Non-neutral tones sit outside the
|
||||
upper right of the last symbol; neutral tone sits above the symbol stack.
|
||||
The stack centres vertically on the base. These rules follow CLREQ
|
||||
sections 5.5.3.2–5.5.3.3, retrieved 2026-10-20:
|
||||
<https://www.w3.org/TR/clreq/>. The strip is wider than the reference's
|
||||
half-em minimum to contain a full-width tone fallback glyph.
|
||||
|
||||
Horizontal layout reserves the strip in the inline advance. Vertical Auto
|
||||
clearance reserves it in the right gutter, outside a same-side underline
|
||||
and inside same-side emphasis. Fixed clearance may allow overlaps, as with
|
||||
ordinary ruby. Zhuyin ignores ruby size, alignment, overhang and side;
|
||||
hiding a reading removes its room without clearing saved text or mode.
|
||||
|
||||
The mode uses bit 1 of span header byte 13; bit 0 keeps over/under for normal
|
||||
ruby. Header size stays unchanged. Unknown flag bits fall back to ordinary
|
||||
over ruby. Position-data direction 5 carries a single upright annotation
|
||||
glyph with offsets into the original ruby string and its drawing baseline.
|
||||
Both layout paths use it for SVG; canvas, PNG, PDF and vertical outlines use
|
||||
the same shaped glyph positions. HTML handoff uses positioned symbol boxes;
|
||||
font metrics can make it differ from the renderer's ink-centred placement.
|
||||
|
||||
This slice supports Mandarin, one base per span. Group-to-syllable mapping,
|
||||
dialectal symbols/tones, automatic pronunciation, native IME acceptance and
|
||||
native-reader approval remain outside this slice. Changing a base into a
|
||||
group preserves the reading and uses normal ruby until a single base is
|
||||
restored. Existing Pinyin and Japanese readings keep their saved behavior.
|
||||
|
||||
## Renderer modules
|
||||
|
||||
- `shapes/text_policy.rs`: `TextLayoutPolicy` (`Plain`, `Japanese`,
|
||||
`Chinese(ChineseRegion)`, `Korean(KoreanWrap)`), parsed once per
|
||||
paragraph from `LanguageTag`. Layout code asks it named questions.
|
||||
- `shapes/jlreq.rs`: JLREQ classes (`JlreqClass`), pair rules and kinsoku.
|
||||
- `shapes/line_breaks.rs`: horizontal layout-text rewrite, `OffsetMap` and
|
||||
`PolicyBoundaries`.
|
||||
- `shapes/cjk_chars.rs`: shared Han, kana, hangul and punctuation ranges.
|
||||
- `shapes/text_attrs.rs`, `text_ruby.rs`, `text_warichu.rs` and
|
||||
`text_allocation.rs`: span attributes, ruby rules, warichu and the
|
||||
justify/squeeze allocators both axes share.
|
||||
- `shapes/text_shaping/`: glyph shaping, orientation and font tables.
|
||||
- `text_vertical/flow/`: JLREQ column planning, with the pairwise policies
|
||||
in `flow/pairwise.rs`. Horizontal and vertical helpers use the same
|
||||
`jlreq_*` and `pairwise_*` prefixes.
|
||||
- `wasm/text/layout_attrs.rs`: the `Raw*` enums the frontend writes, including
|
||||
`RawKoreanWrap`.
|
||||
68
render-wasm/scripts/build-cjk-test-font.py
Normal file
68
render-wasm/scripts/build-cjk-test-font.py
Normal file
@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Build the synthetic CJK language fixture (requires fonttools).
|
||||
|
||||
The one Han character has distinct JAN, ZHS and KOR localized glyphs and
|
||||
advances. This tests language propagation without a system font dependency.
|
||||
All outlines are original rectangles; no external font data is included.
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from fontTools.feaLib.builder import addOpenTypeFeaturesFromString
|
||||
from fontTools.fontBuilder import FontBuilder
|
||||
from fontTools.pens.ttGlyphPen import TTGlyphPen
|
||||
|
||||
|
||||
def main():
|
||||
builder = FontBuilder(1000, isTTF=True)
|
||||
names = [".notdef", "base", "japanese", "chinese", "korean", "square", "space", "thirdEm"]
|
||||
builder.setupGlyphOrder(names)
|
||||
cmap = {ord(char): "square" for char in "文あっ한국어문장한글한「」,。"}
|
||||
cmap.update({ord(char): "square" for char in "ㄅㄆㄇㄈㄉㄊㄋㄌㄍㄎㄏㄐㄑㄒㄓㄔㄕㄖㄗㄘㄙㄧㄨㄩㄚㄛㄜㄝㄞㄟㄠㄡㄢㄣㄤㄥㄦˉˊˇˋ˙"})
|
||||
cmap.update({0x4E2D: "base", 0x20: "space", 0x2004: "thirdEm"})
|
||||
builder.setupCharacterMap(cmap)
|
||||
glyphs = {}
|
||||
for index, name in enumerate(names):
|
||||
pen = TTGlyphPen(None)
|
||||
pen.moveTo((50, 0))
|
||||
pen.lineTo((350 + index * 20, 0))
|
||||
pen.lineTo((350 + index * 20, 700))
|
||||
pen.lineTo((50, 700))
|
||||
pen.closePath()
|
||||
glyphs[name] = pen.glyph()
|
||||
builder.setupGlyf(glyphs)
|
||||
metrics = {name: (400 + index * 100, 50) for index, name in enumerate(names)}
|
||||
metrics.update({"square": (1000, 50), "space": (500, 50), "thirdEm": (333, 50)})
|
||||
builder.setupHorizontalMetrics(metrics)
|
||||
builder.setupHorizontalHeader(ascent=800, descent=-200)
|
||||
builder.setupNameTable({
|
||||
"familyName": "Penpot CJK Test",
|
||||
"styleName": "Regular",
|
||||
"uniqueFontIdentifier": "PenpotCJKTest",
|
||||
"fullName": "Penpot CJK Test Regular",
|
||||
"psName": "PenpotCJKTest-Regular",
|
||||
})
|
||||
builder.setupOS2(sTypoAscender=800, sTypoDescender=-200,
|
||||
usWinAscent=800, usWinDescent=200)
|
||||
builder.setupPost()
|
||||
builder.setupMaxp()
|
||||
addOpenTypeFeaturesFromString(builder.font, """
|
||||
languagesystem DFLT dflt;
|
||||
languagesystem hani JAN;
|
||||
languagesystem hani ZHS;
|
||||
languagesystem hani KOR;
|
||||
feature locl {
|
||||
script hani;
|
||||
language JAN; sub base by japanese;
|
||||
language ZHS; sub base by chinese;
|
||||
language KOR; sub base by korean;
|
||||
} locl;
|
||||
""")
|
||||
builder.font["head"].created = 3800000000
|
||||
builder.font["head"].modified = 3800000000
|
||||
builder.font.recalcTimestamp = False
|
||||
builder.save(Path(__file__).resolve().parents[1] / "src/fonts/cjk-locl-test.ttf")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
BIN
render-wasm/src/fonts/cjk-locl-test.ttf
Normal file
BIN
render-wasm/src/fonts/cjk-locl-test.ttf
Normal file
Binary file not shown.
BIN
render-wasm/src/fonts/pinyin-test.ttf
Normal file
BIN
render-wasm/src/fonts/pinyin-test.ttf
Normal file
Binary file not shown.
@ -56,17 +56,30 @@ pub(crate) fn has_render_state() -> bool {
|
||||
unsafe { !RENDER_STATE.is_null() }
|
||||
}
|
||||
|
||||
/// The render resources in use: in tests, the ones this thread installed with
|
||||
/// [`TestRenderResourcesGuard`], else the global ones.
|
||||
#[inline(always)]
|
||||
fn render_resources() -> *mut RenderResources {
|
||||
#[cfg(test)]
|
||||
{
|
||||
let installed = TEST_RENDER_RESOURCES.with(std::cell::Cell::get);
|
||||
if !installed.is_null() {
|
||||
return installed;
|
||||
}
|
||||
}
|
||||
unsafe { RENDER_RESOURCES }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn has_render_resources() -> bool {
|
||||
unsafe { !RENDER_RESOURCES.is_null() }
|
||||
!render_resources().is_null()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn get_resources() -> &'static mut RenderResources {
|
||||
unsafe {
|
||||
assert!(!RENDER_RESOURCES.is_null(), "Render Resources is null");
|
||||
&mut *RENDER_RESOURCES
|
||||
}
|
||||
let resources = render_resources();
|
||||
assert!(!resources.is_null(), "Render Resources is null");
|
||||
unsafe { &mut *resources }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
@ -120,7 +133,16 @@ macro_rules! with_current_shape {
|
||||
};
|
||||
}
|
||||
|
||||
/// Scoped override of the global render resources pointer for unit tests.
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// Render resources a test installed for its own thread. Tests run on
|
||||
/// parallel threads, and Skia font collections are not thread-safe, so
|
||||
/// another test must never reach them.
|
||||
static TEST_RENDER_RESOURCES: std::cell::Cell<*mut RenderResources> =
|
||||
const { std::cell::Cell::new(std::ptr::null_mut()) };
|
||||
}
|
||||
|
||||
/// Scoped override of the render resources for the current test thread.
|
||||
#[cfg(test)]
|
||||
pub(crate) struct TestRenderResourcesGuard {
|
||||
prev: *mut RenderResources,
|
||||
@ -136,8 +158,7 @@ impl TestRenderResourcesGuard {
|
||||
let lock = TEST_RENDER_RESOURCES_LOCK
|
||||
.lock()
|
||||
.unwrap_or_else(|poisoned| poisoned.into_inner());
|
||||
let prev = unsafe { RENDER_RESOURCES };
|
||||
unsafe { RENDER_RESOURCES = resources as *mut _ };
|
||||
let prev = TEST_RENDER_RESOURCES.replace(resources as *mut _);
|
||||
Self { prev, _lock: lock }
|
||||
}
|
||||
}
|
||||
@ -145,7 +166,7 @@ impl TestRenderResourcesGuard {
|
||||
#[cfg(test)]
|
||||
impl Drop for TestRenderResourcesGuard {
|
||||
fn drop(&mut self) {
|
||||
unsafe { RENDER_RESOURCES = self.prev };
|
||||
TEST_RENDER_RESOURCES.set(self.prev);
|
||||
}
|
||||
}
|
||||
|
||||
@ -184,7 +205,7 @@ fn resources_init_headless() {
|
||||
}
|
||||
|
||||
/// Initializes DesignState.
|
||||
fn design_init() {
|
||||
pub(crate) fn design_init() {
|
||||
unsafe {
|
||||
let design_state = State::new();
|
||||
DESIGN_STATE = Box::into_raw(Box::new(design_state));
|
||||
|
||||
@ -1596,8 +1596,12 @@ impl RenderState {
|
||||
let rebound_text_content =
|
||||
stored_text_content.paint_content_for_selrect(shape.selrect());
|
||||
let text_content = rebound_text_content.as_ref();
|
||||
let mut paragraph_builders =
|
||||
text_content.paragraph_builder_group_from_text(None);
|
||||
// Vertical text paints from its cached vertical layout.
|
||||
let mut paragraph_builders = if text_content.is_vertical() {
|
||||
Vec::new()
|
||||
} else {
|
||||
text_content.paragraph_builder_group_from_text(None)
|
||||
};
|
||||
text::render(
|
||||
Some(self),
|
||||
None,
|
||||
@ -1749,7 +1753,9 @@ impl RenderState {
|
||||
|
||||
// Plain fill (no strokes / parent shadows): reuse cached layout
|
||||
// paragraphs when valid. Skip builder rebuild + Skia layout.
|
||||
let can_use_layout_cache = !shape.has_visible_strokes()
|
||||
// The full text pass paints the developer text grid.
|
||||
let can_use_layout_cache = !self.options.is_text_grid_visible()
|
||||
&& !shape.has_visible_strokes()
|
||||
&& parent_shadows.is_none()
|
||||
&& (skip_effects
|
||||
|| (shape.blur.is_none()
|
||||
@ -1769,191 +1775,41 @@ impl RenderState {
|
||||
let rebound_text_content =
|
||||
stored_text_content.paint_content_for_selrect(shape.selrect());
|
||||
let text_content = rebound_text_content.as_ref();
|
||||
let count_inner_strokes = shape.count_visible_inner_strokes();
|
||||
// Erode the main text fill by 1px when there are inner strokes, to avoid a visible seam at the glyph edge.
|
||||
let text_fill_inset = (count_inner_strokes > 0).then(|| 1.0 / self.get_scale());
|
||||
let text_stroke_blur_outset =
|
||||
Stroke::max_bounds_width(shape.visible_strokes(), false);
|
||||
let mut paragraph_builders =
|
||||
text_content.paragraph_builder_group_from_text(None);
|
||||
let stroke_kinds: Vec<StrokeKind> =
|
||||
shape.visible_strokes().rev().map(|s| s.kind).collect();
|
||||
let (mut stroke_paragraphs_list, stroke_opacities): (Vec<_>, Vec<_>) = shape
|
||||
.visible_strokes()
|
||||
.rev()
|
||||
.map(|stroke| {
|
||||
text::stroke_paragraph_builder_group_from_text(
|
||||
text_content,
|
||||
stroke,
|
||||
&shape.selrect(),
|
||||
None,
|
||||
)
|
||||
})
|
||||
.unzip();
|
||||
if skip_effects {
|
||||
// Fast path: render fills and strokes only (skip shadows/blur).
|
||||
text::render(
|
||||
Some(self),
|
||||
None,
|
||||
if text_content.is_vertical() {
|
||||
text::render_vertical_text(
|
||||
self,
|
||||
&shape,
|
||||
&mut paragraph_builders,
|
||||
Some(fills_surface_id),
|
||||
None,
|
||||
None,
|
||||
text_fill_inset,
|
||||
None,
|
||||
text_content,
|
||||
fills_surface_id,
|
||||
strokes_surface_id,
|
||||
skip_effects,
|
||||
)?;
|
||||
|
||||
for (i, (stroke_paragraphs, layer_opacity)) in stroke_paragraphs_list
|
||||
.iter_mut()
|
||||
.zip(stroke_opacities.iter())
|
||||
.enumerate()
|
||||
{
|
||||
if stroke_kinds[i] == StrokeKind::Inner {
|
||||
let mut fill_builders =
|
||||
text_content.paragraph_builder_group_from_text(None);
|
||||
text::render_inner_stroke(
|
||||
Some(self),
|
||||
None,
|
||||
&shape,
|
||||
stroke_paragraphs,
|
||||
&mut fill_builders,
|
||||
Some(strokes_surface_id),
|
||||
None,
|
||||
text_stroke_blur_outset,
|
||||
*layer_opacity,
|
||||
)?;
|
||||
} else if stroke_kinds[i] == StrokeKind::Outer {
|
||||
text::render_outer_stroke(
|
||||
Some(self),
|
||||
None,
|
||||
&shape,
|
||||
stroke_paragraphs,
|
||||
Some(strokes_surface_id),
|
||||
None,
|
||||
text_stroke_blur_outset,
|
||||
*layer_opacity,
|
||||
)?;
|
||||
} else {
|
||||
text::render_with_bounds_outset(
|
||||
Some(self),
|
||||
None,
|
||||
&shape,
|
||||
stroke_paragraphs,
|
||||
Some(strokes_surface_id),
|
||||
None,
|
||||
None,
|
||||
text_stroke_blur_outset,
|
||||
None,
|
||||
*layer_opacity,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
if shape.has_visible_strokes() && text_content.has_non_ascii() {
|
||||
let mut emoji_builders = text_content.paragraph_builder_group_opaque();
|
||||
let mut deco_builders =
|
||||
text_content.paragraph_builder_group_from_text(None);
|
||||
text::render_emoji_overlay(
|
||||
self,
|
||||
&shape,
|
||||
&mut emoji_builders,
|
||||
&mut deco_builders,
|
||||
strokes_surface_id,
|
||||
None,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
let shape_scale = self.get_scale();
|
||||
let mut drop_shadows = if skip_drop_shadows {
|
||||
Vec::new()
|
||||
} else {
|
||||
let count_inner_strokes = shape.count_visible_inner_strokes();
|
||||
// Erode the main text fill by 1px when there are inner strokes, to avoid a visible seam at the glyph edge.
|
||||
let text_fill_inset =
|
||||
(count_inner_strokes > 0).then(|| 1.0 / self.get_scale());
|
||||
let text_stroke_blur_outset =
|
||||
Stroke::max_bounds_width(shape.visible_strokes(), false);
|
||||
let mut paragraph_builders =
|
||||
text_content.paragraph_builder_group_from_text(None);
|
||||
let stroke_kinds: Vec<StrokeKind> =
|
||||
shape.visible_strokes().rev().map(|s| s.kind).collect();
|
||||
let (mut stroke_paragraphs_list, stroke_opacities): (Vec<_>, Vec<_>) =
|
||||
shape
|
||||
.drop_shadows_visible()
|
||||
.filter(|s| s.is_perceptible_at_scale(shape_scale))
|
||||
.map(|shadow| {
|
||||
let mut paint = skia_safe::Paint::default();
|
||||
paint.set_image_filter(shadow.get_drop_shadow_filter());
|
||||
paint
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
|
||||
if !skip_drop_shadows {
|
||||
if let Some(inherited_shadows) = self.get_inherited_drop_shadows() {
|
||||
drop_shadows.extend(inherited_shadows);
|
||||
}
|
||||
}
|
||||
|
||||
let inner_shadows = shape.inner_shadow_paints();
|
||||
let blur_filter = shape.image_filter(1.);
|
||||
let mut paragraphs_with_shadows =
|
||||
text_content.paragraph_builder_group_from_text(Some(true));
|
||||
let (mut stroke_paragraphs_with_shadows_list, _shadow_opacities): (
|
||||
Vec<_>,
|
||||
Vec<_>,
|
||||
) = shape
|
||||
.visible_strokes()
|
||||
.rev()
|
||||
.map(|stroke| {
|
||||
text::stroke_paragraph_builder_group_from_text(
|
||||
text_content,
|
||||
stroke,
|
||||
&shape.selrect(),
|
||||
Some(true),
|
||||
)
|
||||
})
|
||||
.unzip();
|
||||
|
||||
if let Some(parent_shadows) = parent_shadows {
|
||||
if !skip_drop_shadows {
|
||||
if !shape.has_visible_strokes() {
|
||||
for shadow in parent_shadows {
|
||||
text::render(
|
||||
Some(self),
|
||||
None,
|
||||
&shape,
|
||||
&mut paragraphs_with_shadows,
|
||||
text_drop_shadows_surface_id.into(),
|
||||
Some(&shadow),
|
||||
blur_filter.as_ref(),
|
||||
None,
|
||||
None,
|
||||
)?;
|
||||
}
|
||||
} else {
|
||||
shadows::render_text_shadows(
|
||||
self,
|
||||
&shape,
|
||||
&mut paragraphs_with_shadows,
|
||||
&mut stroke_paragraphs_with_shadows_list,
|
||||
text_drop_shadows_surface_id.into(),
|
||||
&parent_shadows,
|
||||
&blur_filter,
|
||||
&stroke_kinds,
|
||||
.visible_strokes()
|
||||
.rev()
|
||||
.map(|stroke| {
|
||||
text::stroke_paragraph_builder_group_from_text(
|
||||
text_content,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 1. Text drop shadows
|
||||
if !shape.has_visible_strokes() {
|
||||
for shadow in &drop_shadows {
|
||||
text::render(
|
||||
Some(self),
|
||||
stroke,
|
||||
&shape.selrect(),
|
||||
None,
|
||||
&shape,
|
||||
&mut paragraphs_with_shadows,
|
||||
text_drop_shadows_surface_id.into(),
|
||||
Some(shadow),
|
||||
blur_filter.as_ref(),
|
||||
None,
|
||||
None,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Text fills
|
||||
)
|
||||
})
|
||||
.unzip();
|
||||
if skip_effects {
|
||||
// Fast path: render fills and strokes only (skip shadows/blur).
|
||||
text::render(
|
||||
Some(self),
|
||||
None,
|
||||
@ -1961,25 +1817,11 @@ impl RenderState {
|
||||
&mut paragraph_builders,
|
||||
Some(fills_surface_id),
|
||||
None,
|
||||
blur_filter.as_ref(),
|
||||
None,
|
||||
text_fill_inset,
|
||||
None,
|
||||
)?;
|
||||
|
||||
// 3. Stroke drop shadows
|
||||
shadows::render_text_shadows(
|
||||
self,
|
||||
&shape,
|
||||
&mut paragraphs_with_shadows,
|
||||
&mut stroke_paragraphs_with_shadows_list,
|
||||
text_drop_shadows_surface_id.into(),
|
||||
&drop_shadows,
|
||||
&blur_filter,
|
||||
&stroke_kinds,
|
||||
text_content,
|
||||
)?;
|
||||
|
||||
// 4. Stroke fills
|
||||
for (i, (stroke_paragraphs, layer_opacity)) in stroke_paragraphs_list
|
||||
.iter_mut()
|
||||
.zip(stroke_opacities.iter())
|
||||
@ -1995,7 +1837,7 @@ impl RenderState {
|
||||
stroke_paragraphs,
|
||||
&mut fill_builders,
|
||||
Some(strokes_surface_id),
|
||||
blur_filter.as_ref(),
|
||||
None,
|
||||
text_stroke_blur_outset,
|
||||
*layer_opacity,
|
||||
)?;
|
||||
@ -2006,7 +1848,7 @@ impl RenderState {
|
||||
&shape,
|
||||
stroke_paragraphs,
|
||||
Some(strokes_surface_id),
|
||||
blur_filter.as_ref(),
|
||||
None,
|
||||
text_stroke_blur_outset,
|
||||
*layer_opacity,
|
||||
)?;
|
||||
@ -2018,7 +1860,7 @@ impl RenderState {
|
||||
stroke_paragraphs,
|
||||
Some(strokes_surface_id),
|
||||
None,
|
||||
blur_filter.as_ref(),
|
||||
None,
|
||||
text_stroke_blur_outset,
|
||||
None,
|
||||
*layer_opacity,
|
||||
@ -2026,7 +1868,7 @@ impl RenderState {
|
||||
}
|
||||
}
|
||||
|
||||
if shape.has_visible_strokes() && text_content.has_non_ascii() {
|
||||
if shape.has_visible_strokes() && text_content.has_emoji() {
|
||||
let mut emoji_builders =
|
||||
text_content.paragraph_builder_group_opaque();
|
||||
let mut deco_builders =
|
||||
@ -2037,41 +1879,236 @@ impl RenderState {
|
||||
&mut emoji_builders,
|
||||
&mut deco_builders,
|
||||
strokes_surface_id,
|
||||
blur_filter.as_ref(),
|
||||
None,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
let shape_scale = self.get_scale();
|
||||
let mut drop_shadows = if skip_drop_shadows {
|
||||
Vec::new()
|
||||
} else {
|
||||
shape
|
||||
.drop_shadows_visible()
|
||||
.filter(|s| s.is_perceptible_at_scale(shape_scale))
|
||||
.map(|shadow| {
|
||||
let mut paint = skia_safe::Paint::default();
|
||||
paint.set_image_filter(shadow.get_drop_shadow_filter());
|
||||
paint
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
|
||||
// 5. Stroke inner shadows
|
||||
shadows::render_text_shadows(
|
||||
self,
|
||||
&shape,
|
||||
&mut paragraphs_with_shadows,
|
||||
&mut stroke_paragraphs_with_shadows_list,
|
||||
Some(innershadows_surface_id),
|
||||
&inner_shadows,
|
||||
&blur_filter,
|
||||
&stroke_kinds,
|
||||
text_content,
|
||||
)?;
|
||||
if !skip_drop_shadows {
|
||||
if let Some(inherited_shadows) = self.get_inherited_drop_shadows() {
|
||||
drop_shadows.extend(inherited_shadows);
|
||||
}
|
||||
}
|
||||
|
||||
// 6. Fill Inner shadows
|
||||
if !shape.has_visible_strokes() {
|
||||
for shadow in &inner_shadows {
|
||||
text::render(
|
||||
Some(self),
|
||||
None,
|
||||
let inner_shadows = shape.inner_shadow_paints();
|
||||
let blur_filter = shape.image_filter(1.);
|
||||
// Shadow builder groups only serve shadow passes.
|
||||
let needs_shadow_groups = parent_shadows.is_some()
|
||||
|| !drop_shadows.is_empty()
|
||||
|| !inner_shadows.is_empty();
|
||||
let mut paragraphs_with_shadows = if needs_shadow_groups {
|
||||
text_content.paragraph_builder_group_from_text(Some(true))
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
let (mut stroke_paragraphs_with_shadows_list, _shadow_opacities): (
|
||||
Vec<_>,
|
||||
Vec<_>,
|
||||
) = if needs_shadow_groups {
|
||||
shape
|
||||
.visible_strokes()
|
||||
.rev()
|
||||
.map(|stroke| {
|
||||
text::stroke_paragraph_builder_group_from_text(
|
||||
text_content,
|
||||
stroke,
|
||||
&shape.selrect(),
|
||||
Some(true),
|
||||
)
|
||||
})
|
||||
.unzip()
|
||||
} else {
|
||||
(Vec::new(), Vec::new())
|
||||
};
|
||||
|
||||
if let Some(parent_shadows) = parent_shadows {
|
||||
if !skip_drop_shadows {
|
||||
if !shape.has_visible_strokes() {
|
||||
for shadow in parent_shadows {
|
||||
text::render(
|
||||
Some(self),
|
||||
None,
|
||||
&shape,
|
||||
&mut paragraphs_with_shadows,
|
||||
text_drop_shadows_surface_id.into(),
|
||||
Some(&shadow),
|
||||
blur_filter.as_ref(),
|
||||
None,
|
||||
None,
|
||||
)?;
|
||||
}
|
||||
} else {
|
||||
shadows::render_text_shadows(
|
||||
self,
|
||||
&shape,
|
||||
&mut paragraphs_with_shadows,
|
||||
&mut stroke_paragraphs_with_shadows_list,
|
||||
text_drop_shadows_surface_id.into(),
|
||||
&parent_shadows,
|
||||
&blur_filter,
|
||||
&stroke_kinds,
|
||||
text_content,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 1. Text drop shadows
|
||||
if !shape.has_visible_strokes() {
|
||||
for shadow in &drop_shadows {
|
||||
text::render(
|
||||
Some(self),
|
||||
None,
|
||||
&shape,
|
||||
&mut paragraphs_with_shadows,
|
||||
text_drop_shadows_surface_id.into(),
|
||||
Some(shadow),
|
||||
blur_filter.as_ref(),
|
||||
None,
|
||||
None,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Text fills
|
||||
text::render(
|
||||
Some(self),
|
||||
None,
|
||||
&shape,
|
||||
&mut paragraph_builders,
|
||||
Some(fills_surface_id),
|
||||
None,
|
||||
blur_filter.as_ref(),
|
||||
text_fill_inset,
|
||||
None,
|
||||
)?;
|
||||
|
||||
// 3. Stroke drop shadows
|
||||
shadows::render_text_shadows(
|
||||
self,
|
||||
&shape,
|
||||
&mut paragraphs_with_shadows,
|
||||
&mut stroke_paragraphs_with_shadows_list,
|
||||
text_drop_shadows_surface_id.into(),
|
||||
&drop_shadows,
|
||||
&blur_filter,
|
||||
&stroke_kinds,
|
||||
text_content,
|
||||
)?;
|
||||
|
||||
// 4. Stroke fills
|
||||
for (i, (stroke_paragraphs, layer_opacity)) in
|
||||
stroke_paragraphs_list
|
||||
.iter_mut()
|
||||
.zip(stroke_opacities.iter())
|
||||
.enumerate()
|
||||
{
|
||||
if stroke_kinds[i] == StrokeKind::Inner {
|
||||
let mut fill_builders =
|
||||
text_content.paragraph_builder_group_from_text(None);
|
||||
text::render_inner_stroke(
|
||||
Some(self),
|
||||
None,
|
||||
&shape,
|
||||
stroke_paragraphs,
|
||||
&mut fill_builders,
|
||||
Some(strokes_surface_id),
|
||||
blur_filter.as_ref(),
|
||||
text_stroke_blur_outset,
|
||||
*layer_opacity,
|
||||
)?;
|
||||
} else if stroke_kinds[i] == StrokeKind::Outer {
|
||||
text::render_outer_stroke(
|
||||
Some(self),
|
||||
None,
|
||||
&shape,
|
||||
stroke_paragraphs,
|
||||
Some(strokes_surface_id),
|
||||
blur_filter.as_ref(),
|
||||
text_stroke_blur_outset,
|
||||
*layer_opacity,
|
||||
)?;
|
||||
} else {
|
||||
text::render_with_bounds_outset(
|
||||
Some(self),
|
||||
None,
|
||||
&shape,
|
||||
stroke_paragraphs,
|
||||
Some(strokes_surface_id),
|
||||
None,
|
||||
blur_filter.as_ref(),
|
||||
text_stroke_blur_outset,
|
||||
None,
|
||||
*layer_opacity,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
if shape.has_visible_strokes() && text_content.has_emoji() {
|
||||
let mut emoji_builders =
|
||||
text_content.paragraph_builder_group_opaque();
|
||||
let mut deco_builders =
|
||||
text_content.paragraph_builder_group_from_text(None);
|
||||
text::render_emoji_overlay(
|
||||
self,
|
||||
&shape,
|
||||
&mut paragraphs_with_shadows,
|
||||
Some(innershadows_surface_id),
|
||||
Some(shadow),
|
||||
&mut emoji_builders,
|
||||
&mut deco_builders,
|
||||
strokes_surface_id,
|
||||
blur_filter.as_ref(),
|
||||
None,
|
||||
None,
|
||||
)?;
|
||||
);
|
||||
}
|
||||
|
||||
// 5. Stroke inner shadows
|
||||
shadows::render_text_shadows(
|
||||
self,
|
||||
&shape,
|
||||
&mut paragraphs_with_shadows,
|
||||
&mut stroke_paragraphs_with_shadows_list,
|
||||
Some(innershadows_surface_id),
|
||||
&inner_shadows,
|
||||
&blur_filter,
|
||||
&stroke_kinds,
|
||||
text_content,
|
||||
)?;
|
||||
|
||||
// 6. Fill Inner shadows
|
||||
if !shape.has_visible_strokes() {
|
||||
for shadow in &inner_shadows {
|
||||
text::render(
|
||||
Some(self),
|
||||
None,
|
||||
&shape,
|
||||
&mut paragraphs_with_shadows,
|
||||
Some(innershadows_surface_id),
|
||||
Some(shadow),
|
||||
blur_filter.as_ref(),
|
||||
None,
|
||||
None,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !text_content.is_vertical() && self.options.is_text_grid_visible() {
|
||||
let canvas = self.surfaces.canvas_and_mark_dirty(fills_surface_id);
|
||||
text::paint_horizontal_grid(canvas, &shape, text_content);
|
||||
}
|
||||
} // end layout-cache miss fallback
|
||||
}
|
||||
_ => {
|
||||
@ -4914,10 +4951,6 @@ impl RenderState {
|
||||
(self.viewbox.zoom - self.cached_viewbox.zoom).abs() > f32::EPSILON
|
||||
}
|
||||
|
||||
pub fn mark_touched(&mut self, uuid: Uuid) {
|
||||
self.mark_touched_with_prev(uuid, None);
|
||||
}
|
||||
|
||||
pub fn mark_touched_with_prev(&mut self, uuid: Uuid, prev_extrect: Option<Rect>) {
|
||||
if self.touched_ids.insert(uuid) {
|
||||
if let Some(rect) = prev_extrect.filter(|r| !r.is_empty()) {
|
||||
|
||||
@ -1,7 +1,9 @@
|
||||
use skia_safe::{self as skia, textlayout, Font, FontMgr};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::collections::{BTreeSet, HashMap, HashSet};
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::shapes::cjk_chars::{is_hangul, is_kana};
|
||||
use crate::shapes::text_policy::LanguageTag;
|
||||
use crate::shapes::{FontFamily, FontStyle};
|
||||
use crate::uuid::Uuid;
|
||||
|
||||
@ -19,13 +21,114 @@ fn default_font_uuid() -> Uuid {
|
||||
Uuid::nil()
|
||||
}
|
||||
|
||||
/// CJK region whose glyph forms a fallback face draws, read from its family
|
||||
/// name (`Noto Sans JP`, `Noto Sans TC`, ...). The declaration order is the
|
||||
/// fallback order when no language prefers a face.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum CjkFace {
|
||||
SimplifiedChinese,
|
||||
TraditionalChinese,
|
||||
HongKongChinese,
|
||||
Japanese,
|
||||
Korean,
|
||||
}
|
||||
|
||||
impl CjkFace {
|
||||
fn from_family_name(name: &str) -> Option<Self> {
|
||||
match name.rsplit(' ').next()? {
|
||||
"SC" => Some(Self::SimplifiedChinese),
|
||||
"TC" => Some(Self::TraditionalChinese),
|
||||
"HK" => Some(Self::HongKongChinese),
|
||||
"JP" => Some(Self::Japanese),
|
||||
"KR" => Some(Self::Korean),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Faces drawing Han and CJK punctuation in the forms `language` (a BCP 47
|
||||
/// tag) expects, best first. Without a CJK language, kana or hangul in
|
||||
/// `text` decide.
|
||||
fn preferred(language: &str, text: &str) -> &'static [Self] {
|
||||
let tag = LanguageTag::parse(language);
|
||||
if tag.is("ja") {
|
||||
&[Self::Japanese]
|
||||
} else if tag.is("ko") {
|
||||
&[Self::Korean]
|
||||
} else if tag.is("zh") {
|
||||
if tag.has_region(&["HK", "MO"]) {
|
||||
&[Self::HongKongChinese, Self::TraditionalChinese]
|
||||
} else if tag.has_region(&["TW"]) || tag.has_script("Hant") {
|
||||
&[Self::TraditionalChinese, Self::HongKongChinese]
|
||||
} else {
|
||||
&[Self::SimplifiedChinese]
|
||||
}
|
||||
} else if text.chars().any(is_kana) {
|
||||
&[Self::Japanese]
|
||||
} else if text.chars().any(is_hangul) {
|
||||
&[Self::Korean]
|
||||
} else {
|
||||
&[]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Families that take part in character fallback, with the CJK region of
|
||||
/// those that draw Han.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct FallbackFonts {
|
||||
families: BTreeSet<String>,
|
||||
cjk_faces: HashMap<String, CjkFace>,
|
||||
}
|
||||
|
||||
impl FallbackFonts {
|
||||
/// Fallback families without CJK regions, in name order.
|
||||
#[cfg(test)]
|
||||
pub fn from_families(families: impl IntoIterator<Item = String>) -> Self {
|
||||
Self {
|
||||
families: families.into_iter().collect(),
|
||||
cjk_faces: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn insert(&mut self, alias: String, cjk_face: Option<CjkFace>) {
|
||||
if let Some(cjk_face) = cjk_face {
|
||||
self.cjk_faces.insert(alias.clone(), cjk_face);
|
||||
}
|
||||
self.families.insert(alias);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn contains(&self, alias: &str) -> bool {
|
||||
self.families.contains(alias)
|
||||
}
|
||||
|
||||
/// Fallback families for `text` in `language`: the CJK faces the language
|
||||
/// prefers, then the non-CJK faces, then the other CJK faces. CJK faces
|
||||
/// share Han characters with different glyph forms, so the first one
|
||||
/// decides how they look.
|
||||
pub fn for_text(&self, language: &str, text: &str) -> Vec<String> {
|
||||
let preferred = CjkFace::preferred(language, text);
|
||||
let mut families: Vec<&String> = self.families.iter().collect();
|
||||
families.sort_by_key(|alias| match self.cjk_faces.get(*alias) {
|
||||
Some(face) => match preferred.iter().position(|p| p == face) {
|
||||
Some(rank) => (0, rank),
|
||||
None => (2, *face as usize),
|
||||
},
|
||||
None => (1, 0),
|
||||
});
|
||||
families.into_iter().cloned().collect()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FontStore {
|
||||
font_mgr: FontMgr,
|
||||
font_provider: textlayout::TypefaceFontProvider,
|
||||
font_collection: textlayout::FontCollection,
|
||||
debug_font: Font,
|
||||
ui_font: Font,
|
||||
fallback_fonts: HashSet<String>,
|
||||
fallback_fonts: FallbackFonts,
|
||||
/// CJK region of each registered family that draws Han.
|
||||
cjk_faces: HashMap<String, CjkFace>,
|
||||
/// Source URL registered when the font was fetched (SVG export references
|
||||
/// this in `@font-face` rules).
|
||||
source_urls: HashMap<String, String>,
|
||||
@ -62,7 +165,8 @@ impl FontStore {
|
||||
font_collection,
|
||||
debug_font,
|
||||
ui_font,
|
||||
fallback_fonts: HashSet::new(),
|
||||
fallback_fonts: FallbackFonts::default(),
|
||||
cjk_faces: HashMap::new(),
|
||||
source_urls: HashMap::new(),
|
||||
})
|
||||
}
|
||||
@ -95,7 +199,11 @@ impl FontStore {
|
||||
is_emoji: bool,
|
||||
is_fallback: bool,
|
||||
) -> Result<()> {
|
||||
let alias = format!("{}", family);
|
||||
if self.has_family(&family, is_emoji) {
|
||||
if is_fallback {
|
||||
self.insert_fallback(alias);
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@ -106,23 +214,39 @@ impl FontStore {
|
||||
"Failed to create typeface".to_string(),
|
||||
))?;
|
||||
|
||||
let alias = format!("{}", family);
|
||||
let font_name = if is_emoji {
|
||||
DEFAULT_EMOJI_FONT
|
||||
} else {
|
||||
alias.as_str()
|
||||
};
|
||||
|
||||
if let Some(cjk_face) = CjkFace::from_family_name(&typeface.family_name()) {
|
||||
self.cjk_faces.insert(alias.clone(), cjk_face);
|
||||
}
|
||||
self.font_provider.register_typeface(typeface, font_name);
|
||||
self.font_collection.clear_caches();
|
||||
|
||||
if is_fallback {
|
||||
self.fallback_fonts.insert(alias);
|
||||
self.insert_fallback(alias);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn insert_fallback(&mut self, alias: String) {
|
||||
let cjk_face = self.cjk_faces.get(&alias).copied();
|
||||
self.fallback_fonts.insert(alias, cjk_face);
|
||||
}
|
||||
|
||||
/// Upgrade an already-uploaded family to take part in character fallback.
|
||||
/// Font bytes are cached apart from a face's role, so a family may arrive
|
||||
/// as a document font and later be requested as a fallback.
|
||||
pub fn mark_as_fallback(&mut self, family: &FontFamily) {
|
||||
if self.has_family(family, false) {
|
||||
self.insert_fallback(format!("{}", family));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn has_family(&self, family: &FontFamily, is_emoji: bool) -> bool {
|
||||
let alias = format!("{}", family);
|
||||
let font_name = if is_emoji {
|
||||
@ -133,7 +257,7 @@ impl FontStore {
|
||||
self.font_provider.family_names().any(|x| x == font_name)
|
||||
}
|
||||
|
||||
pub fn get_fallback(&self) -> &HashSet<String> {
|
||||
pub fn get_fallback(&self) -> &FallbackFonts {
|
||||
&self.fallback_fonts
|
||||
}
|
||||
|
||||
@ -309,4 +433,76 @@ mod tests {
|
||||
|
||||
assert!(css.is_empty());
|
||||
}
|
||||
|
||||
const TEST_FONT: &[u8] = include_bytes!("../fonts/sourcesanspro-regular.ttf");
|
||||
|
||||
fn cjk_fallbacks() -> FallbackFonts {
|
||||
let mut fallbacks = FallbackFonts::default();
|
||||
fallbacks.insert("arabic".to_string(), None);
|
||||
fallbacks.insert("jp".to_string(), Some(CjkFace::Japanese));
|
||||
fallbacks.insert("kr".to_string(), Some(CjkFace::Korean));
|
||||
fallbacks.insert("sc".to_string(), Some(CjkFace::SimplifiedChinese));
|
||||
fallbacks.insert("tc".to_string(), Some(CjkFace::TraditionalChinese));
|
||||
fallbacks.insert("hk".to_string(), Some(CjkFace::HongKongChinese));
|
||||
fallbacks
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cjk_face_comes_from_the_family_region_suffix() {
|
||||
assert_eq!(
|
||||
CjkFace::from_family_name("Noto Sans JP"),
|
||||
Some(CjkFace::Japanese)
|
||||
);
|
||||
assert_eq!(
|
||||
CjkFace::from_family_name("Noto Sans HK"),
|
||||
Some(CjkFace::HongKongChinese)
|
||||
);
|
||||
assert_eq!(CjkFace::from_family_name("Noto Sans Arabic"), None);
|
||||
assert_eq!(CjkFace::from_family_name(""), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paragraph_language_puts_its_cjk_face_first() {
|
||||
let fallbacks = cjk_fallbacks();
|
||||
assert_eq!(fallbacks.for_text("ja", "東京")[0], "jp");
|
||||
assert_eq!(fallbacks.for_text("ko", "漢字")[0], "kr");
|
||||
assert_eq!(fallbacks.for_text("zh-Hans", "东京")[0], "sc");
|
||||
assert_eq!(fallbacks.for_text("zh-Hant-TW", "東京")[..2], ["tc", "hk"]);
|
||||
assert_eq!(fallbacks.for_text("zh-Hant-HK", "東京")[..2], ["hk", "tc"]);
|
||||
assert_eq!(fallbacks.for_text("zh_hant", "東京")[0], "tc");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn untagged_text_prefers_the_face_of_its_kana_or_hangul() {
|
||||
let fallbacks = cjk_fallbacks();
|
||||
assert_eq!(fallbacks.for_text("", "東京へ行く")[0], "jp");
|
||||
assert_eq!(fallbacks.for_text("en", "서울")[0], "kr");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fallback_order_is_stable_without_a_preference() {
|
||||
let fallbacks = cjk_fallbacks();
|
||||
assert_eq!(
|
||||
fallbacks.for_text("", "東京"),
|
||||
["arabic", "sc", "tc", "hk", "jp", "kr"]
|
||||
);
|
||||
// Non-CJK faces follow the preferred face, other CJK faces come last.
|
||||
assert_eq!(
|
||||
fallbacks.for_text("ja", "東京"),
|
||||
["jp", "arabic", "sc", "tc", "hk", "kr"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn uploaded_family_can_be_upgraded_to_fallback() {
|
||||
let mut store = FontStore::try_new().unwrap();
|
||||
let family = FontFamily::new(Uuid::nil(), 400, FontStyle::Normal);
|
||||
let alias = format!("{}", family);
|
||||
|
||||
store.add(family, TEST_FONT, false, false).unwrap();
|
||||
assert!(!store.get_fallback().contains(&alias));
|
||||
|
||||
store.mark_as_fallback(&family);
|
||||
assert!(store.get_fallback().contains(&alias));
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ const DEBUG_VISIBLE: u32 = 0x01;
|
||||
const PROFILE_REBUILD_TILES: u32 = 0x02;
|
||||
const TEXT_EDITOR_V3: u32 = 0x04;
|
||||
const SHOW_WASM_INFO: u32 = 0x08;
|
||||
const TEXT_GRID_VISIBLE: u32 = 0x10;
|
||||
|
||||
// Render performance options
|
||||
// This is the extra area used for tile rendering (tiles beyond viewport).
|
||||
@ -121,4 +122,10 @@ impl RenderOptions {
|
||||
pub fn show_wasm_info(&self) -> bool {
|
||||
self.flags & SHOW_WASM_INFO == SHOW_WASM_INFO
|
||||
}
|
||||
|
||||
/// jlreq-style character-frame grid overlay for Japanese vertical
|
||||
/// text: a developer aid, never exported.
|
||||
pub fn is_text_grid_visible(&self) -> bool {
|
||||
self.flags & TEXT_GRID_VISIBLE == TEXT_GRID_VISIBLE
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
use skia_safe::{self as skia};
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::cell::Cell;
|
||||
use std::collections::HashSet;
|
||||
|
||||
use crate::error::Result;
|
||||
@ -53,6 +54,44 @@ fn svg_page_bounds(shape: &Shape, tree: ShapesPoolRef, scale: f32) -> skia::Rect
|
||||
}
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
/// Set while an SVG document is being written.
|
||||
static WRITING_SVG: Cell<bool> = const { Cell::new(false) };
|
||||
}
|
||||
|
||||
/// True while an SVG export draws. `SkSVGDevice` rebuilds `<text>` from glyph
|
||||
/// IDs through the font's cmap, which loses vertical alternates and maps
|
||||
/// shared glyphs to the wrong code point, so vertical text draws outlines.
|
||||
pub(crate) fn writing_svg() -> bool {
|
||||
WRITING_SVG.with(Cell::get)
|
||||
}
|
||||
|
||||
/// Glyph output for vertical text drawn on the current canvas: outlines
|
||||
/// while an SVG export draws, text blobs otherwise.
|
||||
pub(crate) fn vertical_glyph_output() -> crate::shapes::text_vertical::GlyphOutput {
|
||||
if writing_svg() {
|
||||
crate::shapes::text_vertical::GlyphOutput::Outlines
|
||||
} else {
|
||||
crate::shapes::text_vertical::GlyphOutput::Text
|
||||
}
|
||||
}
|
||||
|
||||
/// Marks the SVG export as running until dropped.
|
||||
struct WritingSvg;
|
||||
|
||||
impl WritingSvg {
|
||||
fn start() -> Self {
|
||||
WRITING_SVG.with(|writing| writing.set(true));
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for WritingSvg {
|
||||
fn drop(&mut self) {
|
||||
WRITING_SVG.with(|writing| writing.set(false));
|
||||
}
|
||||
}
|
||||
|
||||
/// Renders a shape tree to an SVG document and returns the raw SVG bytes.
|
||||
///
|
||||
/// Dedicated vector-SVG render path. Leaf content (paths, fills, …) is emitted
|
||||
@ -98,7 +137,9 @@ pub(crate) fn render_tree_to_svg(
|
||||
let page_h = bounds.height() * scale;
|
||||
let rect = skia::Rect::from_xywh(0., 0., page_w, page_h);
|
||||
|
||||
let writing = WritingSvg::start();
|
||||
let (defs, body) = render_body(shared, id, tree, scale, rect, -bounds.left(), -bounds.top())?;
|
||||
drop(writing);
|
||||
|
||||
let mut aliases = HashSet::new();
|
||||
collect_font_aliases(tree, id, &mut aliases);
|
||||
|
||||
@ -4043,3 +4043,75 @@ fn raster_export_of_masked_group_draws_inner_shadow() {
|
||||
// Outside the circle the mask still clears everything.
|
||||
assert_eq!(raster_pixel(&png, (3, 3)).a(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exports_vertical_text_as_glyph_outlines() {
|
||||
let mut pool = ShapesPool::new();
|
||||
let id = uid(1);
|
||||
add_solid_text(
|
||||
&mut pool,
|
||||
id,
|
||||
(0.0, 0.0, 240.0, 560.0),
|
||||
"HOLA",
|
||||
200.0,
|
||||
skia::Color::from_rgb(0, 63, 255),
|
||||
);
|
||||
let shape = pool.get_mut(&id).expect("text shape");
|
||||
if let crate::shapes::Type::Text(content) = &mut shape.shape_type {
|
||||
for paragraph in content.paragraphs_mut() {
|
||||
paragraph.set_writing_mode(crate::shapes::WritingMode::VerticalRl);
|
||||
}
|
||||
}
|
||||
|
||||
let svg = render(&pool, id);
|
||||
assert!(
|
||||
!svg.contains("<text"),
|
||||
"vertical glyphs must not go through cmap-rebuilt <text>: {svg}"
|
||||
);
|
||||
assert!(
|
||||
svg.contains("<path"),
|
||||
"vertical glyphs export as outlines: {svg}"
|
||||
);
|
||||
assert!(
|
||||
svg.to_ascii_lowercase().contains("fill=\"#003fff\""),
|
||||
"outlines keep the text fill: {svg}"
|
||||
);
|
||||
assert!(
|
||||
!super::writing_svg(),
|
||||
"the export flag resets once the document is written"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exports_the_stroke_of_vertical_text_as_stroked_outlines() {
|
||||
for kind in [StrokeKind::Center, StrokeKind::Inner, StrokeKind::Outer] {
|
||||
let mut pool = ShapesPool::new();
|
||||
let id = uid(1);
|
||||
add_solid_text(
|
||||
&mut pool,
|
||||
id,
|
||||
(0.0, 0.0, 240.0, 560.0),
|
||||
"HOLA",
|
||||
200.0,
|
||||
skia::Color::from_rgb(0, 63, 255),
|
||||
);
|
||||
let shape = pool.get_mut(&id).expect("text shape");
|
||||
if let crate::shapes::Type::Text(content) = &mut shape.shape_type {
|
||||
for paragraph in content.paragraphs_mut() {
|
||||
paragraph.set_writing_mode(crate::shapes::WritingMode::VerticalRl);
|
||||
}
|
||||
}
|
||||
shape.add_stroke(solid_stroke(kind, 4.0, skia::Color::from_rgb(200, 0, 0)));
|
||||
|
||||
let svg = render(&pool, id).to_ascii_lowercase();
|
||||
assert!(
|
||||
svg.contains("stroke=\"#c80000\""),
|
||||
"{kind:?}: the stroke must stay a stroke, not a second fill: {svg}"
|
||||
);
|
||||
assert_eq!(
|
||||
svg.matches("fill=\"#c80000\"").count(),
|
||||
0,
|
||||
"{kind:?}: the stroke colour must not fill the glyphs: {svg}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,7 +4,7 @@ use skia_safe::Canvas;
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::render::text;
|
||||
use crate::shapes::{Fill, ImageFill, Shape, SolidColor, Stroke, StrokeKind, Type};
|
||||
use crate::shapes::{text_vertical, Fill, ImageFill, Shape, SolidColor, Stroke, StrokeKind, Type};
|
||||
use crate::uuid::Uuid;
|
||||
|
||||
use super::document::SvgLayerCanvas;
|
||||
@ -311,6 +311,21 @@ fn paint_text_glyph_silhouette(canvas: &Canvas, shape: &Shape) -> Result<()> {
|
||||
let Type::Text(text_content) = &shape.shape_type else {
|
||||
return Ok(());
|
||||
};
|
||||
// The vertical pass paints span fills; clips and luminance masks need an
|
||||
// opaque black silhouette.
|
||||
let mut silhouette = skia_safe::Paint::default();
|
||||
silhouette.set_color(skia_safe::Color::BLACK);
|
||||
silhouette.set_anti_alias(true);
|
||||
if text_vertical::paint_text_vertical_with(
|
||||
canvas,
|
||||
text_content,
|
||||
&shape.selrect(),
|
||||
shape.vertical_align(),
|
||||
&silhouette,
|
||||
text_vertical::GlyphOutput::Outlines,
|
||||
) {
|
||||
return Ok(());
|
||||
}
|
||||
let text_content = text_content.new_bounds(shape.selrect());
|
||||
let mut mask_builders = text_content.paragraph_builder_group_opaque();
|
||||
text::render_overlay_emoji(canvas, shape, &mut mask_builders, None, None, None, None)?;
|
||||
@ -325,6 +340,21 @@ fn paint_text_stroke_opaque(canvas: &Canvas, shape: &Shape, stroke: &Stroke) ->
|
||||
let Type::Text(text_content) = &shape.shape_type else {
|
||||
return Ok(());
|
||||
};
|
||||
if text_content.is_vertical() {
|
||||
// One cached layout serves every stroke paint.
|
||||
let (stroke_paints, _) = text::get_text_stroke_paints(stroke, &shape.selrect(), false);
|
||||
for paint in &stroke_paints {
|
||||
text_vertical::paint_text_vertical_with(
|
||||
canvas,
|
||||
text_content,
|
||||
&shape.selrect(),
|
||||
shape.vertical_align(),
|
||||
paint,
|
||||
text_vertical::GlyphOutput::Outlines,
|
||||
);
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
let text_content = text_content.new_bounds(shape.selrect());
|
||||
let stroke_blur_outset = Stroke::max_bounds_width(shape.visible_strokes(), false);
|
||||
let (mut stroke_paragraphs, _) = text::stroke_paragraph_builder_group_from_text(
|
||||
|
||||
@ -1,22 +1,163 @@
|
||||
use super::{filters, RenderState, Shape, SurfaceId, DEFAULT_EMOJI_FONT};
|
||||
use crate::shapes::text_paragraph::{HorizontalBuilder, LayoutParagraph};
|
||||
use crate::{
|
||||
error::Result,
|
||||
math::{Point, Rect},
|
||||
shapes::{
|
||||
add_text_with_tabs, calculate_text_layout_data, set_paint_fill, vertical_align_offset,
|
||||
ListMarker, ListStyleType, Paragraph as TextParagraph, ParagraphBuilderGroup,
|
||||
ParagraphLayout, Stroke, StrokeKind, TextContent, TextDecorationSegment, TextDirection,
|
||||
LIST_MARKER_CENTER_FACTOR, LIST_MARKER_GAP_FACTOR, LIST_MARKER_MIN_RADIUS,
|
||||
LIST_MARKER_RADIUS_FACTOR,
|
||||
add_horizontal_span, calculate_text_layout_data, set_paint_fill, text_vertical,
|
||||
vertical_align_offset, HorizontalOffsets, HorizontalSpanRange, ListMarker, ListStyleType,
|
||||
Paragraph as TextParagraph, ParagraphBuilderGroup, ParagraphLayout, Stroke, StrokeKind,
|
||||
TextContent, TextDecorationSegment, TextDirection, LIST_MARKER_CENTER_FACTOR,
|
||||
LIST_MARKER_GAP_FACTOR, LIST_MARKER_MIN_RADIUS, LIST_MARKER_RADIUS_FACTOR,
|
||||
},
|
||||
utils::{get_fallback_fonts, get_font_collection},
|
||||
};
|
||||
use skia_safe::{
|
||||
self as skia,
|
||||
canvas::SaveLayerRec,
|
||||
textlayout::{ParagraphBuilder, StyleMetrics, TextDecoration},
|
||||
textlayout::{RectHeightStyle, RectWidthStyle, StyleMetrics, TextDecoration},
|
||||
Canvas, ImageFilter, Paint,
|
||||
};
|
||||
use std::borrow::Cow;
|
||||
use text_vertical::GlyphOutput;
|
||||
|
||||
/// Vertical text: shadows, fill, strokes and the debug grid, all painted from
|
||||
/// one layout. Rebinds the content to the selrect: stored text bounds
|
||||
/// describe the measured content and can be taller than a fixed shape, whose
|
||||
/// height is the column-wrap budget.
|
||||
pub fn render_vertical_text(
|
||||
state: &mut RenderState,
|
||||
shape: &Shape,
|
||||
text_content: &TextContent,
|
||||
fills_surface_id: SurfaceId,
|
||||
strokes_surface_id: SurfaceId,
|
||||
skip_effects: bool,
|
||||
) -> Result<()> {
|
||||
let blur_filter = (!skip_effects).then(|| shape.image_filter(1.0)).flatten();
|
||||
let bounds = shape.selrect();
|
||||
let vertical_align = shape.vertical_align();
|
||||
let layout = text_content.vertical_layout(&bounds);
|
||||
|
||||
let skip_shadows = skip_effects || state.should_skip_drop_shadows();
|
||||
if !skip_shadows {
|
||||
let mut drop_shadows = shape.drop_shadow_paints();
|
||||
if let Some(inherited_shadows) = state.get_inherited_drop_shadows() {
|
||||
drop_shadows.extend(inherited_shadows);
|
||||
}
|
||||
if !drop_shadows.is_empty() {
|
||||
let canvas = state.surfaces.canvas_and_mark_dirty(fills_surface_id);
|
||||
for shadow in &drop_shadows {
|
||||
text_vertical::paint_drop_shadow(
|
||||
canvas,
|
||||
&layout,
|
||||
&bounds,
|
||||
vertical_align,
|
||||
shadow,
|
||||
GlyphOutput::Text,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
render_to_surface(
|
||||
state,
|
||||
shape,
|
||||
fills_surface_id,
|
||||
blur_filter.as_ref(),
|
||||
0.0,
|
||||
|canvas| {
|
||||
paint_vertical_fill(
|
||||
canvas,
|
||||
shape,
|
||||
&layout,
|
||||
&bounds,
|
||||
blur_filter.as_ref(),
|
||||
GlyphOutput::Text,
|
||||
)
|
||||
},
|
||||
)?;
|
||||
|
||||
let strokes: Vec<&Stroke> = shape.visible_strokes().rev().collect();
|
||||
if !strokes.is_empty() {
|
||||
let selrect = shape.selrect();
|
||||
let canvas = state.surfaces.canvas_and_mark_dirty(strokes_surface_id);
|
||||
for stroke in strokes {
|
||||
text_vertical::paint_stroke(
|
||||
canvas,
|
||||
&layout,
|
||||
&bounds,
|
||||
vertical_align,
|
||||
stroke,
|
||||
&selrect,
|
||||
blur_filter.as_ref(),
|
||||
GlyphOutput::Text,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if state.options.is_text_grid_visible() {
|
||||
let canvas = state.surfaces.canvas_and_mark_dirty(fills_surface_id);
|
||||
text_vertical::paint_grid(canvas, &layout, &bounds, vertical_align);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Paint a viewport-only debug grid over SkParagraph horizontal text: blue
|
||||
/// line boxes, green per-scalar tight rects from SkParagraph, amber
|
||||
/// baselines. Matches the vertical text grid and shows horizontal annotation
|
||||
/// anchors.
|
||||
pub fn paint_horizontal_grid(canvas: &Canvas, shape: &Shape, text_content: &TextContent) {
|
||||
let mut builders = text_content.paragraph_builder_group_from_text(None);
|
||||
let layout = calculate_text_layout_data(shape, text_content, &mut builders, true);
|
||||
|
||||
let mut line_paint = Paint::default();
|
||||
line_paint.set_anti_alias(true);
|
||||
line_paint.set_style(skia::PaintStyle::Stroke);
|
||||
line_paint.set_stroke_width(1.0);
|
||||
line_paint.set_color(skia::Color::from_argb(0xAA, 0x2F, 0x80, 0xED));
|
||||
|
||||
let mut glyph_paint = Paint::default();
|
||||
glyph_paint.set_anti_alias(true);
|
||||
glyph_paint.set_style(skia::PaintStyle::Stroke);
|
||||
glyph_paint.set_stroke_width(1.0);
|
||||
glyph_paint.set_color(skia::Color::from_argb(0x99, 0x27, 0xAE, 0x60));
|
||||
|
||||
let mut baseline_paint = Paint::default();
|
||||
baseline_paint.set_anti_alias(true);
|
||||
baseline_paint.set_stroke_width(1.0);
|
||||
baseline_paint.set_color(skia::Color::from_argb(0xCC, 0xEB, 0x57, 0x57));
|
||||
|
||||
for paragraph in &layout.paragraphs {
|
||||
for line in paragraph.paragraph.get_line_metrics() {
|
||||
let baseline = line.baseline as f32;
|
||||
let top = paragraph.y + baseline - line.ascent as f32;
|
||||
let left = paragraph.x + line.left as f32;
|
||||
let width = line.width as f32;
|
||||
canvas.draw_rect(
|
||||
Rect::from_xywh(left, top, width, line.height as f32),
|
||||
&line_paint,
|
||||
);
|
||||
canvas.draw_line(
|
||||
(left, paragraph.y + baseline),
|
||||
(left + width, paragraph.y + baseline),
|
||||
&baseline_paint,
|
||||
);
|
||||
|
||||
for offset in line.start_index..line.end_index {
|
||||
for textbox in paragraph.paragraph.get_rects_for_range(
|
||||
offset..offset + 1,
|
||||
RectHeightStyle::Tight,
|
||||
RectWidthStyle::Tight,
|
||||
) {
|
||||
let mut rect = textbox.rect;
|
||||
rect.offset((paragraph.x, paragraph.y));
|
||||
canvas.draw_rect(rect, &glyph_paint);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stroke_paragraph_builder_group_from_text(
|
||||
text_content: &TextContent,
|
||||
@ -30,11 +171,42 @@ pub fn stroke_paragraph_builder_group_from_text(
|
||||
let remove_stroke_alpha = use_shadow.unwrap_or(false) && !stroke.is_transparent();
|
||||
let mut group_layer_opacity: Option<f32> = None;
|
||||
|
||||
for paragraph in text_content.paragraphs() {
|
||||
let mut stroke_paragraphs_map: std::collections::HashMap<usize, ParagraphBuilder> =
|
||||
let plans = text_content.horizontal_plans();
|
||||
let adjustments = text_content.horizontal_line_adjustments();
|
||||
for (index, (paragraph, plan)) in text_content
|
||||
.paragraphs()
|
||||
.iter()
|
||||
.zip(plans.iter())
|
||||
.enumerate()
|
||||
{
|
||||
let adjustment = adjustments
|
||||
.as_ref()
|
||||
.and_then(|adjustments| adjustments[index].as_ref());
|
||||
let mut stroke_paragraphs_map: std::collections::HashMap<usize, HorizontalBuilder> =
|
||||
std::collections::HashMap::new();
|
||||
|
||||
for span in paragraph.children().iter() {
|
||||
for (span_index, (((span, text), sheds), extra)) in paragraph
|
||||
.children()
|
||||
.iter()
|
||||
.zip(&plan.texts)
|
||||
.zip(&plan.pair_spacing)
|
||||
.zip(&plan.ruby_spacing.adjustments)
|
||||
.enumerate()
|
||||
{
|
||||
let sheds: Cow<'_, [(usize, f32)]> = adjustment
|
||||
.map_or(Cow::Borrowed(sheds.as_slice()), |a| {
|
||||
Cow::Owned(sheds.iter().chain(&a.sheds[span_index]).copied().collect())
|
||||
});
|
||||
let extra: Cow<'_, [(usize, f32)]> =
|
||||
adjustment.map_or(Cow::Borrowed(extra.as_slice()), |a| {
|
||||
Cow::Owned(
|
||||
extra
|
||||
.iter()
|
||||
.chain(&a.spacing[span_index])
|
||||
.copied()
|
||||
.collect(),
|
||||
)
|
||||
});
|
||||
let (stroke_paints, stroke_layer_opacity) =
|
||||
get_text_stroke_paints(stroke, bounds, remove_stroke_alpha);
|
||||
|
||||
@ -42,12 +214,20 @@ pub fn stroke_paragraph_builder_group_from_text(
|
||||
group_layer_opacity = stroke_layer_opacity;
|
||||
}
|
||||
|
||||
let text: String = span.apply_text_transform();
|
||||
|
||||
for (paint_idx, stroke_paint) in stroke_paints.iter().enumerate() {
|
||||
let builder = stroke_paragraphs_map.entry(paint_idx).or_insert_with(|| {
|
||||
let paragraph_style = paragraph.paragraph_to_style();
|
||||
ParagraphBuilder::new(¶graph_style, fonts)
|
||||
let mut paragraph_style = paragraph.paragraph_to_style();
|
||||
if plan.has_atomic_overflow() || adjustment.is_some() {
|
||||
paragraph_style.set_apply_rounding_hack(false);
|
||||
if matches!(
|
||||
paragraph.text_align(),
|
||||
crate::shapes::TextAlign::Justify
|
||||
| crate::shapes::TextAlign::Distribute
|
||||
) {
|
||||
paragraph_style.set_text_align(skia::textlayout::TextAlign::Left);
|
||||
}
|
||||
}
|
||||
HorizontalBuilder::new(¶graph_style, fonts)
|
||||
});
|
||||
let stroke_paint = stroke_paint.clone();
|
||||
let remove_alpha = use_shadow.unwrap_or(false) && !span.is_transparent();
|
||||
@ -58,11 +238,11 @@ pub fn stroke_paragraph_builder_group_from_text(
|
||||
paragraph.line_height(),
|
||||
);
|
||||
builder.push_style(&stroke_style);
|
||||
add_text_with_tabs(builder, &text, span.font_size);
|
||||
add_horizontal_span(builder, span, text, &sheds, &extra, &stroke_style, fonts);
|
||||
}
|
||||
}
|
||||
|
||||
let stroke_paragraphs: Vec<ParagraphBuilder> = (0..stroke_paragraphs_map.len())
|
||||
let stroke_paragraphs: ParagraphBuilderGroup = (0..stroke_paragraphs_map.len())
|
||||
.filter_map(|i| stroke_paragraphs_map.remove(&i))
|
||||
.collect();
|
||||
|
||||
@ -72,7 +252,7 @@ pub fn stroke_paragraph_builder_group_from_text(
|
||||
(paragraph_group, group_layer_opacity)
|
||||
}
|
||||
|
||||
fn get_text_stroke_paints(
|
||||
pub(crate) fn get_text_stroke_paints(
|
||||
stroke: &Stroke,
|
||||
bounds: &Rect,
|
||||
remove_stroke_alpha: bool,
|
||||
@ -141,7 +321,7 @@ pub fn render_with_bounds_outset(
|
||||
render_state: Option<&mut RenderState>,
|
||||
canvas: Option<&Canvas>,
|
||||
shape: &Shape,
|
||||
paragraph_builders: &mut [Vec<ParagraphBuilder>],
|
||||
paragraph_builders: &mut [ParagraphBuilderGroup],
|
||||
surface_id: Option<SurfaceId>,
|
||||
shadow: Option<&Paint>,
|
||||
blur: Option<&ImageFilter>,
|
||||
@ -169,7 +349,7 @@ pub fn render_with_bounds_outset(
|
||||
pub fn render_with_bounds_outset_overlay_emoji(
|
||||
canvas: &Canvas,
|
||||
shape: &Shape,
|
||||
paragraph_builders: &mut [Vec<ParagraphBuilder>],
|
||||
paragraph_builders: &mut [ParagraphBuilderGroup],
|
||||
shadow: Option<&Paint>,
|
||||
blur: Option<&ImageFilter>,
|
||||
stroke_bounds_outset: f32,
|
||||
@ -191,12 +371,77 @@ pub fn render_with_bounds_outset_overlay_emoji(
|
||||
)
|
||||
}
|
||||
|
||||
/// Run `paint` on `target_surface`. With a blur, it paints into a filter
|
||||
/// surface sized to the blurred text bounds (grown by `stroke_bounds_outset`)
|
||||
/// when those bounds are usable.
|
||||
fn render_to_surface(
|
||||
render_state: &mut RenderState,
|
||||
shape: &Shape,
|
||||
target_surface: SurfaceId,
|
||||
blur: Option<&ImageFilter>,
|
||||
stroke_bounds_outset: f32,
|
||||
mut paint: impl FnMut(&Canvas),
|
||||
) -> Result<()> {
|
||||
if let Some(blur_filter) = blur {
|
||||
let mut text_bounds = shape
|
||||
.get_text_content()
|
||||
.calculate_bounds(shape, false)
|
||||
.to_rect();
|
||||
if stroke_bounds_outset > 0.0 {
|
||||
text_bounds.inset((-stroke_bounds_outset, -stroke_bounds_outset));
|
||||
}
|
||||
let bounds = blur_filter.compute_fast_bounds(text_bounds);
|
||||
if bounds.is_finite()
|
||||
&& bounds.width() > 0.0
|
||||
&& bounds.height() > 0.0
|
||||
&& filters::render_with_filter_surface(
|
||||
render_state,
|
||||
bounds,
|
||||
target_surface,
|
||||
|state, temp_surface| {
|
||||
paint(state.surfaces.canvas(temp_surface));
|
||||
Ok(())
|
||||
},
|
||||
)?
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
paint(render_state.surfaces.canvas_and_mark_dirty(target_surface));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Fill pass of vertical text: `layout` painted inside the shape's layer
|
||||
/// blur.
|
||||
pub fn paint_vertical_fill(
|
||||
canvas: &Canvas,
|
||||
shape: &Shape,
|
||||
layout: &text_vertical::VerticalLayout,
|
||||
bounds: &Rect,
|
||||
blur: Option<&ImageFilter>,
|
||||
output: GlyphOutput,
|
||||
) {
|
||||
if let Some(blur_filter) = blur {
|
||||
let mut blur_paint = Paint::default();
|
||||
blur_paint.set_image_filter(blur_filter.clone());
|
||||
canvas.save_layer(
|
||||
&SaveLayerRec::default()
|
||||
.bounds(&shape.layer_bounds())
|
||||
.paint(&blur_paint),
|
||||
);
|
||||
}
|
||||
text_vertical::paint_layout(canvas, layout, bounds, shape.vertical_align(), output);
|
||||
if blur.is_some() {
|
||||
canvas.restore();
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn render_with_bounds_outset_inner(
|
||||
render_state: Option<&mut RenderState>,
|
||||
canvas: Option<&Canvas>,
|
||||
shape: &Shape,
|
||||
paragraph_builders: &mut [Vec<ParagraphBuilder>],
|
||||
paragraph_builders: &mut [ParagraphBuilderGroup],
|
||||
surface_id: Option<SurfaceId>,
|
||||
shadow: Option<&Paint>,
|
||||
blur: Option<&ImageFilter>,
|
||||
@ -206,55 +451,25 @@ fn render_with_bounds_outset_inner(
|
||||
overlay_emoji: bool,
|
||||
) -> Result<()> {
|
||||
if let Some(render_state) = render_state {
|
||||
let target_surface = surface_id.unwrap_or(SurfaceId::Fills);
|
||||
|
||||
if let Some(blur_filter) = blur {
|
||||
let mut text_bounds = shape
|
||||
.get_text_content()
|
||||
.calculate_bounds(shape, false)
|
||||
.to_rect();
|
||||
if stroke_bounds_outset > 0.0 {
|
||||
text_bounds.inset((-stroke_bounds_outset, -stroke_bounds_outset));
|
||||
}
|
||||
let bounds = blur_filter.compute_fast_bounds(text_bounds);
|
||||
if bounds.is_finite() && bounds.width() > 0.0 && bounds.height() > 0.0 {
|
||||
let blur_filter_clone = blur_filter.clone();
|
||||
if filters::render_with_filter_surface(
|
||||
render_state,
|
||||
bounds,
|
||||
target_surface,
|
||||
|state, temp_surface| {
|
||||
let temp_canvas = state.surfaces.canvas(temp_surface);
|
||||
render_text_on_canvas(
|
||||
temp_canvas,
|
||||
shape,
|
||||
paragraph_builders,
|
||||
shadow,
|
||||
Some(&blur_filter_clone),
|
||||
fill_inset,
|
||||
layer_opacity,
|
||||
false,
|
||||
);
|
||||
Ok(())
|
||||
},
|
||||
)? {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let canvas = render_state.surfaces.canvas_and_mark_dirty(target_surface);
|
||||
render_text_on_canvas(
|
||||
canvas,
|
||||
return render_to_surface(
|
||||
render_state,
|
||||
shape,
|
||||
paragraph_builders,
|
||||
shadow,
|
||||
surface_id.unwrap_or(SurfaceId::Fills),
|
||||
blur,
|
||||
fill_inset,
|
||||
layer_opacity,
|
||||
false,
|
||||
stroke_bounds_outset,
|
||||
|canvas| {
|
||||
render_text_on_canvas(
|
||||
canvas,
|
||||
shape,
|
||||
paragraph_builders,
|
||||
shadow,
|
||||
blur,
|
||||
fill_inset,
|
||||
layer_opacity,
|
||||
false,
|
||||
)
|
||||
},
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Some(canvas) = canvas {
|
||||
@ -277,7 +492,7 @@ pub fn render(
|
||||
render_state: Option<&mut RenderState>,
|
||||
canvas: Option<&Canvas>,
|
||||
shape: &Shape,
|
||||
paragraph_builders: &mut [Vec<ParagraphBuilder>],
|
||||
paragraph_builders: &mut [ParagraphBuilderGroup],
|
||||
surface_id: Option<SurfaceId>,
|
||||
shadow: Option<&Paint>,
|
||||
blur: Option<&ImageFilter>,
|
||||
@ -303,7 +518,7 @@ pub fn render(
|
||||
pub fn render_overlay_emoji(
|
||||
canvas: &Canvas,
|
||||
shape: &Shape,
|
||||
paragraph_builders: &mut [Vec<ParagraphBuilder>],
|
||||
paragraph_builders: &mut [ParagraphBuilderGroup],
|
||||
shadow: Option<&Paint>,
|
||||
blur: Option<&ImageFilter>,
|
||||
fill_inset: Option<f32>,
|
||||
@ -334,6 +549,9 @@ pub fn try_paint_from_layout_cache(
|
||||
layout_cache_rotation_only: bool,
|
||||
) -> Result<bool> {
|
||||
let text_content = shape.get_text_content();
|
||||
if !text_content.can_paint_from_layout_cache() {
|
||||
return Ok(false);
|
||||
}
|
||||
let cache_usable = if layout_cache_rotation_only {
|
||||
text_content.layout_cache_versions_match()
|
||||
} else {
|
||||
@ -358,6 +576,34 @@ pub fn try_paint_from_layout_cache(
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
/// Paint the warichu, emphasis marks and ruby of the laid-out horizontal
|
||||
/// paragraph `index` at `origin`, with the paint of this pass.
|
||||
fn paint_horizontal_annotations(
|
||||
canvas: &Canvas,
|
||||
text_content: &TextContent,
|
||||
index: usize,
|
||||
laid_out: &LayoutParagraph,
|
||||
origin: (f32, f32),
|
||||
) {
|
||||
paint_horizontal_notes_and_marks(canvas, text_content, index, laid_out, origin);
|
||||
crate::shapes::paint_horizontal_ruby(canvas, text_content, index, laid_out, origin.0, origin.1);
|
||||
}
|
||||
|
||||
/// The warichu and emphasis marks of `paint_horizontal_annotations`.
|
||||
fn paint_horizontal_notes_and_marks(
|
||||
canvas: &Canvas,
|
||||
text_content: &TextContent,
|
||||
index: usize,
|
||||
laid_out: &LayoutParagraph,
|
||||
(x, y): (f32, f32),
|
||||
) {
|
||||
let plans = text_content.horizontal_plans();
|
||||
if let (Some(paragraph), Some(plan)) = (text_content.paragraphs().get(index), plans.get(index))
|
||||
{
|
||||
crate::shapes::paint_horizontal_warichu(canvas, paragraph, plan, laid_out, x, y);
|
||||
crate::shapes::paint_horizontal_emphasis(canvas, paragraph, plan, laid_out, x, y);
|
||||
}
|
||||
}
|
||||
fn paint_from_cached_layout(canvas: &Canvas, shape: &Shape, text_content: &TextContent) {
|
||||
let selrect = shape.selrect();
|
||||
// Absolute image/gradient shaders were baked at `layout_paint_origin`. Paint
|
||||
@ -385,16 +631,26 @@ fn paint_from_cached_layout(canvas: &Canvas, shape: &Shape, text_content: &TextC
|
||||
let vertical_offset =
|
||||
vertical_align_offset(selrect.height(), total_text_height, shape.vertical_align());
|
||||
|
||||
let plans = text_content.horizontal_plans();
|
||||
let top = base_y + vertical_offset;
|
||||
let mut y_accum = top;
|
||||
for (index, group) in paragraphs.iter().enumerate() {
|
||||
let Some(paragraph) = group.first() else {
|
||||
continue;
|
||||
};
|
||||
let x = x + plans
|
||||
.get(index)
|
||||
.zip(text_content.paragraphs().get(index))
|
||||
.map_or(0.0, |(plan, model)| plan.x_offset(model, selrect.width()));
|
||||
paragraph.paint(canvas, (x, y_accum));
|
||||
if draw_decorations {
|
||||
if let Some(text_paragraph) = text_content.paragraphs().get(index) {
|
||||
for deco in decoration_segments(paragraph, text_paragraph, x, y_accum) {
|
||||
paint_horizontal_annotations(canvas, text_content, index, paragraph, (x, y_accum));
|
||||
if let (Some(text_paragraph), Some(plan)) =
|
||||
(text_content.paragraphs().get(index), plans.get(index))
|
||||
{
|
||||
if draw_decorations {
|
||||
for deco in
|
||||
decoration_segments(paragraph, text_paragraph, &plan.offsets, x, y_accum)
|
||||
{
|
||||
draw_decoration_segment(canvas, &deco);
|
||||
}
|
||||
}
|
||||
@ -421,7 +677,7 @@ fn paint_from_cached_layout(canvas: &Canvas, shape: &Shape, text_content: &TextC
|
||||
fn render_text_on_canvas(
|
||||
canvas: &Canvas,
|
||||
shape: &Shape,
|
||||
paragraph_builders: &mut [Vec<ParagraphBuilder>],
|
||||
paragraph_builders: &mut [ParagraphBuilderGroup],
|
||||
shadow: Option<&Paint>,
|
||||
blur: Option<&ImageFilter>,
|
||||
fill_inset: Option<f32>,
|
||||
@ -498,7 +754,7 @@ fn render_text_on_canvas(
|
||||
pub fn paint_text_paragraphs(
|
||||
canvas: &Canvas,
|
||||
shape: &Shape,
|
||||
paragraph_builder_groups: &mut [Vec<ParagraphBuilder>],
|
||||
paragraph_builder_groups: &mut [ParagraphBuilderGroup],
|
||||
) {
|
||||
paint_text_with_emoji_overlay(canvas, shape, paragraph_builder_groups, false);
|
||||
}
|
||||
@ -507,7 +763,7 @@ pub fn paint_text_paragraphs(
|
||||
fn paint_text(
|
||||
canvas: &Canvas,
|
||||
shape: &Shape,
|
||||
paragraph_builder_groups: &mut [Vec<ParagraphBuilder>],
|
||||
paragraph_builder_groups: &mut [ParagraphBuilderGroup],
|
||||
) {
|
||||
paint_text_with_emoji_overlay(canvas, shape, paragraph_builder_groups, false);
|
||||
}
|
||||
@ -533,23 +789,47 @@ pub fn paint_text_mask(canvas: &Canvas, shape: &Shape) {
|
||||
fn paint_text_with_emoji_overlay(
|
||||
canvas: &Canvas,
|
||||
shape: &Shape,
|
||||
paragraph_builder_groups: &mut [Vec<ParagraphBuilder>],
|
||||
paragraph_builder_groups: &mut [ParagraphBuilderGroup],
|
||||
overlay_emoji: bool,
|
||||
) {
|
||||
let text_content = shape.get_text_content();
|
||||
|
||||
// Vertical writing paints through the vertical pass. Stored text bounds
|
||||
// describe the measured content and can be taller than a fixed shape, so
|
||||
// the selrect is the column-wrap budget.
|
||||
if crate::shapes::text_vertical::paint_text_vertical(
|
||||
canvas,
|
||||
text_content,
|
||||
&shape.selrect(),
|
||||
shape.vertical_align(),
|
||||
crate::render::svg::vertical_glyph_output(),
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut layout_info =
|
||||
calculate_text_layout_data(shape, text_content, paragraph_builder_groups, true);
|
||||
|
||||
for para in &mut layout_info.paragraphs {
|
||||
para.paragraph.paint(canvas, (para.x, para.y));
|
||||
|
||||
let (index, origin) = (para.source_paragraph, (para.x, para.y));
|
||||
paint_horizontal_notes_and_marks(canvas, text_content, index, ¶.paragraph, origin);
|
||||
// The emoji overlay goes over the marks and under the ruby, which
|
||||
// takes the paint of this pass like warichu and emphasis.
|
||||
if overlay_emoji {
|
||||
paint_emoji_overlay(canvas, para);
|
||||
}
|
||||
|
||||
if let Some(marker) = ¶.marker {
|
||||
draw_list_marker(canvas, marker);
|
||||
}
|
||||
crate::shapes::paint_horizontal_ruby(
|
||||
canvas,
|
||||
text_content,
|
||||
index,
|
||||
¶.paragraph,
|
||||
origin.0,
|
||||
origin.1,
|
||||
);
|
||||
|
||||
for deco in ¶.decorations {
|
||||
draw_decoration_segment(canvas, deco);
|
||||
@ -557,7 +837,7 @@ fn paint_text_with_emoji_overlay(
|
||||
}
|
||||
}
|
||||
|
||||
fn paragraph_has_emoji(paragraph: &mut skia::textlayout::Paragraph) -> bool {
|
||||
fn paragraph_has_emoji(paragraph: &mut LayoutParagraph) -> bool {
|
||||
let line_bounds: Vec<(usize, usize)> = paragraph
|
||||
.get_line_metrics()
|
||||
.iter()
|
||||
@ -569,7 +849,7 @@ fn paragraph_has_emoji(paragraph: &mut skia::textlayout::Paragraph) -> bool {
|
||||
}
|
||||
|
||||
fn line_emoji_ranges(
|
||||
paragraph: &mut skia::textlayout::Paragraph,
|
||||
paragraph: &mut LayoutParagraph,
|
||||
line_start: usize,
|
||||
line_end: usize,
|
||||
) -> Vec<(usize, usize)> {
|
||||
@ -783,8 +1063,8 @@ fn paint_emoji_opaque(
|
||||
pub fn render_emoji_overlay(
|
||||
render_state: &mut RenderState,
|
||||
shape: &Shape,
|
||||
emoji_builders: &mut [Vec<ParagraphBuilder>],
|
||||
deco_builders: &mut [Vec<ParagraphBuilder>],
|
||||
emoji_builders: &mut [ParagraphBuilderGroup],
|
||||
deco_builders: &mut [ParagraphBuilderGroup],
|
||||
surface_id: SurfaceId,
|
||||
blur: Option<&ImageFilter>,
|
||||
) {
|
||||
@ -841,7 +1121,7 @@ pub fn render_emoji_overlay(
|
||||
fn draw_text(
|
||||
canvas: &Canvas,
|
||||
shape: &Shape,
|
||||
paragraph_builder_groups: &mut [Vec<ParagraphBuilder>],
|
||||
paragraph_builder_groups: &mut [ParagraphBuilderGroup],
|
||||
layer_opacity: Option<f32>,
|
||||
overlay_emoji: bool,
|
||||
) {
|
||||
@ -875,9 +1155,9 @@ fn draw_text(
|
||||
fn render_masked_stroke_on_canvas(
|
||||
canvas: &Canvas,
|
||||
shape: &Shape,
|
||||
mask_builders: &mut [Vec<ParagraphBuilder>],
|
||||
stroke_builders: &mut [Vec<ParagraphBuilder>],
|
||||
fill_builders: Option<&mut [Vec<ParagraphBuilder>]>,
|
||||
mask_builders: &mut [ParagraphBuilderGroup],
|
||||
stroke_builders: &mut [ParagraphBuilderGroup],
|
||||
fill_builders: Option<&mut [ParagraphBuilderGroup]>,
|
||||
stroke_mask_blend: skia::BlendMode,
|
||||
blur: Option<&ImageFilter>,
|
||||
layer_opacity: Option<f32>,
|
||||
@ -955,9 +1235,9 @@ fn render_masked_stroke(
|
||||
render_state: Option<&mut RenderState>,
|
||||
canvas: Option<&Canvas>,
|
||||
shape: &Shape,
|
||||
mask_builders: &mut [Vec<ParagraphBuilder>],
|
||||
stroke_builders: &mut [Vec<ParagraphBuilder>],
|
||||
mut fill_builders: Option<&mut [Vec<ParagraphBuilder>]>,
|
||||
mask_builders: &mut [ParagraphBuilderGroup],
|
||||
stroke_builders: &mut [ParagraphBuilderGroup],
|
||||
mut fill_builders: Option<&mut [ParagraphBuilderGroup]>,
|
||||
stroke_mask_blend: skia::BlendMode,
|
||||
surface_id: Option<SurfaceId>,
|
||||
blur: Option<&ImageFilter>,
|
||||
@ -1037,8 +1317,8 @@ pub fn render_inner_stroke(
|
||||
render_state: Option<&mut RenderState>,
|
||||
canvas: Option<&Canvas>,
|
||||
shape: &Shape,
|
||||
stroke_builders: &mut [Vec<ParagraphBuilder>],
|
||||
fill_builders: &mut [Vec<ParagraphBuilder>],
|
||||
stroke_builders: &mut [ParagraphBuilderGroup],
|
||||
fill_builders: &mut [ParagraphBuilderGroup],
|
||||
surface_id: Option<SurfaceId>,
|
||||
blur: Option<&ImageFilter>,
|
||||
stroke_bounds_outset: f32,
|
||||
@ -1065,7 +1345,7 @@ pub fn render_outer_stroke(
|
||||
render_state: Option<&mut RenderState>,
|
||||
canvas: Option<&Canvas>,
|
||||
shape: &Shape,
|
||||
stroke_builders: &mut [Vec<ParagraphBuilder>],
|
||||
stroke_builders: &mut [ParagraphBuilderGroup],
|
||||
surface_id: Option<SurfaceId>,
|
||||
blur: Option<&ImageFilter>,
|
||||
stroke_bounds_outset: f32,
|
||||
@ -1095,7 +1375,7 @@ fn draw_list_marker(canvas: &Canvas, marker: &ListMarker) {
|
||||
/// stands for a paragraph with no laid-out Skia paragraph; it keeps the
|
||||
/// paragraphs in step with `text_paragraphs`.
|
||||
pub fn list_markers<'a>(
|
||||
skia_paragraphs: impl IntoIterator<Item = Option<&'a skia::textlayout::Paragraph>>,
|
||||
skia_paragraphs: impl IntoIterator<Item = Option<&'a LayoutParagraph>>,
|
||||
text_paragraphs: &[TextParagraph],
|
||||
x: f32,
|
||||
top: f32,
|
||||
@ -1129,7 +1409,7 @@ pub fn list_markers<'a>(
|
||||
/// An empty paragraph has no style run, so it takes the paint of `previous`
|
||||
/// (the last list item painted in the same pass) and its own span's size.
|
||||
pub fn list_marker(
|
||||
skia_paragraph: &skia::textlayout::Paragraph,
|
||||
skia_paragraph: &LayoutParagraph,
|
||||
text_paragraph: &TextParagraph,
|
||||
x: f32,
|
||||
y_accum: f32,
|
||||
@ -1177,38 +1457,62 @@ fn draw_decoration_segment(canvas: &Canvas, deco: &TextDecorationSegment) {
|
||||
canvas.draw_rect(deco.rect(), &decoration_paint);
|
||||
}
|
||||
|
||||
/// One decorated span clipped to a line: UTF-16 range, decoration and the
|
||||
/// Skia style run it falls in (paint + font metrics).
|
||||
type LineDecoration<'a> = (usize, usize, TextDecoration, &'a StyleMetrics<'a>);
|
||||
/// One decorated span clipped to a line: UTF-16 range, decoration, the span's
|
||||
/// offsets and the Skia style run it falls in (paint + font metrics).
|
||||
type LineDecoration<'a> = (
|
||||
usize,
|
||||
usize,
|
||||
TextDecoration,
|
||||
&'a HorizontalSpanRange,
|
||||
&'a StyleMetrics<'a>,
|
||||
);
|
||||
|
||||
/// UTF-16 ranges of the spans that ask for a decoration we draw.
|
||||
fn decorated_span_ranges(text_paragraph: &TextParagraph) -> Vec<(usize, usize, TextDecoration)> {
|
||||
let mut ranges = Vec::new();
|
||||
let mut offset = 0;
|
||||
for span in text_paragraph.children() {
|
||||
let len = span.apply_text_transform().encode_utf16().count();
|
||||
match span.text_decoration {
|
||||
/// Builder-text range of every underlined or struck span. Warichu spans
|
||||
/// collapse to a placeholder and get no bar.
|
||||
fn decorated_span_ranges(
|
||||
text_paragraph: &TextParagraph,
|
||||
offsets: &HorizontalOffsets,
|
||||
) -> Vec<(usize, usize, TextDecoration)> {
|
||||
text_paragraph
|
||||
.children()
|
||||
.iter()
|
||||
.zip(&offsets.ranges)
|
||||
.filter_map(|(span, range)| match span.text_decoration {
|
||||
Some(kind)
|
||||
if kind == TextDecoration::UNDERLINE || kind == TextDecoration::LINE_THROUGH =>
|
||||
if !range.warichu
|
||||
&& (kind == TextDecoration::UNDERLINE
|
||||
|| kind == TextDecoration::LINE_THROUGH) =>
|
||||
{
|
||||
ranges.push((offset, offset + len, kind))
|
||||
Some((range.builder_start, range.builder_end, kind))
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
offset += len;
|
||||
}
|
||||
ranges
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Style run covering `offset`; runs are keyed by their start index.
|
||||
/// Builder-text range of the span holding UTF-16 `offset`.
|
||||
fn span_range_at(offsets: &HorizontalOffsets, offset: usize) -> Option<&HorizontalSpanRange> {
|
||||
let index = offsets
|
||||
.ranges
|
||||
.partition_point(|range| range.builder_start <= offset)
|
||||
.saturating_sub(1);
|
||||
offsets.ranges.get(index)
|
||||
}
|
||||
|
||||
/// Style run of a line covering the UTF-8 `byte_anchor`. Runs are keyed by
|
||||
/// their UTF-8 start, unlike the UTF-16 line indices. An anchor before the
|
||||
/// first run belongs to a span continued from an earlier line, which the
|
||||
/// line's first run carries on.
|
||||
fn style_metric_at<'a>(
|
||||
style_metrics: &[(usize, &'a StyleMetrics<'a>)],
|
||||
offset: usize,
|
||||
byte_anchor: usize,
|
||||
) -> Option<&'a StyleMetrics<'a>> {
|
||||
style_metrics
|
||||
.iter()
|
||||
.rev()
|
||||
.find(|(start, _)| *start <= offset)
|
||||
.find(|(start, _)| *start <= byte_anchor)
|
||||
.or_else(|| style_metrics.first())
|
||||
.map(|(_, metrics)| *metrics)
|
||||
}
|
||||
|
||||
@ -1218,22 +1522,20 @@ fn style_metric_at<'a>(
|
||||
/// how Skia grouped the line into style runs; the runs only supply the paint
|
||||
/// and font metrics covering each segment.
|
||||
pub fn decoration_segments(
|
||||
skia_paragraph: &skia::textlayout::Paragraph,
|
||||
skia_paragraph: &LayoutParagraph,
|
||||
text_paragraph: &TextParagraph,
|
||||
offsets: &HorizontalOffsets,
|
||||
x: f32,
|
||||
y_accum: f32,
|
||||
) -> Vec<TextDecorationSegment> {
|
||||
let decorated = decorated_span_ranges(text_paragraph);
|
||||
let decorated = decorated_span_ranges(text_paragraph, offsets);
|
||||
if decorated.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let mut segments = Vec::new();
|
||||
for line in &skia_paragraph.get_line_metrics() {
|
||||
let style_metrics: Vec<_> = line
|
||||
.get_style_metrics(line.start_index..line.end_index)
|
||||
.into_iter()
|
||||
.collect();
|
||||
let style_metrics = line.get_style_metrics(0..usize::MAX);
|
||||
let line_baseline = y_accum + line.baseline as f32;
|
||||
|
||||
let line_decorations: Vec<LineDecoration<'_>> = decorated
|
||||
@ -1244,15 +1546,16 @@ pub fn decoration_segments(
|
||||
if seg_start >= seg_end {
|
||||
return None;
|
||||
}
|
||||
let metrics = style_metric_at(&style_metrics, seg_start)?;
|
||||
Some((seg_start, seg_end, kind, metrics))
|
||||
let range = span_range_at(offsets, seg_start)?;
|
||||
let metrics = style_metric_at(&style_metrics, range.style_anchor_start)?;
|
||||
Some((seg_start, seg_end, kind, range, metrics))
|
||||
})
|
||||
.collect();
|
||||
|
||||
let (max_underline_thickness, underline_y, max_strike_thickness, strike_y) =
|
||||
calculate_decoration_metrics(&line_decorations, line_baseline);
|
||||
|
||||
for (seg_start, seg_end, kind, metrics) in line_decorations {
|
||||
for (seg_start, seg_end, kind, range, metrics) in line_decorations {
|
||||
let rects = skia_paragraph.get_rects_for_range(
|
||||
seg_start..seg_end,
|
||||
skia::textlayout::RectHeightStyle::Tight,
|
||||
@ -1265,11 +1568,26 @@ pub fn decoration_segments(
|
||||
}
|
||||
None => (0.0, 0.0),
|
||||
};
|
||||
let (y, thickness) = if kind == TextDecoration::LINE_THROUGH {
|
||||
let (mut y, thickness) = if kind == TextDecoration::LINE_THROUGH {
|
||||
(strike_y, max_strike_thickness)
|
||||
} else {
|
||||
(underline_y, max_underline_thickness)
|
||||
};
|
||||
if kind == TextDecoration::UNDERLINE {
|
||||
if let Some(span) = text_paragraph
|
||||
.children()
|
||||
.get(range.span)
|
||||
.filter(|span| span.underline_below_ink())
|
||||
{
|
||||
let bottom = rects
|
||||
.iter()
|
||||
.map(|r| r.rect.bottom())
|
||||
.fold(f32::NEG_INFINITY, f32::max);
|
||||
if bottom.is_finite() {
|
||||
y = Some(y_accum + bottom + span.font_size / 9.0 + thickness / 2.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
segments.push(TextDecorationSegment {
|
||||
kind,
|
||||
text_style: (*metrics.text_style).clone(),
|
||||
@ -1292,7 +1610,7 @@ fn calculate_decoration_metrics(
|
||||
let mut underline_y = None;
|
||||
let mut max_strike_thickness: f32 = 0.0;
|
||||
let mut strike_y = None;
|
||||
for (_seg_start, _seg_end, kind, style_metric) in line_decorations.iter() {
|
||||
for (_seg_start, _seg_end, kind, _range, style_metric) in line_decorations.iter() {
|
||||
let font_metrics = style_metric.font_metrics;
|
||||
let font_size = font_metrics
|
||||
.cap_height
|
||||
@ -1387,7 +1705,7 @@ mod tests {
|
||||
]);
|
||||
|
||||
assert_eq!(
|
||||
decorated_span_ranges(¶),
|
||||
decorated_span_ranges(¶, &HorizontalOffsets::new(¶)),
|
||||
vec![
|
||||
(6, 11, TextDecoration::UNDERLINE),
|
||||
(18, 24, TextDecoration::LINE_THROUGH),
|
||||
@ -1409,7 +1727,7 @@ mod tests {
|
||||
|
||||
// The emoji takes two UTF-16 units and `ß` uppercases to `SS`.
|
||||
assert_eq!(
|
||||
decorated_span_ranges(¶),
|
||||
decorated_span_ranges(¶, &HorizontalOffsets::new(¶)),
|
||||
vec![
|
||||
(2, 9, TextDecoration::UNDERLINE),
|
||||
(9, 10, TextDecoration::UNDERLINE),
|
||||
@ -1417,6 +1735,37 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decorated_ranges_follow_inserted_kinsoku_characters() {
|
||||
// A word joiner goes before 。, so the next span starts one unit later.
|
||||
let mut para = paragraph(vec![
|
||||
span("あ。", None, None),
|
||||
span("い", Some(TextDecoration::UNDERLINE), None),
|
||||
]);
|
||||
para.set_language("ja".to_string());
|
||||
|
||||
assert_eq!(
|
||||
decorated_span_ranges(¶, &HorizontalOffsets::new(¶)),
|
||||
vec![(3, 4, TextDecoration::UNDERLINE)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decorated_ranges_skip_warichu_placeholders() {
|
||||
let mut note = span("割注入り", None, None);
|
||||
note.warichu = true;
|
||||
let para = paragraph(vec![
|
||||
note,
|
||||
span("い", Some(TextDecoration::UNDERLINE), None),
|
||||
]);
|
||||
|
||||
// The warichu span collapses to a three-unit placeholder.
|
||||
assert_eq!(
|
||||
decorated_span_ranges(¶, &HorizontalOffsets::new(¶)),
|
||||
vec![(3, 4, TextDecoration::UNDERLINE)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undecorated_paragraphs_have_no_ranges() {
|
||||
let para = paragraph(vec![
|
||||
@ -1424,7 +1773,7 @@ mod tests {
|
||||
span("none", Some(TextDecoration::NO_DECORATION), None),
|
||||
]);
|
||||
|
||||
assert!(decorated_span_ranges(¶).is_empty());
|
||||
assert!(decorated_span_ranges(¶, &HorizontalOffsets::new(¶)).is_empty());
|
||||
}
|
||||
|
||||
fn sized_span(text: &str, font_size: f32, fills: Vec<Fill>) -> TextSpan {
|
||||
|
||||
@ -5,7 +5,8 @@ use skia_safe::{self as skia, Canvas, Matrix, Paint, RRect};
|
||||
use crate::error::Result;
|
||||
use crate::shapes::{
|
||||
circle_segments_local, merge_fills, radius_to_sigma, rect_segments_local, stroke_to_path,
|
||||
BlurType, Fill, Frame, Path, Rect, Shape, SolidColor, Stroke, StrokeKind, StrokeStyle, Type,
|
||||
BlurType, Fill, Frame, Path, Rect, Shape, SolidColor, Stroke, StrokeKind, StrokeStyle,
|
||||
TextContent, Type,
|
||||
};
|
||||
use crate::state::ShapesPoolRef;
|
||||
use crate::uuid::Uuid;
|
||||
@ -66,6 +67,46 @@ impl<'a> VectorRenderer<'a> {
|
||||
}
|
||||
shape.image_filter(1.)
|
||||
}
|
||||
|
||||
/// Vertical text: shadows, fill and strokes painted from one layout.
|
||||
fn draw_vertical_text(
|
||||
&mut self,
|
||||
shape: &Shape,
|
||||
text_content: &TextContent,
|
||||
blur_filter: Option<&skia::ImageFilter>,
|
||||
) {
|
||||
use crate::shapes::text_vertical;
|
||||
|
||||
let bounds = text_content.bounds();
|
||||
let vertical_align = shape.vertical_align();
|
||||
let layout = text_content.vertical_layout(&bounds);
|
||||
let output = crate::render::svg::vertical_glyph_output();
|
||||
|
||||
for shadow in &shape.drop_shadow_paints() {
|
||||
text_vertical::paint_drop_shadow(
|
||||
self.canvas,
|
||||
&layout,
|
||||
&bounds,
|
||||
vertical_align,
|
||||
shadow,
|
||||
output,
|
||||
);
|
||||
}
|
||||
text::paint_vertical_fill(self.canvas, shape, &layout, &bounds, blur_filter, output);
|
||||
let selrect = shape.selrect();
|
||||
for stroke in shape.visible_strokes().rev() {
|
||||
text_vertical::paint_stroke(
|
||||
self.canvas,
|
||||
&layout,
|
||||
&bounds,
|
||||
vertical_align,
|
||||
stroke,
|
||||
&selrect,
|
||||
blur_filter,
|
||||
output,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ShapeRenderer for VectorRenderer<'_> {
|
||||
@ -200,9 +241,15 @@ impl ShapeRenderer for VectorRenderer<'_> {
|
||||
};
|
||||
|
||||
let text_content = text_content.new_bounds(shape.selrect());
|
||||
let blur_filter = self.layer_blur_filter(shape);
|
||||
|
||||
if text_content.is_vertical() {
|
||||
self.draw_vertical_text(shape, &text_content, blur_filter.as_ref());
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut paragraph_builders =
|
||||
text_content.paragraph_builder_group_from_text(Some(self.silhouette));
|
||||
let blur_filter = self.layer_blur_filter(shape);
|
||||
|
||||
// Text drop shadows: one filter layer per shadow over fill + stroke
|
||||
// silhouettes (mirrors GPU `render_text_shadows`). Spread grows the
|
||||
|
||||
@ -11,12 +11,16 @@ use std::iter::once;
|
||||
mod blend;
|
||||
mod blurs;
|
||||
mod bools;
|
||||
pub mod cjk_chars;
|
||||
mod corners;
|
||||
mod fills;
|
||||
mod fonts;
|
||||
mod frames;
|
||||
mod gpos_vpal;
|
||||
mod groups;
|
||||
pub mod jlreq;
|
||||
mod layouts;
|
||||
pub mod line_breaks;
|
||||
pub mod modifiers;
|
||||
mod paths;
|
||||
mod rects;
|
||||
@ -27,7 +31,17 @@ mod strokes;
|
||||
mod svg_attrs;
|
||||
mod svgraw;
|
||||
mod text;
|
||||
mod text_allocation;
|
||||
mod text_attrs;
|
||||
mod text_horizontal;
|
||||
pub mod text_paragraph;
|
||||
pub mod text_paths;
|
||||
pub mod text_policy;
|
||||
mod text_ruby;
|
||||
mod text_shaping;
|
||||
pub mod text_vertical;
|
||||
mod text_warichu;
|
||||
mod text_zhuyin;
|
||||
mod transform;
|
||||
|
||||
pub use blend::*;
|
||||
@ -49,6 +63,8 @@ pub use strokes::*;
|
||||
pub use svg_attrs::*;
|
||||
pub use svgraw::*;
|
||||
pub use text::*;
|
||||
pub use text_attrs::*;
|
||||
pub(crate) use text_horizontal::*;
|
||||
pub use transform::*;
|
||||
|
||||
use crate::math::{self, Bounds, Matrix, Point};
|
||||
@ -435,6 +451,7 @@ impl Shape {
|
||||
|
||||
pub fn set_vertical_align(&mut self, align: VerticalAlign) {
|
||||
self.vertical_align = align;
|
||||
self.invalidate_extrect();
|
||||
}
|
||||
|
||||
pub fn vertical_align(&self) -> VerticalAlign {
|
||||
@ -1545,7 +1562,8 @@ impl Shape {
|
||||
pub fn clear_text(&mut self) {
|
||||
self.invalidate_extrect();
|
||||
if let Type::Text(old_text_content) = &self.shape_type {
|
||||
let new_text_content = TextContent::new(self.selrect, old_text_content.grow_type());
|
||||
let mut new_text_content = TextContent::new(self.selrect, old_text_content.grow_type());
|
||||
new_text_content.set_line_adjustment(old_text_content.line_adjustment());
|
||||
self.shape_type = Type::Text(new_text_content);
|
||||
}
|
||||
}
|
||||
@ -2193,6 +2211,16 @@ impl Shape {
|
||||
true
|
||||
}
|
||||
|
||||
pub fn drop_shadow_paints(&self) -> Vec<skia_safe::Paint> {
|
||||
self.drop_shadows_visible()
|
||||
.map(|shadow| {
|
||||
let mut paint = skia_safe::Paint::default();
|
||||
paint.set_image_filter(shadow.get_drop_shadow_filter());
|
||||
paint
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn inner_shadow_paints(&self) -> Vec<skia_safe::Paint> {
|
||||
let inner_shadows: Vec<&Shadow> = self.inner_shadows_visible().collect();
|
||||
|
||||
|
||||
64
render-wasm/src/shapes/cjk_chars.rs
Normal file
64
render-wasm/src/shapes/cjk_chars.rs
Normal file
@ -0,0 +1,64 @@
|
||||
//! Character ranges and punctuation sets of CJK text, shared by the layout
|
||||
//! policies, the JLREQ classes, Zhuyin and font fallback. Where two users
|
||||
//! need different ranges on purpose, each keeps its own function here with
|
||||
//! a note on why. The JLREQ class tables (`jlreq`) and the vertical upright
|
||||
//! table (`text_shaping::is_upright_char`) follow their own references and
|
||||
//! stay with their users.
|
||||
|
||||
/// Han ideographs of the BMP unified blocks: Extension A and the URO.
|
||||
pub fn is_bmp_unified_han(c: char) -> bool {
|
||||
matches!(u32::from(c), 0x3400..=0x4DBF | 0x4E00..=0x9FFF)
|
||||
}
|
||||
|
||||
/// Han ideographs for the Chinese and Korean presets: the BMP unified
|
||||
/// blocks, the compatibility ideographs and Extensions B to H.
|
||||
pub fn is_han(c: char) -> bool {
|
||||
is_bmp_unified_han(c) || matches!(u32::from(c), 0xF900..=0xFAFF | 0x20000..=0x323AF)
|
||||
}
|
||||
|
||||
/// A base a Zhuyin reading may annotate. Besides `is_han`, it takes the
|
||||
/// Yijing hexagram symbols (U+4DC0..U+4DFF) between Extension A and the URO,
|
||||
/// which the Zhuyin check has always accepted, so saved readings keep their
|
||||
/// mode.
|
||||
pub fn is_zhuyin_base(c: char) -> bool {
|
||||
is_han(c) || matches!(u32::from(c), 0x4DC0..=0x4DFF)
|
||||
}
|
||||
|
||||
/// Assigned hiragana, including the voiced marks and the iteration marks.
|
||||
pub fn is_hiragana(c: char) -> bool {
|
||||
matches!(u32::from(c), 0x3041..=0x309F)
|
||||
}
|
||||
|
||||
/// Katakana, its phonetic extensions and half-width katakana.
|
||||
pub fn is_katakana(c: char) -> bool {
|
||||
matches!(u32::from(c), 0x30A0..=0x30FF | 0x31F0..=0x31FF | 0xFF66..=0xFF9F)
|
||||
}
|
||||
|
||||
/// Kana that mark untagged text as Japanese for font choice: the whole
|
||||
/// Hiragana block (with its unassigned U+3040) and `is_katakana`.
|
||||
pub fn is_kana(c: char) -> bool {
|
||||
u32::from(c) == 0x3040 || is_hiragana(c) || is_katakana(c)
|
||||
}
|
||||
|
||||
/// Hangul syllables, conjoining jamo and compatibility jamo.
|
||||
pub fn is_hangul(c: char) -> bool {
|
||||
matches!(u32::from(c), 0x1100..=0x11FF | 0x3130..=0x318F | 0xAC00..=0xD7AF)
|
||||
}
|
||||
|
||||
/// Full-width opening brackets with a removable leading half-em.
|
||||
pub const FULLWIDTH_OPENING_BRACKETS: &str = "([{〈《「『【〔〖〘〚";
|
||||
|
||||
/// Full-width closing brackets with a removable trailing half-em.
|
||||
pub const FULLWIDTH_CLOSING_BRACKETS: &str = ")]}〉》」』】〕〗〙〛";
|
||||
|
||||
/// Full-width commas and full stops.
|
||||
pub const COMMAS_AND_FULL_STOPS: &str = ",。、.";
|
||||
|
||||
/// CJK and ASCII punctuation, brackets and quotes. A comma or full stop next
|
||||
/// to one of these does not hang alone.
|
||||
pub fn is_punctuation(c: char) -> bool {
|
||||
COMMAS_AND_FULL_STOPS.contains(c)
|
||||
|| FULLWIDTH_OPENING_BRACKETS.contains(c)
|
||||
|| FULLWIDTH_CLOSING_BRACKETS.contains(c)
|
||||
|| "!?:;…‥—―‘’“”.,;!?:()[]{}".contains(c)
|
||||
}
|
||||
285
render-wasm/src/shapes/gpos_vpal.rs
Normal file
285
render-wasm/src/shapes/gpos_vpal.rs
Normal file
@ -0,0 +1,285 @@
|
||||
//! Minimal `GPOS` parser for the `vpal` (proportional vertical alternate
|
||||
//! metrics) feature. Vertical layout places cells by `vmtx` advances and
|
||||
//! applies these deltas itself: SkShaper shapes on a horizontal line, so
|
||||
//! HarfBuzz never applies vertical GPOS positioning.
|
||||
//!
|
||||
//! Reads only single-adjustment lookups (SinglePos, direct or behind an
|
||||
//! Extension lookup): `vpal` is metrics-only, and real fonts (Noto CJK,
|
||||
//! Source Han) encode it this way.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Font-unit deltas for one glyph. `y_placement` is y-up (positive lifts
|
||||
/// the ink toward the flow start); `y_advance` is negative when the
|
||||
/// vertical advance shrinks.
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq)]
|
||||
pub struct VpalDelta {
|
||||
pub y_placement: i16,
|
||||
pub y_advance: i16,
|
||||
}
|
||||
|
||||
fn read_u16(data: &[u8], offset: usize) -> Option<u16> {
|
||||
Some(u16::from_be_bytes([
|
||||
*data.get(offset)?,
|
||||
*data.get(offset + 1)?,
|
||||
]))
|
||||
}
|
||||
|
||||
fn read_i16(data: &[u8], offset: usize) -> Option<i16> {
|
||||
read_u16(data, offset).map(|v| v as i16)
|
||||
}
|
||||
|
||||
fn read_u32(data: &[u8], offset: usize) -> Option<u32> {
|
||||
Some(u32::from_be_bytes([
|
||||
*data.get(offset)?,
|
||||
*data.get(offset + 1)?,
|
||||
*data.get(offset + 2)?,
|
||||
*data.get(offset + 3)?,
|
||||
]))
|
||||
}
|
||||
|
||||
/// Glyph ids covered by a Coverage table, in coverage-index order.
|
||||
fn parse_coverage(data: &[u8], offset: usize) -> Option<Vec<u16>> {
|
||||
match read_u16(data, offset)? {
|
||||
1 => {
|
||||
let count = read_u16(data, offset + 2)? as usize;
|
||||
(0..count)
|
||||
.map(|i| read_u16(data, offset + 4 + i * 2))
|
||||
.collect()
|
||||
}
|
||||
2 => {
|
||||
let range_count = read_u16(data, offset + 2)? as usize;
|
||||
let mut glyphs = Vec::new();
|
||||
for i in 0..range_count {
|
||||
let o = offset + 4 + i * 6;
|
||||
let start = read_u16(data, o)?;
|
||||
let end = read_u16(data, o + 2)?;
|
||||
if end < start {
|
||||
return None;
|
||||
}
|
||||
glyphs.extend(start..=end);
|
||||
}
|
||||
Some(glyphs)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// A ValueRecord holds one i16 per set low bit of `value_format`, in bit
|
||||
/// order (xPlacement, yPlacement, xAdvance, yAdvance, then four device
|
||||
/// offsets). Returns only the y deltas.
|
||||
fn parse_value_record(data: &[u8], offset: usize, value_format: u16) -> Option<VpalDelta> {
|
||||
let mut delta = VpalDelta::default();
|
||||
let mut o = offset;
|
||||
for bit in 0..8 {
|
||||
if value_format & (1 << bit) == 0 {
|
||||
continue;
|
||||
}
|
||||
match bit {
|
||||
1 => delta.y_placement = read_i16(data, o)?,
|
||||
3 => delta.y_advance = read_i16(data, o)?,
|
||||
_ => {}
|
||||
}
|
||||
o += 2;
|
||||
}
|
||||
Some(delta)
|
||||
}
|
||||
|
||||
fn value_record_size(value_format: u16) -> usize {
|
||||
(value_format & 0x00FF).count_ones() as usize * 2
|
||||
}
|
||||
|
||||
/// Accumulate a SinglePos subtable into `deltas`.
|
||||
fn parse_single_pos(
|
||||
data: &[u8],
|
||||
offset: usize,
|
||||
deltas: &mut HashMap<u16, VpalDelta>,
|
||||
) -> Option<()> {
|
||||
let format = read_u16(data, offset)?;
|
||||
let coverage = parse_coverage(data, offset + read_u16(data, offset + 2)? as usize)?;
|
||||
let value_format = read_u16(data, offset + 4)?;
|
||||
match format {
|
||||
1 => {
|
||||
let value = parse_value_record(data, offset + 6, value_format)?;
|
||||
for glyph in coverage {
|
||||
let entry = deltas.entry(glyph).or_default();
|
||||
entry.y_placement += value.y_placement;
|
||||
entry.y_advance += value.y_advance;
|
||||
}
|
||||
}
|
||||
2 => {
|
||||
let count = read_u16(data, offset + 6)? as usize;
|
||||
let size = value_record_size(value_format);
|
||||
for (i, glyph) in coverage.into_iter().take(count).enumerate() {
|
||||
let value = parse_value_record(data, offset + 8 + i * size, value_format)?;
|
||||
let entry = deltas.entry(glyph).or_default();
|
||||
entry.y_placement += value.y_placement;
|
||||
entry.y_advance += value.y_advance;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
|
||||
/// Parse the `vpal` deltas out of a raw `GPOS` table. Returns `None` when
|
||||
/// the table is malformed or carries no `vpal` feature.
|
||||
pub fn parse_vpal(gpos: &[u8]) -> Option<HashMap<u16, VpalDelta>> {
|
||||
let feature_list = read_u16(gpos, 6)? as usize;
|
||||
let lookup_list = read_u16(gpos, 8)? as usize;
|
||||
|
||||
// Collect the lookup indices of every feature tagged `vpal`,
|
||||
// regardless of script: the deltas are per-glyph metrics.
|
||||
let feature_count = read_u16(gpos, feature_list)? as usize;
|
||||
let mut lookup_indices = Vec::new();
|
||||
for i in 0..feature_count {
|
||||
let record = feature_list + 2 + i * 6;
|
||||
if gpos.get(record..record.checked_add(4)?)? != b"vpal" {
|
||||
continue;
|
||||
}
|
||||
let feature = feature_list + read_u16(gpos, record + 4)? as usize;
|
||||
let index_count = read_u16(gpos, feature + 2)? as usize;
|
||||
for j in 0..index_count {
|
||||
lookup_indices.push(read_u16(gpos, feature + 4 + j * 2)? as usize);
|
||||
}
|
||||
}
|
||||
lookup_indices.sort_unstable();
|
||||
lookup_indices.dedup();
|
||||
if lookup_indices.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let lookup_count = read_u16(gpos, lookup_list)? as usize;
|
||||
let mut deltas = HashMap::new();
|
||||
for index in lookup_indices {
|
||||
if index >= lookup_count {
|
||||
continue;
|
||||
}
|
||||
let lookup = lookup_list + read_u16(gpos, lookup_list + 2 + index * 2)? as usize;
|
||||
let lookup_type = read_u16(gpos, lookup)?;
|
||||
let subtable_count = read_u16(gpos, lookup + 4)? as usize;
|
||||
for s in 0..subtable_count {
|
||||
let subtable = lookup + read_u16(gpos, lookup + 6 + s * 2)? as usize;
|
||||
match lookup_type {
|
||||
1 => {
|
||||
parse_single_pos(gpos, subtable, &mut deltas)?;
|
||||
}
|
||||
// ExtensionPos: { format: u16, extensionLookupType: u16,
|
||||
// extensionOffset: u32 (from the subtable start) }.
|
||||
9 if read_u16(gpos, subtable + 2)? == 1 => {
|
||||
let inner = subtable + read_u32(gpos, subtable + 4)? as usize;
|
||||
parse_single_pos(gpos, inner, &mut deltas)?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
if deltas.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(deltas)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// Subset of Noto Sans JP: 〱あく「」、。 with `vert` alternates and the
|
||||
// real `vpal` deltas (Extension→SinglePos, both coverage and both
|
||||
// SinglePos formats, value formats 0x8 and 0xA).
|
||||
const VPAL_TEST_FONT: &[u8] = include_bytes!("../fonts/notosansjp-vpal-test.ttf");
|
||||
|
||||
fn gpos_table() -> Vec<u8> {
|
||||
// Locate the GPOS table in the raw sfnt directory.
|
||||
let data = VPAL_TEST_FONT;
|
||||
let num_tables = u16::from_be_bytes([data[4], data[5]]) as usize;
|
||||
for i in 0..num_tables {
|
||||
let record = 12 + i * 16;
|
||||
if &data[record..record + 4] == b"GPOS" {
|
||||
let offset = u32::from_be_bytes([
|
||||
data[record + 8],
|
||||
data[record + 9],
|
||||
data[record + 10],
|
||||
data[record + 11],
|
||||
]) as usize;
|
||||
let length = u32::from_be_bytes([
|
||||
data[record + 12],
|
||||
data[record + 13],
|
||||
data[record + 14],
|
||||
data[record + 15],
|
||||
]) as usize;
|
||||
return data[offset..offset + length].to_vec();
|
||||
}
|
||||
}
|
||||
panic!("test font has no GPOS table");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_noto_vpal_deltas() {
|
||||
let deltas = parse_vpal(&gpos_table()).expect("vpal deltas");
|
||||
// Vertical alternates: 、→8 。→9 「→10 」→11 あ→12 く→13.
|
||||
assert_eq!(
|
||||
deltas.get(&8),
|
||||
Some(&VpalDelta {
|
||||
y_placement: 0,
|
||||
y_advance: -500
|
||||
}),
|
||||
"、 halves its vertical advance"
|
||||
);
|
||||
assert_eq!(
|
||||
deltas.get(&10),
|
||||
Some(&VpalDelta {
|
||||
y_placement: 481,
|
||||
y_advance: -500
|
||||
}),
|
||||
"「 lifts its trailing-half ink into the compressed cell"
|
||||
);
|
||||
assert_eq!(
|
||||
deltas.get(&12),
|
||||
Some(&VpalDelta {
|
||||
y_placement: 39,
|
||||
y_advance: -58
|
||||
}),
|
||||
"あ tightens slightly"
|
||||
);
|
||||
// Base (horizontal) glyphs carry no vpal.
|
||||
assert!(!deltas.contains_key(&6), "base あ glyph is uncovered");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_vpal_returns_none() {
|
||||
// A GPOS header with an empty feature list.
|
||||
let gpos = [
|
||||
0x00, 0x01, 0x00, 0x00, // version 1.0
|
||||
0x00, 0x0A, // scriptList
|
||||
0x00, 0x0C, // featureList
|
||||
0x00, 0x0E, // lookupList
|
||||
0x00, 0x00, // scriptCount
|
||||
0x00, 0x00, // featureCount
|
||||
0x00, 0x00, // lookupCount
|
||||
];
|
||||
assert!(parse_vpal(&gpos).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncated_table_is_rejected() {
|
||||
let table = gpos_table();
|
||||
for len in [0usize, 4, 9, 16] {
|
||||
assert!(parse_vpal(&table[..len.min(table.len())]).is_none());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncated_feature_records_are_rejected() {
|
||||
let gpos = [
|
||||
0x00, 0x01, 0x00, 0x00, // version 1.0
|
||||
0x00, 0x0A, // scriptList
|
||||
0x00, 0x0A, // featureList
|
||||
0x00, 0x0A, // lookupList
|
||||
0x00, 0x02, // featureCount without matching records
|
||||
];
|
||||
|
||||
assert!(parse_vpal(&gpos).is_none());
|
||||
}
|
||||
}
|
||||
885
render-wasm/src/shapes/jlreq.rs
Normal file
885
render-wasm/src/shapes/jlreq.rs
Normal file
@ -0,0 +1,885 @@
|
||||
//! JLREQ (Japanese) character classes, pair-rule tables and kinsoku rules,
|
||||
//! shared by the horizontal and vertical engines.
|
||||
//!
|
||||
//! JLREQ defines thirty layout classes. Classes 20–24 and 28–30 are
|
||||
//! virtual classes for inline composites: [`classify`] handles single
|
||||
//! characters, and callers assign the virtual classes when building
|
||||
//! reference marks, ruby, grouped numerals, warichu, or tate-chu-yoko.
|
||||
|
||||
use super::cjk_chars::{is_bmp_unified_han, is_hiragana, is_katakana};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
#[repr(u8)]
|
||||
pub enum JlreqClass {
|
||||
OpeningBracket = 0,
|
||||
ClosingBracket,
|
||||
Hyphen,
|
||||
DividingPunctuation,
|
||||
MiddleDot,
|
||||
FullStop,
|
||||
Comma,
|
||||
Inseparable,
|
||||
IterationMark,
|
||||
ProlongedSoundMark,
|
||||
SmallKana,
|
||||
PrefixedAbbreviation,
|
||||
PostfixedAbbreviation,
|
||||
IdeographicSpace,
|
||||
Hiragana,
|
||||
Katakana,
|
||||
MathSymbol,
|
||||
MathOperator,
|
||||
Ideographic,
|
||||
ReferenceMark,
|
||||
OrnamentedComplex,
|
||||
SimpleRuby,
|
||||
JukugoRuby,
|
||||
GroupedNumeral,
|
||||
UnitSymbol,
|
||||
WesternWordSpace,
|
||||
Western,
|
||||
WarichuOpening,
|
||||
WarichuClosing,
|
||||
TateChuYoko,
|
||||
}
|
||||
|
||||
impl JlreqClass {
|
||||
pub const COUNT: usize = 30;
|
||||
|
||||
pub const ALL: [Self; Self::COUNT] = [
|
||||
Self::OpeningBracket,
|
||||
Self::ClosingBracket,
|
||||
Self::Hyphen,
|
||||
Self::DividingPunctuation,
|
||||
Self::MiddleDot,
|
||||
Self::FullStop,
|
||||
Self::Comma,
|
||||
Self::Inseparable,
|
||||
Self::IterationMark,
|
||||
Self::ProlongedSoundMark,
|
||||
Self::SmallKana,
|
||||
Self::PrefixedAbbreviation,
|
||||
Self::PostfixedAbbreviation,
|
||||
Self::IdeographicSpace,
|
||||
Self::Hiragana,
|
||||
Self::Katakana,
|
||||
Self::MathSymbol,
|
||||
Self::MathOperator,
|
||||
Self::Ideographic,
|
||||
Self::ReferenceMark,
|
||||
Self::OrnamentedComplex,
|
||||
Self::SimpleRuby,
|
||||
Self::JukugoRuby,
|
||||
Self::GroupedNumeral,
|
||||
Self::UnitSymbol,
|
||||
Self::WesternWordSpace,
|
||||
Self::Western,
|
||||
Self::WarichuOpening,
|
||||
Self::WarichuClosing,
|
||||
Self::TateChuYoko,
|
||||
];
|
||||
|
||||
pub const fn index(self) -> usize {
|
||||
self as usize
|
||||
}
|
||||
|
||||
pub const fn forbids_line_start(self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::ClosingBracket
|
||||
| Self::Hyphen
|
||||
| Self::DividingPunctuation
|
||||
| Self::MiddleDot
|
||||
| Self::FullStop
|
||||
| Self::Comma
|
||||
| Self::IterationMark
|
||||
| Self::ProlongedSoundMark
|
||||
| Self::SmallKana
|
||||
| Self::WarichuClosing
|
||||
)
|
||||
}
|
||||
|
||||
pub const fn forbids_line_end(self) -> bool {
|
||||
matches!(self, Self::OpeningBracket | Self::WarichuOpening)
|
||||
}
|
||||
|
||||
pub const fn is_japanese_letter(self) -> bool {
|
||||
matches!(self, Self::Hiragana | Self::Katakana | Self::Ideographic)
|
||||
}
|
||||
|
||||
/// Kana, kanji and the marks that read as part of a word: small kana,
|
||||
/// the prolonged sound mark and iteration marks.
|
||||
pub const fn is_japanese_text(self) -> bool {
|
||||
self.is_japanese_letter()
|
||||
|| matches!(
|
||||
self,
|
||||
Self::SmallKana | Self::ProlongedSoundMark | Self::IterationMark
|
||||
)
|
||||
}
|
||||
|
||||
pub const fn is_western_run(self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::GroupedNumeral | Self::UnitSymbol | Self::Western
|
||||
)
|
||||
}
|
||||
|
||||
pub const fn is_emphasis_prohibited(self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::OpeningBracket | Self::ClosingBracket | Self::FullStop | Self::Comma
|
||||
)
|
||||
}
|
||||
|
||||
/// Half-width punctuation followed by a half-em of aki that completes
|
||||
/// its character frame. Consecutive punctuation may drop that aki; the
|
||||
/// glyph body stays half-width.
|
||||
pub const fn is_trailing_aki_punctuation(self) -> bool {
|
||||
matches!(self, Self::ClosingBracket | Self::FullStop | Self::Comma)
|
||||
}
|
||||
|
||||
/// Aki (in em) a full-width font builds into the glyph before its ink.
|
||||
pub const fn embedded_leading_aki_em(self) -> f32 {
|
||||
match self {
|
||||
Self::OpeningBracket => 0.5,
|
||||
Self::MiddleDot => 0.25,
|
||||
_ => 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Aki (in em) a full-width font builds into the glyph after its ink.
|
||||
pub const fn embedded_trailing_aki_em(self) -> f32 {
|
||||
match self {
|
||||
Self::ClosingBracket | Self::FullStop | Self::Comma => 0.5,
|
||||
Self::MiddleDot => 0.25,
|
||||
_ => 0.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the half-em aki between `before` and `after` goes (JLREQ §3.1.4):
|
||||
/// closing sequences set solid, closing→opening keeps one half-em, opening
|
||||
/// sequences set solid after the first bracket. Returns (shed the trailing
|
||||
/// aki of `before`, shed the leading aki of `after`); a closing mark owns the
|
||||
/// reduction, so both halves never go.
|
||||
pub fn shed_pair_aki(before: JlreqClass, after: JlreqClass) -> (bool, bool) {
|
||||
let preferred = pair_rule(before, after).preferred_em + f32::EPSILON;
|
||||
if before.is_trailing_aki_punctuation() {
|
||||
(0.5 + after.embedded_leading_aki_em() > preferred, false)
|
||||
} else if after == JlreqClass::OpeningBracket {
|
||||
(false, before.embedded_trailing_aki_em() + 0.5 > preferred)
|
||||
} else {
|
||||
(false, false)
|
||||
}
|
||||
}
|
||||
|
||||
/// Which built-in half-em aki of a character goes.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum AkiShed {
|
||||
/// The aki after a closing mark.
|
||||
Trailing,
|
||||
/// The aki before an opening bracket.
|
||||
Leading,
|
||||
}
|
||||
|
||||
/// Class a character takes for punctuation aki, or `None` when its built-in
|
||||
/// aki is unknown: punctuation set proportionally (`proportional`, from
|
||||
/// `palt` or `vpal` in the flow direction) and curly quotes, which are
|
||||
/// full-width in some fonts and proportional in others. A ruby base counts
|
||||
/// as simple ruby (cl-22).
|
||||
pub fn aki_class(ch: char, ruby_base: bool, proportional: bool) -> Option<JlreqClass> {
|
||||
if ruby_base {
|
||||
return Some(JlreqClass::SimpleRuby);
|
||||
}
|
||||
let curly_quote = matches!(ch, '‘' | '’' | '“' | '”');
|
||||
(!proportional && !curly_quote).then(|| classify(ch))
|
||||
}
|
||||
|
||||
/// The half-em aki that go in a run of characters classed by `aki_class`
|
||||
/// (JLREQ §3.1.4, see `shed_pair_aki`), as (character index, which aki).
|
||||
/// A character of unknown aki (`None`) sheds nothing and makes its
|
||||
/// neighbours keep theirs.
|
||||
pub fn punctuation_aki_sheds(classes: &[Option<JlreqClass>]) -> Vec<(usize, AkiShed)> {
|
||||
let mut sheds = Vec::new();
|
||||
for (index, pair) in classes.windows(2).enumerate() {
|
||||
let [Some(before), Some(after)] = *pair else {
|
||||
continue;
|
||||
};
|
||||
match shed_pair_aki(before, after) {
|
||||
(true, _) => sheds.push((index, AkiShed::Trailing)),
|
||||
(_, true) => sheds.push((index + 1, AkiShed::Leading)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
sheds
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct PairRule {
|
||||
/// Preferred extra spacing between the two character frames, in em.
|
||||
pub preferred_em: f32,
|
||||
/// Smallest spacing allowed during oikomi, in em.
|
||||
pub minimum_em: f32,
|
||||
/// Largest spacing allowed during oidashi/justification, in em.
|
||||
pub maximum_em: f32,
|
||||
pub break_allowed: bool,
|
||||
/// Whether horizontal SkParagraph needs a WORD JOINER to block a break
|
||||
/// in this pair. Its Unicode breaker already keeps Western/numeral runs
|
||||
/// whole.
|
||||
pub suppress_break_with_joiner: bool,
|
||||
/// Lower values are adjusted first; zero means not adjustable.
|
||||
pub shrink_priority: u8,
|
||||
pub expand_priority: u8,
|
||||
}
|
||||
|
||||
impl PairRule {
|
||||
const SOLID: Self = Self {
|
||||
preferred_em: 0.0,
|
||||
minimum_em: 0.0,
|
||||
maximum_em: 0.0,
|
||||
break_allowed: true,
|
||||
suppress_break_with_joiner: false,
|
||||
shrink_priority: 0,
|
||||
expand_priority: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const fn generated_pair_rules() -> [[PairRule; JlreqClass::COUNT]; JlreqClass::COUNT] {
|
||||
let mut table = [[PairRule::SOLID; JlreqClass::COUNT]; JlreqClass::COUNT];
|
||||
let mut before_index = 0;
|
||||
while before_index < JlreqClass::COUNT {
|
||||
let before = JlreqClass::ALL[before_index];
|
||||
let mut after_index = 0;
|
||||
while after_index < JlreqClass::COUNT {
|
||||
let after = JlreqClass::ALL[after_index];
|
||||
let mut rule = PairRule::SOLID;
|
||||
|
||||
rule.suppress_break_with_joiner =
|
||||
before.forbids_line_end() || after.forbids_line_start();
|
||||
rule.break_allowed = !rule.suppress_break_with_joiner;
|
||||
if before_index == after_index
|
||||
&& matches!(before, JlreqClass::GroupedNumeral | JlreqClass::Western)
|
||||
{
|
||||
rule.break_allowed = false;
|
||||
}
|
||||
|
||||
if matches!(before, JlreqClass::WesternWordSpace) {
|
||||
rule.preferred_em = 1.0 / 3.0;
|
||||
rule.minimum_em = 0.25;
|
||||
rule.maximum_em = 0.5;
|
||||
rule.shrink_priority = 1;
|
||||
rule.expand_priority = 1;
|
||||
} else if (before.is_japanese_text() && after.is_western_run())
|
||||
|| (before.is_western_run() && after.is_japanese_text())
|
||||
{
|
||||
rule.preferred_em = 0.25;
|
||||
rule.minimum_em = 0.125;
|
||||
rule.maximum_em = 0.5;
|
||||
rule.shrink_priority = 5;
|
||||
rule.expand_priority = 2;
|
||||
} else if matches!(
|
||||
(before, after),
|
||||
(
|
||||
JlreqClass::ClosingBracket | JlreqClass::Comma | JlreqClass::FullStop,
|
||||
JlreqClass::TateChuYoko
|
||||
) | (JlreqClass::TateChuYoko, JlreqClass::OpeningBracket)
|
||||
) {
|
||||
rule.preferred_em = 0.5;
|
||||
rule.minimum_em = 0.0;
|
||||
rule.maximum_em = 0.5;
|
||||
rule.shrink_priority = 3;
|
||||
} else if matches!(
|
||||
(before, after),
|
||||
(JlreqClass::TateChuYoko, JlreqClass::TateChuYoko)
|
||||
) {
|
||||
// Adjacent cl-30 entries are always separate TCY composites
|
||||
// (characters inside one composite are atomic).
|
||||
rule.maximum_em = 0.25;
|
||||
rule.expand_priority = 3;
|
||||
} else if matches!(before, JlreqClass::DividingPunctuation)
|
||||
&& !matches!(
|
||||
after,
|
||||
JlreqClass::ClosingBracket
|
||||
| JlreqClass::DividingPunctuation
|
||||
| JlreqClass::IdeographicSpace
|
||||
| JlreqClass::WesternWordSpace
|
||||
| JlreqClass::FullStop
|
||||
| JlreqClass::Comma
|
||||
| JlreqClass::MiddleDot
|
||||
)
|
||||
{
|
||||
// A sentence-ending question/exclamation mark carries one em
|
||||
// after it, unless punctuation or a typed space follows. Line
|
||||
// planning may drop it at the line edge.
|
||||
rule.preferred_em = 1.0;
|
||||
rule.minimum_em = 0.0;
|
||||
rule.maximum_em = 1.0;
|
||||
rule.shrink_priority = 2;
|
||||
} else if before.is_trailing_aki_punctuation()
|
||||
&& matches!(after, JlreqClass::OpeningBracket)
|
||||
{
|
||||
// Keep one half-em between the two half-width glyph bodies,
|
||||
// not the sum of both characters' aki.
|
||||
rule.preferred_em = 0.5;
|
||||
rule.minimum_em = 0.0;
|
||||
rule.maximum_em = 0.5;
|
||||
rule.shrink_priority = 4;
|
||||
} else if before.is_trailing_aki_punctuation() && after.is_trailing_aki_punctuation() {
|
||||
// Consecutive closing punctuation sets solid internally; the
|
||||
// last character in the sequence supplies the trailing aki.
|
||||
} else if matches!(before, JlreqClass::OpeningBracket)
|
||||
&& matches!(after, JlreqClass::OpeningBracket)
|
||||
{
|
||||
// Consecutive opening brackets set solid internally; the first
|
||||
// character in the sequence supplies the leading aki.
|
||||
} else if (before.is_trailing_aki_punctuation()
|
||||
&& matches!(after, JlreqClass::MiddleDot))
|
||||
|| (matches!(before, JlreqClass::MiddleDot)
|
||||
&& matches!(after, JlreqClass::OpeningBracket))
|
||||
{
|
||||
rule.preferred_em = 0.25;
|
||||
rule.minimum_em = 0.0;
|
||||
rule.maximum_em = 0.25;
|
||||
rule.shrink_priority = 3;
|
||||
} else if before.is_trailing_aki_punctuation() {
|
||||
rule.preferred_em = 0.5;
|
||||
rule.minimum_em = if matches!(before, JlreqClass::FullStop) {
|
||||
0.5
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
rule.maximum_em = 0.5;
|
||||
rule.shrink_priority = if matches!(before, JlreqClass::FullStop) {
|
||||
0
|
||||
} else {
|
||||
4
|
||||
};
|
||||
} else if matches!(after, JlreqClass::OpeningBracket) {
|
||||
rule.preferred_em = 0.5;
|
||||
rule.minimum_em = 0.0;
|
||||
rule.maximum_em = 0.5;
|
||||
rule.shrink_priority = 4;
|
||||
} else if matches!(before, JlreqClass::MiddleDot)
|
||||
|| matches!(after, JlreqClass::MiddleDot)
|
||||
{
|
||||
rule.preferred_em = 0.25;
|
||||
rule.minimum_em = 0.0;
|
||||
rule.maximum_em = 0.25;
|
||||
rule.shrink_priority = 3;
|
||||
} else if before.is_japanese_letter() && after.is_japanese_letter() {
|
||||
// Solid Japanese text is the general third-stage expansion
|
||||
// point. The planner passes this quarter-em cap only in
|
||||
// JLREQ's final equal-expansion fallback.
|
||||
rule.maximum_em = 0.25;
|
||||
rule.expand_priority = 3;
|
||||
}
|
||||
|
||||
table[before_index][after_index] = rule;
|
||||
after_index += 1;
|
||||
}
|
||||
before_index += 1;
|
||||
}
|
||||
table
|
||||
}
|
||||
|
||||
pub const PAIR_RULES: [[PairRule; JlreqClass::COUNT]; JlreqClass::COUNT] = generated_pair_rules();
|
||||
|
||||
pub const fn pair_rule(before: JlreqClass, after: JlreqClass) -> PairRule {
|
||||
PAIR_RULES[before.index()][after.index()]
|
||||
}
|
||||
|
||||
// JLREQ Appendix A class tables. They are wider than the `cjk_chars`
|
||||
// punctuation sets the pairwise policies use.
|
||||
const OPENING_BRACKETS: &str = "(〔[{〈《「『【〖〘〚‘“〝«⦅⦅「";
|
||||
const CLOSING_BRACKETS: &str = ")〕]}〉》」』】〗〙〛’”〟〞»⦆⦆」";
|
||||
const HYPHENS: &str = "‐゠–〜~";
|
||||
const DIVIDING_PUNCTUATION: &str = "!?‼⁇⁈⁉";
|
||||
const MIDDLE_DOTS: &str = "・・:;";
|
||||
const FULL_STOPS: &str = "。.。";
|
||||
const COMMAS: &str = "、,、";
|
||||
const INSEPARABLE: &str = "—―…‥〳〴〵";
|
||||
const ITERATION_MARKS: &str = "々〻ゝゞヽヾ";
|
||||
const SMALL_KANA: &str = concat!(
|
||||
"ぁぃぅぇぉっゃゅょゎゕゖ",
|
||||
"ァィゥェォッャュョヮヵヶㇰㇱㇲㇳㇴㇵㇶㇷㇸㇹㇺㇻㇼㇽㇾㇿ",
|
||||
"ァィゥェォャュョッ"
|
||||
);
|
||||
const PREFIXED_ABBREVIATIONS: &str = "¥¥$$££##€";
|
||||
const POSTFIXED_ABBREVIATIONS: &str = concat!("°′″℃¢¢%%‰‱ℓ", "㌃㌍㌔㌘㌢㌣㌦㌧㌫㌶㌻㍉㍊㍍㍑㍗");
|
||||
const MATH_SYMBOLS: &str = "==≠≒≃≅≈≡≢<<>>≦≧≤≥≪≫≶≷⋚⋛∈∋⊆⊇⊂⊃∪∩⊄⊅⊊⊋∉⌅⌆∧∨⇒⇔↔∥∦∽∝⊥⊕⊗";
|
||||
const MATH_OPERATORS: &str = "++-−-÷×±∓∗∙√∫∬∭∑∏";
|
||||
|
||||
pub fn classify(c: char) -> JlreqClass {
|
||||
// Common letters skip the table scans.
|
||||
match c {
|
||||
c if is_bmp_unified_han(c) => JlreqClass::Ideographic,
|
||||
'a'..='z' | 'A'..='Z' => JlreqClass::Western,
|
||||
_ => classify_by_tables(c),
|
||||
}
|
||||
}
|
||||
|
||||
fn classify_by_tables(c: char) -> JlreqClass {
|
||||
if OPENING_BRACKETS.contains(c) {
|
||||
JlreqClass::OpeningBracket
|
||||
} else if CLOSING_BRACKETS.contains(c) {
|
||||
JlreqClass::ClosingBracket
|
||||
} else if HYPHENS.contains(c) {
|
||||
JlreqClass::Hyphen
|
||||
} else if DIVIDING_PUNCTUATION.contains(c) {
|
||||
JlreqClass::DividingPunctuation
|
||||
} else if MIDDLE_DOTS.contains(c) {
|
||||
JlreqClass::MiddleDot
|
||||
} else if FULL_STOPS.contains(c) {
|
||||
JlreqClass::FullStop
|
||||
} else if COMMAS.contains(c) {
|
||||
JlreqClass::Comma
|
||||
} else if INSEPARABLE.contains(c) {
|
||||
JlreqClass::Inseparable
|
||||
} else if ITERATION_MARKS.contains(c) {
|
||||
JlreqClass::IterationMark
|
||||
} else if c == 'ー' || c == 'ー' {
|
||||
JlreqClass::ProlongedSoundMark
|
||||
} else if SMALL_KANA.contains(c) {
|
||||
JlreqClass::SmallKana
|
||||
} else if PREFIXED_ABBREVIATIONS.contains(c) {
|
||||
JlreqClass::PrefixedAbbreviation
|
||||
} else if POSTFIXED_ABBREVIATIONS.contains(c) {
|
||||
JlreqClass::PostfixedAbbreviation
|
||||
} else if c == '\u{3000}' {
|
||||
JlreqClass::IdeographicSpace
|
||||
} else if MATH_SYMBOLS.contains(c) {
|
||||
JlreqClass::MathSymbol
|
||||
} else if MATH_OPERATORS.contains(c) {
|
||||
JlreqClass::MathOperator
|
||||
} else if c == ' ' || c == '\t' || c == '\u{00a0}' {
|
||||
JlreqClass::WesternWordSpace
|
||||
} else if c.is_ascii_digit() {
|
||||
JlreqClass::GroupedNumeral
|
||||
} else if is_hiragana(c) {
|
||||
JlreqClass::Hiragana
|
||||
} else if is_katakana(c) {
|
||||
JlreqClass::Katakana
|
||||
} else if is_ideographic(c) {
|
||||
JlreqClass::Ideographic
|
||||
} else if is_squared_unit(c) {
|
||||
JlreqClass::PostfixedAbbreviation
|
||||
} else if is_ideographic_symbol(c) {
|
||||
JlreqClass::Ideographic
|
||||
} else {
|
||||
JlreqClass::Western
|
||||
}
|
||||
}
|
||||
|
||||
/// Digits that group with an adjacent prefixed or postfixed abbreviation.
|
||||
fn is_numeral(c: char) -> bool {
|
||||
c.is_ascii_digit() || matches!(u32::from(c), 0xFF10..=0xFF19)
|
||||
}
|
||||
|
||||
/// Character pairs kept on one line beyond the class table: two identical
|
||||
/// inseparable marks (`——`, `……`), an abbreviation with its numeral
|
||||
/// (`¥100`, `60㎏`, `100%`), per JLREQ §3.1.10, and digits of one
|
||||
/// number, full-width ones included (`2026`), which JLREQ would let break
|
||||
/// as ideographs. ASCII digit pairs are already grouped numerals.
|
||||
pub fn keeps_together(before: char, after: char) -> bool {
|
||||
keeps_together_classified((before, classify(before)), (after, classify(after)))
|
||||
}
|
||||
|
||||
/// `keeps_together` for characters already paired with their classes.
|
||||
pub fn keeps_together_classified(
|
||||
(before, before_class): (char, JlreqClass),
|
||||
(after, after_class): (char, JlreqClass),
|
||||
) -> bool {
|
||||
(before == after && before_class == JlreqClass::Inseparable)
|
||||
|| (is_numeral(before)
|
||||
&& is_numeral(after)
|
||||
&& !(before.is_ascii_digit() && after.is_ascii_digit()))
|
||||
|| (is_numeral(before) && after_class == JlreqClass::PostfixedAbbreviation)
|
||||
|| (before_class == JlreqClass::PrefixedAbbreviation && is_numeral(after))
|
||||
}
|
||||
|
||||
/// Kinsoku shori: a character that may not start a line.
|
||||
pub fn forbidden_at_line_start(c: char) -> bool {
|
||||
classify(c).forbids_line_start()
|
||||
}
|
||||
|
||||
/// Kinsoku shori: a character that may not end a line.
|
||||
pub fn forbidden_at_line_end(c: char) -> bool {
|
||||
classify(c).forbids_line_end()
|
||||
}
|
||||
|
||||
/// Most kinsoku pairs oidashi moves a break back over. Past that, pushing
|
||||
/// out does not resolve the break, and the line may squeeze instead.
|
||||
pub const MAX_KINSOKU_SHIFT: usize = 4;
|
||||
|
||||
/// True when kinsoku forbids a break between `before` and `after`.
|
||||
pub fn kinsoku_blocks(before: char, after: char) -> bool {
|
||||
forbidden_at_line_end(before) || forbidden_at_line_start(after)
|
||||
}
|
||||
|
||||
/// Whether a line may break between two adjacent characters: the pair table
|
||||
/// (kinsoku, Western and numeral runs) plus `keeps_together`.
|
||||
pub fn break_allowed_between(before: char, after: char) -> bool {
|
||||
pair_rule(classify(before), classify(after)).break_allowed && !keeps_together(before, after)
|
||||
}
|
||||
|
||||
/// True for CJK text characters: kana, Han ideographs, and the CJK symbols
|
||||
/// and punctuation, katakana extension and half/full-width form blocks.
|
||||
/// Hangul is not included.
|
||||
pub fn is_cjk_text_char(c: char) -> bool {
|
||||
classify(c).is_japanese_letter()
|
||||
|| matches!(u32::from(c), 0x3000..=0x30FF | 0x31F0..=0x31FF | 0xFF00..=0xFFEF)
|
||||
}
|
||||
|
||||
/// JLREQ cl-19 ideographs. Wider than `cjk_chars::is_han` (radicals, strokes,
|
||||
/// the compatibility supplement and a few marks), as JLREQ Appendix A lists.
|
||||
fn is_ideographic(c: char) -> bool {
|
||||
matches!(u32::from(c),
|
||||
0x2E80..=0x2FDF
|
||||
| 0x31C0..=0x31EF
|
||||
| 0x3400..=0x4DBF
|
||||
| 0x4E00..=0x9FFF
|
||||
| 0xF900..=0xFAFF
|
||||
| 0x20000..=0x2FA1F
|
||||
| 0x30000..=0x323AF
|
||||
) || matches!(c, '〃' | '仝' | '〆' | '♂' | '♀')
|
||||
}
|
||||
|
||||
/// Squared Latin unit abbreviations (㎏, ㎡, ㏄, …), cl-13 like %. ㏍ is a
|
||||
/// company mark, cl-19.
|
||||
fn is_squared_unit(c: char) -> bool {
|
||||
matches!(u32::from(c), 0x3380..=0x33DF) && c != '㏍'
|
||||
}
|
||||
|
||||
/// Symbols JLREQ sets as full-width ideographic characters (cl-19): the
|
||||
/// full-width forms, CJK symbols, circled and parenthesized characters,
|
||||
/// Roman numerals, arrows, shapes and squared words. Latin-1 symbols such
|
||||
/// as © or † stay Western, as they also appear in Latin text.
|
||||
fn is_ideographic_symbol(c: char) -> bool {
|
||||
matches!(u32::from(c),
|
||||
0xFF01..=0xFF5E
|
||||
| 0x2160..=0x217F
|
||||
| 0x2190..=0x21FF
|
||||
| 0x2460..=0x24FF
|
||||
| 0x25A0..=0x25FF
|
||||
| 0x2600..=0x26FF
|
||||
| 0x2776..=0x277F
|
||||
| 0x3000..=0x303F
|
||||
| 0x3200..=0x33FF
|
||||
) || matches!(
|
||||
c,
|
||||
'※' | '⁂' | '⁑' | '℡' | '✓' | '❖' | '⤴' | '⤵' | '⦿' | 'ゟ' | 'ヿ'
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn classes_forbidden_at_start() {
|
||||
for c in "、。」』)ーっゃァッ々・!?".chars() {
|
||||
assert!(forbidden_at_line_start(c), "expected start-forbidden: {c}");
|
||||
}
|
||||
for c in "あ漢A1「( ".chars() {
|
||||
assert!(!forbidden_at_line_start(c), "not start-forbidden: {c}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classes_forbidden_at_end() {
|
||||
for c in "「『([【〈《".chars() {
|
||||
assert!(forbidden_at_line_end(c), "expected end-forbidden: {c}");
|
||||
}
|
||||
for c in "あ漢A1」)。".chars() {
|
||||
assert!(!forbidden_at_line_end(c), "not end-forbidden: {c}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn aki_class_leaves_out_curly_quotes_and_proportional_punctuation() {
|
||||
assert_eq!(aki_class('“', false, false), None);
|
||||
assert_eq!(aki_class('」', false, true), None);
|
||||
assert_eq!(
|
||||
aki_class('」', false, false),
|
||||
Some(JlreqClass::ClosingBracket)
|
||||
);
|
||||
assert_eq!(aki_class('」', true, false), Some(JlreqClass::SimpleRuby));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn punctuation_aki_sheds_the_trailing_aki_of_a_closing_mark() {
|
||||
use JlreqClass::*;
|
||||
let classes = [Some(ClosingBracket), Some(OpeningBracket)];
|
||||
assert_eq!(
|
||||
punctuation_aki_sheds(&classes),
|
||||
vec![(0, AkiShed::Trailing)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn punctuation_aki_sheds_the_leading_aki_of_a_second_opening_bracket() {
|
||||
use JlreqClass::*;
|
||||
let classes = [Some(OpeningBracket), Some(OpeningBracket)];
|
||||
assert_eq!(punctuation_aki_sheds(&classes), vec![(1, AkiShed::Leading)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn punctuation_aki_sheds_nothing_next_to_an_unknown_aki() {
|
||||
use JlreqClass::*;
|
||||
let classes = [Some(ClosingBracket), None, Some(OpeningBracket)];
|
||||
assert!(punctuation_aki_sheds(&classes).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn class_model_contains_all_thirty_jlreq_classes_in_order() {
|
||||
assert_eq!(JlreqClass::ALL.len(), 30);
|
||||
for (index, class) in JlreqClass::ALL.iter().enumerate() {
|
||||
assert_eq!(class.index(), index);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_representative_jlreq_characters() {
|
||||
let cases = [
|
||||
('「', JlreqClass::OpeningBracket),
|
||||
('」', JlreqClass::ClosingBracket),
|
||||
('〜', JlreqClass::Hyphen),
|
||||
('!', JlreqClass::DividingPunctuation),
|
||||
('・', JlreqClass::MiddleDot),
|
||||
('。', JlreqClass::FullStop),
|
||||
('、', JlreqClass::Comma),
|
||||
('…', JlreqClass::Inseparable),
|
||||
('々', JlreqClass::IterationMark),
|
||||
('ー', JlreqClass::ProlongedSoundMark),
|
||||
('ょ', JlreqClass::SmallKana),
|
||||
('¥', JlreqClass::PrefixedAbbreviation),
|
||||
('%', JlreqClass::PostfixedAbbreviation),
|
||||
('\u{3000}', JlreqClass::IdeographicSpace),
|
||||
('あ', JlreqClass::Hiragana),
|
||||
('ア', JlreqClass::Katakana),
|
||||
('≠', JlreqClass::MathSymbol),
|
||||
('+', JlreqClass::MathOperator),
|
||||
('漢', JlreqClass::Ideographic),
|
||||
('2', JlreqClass::GroupedNumeral),
|
||||
('㎏', JlreqClass::PostfixedAbbreviation),
|
||||
(' ', JlreqClass::WesternWordSpace),
|
||||
('A', JlreqClass::Western),
|
||||
];
|
||||
for (character, expected) in cases {
|
||||
assert_eq!(classify(character), expected, "wrong class for {character}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generated_rules_cover_every_class_pair() {
|
||||
assert_eq!(PAIR_RULES.len(), JlreqClass::COUNT);
|
||||
assert!(PAIR_RULES.iter().all(|row| row.len() == JlreqClass::COUNT));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generated_rules_encode_kinsoku_and_atomic_runs() {
|
||||
assert!(!pair_rule(JlreqClass::OpeningBracket, JlreqClass::Ideographic).break_allowed);
|
||||
assert!(!pair_rule(JlreqClass::Ideographic, JlreqClass::ClosingBracket).break_allowed);
|
||||
assert!(!pair_rule(JlreqClass::GroupedNumeral, JlreqClass::GroupedNumeral).break_allowed);
|
||||
assert!(pair_rule(JlreqClass::Ideographic, JlreqClass::Ideographic).break_allowed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generated_rules_encode_script_and_tcy_spacing() {
|
||||
let script = pair_rule(JlreqClass::Ideographic, JlreqClass::Western);
|
||||
assert_eq!(script.preferred_em, 0.25);
|
||||
assert_eq!(script.minimum_em, 0.125);
|
||||
assert_eq!(script.maximum_em, 0.5);
|
||||
|
||||
let tcy = pair_rule(JlreqClass::Comma, JlreqClass::TateChuYoko);
|
||||
assert_eq!(tcy.preferred_em, 0.5);
|
||||
assert_eq!(
|
||||
pair_rule(JlreqClass::TateChuYoko, JlreqClass::Comma).preferred_em,
|
||||
0.0
|
||||
);
|
||||
assert_eq!(
|
||||
pair_rule(JlreqClass::OpeningBracket, JlreqClass::TateChuYoko).preferred_em,
|
||||
0.0
|
||||
);
|
||||
assert_eq!(
|
||||
pair_rule(JlreqClass::TateChuYoko, JlreqClass::OpeningBracket).preferred_em,
|
||||
0.5
|
||||
);
|
||||
assert_eq!(
|
||||
pair_rule(JlreqClass::Ideographic, JlreqClass::TateChuYoko).preferred_em,
|
||||
0.0
|
||||
);
|
||||
assert_eq!(
|
||||
pair_rule(JlreqClass::TateChuYoko, JlreqClass::Ideographic).preferred_em,
|
||||
0.0
|
||||
);
|
||||
let adjacent_tcy = pair_rule(JlreqClass::TateChuYoko, JlreqClass::TateChuYoko);
|
||||
assert_eq!(adjacent_tcy.preferred_em, 0.0);
|
||||
assert_eq!(adjacent_tcy.maximum_em, 0.25);
|
||||
assert_eq!(adjacent_tcy.expand_priority, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generated_rules_encode_punctuation_sequences() {
|
||||
assert_eq!(
|
||||
pair_rule(JlreqClass::Ideographic, JlreqClass::OpeningBracket).preferred_em,
|
||||
0.5
|
||||
);
|
||||
assert_eq!(
|
||||
pair_rule(JlreqClass::ClosingBracket, JlreqClass::Ideographic).preferred_em,
|
||||
0.5
|
||||
);
|
||||
assert_eq!(
|
||||
pair_rule(JlreqClass::FullStop, JlreqClass::ClosingBracket).preferred_em,
|
||||
0.0
|
||||
);
|
||||
assert_eq!(
|
||||
pair_rule(JlreqClass::ClosingBracket, JlreqClass::OpeningBracket).preferred_em,
|
||||
0.5
|
||||
);
|
||||
assert_eq!(
|
||||
pair_rule(JlreqClass::ClosingBracket, JlreqClass::MiddleDot).preferred_em,
|
||||
0.25
|
||||
);
|
||||
assert_eq!(
|
||||
pair_rule(JlreqClass::MiddleDot, JlreqClass::OpeningBracket).preferred_em,
|
||||
0.25
|
||||
);
|
||||
assert_eq!(
|
||||
pair_rule(JlreqClass::DividingPunctuation, JlreqClass::Ideographic).preferred_em,
|
||||
1.0
|
||||
);
|
||||
assert_eq!(
|
||||
pair_rule(
|
||||
JlreqClass::DividingPunctuation,
|
||||
JlreqClass::DividingPunctuation
|
||||
)
|
||||
.preferred_em,
|
||||
0.0
|
||||
);
|
||||
assert_eq!(
|
||||
pair_rule(
|
||||
JlreqClass::DividingPunctuation,
|
||||
JlreqClass::IdeographicSpace
|
||||
)
|
||||
.preferred_em,
|
||||
0.0
|
||||
);
|
||||
let solid_japanese = pair_rule(JlreqClass::Ideographic, JlreqClass::Hiragana);
|
||||
assert_eq!(solid_japanese.preferred_em, 0.0);
|
||||
assert_eq!(solid_japanese.maximum_em, 0.25);
|
||||
assert_eq!(solid_japanese.expand_priority, 3);
|
||||
let ruby_adjacency = pair_rule(JlreqClass::SimpleRuby, JlreqClass::JukugoRuby);
|
||||
assert_eq!(ruby_adjacency.preferred_em, 0.0);
|
||||
assert_eq!(ruby_adjacency.maximum_em, 0.0);
|
||||
assert_eq!(ruby_adjacency.expand_priority, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classify_follows_jlreq_appendix_a() {
|
||||
let cases = [
|
||||
('—', JlreqClass::Inseparable),
|
||||
('〝', JlreqClass::OpeningBracket),
|
||||
('〟', JlreqClass::ClosingBracket),
|
||||
('〞', JlreqClass::ClosingBracket),
|
||||
('«', JlreqClass::OpeningBracket),
|
||||
('»', JlreqClass::ClosingBracket),
|
||||
('⦅', JlreqClass::OpeningBracket),
|
||||
('⦆', JlreqClass::ClosingBracket),
|
||||
('⦅', JlreqClass::OpeningBracket),
|
||||
('⦆', JlreqClass::ClosingBracket),
|
||||
('A', JlreqClass::Ideographic),
|
||||
('z', JlreqClass::Ideographic),
|
||||
('1', JlreqClass::Ideographic),
|
||||
('〇', JlreqClass::Ideographic),
|
||||
('〒', JlreqClass::Ideographic),
|
||||
('※', JlreqClass::Ideographic),
|
||||
('①', JlreqClass::Ideographic),
|
||||
('㈱', JlreqClass::Ideographic),
|
||||
('○', JlreqClass::Ideographic),
|
||||
('★', JlreqClass::Ideographic),
|
||||
('→', JlreqClass::Ideographic),
|
||||
('Ⅳ', JlreqClass::Ideographic),
|
||||
('㏍', JlreqClass::Ideographic),
|
||||
('≒', JlreqClass::MathSymbol),
|
||||
('≡', JlreqClass::MathSymbol),
|
||||
('≪', JlreqClass::MathSymbol),
|
||||
('≈', JlreqClass::MathSymbol),
|
||||
('∝', JlreqClass::MathSymbol),
|
||||
('↔', JlreqClass::MathSymbol),
|
||||
('€', JlreqClass::PrefixedAbbreviation),
|
||||
('㎡', JlreqClass::PostfixedAbbreviation),
|
||||
('㏄', JlreqClass::PostfixedAbbreviation),
|
||||
('㌔', JlreqClass::PostfixedAbbreviation),
|
||||
('ℓ', JlreqClass::PostfixedAbbreviation),
|
||||
('。', JlreqClass::FullStop),
|
||||
('、', JlreqClass::Comma),
|
||||
('「', JlreqClass::OpeningBracket),
|
||||
('」', JlreqClass::ClosingBracket),
|
||||
('ァ', JlreqClass::SmallKana),
|
||||
('ッ', JlreqClass::SmallKana),
|
||||
('ー', JlreqClass::ProlongedSoundMark),
|
||||
('\u{30000}', JlreqClass::Ideographic),
|
||||
('\u{31350}', JlreqClass::Ideographic),
|
||||
];
|
||||
for (character, expected) in cases {
|
||||
assert_eq!(classify(character), expected, "wrong class for {character}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fast_classes_match_the_tables() {
|
||||
let fast = ('\u{4E00}'..='\u{9FFF}')
|
||||
.chain('\u{3400}'..='\u{4DBF}')
|
||||
.chain('a'..='z')
|
||||
.chain('A'..='Z');
|
||||
for c in fast {
|
||||
assert_eq!(classify(c), classify_by_tables(c), "{c:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ascii_and_latin_symbols_stay_western() {
|
||||
for character in ['(', ')', '[', ']', 'A', '©', '†', 'α'] {
|
||||
assert_eq!(classify(character), JlreqClass::Western, "{character}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_identical_inseparable_pairs_are_unbreakable() {
|
||||
assert!(!JlreqClass::Inseparable.forbids_line_start());
|
||||
assert!(break_allowed_between('あ', '…'));
|
||||
assert!(!break_allowed_between('…', '…'));
|
||||
assert!(!break_allowed_between('—', '—'));
|
||||
assert!(break_allowed_between('—', '…'));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn abbreviations_bind_only_to_numerals() {
|
||||
assert!(!JlreqClass::PostfixedAbbreviation.forbids_line_start());
|
||||
assert!(!JlreqClass::PrefixedAbbreviation.forbids_line_end());
|
||||
assert!(!break_allowed_between('0', '㎏'));
|
||||
assert!(!break_allowed_between('0', '%'));
|
||||
assert!(!break_allowed_between('$', '5'));
|
||||
assert!(!break_allowed_between('¥', '1'));
|
||||
assert!(break_allowed_between('#', 'は'));
|
||||
assert!(break_allowed_between('漢', '%'));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn breaks_follow_kinsoku_and_western_runs() {
|
||||
assert!(!break_allowed_between('あ', 'っ'));
|
||||
assert!(!break_allowed_between('「', 'あ'));
|
||||
assert!(!break_allowed_between('P', 'e'));
|
||||
assert!(!break_allowed_between('2', '0'));
|
||||
assert!(break_allowed_between('あ', 'い'));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn digit_runs_stay_together() {
|
||||
assert!(!break_allowed_between('2', '0'));
|
||||
assert!(!break_allowed_between('2', '0'));
|
||||
assert!(break_allowed_between('6', '年'));
|
||||
assert!(break_allowed_between('年', '2'));
|
||||
}
|
||||
}
|
||||
807
render-wasm/src/shapes/line_breaks.rs
Normal file
807
render-wasm/src/shapes/line_breaks.rs
Normal file
@ -0,0 +1,807 @@
|
||||
//! Layout-text rewrite for horizontal CJK line breaking.
|
||||
//!
|
||||
//! skia-safe exposes no ICU BreakIterator, so the layout policy steers
|
||||
//! skparagraph's breaker by editing the text handed to it. U+2060 WORD
|
||||
//! JOINER suppresses a break the policy forbids (Japanese kinsoku shori,
|
||||
//! Chinese and Korean punctuation, nonbreaking units and annotations).
|
||||
//! U+200B ZERO WIDTH SPACE reopens a break that UAX #14 forbids but the
|
||||
//! policy allows, such as before … and ‥ in JLREQ, or an emergency break
|
||||
//! inside an overlong unit. U+2028 LINE SEPARATOR keeps the original wrap
|
||||
//! budget around an indivisible unit that overflows its box. In Japanese
|
||||
//! text the same transform inserts the JLREQ quarter-em space at
|
||||
//! Japanese/Western boundaries and sets Western word spaces to one third em.
|
||||
//!
|
||||
//! Inserted characters shift every UTF-16 offset the laid-out paragraph
|
||||
//! reports (position-data, carets, selection rects). [`OffsetMap`]
|
||||
//! translates between original and shifted offsets. It depends only on the
|
||||
//! span texts, so any consumer can recompute it and match the builders.
|
||||
|
||||
/// Zero-width character; its UAX #14 class forbids a break on either side.
|
||||
pub const WORD_JOINER: char = '\u{2060}';
|
||||
/// ZERO WIDTH SPACE: opens a break UAX #14 forbids but JLREQ allows, before
|
||||
/// an inseparable mark such as … (JLREQ only keeps two of them together).
|
||||
pub const BREAK_OPPORTUNITY: char = '\u{200B}';
|
||||
/// Layout-only line separator used to retain the original wrap budget when
|
||||
/// an indivisible CJK unit needs more width than its container.
|
||||
pub const FORCED_LINE_BREAK: char = '\u{2028}';
|
||||
|
||||
/// True for the zero-width characters this pass inserts to steer breaks;
|
||||
/// they take no letter-spacing and count as no character.
|
||||
pub fn is_inserted_zero_width(ch: char) -> bool {
|
||||
ch == WORD_JOINER || ch == BREAK_OPPORTUNITY || ch == FORCED_LINE_BREAK
|
||||
}
|
||||
/// Unicode FOUR-PER-EM SPACE, used for the preferred Japanese/Western gap.
|
||||
pub const JAPANESE_WESTERN_SPACE: char = '\u{2005}';
|
||||
/// Unicode THREE-PER-EM SPACE, used for Western word spaces.
|
||||
pub const WESTERN_WORD_SPACE: char = '\u{2004}';
|
||||
/// Unicode IDEOGRAPHIC SPACE, the one-em space after a sentence-ending
|
||||
/// question or exclamation mark.
|
||||
pub const IDEOGRAPHIC_SPACE: char = '\u{3000}';
|
||||
|
||||
use super::jlreq::{classify, pair_rule, JlreqClass};
|
||||
use super::text_policy::TextLayoutPolicy;
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
/// One grapheme of a paragraph's text with the policy's rules for the
|
||||
/// boundary before it. The first grapheme allows both kinds of break.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct PolicyBoundary {
|
||||
/// UTF-16 offset of the grapheme in the text.
|
||||
pub utf16: usize,
|
||||
/// First character of the grapheme, which the policy rules read.
|
||||
pub first: char,
|
||||
/// `TextLayoutPolicy::break_allowed` before this grapheme.
|
||||
pub allowed: bool,
|
||||
/// `TextLayoutPolicy::emergency_break_allowed` before this grapheme.
|
||||
pub emergency: bool,
|
||||
}
|
||||
|
||||
/// Break boundaries of a whole paragraph text (all spans, transformed), so
|
||||
/// font and style runs cannot split a grapheme or create a boundary.
|
||||
#[derive(Debug, Clone, Default, PartialEq)]
|
||||
pub struct PolicyBoundaries {
|
||||
pub graphemes: Vec<PolicyBoundary>,
|
||||
/// UTF-16 length of the text.
|
||||
pub length: usize,
|
||||
}
|
||||
|
||||
impl PolicyBoundaries {
|
||||
pub fn new(text: &str, policy: TextLayoutPolicy) -> Self {
|
||||
let mut length = 0;
|
||||
let mut previous: Option<char> = None;
|
||||
let graphemes = text
|
||||
.graphemes(true)
|
||||
.map(|grapheme| {
|
||||
let first = grapheme.chars().next().unwrap();
|
||||
let boundary = PolicyBoundary {
|
||||
utf16: length,
|
||||
first,
|
||||
allowed: previous.is_none_or(|before| policy.break_allowed(before, first)),
|
||||
emergency: previous
|
||||
.is_none_or(|before| policy.emergency_break_allowed(before, first)),
|
||||
};
|
||||
previous = Some(first);
|
||||
length += grapheme.encode_utf16().count();
|
||||
boundary
|
||||
})
|
||||
.collect();
|
||||
Self { graphemes, length }
|
||||
}
|
||||
|
||||
/// (allowed, emergency) at a UTF-16 offset: a grapheme start, or the
|
||||
/// text end, which allows both. `None` inside a grapheme.
|
||||
pub fn at(&self, utf16: usize) -> Option<(bool, bool)> {
|
||||
if utf16 == self.length {
|
||||
return Some((true, true));
|
||||
}
|
||||
self.graphemes
|
||||
.binary_search_by_key(&utf16, |g| g.utf16)
|
||||
.ok()
|
||||
.map(|index| {
|
||||
(
|
||||
self.graphemes[index].allowed,
|
||||
self.graphemes[index].emergency,
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Translates between original UTF-16 offsets (the source-of-truth span text)
|
||||
/// and layout-text UTF-16 offsets (the text handed to skparagraph).
|
||||
#[derive(Debug, Clone, Default, PartialEq)]
|
||||
pub struct OffsetMap {
|
||||
/// Ascending shifted UTF-16 indices of inserted (not substituted) chars.
|
||||
inserted: Vec<usize>,
|
||||
}
|
||||
|
||||
impl OffsetMap {
|
||||
#[cfg(test)]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.inserted.is_empty()
|
||||
}
|
||||
|
||||
/// True when the character at a shifted offset was inserted by the
|
||||
/// transform.
|
||||
pub fn is_inserted(&self, shifted: usize) -> bool {
|
||||
self.inserted.binary_search(&shifted).is_ok()
|
||||
}
|
||||
|
||||
/// Original offset for a shifted offset. An offset on an inserted
|
||||
/// character resolves to the boundary where it was inserted.
|
||||
pub fn to_original(&self, shifted: usize) -> usize {
|
||||
shifted - self.inserted.partition_point(|&p| p < shifted)
|
||||
}
|
||||
|
||||
/// Shifted offset for an original offset. A boundary that received an
|
||||
/// inserted character resolves after it, so carets skip synthetic spacing.
|
||||
pub fn to_shifted(&self, original: usize) -> usize {
|
||||
// The i-th insertion lands before `original` when it sits at or
|
||||
// before `original + i`; `inserted[i] - i` never decreases, so the
|
||||
// count is a partition point.
|
||||
let (mut low, mut high) = (0, self.inserted.len());
|
||||
while low < high {
|
||||
let middle = (low + high) / 2;
|
||||
if self.inserted[middle] - middle <= original {
|
||||
low = middle + 1;
|
||||
} else {
|
||||
high = middle;
|
||||
}
|
||||
}
|
||||
original + low
|
||||
}
|
||||
}
|
||||
|
||||
/// Applies the horizontal Japanese layout-text transform (the `Japanese`
|
||||
/// policy of `apply_policy_with_breaks`): inserts WORD
|
||||
/// JOINER wherever a break would violate kinsoku, inserts a quarter-em space
|
||||
/// between Japanese letters and Western letters or digits, and sets
|
||||
/// breakable ASCII spaces to one third em. Span boundaries are transparent;
|
||||
/// grapheme clusters are not, so nothing lands inside one. `ruby_breaks[span]
|
||||
/// == Some(boundaries)` forbids breaks at every internal scalar boundary of
|
||||
/// that span except those UTF-16 offsets. Returns `None` when nothing changes.
|
||||
#[cfg(test)]
|
||||
fn apply_to_span_texts_with_ruby_breaks(
|
||||
span_texts: &[String],
|
||||
ruby_breaks: &[Option<Vec<usize>>],
|
||||
) -> Option<(Vec<String>, OffsetMap)> {
|
||||
apply_policy_to_span_texts(span_texts, ruby_breaks, TextLayoutPolicy::Japanese)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn apply_policy_to_span_texts(
|
||||
span_texts: &[String],
|
||||
ruby_breaks: &[Option<Vec<usize>>],
|
||||
policy: TextLayoutPolicy,
|
||||
) -> Option<(Vec<String>, OffsetMap)> {
|
||||
apply_policy_with_breaks(span_texts, ruby_breaks, &[], policy, &[], &[])
|
||||
}
|
||||
|
||||
/// `joined[span]` is true when the span continues the ruby base of the span
|
||||
/// before it, so its start is internal to that base too.
|
||||
pub fn apply_policy_with_breaks(
|
||||
span_texts: &[String],
|
||||
ruby_breaks: &[Option<Vec<usize>>],
|
||||
joined: &[bool],
|
||||
policy: TextLayoutPolicy,
|
||||
emergency_breaks: &[usize],
|
||||
forced_breaks: &[usize],
|
||||
) -> Option<(Vec<String>, OffsetMap)> {
|
||||
let jlreq = policy.uses_jlreq();
|
||||
let source = span_texts.concat();
|
||||
let boundaries: Vec<usize> = source.grapheme_indices(true).map(|(at, _)| at).collect();
|
||||
let mut source_byte = 0;
|
||||
let mut source_utf16 = 0;
|
||||
let mut inserted: Vec<usize> = Vec::new();
|
||||
let mut out: Vec<String> = Vec::with_capacity(span_texts.len());
|
||||
// Base character of the previous grapheme cluster, with its class.
|
||||
let mut prev: Option<(char, JlreqClass)> = None;
|
||||
let mut changed = false;
|
||||
// Running position in shifted UTF-16 coordinates.
|
||||
let mut shifted_pos: usize = 0;
|
||||
|
||||
for (span_index, text) in span_texts.iter().enumerate() {
|
||||
let mut shifted_text = String::with_capacity(text.len() + 4);
|
||||
let mut local_utf16 = 0usize;
|
||||
for c in text.chars() {
|
||||
let at_boundary = boundaries.binary_search(&source_byte).is_ok();
|
||||
source_byte += c.len_utf8();
|
||||
let source_at = source_utf16;
|
||||
source_utf16 += c.len_utf16();
|
||||
if !at_boundary {
|
||||
shifted_text.push(c);
|
||||
shifted_pos += c.len_utf16();
|
||||
local_utf16 += c.len_utf16();
|
||||
continue;
|
||||
}
|
||||
let current = (c, classify(c));
|
||||
let inside_base = local_utf16 > 0 || joined.get(span_index).copied().unwrap_or(false);
|
||||
let ruby_forbids_break = inside_base
|
||||
&& ruby_breaks
|
||||
.get(span_index)
|
||||
.and_then(Option::as_ref)
|
||||
.is_some_and(|breaks| !breaks.contains(&local_utf16));
|
||||
let forbid_break =
|
||||
ruby_forbids_break || prev.is_some_and(|p| policy.needs_joiner(p, current));
|
||||
let mut insert = |ch: char| {
|
||||
shifted_text.push(ch);
|
||||
inserted.push(shifted_pos);
|
||||
shifted_pos += 1;
|
||||
changed = true;
|
||||
};
|
||||
if jlreq && prev.is_some_and(|p| takes_em_space_after(p, current)) {
|
||||
insert(IDEOGRAPHIC_SPACE);
|
||||
}
|
||||
if jlreq && prev.is_some_and(|p| is_japanese_western_boundary(p, current)) {
|
||||
insert(JAPANESE_WESTERN_SPACE);
|
||||
}
|
||||
// After an inserted space, the joiner keeps the break blocked.
|
||||
if forced_breaks.binary_search(&source_at).is_ok() {
|
||||
insert(FORCED_LINE_BREAK);
|
||||
} else if forbid_break {
|
||||
insert(if emergency_breaks.binary_search(&source_at).is_ok() {
|
||||
BREAK_OPPORTUNITY
|
||||
} else {
|
||||
WORD_JOINER
|
||||
});
|
||||
} else if jlreq
|
||||
&& current.1 == JlreqClass::Inseparable
|
||||
&& prev.is_some_and(|p| pair_rule(p.1, current.1).break_allowed)
|
||||
{
|
||||
insert(BREAK_OPPORTUNITY);
|
||||
}
|
||||
let layout_char = if policy.normalizes_western_spaces() && c == ' ' {
|
||||
changed = true;
|
||||
WESTERN_WORD_SPACE
|
||||
} else {
|
||||
c
|
||||
};
|
||||
shifted_text.push(layout_char);
|
||||
shifted_pos += c.len_utf16();
|
||||
local_utf16 += c.len_utf16();
|
||||
prev = Some(current);
|
||||
}
|
||||
out.push(shifted_text);
|
||||
}
|
||||
|
||||
if !changed {
|
||||
return None;
|
||||
}
|
||||
Some((out, OffsetMap { inserted }))
|
||||
}
|
||||
|
||||
/// JLREQ §3.2.6 boundary between Japanese text and a Western letter or
|
||||
/// digit. Western symbols, brackets and emoji set solid.
|
||||
fn is_japanese_western_boundary(
|
||||
(before, before_class): (char, JlreqClass),
|
||||
(after, after_class): (char, JlreqClass),
|
||||
) -> bool {
|
||||
(before_class.is_japanese_text() && after_class.is_western_run() && after.is_alphanumeric())
|
||||
|| (before_class.is_western_run()
|
||||
&& before.is_alphanumeric()
|
||||
&& after_class.is_japanese_text())
|
||||
}
|
||||
|
||||
/// A question or exclamation mark that ends a sentence takes a one-em space
|
||||
/// (JLREQ §3.1.6), as in vertical layout.
|
||||
fn takes_em_space_after(
|
||||
(_, before_class): (char, JlreqClass),
|
||||
(_, after_class): (char, JlreqClass),
|
||||
) -> bool {
|
||||
before_class == JlreqClass::DividingPunctuation
|
||||
&& pair_rule(before_class, after_class).preferred_em >= 1.0
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::shapes::jlreq::{forbidden_at_line_end, forbidden_at_line_start};
|
||||
use skia_safe::textlayout::{
|
||||
FontCollection, ParagraphBuilder, ParagraphStyle, TextStyle, TypefaceFontProvider,
|
||||
};
|
||||
use skia_safe::FontMgr;
|
||||
|
||||
const TEST_FONT: &[u8] = include_bytes!("../fonts/sourcesanspro-regular.ttf");
|
||||
|
||||
fn strings(texts: &[&str]) -> Vec<String> {
|
||||
texts.iter().map(|t| t.to_string()).collect()
|
||||
}
|
||||
|
||||
fn apply(texts: &[&str]) -> (Vec<String>, OffsetMap) {
|
||||
apply_to_span_texts_with_ruby_breaks(&strings(texts), &[])
|
||||
.expect("expected kinsoku insertions")
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// Insertion
|
||||
// -----------------------------------------------------------------
|
||||
|
||||
#[test]
|
||||
fn western_word_space_uses_one_third_em_character() {
|
||||
let (texts, map) = apply(&["hello world"]);
|
||||
assert_eq!(texts, vec![format!("hello{WESTERN_WORD_SPACE}world")]);
|
||||
assert!(
|
||||
map.inserted.is_empty(),
|
||||
"substitution does not shift offsets"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_insertion_for_plain_cjk_text() {
|
||||
assert!(
|
||||
apply_to_span_texts_with_ruby_breaks(&strings(&["国境の長いトンネル"]), &[]).is_none()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inserts_before_forbidden_start_char() {
|
||||
let (texts, map) = apply(&["雪国。"]);
|
||||
assert_eq!(texts, vec!["雪国\u{2060}。".to_string()]);
|
||||
assert_eq!(map.inserted, vec![2]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inserts_after_forbidden_end_char() {
|
||||
let (texts, _) = apply(&["「雪"]);
|
||||
assert_eq!(texts, vec!["「\u{2060}雪".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_insertion_at_paragraph_start() {
|
||||
// A leading start-forbidden char has no break before it to suppress.
|
||||
let (texts, _) = apply(&["。あ。"]);
|
||||
assert_eq!(texts, vec!["。あ\u{2060}。".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insertion_spans_boundary() {
|
||||
// The pair (end of span 0, start of span 1) is evaluated; the
|
||||
// joiner lands at the head of span 1.
|
||||
let (texts, _) = apply(&["雪国", "。です"]);
|
||||
assert_eq!(
|
||||
texts,
|
||||
vec!["雪国".to_string(), "\u{2060}。です".to_string()]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inserts_quarter_em_at_japanese_western_boundaries() {
|
||||
let (texts, map) = apply(&["日本Penpot版"]);
|
||||
assert_eq!(
|
||||
texts,
|
||||
vec![format!(
|
||||
"日本{JAPANESE_WESTERN_SPACE}Penpot{JAPANESE_WESTERN_SPACE}版"
|
||||
)]
|
||||
);
|
||||
assert_eq!(map.inserted, vec![2, 9]);
|
||||
assert_eq!(map.to_original(map.to_shifted(2)), 2);
|
||||
assert_eq!(map.to_original(map.to_shifted(8)), 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn japanese_western_spacing_crosses_span_boundary() {
|
||||
let (texts, map) = apply(&["日本", "Penpot"]);
|
||||
assert_eq!(
|
||||
texts,
|
||||
vec![
|
||||
"日本".to_string(),
|
||||
format!("{JAPANESE_WESTERN_SPACE}Penpot")
|
||||
]
|
||||
);
|
||||
assert_eq!(map.inserted, vec![2]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_insertion_inside_a_variation_sequence() {
|
||||
assert!(apply_to_span_texts_with_ruby_breaks(&strings(&["葛\u{E0100}城"]), &[]).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_insertion_before_halfwidth_voiced_marks() {
|
||||
assert!(apply_to_span_texts_with_ruby_breaks(&strings(&["ガギ"]), &[]).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_insertion_inside_an_emoji_zwj_sequence() {
|
||||
assert!(
|
||||
apply_to_span_texts_with_ruby_breaks(&strings(&["👩\u{200D}💻\u{200D}あ"]), &[])
|
||||
.is_none()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn grapheme_extenders_keep_the_base_as_previous_character() {
|
||||
let (texts, _) = apply(&["葛\u{E0100}Penpot"]);
|
||||
assert_eq!(
|
||||
texts,
|
||||
vec![format!("葛\u{E0100}{JAPANESE_WESTERN_SPACE}Penpot")]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ruby_joiners_stay_outside_grapheme_clusters() {
|
||||
let texts = strings(&["葛\u{E0100}城"]);
|
||||
let (shifted, _) = apply_to_span_texts_with_ruby_breaks(&texts, &[Some(Vec::new())])
|
||||
.expect("expected a ruby joiner");
|
||||
assert_eq!(shifted, vec!["葛\u{E0100}\u{2060}城".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_japanese_western_space_beside_symbols() {
|
||||
assert!(apply_to_span_texts_with_ruby_breaks(&strings(&["あ😀い"]), &[]).is_none());
|
||||
assert!(apply_to_span_texts_with_ruby_breaks(&strings(&["(注)あ"]), &[]).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_width_digits_stay_together() {
|
||||
let (texts, _) = apply(&["2026年"]);
|
||||
assert_eq!(
|
||||
texts,
|
||||
vec!["2\u{2060}0\u{2060}2\u{2060}6年".to_string()]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn numerals_keep_their_unit() {
|
||||
let (texts, _) = apply(&["体重60㎏"]);
|
||||
assert_eq!(
|
||||
texts,
|
||||
vec![format!("体重{JAPANESE_WESTERN_SPACE}60\u{2060}㎏")]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prefixed_signs_bind_only_to_numerals() {
|
||||
let (texts, _) = apply(&["C# は"]);
|
||||
assert_eq!(texts, vec![format!("C#{WESTERN_WORD_SPACE}は")]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_line_may_start_with_an_inseparable_mark() {
|
||||
// UAX #14 forbids the break before …; the zero-width space reopens it.
|
||||
let (texts, _) = apply(&["あ…"]);
|
||||
assert_eq!(texts, vec!["あ\u{200B}…".to_string()]);
|
||||
let (texts, _) = apply(&["あ……"]);
|
||||
assert_eq!(texts, vec!["あ\u{200B}…\u{2060}…".to_string()]);
|
||||
let (texts, _) = apply(&["「……"]);
|
||||
assert_eq!(texts, vec!["「\u{2060}…\u{2060}…".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn japanese_western_space_follows_long_vowels_and_small_kana() {
|
||||
let (texts, _) = apply(&["ユーザーID"]);
|
||||
assert_eq!(
|
||||
texts,
|
||||
vec![format!(
|
||||
"ユ\u{2060}ーザ\u{2060}ー{JAPANESE_WESTERN_SPACE}ID"
|
||||
)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn japanese_western_space_keeps_a_forbidden_break_blocked() {
|
||||
let (texts, _) = apply(&["Excelっぽい"]);
|
||||
assert_eq!(
|
||||
texts,
|
||||
vec![format!("Excel{JAPANESE_WESTERN_SPACE}\u{2060}っぽい")]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dividing_punctuation_takes_an_em_space_before_text() {
|
||||
let (texts, _) = apply(&["え?はい"]);
|
||||
assert_eq!(texts, vec![format!("え\u{2060}?{IDEOGRAPHIC_SPACE}はい")]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dividing_punctuation_adds_no_space_in_a_sequence_or_before_a_typed_space() {
|
||||
let (texts, _) = apply(&["え!?はい"]);
|
||||
assert_eq!(
|
||||
texts,
|
||||
vec![format!("え\u{2060}!\u{2060}?{IDEOGRAPHIC_SPACE}はい")]
|
||||
);
|
||||
let (texts, _) = apply(&["え? はい"]);
|
||||
assert_eq!(texts, vec!["え\u{2060}? はい".to_string()]);
|
||||
let (texts, _) = apply(&["え?」"]);
|
||||
assert_eq!(texts, vec!["え\u{2060}?\u{2060}」".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn offset_map_lookups_match_a_linear_scan() {
|
||||
let linear_to_shifted = |inserted: &[usize], original: usize| {
|
||||
let mut shifted = original;
|
||||
for &p in inserted {
|
||||
if p <= shifted {
|
||||
shifted += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
shifted
|
||||
};
|
||||
for inserted in [
|
||||
vec![],
|
||||
vec![0],
|
||||
vec![2, 3, 4],
|
||||
vec![1, 5, 6, 10],
|
||||
vec![0, 1, 2],
|
||||
] {
|
||||
let map = OffsetMap {
|
||||
inserted: inserted.clone(),
|
||||
};
|
||||
for offset in 0..16 {
|
||||
assert_eq!(
|
||||
map.to_shifted(offset),
|
||||
linear_to_shifted(&inserted, offset),
|
||||
"to_shifted({offset}) for {inserted:?}"
|
||||
);
|
||||
assert_eq!(
|
||||
map.to_original(offset),
|
||||
offset - inserted.iter().take_while(|&&p| p < offset).count(),
|
||||
"to_original({offset}) for {inserted:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn consecutive_forbidden_chars() {
|
||||
let (texts, map) = apply(&["た」。"]);
|
||||
assert_eq!(texts, vec!["た\u{2060}」\u{2060}。".to_string()]);
|
||||
assert_eq!(map.inserted, vec![1, 3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn small_kana_and_prolonged_sound() {
|
||||
let (texts, _) = apply(&["コーヒー"]);
|
||||
assert_eq!(texts, vec!["コ\u{2060}ーヒ\u{2060}ー".to_string()]);
|
||||
let (texts, _) = apply(&["ちょっと"]);
|
||||
assert_eq!(texts, vec!["ち\u{2060}ょ\u{2060}っと".to_string()]);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// Offset map
|
||||
// -----------------------------------------------------------------
|
||||
|
||||
#[test]
|
||||
fn offset_map_round_trips_every_original_index() {
|
||||
let original = "「こんにちは」と彼は言った。『吾輩は猫である』(夏目漱石)!?コーヒー。";
|
||||
let (_, map) = apply(&[original]);
|
||||
let len = original.encode_utf16().count();
|
||||
for i in 0..=len {
|
||||
assert_eq!(
|
||||
map.to_original(map.to_shifted(i)),
|
||||
i,
|
||||
"round-trip failed at original index {i}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn offset_map_recovers_original_characters() {
|
||||
let original = "た」。あ";
|
||||
let (texts, map) = apply(&[original]);
|
||||
let shifted = texts.concat();
|
||||
let shifted_units: Vec<u16> = shifted.encode_utf16().collect();
|
||||
let original_units: Vec<u16> = original.encode_utf16().collect();
|
||||
for (s_idx, unit) in shifted_units.iter().enumerate() {
|
||||
if *unit == WORD_JOINER as u16 {
|
||||
continue;
|
||||
}
|
||||
assert_eq!(original_units[map.to_original(s_idx)], *unit);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn offset_map_on_joiner_resolves_to_boundary() {
|
||||
// "雪国。" → joiner at shifted 2; both sides of the joiner map
|
||||
// to original boundary 2.
|
||||
let (_, map) = apply(&["雪国。"]);
|
||||
assert_eq!(map.to_original(2), 2);
|
||||
assert_eq!(map.to_original(3), 2);
|
||||
assert_eq!(map.to_shifted(2), 3);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// Real skparagraph layout
|
||||
// -----------------------------------------------------------------
|
||||
|
||||
fn font_collection() -> FontCollection {
|
||||
let font_mgr = FontMgr::new();
|
||||
let typeface = font_mgr
|
||||
.new_from_data(skia_safe::Data::new_copy(TEST_FONT), None)
|
||||
.expect("failed to load test font");
|
||||
let mut provider = TypefaceFontProvider::new();
|
||||
provider.register_typeface(typeface, Some("TestFont"));
|
||||
let mut collection = FontCollection::new();
|
||||
collection.set_asset_font_manager(Some(provider.into()));
|
||||
collection.set_default_font_manager(FontMgr::new(), None);
|
||||
collection
|
||||
}
|
||||
|
||||
/// Lays out `text` at `width` and returns each line as a UTF-16
|
||||
/// (start, end) range.
|
||||
fn layout_lines(text: &str, width: f32, letter_spacing: f32) -> Vec<(usize, usize)> {
|
||||
let collection = font_collection();
|
||||
let paragraph_style = ParagraphStyle::default();
|
||||
let mut builder = ParagraphBuilder::new(¶graph_style, collection);
|
||||
let mut style = TextStyle::default();
|
||||
style.set_font_families(&["TestFont"]);
|
||||
style.set_font_size(20.0);
|
||||
style.set_letter_spacing(letter_spacing);
|
||||
builder.push_style(&style);
|
||||
builder.add_text(text);
|
||||
let mut paragraph = builder.build();
|
||||
paragraph.layout(width);
|
||||
paragraph
|
||||
.get_line_metrics()
|
||||
.iter()
|
||||
.map(|line| (line.start_index, line.end_index))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layout_breaks_before_an_ellipsis() {
|
||||
// JLREQ Fig 81: という……そういう breaks as という|……そういう; only the
|
||||
// two dots of …… stay together.
|
||||
let (texts, map) = apply(&["という……そういう"]);
|
||||
let shifted = texts.concat();
|
||||
assert_eq!(shifted.matches(BREAK_OPPORTUNITY).count(), 1);
|
||||
assert!(shifted.contains("う\u{200B}…\u{2060}…"));
|
||||
let lines = layout_lines(&shifted, 20.0 * 4.0 + 1.0, 0.0);
|
||||
let second = map.to_original(lines[1].0);
|
||||
assert_eq!(second, 3, "the second line starts at ……: {lines:?}");
|
||||
}
|
||||
|
||||
fn utf16_chars(text: &str) -> Vec<char> {
|
||||
// BMP-only fixtures: one char per UTF-16 unit.
|
||||
text.chars().collect()
|
||||
}
|
||||
|
||||
/// Asserts no line starts with a start-forbidden char nor ends with
|
||||
/// an end-forbidden char, in ORIGINAL text coordinates.
|
||||
fn assert_kinsoku_clean(original: &str, lines: &[(usize, usize)], map: &OffsetMap) {
|
||||
let chars = utf16_chars(original);
|
||||
for (i, (start, end)) in lines.iter().enumerate() {
|
||||
let orig_start = map.to_original(*start);
|
||||
let orig_end = map.to_original(*end);
|
||||
if i > 0 {
|
||||
let first = chars[orig_start];
|
||||
assert!(
|
||||
!forbidden_at_line_start(first),
|
||||
"line {i} starts with forbidden char {first} (text {original})"
|
||||
);
|
||||
}
|
||||
if i < lines.len() - 1 && orig_end > orig_start {
|
||||
// The last char actually rendered on the line (end may
|
||||
// include trailing whitespace-like positions).
|
||||
let last = chars[orig_end - 1];
|
||||
assert!(
|
||||
!forbidden_at_line_end(last),
|
||||
"line {i} ends with forbidden char {last} (text {original})"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Lines mapped back to original offsets must tile the original
|
||||
/// text exactly: contiguous, in order, full coverage.
|
||||
fn assert_lines_tile_original(original: &str, lines: &[(usize, usize)], map: &OffsetMap) {
|
||||
let mut expected_start = 0;
|
||||
for (start, end) in lines {
|
||||
let orig_start = map.to_original(*start);
|
||||
let orig_end = map.to_original(*end);
|
||||
assert_eq!(orig_start, expected_start, "line ranges must be contiguous");
|
||||
expected_start = orig_end;
|
||||
}
|
||||
assert_eq!(expected_start, original.encode_utf16().count());
|
||||
}
|
||||
|
||||
fn fixture_lines(original: &str, width: f32) -> (Vec<(usize, usize)>, OffsetMap) {
|
||||
let (texts, map) = apply(&[original]);
|
||||
let lines = layout_lines(&texts.concat(), width, 0.0);
|
||||
(lines, map)
|
||||
}
|
||||
|
||||
fn measure_width(text: &str) -> f32 {
|
||||
let collection = font_collection();
|
||||
let mut builder = ParagraphBuilder::new(&ParagraphStyle::default(), collection);
|
||||
let mut style = TextStyle::default();
|
||||
style.set_font_families(&["TestFont"]);
|
||||
style.set_font_size(20.0);
|
||||
builder.push_style(&style);
|
||||
builder.add_text(text);
|
||||
let mut p = builder.build();
|
||||
p.layout(f32::MAX);
|
||||
p.longest_line()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn joiner_is_zero_width_in_layout() {
|
||||
let plain = layout_lines("国国", f32::MAX, 0.0);
|
||||
let joined = layout_lines("国\u{2060}国", f32::MAX, 0.0);
|
||||
assert_eq!(plain.len(), 1);
|
||||
assert_eq!(joined.len(), 1);
|
||||
|
||||
let collection = font_collection();
|
||||
let measure = |text: &str| {
|
||||
let mut builder = ParagraphBuilder::new(&ParagraphStyle::default(), collection.clone());
|
||||
let mut style = TextStyle::default();
|
||||
style.set_font_families(&["TestFont"]);
|
||||
style.set_font_size(20.0);
|
||||
builder.push_style(&style);
|
||||
builder.add_text(text);
|
||||
let mut p = builder.build();
|
||||
p.layout(f32::MAX);
|
||||
p.longest_line()
|
||||
};
|
||||
let diff = (measure("国国") - measure("国\u{2060}国")).abs();
|
||||
assert!(diff < 0.01, "WORD JOINER must not add width, diff {diff}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn suppresses_breaks_on_kinsoku_fixtures() {
|
||||
let fixtures = [
|
||||
// kinsoku-line-start
|
||||
"これは長い文章です、句読点や閉じ括弧」が行頭に来てはいけません。小さい「ゃゅょっ」も同様です。",
|
||||
// kinsoku-line-end
|
||||
"開き括弧「や『それに(と[や【は行末に置けないので、次の行に送り込まれます。",
|
||||
// prolonged-sound
|
||||
"コーヒーとケーキ、サーバーとルーター。人々の時々の心。",
|
||||
// small-kana-sokuon
|
||||
"ちょっと待ってください。キャッシュとクッキーをチェックする。",
|
||||
// long-paragraph-wrap
|
||||
"国境の長いトンネルを抜けると雪国であった。夜の底が白くなった。信号所に汽車が止まった。向側の座席から娘が立って来て、島村の前のガラス窓を落した。雪の冷気が流れこんだ。",
|
||||
];
|
||||
// Vary the width to move the breaks. Each width must exceed the
|
||||
// longest joined run, or skparagraph falls back to an emergency
|
||||
// mid-run break.
|
||||
let char_width = measure_width("国");
|
||||
for width in [9.0, 12.0, 16.5, 24.0].map(|n: f32| n * char_width) {
|
||||
for original in fixtures {
|
||||
let (lines, map) = fixture_lines(original, width);
|
||||
assert!(lines.len() > 1, "fixture must wrap at width {width}");
|
||||
assert_kinsoku_clean(original, &lines, &map);
|
||||
assert_lines_tile_original(original, &lines, &map);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mixed_latin_cjk_fixture() {
|
||||
let original = "Penpotは2024年にWASMレンダラーを導入した。価格は¥1,500(税込)です!";
|
||||
let char_width = measure_width("国");
|
||||
for width in [9.0, 13.0, 18.0].map(|n: f32| n * char_width) {
|
||||
let (lines, map) = fixture_lines(original, width);
|
||||
assert_kinsoku_clean(original, &lines, &map);
|
||||
assert_lines_tile_original(original, &lines, &map);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn joiner_becomes_visible_under_letter_spacing() {
|
||||
// skparagraph applies letter-spacing per cluster, INCLUDING the
|
||||
// zero-width joiner, which would double the tracking at every
|
||||
// suppressed break, so the horizontal builder gives inserted
|
||||
// joiners zero letter-spacing. If this test fails (Skia stops
|
||||
// spacing ignorables), that special style can go.
|
||||
let collection = font_collection();
|
||||
let measure = |text: &str| {
|
||||
let mut builder = ParagraphBuilder::new(&ParagraphStyle::default(), collection.clone());
|
||||
let mut style = TextStyle::default();
|
||||
style.set_font_families(&["TestFont"]);
|
||||
style.set_font_size(20.0);
|
||||
style.set_letter_spacing(5.0);
|
||||
builder.push_style(&style);
|
||||
builder.add_text(text);
|
||||
let mut p = builder.build();
|
||||
p.layout(f32::MAX);
|
||||
p.longest_line()
|
||||
};
|
||||
let diff = (measure("国\u{2060}国") - measure("国国")).abs();
|
||||
assert!(
|
||||
diff > 0.01,
|
||||
"letter-spacing no longer affects the joiner; the joiner \
|
||||
style in add_text_with_sheds can be removed"
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -13,7 +13,7 @@ use crate::error::Result;
|
||||
use crate::shapes;
|
||||
use crate::shapes::{
|
||||
ConstraintH, ConstraintV, Frame, Group, GrowType, Layout, Modifier, PixelPrecision, Shape,
|
||||
TransformEntry, TransformEntrySource, Type,
|
||||
TransformEntry, TransformEntrySource, Type, VerticalAlign,
|
||||
};
|
||||
use crate::state::{ShapesPoolRef, State};
|
||||
use crate::uuid::Uuid;
|
||||
@ -268,15 +268,34 @@ fn propagate_transform(
|
||||
);
|
||||
match text_content.grow_type() {
|
||||
GrowType::AutoHeight => {
|
||||
let width_before = text_content.size.width;
|
||||
let height_before = text_content.size.height;
|
||||
let new_height = if width_changed {
|
||||
let (new_width, new_height) = if text_content.is_vertical() {
|
||||
// Vertical auto-height keeps the physical height (the
|
||||
// column wrap axis) and grows width as columns are added.
|
||||
if height_changed {
|
||||
let mut clone = text_content.clone();
|
||||
clone.update_layout(resized_selrect);
|
||||
(clone.size.width, shape_bounds_after.height())
|
||||
} else {
|
||||
(width_before, shape_bounds_after.height())
|
||||
}
|
||||
} else if width_changed {
|
||||
let mut clone = text_content.clone();
|
||||
clone.update_layout(resized_selrect);
|
||||
clone.size.height
|
||||
(shape_bounds_after.width(), clone.size.height)
|
||||
} else {
|
||||
height_before
|
||||
(shape_bounds_after.width(), height_before)
|
||||
};
|
||||
if !is_close_to(height_before, new_height) && reflowed_shapes.insert(shape.id) {
|
||||
// Reflow only when the grow axis (the WASM-computed
|
||||
// dimension) changes; the resize itself sets the wrap
|
||||
// axis.
|
||||
let grow_axis_changed = if text_content.is_vertical() {
|
||||
!is_close_to(width_before, new_width)
|
||||
} else {
|
||||
!is_close_to(height_before, new_height)
|
||||
};
|
||||
if grow_axis_changed && reflowed_shapes.insert(shape.id) {
|
||||
entries.push_back(Modifier::reflow(shape.id, false));
|
||||
|
||||
if let Some(parent_id) = shape.parent_id {
|
||||
@ -287,12 +306,25 @@ fn propagate_transform(
|
||||
}
|
||||
}
|
||||
}
|
||||
let resize_transform = math::resize_matrix(
|
||||
let mut resize_transform = math::resize_matrix(
|
||||
&shape_bounds_after,
|
||||
&shape_bounds_after,
|
||||
shape_bounds_after.width(),
|
||||
new_width,
|
||||
new_height,
|
||||
);
|
||||
if text_content.is_vertical() {
|
||||
// Reflow adds columns toward the block end. Keep the
|
||||
// right/center/left anchor set by vertical alignment.
|
||||
let anchor = match shape.vertical_align() {
|
||||
VerticalAlign::Top => shape_bounds_after.ne,
|
||||
VerticalAlign::Center => {
|
||||
(shape_bounds_after.nw + shape_bounds_after.ne) * 0.5
|
||||
}
|
||||
VerticalAlign::Bottom => shape_bounds_after.nw,
|
||||
};
|
||||
resize_transform
|
||||
.post_translate(anchor - resize_transform.map_point(anchor));
|
||||
}
|
||||
shape_bounds_after = shape_bounds_after.transform(&resize_transform);
|
||||
transform.post_concat(&resize_transform);
|
||||
}
|
||||
@ -580,6 +612,50 @@ mod tests {
|
||||
use crate::shapes::*;
|
||||
use crate::state::ShapesPool;
|
||||
|
||||
#[test]
|
||||
fn vertical_auto_height_resize_keeps_the_right_edge() {
|
||||
let mut resources =
|
||||
crate::render::RenderResources::try_new_headless().expect("headless resources");
|
||||
let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources);
|
||||
let rect = math::Rect::from_xywh(10.0, 20.0, 100.0, 200.0);
|
||||
let mut content = TextContent::new(rect, GrowType::AutoHeight);
|
||||
let mut paragraph = Paragraph::new(
|
||||
TextAlign::Left,
|
||||
TextDirection::LTR,
|
||||
None,
|
||||
None,
|
||||
1.2,
|
||||
0.0,
|
||||
vec![TextSpan {
|
||||
text: "あいうえおかきくけこ".to_string(),
|
||||
font_size: 20.0,
|
||||
..TextSpan::default()
|
||||
}],
|
||||
);
|
||||
paragraph.set_writing_mode(WritingMode::VerticalRl);
|
||||
content.add_paragraph(paragraph);
|
||||
let size = content.update_layout(rect);
|
||||
let id = Uuid::new_v4();
|
||||
let mut state = State::new();
|
||||
state.shapes.initialize(1);
|
||||
let shape = state.shapes.add_shape(id);
|
||||
shape.set_selrect(10.0, 20.0, 10.0 + size.width, 220.0);
|
||||
shape.set_shape_type(Type::Text(content));
|
||||
let before = shape.bounds();
|
||||
let transform = math::resize_matrix(&before, &before, size.width, 60.0);
|
||||
let result = propagate_modifiers(
|
||||
&state,
|
||||
&[TransformEntry::from_input(id, transform)],
|
||||
PixelPrecision::Disabled,
|
||||
)
|
||||
.unwrap();
|
||||
let transform = result.iter().find(|e| e.id == id).unwrap().transform;
|
||||
let after = before.transform(&transform);
|
||||
assert!(after.width() > before.width());
|
||||
assert!(is_close_to(after.height(), 60.0));
|
||||
assert!(is_close_to(after.ne.x, before.ne.x));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_propagate_shape() {
|
||||
let parent_id = Uuid::new_v4();
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
164
render-wasm/src/shapes/text_allocation.rs
Normal file
164
render-wasm/src/shapes/text_allocation.rs
Normal file
@ -0,0 +1,164 @@
|
||||
//! Equal division of an adjustment over capped opportunities, shared by the
|
||||
//! horizontal and vertical justification and squeezing passes.
|
||||
|
||||
use crate::shapes::jlreq::{pair_rule, JlreqClass};
|
||||
use crate::shapes::text_policy::TextLayoutPolicy;
|
||||
|
||||
/// Amounts below this count as zero when dividing adjustments.
|
||||
const ALLOCATION_EPSILON: f32 = 0.0001;
|
||||
|
||||
/// Divide `amount` equally across capped opportunities, redistributing the
|
||||
/// remainder whenever one opportunity reaches its cap.
|
||||
pub(crate) fn capped_equal_allocations(
|
||||
capacities: &[(usize, f32)],
|
||||
amount: f32,
|
||||
) -> Vec<(usize, f32)> {
|
||||
let mut allocations: Vec<(usize, f32)> = capacities.iter().map(|(i, _)| (*i, 0.0)).collect();
|
||||
let mut remaining = amount.max(0.0);
|
||||
while remaining > ALLOCATION_EPSILON {
|
||||
let active: Vec<usize> = capacities
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(slot, (_, cap))| {
|
||||
(allocations[slot].1 + ALLOCATION_EPSILON < *cap).then_some(slot)
|
||||
})
|
||||
.collect();
|
||||
if active.is_empty() {
|
||||
break;
|
||||
}
|
||||
let share = remaining / active.len() as f32;
|
||||
let mut used = 0.0;
|
||||
for slot in active {
|
||||
let cap = capacities[slot].1;
|
||||
let delta = share.min(cap - allocations[slot].1);
|
||||
allocations[slot].1 += delta;
|
||||
used += delta;
|
||||
}
|
||||
if used <= ALLOCATION_EPSILON {
|
||||
break;
|
||||
}
|
||||
remaining -= used;
|
||||
}
|
||||
allocations
|
||||
}
|
||||
|
||||
/// Priority and pixel cap of the justification gap between `before` and
|
||||
/// `after`, at the smaller adjacent font size `em`, or `None` when the gap
|
||||
/// takes no expansion. Distribution (`distribute_budget`, the line or column
|
||||
/// budget) fills every gap at one priority, capped only by that budget;
|
||||
/// otherwise the policy bounds the gap (`TextLayoutPolicy::expansion_em`).
|
||||
pub(crate) fn bounded_expansion(
|
||||
policy: TextLayoutPolicy,
|
||||
before: char,
|
||||
after: char,
|
||||
em: f32,
|
||||
distribute_budget: Option<f32>,
|
||||
) -> Option<(u8, f32)> {
|
||||
let (priority, capacity) = match distribute_budget {
|
||||
Some(budget) => (1, budget / em.max(0.01)),
|
||||
None => policy.expansion_em(before, after),
|
||||
};
|
||||
(priority > 0).then_some((priority, capacity * em))
|
||||
}
|
||||
|
||||
/// Divide `slack` over bounded justification `opportunities` (index,
|
||||
/// priority, cap in px): priority 1 first, then priority 2 with what is
|
||||
/// left, equally within a priority. Returns (index, amount) per allocation.
|
||||
pub(crate) fn allocate_bounded_expansion(
|
||||
opportunities: &[(usize, u8, f32)],
|
||||
slack: f32,
|
||||
) -> Vec<(usize, f32)> {
|
||||
let mut remaining = slack;
|
||||
let mut allocations = Vec::new();
|
||||
for priority in 1..=2 {
|
||||
let caps: Vec<_> = opportunities
|
||||
.iter()
|
||||
.filter(|(_, p, _)| *p == priority)
|
||||
.map(|(index, _, cap)| (*index, *cap))
|
||||
.collect();
|
||||
for (index, amount) in capped_equal_allocations(&caps, remaining) {
|
||||
allocations.push((index, amount));
|
||||
remaining -= amount;
|
||||
}
|
||||
}
|
||||
allocations
|
||||
}
|
||||
|
||||
/// Priority and pixel cap of the JLREQ justification gap between classes
|
||||
/// `before` and `after`, which already hold `spacing_em` of pair spacing, at
|
||||
/// the smaller adjacent font size `em`; `None` when the pair table gives the
|
||||
/// gap no expansion. Both axes classify their gaps with this.
|
||||
pub(crate) fn jlreq_expansion_gap(
|
||||
before: JlreqClass,
|
||||
after: JlreqClass,
|
||||
spacing_em: f32,
|
||||
em: f32,
|
||||
) -> Option<(u8, f32)> {
|
||||
let rule = pair_rule(before, after);
|
||||
(rule.expand_priority > 0).then(|| {
|
||||
(
|
||||
rule.expand_priority,
|
||||
(rule.maximum_em - spacing_em).max(0.0) * em,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// JLREQ ordered expansion (§3.8.4) of one line or column's `slack` over
|
||||
/// its `gaps` (index, priority, cap in px) from `jlreq_expansion_gap`:
|
||||
/// stages 1–3 widen gaps in priority order, equally within a stage and up
|
||||
/// to each cap; a fourth stage spreads what is left evenly over every gap.
|
||||
/// Returns (index, amount) per allocation; an index may repeat.
|
||||
pub(crate) fn jlreq_ordered_expansion(gaps: &[(usize, u8, f32)], slack: f32) -> Vec<(usize, f32)> {
|
||||
let mut remaining = slack.max(0.0);
|
||||
let mut allocations = Vec::new();
|
||||
for priority in 1..=3 {
|
||||
let caps: Vec<(usize, f32)> = gaps
|
||||
.iter()
|
||||
.filter(|(_, p, _)| *p == priority)
|
||||
.map(|(index, _, cap)| (*index, *cap))
|
||||
.collect();
|
||||
for (index, amount) in capped_equal_allocations(&caps, remaining) {
|
||||
allocations.push((index, amount));
|
||||
remaining -= amount;
|
||||
}
|
||||
}
|
||||
if remaining > ALLOCATION_EPSILON && !gaps.is_empty() {
|
||||
let extra = remaining / gaps.len() as f32;
|
||||
allocations.extend(gaps.iter().map(|(index, _, _)| (*index, extra)));
|
||||
}
|
||||
allocations
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn ordered_expansion_fills_each_stage_before_the_next() {
|
||||
// Inter-script (stage 2, half an em) before solid kana (stage 3).
|
||||
let script = jlreq_expansion_gap(JlreqClass::Hiragana, JlreqClass::Western, 0.25, 20.0);
|
||||
let kana = jlreq_expansion_gap(JlreqClass::Hiragana, JlreqClass::Hiragana, 0.0, 20.0);
|
||||
assert_eq!(script, Some((2, 5.0)));
|
||||
assert_eq!(kana, Some((3, 5.0)));
|
||||
assert_eq!(
|
||||
jlreq_expansion_gap(JlreqClass::Comma, JlreqClass::Hiragana, 0.5, 20.0),
|
||||
None,
|
||||
"punctuation aki takes no justification"
|
||||
);
|
||||
let gaps = [(0, 3, 5.0), (1, 2, 5.0), (2, 3, 5.0)];
|
||||
let total = |allocations: &[(usize, f32)], index: usize| -> f32 {
|
||||
allocations
|
||||
.iter()
|
||||
.filter(|(at, _)| *at == index)
|
||||
.map(|(_, amount)| amount)
|
||||
.sum()
|
||||
};
|
||||
let small = jlreq_ordered_expansion(&gaps, 4.0);
|
||||
assert_eq!((total(&small, 0), total(&small, 1)), (0.0, 4.0));
|
||||
let medium = jlreq_ordered_expansion(&gaps, 9.0);
|
||||
assert_eq!((total(&medium, 0), total(&medium, 1)), (2.0, 5.0));
|
||||
let large = jlreq_ordered_expansion(&gaps, 18.0);
|
||||
assert!((total(&large, 0) - 6.0).abs() < 0.001);
|
||||
assert!((total(&large, 1) - 6.0).abs() < 0.001);
|
||||
}
|
||||
}
|
||||
256
render-wasm/src/shapes/text_attrs.rs
Normal file
256
render-wasm/src/shapes/text_attrs.rs
Normal file
@ -0,0 +1,256 @@
|
||||
//! Typographic span and paragraph attributes shared by the horizontal and
|
||||
//! vertical engines: writing mode, orientation, emphasis, decoration
|
||||
//! positions, annotation clearance, font features, line adjustment and the
|
||||
//! ruby presentation enums.
|
||||
|
||||
use crate::shapes::jlreq::classify;
|
||||
use crate::shapes::text_policy::TextLayoutPolicy;
|
||||
|
||||
pub const EMPHASIS_FONT_SCALE: f32 = 0.5;
|
||||
|
||||
/// Whether an emphasis mark goes on `character`: never on whitespace nor on
|
||||
/// the punctuation JLREQ leaves unmarked.
|
||||
pub(crate) fn emphasis_char_allowed(character: char) -> bool {
|
||||
!character.is_whitespace() && !classify(character).is_emphasis_prohibited()
|
||||
}
|
||||
|
||||
/// Block flow direction of a paragraph. Horizontal uses skparagraph;
|
||||
/// vertical-rl uses the custom vertical pass: columns top to bottom,
|
||||
/// advancing right to left.
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Default)]
|
||||
pub enum WritingMode {
|
||||
#[default]
|
||||
HorizontalTb,
|
||||
VerticalRl,
|
||||
}
|
||||
|
||||
/// How a line slightly too long for its box is fitted (JLREQ §3.8.1; Adobe's
|
||||
/// Kinsoku Type). Squeezing reduces punctuation aki to keep the next
|
||||
/// character; pushing out moves characters to the next line. Alignment then
|
||||
/// decides what happens to the space a line leaves over.
|
||||
// The variants are Adobe's Kinsoku Type values and the JS keys.
|
||||
#[allow(clippy::enum_variant_names)]
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Default)]
|
||||
pub enum LineAdjustment {
|
||||
/// Squeeze first; push out when the aki cannot absorb the overflow.
|
||||
#[default]
|
||||
PushInFirst,
|
||||
/// Push out first; squeeze only when kinsoku leaves no other break.
|
||||
PushOutFirst,
|
||||
/// Never squeeze.
|
||||
PushOutOnly,
|
||||
}
|
||||
|
||||
/// Glyph orientation inside vertical flow: `Mixed` rotates non-CJK runs
|
||||
/// sideways, `Upright` keeps every character upright. Ignored in
|
||||
/// horizontal writing.
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Default)]
|
||||
pub enum TextOrientation {
|
||||
#[default]
|
||||
Mixed,
|
||||
Upright,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Default)]
|
||||
pub enum TextCombineUpright {
|
||||
#[default]
|
||||
None,
|
||||
All,
|
||||
/// Runs of up to 4 ASCII or full-width digits combine into one upright
|
||||
/// cell, so a lone digit stands upright (CSS `digits`).
|
||||
Digits,
|
||||
/// Like `Digits`, for runs of up to 2 (CSS `digits 2`).
|
||||
Digits2,
|
||||
/// Like `Digits`, for runs of up to 3.
|
||||
Digits3,
|
||||
}
|
||||
|
||||
impl TextCombineUpright {
|
||||
/// Longest digit run that combines, when digits mode is active.
|
||||
pub fn digits_max(self) -> Option<usize> {
|
||||
match self {
|
||||
TextCombineUpright::Digits => Some(4),
|
||||
TextCombineUpright::Digits2 => Some(2),
|
||||
TextCombineUpright::Digits3 => Some(3),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Independent emphasis positions in the two writing directions.
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Default)]
|
||||
pub enum TextEmphasisPosition {
|
||||
#[default]
|
||||
Auto,
|
||||
OverRight,
|
||||
OverLeft,
|
||||
UnderRight,
|
||||
UnderLeft,
|
||||
}
|
||||
|
||||
impl TextEmphasisPosition {
|
||||
/// Side of the marks; `Auto` takes the default of the paragraph policy.
|
||||
pub fn side(self, vertical: bool, policy: TextLayoutPolicy) -> AnnotationSide {
|
||||
let under = if vertical {
|
||||
matches!(self, Self::OverLeft | Self::UnderLeft)
|
||||
} else {
|
||||
matches!(self, Self::UnderRight | Self::UnderLeft)
|
||||
|| (self == Self::Auto && policy.uses_clreq_positions())
|
||||
};
|
||||
if under {
|
||||
AnnotationSide::Under
|
||||
} else {
|
||||
AnnotationSide::Over
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// CSS underline position: horizontal depth and vertical side are independent.
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Default)]
|
||||
pub enum TextUnderlinePosition {
|
||||
#[default]
|
||||
Auto,
|
||||
Under,
|
||||
Left,
|
||||
Right,
|
||||
UnderLeft,
|
||||
UnderRight,
|
||||
}
|
||||
|
||||
impl TextUnderlinePosition {
|
||||
pub fn side(self) -> AnnotationSide {
|
||||
if matches!(self, Self::Right | Self::UnderRight) {
|
||||
AnnotationSide::Over
|
||||
} else {
|
||||
AnnotationSide::Under
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a horizontal underline sits below the ink; `Auto` takes the
|
||||
/// default of the paragraph policy.
|
||||
pub fn below_ink(self, policy: TextLayoutPolicy) -> bool {
|
||||
matches!(self, Self::Under | Self::UnderLeft | Self::UnderRight)
|
||||
|| (self == Self::Auto && policy.uses_clreq_positions())
|
||||
}
|
||||
}
|
||||
|
||||
/// Emphasis mark (圏点 / bouten) applied per span, mirroring CSS
|
||||
/// `text-emphasis-style`. Position is a separate style with a language default.
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Default)]
|
||||
pub enum TextEmphasis {
|
||||
#[default]
|
||||
None,
|
||||
FilledDot,
|
||||
OpenDot,
|
||||
FilledCircle,
|
||||
OpenCircle,
|
||||
FilledSesame,
|
||||
OpenSesame,
|
||||
}
|
||||
|
||||
impl TextEmphasis {
|
||||
pub fn is_none(self) -> bool {
|
||||
matches!(self, TextEmphasis::None)
|
||||
}
|
||||
|
||||
/// The glyph drawn as the emphasis mark, following the CSS
|
||||
/// `text-emphasis-style` character mapping.
|
||||
pub fn mark_char(self) -> Option<char> {
|
||||
match self {
|
||||
TextEmphasis::None => None,
|
||||
TextEmphasis::FilledDot => Some('•'),
|
||||
TextEmphasis::OpenDot => Some('◦'),
|
||||
TextEmphasis::FilledCircle => Some('●'),
|
||||
TextEmphasis::OpenCircle => Some('○'),
|
||||
TextEmphasis::FilledSesame => Some('﹅'),
|
||||
TextEmphasis::OpenSesame => Some('﹆'),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Default)]
|
||||
pub enum FontFeatures {
|
||||
#[default]
|
||||
None,
|
||||
Palt,
|
||||
Vpal,
|
||||
}
|
||||
|
||||
/// Whether ruby and emphasis on the same side stack. `Auto` places emphasis
|
||||
/// outside over-side ruby and reserves room for both; `None` lets them share
|
||||
/// one layer.
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Default)]
|
||||
pub enum AnnotationClearance {
|
||||
#[default]
|
||||
None,
|
||||
Auto,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Default)]
|
||||
pub enum RubySize {
|
||||
#[default]
|
||||
Half,
|
||||
Third,
|
||||
Quarter,
|
||||
}
|
||||
|
||||
impl RubySize {
|
||||
pub fn scale(self) -> f32 {
|
||||
match self {
|
||||
Self::Half => 0.5,
|
||||
Self::Third => 1.0 / 3.0,
|
||||
Self::Quarter => 0.25,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Default)]
|
||||
pub enum RubyAlign {
|
||||
#[default]
|
||||
SpaceAround,
|
||||
Center,
|
||||
Start,
|
||||
SpaceBetween,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Default)]
|
||||
pub enum RubyOverhang {
|
||||
#[default]
|
||||
Auto,
|
||||
None,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Default)]
|
||||
pub enum AnnotationSide {
|
||||
#[default]
|
||||
Over,
|
||||
Under,
|
||||
}
|
||||
|
||||
impl AnnotationClearance {
|
||||
pub fn is_auto(self) -> bool {
|
||||
matches!(self, AnnotationClearance::Auto)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn emphasis_excludes_whitespace_and_japanese_punctuation() {
|
||||
for character in " \t\n、。,.「」『』()[]【】〔〕〈〉《》‘’“”".chars()
|
||||
{
|
||||
assert!(
|
||||
!emphasis_char_allowed(character),
|
||||
"emphasis must skip {character:?}"
|
||||
);
|
||||
}
|
||||
for character in "漢あA1・!?".chars() {
|
||||
assert!(
|
||||
emphasis_char_allowed(character),
|
||||
"emphasis should mark {character:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
218
render-wasm/src/shapes/text_horizontal/emphasis.rs
Normal file
218
render-wasm/src/shapes/text_horizontal/emphasis.rs
Normal file
@ -0,0 +1,218 @@
|
||||
use super::*;
|
||||
use crate::shapes::text_paragraph::LayoutParagraph;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct HorizontalEmphasisPlacement {
|
||||
pub(crate) span: usize,
|
||||
/// UTF-16 range of the base character in the span's transformed text.
|
||||
pub(crate) range: std::ops::Range<usize>,
|
||||
pub(crate) mark: char,
|
||||
/// Base character rect in the laid-out paragraph.
|
||||
pub(crate) rect: skia::Rect,
|
||||
/// Top of the base character's em box (`horizontal_base_em_top`).
|
||||
pub(crate) em_top: f32,
|
||||
}
|
||||
|
||||
/// Place one emphasis mark beside each eligible character, from the laid-out
|
||||
/// rect of each character so marks follow SkParagraph's wrapping and bidi.
|
||||
pub(crate) fn horizontal_emphasis_placements(
|
||||
paragraph: &Paragraph,
|
||||
offsets: &HorizontalOffsets,
|
||||
laid_out: &LayoutParagraph,
|
||||
) -> Vec<HorizontalEmphasisPlacement> {
|
||||
let offset_map = &offsets.offset_map;
|
||||
let lines = laid_out.get_line_metrics();
|
||||
let mut placements = Vec::new();
|
||||
|
||||
for range in offsets.ranges.iter().filter(|range| !range.warichu) {
|
||||
let Some(span) = paragraph.children().get(range.span) else {
|
||||
continue;
|
||||
};
|
||||
let Some(mark) = span.text_emphasis.mark_char() else {
|
||||
continue;
|
||||
};
|
||||
let transformed = span.apply_text_transform();
|
||||
let mut local_utf16 = 0usize;
|
||||
for character in transformed.chars() {
|
||||
let next_utf16 = local_utf16 + character.len_utf16();
|
||||
if emphasis_char_allowed(character) {
|
||||
let shifted_start = offset_map.to_shifted(range.source_start + local_utf16);
|
||||
let shifted_end = offset_map.to_shifted(range.source_start + next_utf16);
|
||||
let builder_start =
|
||||
range.builder_start + shifted_start.saturating_sub(range.shifted_start);
|
||||
let builder_end =
|
||||
range.builder_start + shifted_end.saturating_sub(range.shifted_start);
|
||||
let scalar_rect = laid_out
|
||||
.get_rects_for_range(
|
||||
builder_start..builder_end,
|
||||
RectHeightStyle::Tight,
|
||||
RectWidthStyle::Tight,
|
||||
)
|
||||
.into_iter()
|
||||
.map(|textbox| textbox.rect)
|
||||
.reduce(|mut rect, next| {
|
||||
rect.join(next);
|
||||
rect
|
||||
});
|
||||
if let Some(rect) = scalar_rect {
|
||||
placements.push(HorizontalEmphasisPlacement {
|
||||
span: range.span,
|
||||
range: local_utf16..next_utf16,
|
||||
mark,
|
||||
rect,
|
||||
em_top: horizontal_base_em_top(
|
||||
laid_out,
|
||||
&lines,
|
||||
builder_start,
|
||||
span.font_size,
|
||||
rect,
|
||||
),
|
||||
});
|
||||
}
|
||||
}
|
||||
local_utf16 = next_utf16;
|
||||
}
|
||||
}
|
||||
placements
|
||||
}
|
||||
|
||||
/// One emphasis mark shaped at its span's style. Paint, bounds and position
|
||||
/// data all place this measurement, so they agree on where the mark is.
|
||||
pub(crate) struct HorizontalEmphasisMark {
|
||||
paragraph: LayoutParagraph,
|
||||
/// Tight rect of the mark in `paragraph`.
|
||||
pub(crate) ink: skia::Rect,
|
||||
}
|
||||
|
||||
impl HorizontalEmphasisMark {
|
||||
/// Shape `mark` at the style of `range` in the laid-out paragraph.
|
||||
pub(crate) fn measure(
|
||||
laid_out: &LayoutParagraph,
|
||||
range: &HorizontalSpanRange,
|
||||
span: &TextSpan,
|
||||
mark: char,
|
||||
) -> Option<Self> {
|
||||
let mut style = horizontal_span_style(laid_out, range)?;
|
||||
style.set_font_size(span.font_size * EMPHASIS_FONT_SCALE);
|
||||
style.set_height(1.0);
|
||||
style.set_height_override(true);
|
||||
style.set_letter_spacing(0.0);
|
||||
let paragraph = mini_paragraph(&mark.to_string(), &style, get_font_collection());
|
||||
let ink = paragraph
|
||||
.get_rects_for_range(
|
||||
0..mark.len_utf16(),
|
||||
RectHeightStyle::Tight,
|
||||
RectWidthStyle::Tight,
|
||||
)
|
||||
.into_iter()
|
||||
.map(|textbox| textbox.rect)
|
||||
.reduce(|mut rect, next| {
|
||||
rect.join(next);
|
||||
rect
|
||||
})
|
||||
.unwrap_or_else(|| skia::Rect::from_wh(paragraph.longest_line(), paragraph.height()));
|
||||
Some(Self { paragraph, ink })
|
||||
}
|
||||
}
|
||||
|
||||
/// Box of a horizontal emphasis mark whose ink is `ink`, in the laid-out
|
||||
/// paragraph: centred beside its base character on the selected side,
|
||||
/// outside stacked annotations. This is exactly where the mark paints.
|
||||
pub(crate) fn horizontal_emphasis_mark_box(
|
||||
span: &TextSpan,
|
||||
placement: &HorizontalEmphasisPlacement,
|
||||
ink: &skia::Rect,
|
||||
) -> skia::Rect {
|
||||
let offset = span.emphasis_outside_offset(false);
|
||||
let top = if span.emphasis_side(false) == AnnotationSide::Over {
|
||||
placement.em_top - offset - ink.height()
|
||||
} else {
|
||||
placement.rect.bottom() + offset
|
||||
};
|
||||
skia::Rect::from_xywh(
|
||||
placement.rect.center_x() - ink.width() / 2.0,
|
||||
top,
|
||||
ink.width(),
|
||||
ink.height(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Em box of a mark painted in `ink_box`, centred on it. Position data
|
||||
/// carries it: SVG export draws the mark at a font size of its width.
|
||||
pub(crate) fn horizontal_emphasis_mark_em_box(span: &TextSpan, ink_box: &skia::Rect) -> skia::Rect {
|
||||
let size = span.font_size * EMPHASIS_FONT_SCALE;
|
||||
skia::Rect::from_xywh(
|
||||
ink_box.center_x() - size / 2.0,
|
||||
ink_box.center_y() - size / 2.0,
|
||||
size,
|
||||
size,
|
||||
)
|
||||
}
|
||||
|
||||
/// Call `f` with each placed mark, its span, its shaped mark and the box
|
||||
/// it paints in, shaping one mark per span range.
|
||||
fn for_each_horizontal_emphasis_mark(
|
||||
paragraph: &Paragraph,
|
||||
offsets: &HorizontalOffsets,
|
||||
laid_out: &LayoutParagraph,
|
||||
mut f: impl FnMut(&TextSpan, HorizontalEmphasisPlacement, &HorizontalEmphasisMark, skia::Rect),
|
||||
) {
|
||||
let mut placements = horizontal_emphasis_placements(paragraph, offsets, laid_out);
|
||||
for range in offsets.ranges.iter().filter(|range| !range.warichu) {
|
||||
let Some(span) = paragraph.children().get(range.span) else {
|
||||
continue;
|
||||
};
|
||||
let Some(mark) = span.text_emphasis.mark_char() else {
|
||||
continue;
|
||||
};
|
||||
let Some(shaped) = HorizontalEmphasisMark::measure(laid_out, range, span, mark) else {
|
||||
continue;
|
||||
};
|
||||
let (own, rest) = placements
|
||||
.into_iter()
|
||||
.partition(|placement| placement.span == range.span && placement.mark == mark);
|
||||
placements = rest;
|
||||
for placement in own {
|
||||
let ink_box = horizontal_emphasis_mark_box(span, &placement, &shaped.ink);
|
||||
f(span, placement, &shaped, ink_box);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Every horizontal emphasis mark with the box it paints in, in the
|
||||
/// laid-out paragraph. Text bounds join these boxes.
|
||||
pub(crate) fn horizontal_emphasis_mark_boxes(
|
||||
paragraph: &Paragraph,
|
||||
offsets: &HorizontalOffsets,
|
||||
laid_out: &LayoutParagraph,
|
||||
) -> Vec<(HorizontalEmphasisPlacement, skia::Rect)> {
|
||||
let mut boxes = Vec::new();
|
||||
for_each_horizontal_emphasis_mark(paragraph, offsets, laid_out, |_, placement, _, ink_box| {
|
||||
boxes.push((placement, ink_box));
|
||||
});
|
||||
boxes
|
||||
}
|
||||
|
||||
/// Paint horizontal emphasis marks on their configured side of the base.
|
||||
/// Draw-only: the base paragraph keeps its own metrics.
|
||||
pub(crate) fn paint_horizontal_emphasis(
|
||||
canvas: &skia::Canvas,
|
||||
paragraph: &Paragraph,
|
||||
plan: &HorizontalParagraphPlan,
|
||||
laid_out: &LayoutParagraph,
|
||||
x: f32,
|
||||
y: f32,
|
||||
) {
|
||||
for_each_horizontal_emphasis_mark(
|
||||
paragraph,
|
||||
&plan.offsets,
|
||||
laid_out,
|
||||
|_, _, shaped, ink_box| {
|
||||
let origin = (
|
||||
x + ink_box.left - shaped.ink.left,
|
||||
y + ink_box.top - shaped.ink.top,
|
||||
);
|
||||
shaped.paragraph.paint(canvas, origin);
|
||||
},
|
||||
);
|
||||
}
|
||||
720
render-wasm/src/shapes/text_horizontal/justify.rs
Normal file
720
render-wasm/src/shapes/text_horizontal/justify.rs
Normal file
@ -0,0 +1,720 @@
|
||||
// Line adjustment of horizontal CJK text. SkParagraph only expands around
|
||||
// ideographs, which bunches the space next to kanji. Japanese justification
|
||||
// (JLREQ §3.8) widens each line's gaps in the pair-table order vertical
|
||||
// columns use, and Japanese squeezing (oikomi) reduces punctuation aki to
|
||||
// keep the next character. The other policies justify over their pairwise
|
||||
// expansion opportunities with bounded amounts.
|
||||
|
||||
use crate::shapes::jlreq::{classify, kinsoku_blocks, MAX_KINSOKU_SHIFT};
|
||||
use crate::shapes::text_allocation::{
|
||||
allocate_bounded_expansion, bounded_expansion, capped_equal_allocations, jlreq_expansion_gap,
|
||||
jlreq_ordered_expansion,
|
||||
};
|
||||
use crate::shapes::text_paragraph::LayoutParagraph;
|
||||
use crate::shapes::LineAdjustment;
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Slack kept back from each justified line so float noise never wraps it.
|
||||
const JUSTIFY_EPSILON: f32 = 0.05;
|
||||
|
||||
/// How far a character may pass the line end and still count as fitting.
|
||||
const FIT_TOLERANCE: f32 = 0.01;
|
||||
|
||||
/// True for a character that belongs to a Western word: no space opens
|
||||
/// between two of them.
|
||||
fn is_word_char(ch: char) -> bool {
|
||||
!ch.is_whitespace() && !is_cjk_text_char(ch) && !line_breaks::is_inserted_zero_width(ch)
|
||||
}
|
||||
|
||||
/// One real character of a horizontal Japanese paragraph for justification.
|
||||
struct JustifyChar {
|
||||
/// UTF-16 offset in the builder text.
|
||||
utf16: usize,
|
||||
span: usize,
|
||||
/// Char index in the span's layout text.
|
||||
index: usize,
|
||||
/// Pair-table class: ruby bases count as simple ruby, as vertically.
|
||||
class: JlreqClass,
|
||||
font_size: f32,
|
||||
/// A warichu note sits between it and the previous character.
|
||||
after_note: bool,
|
||||
}
|
||||
|
||||
/// The characters justification spaces, in builder order. Characters the
|
||||
/// layout pass inserted (word joiners, break opportunities and the spaces
|
||||
/// that set the preferred inter-script and question-mark gaps) belong to
|
||||
/// the gap between their neighbours, not to a gap of their own.
|
||||
fn justify_chars(paragraph: &Paragraph, plan: &HorizontalParagraphPlan) -> Vec<JustifyChar> {
|
||||
let spans = paragraph.children();
|
||||
let offset_map = &plan.offsets.offset_map;
|
||||
let mut chars = Vec::new();
|
||||
let mut after_note = false;
|
||||
for range in &plan.offsets.ranges {
|
||||
if range.warichu {
|
||||
after_note = true;
|
||||
continue;
|
||||
}
|
||||
let (Some(span), Some(text)) = (spans.get(range.span), plan.texts.get(range.span)) else {
|
||||
continue;
|
||||
};
|
||||
let mut utf16 = range.builder_start;
|
||||
for (index, ch) in text.chars().enumerate() {
|
||||
let shifted = range.shifted_start + (utf16 - range.builder_start);
|
||||
if !line_breaks::is_inserted_zero_width(ch) && !offset_map.is_inserted(shifted) {
|
||||
let class = if span.has_ruby() {
|
||||
JlreqClass::SimpleRuby
|
||||
} else if ch == line_breaks::WESTERN_WORD_SPACE {
|
||||
JlreqClass::WesternWordSpace
|
||||
} else {
|
||||
classify(ch)
|
||||
};
|
||||
chars.push(JustifyChar {
|
||||
utf16,
|
||||
span: range.span,
|
||||
index,
|
||||
class,
|
||||
font_size: span.font_size,
|
||||
after_note,
|
||||
});
|
||||
after_note = false;
|
||||
}
|
||||
utf16 += ch.len_utf16();
|
||||
}
|
||||
}
|
||||
chars
|
||||
}
|
||||
|
||||
/// Per-span letter-spacing (char index in the layout text, amount) that
|
||||
/// justifies `laid_out`, the paragraph of `plan` laid out unjustified at
|
||||
/// `width`. Every line but the last takes the JLREQ ordered expansion that
|
||||
/// vertical columns use (`jlreq_ordered_expansion`): gaps widen in pair-table
|
||||
/// priority, Western word spaces, then Japanese/Western gaps, then solid
|
||||
/// kana and kanji, and any slack left spreads over all of them. Gaps inside
|
||||
/// a Western word, beside a ruby base, around punctuation aki or across a
|
||||
/// warichu note take none; neither do inserted joiners. Each amount lands
|
||||
/// after the character before its gap.
|
||||
pub(crate) fn horizontal_jlreq_justify_spacing(
|
||||
paragraph: &Paragraph,
|
||||
laid_out: &LayoutParagraph,
|
||||
plan: &HorizontalParagraphPlan,
|
||||
width: f32,
|
||||
) -> Vec<Vec<(usize, f32)>> {
|
||||
let mut spacing: Vec<Vec<(usize, f32)>> = vec![Vec::new(); plan.texts.len()];
|
||||
let chars = justify_chars(paragraph, plan);
|
||||
let lines = laid_out.get_line_metrics();
|
||||
let Some((_, justified)) = lines.split_last() else {
|
||||
return spacing;
|
||||
};
|
||||
for line in justified {
|
||||
let slack = width - line.width as f32 - JUSTIFY_EPSILON;
|
||||
if slack <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
let start = chars.partition_point(|c| c.utf16 < line.start_index);
|
||||
let end = chars.partition_point(|c| c.utf16 < line.end_excluding_whitespaces);
|
||||
// (char before the gap, priority, cap)
|
||||
let gaps: Vec<(usize, u8, f32)> = (start..end.saturating_sub(1))
|
||||
.filter(|at| !chars[at + 1].after_note)
|
||||
.filter_map(|at| {
|
||||
let (before, after) = (&chars[at], &chars[at + 1]);
|
||||
let em = before.font_size.min(after.font_size);
|
||||
let spacing_em = pair_rule(before.class, after.class).preferred_em;
|
||||
jlreq_expansion_gap(before.class, after.class, spacing_em, em)
|
||||
.map(|(priority, cap)| (at, priority, cap))
|
||||
})
|
||||
.collect();
|
||||
let mut amounts: Vec<(usize, f32)> = Vec::new();
|
||||
for (at, amount) in jlreq_ordered_expansion(&gaps, slack) {
|
||||
match amounts.iter_mut().find(|(index, _)| *index == at) {
|
||||
Some((_, total)) => *total += amount,
|
||||
None => amounts.push((at, amount)),
|
||||
}
|
||||
}
|
||||
for (at, amount) in amounts {
|
||||
if amount > 0.0 {
|
||||
spacing[chars[at].span].push((chars[at].index, amount));
|
||||
}
|
||||
}
|
||||
}
|
||||
spacing
|
||||
}
|
||||
|
||||
/// Pairwise, bounded justification; explicit distribution fills every
|
||||
/// line without a per-gap cap. Annotation and grapheme interiors stay solid.
|
||||
pub(crate) fn horizontal_pairwise_justify_spacing(
|
||||
paragraph: &Paragraph,
|
||||
laid_out: &LayoutParagraph,
|
||||
plan: &HorizontalParagraphPlan,
|
||||
width: f32,
|
||||
) -> Vec<Vec<(usize, f32)>> {
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
let distribute = paragraph.text_align() == crate::shapes::TextAlign::Distribute;
|
||||
let policy = paragraph.layout_policy();
|
||||
let chars = line_chars(paragraph, plan);
|
||||
let source: String = chars.iter().map(|c| c.ch).collect();
|
||||
let original: Vec<_> = paragraph
|
||||
.children()
|
||||
.iter()
|
||||
.flat_map(|span| span.text.chars())
|
||||
.collect();
|
||||
let starts: Vec<_> = source
|
||||
.graphemes(true)
|
||||
.scan(0usize, |at, g| {
|
||||
let start = *at;
|
||||
*at += g.chars().count();
|
||||
Some(start)
|
||||
})
|
||||
.collect();
|
||||
let mut result = vec![Vec::new(); plan.texts.len()];
|
||||
// Ruby base of each span: distribution never opens a base.
|
||||
let group_of = paragraph.ruby_group_of_spans();
|
||||
let notes: Vec<_> = plan
|
||||
.offsets
|
||||
.ranges
|
||||
.iter()
|
||||
.filter(|range| range.warichu)
|
||||
.collect();
|
||||
let lines = laid_out.get_line_metrics();
|
||||
let count = if distribute {
|
||||
lines.len()
|
||||
} else {
|
||||
lines.len().saturating_sub(1)
|
||||
};
|
||||
for line in lines.iter().take(count) {
|
||||
let remaining = (width - line.width as f32 - JUSTIFY_EPSILON).max(0.0);
|
||||
let mut opportunities = Vec::new();
|
||||
let start = chars.partition_point(|c| c.utf16 < line.start_index);
|
||||
let end = chars.partition_point(|c| c.utf16 < line.end_excluding_whitespaces);
|
||||
for index in start..end.saturating_sub(1) {
|
||||
let (before, after) = (&chars[index], &chars[index + 1]);
|
||||
let source_before = plan.offsets.builder_to_source(before.utf16);
|
||||
let source_after = plan.offsets.builder_to_source(after.utf16);
|
||||
let nonbreaking = distribute
|
||||
&& original
|
||||
.get(source_before..=source_after)
|
||||
.is_some_and(|pair| {
|
||||
pair.iter()
|
||||
.any(|ch| matches!(ch, '\u{00a0}' | '\u{202f}' | '\u{2060}'))
|
||||
});
|
||||
let note = notes.get(notes.partition_point(|r| r.builder_end <= before.utf16));
|
||||
if before.utf16 < line.start_index
|
||||
|| after.utf16 >= line.end_excluding_whitespaces
|
||||
|| (!distribute && after.joined)
|
||||
|| nonbreaking
|
||||
|| starts.binary_search(&(index + 1)).is_err()
|
||||
|| (distribute
|
||||
&& group_of[before.span].is_some()
|
||||
&& group_of[before.span] == group_of[after.span])
|
||||
|| note.is_some_and(|r| {
|
||||
r.builder_start > before.utf16 && r.builder_start < after.utf16
|
||||
})
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let em = before.font_size.min(after.font_size);
|
||||
if let Some((priority, cap)) =
|
||||
bounded_expansion(policy, before.ch, after.ch, em, distribute.then_some(width))
|
||||
{
|
||||
opportunities.push((index, priority, cap));
|
||||
}
|
||||
}
|
||||
for (index, amount) in allocate_bounded_expansion(&opportunities, remaining) {
|
||||
let ch = &chars[index];
|
||||
result[ch.span].push((ch.index, amount));
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// Lays a paragraph out with extra per-span sheds (char index, amount).
|
||||
/// Lays out a paragraph with extra sheds, restricted to a builder UTF-16
|
||||
/// window (`FULL_WINDOW` for all of it).
|
||||
pub(crate) type LayOutWithSheds<'a> =
|
||||
dyn Fn(&[Vec<(usize, f32)>], std::ops::Range<usize>) -> LayoutParagraph + 'a;
|
||||
|
||||
/// Builder UTF-16 units first laid out from a line start: a few lines of
|
||||
/// text. The window doubles until it holds the lines a decision needs.
|
||||
const SQUEEZE_WINDOW: usize = 512;
|
||||
|
||||
/// One real character of a horizontal paragraph in builder order.
|
||||
struct LineChar {
|
||||
/// UTF-16 offset in the builder text.
|
||||
utf16: usize,
|
||||
span: usize,
|
||||
/// Char index in the span's layout text.
|
||||
index: usize,
|
||||
ch: char,
|
||||
class: Option<JlreqClass>,
|
||||
font_size: f32,
|
||||
/// A WORD JOINER before it forbids a break.
|
||||
joined: bool,
|
||||
}
|
||||
|
||||
fn line_chars(paragraph: &Paragraph, plan: &HorizontalParagraphPlan) -> Vec<LineChar> {
|
||||
let spans = paragraph.children();
|
||||
let mut chars = Vec::new();
|
||||
for range in &plan.offsets.ranges {
|
||||
let (Some(span), Some(text)) = (spans.get(range.span), plan.texts.get(range.span)) else {
|
||||
continue;
|
||||
};
|
||||
if range.warichu {
|
||||
continue;
|
||||
}
|
||||
let proportional = span.font_features == FontFeatures::Palt;
|
||||
let mut utf16 = range.builder_start;
|
||||
let mut joined = false;
|
||||
for (index, ch) in text.chars().enumerate() {
|
||||
if ch == line_breaks::WORD_JOINER {
|
||||
joined = true;
|
||||
} else if !line_breaks::is_inserted_zero_width(ch) {
|
||||
chars.push(LineChar {
|
||||
utf16,
|
||||
span: range.span,
|
||||
index,
|
||||
ch,
|
||||
class: aki_class(ch, span.has_ruby(), proportional),
|
||||
font_size: span.font_size,
|
||||
joined,
|
||||
});
|
||||
joined = false;
|
||||
}
|
||||
utf16 += ch.len_utf16();
|
||||
}
|
||||
}
|
||||
chars
|
||||
}
|
||||
|
||||
/// Chars `[start, end)` that start a line at `start` and move as one: a
|
||||
/// word joiner or a Western word keeps the next character with them.
|
||||
fn line_start_unit(chars: &[LineChar], start: usize) -> std::ops::Range<usize> {
|
||||
let mut end = start + 1;
|
||||
while end < chars.len()
|
||||
&& (chars[end].joined || (is_word_char(chars[end - 1].ch) && is_word_char(chars[end].ch)))
|
||||
{
|
||||
end += 1;
|
||||
}
|
||||
start..end
|
||||
}
|
||||
|
||||
/// Squeezable spacing (char, priority, px) after each char of `range`: what
|
||||
/// the JLREQ pair table lets oikomi take between it and the next char.
|
||||
fn squeeze_capacities(chars: &[LineChar], range: std::ops::Range<usize>) -> Vec<(usize, u8, f32)> {
|
||||
range
|
||||
.filter(|at| at + 1 < chars.len())
|
||||
.filter_map(|at| {
|
||||
let (before, after) = (&chars[at], &chars[at + 1]);
|
||||
let rule = pair_rule(before.class?, after.class?);
|
||||
let em = before.font_size.min(after.font_size);
|
||||
let capacity = (rule.preferred_em - rule.minimum_em) * em;
|
||||
(rule.shrink_priority > 0 && capacity > 0.0).then_some((
|
||||
at,
|
||||
rule.shrink_priority,
|
||||
capacity,
|
||||
))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The trailing aki a closing bracket or comma sheds at a line end.
|
||||
fn line_end_aki(ch: &LineChar) -> f32 {
|
||||
match ch.class {
|
||||
Some(JlreqClass::ClosingBracket | JlreqClass::Comma) => ch.font_size / 2.0,
|
||||
_ => 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// True when some line of the paragraph could squeeze under
|
||||
/// `LineAdjustment::PushOutFirst`: only a run of more kinsoku pairs than
|
||||
/// oidashi moves over (`MAX_KINSOKU_SHIFT`) leaves push-out unresolved.
|
||||
pub(crate) fn horizontal_push_out_may_fail(
|
||||
paragraph: &Paragraph,
|
||||
plan: &HorizontalParagraphPlan,
|
||||
) -> bool {
|
||||
let chars = line_chars(paragraph, plan);
|
||||
let mut run = 0usize;
|
||||
chars.windows(2).any(|pair| {
|
||||
run = if kinsoku_blocks(pair[0].ch, pair[1].ch) {
|
||||
run + 1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
run > MAX_KINSOKU_SHIFT
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether SkParagraph's push-out of `unit`, the characters that start the
|
||||
/// next line, is one vertical oidashi accepts (`push_out_resolves`). Without
|
||||
/// kinsoku the line would break before the first unit character that does
|
||||
/// not fit `room`, the space the line leaves; moving that break back to the
|
||||
/// unit start must cross at most `MAX_KINSOKU_SHIFT` kinsoku pairs. Lines
|
||||
/// never start inside the unit, since joiners keep it whole, and a unit
|
||||
/// wider than the box overflows on its own line, so the shift limit is the
|
||||
/// only way push-out fails here. `widths` holds each unit character's width.
|
||||
fn push_out_resolves(
|
||||
chars: &[LineChar],
|
||||
unit: std::ops::Range<usize>,
|
||||
widths: &[f32],
|
||||
room: f32,
|
||||
) -> bool {
|
||||
let mut used = 0.0;
|
||||
let mut natural = unit.end;
|
||||
for (at, width) in unit.clone().zip(widths) {
|
||||
if used + width - line_end_aki(&chars[at]) > room + FIT_TOLERANCE {
|
||||
natural = at;
|
||||
break;
|
||||
}
|
||||
used += width;
|
||||
}
|
||||
let shifts = (unit.start + 1..=natural.min(unit.end - 1))
|
||||
.filter(|at| kinsoku_blocks(chars[at - 1].ch, chars[*at].ch))
|
||||
.count();
|
||||
natural == unit.start || shifts <= MAX_KINSOKU_SHIFT
|
||||
}
|
||||
|
||||
/// Per-span sheds (char index, amount) that squeeze lines of a horizontal
|
||||
/// Japanese paragraph to keep the character that would start the next line
|
||||
/// (JLREQ §3.8 oikomi). Each line, in order, takes the unit starting the next
|
||||
/// line when the pair table's squeezable aki on the line covers the
|
||||
/// overflow, reduced in priority order and equally within a priority. A
|
||||
/// trailing closing bracket or comma pulled to the line end sheds its aki
|
||||
/// there. Under `PushInFirst` every line may squeeze; under `PushOutFirst`
|
||||
/// only a line whose push-out does not resolve kinsoku (`push_out_resolves`),
|
||||
/// as vertical columns do; `PushOutOnly` never squeezes. `lay_out` lays the
|
||||
/// paragraph out with extra sheds; returns the sheds and the final layout.
|
||||
pub(crate) fn horizontal_squeeze_sheds(
|
||||
paragraph: &Paragraph,
|
||||
plan: &HorizontalParagraphPlan,
|
||||
width: f32,
|
||||
adjustment: LineAdjustment,
|
||||
lay_out: &LayOutWithSheds,
|
||||
) -> (Vec<Vec<(usize, f32)>>, LayoutParagraph) {
|
||||
if adjustment == LineAdjustment::PushOutOnly {
|
||||
let sheds = vec![Vec::new(); plan.texts.len()];
|
||||
let laid_out = lay_out(&sheds, FULL_WINDOW);
|
||||
return (sheds, laid_out);
|
||||
}
|
||||
let chars = line_chars(paragraph, plan);
|
||||
let mut sheds: Vec<Vec<(usize, f32)>> = vec![Vec::new(); plan.texts.len()];
|
||||
let total = plan
|
||||
.offsets
|
||||
.ranges
|
||||
.iter()
|
||||
.map(|range| range.builder_end)
|
||||
.max()
|
||||
.unwrap_or(0);
|
||||
// Lines are broken greedily, so a line and the decision about it depend
|
||||
// only on the text from its start. Lay out that text alone, enough of it
|
||||
// for the line, the next one and what decides the next break: relaying
|
||||
// out the whole paragraph per line made long paragraphs quadratic.
|
||||
let lay_out_from = |sheds: &[Vec<(usize, f32)>], start: usize| {
|
||||
let mut size = SQUEEZE_WINDOW;
|
||||
loop {
|
||||
let end = start.saturating_add(size).min(total);
|
||||
let laid_out = lay_out(sheds, start..end);
|
||||
if end >= total || laid_out.get_line_metrics().len() >= 3 {
|
||||
return laid_out;
|
||||
}
|
||||
size *= 2;
|
||||
}
|
||||
};
|
||||
// `window` is laid out from `window_start` with the current sheds; its
|
||||
// lines stay valid until a squeeze changes the sheds, so lines without a
|
||||
// candidate move on inside it instead of laying out again.
|
||||
let mut window_start = 0usize;
|
||||
let mut window = lay_out_from(&sheds, window_start);
|
||||
let mut line_index = 0usize;
|
||||
for _ in 0..chars.len() + 1 {
|
||||
let window_lines = window.get_line_metrics();
|
||||
// A line is decided by the next one and what follows it: past that,
|
||||
// lay out again from the line.
|
||||
let reached_end = window_lines
|
||||
.last()
|
||||
.is_some_and(|last| window_start + last.end_index >= total);
|
||||
if line_index > 0 && line_index + 2 >= window_lines.len() && !reached_end {
|
||||
window_start += window_lines.get(line_index).map_or_else(
|
||||
|| window_lines.last().map_or(0, |last| last.end_index),
|
||||
|line| line.start_index,
|
||||
);
|
||||
window = lay_out_from(&sheds, window_start);
|
||||
line_index = 0;
|
||||
continue;
|
||||
}
|
||||
let start = window_start
|
||||
+ window_lines
|
||||
.get(line_index)
|
||||
.map_or(0, |line| line.start_index);
|
||||
let laid_out = &window;
|
||||
let lines: Vec<_> = window_lines.iter().skip(line_index).collect();
|
||||
if lines.len() < 2 {
|
||||
break;
|
||||
}
|
||||
let (line, next) = (lines[0], lines[1]);
|
||||
let next_start = window_start + next.start_index;
|
||||
let Some(unit_start) = chars.iter().position(|c| c.utf16 >= next_start) else {
|
||||
break;
|
||||
};
|
||||
let unit = line_start_unit(&chars, unit_start);
|
||||
let unit_end_utf16 = chars
|
||||
.get(unit.end)
|
||||
.map_or(window_start + next.end_index, |c| c.utf16);
|
||||
let unit_width = laid_out
|
||||
.get_rects_for_range(
|
||||
chars[unit.start].utf16 - window_start..unit_end_utf16 - window_start,
|
||||
RectHeightStyle::Tight,
|
||||
RectWidthStyle::Tight,
|
||||
)
|
||||
.iter()
|
||||
.map(|rect| rect.rect.width())
|
||||
.sum::<f32>();
|
||||
let last = &chars[unit.end - 1];
|
||||
let trailing_aki = line_end_aki(last);
|
||||
let needed = line.width as f32 + unit_width - trailing_aki - width + JUSTIFY_EPSILON;
|
||||
if needed > 0.0 && adjustment == LineAdjustment::PushOutFirst {
|
||||
let widths: Vec<f32> = unit
|
||||
.clone()
|
||||
.map(|at| {
|
||||
let end = chars.get(at + 1).map_or(unit_end_utf16, |c| c.utf16);
|
||||
laid_out
|
||||
.get_rects_for_range(
|
||||
chars[at].utf16 - window_start..end - window_start,
|
||||
RectHeightStyle::Tight,
|
||||
RectWidthStyle::Tight,
|
||||
)
|
||||
.iter()
|
||||
.map(|rect| rect.rect.width())
|
||||
.sum::<f32>()
|
||||
})
|
||||
.collect();
|
||||
if push_out_resolves(&chars, unit.clone(), &widths, width - line.width as f32) {
|
||||
line_index += 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
let line_chars_end = unit.start;
|
||||
let line_chars_start = chars
|
||||
.iter()
|
||||
.position(|c| c.utf16 >= start)
|
||||
.unwrap_or(line_chars_end);
|
||||
let capacities = squeeze_capacities(&chars, line_chars_start..line_chars_end);
|
||||
let capacity: f32 = capacities.iter().map(|(_, _, cap)| cap).sum();
|
||||
if needed <= 0.0 || capacity < needed {
|
||||
line_index += 1;
|
||||
continue;
|
||||
}
|
||||
let mut candidate = sheds.clone();
|
||||
let mut remaining = needed;
|
||||
for priority in 1..=5 {
|
||||
let caps: Vec<(usize, f32)> = capacities
|
||||
.iter()
|
||||
.filter(|(_, p, _)| *p == priority)
|
||||
.map(|(at, _, cap)| (*at, *cap))
|
||||
.collect();
|
||||
for (at, amount) in capped_equal_allocations(&caps, remaining) {
|
||||
candidate[chars[at].span].push((chars[at].index, amount));
|
||||
remaining -= amount;
|
||||
}
|
||||
if remaining <= 0.0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if trailing_aki > 0.0 {
|
||||
candidate[last.span].push((last.index, trailing_aki));
|
||||
}
|
||||
// The check needs this line only: lay out to the end of the pulled
|
||||
// unit plus a line's worth of what follows.
|
||||
let check_end = unit_end_utf16
|
||||
.saturating_add(unit_end_utf16 - start)
|
||||
.min(total);
|
||||
let squeezed = lay_out(&candidate, start..check_end);
|
||||
let squeezed_lines = squeezed.get_line_metrics();
|
||||
let kept = squeezed_lines
|
||||
.first()
|
||||
.is_some_and(|squeezed_line| start + squeezed_line.end_index >= unit_end_utf16);
|
||||
if kept {
|
||||
sheds = candidate;
|
||||
if check_end >= total && squeezed_lines.len() < 2 {
|
||||
break;
|
||||
}
|
||||
window_start = start;
|
||||
window = squeezed;
|
||||
line_index = 1;
|
||||
} else {
|
||||
line_index += 1;
|
||||
}
|
||||
}
|
||||
let laid_out = lay_out(&sheds, FULL_WINDOW);
|
||||
(sheds, laid_out)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::shapes::{TextAlign, TextDirection};
|
||||
use skia::textlayout::{FontCollection, ParagraphBuilder, ParagraphStyle, TextStyle};
|
||||
use std::cell::Cell;
|
||||
|
||||
type LayOutFull<'a> = dyn Fn(&[Vec<(usize, f32)>]) -> LayoutParagraph + 'a;
|
||||
|
||||
/// The squeeze pass as it was before windowing: one layout of the whole
|
||||
/// paragraph per line.
|
||||
fn full_relayout_sheds(
|
||||
paragraph: &Paragraph,
|
||||
plan: &HorizontalParagraphPlan,
|
||||
width: f32,
|
||||
lay_out: &LayOutFull,
|
||||
) -> Vec<Vec<(usize, f32)>> {
|
||||
let chars = line_chars(paragraph, plan);
|
||||
let mut sheds: Vec<Vec<(usize, f32)>> = vec![Vec::new(); plan.texts.len()];
|
||||
let mut laid_out = lay_out(&sheds);
|
||||
let mut line_index = 0usize;
|
||||
for _ in 0..chars.len() + 1 {
|
||||
let lines = laid_out.get_line_metrics();
|
||||
if line_index + 1 >= lines.len() {
|
||||
break;
|
||||
}
|
||||
let (line, next) = (&lines[line_index], &lines[line_index + 1]);
|
||||
let Some(unit_start) = chars.iter().position(|c| c.utf16 >= next.start_index) else {
|
||||
break;
|
||||
};
|
||||
let unit = line_start_unit(&chars, unit_start);
|
||||
let unit_end_utf16 = chars.get(unit.end).map_or(next.end_index, |c| c.utf16);
|
||||
let unit_width = laid_out
|
||||
.get_rects_for_range(
|
||||
chars[unit.start].utf16..unit_end_utf16,
|
||||
RectHeightStyle::Tight,
|
||||
RectWidthStyle::Tight,
|
||||
)
|
||||
.iter()
|
||||
.map(|rect| rect.rect.width())
|
||||
.sum::<f32>();
|
||||
let last = &chars[unit.end - 1];
|
||||
let trailing_aki = match last.class {
|
||||
Some(JlreqClass::ClosingBracket | JlreqClass::Comma) => last.font_size / 2.0,
|
||||
_ => 0.0,
|
||||
};
|
||||
let needed = line.width as f32 + unit_width - trailing_aki - width + JUSTIFY_EPSILON;
|
||||
let line_chars_end = unit.start;
|
||||
let line_chars_start = chars
|
||||
.iter()
|
||||
.position(|c| c.utf16 >= line.start_index)
|
||||
.unwrap_or(line_chars_end);
|
||||
let capacities = squeeze_capacities(&chars, line_chars_start..line_chars_end);
|
||||
let total: f32 = capacities.iter().map(|(_, _, cap)| cap).sum();
|
||||
if needed <= 0.0 || total < needed {
|
||||
line_index += 1;
|
||||
continue;
|
||||
}
|
||||
let mut candidate = sheds.clone();
|
||||
let mut remaining = needed;
|
||||
for priority in 1..=5 {
|
||||
let caps: Vec<(usize, f32)> = capacities
|
||||
.iter()
|
||||
.filter(|(_, p, _)| *p == priority)
|
||||
.map(|(at, _, cap)| (*at, *cap))
|
||||
.collect();
|
||||
for (at, amount) in capped_equal_allocations(&caps, remaining) {
|
||||
candidate[chars[at].span].push((chars[at].index, amount));
|
||||
remaining -= amount;
|
||||
}
|
||||
if remaining <= 0.0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if trailing_aki > 0.0 {
|
||||
candidate[last.span].push((last.index, trailing_aki));
|
||||
}
|
||||
let squeezed = lay_out(&candidate);
|
||||
if squeezed
|
||||
.get_line_metrics()
|
||||
.get(line_index)
|
||||
.is_some_and(|squeezed_line| squeezed_line.end_index >= unit_end_utf16)
|
||||
{
|
||||
sheds = candidate;
|
||||
laid_out = squeezed;
|
||||
}
|
||||
line_index += 1;
|
||||
}
|
||||
sheds
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn windowed_squeeze_matches_full_relayout_and_lays_out_the_paragraph_once() {
|
||||
crate::globals::design_init();
|
||||
let text = "あい、うえ、おかきくけこさしすせそ「かぎ」、と。".repeat(40);
|
||||
let span = TextSpan {
|
||||
text,
|
||||
font_size: 16.0,
|
||||
line_height: 1.0,
|
||||
..TextSpan::default()
|
||||
};
|
||||
let mut paragraph = Paragraph::new(
|
||||
TextAlign::default(),
|
||||
TextDirection::LTR,
|
||||
None,
|
||||
None,
|
||||
1.0,
|
||||
0.0,
|
||||
vec![span],
|
||||
);
|
||||
paragraph.set_language("ja".to_string());
|
||||
let plan = HorizontalParagraphPlan::new(¶graph);
|
||||
let mut style = TextStyle::default();
|
||||
style.set_font_size(16.0);
|
||||
let mut fonts = FontCollection::new();
|
||||
fonts.set_default_font_manager(skia::FontMgr::new(), None);
|
||||
let mut paragraph_style = ParagraphStyle::default();
|
||||
paragraph_style.set_apply_rounding_hack(false);
|
||||
let width = 16.0 * 7.6;
|
||||
let full_layouts = Cell::new(0usize);
|
||||
let lay_out = |extra: &[Vec<(usize, f32)>], window: std::ops::Range<usize>| {
|
||||
if window == FULL_WINDOW {
|
||||
full_layouts.set(full_layouts.get() + 1);
|
||||
}
|
||||
let sheds: Vec<(usize, f32)> = plan.pair_spacing[0]
|
||||
.iter()
|
||||
.chain(extra.first().into_iter().flatten())
|
||||
.copied()
|
||||
.collect();
|
||||
let (text, from) =
|
||||
window_slice(&plan.texts[0], &plan.offsets.ranges[0], &window).unwrap_or(("", 0));
|
||||
let sheds = window_entries(&sheds, from, text.chars().count());
|
||||
let mut builder = ParagraphBuilder::new(¶graph_style, &fonts);
|
||||
builder.push_style(&style);
|
||||
add_horizontal_span(
|
||||
&mut builder,
|
||||
¶graph.children()[0],
|
||||
text,
|
||||
&sheds,
|
||||
&[],
|
||||
&style,
|
||||
&fonts,
|
||||
);
|
||||
let mut laid_out = LayoutParagraph::build(&mut builder);
|
||||
laid_out.layout(width);
|
||||
laid_out
|
||||
};
|
||||
let (windowed, _) = horizontal_squeeze_sheds(
|
||||
¶graph,
|
||||
&plan,
|
||||
width,
|
||||
LineAdjustment::PushInFirst,
|
||||
&lay_out,
|
||||
);
|
||||
assert_eq!(
|
||||
full_layouts.get(),
|
||||
1,
|
||||
"the whole paragraph is laid out once"
|
||||
);
|
||||
let reference = full_relayout_sheds(¶graph, &plan, width, &|extra| {
|
||||
lay_out(extra, FULL_WINDOW)
|
||||
});
|
||||
assert!(!reference[0].is_empty(), "the text squeezes some lines");
|
||||
assert_eq!(windowed.len(), reference.len());
|
||||
for (got, want) in windowed[0].iter().zip(&reference[0]) {
|
||||
assert_eq!(got.0, want.0);
|
||||
assert!((got.1 - want.1).abs() < 0.01, "{got:?} vs {want:?}");
|
||||
}
|
||||
assert_eq!(windowed[0].len(), reference[0].len());
|
||||
}
|
||||
}
|
||||
451
render-wasm/src/shapes/text_horizontal/mod.rs
Normal file
451
render-wasm/src/shapes/text_horizontal/mod.rs
Normal file
@ -0,0 +1,451 @@
|
||||
// Horizontal CJK layout on top of SkParagraph: the policy's layout texts
|
||||
// (break joiners and spacing characters), punctuation and inter-script
|
||||
// spacing, offset maps between source, layout and builder text, line
|
||||
// adjustment, and the ruby, emphasis and warichu painters.
|
||||
|
||||
use crate::shapes::text_paragraph::{LayoutParagraph, SpacedParagraphBuilder};
|
||||
|
||||
mod emphasis;
|
||||
mod justify;
|
||||
mod offsets;
|
||||
mod overflow;
|
||||
mod ruby;
|
||||
mod warichu;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
pub(crate) use emphasis::*;
|
||||
pub(crate) use justify::*;
|
||||
pub(crate) use offsets::*;
|
||||
pub(crate) use ruby::*;
|
||||
pub(crate) use warichu::*;
|
||||
|
||||
use super::text::{add_text_with_tabs, Paragraph, TextContent, TextSpan};
|
||||
use super::text_attrs::*;
|
||||
use super::text_ruby::*;
|
||||
use super::text_shaping::{
|
||||
is_upright_char, shape_segment_with_fallbacks, single_glyph_blob, span_font_families, ShapedRun,
|
||||
};
|
||||
use super::text_warichu::*;
|
||||
use crate::globals::get_resources;
|
||||
use crate::math::Point;
|
||||
use crate::shapes::jlreq::{
|
||||
aki_class, is_cjk_text_char, pair_rule, punctuation_aki_sheds, AkiShed, JlreqClass,
|
||||
};
|
||||
use crate::shapes::{line_breaks, merge_fills};
|
||||
use crate::utils::{get_fallback_fonts, get_font_collection};
|
||||
use skia_safe::{
|
||||
self as skia,
|
||||
textlayout::{
|
||||
ParagraphBuilder, ParagraphStyle, PlaceholderAlignment, PlaceholderStyle, RectHeightStyle,
|
||||
RectWidthStyle, TextBaseline,
|
||||
},
|
||||
Canvas, Contains, Font, FontMgr, GlyphId,
|
||||
};
|
||||
|
||||
/// True when a paragraph with layout texts `texts` needs the Japanese passes:
|
||||
/// its policy is Japanese and it has ruby or a CJK text character.
|
||||
pub(crate) fn uses_japanese_layout(paragraph: &Paragraph, texts: &[String]) -> bool {
|
||||
paragraph.layout_policy().uses_jlreq()
|
||||
&& (paragraph.children().iter().any(TextSpan::has_ruby)
|
||||
|| texts.iter().any(|text| text.chars().any(is_cjk_text_char)))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn layout_span_texts(paragraph: &Paragraph) -> (Vec<String>, line_breaks::OffsetMap) {
|
||||
layout_span_texts_with_breaks(paragraph, &[], &[])
|
||||
}
|
||||
|
||||
fn layout_span_texts_with_breaks(
|
||||
paragraph: &Paragraph,
|
||||
emergency: &[usize],
|
||||
forced: &[usize],
|
||||
) -> (Vec<String>, line_breaks::OffsetMap) {
|
||||
let texts: Vec<String> = paragraph
|
||||
.children()
|
||||
.iter()
|
||||
.map(TextSpan::apply_text_transform)
|
||||
.collect();
|
||||
// Text without Japanese characters or ruby keeps plain Skia layout.
|
||||
let policy = paragraph.layout_policy();
|
||||
if policy.plans_breaks() || uses_japanese_layout(paragraph, &texts) {
|
||||
let ruby_breaks: Vec<Option<Vec<usize>>> = paragraph
|
||||
.children()
|
||||
.iter()
|
||||
.map(|span| span.has_ruby().then(Vec::new))
|
||||
.collect();
|
||||
// A base that crosses spans stays whole across their boundaries.
|
||||
let group_of = paragraph.ruby_group_of_spans();
|
||||
let joined: Vec<bool> = group_of
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, group)| index > 0 && group.is_some() && *group == group_of[index - 1])
|
||||
.collect();
|
||||
let shifted = line_breaks::apply_policy_with_breaks(
|
||||
&texts,
|
||||
&ruby_breaks,
|
||||
&joined,
|
||||
policy,
|
||||
emergency,
|
||||
forced,
|
||||
);
|
||||
if let Some((shifted, map)) = shifted {
|
||||
return (shifted, map);
|
||||
}
|
||||
}
|
||||
(texts, line_breaks::OffsetMap::default())
|
||||
}
|
||||
|
||||
/// Advance changes between adjacent characters of the layout `texts`, as
|
||||
/// (char index, amount) per span: positive narrows the character, negative
|
||||
/// widens it. Japanese sheds JLREQ punctuation aki; the other policies use
|
||||
/// their pairwise punctuation compression and script spacing.
|
||||
pub(crate) fn horizontal_pair_spacing(
|
||||
paragraph: &Paragraph,
|
||||
texts: &[String],
|
||||
) -> Vec<Vec<(usize, f32)>> {
|
||||
if !paragraph.layout_policy().uses_jlreq() {
|
||||
return horizontal_pairwise_spacing(paragraph, texts);
|
||||
}
|
||||
jlreq_aki_sheds(paragraph, texts)
|
||||
}
|
||||
|
||||
/// (char index, amount) per span of the layout `texts` for the characters
|
||||
/// whose advance loses a half-em to `punctuation_aki_sheds`: a closing mark
|
||||
/// sheds half of its own em, and the character before an opening bracket
|
||||
/// gives up half of the bracket's em. Inserted word joiners are transparent;
|
||||
/// `palt` spans set punctuation proportionally.
|
||||
fn jlreq_aki_sheds(paragraph: &Paragraph, texts: &[String]) -> Vec<Vec<(usize, f32)>> {
|
||||
let spans = paragraph.children();
|
||||
let half_em = |span: usize| spans.get(span).map_or(0.0, |span| span.font_size * 0.5);
|
||||
// (span, char index in the span text) of every real character.
|
||||
let mut chars: Vec<(usize, usize)> = Vec::new();
|
||||
let mut classes: Vec<Option<JlreqClass>> = Vec::new();
|
||||
for (span_index, text) in texts.iter().enumerate() {
|
||||
let span = spans.get(span_index);
|
||||
let ruby_base = span.is_some_and(TextSpan::has_ruby);
|
||||
let proportional = span.is_some_and(|span| span.font_features == FontFeatures::Palt);
|
||||
for (index, ch) in text.chars().enumerate() {
|
||||
if line_breaks::is_inserted_zero_width(ch) {
|
||||
continue;
|
||||
}
|
||||
chars.push((span_index, index));
|
||||
classes.push(aki_class(ch, ruby_base, proportional));
|
||||
}
|
||||
}
|
||||
let mut sheds: Vec<Vec<(usize, f32)>> = vec![Vec::new(); texts.len()];
|
||||
for (at, shed) in punctuation_aki_sheds(&classes) {
|
||||
let (span, amount_span, index) = match shed {
|
||||
AkiShed::Trailing => (chars[at].0, chars[at].0, chars[at].1),
|
||||
AkiShed::Leading => (chars[at - 1].0, chars[at].0, chars[at - 1].1),
|
||||
};
|
||||
sheds[span].push((index, half_em(amount_span)));
|
||||
}
|
||||
sheds
|
||||
}
|
||||
|
||||
/// Signed advance changes: positive removes punctuation space, negative adds
|
||||
/// the regional Han/Latin gap. Source and builder text stay unchanged.
|
||||
fn horizontal_pairwise_spacing(paragraph: &Paragraph, texts: &[String]) -> Vec<Vec<(usize, f32)>> {
|
||||
let mut result = vec![Vec::new(); texts.len()];
|
||||
let policy = paragraph.layout_policy();
|
||||
let mut previous: Option<(usize, usize, char)> = None;
|
||||
for (span, text) in texts.iter().enumerate() {
|
||||
for (index, ch) in text.chars().enumerate() {
|
||||
if line_breaks::is_inserted_zero_width(ch) {
|
||||
continue;
|
||||
}
|
||||
if let Some((before_span, before_index, before)) = previous {
|
||||
let a = ¶graph.children()[before_span];
|
||||
let b = ¶graph.children()[span];
|
||||
if !a.has_ruby() && !b.has_ruby() && !a.is_warichu() && !b.is_warichu() {
|
||||
let em = a.font_size.min(b.font_size);
|
||||
let spacing = policy.pair_spacing_em(before, ch, false);
|
||||
// `palt` already sets punctuation proportionally.
|
||||
let compression = if a.font_features == FontFeatures::Palt
|
||||
|| b.font_features == FontFeatures::Palt
|
||||
{
|
||||
0.0
|
||||
} else {
|
||||
spacing.compression
|
||||
};
|
||||
let amount = (compression - spacing.script_gap) * em;
|
||||
if amount != 0.0 {
|
||||
result[before_span].push((before_index, amount));
|
||||
}
|
||||
}
|
||||
}
|
||||
previous = Some((span, index, ch));
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// Add a span to a horizontal paragraph builder. A warichu span becomes one
|
||||
/// inline placeholder so its two lines wrap as a unit; its glyphs are painted
|
||||
/// after layout. The characters at `sheds` (from `horizontal_pair_spacing`)
|
||||
/// set narrower by their amounts; `extra` (from `horizontal_ruby_spacing`)
|
||||
/// adds letter-spacing to characters.
|
||||
pub(crate) fn add_horizontal_span(
|
||||
builder: &mut impl SpacedParagraphBuilder,
|
||||
span: &TextSpan,
|
||||
builder_text: &str,
|
||||
sheds: &[(usize, f32)],
|
||||
extra: &[(usize, f32)],
|
||||
text_style: &skia::textlayout::TextStyle,
|
||||
fonts: &skia::textlayout::FontCollection,
|
||||
) {
|
||||
if span.is_warichu() {
|
||||
let builder = builder.paragraph_builder();
|
||||
if builder_text.starts_with(line_breaks::FORCED_LINE_BREAK) {
|
||||
builder.add_text(line_breaks::FORCED_LINE_BREAK.to_string());
|
||||
}
|
||||
let text = span.apply_text_transform();
|
||||
let (first, second) = warichu_text_lines(&text);
|
||||
let mut mini_style = text_style.clone();
|
||||
mini_style.set_font_size(span.font_size * WARICHU_FONT_SCALE);
|
||||
mini_style.set_height(1.0);
|
||||
mini_style.set_height_override(true);
|
||||
mini_style.set_letter_spacing(span.letter_spacing * WARICHU_FONT_SCALE);
|
||||
let measure = |line: &str| mini_paragraph(line, &mini_style, fonts).longest_line();
|
||||
let width = measure(first).max(measure(second)).max(0.01);
|
||||
let height = span.font_size.max(0.01);
|
||||
builder.add_placeholder(&PlaceholderStyle::new(
|
||||
width,
|
||||
height,
|
||||
PlaceholderAlignment::Middle,
|
||||
TextBaseline::Alphabetic,
|
||||
height,
|
||||
));
|
||||
// SkParagraph does not expose a placeholder's TextStyle through line
|
||||
// metrics. A near-zero, inkless NBSP carries the span style for the
|
||||
// paint pass; the zero-width space after it allows a line break after
|
||||
// the atomic placeholder.
|
||||
let mut anchor_style = text_style.clone();
|
||||
anchor_style.set_font_size(0.01);
|
||||
anchor_style.set_height(0.01);
|
||||
anchor_style.set_height_override(true);
|
||||
anchor_style.set_letter_spacing(0.0);
|
||||
builder.push_style(&anchor_style);
|
||||
builder.add_text(HORIZONTAL_WARICHU_STYLE_ANCHOR.to_string());
|
||||
builder.add_text(HORIZONTAL_WARICHU_BREAK_ANCHOR.to_string());
|
||||
} else {
|
||||
add_text_with_sheds(builder, span, builder_text, sheds, extra, text_style);
|
||||
}
|
||||
}
|
||||
|
||||
/// Builder UTF-16 window that covers a whole paragraph.
|
||||
pub(crate) const FULL_WINDOW: std::ops::Range<usize> = 0..usize::MAX;
|
||||
|
||||
/// Part of a span's builder `text` inside the builder UTF-16 `window`, with
|
||||
/// the char index of its first character, or None when the span lies
|
||||
/// outside the window. A warichu box is atomic: kept whole when it starts
|
||||
/// inside the window.
|
||||
pub(crate) fn window_slice<'a>(
|
||||
text: &'a str,
|
||||
range: &HorizontalSpanRange,
|
||||
window: &std::ops::Range<usize>,
|
||||
) -> Option<(&'a str, usize)> {
|
||||
if range.builder_end <= window.start || range.builder_start >= window.end {
|
||||
return None;
|
||||
}
|
||||
if range.warichu {
|
||||
return (range.builder_start >= window.start).then_some((text, 0));
|
||||
}
|
||||
let mut utf16 = range.builder_start;
|
||||
let mut from = None;
|
||||
let mut to = text.len();
|
||||
for (index, (byte, ch)) in text.char_indices().enumerate() {
|
||||
if from.is_none() && utf16 >= window.start {
|
||||
from = Some((byte, index));
|
||||
}
|
||||
if utf16 >= window.end {
|
||||
to = byte;
|
||||
break;
|
||||
}
|
||||
utf16 += ch.len_utf16();
|
||||
}
|
||||
let (from_byte, from_char) = from?;
|
||||
Some((&text[from_byte..to.max(from_byte)], from_char))
|
||||
}
|
||||
|
||||
/// Per-char `entries` of a span shifted to a slice starting at char `from`
|
||||
/// and `len` chars long.
|
||||
pub(crate) fn window_entries(
|
||||
entries: &[(usize, f32)],
|
||||
from: usize,
|
||||
len: usize,
|
||||
) -> Vec<(usize, f32)> {
|
||||
entries
|
||||
.iter()
|
||||
.filter(|(index, _)| *index >= from && *index < from + len)
|
||||
.map(|(index, amount)| (index - from, *amount))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Add `text`, adjusting the letter-spacing of single characters so the
|
||||
/// builder text stays unchanged: those at `sheds` lose their amount and
|
||||
/// those in `extra` take its added spacing. Skia tracks every cluster,
|
||||
/// including the zero-width word joiners the kinsoku pass inserts, so those
|
||||
/// take no letter-spacing. The builder records how far each adjusted
|
||||
/// character departs from the span tracking.
|
||||
fn add_text_with_sheds(
|
||||
builder: &mut impl SpacedParagraphBuilder,
|
||||
span: &TextSpan,
|
||||
text: &str,
|
||||
sheds: &[(usize, f32)],
|
||||
extra: &[(usize, f32)],
|
||||
text_style: &skia::textlayout::TextStyle,
|
||||
) {
|
||||
let tracking = text_style.letter_spacing();
|
||||
let count = text.chars().count();
|
||||
let mut amounts = vec![(0.0, 0.0); count];
|
||||
for &(index, amount) in extra {
|
||||
if let Some((added, _)) = amounts.get_mut(index) {
|
||||
*added += amount;
|
||||
}
|
||||
}
|
||||
for &(index, amount) in sheds {
|
||||
if let Some((_, shed)) = amounts.get_mut(index) {
|
||||
*shed += amount;
|
||||
}
|
||||
}
|
||||
let mut piece_start = 0;
|
||||
// UTF-16 offset of the span text in the builder, read at the first piece.
|
||||
let mut builder_start = None;
|
||||
let mut utf16 = 0;
|
||||
for (index, (byte, ch)) in text.char_indices().enumerate() {
|
||||
let at = utf16;
|
||||
utf16 += ch.len_utf16();
|
||||
let (added, shed) = amounts[index];
|
||||
let letter_spacing = if line_breaks::is_inserted_zero_width(ch) {
|
||||
0.0
|
||||
} else {
|
||||
tracking + added - shed
|
||||
};
|
||||
if letter_spacing == tracking {
|
||||
continue;
|
||||
}
|
||||
let start = *builder_start.get_or_insert_with(|| {
|
||||
builder
|
||||
.paragraph_builder()
|
||||
.get_text()
|
||||
.encode_utf16()
|
||||
.count()
|
||||
});
|
||||
builder.add_extra_spacing(start + at, letter_spacing - tracking);
|
||||
let mut style = text_style.clone();
|
||||
style.set_letter_spacing(letter_spacing);
|
||||
let end = byte + ch.len_utf8();
|
||||
let paragraph_builder = builder.paragraph_builder();
|
||||
add_text_with_tabs(paragraph_builder, &text[piece_start..byte], span.font_size);
|
||||
paragraph_builder.push_style(&style);
|
||||
paragraph_builder.add_text(&text[byte..end]);
|
||||
paragraph_builder.pop();
|
||||
piece_start = end;
|
||||
}
|
||||
add_text_with_tabs(
|
||||
builder.paragraph_builder(),
|
||||
&text[piece_start..],
|
||||
span.font_size,
|
||||
);
|
||||
}
|
||||
|
||||
/// The horizontal passes of one paragraph that layout, painting, position
|
||||
/// data and the editor share: layout texts, offsets, pair spacing
|
||||
/// (punctuation aki and script gaps) and long-ruby spacing. `TextContent::horizontal_plans` caches them.
|
||||
pub(crate) struct HorizontalParagraphPlan {
|
||||
pub(crate) texts: Vec<String>,
|
||||
pub(crate) offsets: HorizontalOffsets,
|
||||
pub(crate) pair_spacing: Vec<Vec<(usize, f32)>>,
|
||||
pub(crate) ruby_spacing: HorizontalRubySpacing,
|
||||
emergency_breaks: Vec<usize>,
|
||||
overflow_width: Option<f32>,
|
||||
}
|
||||
|
||||
impl HorizontalParagraphPlan {
|
||||
pub(crate) fn new(paragraph: &Paragraph) -> Self {
|
||||
Self::with_emergency_breaks(paragraph, &[])
|
||||
}
|
||||
|
||||
pub(super) fn with_emergency_breaks(paragraph: &Paragraph, emergency: &[usize]) -> Self {
|
||||
Self::with_breaks(paragraph, emergency, &[])
|
||||
}
|
||||
|
||||
pub(super) fn with_breaks(
|
||||
paragraph: &Paragraph,
|
||||
emergency: &[usize],
|
||||
forced: &[usize],
|
||||
) -> Self {
|
||||
let (texts, offset_map) = layout_span_texts_with_breaks(paragraph, emergency, forced);
|
||||
let pair_spacing = horizontal_pair_spacing(paragraph, &texts);
|
||||
let ruby_spacing = horizontal_ruby_spacing(paragraph, &texts, &offset_map);
|
||||
let offsets = HorizontalOffsets::from_layout_texts(paragraph, &texts, offset_map);
|
||||
Self {
|
||||
texts,
|
||||
offsets,
|
||||
pair_spacing,
|
||||
ruby_spacing,
|
||||
emergency_breaks: emergency.to_vec(),
|
||||
overflow_width: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Single-line paragraph for an annotation run. Laid out unbounded: a
|
||||
/// re-layout at its own `longest_line()` can wrap the last glyph.
|
||||
fn mini_paragraph(
|
||||
text: &str,
|
||||
style: &skia::textlayout::TextStyle,
|
||||
fonts: &skia::textlayout::FontCollection,
|
||||
) -> LayoutParagraph {
|
||||
let mut builder = ParagraphBuilder::new(&ParagraphStyle::default(), fonts);
|
||||
builder.push_style(style);
|
||||
builder.add_text(text);
|
||||
let mut paragraph = LayoutParagraph::build(&mut builder);
|
||||
paragraph.layout(f32::MAX);
|
||||
paragraph
|
||||
}
|
||||
|
||||
pub(crate) fn horizontal_span_style(
|
||||
laid_out: &LayoutParagraph,
|
||||
range: &HorizontalSpanRange,
|
||||
) -> Option<skia::textlayout::TextStyle> {
|
||||
// Lines use UTF-16, while their style entries use UTF-8 run starts.
|
||||
// Skia can merge neighboring spans into one run, so an exact lookup at
|
||||
// the later span's anchor may be empty. Recover the preceding style on
|
||||
// the line containing the base, without carrying a style across lines.
|
||||
let lines = laid_out.get_line_metrics();
|
||||
let line = lines.iter().find(|line| {
|
||||
line.start_index <= range.style_anchor_utf16
|
||||
&& range.style_anchor_utf16 < line.end_including_newline
|
||||
})?;
|
||||
line.get_style_metrics(0..range.style_anchor_start.saturating_add(1))
|
||||
.last()
|
||||
.map(|(_, metric)| metric.text_style.clone())
|
||||
}
|
||||
|
||||
/// Top of the em box of a horizontal base laid out in `rect`, whose builder
|
||||
/// text starts at UTF-16 `base_start`. Annotations attach to this box: the
|
||||
/// ideographic em box (OS/2 typo metrics) of the font that shaped the base,
|
||||
/// centred on the line baseline. Skia's tight rect spans the hhea ascent and
|
||||
/// descent instead, which in CJK faces reach past the em box.
|
||||
pub(crate) fn horizontal_base_em_top(
|
||||
laid_out: &LayoutParagraph,
|
||||
lines: &[skia::textlayout::LineMetrics],
|
||||
base_start: usize,
|
||||
font_size: f32,
|
||||
rect: skia::Rect,
|
||||
) -> f32 {
|
||||
let middle = rect.center_y();
|
||||
let Some(line) = lines.iter().find(|line| {
|
||||
let baseline = line.baseline as f32;
|
||||
middle >= baseline - line.ascent as f32 && middle <= baseline + line.descent as f32
|
||||
}) else {
|
||||
return rect.top.max(rect.bottom - font_size);
|
||||
};
|
||||
let font = laid_out.font_at_utf16(base_start.max(line.start_index));
|
||||
let (ascent, descent) = super::text_shaping::upright_centre_metrics(&font);
|
||||
line.baseline as f32 + (ascent + descent - font_size) / 2.0
|
||||
}
|
||||
258
render-wasm/src/shapes/text_horizontal/offsets.rs
Normal file
258
render-wasm/src/shapes/text_horizontal/offsets.rs
Normal file
@ -0,0 +1,258 @@
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct HorizontalSpanRange {
|
||||
pub span: usize,
|
||||
pub builder_start: usize,
|
||||
pub builder_end: usize,
|
||||
pub shifted_start: usize,
|
||||
pub source_start: usize,
|
||||
pub source_end: usize,
|
||||
pub warichu: bool,
|
||||
/// UTF-8 byte anchor for indexed Skia style metrics.
|
||||
pub style_anchor_start: usize,
|
||||
/// UTF-16 anchor for choosing the line containing those metrics.
|
||||
pub style_anchor_utf16: usize,
|
||||
}
|
||||
|
||||
/// Offset maps of one horizontal paragraph between source characters, the
|
||||
/// kinsoku-adjusted layout text and the builder text. Building it runs the
|
||||
/// kinsoku pass; reuse it for every lookup in the paragraph.
|
||||
pub(crate) struct HorizontalOffsets {
|
||||
/// Map between the transformed text and the kinsoku-adjusted text.
|
||||
pub(crate) offset_map: line_breaks::OffsetMap,
|
||||
pub(crate) ranges: Vec<HorizontalSpanRange>,
|
||||
/// Transformed-text UTF-16 offset of every source character boundary.
|
||||
boundaries: Vec<usize>,
|
||||
}
|
||||
|
||||
impl HorizontalOffsets {
|
||||
#[cfg(test)]
|
||||
pub(crate) fn new(paragraph: &Paragraph) -> Self {
|
||||
let (span_texts, offset_map) = paragraph.layout_span_texts();
|
||||
Self::from_layout_texts(paragraph, &span_texts, offset_map)
|
||||
}
|
||||
|
||||
/// Offsets for layout `span_texts` already built by `layout_span_texts`.
|
||||
pub(super) fn from_layout_texts(
|
||||
paragraph: &Paragraph,
|
||||
span_texts: &[String],
|
||||
offset_map: line_breaks::OffsetMap,
|
||||
) -> Self {
|
||||
let ranges = span_ranges(paragraph, span_texts, &offset_map);
|
||||
Self {
|
||||
offset_map,
|
||||
ranges,
|
||||
boundaries: source_char_boundaries(paragraph),
|
||||
}
|
||||
}
|
||||
|
||||
/// Builder-text UTF-16 offset of a paragraph source character offset.
|
||||
pub(crate) fn source_to_builder(&self, source_char_offset: usize) -> usize {
|
||||
let boundaries = &self.boundaries;
|
||||
let source_utf16 = boundaries
|
||||
.get(source_char_offset)
|
||||
.copied()
|
||||
.unwrap_or_else(|| boundaries.last().copied().unwrap_or(0));
|
||||
self.transformed_to_builder(source_utf16)
|
||||
}
|
||||
|
||||
pub(super) fn transformed_to_builder(&self, source_utf16: usize) -> usize {
|
||||
let at = self
|
||||
.ranges
|
||||
.partition_point(|range| range.source_end < source_utf16);
|
||||
let Some(range) = self
|
||||
.ranges
|
||||
.get(at)
|
||||
.filter(|range| source_utf16 >= range.source_start)
|
||||
else {
|
||||
return self
|
||||
.ranges
|
||||
.last()
|
||||
.map(|range| range.builder_end)
|
||||
.unwrap_or(0);
|
||||
};
|
||||
if range.warichu {
|
||||
return if source_utf16 >= range.source_end {
|
||||
range.builder_end
|
||||
} else {
|
||||
range.builder_start
|
||||
};
|
||||
}
|
||||
let shifted = self.offset_map.to_shifted(source_utf16);
|
||||
range.builder_start + shifted.saturating_sub(range.shifted_start)
|
||||
}
|
||||
|
||||
/// Paragraph source character offset of a builder-text UTF-16 offset.
|
||||
pub(crate) fn builder_to_source(&self, builder_offset: usize) -> usize {
|
||||
let source_utf16 = self.builder_to_transformed(builder_offset);
|
||||
let boundaries = &self.boundaries;
|
||||
boundaries
|
||||
.partition_point(|boundary| *boundary < source_utf16)
|
||||
.min(boundaries.len().saturating_sub(1))
|
||||
}
|
||||
|
||||
pub(super) fn builder_to_transformed(&self, builder_offset: usize) -> usize {
|
||||
self.ranges
|
||||
.iter()
|
||||
.find(|range| {
|
||||
builder_offset >= range.builder_start && builder_offset <= range.builder_end
|
||||
})
|
||||
.map(|range| {
|
||||
if range.warichu {
|
||||
if builder_offset > range.builder_start {
|
||||
range.source_end
|
||||
} else {
|
||||
range.source_start
|
||||
}
|
||||
} else {
|
||||
let within = builder_offset
|
||||
.saturating_sub(range.builder_start)
|
||||
.min(range.builder_end - range.builder_start);
|
||||
self.offset_map.to_original(range.shifted_start + within)
|
||||
}
|
||||
})
|
||||
.unwrap_or_else(|| {
|
||||
self.ranges
|
||||
.last()
|
||||
.map(|range| range.source_end)
|
||||
.unwrap_or(0)
|
||||
})
|
||||
}
|
||||
|
||||
/// Builder-text ranges of the selected source characters
|
||||
/// `[source_start, source_end)` outside warichu spans.
|
||||
pub(crate) fn normal_selection_ranges(
|
||||
&self,
|
||||
paragraph: &Paragraph,
|
||||
source_start: usize,
|
||||
source_end: usize,
|
||||
) -> Vec<std::ops::Range<usize>> {
|
||||
let mut span_start = 0usize;
|
||||
paragraph
|
||||
.children()
|
||||
.iter()
|
||||
.filter_map(|span| {
|
||||
let span_end = span_start + span.text.chars().count();
|
||||
let selected_start = source_start.max(span_start);
|
||||
let selected_end = source_end.min(span_end);
|
||||
span_start = span_end;
|
||||
if span.is_warichu() || selected_start >= selected_end {
|
||||
return None;
|
||||
}
|
||||
Some(self.source_to_builder(selected_start)..self.source_to_builder(selected_end))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn horizontal_span_ranges(paragraph: &Paragraph) -> Vec<HorizontalSpanRange> {
|
||||
HorizontalOffsets::new(paragraph).ranges
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn horizontal_builder_to_source(paragraph: &Paragraph, builder_offset: usize) -> usize {
|
||||
HorizontalOffsets::new(paragraph).builder_to_source(builder_offset)
|
||||
}
|
||||
|
||||
/// Ranges shared by layout, position data and editor mapping. `shifted_*`
|
||||
/// addresses the kinsoku-adjusted text; `builder_*` addresses the builder
|
||||
/// text, where each warichu span collapses to one placeholder.
|
||||
fn span_ranges(
|
||||
paragraph: &Paragraph,
|
||||
span_texts: &[String],
|
||||
offset_map: &line_breaks::OffsetMap,
|
||||
) -> Vec<HorizontalSpanRange> {
|
||||
let mut builder_cursor = 0usize;
|
||||
let mut builder_byte_cursor = 0usize;
|
||||
let mut shifted_cursor = 0usize;
|
||||
paragraph
|
||||
.children()
|
||||
.iter()
|
||||
.zip(span_texts)
|
||||
.enumerate()
|
||||
.map(|(span_index, (span, text))| {
|
||||
let shifted_start = shifted_cursor;
|
||||
shifted_cursor += text.encode_utf16().count();
|
||||
let shifted_end = shifted_cursor;
|
||||
let source_start = offset_map.to_original(shifted_start);
|
||||
let source_end = offset_map.to_original(shifted_end);
|
||||
let warichu = span.is_warichu();
|
||||
let leading_break =
|
||||
usize::from(warichu && text.starts_with(line_breaks::FORCED_LINE_BREAK));
|
||||
builder_cursor += leading_break;
|
||||
builder_byte_cursor += leading_break * line_breaks::FORCED_LINE_BREAK.len_utf8();
|
||||
let builder_start = builder_cursor;
|
||||
let (style_anchor_start, style_anchor_utf16) = if warichu {
|
||||
(
|
||||
builder_byte_cursor + '\u{FFFC}'.len_utf8(),
|
||||
builder_start + 1,
|
||||
)
|
||||
} else {
|
||||
// A forced separator belongs to the preceding line and has
|
||||
// no style metrics. Anchor annotations to the first source
|
||||
// character rather than a leading layout-only control.
|
||||
let mut shifted = shifted_start;
|
||||
let (bytes, utf16) = text
|
||||
.chars()
|
||||
.take_while(|ch| {
|
||||
let inserted = offset_map.is_inserted(shifted);
|
||||
shifted += ch.len_utf16();
|
||||
inserted
|
||||
})
|
||||
.fold((0, 0), |(bytes, utf16), ch| {
|
||||
(bytes + ch.len_utf8(), utf16 + ch.len_utf16())
|
||||
});
|
||||
(builder_byte_cursor + bytes, builder_start + utf16)
|
||||
};
|
||||
builder_cursor += if warichu {
|
||||
HORIZONTAL_WARICHU_BUILDER_LEN
|
||||
} else {
|
||||
shifted_end - shifted_start
|
||||
};
|
||||
builder_byte_cursor += if warichu {
|
||||
'\u{FFFC}'.len_utf8()
|
||||
+ HORIZONTAL_WARICHU_STYLE_ANCHOR.len_utf8()
|
||||
+ HORIZONTAL_WARICHU_BREAK_ANCHOR.len_utf8()
|
||||
} else {
|
||||
// `add_text_with_tabs` stores every tab as a U+FFFC placeholder.
|
||||
text.len() + text.matches('\t').count() * ('\u{FFFC}'.len_utf8() - 1)
|
||||
};
|
||||
HorizontalSpanRange {
|
||||
span: span_index,
|
||||
builder_start,
|
||||
builder_end: builder_cursor,
|
||||
shifted_start,
|
||||
source_start,
|
||||
source_end,
|
||||
warichu,
|
||||
style_anchor_start,
|
||||
style_anchor_utf16,
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Transformed-text UTF-16 offset of every source character boundary: the
|
||||
/// space the kinsoku offset map reports as "original". Differs from the
|
||||
/// source UTF-16 when a transform changes the text length (`ß` -> `SS`).
|
||||
fn source_char_boundaries(paragraph: &Paragraph) -> Vec<usize> {
|
||||
let mut boundaries = vec![0usize];
|
||||
let mut span_base = 0usize;
|
||||
for span in paragraph.children() {
|
||||
let applied = span.apply_text_transform_with_source_ranges();
|
||||
let mut ranges = applied.source_ranges.iter().peekable();
|
||||
let mut source_utf16 = 0usize;
|
||||
let mut output_end = 0usize;
|
||||
for character in span.text.chars() {
|
||||
source_utf16 += character.len_utf16();
|
||||
while let Some((output, _)) = ranges.next_if(|(_, source)| source.end <= source_utf16) {
|
||||
output_end = output.end;
|
||||
}
|
||||
boundaries.push(span_base + output_end);
|
||||
}
|
||||
span_base += applied.text.encode_utf16().count();
|
||||
}
|
||||
boundaries
|
||||
}
|
||||
258
render-wasm/src/shapes/text_horizontal/overflow.rs
Normal file
258
render-wasm/src/shapes/text_horizontal/overflow.rs
Normal file
@ -0,0 +1,258 @@
|
||||
//! Repair SkParagraph's emergency splits without changing the wrap budget.
|
||||
|
||||
use super::*;
|
||||
use crate::shapes::line_breaks::PolicyBoundaries;
|
||||
use crate::shapes::text_paragraph::LayoutParagraph;
|
||||
|
||||
/// The paragraph's span texts after text transform, joined.
|
||||
fn transformed_text(paragraph: &Paragraph) -> String {
|
||||
paragraph
|
||||
.children()
|
||||
.iter()
|
||||
.map(TextSpan::apply_text_transform)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// UTF-16 ranges, in the transformed text, of the ruby bases
|
||||
/// (`Paragraph::ruby_groups`) and warichu spans that must stay whole.
|
||||
fn annotation_ranges(paragraph: &Paragraph) -> Vec<std::ops::Range<usize>> {
|
||||
let group_of = paragraph.ruby_group_of_spans();
|
||||
let mut offset = 0;
|
||||
let mut ranges: Vec<(Option<usize>, std::ops::Range<usize>)> = Vec::new();
|
||||
for (index, span) in paragraph.children().iter().enumerate() {
|
||||
let start = offset;
|
||||
offset += span.apply_text_transform().encode_utf16().count();
|
||||
let group = group_of[index];
|
||||
match ranges.last_mut() {
|
||||
Some((last_group, range)) if group.is_some() && *last_group == group => {
|
||||
range.end = offset
|
||||
}
|
||||
_ if group.is_some() || span.is_warichu() => ranges.push((group, start..offset)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
ranges.into_iter().map(|(_, range)| range).collect()
|
||||
}
|
||||
|
||||
/// True when `position` falls strictly inside one of the ascending
|
||||
/// `annotations` ranges.
|
||||
fn within_annotation(annotations: &[std::ops::Range<usize>], position: usize) -> bool {
|
||||
annotations
|
||||
.get(annotations.partition_point(|r| r.end <= position))
|
||||
.is_some_and(|r| r.start < position)
|
||||
}
|
||||
|
||||
/// A finite, real wrap width; auto-width and unbounded layout skip repairs.
|
||||
fn is_wrap_width(width: f32) -> bool {
|
||||
width.is_finite() && width > 0.0 && width != f32::MAX
|
||||
}
|
||||
|
||||
impl HorizontalParagraphPlan {
|
||||
/// SkParagraph's emergency fallback can ignore punctuation joins inside
|
||||
/// an overlong word. Measure whole units once, then reopen only safe
|
||||
/// grapheme boundaries of units that cannot fit an empty line. The
|
||||
/// replacement markers have identical UTF-8/UTF-16 lengths, so every
|
||||
/// source/builder offset remains identical to the unrelaxed plan.
|
||||
pub(crate) fn relax_overlong_words(
|
||||
&mut self,
|
||||
paragraph: &Paragraph,
|
||||
width: f32,
|
||||
measure: impl FnOnce(&Self) -> LayoutParagraph,
|
||||
) {
|
||||
let policy = paragraph.layout_policy();
|
||||
if !policy.reopens_overlong_words() || !is_wrap_width(width) {
|
||||
return;
|
||||
}
|
||||
let boundaries = PolicyBoundaries::new(&transformed_text(paragraph), policy);
|
||||
let graphemes = &boundaries.graphemes;
|
||||
let annotations = annotation_ranges(paragraph);
|
||||
let mut units = Vec::new();
|
||||
let mut start = 0;
|
||||
for (at, grapheme) in graphemes.iter().enumerate().skip(1) {
|
||||
if grapheme.allowed {
|
||||
if at - start > 1 {
|
||||
units.push(start..at);
|
||||
}
|
||||
start = at;
|
||||
}
|
||||
}
|
||||
if graphemes.len() - start > 1 {
|
||||
units.push(start..graphemes.len());
|
||||
}
|
||||
if units.is_empty() {
|
||||
return;
|
||||
}
|
||||
let measured = measure(self);
|
||||
let line_starts: Vec<_> = measured
|
||||
.get_line_metrics()
|
||||
.iter()
|
||||
.skip(1)
|
||||
.map(|line| line.start_index)
|
||||
.collect();
|
||||
let mut emergency = Vec::new();
|
||||
for unit in units {
|
||||
let first = graphemes[unit.start].utf16;
|
||||
let last = graphemes
|
||||
.get(unit.end)
|
||||
.map_or(boundaries.length, |g| g.utf16);
|
||||
let from = self.offsets.transformed_to_builder(first);
|
||||
let to = self.offsets.transformed_to_builder(last);
|
||||
let split = line_starts.partition_point(|at| *at <= from);
|
||||
if line_starts.get(split).is_none_or(|at| *at >= to) {
|
||||
continue;
|
||||
}
|
||||
let extent: f32 = measured
|
||||
.get_rects_for_range(from..to, RectHeightStyle::Tight, RectWidthStyle::Tight)
|
||||
.iter()
|
||||
.map(|box_| box_.rect.width())
|
||||
.sum();
|
||||
if extent <= width + 0.01 {
|
||||
continue;
|
||||
}
|
||||
for grapheme in &graphemes[unit.start + 1..unit.end] {
|
||||
if grapheme.emergency && !within_annotation(&annotations, grapheme.utf16) {
|
||||
emergency.push(grapheme.utf16);
|
||||
}
|
||||
}
|
||||
}
|
||||
if !emergency.is_empty() {
|
||||
*self = Self::with_emergency_breaks(paragraph, &emergency);
|
||||
}
|
||||
}
|
||||
|
||||
/// Keep an indivisible unit together even when Skia's last-resort wrapping
|
||||
/// ignores its joiners. Isolate overflowing units, measure the surrounding
|
||||
/// text at the original width, and freeze those legal line starts before
|
||||
/// increasing Skia's physical width. All consumers use this same plan.
|
||||
pub(crate) fn retain_atomic_overflow(
|
||||
&mut self,
|
||||
paragraph: &Paragraph,
|
||||
width: f32,
|
||||
mut measure: impl FnMut(&Self, f32) -> LayoutParagraph,
|
||||
) {
|
||||
let policy = paragraph.layout_policy();
|
||||
if !policy.keeps_overlong_units() || !is_wrap_width(width) {
|
||||
return;
|
||||
}
|
||||
let boundaries = PolicyBoundaries::new(&transformed_text(paragraph), policy);
|
||||
let graphemes = &boundaries.graphemes;
|
||||
let length = boundaries.length;
|
||||
let annotations = annotation_ranges(paragraph);
|
||||
let hard_break = |c| matches!(c, '\n' | '\r' | '\u{2028}' | '\u{2029}');
|
||||
let mut units = Vec::new();
|
||||
let mut start = 0;
|
||||
for at in 1..graphemes.len() {
|
||||
let position = graphemes[at].utf16;
|
||||
let (before, after) = (graphemes[at - 1].first, graphemes[at].first);
|
||||
if hard_break(before)
|
||||
|| hard_break(after)
|
||||
|| (!within_annotation(&annotations, position) && graphemes[at].emergency)
|
||||
{
|
||||
if at - start > 1 {
|
||||
units.push(graphemes[start].utf16..position);
|
||||
}
|
||||
start = at;
|
||||
}
|
||||
}
|
||||
if graphemes.len() - start > 1 {
|
||||
units.push(graphemes[start].utf16..length);
|
||||
}
|
||||
if units.is_empty() {
|
||||
return;
|
||||
}
|
||||
let bounded = measure(self, width);
|
||||
let starts: Vec<_> = bounded
|
||||
.get_line_metrics()
|
||||
.iter()
|
||||
.skip(1)
|
||||
.map(|line| self.offsets.builder_to_transformed(line.start_index))
|
||||
.collect();
|
||||
units.retain(|unit| {
|
||||
starts
|
||||
.get(starts.partition_point(|at| *at <= unit.start))
|
||||
.is_some_and(|at| *at < unit.end)
|
||||
});
|
||||
if units.is_empty() {
|
||||
return;
|
||||
}
|
||||
let natural = measure(self, f32::MAX);
|
||||
let mut physical_width = width;
|
||||
units.retain(|unit| {
|
||||
let from = self.offsets.transformed_to_builder(unit.start);
|
||||
let to = self.offsets.transformed_to_builder(unit.end);
|
||||
let extent: f32 = natural
|
||||
.get_rects_for_range(from..to, RectHeightStyle::Tight, RectWidthStyle::Tight)
|
||||
.iter()
|
||||
.map(|box_| box_.rect.width())
|
||||
.sum();
|
||||
if extent <= width + 0.01 {
|
||||
return false;
|
||||
}
|
||||
physical_width = physical_width.max(extent);
|
||||
true
|
||||
});
|
||||
if units.is_empty() {
|
||||
return;
|
||||
}
|
||||
let can_insert = |at: usize| {
|
||||
at > 0
|
||||
&& at < length
|
||||
&& graphemes
|
||||
.binary_search_by_key(&at, |g| g.utf16)
|
||||
.ok()
|
||||
.is_some_and(|i| {
|
||||
!hard_break(graphemes[i].first) && !hard_break(graphemes[i - 1].first)
|
||||
})
|
||||
};
|
||||
let mut forced: Vec<_> = units
|
||||
.iter()
|
||||
.flat_map(|unit| [unit.start, unit.end])
|
||||
.filter(|at| can_insert(*at))
|
||||
.collect();
|
||||
forced.sort_unstable();
|
||||
forced.dedup();
|
||||
let isolated = Self::with_breaks(paragraph, &self.emergency_breaks, &forced);
|
||||
let bounded = measure(&isolated, width);
|
||||
physical_width = physical_width.max(bounded.longest_line());
|
||||
forced.extend(
|
||||
bounded
|
||||
.get_line_metrics()
|
||||
.iter()
|
||||
.skip(1)
|
||||
.map(|line| isolated.offsets.builder_to_transformed(line.start_index))
|
||||
.filter(|at| {
|
||||
can_insert(*at)
|
||||
&& units
|
||||
.get(units.partition_point(|unit| unit.end <= *at))
|
||||
.is_none_or(|unit| *at <= unit.start)
|
||||
}),
|
||||
);
|
||||
forced.sort_unstable();
|
||||
forced.dedup();
|
||||
*self = Self::with_breaks(paragraph, &self.emergency_breaks, &forced);
|
||||
// Avoid a floating-point edge reintroducing the very fallback being
|
||||
// repaired. The epsilon changes physical width, not the wrap budget.
|
||||
self.overflow_width = Some(physical_width + 0.01);
|
||||
}
|
||||
|
||||
pub(crate) fn layout_width(&self, width: f32) -> f32 {
|
||||
self.overflow_width.unwrap_or(width)
|
||||
}
|
||||
|
||||
/// Skia aligns against the physical width; translate it back to the box.
|
||||
pub(crate) fn x_offset(&self, paragraph: &Paragraph, width: f32) -> f32 {
|
||||
use crate::shapes::{TextAlign, TextDirection};
|
||||
let slack = width - self.layout_width(width);
|
||||
match paragraph.text_align() {
|
||||
TextAlign::Center => slack * 0.5,
|
||||
TextAlign::Right => slack,
|
||||
TextAlign::Start if paragraph.text_direction() == TextDirection::RTL => slack,
|
||||
TextAlign::End if paragraph.text_direction() == TextDirection::LTR => slack,
|
||||
_ => 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn has_atomic_overflow(&self) -> bool {
|
||||
self.overflow_width.is_some()
|
||||
}
|
||||
}
|
||||
767
render-wasm/src/shapes/text_horizontal/ruby.rs
Normal file
767
render-wasm/src/shapes/text_horizontal/ruby.rs
Normal file
@ -0,0 +1,767 @@
|
||||
use crate::render::FallbackFonts;
|
||||
use crate::shapes::text_paragraph::LayoutParagraph;
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Top-left of the Zhuyin strip beside a horizontal base whose em box starts
|
||||
/// at `em_top`.
|
||||
pub(crate) fn horizontal_zhuyin_origin(
|
||||
span: &TextSpan,
|
||||
rect: skia::Rect,
|
||||
em_top: f32,
|
||||
) -> skia::Point {
|
||||
skia::Point::new(
|
||||
rect.right() - span.font_size * super::super::text_zhuyin::STRIP_WIDTH,
|
||||
em_top,
|
||||
)
|
||||
}
|
||||
|
||||
/// Zhuyin glyphs of a horizontal base whose builder text starts at UTF-16
|
||||
/// `base_start` and whose first line rect is `rect`, with the strip's
|
||||
/// top-left in the laid-out paragraph. Position data, painting and bounds
|
||||
/// share it.
|
||||
fn horizontal_zhuyin_glyphs(
|
||||
span: &TextSpan,
|
||||
syllable: &super::super::text_zhuyin::Syllable,
|
||||
laid_out: &LayoutParagraph,
|
||||
lines: &[skia::textlayout::LineMetrics],
|
||||
base_start: usize,
|
||||
rect: skia::Rect,
|
||||
fonts: (&skia::textlayout::TypefaceFontProvider, &FallbackFonts),
|
||||
) -> (skia::Point, Vec<super::super::text_zhuyin::Glyph>) {
|
||||
let em_top = horizontal_base_em_top(laid_out, lines, base_start, span.font_size, rect);
|
||||
let origin = horizontal_zhuyin_origin(span, rect, em_top);
|
||||
let glyphs = super::super::text_zhuyin::shape(span, syllable, fonts.0, fonts.1);
|
||||
(origin, glyphs)
|
||||
}
|
||||
|
||||
pub(crate) fn horizontal_zhuyin_entries(
|
||||
paragraph: &Paragraph,
|
||||
offsets: &HorizontalOffsets,
|
||||
laid_out: &LayoutParagraph,
|
||||
) -> Vec<crate::shapes::PositionData> {
|
||||
if !paragraph.children().iter().any(TextSpan::is_zhuyin) {
|
||||
return Vec::new();
|
||||
}
|
||||
let provider = get_resources().fonts.font_provider();
|
||||
let fallbacks = get_fallback_fonts();
|
||||
let lines = laid_out.get_line_metrics();
|
||||
let mut entries = Vec::new();
|
||||
for (index, range) in horizontal_ruby_targets(paragraph, offsets) {
|
||||
let base_start = range.start;
|
||||
let span = ¶graph.children()[index];
|
||||
let Some(syllable) = super::super::text_zhuyin::syllable(span) else {
|
||||
continue;
|
||||
};
|
||||
let rects =
|
||||
laid_out.get_rects_for_range(range, RectHeightStyle::Tight, RectWidthStyle::Tight);
|
||||
let Some(rect) = rects.first() else {
|
||||
continue;
|
||||
};
|
||||
let (origin, glyphs) = horizontal_zhuyin_glyphs(
|
||||
span,
|
||||
&syllable,
|
||||
laid_out,
|
||||
&lines,
|
||||
base_start,
|
||||
rect.rect,
|
||||
(provider, fallbacks),
|
||||
);
|
||||
let size = span.ruby_font_size();
|
||||
for glyph in glyphs {
|
||||
entries.push(crate::shapes::PositionData {
|
||||
paragraph: 0,
|
||||
span: index as u32,
|
||||
start_pos: glyph.range.start as u32,
|
||||
end_pos: glyph.range.end as u32,
|
||||
x: origin.x + glyph.origin.x,
|
||||
y: origin.y + glyph.origin.y - size,
|
||||
width: size,
|
||||
height: size,
|
||||
direction: crate::shapes::DIRECTION_ZHUYIN,
|
||||
});
|
||||
}
|
||||
}
|
||||
entries
|
||||
}
|
||||
|
||||
/// Extra spacing for long horizontal ruby, per span of a paragraph.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub(crate) struct HorizontalRubySpacing {
|
||||
/// Letter-spacing added to characters of the layout texts: (char index,
|
||||
/// amount) per span.
|
||||
pub adjustments: Vec<Vec<(usize, f32)>>,
|
||||
/// Gap added between the base characters of each ruby span.
|
||||
pub base_gaps: Vec<f32>,
|
||||
/// Spacing added before each ruby base, on the previous character: half
|
||||
/// a gap, or none at a paragraph start, where the base takes it after.
|
||||
pub leading_gaps: Vec<f32>,
|
||||
/// Overhang room (before, after) of each ruby span over its neighbours.
|
||||
pub rooms: Vec<(f32, f32)>,
|
||||
}
|
||||
|
||||
/// The span that owns a character of the layout `texts`, its char index and
|
||||
/// the character, searching from (span, index) in `step` direction and
|
||||
/// skipping inserted word joiners.
|
||||
fn layout_neighbour(texts: &[String], span: usize, forward: bool) -> Option<(usize, usize, char)> {
|
||||
let real = |(index, ch): (usize, char)| {
|
||||
(!line_breaks::is_inserted_zero_width(ch)).then_some((index, ch))
|
||||
};
|
||||
if forward {
|
||||
texts
|
||||
.iter()
|
||||
.enumerate()
|
||||
.skip(span + 1)
|
||||
.find_map(|(owner, text)| {
|
||||
text.chars()
|
||||
.enumerate()
|
||||
.find_map(real)
|
||||
.map(|(index, ch)| (owner, index, ch))
|
||||
})
|
||||
} else {
|
||||
texts
|
||||
.iter()
|
||||
.enumerate()
|
||||
.take(span)
|
||||
.rev()
|
||||
.find_map(|(owner, text)| {
|
||||
let chars: Vec<char> = text.chars().collect();
|
||||
chars
|
||||
.iter()
|
||||
.copied()
|
||||
.enumerate()
|
||||
.rev()
|
||||
.find_map(real)
|
||||
.map(|(index, ch)| (owner, index, ch))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Spacing that makes room for horizontal ruby longer than its base. The
|
||||
/// reading overhangs its neighbours within their `ruby_overhang_room`, less
|
||||
/// what an earlier reading already hangs over the same character (JLREQ
|
||||
/// Fig 138); the rest spreads the base (JLREQ §3.3.8): an equal gap between base
|
||||
/// characters and half a gap at each end, the leading half on the previous
|
||||
/// character.
|
||||
pub(crate) fn horizontal_ruby_spacing(
|
||||
paragraph: &Paragraph,
|
||||
texts: &[String],
|
||||
offset_map: &line_breaks::OffsetMap,
|
||||
) -> HorizontalRubySpacing {
|
||||
if !paragraph.children().iter().any(TextSpan::has_ruby) {
|
||||
return HorizontalRubySpacing::empty(texts.len());
|
||||
}
|
||||
horizontal_ruby_spacing_with(
|
||||
paragraph,
|
||||
texts,
|
||||
offset_map,
|
||||
get_resources().fonts.font_provider(),
|
||||
get_fallback_fonts(),
|
||||
)
|
||||
}
|
||||
|
||||
impl HorizontalRubySpacing {
|
||||
fn empty(spans: usize) -> Self {
|
||||
Self {
|
||||
adjustments: vec![Vec::new(); spans],
|
||||
base_gaps: vec![0.0; spans],
|
||||
leading_gaps: vec![0.0; spans],
|
||||
rooms: vec![(0.0, 0.0); spans],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `horizontal_ruby_spacing` with explicit fonts.
|
||||
pub(super) fn horizontal_ruby_spacing_with(
|
||||
paragraph: &Paragraph,
|
||||
texts: &[String],
|
||||
offset_map: &line_breaks::OffsetMap,
|
||||
font_provider: &skia::textlayout::TypefaceFontProvider,
|
||||
fallback_families: &FallbackFonts,
|
||||
) -> HorizontalRubySpacing {
|
||||
let spans = paragraph.children();
|
||||
let mut spacing = HorizontalRubySpacing::empty(texts.len());
|
||||
let fallback_mgr = FontMgr::from(font_provider.clone());
|
||||
let advances = |span: &TextSpan, text: &str, font_size: f32| -> Vec<f32> {
|
||||
shape_segment_with_fallbacks(
|
||||
text,
|
||||
font_size,
|
||||
&span_font_families(span, fallback_families),
|
||||
font_provider,
|
||||
false,
|
||||
span.font_features,
|
||||
&fallback_mgr,
|
||||
&span.language,
|
||||
)
|
||||
.iter()
|
||||
.flat_map(|run| run.advances.iter().copied())
|
||||
.collect()
|
||||
};
|
||||
let mut claims = OverhangClaims::new();
|
||||
// UTF-16 start of each span in the layout text.
|
||||
let span_starts: Vec<usize> = texts
|
||||
.iter()
|
||||
.scan(0usize, |start, text| {
|
||||
let span_start = *start;
|
||||
*start += text.encode_utf16().count();
|
||||
Some(span_start)
|
||||
})
|
||||
.collect();
|
||||
for group in paragraph.ruby_groups() {
|
||||
// The first span holds the reading and its presentation.
|
||||
let index = group.start;
|
||||
let span = &spans[index];
|
||||
// Source characters of the base, as (span, char index); inserted
|
||||
// spacing and joiners are not.
|
||||
let base: Vec<(usize, usize)> = group
|
||||
.clone()
|
||||
.flat_map(|member| {
|
||||
let mut utf16 = span_starts[member];
|
||||
texts[member]
|
||||
.chars()
|
||||
.enumerate()
|
||||
.filter(move |(_, ch)| {
|
||||
let inserted = offset_map.is_inserted(utf16);
|
||||
utf16 += ch.len_utf16();
|
||||
!inserted
|
||||
})
|
||||
.map(move |(char_index, _)| (member, char_index))
|
||||
})
|
||||
.collect();
|
||||
let Some(&last) = base.last() else {
|
||||
continue;
|
||||
};
|
||||
if span.is_zhuyin() {
|
||||
// Inter-character annotations consume inline space, not ruby's
|
||||
// above/below leading. Reserve the strip even at a line end.
|
||||
for (member, char_index) in &base {
|
||||
spacing.adjustments[*member].push((
|
||||
*char_index,
|
||||
span.font_size * super::super::text_zhuyin::STRIP_WIDTH,
|
||||
));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let ruby_font_size = span.ruby_font_size();
|
||||
let base_width: f32 = group
|
||||
.clone()
|
||||
.map(|member| {
|
||||
let member_span = &spans[member];
|
||||
let chars = base.iter().filter(|(owner, _)| *owner == member).count();
|
||||
advances(
|
||||
member_span,
|
||||
&member_span.apply_text_transform(),
|
||||
member_span.font_size,
|
||||
)
|
||||
.iter()
|
||||
.sum::<f32>()
|
||||
+ member_span.letter_spacing * chars as f32
|
||||
})
|
||||
.sum();
|
||||
// The painter sets the reading in pieces (see `RubyLine::paint`).
|
||||
let shaped = shape_segment_with_fallbacks(
|
||||
span.ruby_text(),
|
||||
ruby_font_size,
|
||||
&span_font_families(span, fallback_families),
|
||||
font_provider,
|
||||
false,
|
||||
span.font_features,
|
||||
&fallback_mgr,
|
||||
&span.language,
|
||||
);
|
||||
let (_, pieces, _) =
|
||||
horizontal_ruby_pieces(&ruby_glyph_refs(span.ruby_text(), &shaped, ruby_font_size));
|
||||
let ruby_length: f32 = pieces.iter().sum();
|
||||
let previous = layout_neighbour(texts, group.start, false);
|
||||
let next = layout_neighbour(texts, group.end - 1, true);
|
||||
let room = |neighbour: Option<(usize, usize, char)>| {
|
||||
neighbour.map_or(0.0, |(owner, _, ch)| {
|
||||
ruby_overhang_room(
|
||||
span.ruby_overhang,
|
||||
Some(ch),
|
||||
spans[owner].has_ruby(),
|
||||
spans[owner].font_size,
|
||||
ruby_font_size,
|
||||
)
|
||||
})
|
||||
};
|
||||
let key = |(owner, char_index, _): (usize, usize, char)| (owner, char_index);
|
||||
let rooms = claims.room(
|
||||
previous.map(key),
|
||||
next.map(key),
|
||||
(room(previous), room(next)),
|
||||
ruby_length - base_width,
|
||||
);
|
||||
spacing.rooms[index] = rooms;
|
||||
let gap = long_ruby_gap(ruby_length, base_width, rooms, base.len());
|
||||
if gap <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
spacing.base_gaps[index] = gap;
|
||||
for &(member, char_index) in &base {
|
||||
let amount = if (member, char_index) == last {
|
||||
gap / 2.0
|
||||
} else {
|
||||
gap
|
||||
};
|
||||
spacing.adjustments[member].push((char_index, amount));
|
||||
}
|
||||
match previous {
|
||||
Some((owner, char_index, _)) => {
|
||||
spacing.adjustments[owner].push((char_index, gap / 2.0));
|
||||
spacing.leading_gaps[index] = gap / 2.0;
|
||||
}
|
||||
None => spacing.adjustments[last.0].push((last.1, gap / 2.0)),
|
||||
}
|
||||
}
|
||||
spacing
|
||||
}
|
||||
|
||||
/// Split `total` glyphs across segments proportionally to their extents
|
||||
/// (rounded per segment, remainder to the last) so no glyph is dropped.
|
||||
pub(super) fn split_counts_by_extent(extents: &[f32], total: usize) -> Vec<usize> {
|
||||
let mut counts = vec![0usize; extents.len()];
|
||||
if extents.is_empty() || total == 0 {
|
||||
return counts;
|
||||
}
|
||||
let sum: f32 = extents.iter().sum();
|
||||
if sum <= 0.0 {
|
||||
counts[extents.len() - 1] = total;
|
||||
return counts;
|
||||
}
|
||||
let mut assigned = 0usize;
|
||||
let last = extents.len() - 1;
|
||||
for (index, extent) in extents.iter().enumerate() {
|
||||
let count = if index == last {
|
||||
total - assigned
|
||||
} else {
|
||||
(((extent / sum) * total as f32).round() as usize).min(total - assigned)
|
||||
};
|
||||
counts[index] = count;
|
||||
assigned += count;
|
||||
}
|
||||
counts
|
||||
}
|
||||
|
||||
/// (first span index, builder-text range) of every base whose ruby is
|
||||
/// painted: visible ruby on a base that is not warichu. A base covers the
|
||||
/// spans of its `Paragraph::ruby_groups` entry; its first span holds the
|
||||
/// reading and its presentation.
|
||||
pub(super) fn horizontal_ruby_targets(
|
||||
paragraph: &Paragraph,
|
||||
offsets: &HorizontalOffsets,
|
||||
) -> Vec<(usize, std::ops::Range<usize>)> {
|
||||
paragraph
|
||||
.ruby_groups()
|
||||
.into_iter()
|
||||
.filter_map(|spans| {
|
||||
let first = offsets.ranges.get(spans.start)?;
|
||||
let last = offsets.ranges.get(spans.end - 1)?;
|
||||
let start = offsets.transformed_to_builder(first.source_start);
|
||||
(start < last.builder_end).then_some((first.span, start..last.builder_end))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Rects of a base range joined per line: a base that crosses spans or
|
||||
/// font runs gets one rect for each run, but its reading spreads over the
|
||||
/// whole line part.
|
||||
fn base_line_rects(rects: Vec<skia::textlayout::TextBox>) -> Vec<skia::textlayout::TextBox> {
|
||||
let mut lines: Vec<skia::textlayout::TextBox> = Vec::new();
|
||||
for rect in rects {
|
||||
match lines.last_mut() {
|
||||
Some(line)
|
||||
if rect.rect.top() < line.rect.bottom() && rect.rect.bottom() > line.rect.top() =>
|
||||
{
|
||||
line.rect.join(rect.rect)
|
||||
}
|
||||
_ => lines.push(rect),
|
||||
}
|
||||
}
|
||||
lines
|
||||
}
|
||||
|
||||
/// Paint of the pass that laid out the base: fill, stroke, shadow or mask.
|
||||
/// `fallback` covers a base Skia kept no style metrics for.
|
||||
pub(super) fn horizontal_ruby_paint(
|
||||
laid_out: &LayoutParagraph,
|
||||
range: &HorizontalSpanRange,
|
||||
fallback: skia::Paint,
|
||||
) -> skia::Paint {
|
||||
horizontal_span_style(laid_out, range).map_or(fallback, |style| style.foreground())
|
||||
}
|
||||
|
||||
/// Ink edge of `glyph` (bottom when `over`, else top), used to attach ruby
|
||||
/// ink to its base. Font-wide ascent/descent include leading, which leaves
|
||||
/// ruby detached in many Japanese faces.
|
||||
fn horizontal_ruby_ink_edge(font: &Font, glyph: GlyphId, fallback: f32, over: bool) -> f32 {
|
||||
let mut bounds = [skia::Rect::default()];
|
||||
font.get_bounds(&[glyph], &mut bounds, None);
|
||||
let bound = bounds[0];
|
||||
if bound.right > bound.left && bound.bottom > bound.top {
|
||||
if over {
|
||||
bound.bottom
|
||||
} else {
|
||||
bound.top
|
||||
}
|
||||
} else {
|
||||
fallback
|
||||
}
|
||||
}
|
||||
|
||||
/// One shaped ruby glyph: (run index, glyph index in the run, advance,
|
||||
/// whether it belongs to a Western word).
|
||||
pub(super) type RubyGlyphRef = (usize, usize, f32, bool);
|
||||
|
||||
/// The glyphs of a reading shaped from `ruby_text`, in order.
|
||||
pub(super) fn ruby_glyph_refs(
|
||||
ruby_text: &str,
|
||||
shaped: &[ShapedRun],
|
||||
ruby_font_size: f32,
|
||||
) -> Vec<RubyGlyphRef> {
|
||||
shaped
|
||||
.iter()
|
||||
.enumerate()
|
||||
.flat_map(|(run_index, run)| {
|
||||
(0..run.glyphs.len()).map(move |glyph| {
|
||||
let advance = run.advances.get(glyph).copied().unwrap_or(ruby_font_size);
|
||||
let western = run
|
||||
.clusters
|
||||
.get(glyph)
|
||||
.and_then(|cluster| ruby_text.get(*cluster as usize..))
|
||||
.and_then(|rest| rest.chars().next())
|
||||
.is_some_and(|ch| !is_upright_char(ch));
|
||||
(run_index, glyph, advance, western)
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Pieces of a horizontal reading (`ruby_pieces`): each upright glyph in a
|
||||
/// slot as wide as the widest upright glyph, each Western word whole.
|
||||
/// Returns the slot, the piece extents and each glyph's piece and offset.
|
||||
pub(super) fn horizontal_ruby_pieces(
|
||||
glyphs: &[RubyGlyphRef],
|
||||
) -> (f32, Vec<f32>, Vec<(usize, f32)>) {
|
||||
let slot = glyphs
|
||||
.iter()
|
||||
.filter(|(_, _, _, western)| !western)
|
||||
.map(|(_, _, advance, _)| *advance)
|
||||
.fold(0.0f32, f32::max)
|
||||
.max(1.0);
|
||||
let flags: Vec<(bool, f32)> = glyphs
|
||||
.iter()
|
||||
.map(|(_, _, advance, western)| (*western, *advance))
|
||||
.collect();
|
||||
let (extents, placement) = ruby_pieces(&flags, slot);
|
||||
(slot, extents, placement)
|
||||
}
|
||||
|
||||
/// Ink edge shared by the glyphs of one ruby line: the lowest ink bottom for
|
||||
/// over ruby, the highest ink top for under ruby. One edge for the whole line
|
||||
/// keeps marks whose ink sits mid-height, such as ー and ・, on the line's
|
||||
/// baseline instead of pulling each of them onto the base. Western letters
|
||||
/// (Pinyin, romaji) take the font's descent and cap height instead of their
|
||||
/// ink, so every reading keeps one baseline whatever its descenders, ascenders
|
||||
/// or tone marks.
|
||||
pub(super) fn ruby_line_ink_edge(shaped: &[ShapedRun], glyphs: &[RubyGlyphRef], over: bool) -> f32 {
|
||||
let edges = glyphs.iter().map(|(run_index, glyph, _, western)| {
|
||||
let run = &shaped[*run_index];
|
||||
let (_, metrics) = run.font.metrics();
|
||||
if *western {
|
||||
if over {
|
||||
return metrics.descent;
|
||||
}
|
||||
if metrics.cap_height > 0.0 {
|
||||
return -metrics.cap_height;
|
||||
}
|
||||
}
|
||||
let fallback = if over {
|
||||
metrics.descent
|
||||
} else {
|
||||
metrics.ascent
|
||||
};
|
||||
horizontal_ruby_ink_edge(&run.font, run.glyphs[*glyph], fallback, over)
|
||||
});
|
||||
let edge = if over {
|
||||
edges.fold(f32::MIN, f32::max)
|
||||
} else {
|
||||
edges.fold(f32::MAX, f32::min)
|
||||
};
|
||||
if edge.is_finite() && edge.abs() < f32::MAX {
|
||||
edge
|
||||
} else {
|
||||
0.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Paint ruby for one laid-out horizontal paragraph. Draw-only: base rects
|
||||
/// come from `get_rects_for_range`, and the ruby is shaped at
|
||||
/// `ruby_font_size` and spread over each line's rect with the vertical path's
|
||||
/// jlreq distribution (`distribute_ruby_pieces`). Lines are not reflowed; ruby
|
||||
/// draws in the line's leading.
|
||||
pub(crate) fn paint_horizontal_ruby(
|
||||
canvas: &Canvas,
|
||||
text_content: &TextContent,
|
||||
paragraph_index: usize,
|
||||
laid_out: &LayoutParagraph,
|
||||
x: f32,
|
||||
y: f32,
|
||||
) {
|
||||
visit_horizontal_ruby_glyphs(
|
||||
text_content,
|
||||
paragraph_index,
|
||||
laid_out,
|
||||
x,
|
||||
y,
|
||||
|font, glyph, origin, paint| {
|
||||
if let Some(blob) = single_glyph_blob(font, glyph) {
|
||||
canvas.draw_text_blob(&blob, origin, paint);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// Ink bounds of the horizontal readings (ruby and Zhuyin) of a paragraph
|
||||
/// laid out at (`x`, `y`), or None without readings.
|
||||
pub(crate) fn horizontal_ruby_ink_bounds(
|
||||
text_content: &TextContent,
|
||||
paragraph_index: usize,
|
||||
laid_out: &LayoutParagraph,
|
||||
x: f32,
|
||||
y: f32,
|
||||
) -> Option<skia::Rect> {
|
||||
let mut bounds: Option<skia::Rect> = None;
|
||||
visit_horizontal_ruby_glyphs(
|
||||
text_content,
|
||||
paragraph_index,
|
||||
laid_out,
|
||||
x,
|
||||
y,
|
||||
|font, glyph, origin, _| {
|
||||
let mut ink = [skia::Rect::default()];
|
||||
font.get_bounds(&[glyph], &mut ink, None);
|
||||
let ink = ink[0].with_offset(origin);
|
||||
if !ink.is_empty() {
|
||||
bounds = Some(match bounds {
|
||||
Some(mut joined) => {
|
||||
joined.join(ink);
|
||||
joined
|
||||
}
|
||||
None => ink,
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
bounds
|
||||
}
|
||||
|
||||
/// Each reading glyph of a horizontal paragraph laid out at (`x`, `y`): its
|
||||
/// font, glyph id, baseline origin and paint. Painting and bounds share it,
|
||||
/// so exports keep exactly the ink the canvas draws.
|
||||
fn visit_horizontal_ruby_glyphs(
|
||||
text_content: &TextContent,
|
||||
paragraph_index: usize,
|
||||
laid_out: &LayoutParagraph,
|
||||
x: f32,
|
||||
y: f32,
|
||||
mut visit: impl FnMut(&skia::Font, skia::GlyphId, (f32, f32), &skia::Paint),
|
||||
) {
|
||||
let Some(paragraph) = text_content.paragraphs().get(paragraph_index) else {
|
||||
return;
|
||||
};
|
||||
if !paragraph.children().iter().any(TextSpan::has_ruby) {
|
||||
return;
|
||||
}
|
||||
let font_provider = get_resources().fonts.font_provider();
|
||||
let fallback_mgr = FontMgr::from(font_provider.clone());
|
||||
let fallback_families = get_fallback_fonts();
|
||||
let bounds = text_content.bounds();
|
||||
let plans = text_content.horizontal_plans();
|
||||
let Some(plan) = plans.get(paragraph_index) else {
|
||||
return;
|
||||
};
|
||||
let offsets = &plan.offsets;
|
||||
let lines = laid_out.get_line_metrics();
|
||||
|
||||
for (span_index, span_range) in horizontal_ruby_targets(paragraph, offsets) {
|
||||
let span = ¶graph.children()[span_index];
|
||||
let base_start = span_range.start;
|
||||
let paint = horizontal_ruby_paint(
|
||||
laid_out,
|
||||
&offsets.ranges[span_index],
|
||||
merge_fills(&span.fills, bounds),
|
||||
);
|
||||
let rects = base_line_rects(laid_out.get_rects_for_range(
|
||||
span_range,
|
||||
skia::textlayout::RectHeightStyle::Tight,
|
||||
skia::textlayout::RectWidthStyle::Tight,
|
||||
));
|
||||
if let Some(syllable) = super::super::text_zhuyin::syllable(span) {
|
||||
if let Some(rect) = rects.first() {
|
||||
let (origin, glyphs) = horizontal_zhuyin_glyphs(
|
||||
span,
|
||||
&syllable,
|
||||
laid_out,
|
||||
&lines,
|
||||
base_start,
|
||||
rect.rect,
|
||||
(font_provider, fallback_families),
|
||||
);
|
||||
for glyph in glyphs {
|
||||
visit(
|
||||
&glyph.run.font,
|
||||
glyph.run.glyphs[glyph.glyph],
|
||||
(x + origin.x + glyph.origin.x, y + origin.y + glyph.origin.y),
|
||||
&paint,
|
||||
);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let shaped = shape_segment_with_fallbacks(
|
||||
span.ruby_text(),
|
||||
span.ruby_font_size(),
|
||||
&span_font_families(span, fallback_families),
|
||||
font_provider,
|
||||
false,
|
||||
span.font_features,
|
||||
&fallback_mgr,
|
||||
&span.language,
|
||||
);
|
||||
let glyphs = ruby_glyph_refs(span.ruby_text(), &shaped, span.ruby_font_size());
|
||||
let extents: Vec<f32> = rects.iter().map(|rect| rect.rect.width()).collect();
|
||||
let counts = split_counts_by_extent(&extents, glyphs.len());
|
||||
let mut assigned = 0usize;
|
||||
for (rect_box, count) in rects.iter().zip(counts) {
|
||||
let slice = &glyphs[assigned..assigned + count];
|
||||
assigned += count;
|
||||
if slice.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let leading_gap = plan.ruby_spacing.leading_gaps[span_index];
|
||||
let room = room_inside_line(
|
||||
&lines,
|
||||
rect_box.rect,
|
||||
rect_box.rect.left() - leading_gap,
|
||||
rect_box.rect.width() + leading_gap,
|
||||
plan.ruby_spacing.rooms[span_index],
|
||||
);
|
||||
let ruby_line = RubyLine {
|
||||
span,
|
||||
shaped: &shaped,
|
||||
glyphs: slice,
|
||||
base: rect_box.rect,
|
||||
em_top: horizontal_base_em_top(
|
||||
laid_out,
|
||||
&lines,
|
||||
base_start,
|
||||
span.font_size,
|
||||
rect_box.rect,
|
||||
),
|
||||
leading_gap,
|
||||
room,
|
||||
};
|
||||
ruby_line.visit_glyphs(x, y, |font, glyph, origin| {
|
||||
visit(font, glyph, origin, &paint)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The ruby glyphs over one line's rect of a base.
|
||||
pub(super) struct RubyLine<'a> {
|
||||
pub span: &'a TextSpan,
|
||||
pub shaped: &'a [ShapedRun],
|
||||
pub glyphs: &'a [RubyGlyphRef],
|
||||
/// Base rect in the laid-out paragraph.
|
||||
pub base: skia::Rect,
|
||||
/// Top of the base em box (`horizontal_base_em_top`).
|
||||
pub em_top: f32,
|
||||
/// Spacing before the base, which sits on the previous character.
|
||||
pub leading_gap: f32,
|
||||
/// Overhang room (before, after) inside the line.
|
||||
pub room: (f32, f32),
|
||||
}
|
||||
|
||||
impl RubyLine<'_> {
|
||||
/// Glyphs set in pieces (`horizontal_ruby_pieces`), distributed over the
|
||||
/// base and the spacing before it, with their baseline origins.
|
||||
fn visit_glyphs(
|
||||
&self,
|
||||
x: f32,
|
||||
y: f32,
|
||||
mut visit: impl FnMut(&skia::Font, skia::GlyphId, (f32, f32)),
|
||||
) {
|
||||
let baseline = self.baseline(y);
|
||||
for ((run_index, glyph, _, _), left) in self.glyphs.iter().zip(self.glyph_lefts()) {
|
||||
let run = &self.shaped[*run_index];
|
||||
visit(&run.font, run.glyphs[*glyph], (x + left, baseline));
|
||||
}
|
||||
}
|
||||
|
||||
/// Left edge of each glyph: upright glyphs centred in their slot, the
|
||||
/// letters of a Western word at their own advances.
|
||||
pub(super) fn glyph_lefts(&self) -> Vec<f32> {
|
||||
let (slot, pieces, placement) = horizontal_ruby_pieces(self.glyphs);
|
||||
let piece_lefts = distribute_ruby_pieces(
|
||||
self.base.left() - self.leading_gap,
|
||||
self.base.width() + self.leading_gap,
|
||||
&pieces,
|
||||
self.span.ruby_align,
|
||||
self.room,
|
||||
);
|
||||
self.glyphs
|
||||
.iter()
|
||||
.zip(placement)
|
||||
.map(|((_, _, advance, western), (piece, offset))| {
|
||||
let centring = if *western {
|
||||
0.0
|
||||
} else {
|
||||
(slot - advance) / 2.0
|
||||
};
|
||||
piece_lefts[piece] + offset + centring
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Baseline of the line that attaches its ink to the base: above the base
|
||||
/// em for over ruby, below the base rect for under ruby.
|
||||
pub(super) fn baseline(&self, y: f32) -> f32 {
|
||||
let over = self.span.ruby_side == AnnotationSide::Over;
|
||||
let edge = ruby_line_ink_edge(self.shaped, self.glyphs, over);
|
||||
if over {
|
||||
y + self.em_top - edge - self.span.ruby_decoration_offset(false)
|
||||
} else {
|
||||
y + self.base.bottom() - edge + self.span.ruby_decoration_offset(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Overhang room of a horizontal ruby base, without the sides at its line's
|
||||
/// edges: ruby never sticks out past the first or last character.
|
||||
fn room_inside_line(
|
||||
lines: &[skia::textlayout::LineMetrics],
|
||||
rect: skia::Rect,
|
||||
left: f32,
|
||||
width: f32,
|
||||
room: (f32, f32),
|
||||
) -> (f32, f32) {
|
||||
let middle = rect.center_y();
|
||||
let Some(line) = lines.iter().find(|line| {
|
||||
let baseline = line.baseline as f32;
|
||||
middle >= baseline - line.ascent as f32 && middle <= baseline + line.descent as f32
|
||||
}) else {
|
||||
return room;
|
||||
};
|
||||
let line_left = line.left as f32;
|
||||
let line_right = line_left + line.width as f32;
|
||||
(
|
||||
if left <= line_left + 0.5 { 0.0 } else { room.0 },
|
||||
if left + width >= line_right - 0.5 {
|
||||
0.0
|
||||
} else {
|
||||
room.1
|
||||
},
|
||||
)
|
||||
}
|
||||
2943
render-wasm/src/shapes/text_horizontal/tests.rs
Normal file
2943
render-wasm/src/shapes/text_horizontal/tests.rs
Normal file
File diff suppressed because it is too large
Load Diff
189
render-wasm/src/shapes/text_horizontal/warichu.rs
Normal file
189
render-wasm/src/shapes/text_horizontal/warichu.rs
Normal file
@ -0,0 +1,189 @@
|
||||
use super::*;
|
||||
use crate::shapes::text_paragraph::LayoutParagraph;
|
||||
|
||||
pub(super) const HORIZONTAL_WARICHU_BUILDER_LEN: usize = 3;
|
||||
pub(super) const HORIZONTAL_WARICHU_STYLE_ANCHOR: char = '\u{00A0}';
|
||||
pub(super) const HORIZONTAL_WARICHU_BREAK_ANCHOR: char = '\u{200B}';
|
||||
|
||||
/// Placeholder rect of each horizontal warichu span, keyed by span index.
|
||||
/// Tabs in normal spans are placeholders too, so they are skipped.
|
||||
pub(crate) fn horizontal_warichu_placeholders(
|
||||
paragraph: &Paragraph,
|
||||
laid_out: &LayoutParagraph,
|
||||
) -> Vec<(usize, skia::Rect)> {
|
||||
let mut placeholders = laid_out.get_rects_for_placeholders().into_iter();
|
||||
let mut result = Vec::new();
|
||||
for (index, span) in paragraph.children().iter().enumerate() {
|
||||
if span.is_warichu() {
|
||||
if let Some(textbox) = placeholders.next() {
|
||||
result.push((index, textbox.rect));
|
||||
}
|
||||
} else {
|
||||
for _ in span.text.matches('\t') {
|
||||
placeholders.next();
|
||||
}
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn horizontal_warichu_boxes<'a>(
|
||||
paragraph: &'a Paragraph,
|
||||
laid_out: &LayoutParagraph,
|
||||
) -> Vec<(&'a TextSpan, usize, usize, skia::Rect)> {
|
||||
let mut source_start = 0usize;
|
||||
let mut placeholders = horizontal_warichu_placeholders(paragraph, laid_out)
|
||||
.into_iter()
|
||||
.peekable();
|
||||
let mut boxes = Vec::new();
|
||||
for (index, span) in paragraph.children().iter().enumerate() {
|
||||
let length = span.text.chars().count();
|
||||
if let Some((_, rect)) = placeholders.next_if(|(span_index, _)| *span_index == index) {
|
||||
boxes.push((span, source_start, source_start + length, rect));
|
||||
}
|
||||
source_start += length;
|
||||
}
|
||||
boxes
|
||||
}
|
||||
|
||||
pub(crate) fn horizontal_warichu_hit_test(
|
||||
paragraph: &Paragraph,
|
||||
laid_out: &LayoutParagraph,
|
||||
point: Point,
|
||||
) -> Option<usize> {
|
||||
for (span, source_start, _, rect) in horizontal_warichu_boxes(paragraph, laid_out) {
|
||||
if !rect.contains(&point) {
|
||||
continue;
|
||||
}
|
||||
let split = warichu_split_chars(&span.apply_text_transform());
|
||||
let total = span.text.chars().count();
|
||||
let second = point.y >= rect.top() + rect.height() / 2.0;
|
||||
let (line_start, line_len) = if second {
|
||||
(split, total - split)
|
||||
} else {
|
||||
(0, split)
|
||||
};
|
||||
let fraction = ((point.x - rect.left()) / rect.width().max(0.01)).clamp(0.0, 1.0);
|
||||
let within = (fraction * line_len as f32).round() as usize;
|
||||
return Some(source_start + line_start + within.min(line_len));
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub(crate) fn horizontal_warichu_caret_rect(
|
||||
paragraph: &Paragraph,
|
||||
laid_out: &LayoutParagraph,
|
||||
source_offset: usize,
|
||||
) -> Option<skia::Rect> {
|
||||
for (span, source_start, source_end, rect) in horizontal_warichu_boxes(paragraph, laid_out) {
|
||||
if source_offset < source_start || source_offset > source_end {
|
||||
continue;
|
||||
}
|
||||
let split = warichu_split_chars(&span.apply_text_transform());
|
||||
let local = source_offset - source_start;
|
||||
let total = source_end - source_start;
|
||||
let (line_start, line_len, top) = if local >= split {
|
||||
(split, total - split, rect.top() + rect.height() / 2.0)
|
||||
} else {
|
||||
(0, split, rect.top())
|
||||
};
|
||||
let within = local.saturating_sub(line_start).min(line_len);
|
||||
let char_width = rect.width() / line_len.max(1) as f32;
|
||||
return Some(skia::Rect::from_xywh(
|
||||
rect.left() + within as f32 * char_width,
|
||||
top,
|
||||
char_width,
|
||||
rect.height() / 2.0,
|
||||
));
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub(crate) fn horizontal_warichu_range_rects(
|
||||
paragraph: &Paragraph,
|
||||
laid_out: &LayoutParagraph,
|
||||
source_start: usize,
|
||||
source_end: usize,
|
||||
) -> Vec<skia::Rect> {
|
||||
let mut rects = Vec::new();
|
||||
for (span, span_start, span_end, rect) in horizontal_warichu_boxes(paragraph, laid_out) {
|
||||
let selected_start = source_start.max(span_start);
|
||||
let selected_end = source_end.min(span_end);
|
||||
if selected_start >= selected_end {
|
||||
continue;
|
||||
}
|
||||
let split = warichu_split_chars(&span.apply_text_transform());
|
||||
for (line_start, line_end, top) in [
|
||||
(span_start, span_start + split, rect.top()),
|
||||
(
|
||||
span_start + split,
|
||||
span_end,
|
||||
rect.top() + rect.height() / 2.0,
|
||||
),
|
||||
] {
|
||||
let start = selected_start.max(line_start);
|
||||
let end = selected_end.min(line_end);
|
||||
if start >= end {
|
||||
continue;
|
||||
}
|
||||
let line_len = line_end - line_start;
|
||||
let char_width = rect.width() / line_len.max(1) as f32;
|
||||
rects.push(skia::Rect::from_xywh(
|
||||
rect.left() + (start - line_start) as f32 * char_width,
|
||||
top,
|
||||
(end - start) as f32 * char_width,
|
||||
rect.height() / 2.0,
|
||||
));
|
||||
}
|
||||
}
|
||||
rects
|
||||
}
|
||||
|
||||
/// The two warichu sub-lines of `text`, split at `warichu_split_chars`.
|
||||
pub(crate) fn warichu_text_lines(text: &str) -> (&str, &str) {
|
||||
let split = warichu_split_chars(text);
|
||||
let split_byte = text
|
||||
.char_indices()
|
||||
.nth(split)
|
||||
.map(|(index, _)| index)
|
||||
.unwrap_or(text.len());
|
||||
text.split_at(split_byte)
|
||||
}
|
||||
|
||||
/// Paint the two horizontal warichu sub-lines into SkParagraph's inline
|
||||
/// placeholder boxes. Reading order is top line then bottom line.
|
||||
pub(crate) fn paint_horizontal_warichu(
|
||||
canvas: &skia::Canvas,
|
||||
paragraph: &Paragraph,
|
||||
plan: &HorizontalParagraphPlan,
|
||||
laid_out: &LayoutParagraph,
|
||||
x: f32,
|
||||
y: f32,
|
||||
) {
|
||||
let ranges = &plan.offsets.ranges;
|
||||
let placeholders = horizontal_warichu_placeholders(paragraph, laid_out);
|
||||
let warichu_ranges: Vec<_> = ranges.iter().filter(|range| range.warichu).collect();
|
||||
if placeholders.len() != warichu_ranges.len() {
|
||||
return;
|
||||
}
|
||||
|
||||
for (range, (_, rect)) in warichu_ranges.into_iter().zip(placeholders) {
|
||||
let Some(span) = paragraph.children().get(range.span) else {
|
||||
continue;
|
||||
};
|
||||
let Some(mut style) = horizontal_span_style(laid_out, range) else {
|
||||
continue;
|
||||
};
|
||||
style.set_font_size(span.font_size * WARICHU_FONT_SCALE);
|
||||
style.set_height(1.0);
|
||||
style.set_height_override(true);
|
||||
style.set_letter_spacing(span.letter_spacing * WARICHU_FONT_SCALE);
|
||||
let transformed = span.apply_text_transform();
|
||||
let (first, second) = warichu_text_lines(&transformed);
|
||||
let first_para = mini_paragraph(first, &style, get_font_collection());
|
||||
let second_para = mini_paragraph(second, &style, get_font_collection());
|
||||
let half_height = rect.height() / 2.0;
|
||||
first_para.paint(canvas, (x + rect.left(), y + rect.top()));
|
||||
second_para.paint(canvas, (x + rect.left(), y + rect.top() + half_height));
|
||||
}
|
||||
}
|
||||
447
render-wasm/src/shapes/text_paragraph.rs
Normal file
447
render-wasm/src/shapes/text_paragraph.rs
Normal file
@ -0,0 +1,447 @@
|
||||
//! SkParagraph rounds every line height and baseline, even with its rounding
|
||||
//! hack disabled. Keep its shaping and horizontal layout, but use the original
|
||||
//! ascent/descent for vertical geometry and painting.
|
||||
//!
|
||||
//! SkParagraph also moves each line by half the letter-spacing of its first
|
||||
//! character. That centers span tracking, but letter-spacing added to single
|
||||
//! characters (justification, distribution, squeezing, ruby room) must only
|
||||
//! open the gap after them, so each line takes back half of that extra.
|
||||
|
||||
use skia_safe::{
|
||||
textlayout::{
|
||||
FontCollection, LineMetrics, Paragraph, ParagraphBuilder, ParagraphStyle,
|
||||
PositionWithAffinity, RectHeightStyle, RectWidthStyle, TextBox,
|
||||
},
|
||||
Canvas, Paint, Point, TextBlob, TextBlobBuilder,
|
||||
};
|
||||
use std::ops::{Deref, DerefMut, Range};
|
||||
|
||||
/// A builder `LayoutParagraph` can lay out, with the letter-spacing its
|
||||
/// characters take beyond their span tracking.
|
||||
pub trait SpacedParagraphBuilder {
|
||||
fn paragraph_builder(&mut self) -> &mut ParagraphBuilder;
|
||||
|
||||
/// Records `amount` of extra letter-spacing on the character at the
|
||||
/// builder's UTF-16 offset `at`.
|
||||
fn add_extra_spacing(&mut self, _at: usize, _amount: f32) {}
|
||||
|
||||
/// Extra letter-spacing as (UTF-16 offset, amount), sorted by offset.
|
||||
fn extra_spacing(&self) -> &[(usize, f32)] {
|
||||
&[]
|
||||
}
|
||||
}
|
||||
|
||||
impl SpacedParagraphBuilder for ParagraphBuilder {
|
||||
fn paragraph_builder(&mut self) -> &mut ParagraphBuilder {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Paragraph builder of horizontal text, which records the extra
|
||||
/// letter-spacing of its characters.
|
||||
pub struct HorizontalBuilder {
|
||||
builder: ParagraphBuilder,
|
||||
extra_spacing: Vec<(usize, f32)>,
|
||||
}
|
||||
|
||||
impl HorizontalBuilder {
|
||||
pub fn new(style: &ParagraphStyle, fonts: impl Into<FontCollection>) -> Self {
|
||||
Self {
|
||||
builder: ParagraphBuilder::new(style, fonts),
|
||||
extra_spacing: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SpacedParagraphBuilder for HorizontalBuilder {
|
||||
fn paragraph_builder(&mut self) -> &mut ParagraphBuilder {
|
||||
&mut self.builder
|
||||
}
|
||||
|
||||
fn add_extra_spacing(&mut self, at: usize, amount: f32) {
|
||||
self.extra_spacing.push((at, amount));
|
||||
}
|
||||
|
||||
fn extra_spacing(&self) -> &[(usize, f32)] {
|
||||
&self.extra_spacing
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for HorizontalBuilder {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("HorizontalBuilder")
|
||||
.field("extra_spacing", &self.extra_spacing)
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for HorizontalBuilder {
|
||||
type Target = ParagraphBuilder;
|
||||
fn deref(&self) -> &ParagraphBuilder {
|
||||
&self.builder
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for HorizontalBuilder {
|
||||
fn deref_mut(&mut self) -> &mut ParagraphBuilder {
|
||||
&mut self.builder
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct LayoutParagraph {
|
||||
inner: Paragraph,
|
||||
text: String,
|
||||
extra_spacing: Vec<(usize, f32)>,
|
||||
shifts: Vec<f32>,
|
||||
x_shifts: Vec<f32>,
|
||||
height: f32,
|
||||
runs: Vec<(TextBlob, Paint, Point)>,
|
||||
}
|
||||
|
||||
impl LayoutParagraph {
|
||||
pub fn build(builder: &mut impl SpacedParagraphBuilder) -> Self {
|
||||
let extra_spacing = builder.extra_spacing().to_vec();
|
||||
let builder = builder.paragraph_builder();
|
||||
let text = builder.get_text().to_owned();
|
||||
Self {
|
||||
inner: builder.build(),
|
||||
text,
|
||||
extra_spacing,
|
||||
shifts: Vec::new(),
|
||||
x_shifts: Vec::new(),
|
||||
height: 0.0,
|
||||
runs: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn layout(&mut self, width: f32) {
|
||||
self.inner.layout(width);
|
||||
let lines = self.inner.get_line_metrics();
|
||||
self.shifts.clear();
|
||||
self.x_shifts.clear();
|
||||
self.height = 0.0;
|
||||
// Keep the first baseline anchored as before. Only remove drift
|
||||
// between lines; this also preserves existing one-line layouts.
|
||||
let anchor = lines
|
||||
.first()
|
||||
.map_or(0.0, |line| (line.baseline - line.ascent) as f32);
|
||||
for line in &lines {
|
||||
self.shifts
|
||||
.push(self.height + line.ascent as f32 + anchor - line.baseline as f32);
|
||||
self.height += (line.ascent + line.descent) as f32;
|
||||
let first = self
|
||||
.extra_spacing
|
||||
.binary_search_by_key(&line.start_index, |(at, _)| *at)
|
||||
.map_or(0.0, |found| self.extra_spacing[found].1);
|
||||
self.x_shifts.push(-first / 2.0);
|
||||
}
|
||||
if lines.is_empty() {
|
||||
self.height = self.inner.height();
|
||||
}
|
||||
self.runs.clear();
|
||||
if self
|
||||
.shifts
|
||||
.iter()
|
||||
.chain(&self.x_shifts)
|
||||
.all(|shift| shift.abs() < 0.00001)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// Copy the paints before visiting: Skia exposes style metrics as
|
||||
// references into the paragraph that visit mutably borrows.
|
||||
let paints: Vec<Vec<(usize, Paint)>> = lines
|
||||
.iter()
|
||||
.map(|line| {
|
||||
line.get_style_metrics(0..self.text.len().saturating_add(1))
|
||||
.into_iter()
|
||||
.map(|(at, metric)| (at, metric.text_style.foreground()))
|
||||
.collect()
|
||||
})
|
||||
.collect();
|
||||
drop(lines);
|
||||
// Retain UTF-8 clusters in the blobs so PDF text stays selectable.
|
||||
let mut boundaries = vec![self.text.len()];
|
||||
self.inner.visit(|_, info| {
|
||||
if let Some(info) = info {
|
||||
boundaries.extend(info.utf8_starts().iter().map(|at| *at as usize));
|
||||
}
|
||||
});
|
||||
boundaries.sort_unstable();
|
||||
boundaries.dedup();
|
||||
let text = &self.text;
|
||||
let shifts = &self.shifts;
|
||||
let x_shifts = &self.x_shifts;
|
||||
let runs = &mut self.runs;
|
||||
self.inner.visit(|line, info| {
|
||||
let Some(info) = info.filter(|info| info.count() > 0) else {
|
||||
return;
|
||||
};
|
||||
let starts = info.utf8_starts();
|
||||
let start = *starts.iter().min().unwrap() as usize;
|
||||
let last = *starts.iter().max().unwrap() as usize;
|
||||
let end = boundaries
|
||||
.get(boundaries.partition_point(|at| *at <= last))
|
||||
.copied()
|
||||
.unwrap_or(text.len());
|
||||
let paint = paints
|
||||
.get(line)
|
||||
.and_then(|paints| paints.iter().rev().find(|(at, _)| *at <= start))
|
||||
.map(|(_, paint)| paint.clone())
|
||||
.unwrap_or_default();
|
||||
let mut builder = TextBlobBuilder::new();
|
||||
if let Some(source) = text
|
||||
.as_bytes()
|
||||
.get(start..end)
|
||||
.filter(|source| !source.is_empty())
|
||||
{
|
||||
let (glyphs, points, utf8, clusters) =
|
||||
builder.alloc_run_text_pos(info.font(), info.count(), source.len(), None);
|
||||
glyphs.copy_from_slice(info.glyphs());
|
||||
points.copy_from_slice(info.positions());
|
||||
utf8.copy_from_slice(source);
|
||||
for (cluster, at) in clusters.iter_mut().zip(starts) {
|
||||
*cluster = *at - start as u32;
|
||||
}
|
||||
} else {
|
||||
let (glyphs, points) = builder.alloc_run_pos(info.font(), info.count(), None);
|
||||
glyphs.copy_from_slice(info.glyphs());
|
||||
points.copy_from_slice(info.positions());
|
||||
}
|
||||
if let Some(blob) = builder.make() {
|
||||
let mut origin = info.origin();
|
||||
origin.x += x_shifts.get(line).copied().unwrap_or(0.0);
|
||||
origin.y += shifts.get(line).copied().unwrap_or(0.0);
|
||||
runs.push((blob, paint, origin));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn line_shift(&self, line: usize) -> f32 {
|
||||
self.shifts.get(line).copied().unwrap_or(0.0)
|
||||
}
|
||||
|
||||
pub fn line_x_shift(&self, line: usize) -> f32 {
|
||||
self.x_shifts.get(line).copied().unwrap_or(0.0)
|
||||
}
|
||||
|
||||
pub fn height(&self) -> f32 {
|
||||
self.height
|
||||
}
|
||||
|
||||
pub fn ideographic_baseline(&self) -> f32 {
|
||||
self.inner.ideographic_baseline() + self.line_shift(0)
|
||||
}
|
||||
|
||||
/// Font that shaped the text at UTF-16 `offset`, fallbacks included.
|
||||
pub fn font_at_utf16(&self, offset: usize) -> skia_safe::Font {
|
||||
let mut utf16 = 0;
|
||||
let byte = self
|
||||
.text
|
||||
.char_indices()
|
||||
.find(|(_, ch)| {
|
||||
let start = utf16;
|
||||
utf16 += ch.len_utf16();
|
||||
start >= offset
|
||||
})
|
||||
.map_or(self.text.len(), |(byte, _)| byte);
|
||||
self.inner.get_font_at(byte)
|
||||
}
|
||||
|
||||
pub fn get_line_metrics(&self) -> Vec<LineMetrics<'_>> {
|
||||
let mut lines = self.inner.get_line_metrics();
|
||||
for (index, line) in lines.iter_mut().enumerate() {
|
||||
line.left += self.line_x_shift(index) as f64;
|
||||
line.baseline += self.line_shift(index) as f64;
|
||||
line.height = line.ascent + line.descent;
|
||||
}
|
||||
lines
|
||||
}
|
||||
|
||||
pub fn get_rects_for_range(
|
||||
&self,
|
||||
range: Range<usize>,
|
||||
height: RectHeightStyle,
|
||||
width: RectWidthStyle,
|
||||
) -> Vec<TextBox> {
|
||||
let mut result = Vec::new();
|
||||
for (index, line) in self.inner.get_line_metrics().iter().enumerate() {
|
||||
let start = range.start.max(line.start_index);
|
||||
let end = range.end.min(line.end_including_newline);
|
||||
if start >= end {
|
||||
continue;
|
||||
}
|
||||
let mut boxes = self.inner.get_rects_for_range(start..end, height, width);
|
||||
for textbox in &mut boxes {
|
||||
textbox
|
||||
.rect
|
||||
.offset((self.line_x_shift(index), self.line_shift(index)));
|
||||
}
|
||||
result.extend(boxes);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
pub fn get_rects_for_placeholders(&self) -> Vec<TextBox> {
|
||||
let lines = self.inner.get_line_metrics();
|
||||
let mut boxes = self.inner.get_rects_for_placeholders();
|
||||
for textbox in &mut boxes {
|
||||
let center = textbox.rect.center_y();
|
||||
let index = lines
|
||||
.iter()
|
||||
.position(|line| center < (line.baseline + line.descent) as f32)
|
||||
.unwrap_or(lines.len().saturating_sub(1));
|
||||
textbox
|
||||
.rect
|
||||
.offset((self.line_x_shift(index), self.line_shift(index)));
|
||||
}
|
||||
boxes
|
||||
}
|
||||
|
||||
pub fn get_glyph_position_at_coordinate(
|
||||
&self,
|
||||
point: impl Into<Point>,
|
||||
) -> PositionWithAffinity {
|
||||
let mut point = point.into();
|
||||
let lines = self.get_line_metrics();
|
||||
let index = lines
|
||||
.iter()
|
||||
.position(|line| point.y < (line.baseline + line.descent) as f32)
|
||||
.unwrap_or(lines.len().saturating_sub(1));
|
||||
point.x -= self.line_x_shift(index);
|
||||
point.y -= self.line_shift(index);
|
||||
self.inner.get_glyph_position_at_coordinate(point)
|
||||
}
|
||||
|
||||
pub fn paint(&self, canvas: &Canvas, point: impl Into<Point>) {
|
||||
let point = point.into();
|
||||
if self.runs.is_empty() {
|
||||
self.inner.paint(canvas, point);
|
||||
} else {
|
||||
for (blob, paint, origin) in &self.runs {
|
||||
canvas.draw_text_blob(blob, point + *origin, paint);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for LayoutParagraph {
|
||||
type Target = Paragraph;
|
||||
fn deref(&self) -> &Paragraph {
|
||||
&self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for LayoutParagraph {
|
||||
fn deref_mut(&mut self) -> &mut Paragraph {
|
||||
&mut self.inner
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::shapes::text_vertical::test_support::provider;
|
||||
use skia_safe::textlayout::{FontCollection, ParagraphStyle, TextStyle};
|
||||
|
||||
fn builder() -> ParagraphBuilder {
|
||||
crate::globals::design_init();
|
||||
let mut fonts = FontCollection::new();
|
||||
fonts.set_asset_font_manager(Some(
|
||||
provider(include_bytes!("../fonts/cjk-locl-test.ttf")).into(),
|
||||
));
|
||||
ParagraphBuilder::new(&ParagraphStyle::default(), fonts)
|
||||
}
|
||||
|
||||
fn style(color: skia_safe::Color, stroke: bool) -> TextStyle {
|
||||
let mut paint = Paint::default();
|
||||
paint.set_color(color);
|
||||
if stroke {
|
||||
paint.set_style(skia_safe::PaintStyle::Stroke);
|
||||
paint.set_stroke_width(0.5);
|
||||
}
|
||||
let mut style = TextStyle::default();
|
||||
let family = format!(
|
||||
"{}",
|
||||
crate::shapes::FontFamily::new(
|
||||
crate::Uuid::nil(),
|
||||
400,
|
||||
crate::shapes::FontStyle::Normal
|
||||
)
|
||||
);
|
||||
style.set_font_families(&[family]);
|
||||
style.set_font_size(18.0);
|
||||
style.set_height(1.6);
|
||||
style.set_height_override(true);
|
||||
style.set_half_leading(true);
|
||||
style.set_foreground_paint(&paint);
|
||||
style
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fractional_wrapped_paint_matches_individual_lines_with_mixed_fills_and_strokes() {
|
||||
for stroke in [false, true] {
|
||||
let colors = [
|
||||
skia_safe::Color::RED,
|
||||
skia_safe::Color::GREEN,
|
||||
skia_safe::Color::BLUE,
|
||||
];
|
||||
let mut builder = builder();
|
||||
for (index, color) in colors.into_iter().enumerate() {
|
||||
builder.push_style(&style(color, stroke));
|
||||
builder.add_text(if index < 2 { "한글\n" } else { "한글" });
|
||||
builder.pop();
|
||||
}
|
||||
let mut paragraph = LayoutParagraph::build(&mut builder);
|
||||
paragraph.layout(100.0);
|
||||
let mut actual = skia_safe::surfaces::raster_n32_premul((100, 100)).unwrap();
|
||||
paragraph.paint(actual.canvas(), (0.0, 0.0));
|
||||
let mut expected = skia_safe::surfaces::raster_n32_premul((100, 100)).unwrap();
|
||||
for (index, color) in colors.into_iter().enumerate() {
|
||||
let mut line_builder = self::builder();
|
||||
line_builder.push_style(&style(color, stroke));
|
||||
line_builder.add_text("한글");
|
||||
let mut line = line_builder.build();
|
||||
line.layout(100.0);
|
||||
line.paint(expected.canvas(), (0.0, index as f32 * 28.8));
|
||||
}
|
||||
let pixels = actual.peek_pixels().unwrap();
|
||||
assert!(
|
||||
pixels.bytes().unwrap().iter().any(|byte| *byte > 0),
|
||||
"the font paints visible ink"
|
||||
);
|
||||
assert_eq!(pixels.bytes(), expected.peek_pixels().unwrap().bytes());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fractional_geometry_hit_testing_and_relayout_share_the_same_lines() {
|
||||
let mut builder = builder();
|
||||
builder.push_style(&style(skia_safe::Color::BLACK, false));
|
||||
builder.add_text("한글\n한글\n한글");
|
||||
let mut paragraph = LayoutParagraph::build(&mut builder);
|
||||
for width in [100.0, 50.0, 100.0] {
|
||||
paragraph.layout(width);
|
||||
assert!((paragraph.height() - 86.4).abs() < 0.001);
|
||||
let boxes: Vec<_> = [0, 3, 6]
|
||||
.into_iter()
|
||||
.map(|at| {
|
||||
paragraph.get_rects_for_range(
|
||||
at..at + 1,
|
||||
RectHeightStyle::Tight,
|
||||
RectWidthStyle::Tight,
|
||||
)[0]
|
||||
.rect
|
||||
})
|
||||
.collect();
|
||||
for pair in boxes.windows(2) {
|
||||
assert!((pair[1].top - pair[0].top - 28.8).abs() < 0.001);
|
||||
}
|
||||
for (index, rect) in boxes.iter().enumerate() {
|
||||
let hit =
|
||||
paragraph.get_glyph_position_at_coordinate((rect.left + 0.01, rect.center_y()));
|
||||
assert_eq!(hit.position as usize, index * 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,11 +1,8 @@
|
||||
use crate::render::text::{decoration_segments, list_markers};
|
||||
use crate::shapes::text::{vertical_align_offset, Paragraph, TextContent};
|
||||
use crate::shapes::text_paragraph::LayoutParagraph as SkiaParagraph;
|
||||
use crate::shapes::VerticalAlign;
|
||||
use skia_safe::{
|
||||
self as skia,
|
||||
textlayout::{paragraph::VisitorInfo, Paragraph as SkiaParagraph},
|
||||
Point,
|
||||
};
|
||||
use skia_safe::{self as skia, textlayout::paragraph::VisitorInfo, Point};
|
||||
use std::ops::Deref;
|
||||
|
||||
pub struct TextPaths(TextContent);
|
||||
@ -16,14 +13,27 @@ impl TextPaths {
|
||||
}
|
||||
|
||||
pub fn get_paths(&self, vertical_align: VerticalAlign) -> Vec<skia::Path> {
|
||||
if self.0.is_vertical() {
|
||||
return crate::shapes::text_vertical::vertical_text_paths(
|
||||
&self.0,
|
||||
vertical_align,
|
||||
true,
|
||||
)
|
||||
.into_iter()
|
||||
.map(|(path, _)| path)
|
||||
.collect();
|
||||
}
|
||||
|
||||
let layout_width = self.0.get_width(self.bounds.width());
|
||||
let plans = self.0.horizontal_plans();
|
||||
let mut paragraph_builders = self.0.paragraph_builder_group_from_text(None);
|
||||
let mut paragraphs: Vec<SkiaParagraph> = paragraph_builders
|
||||
.iter_mut()
|
||||
.filter_map(|group| group.first_mut())
|
||||
.map(|paragraph_builder| {
|
||||
let mut paragraph = paragraph_builder.build();
|
||||
paragraph.layout(layout_width);
|
||||
.enumerate()
|
||||
.map(|(index, paragraph_builder)| {
|
||||
let mut paragraph = SkiaParagraph::build(paragraph_builder);
|
||||
paragraph.layout(plans[index].layout_width(layout_width));
|
||||
paragraph
|
||||
})
|
||||
.collect();
|
||||
@ -48,9 +58,21 @@ impl TextPaths {
|
||||
.collect();
|
||||
|
||||
let mut offset_y = top;
|
||||
for (paragraph, text_paragraph) in paragraphs.iter_mut().zip(self.0.paragraphs()) {
|
||||
let origin = Point::new(self.bounds.x(), offset_y);
|
||||
Self::collect_paragraph_paths(paragraph, text_paragraph, origin, &mut paths);
|
||||
for (index, (paragraph, text_paragraph)) in
|
||||
paragraphs.iter_mut().zip(self.0.paragraphs()).enumerate()
|
||||
{
|
||||
let plan = &plans[index];
|
||||
let origin = Point::new(
|
||||
self.bounds.x() + plan.x_offset(text_paragraph, layout_width),
|
||||
offset_y,
|
||||
);
|
||||
Self::collect_paragraph_paths(
|
||||
paragraph,
|
||||
text_paragraph,
|
||||
&plan.offsets,
|
||||
origin,
|
||||
&mut paths,
|
||||
);
|
||||
offset_y += paragraph.height();
|
||||
}
|
||||
|
||||
@ -60,22 +82,26 @@ impl TextPaths {
|
||||
fn collect_paragraph_paths(
|
||||
paragraph: &mut SkiaParagraph,
|
||||
text_paragraph: &Paragraph,
|
||||
offsets: &crate::shapes::HorizontalOffsets,
|
||||
origin: Point,
|
||||
paths: &mut Vec<skia::Path>,
|
||||
) {
|
||||
for deco in decoration_segments(paragraph, text_paragraph, origin.x, origin.y) {
|
||||
for deco in decoration_segments(paragraph, text_paragraph, offsets, origin.x, origin.y) {
|
||||
let mut builder = skia::PathBuilder::new();
|
||||
builder.add_rect(deco.rect(), None, None);
|
||||
paths.push(builder.detach());
|
||||
}
|
||||
|
||||
paragraph.visit(|_: usize, info: Option<&VisitorInfo>| {
|
||||
let shifts: Vec<_> = (0..paragraph.get_line_metrics().len())
|
||||
.map(|line| Point::new(paragraph.line_x_shift(line), paragraph.line_shift(line)))
|
||||
.collect();
|
||||
paragraph.visit(|line: usize, info: Option<&VisitorInfo>| {
|
||||
let Some(info) = info else {
|
||||
return;
|
||||
};
|
||||
|
||||
let font = info.font();
|
||||
let run_origin = origin + info.origin();
|
||||
let run_origin = origin + info.origin() + shifts[line];
|
||||
let mut builder = skia::PathBuilder::new();
|
||||
let mut has_glyphs = false;
|
||||
|
||||
|
||||
531
render-wasm/src/shapes/text_policy.rs
Normal file
531
render-wasm/src/shapes/text_policy.rs
Normal file
@ -0,0 +1,531 @@
|
||||
//! Paragraph typography policies. Geometry and source offsets are shared.
|
||||
//! Regional presets use CLREQ (2026-10-07); Korean restrictions use KLREQ
|
||||
//! (2026-03-21). These are named presets, not universal regional mandates.
|
||||
|
||||
use super::cjk_chars::{
|
||||
is_han, COMMAS_AND_FULL_STOPS, FULLWIDTH_CLOSING_BRACKETS, FULLWIDTH_OPENING_BRACKETS,
|
||||
};
|
||||
use super::jlreq::{
|
||||
break_allowed_between, forbidden_at_line_end, forbidden_at_line_start,
|
||||
keeps_together_classified, pair_rule, JlreqClass,
|
||||
};
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
|
||||
#[repr(u8)]
|
||||
pub enum KoreanWrap {
|
||||
#[default]
|
||||
Syllable = 0,
|
||||
/// Preserve the original experiment when old content is reopened.
|
||||
SpacesOnly = 1,
|
||||
KeepWords = 2,
|
||||
}
|
||||
|
||||
/// Regional Chinese preset, chosen by the region subtag only.
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ChineseRegion {
|
||||
/// Original basic preset, retained for tags without a known region.
|
||||
#[default]
|
||||
Basic,
|
||||
/// CN and SG.
|
||||
Mainland,
|
||||
/// TW.
|
||||
Taiwan,
|
||||
/// HK and MO.
|
||||
HongKong,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum TextLayoutPolicy {
|
||||
#[default]
|
||||
Plain,
|
||||
Japanese,
|
||||
Chinese(ChineseRegion),
|
||||
Korean(KoreanWrap),
|
||||
}
|
||||
|
||||
/// Spacing between two adjacent characters of a pairwise policy, in em.
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq)]
|
||||
pub struct PairSpacing {
|
||||
/// Removable punctuation side bearings.
|
||||
pub compression: f32,
|
||||
/// Han/Latin gap to add.
|
||||
pub script_gap: f32,
|
||||
}
|
||||
|
||||
/// The parts of a BCP 47 language tag the layout and font choices read.
|
||||
/// Subtags may be separated by `-` or `_`; matching ignores case.
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct LanguageTag<'a> {
|
||||
pub primary: &'a str,
|
||||
/// Four-letter script subtag (`Hans`, `Hant`).
|
||||
pub script: Option<&'a str>,
|
||||
/// Two-letter or three-digit region subtag (`CN`, `TW`, `419`).
|
||||
pub region: Option<&'a str>,
|
||||
}
|
||||
|
||||
impl<'a> LanguageTag<'a> {
|
||||
pub fn parse(tag: &'a str) -> Self {
|
||||
let mut subtags = tag.split(['-', '_']);
|
||||
let primary = subtags.next().unwrap_or_default();
|
||||
let mut script = None;
|
||||
let mut region = None;
|
||||
// Script and region precede extensions and private use, which start
|
||||
// at a singleton subtag.
|
||||
for subtag in subtags.take_while(|subtag| subtag.len() != 1) {
|
||||
if script.is_none()
|
||||
&& subtag.len() == 4
|
||||
&& subtag.bytes().all(|b| b.is_ascii_alphabetic())
|
||||
{
|
||||
script = Some(subtag);
|
||||
} else if region.is_none()
|
||||
&& (subtag.len() == 2
|
||||
|| (subtag.len() == 3 && subtag.bytes().all(|b| b.is_ascii_digit())))
|
||||
{
|
||||
region = Some(subtag);
|
||||
}
|
||||
}
|
||||
Self {
|
||||
primary,
|
||||
script,
|
||||
region,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is(&self, primary: &str) -> bool {
|
||||
self.primary.eq_ignore_ascii_case(primary)
|
||||
}
|
||||
|
||||
pub fn has_script(&self, script: &str) -> bool {
|
||||
self.script.is_some_and(|s| s.eq_ignore_ascii_case(script))
|
||||
}
|
||||
|
||||
pub fn has_region(&self, regions: &[&str]) -> bool {
|
||||
self.region
|
||||
.is_some_and(|r| regions.iter().any(|region| r.eq_ignore_ascii_case(region)))
|
||||
}
|
||||
}
|
||||
|
||||
fn numeric(ch: char) -> bool {
|
||||
ch.is_numeric() || "一二三四五六七八九十百千萬万億亿〇零".contains(ch)
|
||||
}
|
||||
|
||||
fn opening(ch: char) -> bool {
|
||||
FULLWIDTH_OPENING_BRACKETS.contains(ch) || "‘“([{".contains(ch)
|
||||
}
|
||||
|
||||
fn closing(ch: char) -> bool {
|
||||
COMMAS_AND_FULL_STOPS.contains(ch)
|
||||
|| FULLWIDTH_CLOSING_BRACKETS.contains(ch)
|
||||
|| "!?:;’”)]},.!?:;".contains(ch)
|
||||
}
|
||||
|
||||
fn protected(ch: char) -> bool {
|
||||
matches!(
|
||||
ch,
|
||||
'\u{00a0}' | '\u{202f}' | '\u{2060}' | '\u{feff}' | '\u{2011}'
|
||||
)
|
||||
}
|
||||
|
||||
impl TextLayoutPolicy {
|
||||
/// The policy a paragraph language (a BCP 47 tag) selects. Script never
|
||||
/// implies region. `korean_wrap` applies only to Korean.
|
||||
pub fn for_language(language: &str, korean_wrap: KoreanWrap) -> Self {
|
||||
let tag = LanguageTag::parse(language);
|
||||
if tag.is("ja") {
|
||||
Self::Japanese
|
||||
} else if tag.is("zh") {
|
||||
Self::Chinese(if tag.has_region(&["CN", "SG"]) {
|
||||
ChineseRegion::Mainland
|
||||
} else if tag.has_region(&["TW"]) {
|
||||
ChineseRegion::Taiwan
|
||||
} else if tag.has_region(&["HK", "MO"]) {
|
||||
ChineseRegion::HongKong
|
||||
} else {
|
||||
ChineseRegion::Basic
|
||||
})
|
||||
} else if tag.is("ko") {
|
||||
Self::Korean(korean_wrap)
|
||||
} else {
|
||||
Self::Plain
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_chinese(self) -> bool {
|
||||
matches!(self, Self::Chinese(_))
|
||||
}
|
||||
|
||||
pub fn is_korean(self) -> bool {
|
||||
matches!(self, Self::Korean(_))
|
||||
}
|
||||
|
||||
/// Japanese layout: JLREQ classes, pair rules, kinsoku, aki and the
|
||||
/// squeeze/justify passes built on them.
|
||||
pub fn uses_jlreq(self) -> bool {
|
||||
self == Self::Japanese
|
||||
}
|
||||
|
||||
/// Horizontal layout texts always carry this policy's break rules.
|
||||
/// Japanese plans them only for text with ruby or CJK characters
|
||||
/// (`uses_japanese_layout`); plain text keeps Skia's breaks.
|
||||
pub fn plans_breaks(self) -> bool {
|
||||
matches!(self, Self::Chinese(_) | Self::Korean(_))
|
||||
}
|
||||
|
||||
/// An indivisible unit wider than the box overflows whole instead of
|
||||
/// taking Skia's emergency split. The legacy Korean spaces-only mode
|
||||
/// keeps its earlier behaviour.
|
||||
pub fn keeps_overlong_units(self) -> bool {
|
||||
self == Self::Japanese || self.reopens_overlong_words()
|
||||
}
|
||||
|
||||
/// An overlong word may wrap at safe grapheme boundaries the policy's
|
||||
/// emergency rules allow.
|
||||
pub fn reopens_overlong_words(self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::Chinese(_) | Self::Korean(KoreanWrap::Syllable | KoreanWrap::KeepWords)
|
||||
)
|
||||
}
|
||||
|
||||
/// Justification expands the policy's pairwise opportunities by bounded
|
||||
/// amounts (`expansion_em`) instead of leaving it to Skia.
|
||||
pub fn bounded_justify(self) -> bool {
|
||||
self.reopens_overlong_words()
|
||||
}
|
||||
|
||||
/// The legacy Korean spaces-only mode never justified vertical columns.
|
||||
pub fn justifies_columns(self) -> bool {
|
||||
self != Self::Korean(KoreanWrap::SpacesOnly)
|
||||
}
|
||||
|
||||
/// Whether a vertical column break measures the whole unit that would
|
||||
/// move to the next column, or (legacy spaces-only) only the part up to
|
||||
/// the overflowing cell.
|
||||
pub fn measures_whole_unit(self) -> bool {
|
||||
self != Self::Korean(KoreanWrap::SpacesOnly)
|
||||
}
|
||||
|
||||
/// Western word spaces set to one third em (JLREQ §3.2.6).
|
||||
pub fn normalizes_western_spaces(self) -> bool {
|
||||
self == Self::Japanese
|
||||
}
|
||||
|
||||
/// Automatic emphasis and underline positions follow CLREQ: emphasis
|
||||
/// below horizontal text, underlines below the ink.
|
||||
pub fn uses_clreq_positions(self) -> bool {
|
||||
self.is_chinese()
|
||||
}
|
||||
|
||||
/// Whether horizontal layout text needs a WORD JOINER between two
|
||||
/// adjacent grapheme bases, given with their JLREQ classes, to block a
|
||||
/// break Skia would otherwise take.
|
||||
pub fn needs_joiner(self, before: (char, JlreqClass), after: (char, JlreqClass)) -> bool {
|
||||
if self.uses_jlreq() {
|
||||
pair_rule(before.1, after.1).suppress_break_with_joiner
|
||||
|| keeps_together_classified(before, after)
|
||||
} else {
|
||||
!self.break_allowed(before.0, after.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Even emergency wrapping must retain punctuation, nonbreaking controls,
|
||||
/// numeric expressions and paired marks. Grapheme/annotation atomicity is
|
||||
/// enforced by the caller on the complete paragraph, across style runs.
|
||||
pub fn emergency_break_allowed(self, before: char, after: char) -> bool {
|
||||
if matches!(self, Self::Plain | Self::Korean(KoreanWrap::SpacesOnly)) {
|
||||
return true;
|
||||
}
|
||||
!protected(before)
|
||||
&& !protected(after)
|
||||
&& !opening(before)
|
||||
&& !closing(after)
|
||||
&& !(self.is_korean() && "‐‑–-・·々〻ー".contains(after))
|
||||
&& !(self.uses_jlreq()
|
||||
&& (forbidden_at_line_start(after) || forbidden_at_line_end(before)))
|
||||
&& !(before == after && matches!(after, '…' | '‥' | '—' | '―'))
|
||||
&& !("$$£€₩¥¥₽₺₫++--−".contains(before) && numeric(after))
|
||||
&& !(numeric(before) && "%%‰℃℉°".contains(after))
|
||||
&& !(numeric(before) && matches!(after, '.' | ',' | '.' | ','))
|
||||
&& !(matches!(before, '.' | ',' | '.' | ',') && numeric(after))
|
||||
}
|
||||
|
||||
/// Legal boundary between graphemes, before fitting a line or column.
|
||||
pub fn break_allowed(self, before: char, after: char) -> bool {
|
||||
match self {
|
||||
Self::Japanese => break_allowed_between(before, after),
|
||||
Self::Korean(KoreanWrap::SpacesOnly) => before.is_whitespace() || after.is_whitespace(),
|
||||
Self::Plain => true,
|
||||
Self::Chinese(_) | Self::Korean(_) => {
|
||||
self.emergency_break_allowed(before, after)
|
||||
&& !(before.is_ascii_alphanumeric() && after.is_ascii_alphanumeric())
|
||||
&& !(self == Self::Korean(KoreanWrap::KeepWords)
|
||||
&& before.is_alphanumeric()
|
||||
&& after.is_alphanumeric())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Removable leading/trailing side bearings of full-width punctuation.
|
||||
/// TW uses a solid preset. CN/HK compress only adjacent punctuation; fixed
|
||||
/// marks (notably vertical colon/semicolon/question/exclamation) keep 1em.
|
||||
pub fn punctuation_space(self, ch: char, vertical: bool) -> (f32, f32) {
|
||||
let Self::Chinese(region @ (ChineseRegion::Mainland | ChineseRegion::HongKong)) = self
|
||||
else {
|
||||
return (0.0, 0.0);
|
||||
};
|
||||
if FULLWIDTH_OPENING_BRACKETS.contains(ch) {
|
||||
return (0.5, 0.0);
|
||||
}
|
||||
if FULLWIDTH_CLOSING_BRACKETS.contains(ch) {
|
||||
return (0.0, 0.5);
|
||||
}
|
||||
let adjustable = COMMAS_AND_FULL_STOPS.contains(ch)
|
||||
|| (!vertical && region == ChineseRegion::Mainland && ":;!?".contains(ch))
|
||||
|| (!vertical && region == ChineseRegion::HongKong && ":;".contains(ch));
|
||||
if !adjustable {
|
||||
return (0.0, 0.0);
|
||||
}
|
||||
if region == ChineseRegion::Mainland {
|
||||
(0.0, 0.5)
|
||||
} else {
|
||||
(0.25, 0.25)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compression_em(self, before: char, after: char, vertical: bool) -> f32 {
|
||||
let a = self.punctuation_space(before, vertical);
|
||||
let b = self.punctuation_space(after, vertical);
|
||||
// Both characters must be adjustable punctuation. Never trim ordinary
|
||||
// Han cells or assume proportional ASCII punctuation contains half an em.
|
||||
if a == (0.0, 0.0) || b == (0.0, 0.0) {
|
||||
0.0
|
||||
} else {
|
||||
(a.1 + b.0).min(0.5)
|
||||
}
|
||||
}
|
||||
|
||||
/// Pairwise spacing between two adjacent characters, in em: punctuation
|
||||
/// compression to remove and the script gap to add. Each axis applies
|
||||
/// its own caps and skips.
|
||||
pub fn pair_spacing_em(self, before: char, after: char, vertical: bool) -> PairSpacing {
|
||||
PairSpacing {
|
||||
compression: self.compression_em(before, after, vertical),
|
||||
script_gap: self.script_spacing_em(before, after),
|
||||
}
|
||||
}
|
||||
|
||||
/// This preset permits hanging only in vertical traditional composition.
|
||||
/// Consecutive marks are excluded by the planner. Horizontal CN uses
|
||||
/// compression and push-out; no preset silently enables horizontal hanging.
|
||||
pub fn hanging_em(self, ch: char, vertical: bool) -> f32 {
|
||||
if vertical
|
||||
&& matches!(
|
||||
self,
|
||||
Self::Chinese(ChineseRegion::Taiwan | ChineseRegion::HongKong)
|
||||
)
|
||||
&& COMMAS_AND_FULL_STOPS.contains(ch)
|
||||
{
|
||||
0.5
|
||||
} else {
|
||||
0.0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn script_spacing_em(self, before: char, after: char) -> f32 {
|
||||
let regional = matches!(self, Self::Chinese(region) if region != ChineseRegion::Basic);
|
||||
if regional
|
||||
&& ((is_han(before) && after.is_ascii_alphanumeric())
|
||||
|| (before.is_ascii_alphanumeric() && is_han(after)))
|
||||
{
|
||||
0.25
|
||||
} else {
|
||||
0.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Bounded justification: spaces first for Korean, character gaps for
|
||||
/// Chinese and unspaced Korean syllable layout. Never stretch word mode.
|
||||
pub fn expansion_em(self, before: char, after: char) -> (u8, f32) {
|
||||
if !self.break_allowed(before, after) {
|
||||
return (0, 0.0);
|
||||
}
|
||||
if before == ' ' && !after.is_whitespace() && self.is_korean() {
|
||||
return (1, 0.5);
|
||||
}
|
||||
if self.is_chinese()
|
||||
&& !before.is_whitespace()
|
||||
&& !after.is_whitespace()
|
||||
&& (is_han(before) || is_han(after))
|
||||
{
|
||||
return (1, 0.5);
|
||||
}
|
||||
if self == Self::Korean(KoreanWrap::Syllable)
|
||||
&& before.is_alphanumeric()
|
||||
&& after.is_alphanumeric()
|
||||
{
|
||||
return (2, 0.125);
|
||||
}
|
||||
(0, 0.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn language_selects_one_policy_without_script_detection() {
|
||||
for (language, expected) in [
|
||||
("", TextLayoutPolicy::Plain),
|
||||
("en", TextLayoutPolicy::Plain),
|
||||
("ja-JP", TextLayoutPolicy::Japanese),
|
||||
(
|
||||
"ZH-Hant-TW",
|
||||
TextLayoutPolicy::Chinese(ChineseRegion::Taiwan),
|
||||
),
|
||||
("ko", TextLayoutPolicy::Korean(KoreanWrap::Syllable)),
|
||||
] {
|
||||
assert_eq!(
|
||||
TextLayoutPolicy::for_language(language, KoreanWrap::Syllable),
|
||||
expected
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
TextLayoutPolicy::for_language("ko-KR", KoreanWrap::SpacesOnly),
|
||||
TextLayoutPolicy::Korean(KoreanWrap::SpacesOnly)
|
||||
);
|
||||
assert_eq!(
|
||||
TextLayoutPolicy::for_language("zh", KoreanWrap::SpacesOnly),
|
||||
TextLayoutPolicy::Chinese(ChineseRegion::Basic)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chinese_regions_are_explicit_and_independent_of_script() {
|
||||
let tag = LanguageTag::parse("zh-Hant-TW-x-hk");
|
||||
assert!(tag.is("ZH"));
|
||||
assert!(tag.has_script("hant"));
|
||||
assert!(tag.has_region(&["tw"]));
|
||||
assert!(!tag.has_region(&["HK"]));
|
||||
assert_eq!(LanguageTag::parse("es-419").region, Some("419"));
|
||||
for (language, expected) in [
|
||||
("zh-Hant-HK", ChineseRegion::HongKong),
|
||||
("zh-MO", ChineseRegion::HongKong),
|
||||
("zh-SG", ChineseRegion::Mainland),
|
||||
] {
|
||||
assert_eq!(
|
||||
TextLayoutPolicy::for_language(language, KoreanWrap::Syllable),
|
||||
TextLayoutPolicy::Chinese(expected),
|
||||
"{language}"
|
||||
);
|
||||
}
|
||||
let mainland = TextLayoutPolicy::for_language("zh-Hans-CN", KoreanWrap::Syllable);
|
||||
let mainland_traditional =
|
||||
TextLayoutPolicy::for_language("zh-Hant-CN", KoreanWrap::Syllable);
|
||||
let taiwan = TextLayoutPolicy::for_language("zh-Hant-TW", KoreanWrap::Syllable);
|
||||
let hong_kong = TextLayoutPolicy::for_language("zh-Hant-HK", KoreanWrap::Syllable);
|
||||
assert_eq!(mainland, mainland_traditional);
|
||||
assert_ne!(mainland, taiwan);
|
||||
assert_ne!(taiwan, hong_kong);
|
||||
assert_eq!(
|
||||
mainland,
|
||||
TextLayoutPolicy::for_language("ZH-cn", KoreanWrap::Syllable)
|
||||
);
|
||||
assert_eq!(
|
||||
TextLayoutPolicy::Chinese(ChineseRegion::Basic),
|
||||
TextLayoutPolicy::for_language("zh-Hant", KoreanWrap::Syllable)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn korean_punctuation_and_numeric_units_stay_attached() {
|
||||
let policy = TextLayoutPolicy::Korean(KoreanWrap::Syllable);
|
||||
for (before, after) in [
|
||||
('한', '-'),
|
||||
('한', '·'),
|
||||
('₩', '1'),
|
||||
('1', '%'),
|
||||
('1', '.'),
|
||||
('.', '2'),
|
||||
(',', '2'),
|
||||
(',', '2'),
|
||||
('-', '2'),
|
||||
('-', '2'),
|
||||
('…', '…'),
|
||||
('―', '―'),
|
||||
('한', '\u{00a0}'),
|
||||
('\u{00a0}', '글'),
|
||||
('한', '\u{2060}'),
|
||||
] {
|
||||
assert!(!policy.break_allowed(before, after), "{before}|{after}");
|
||||
}
|
||||
assert!(policy.break_allowed('한', '글'));
|
||||
assert!(policy.break_allowed('한', '漢'));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn word_mode_retains_punctuation_opportunities_and_blocks_letter_pairs() {
|
||||
let policy = TextLayoutPolicy::for_language("ko-KR", KoreanWrap::KeepWords);
|
||||
for (before, after) in [
|
||||
('한', '글'),
|
||||
('漢', '字'),
|
||||
('한', 'A'),
|
||||
('A', '漢'),
|
||||
('1', '글'),
|
||||
] {
|
||||
assert!(!policy.break_allowed(before, after));
|
||||
assert!(policy.emergency_break_allowed(before, after));
|
||||
}
|
||||
for (before, after) in [
|
||||
('글', '('),
|
||||
(')', '한'),
|
||||
('-', '글'),
|
||||
('글', ' '),
|
||||
(' ', '한'),
|
||||
] {
|
||||
assert!(policy.break_allowed(before, after), "{before}|{after}");
|
||||
}
|
||||
for (before, after) in [('(', '한'), ('글', ')'), ('한', ','), ('글', '\u{00a0}')] {
|
||||
assert!(!policy.emergency_break_allowed(before, after));
|
||||
}
|
||||
assert!(!TextLayoutPolicy::Korean(KoreanWrap::SpacesOnly).break_allowed(')', '한'));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn regional_fitting_uses_its_own_spacing_and_orientation_rules() {
|
||||
let cn = TextLayoutPolicy::Chinese(ChineseRegion::Mainland);
|
||||
let tw = TextLayoutPolicy::Chinese(ChineseRegion::Taiwan);
|
||||
let hk = TextLayoutPolicy::Chinese(ChineseRegion::HongKong);
|
||||
assert_eq!(cn.compression_em(',', '」', false), 0.5);
|
||||
assert_eq!(tw.compression_em(',', '」', false), 0.0);
|
||||
assert_eq!(hk.compression_em(',', '」', false), 0.25);
|
||||
assert_eq!(cn.punctuation_space('!', true), (0.0, 0.0));
|
||||
assert_eq!(hk.punctuation_space('!', false), (0.0, 0.0));
|
||||
assert_eq!(tw.hanging_em(',', false), 0.0);
|
||||
assert_eq!(tw.hanging_em(',', true), 0.5);
|
||||
assert_eq!(cn.script_spacing_em('中', 'A'), 0.25);
|
||||
assert_eq!(cn.script_spacing_em(',', 'A'), 0.0);
|
||||
assert_eq!(
|
||||
TextLayoutPolicy::Korean(KoreanWrap::KeepWords).script_spacing_em('漢', 'A'),
|
||||
0.0
|
||||
);
|
||||
assert_eq!(
|
||||
TextLayoutPolicy::Korean(KoreanWrap::KeepWords).expansion_em('한', '글'),
|
||||
(0, 0.0)
|
||||
);
|
||||
assert_eq!(
|
||||
TextLayoutPolicy::Korean(KoreanWrap::KeepWords).expansion_em(' ', '한'),
|
||||
(1, 0.5)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chinese_basic_restrictions_do_not_include_small_kana() {
|
||||
let policy = TextLayoutPolicy::Chinese(ChineseRegion::Basic);
|
||||
assert!(!policy.break_allowed('「', '中'));
|
||||
assert!(!policy.break_allowed('文', '」'));
|
||||
assert!(policy.break_allowed('中', '文'));
|
||||
assert!(policy.break_allowed('あ', 'っ'));
|
||||
assert!(!TextLayoutPolicy::Japanese.break_allowed('あ', 'っ'));
|
||||
}
|
||||
}
|
||||
356
render-wasm/src/shapes/text_ruby.rs
Normal file
356
render-wasm/src/shapes/text_ruby.rs
Normal file
@ -0,0 +1,356 @@
|
||||
//! Ruby (furigana, Pinyin) piece, distribution and overhang rules shared by
|
||||
//! the horizontal and vertical engines (JLREQ §3.3).
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::hash::Hash;
|
||||
|
||||
use crate::shapes::jlreq::{classify, JlreqClass};
|
||||
use crate::shapes::{RubyAlign, RubyOverhang};
|
||||
|
||||
/// Room a long reading may overhang one neighbouring character (JLREQ
|
||||
/// §3.3.8, Fig 135): up to one ruby character, and half the neighbour, over
|
||||
/// kana or a comma, full stop or middle dot without ruby of their own.
|
||||
/// Kanji, brackets, another ruby base and a line edge (`None`) get no
|
||||
/// overhang, nor does any neighbour under `RubyOverhang::None`.
|
||||
pub(crate) fn ruby_overhang_room(
|
||||
policy: RubyOverhang,
|
||||
neighbour: Option<char>,
|
||||
neighbour_has_ruby: bool,
|
||||
neighbour_extent: f32,
|
||||
ruby_font_size: f32,
|
||||
) -> f32 {
|
||||
let overhangable = neighbour.is_some_and(|ch| {
|
||||
matches!(
|
||||
classify(ch),
|
||||
JlreqClass::Hiragana
|
||||
| JlreqClass::Katakana
|
||||
| JlreqClass::SmallKana
|
||||
| JlreqClass::ProlongedSoundMark
|
||||
| JlreqClass::Comma
|
||||
| JlreqClass::FullStop
|
||||
| JlreqClass::MiddleDot
|
||||
)
|
||||
});
|
||||
if policy == RubyOverhang::Auto && overhangable && !neighbour_has_ruby {
|
||||
ruby_font_size.min(neighbour_extent / 2.0).max(0.0)
|
||||
} else {
|
||||
0.0
|
||||
}
|
||||
}
|
||||
|
||||
/// How far a reading `overflow` longer than its base hangs over the
|
||||
/// neighbours before and after it, given their `room`: centred where the room
|
||||
/// allows, as `distribute_ruby_pieces` places it. Overflow past the room
|
||||
/// spreads the base instead.
|
||||
pub(crate) fn ruby_overhangs(overflow: f32, room: (f32, f32)) -> (f32, f32) {
|
||||
let (before, after) = room;
|
||||
if overflow <= 0.0 {
|
||||
return (0.0, 0.0);
|
||||
}
|
||||
if overflow >= before + after {
|
||||
return (before, after);
|
||||
}
|
||||
let hang_before = (overflow / 2.0)
|
||||
.min(before)
|
||||
.max((overflow - after).min(before))
|
||||
.max(0.0);
|
||||
(hang_before, (overflow - hang_before).clamp(0.0, after))
|
||||
}
|
||||
|
||||
/// Overhang that earlier readings already take on the character after them.
|
||||
/// A kana between two readings lends the later one only what the earlier one
|
||||
/// leaves (JLREQ Fig 138), so the two readings never meet over it. `K`
|
||||
/// identifies a neighbouring character.
|
||||
pub(crate) struct OverhangClaims<K> {
|
||||
claimed: HashMap<K, f32>,
|
||||
}
|
||||
|
||||
impl<K: Eq + Hash> OverhangClaims<K> {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self {
|
||||
claimed: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Room (before, after) left for a reading `overflow` longer than its
|
||||
/// base, given the `room` its neighbours `before` and `after` offer, in
|
||||
/// reading order. Records how far the reading hangs over `after`.
|
||||
pub(crate) fn room(
|
||||
&mut self,
|
||||
before: Option<K>,
|
||||
after: Option<K>,
|
||||
room: (f32, f32),
|
||||
overflow: f32,
|
||||
) -> (f32, f32) {
|
||||
let taken = before
|
||||
.and_then(|key| self.claimed.get(&key))
|
||||
.copied()
|
||||
.unwrap_or(0.0);
|
||||
let room = ((room.0 - taken).max(0.0), room.1);
|
||||
if let Some(after) = after {
|
||||
self.claimed.insert(after, ruby_overhangs(overflow, room).1);
|
||||
}
|
||||
room
|
||||
}
|
||||
}
|
||||
|
||||
/// Gap that spreads a base under a reading longer than itself (JLREQ
|
||||
/// §3.3.8). What the reading set solid (`ruby_length`) overflows the base
|
||||
/// by, less the overhang `room`, becomes one gap per base character: a full
|
||||
/// gap between characters and half a gap at each end.
|
||||
pub(crate) fn long_ruby_gap(
|
||||
ruby_length: f32,
|
||||
base_length: f32,
|
||||
room: (f32, f32),
|
||||
base_chars: usize,
|
||||
) -> f32 {
|
||||
if base_chars == 0 {
|
||||
return 0.0;
|
||||
}
|
||||
let overflow = ruby_length - base_length - room.0 - room.1;
|
||||
(overflow / base_chars as f32).max(0.0)
|
||||
}
|
||||
|
||||
/// Pieces a reading is set in (JLREQ §3.3.4): an upright glyph is a piece
|
||||
/// of its own in a `slot`; a run of Western glyphs is one word at its own
|
||||
/// advances, which distribution never spreads. `glyphs` holds (western,
|
||||
/// advance) per glyph. Returns each piece's extent and, per glyph, its piece
|
||||
/// and its offset inside it.
|
||||
pub(crate) fn ruby_pieces(glyphs: &[(bool, f32)], slot: f32) -> (Vec<f32>, Vec<(usize, f32)>) {
|
||||
let mut extents: Vec<f32> = Vec::new();
|
||||
let mut placement = Vec::with_capacity(glyphs.len());
|
||||
let mut previous_western = false;
|
||||
for &(western, advance) in glyphs {
|
||||
if western && previous_western {
|
||||
let piece = extents.len() - 1;
|
||||
placement.push((piece, extents[piece]));
|
||||
extents[piece] += advance;
|
||||
} else {
|
||||
placement.push((extents.len(), 0.0));
|
||||
extents.push(if western { advance } else { slot });
|
||||
}
|
||||
previous_western = western;
|
||||
}
|
||||
(extents, placement)
|
||||
}
|
||||
|
||||
/// Flow-axis start of each reading piece of `extents` along the base segment
|
||||
/// `[seg_top, seg_top + seg_extent)`. Per jlreq:
|
||||
///
|
||||
/// - A reading that fits the base is placed by `align`; `SpaceAround` is even
|
||||
/// distribution (均等割り付け): an equal gap around each piece.
|
||||
/// - A longer reading packs solid and overhangs the base, centred where the
|
||||
/// `room` (before, after) of its neighbours allows. Layout grows the base so
|
||||
/// the overflow fits that room.
|
||||
pub(crate) fn distribute_ruby_pieces(
|
||||
seg_top: f32,
|
||||
seg_extent: f32,
|
||||
extents: &[f32],
|
||||
align: RubyAlign,
|
||||
room: (f32, f32),
|
||||
) -> Vec<f32> {
|
||||
let count = extents.len();
|
||||
if count == 0 {
|
||||
return Vec::new();
|
||||
}
|
||||
let line: f32 = extents.iter().sum();
|
||||
let packed = |start: f32, gap: f32| -> Vec<f32> {
|
||||
let mut cursor = start;
|
||||
extents
|
||||
.iter()
|
||||
.map(|extent| {
|
||||
let top = cursor;
|
||||
cursor += extent + gap;
|
||||
top
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
if line <= seg_extent {
|
||||
let free = seg_extent - line;
|
||||
match align {
|
||||
RubyAlign::SpaceAround => {
|
||||
let gap = free / count as f32;
|
||||
packed(seg_top + gap / 2.0, gap)
|
||||
}
|
||||
RubyAlign::Center => packed(seg_top + free / 2.0, 0.0),
|
||||
RubyAlign::Start => packed(seg_top, 0.0),
|
||||
RubyAlign::SpaceBetween if count > 1 => packed(seg_top, free / (count - 1) as f32),
|
||||
RubyAlign::SpaceBetween => packed(seg_top + free / 2.0, 0.0),
|
||||
}
|
||||
} else {
|
||||
let overflow = line - seg_extent;
|
||||
let (before, after) = room;
|
||||
let overhang = (overflow / 2.0)
|
||||
.min(before)
|
||||
.max((overflow - after).min(before));
|
||||
packed(seg_top - overhang.max(0.0), 0.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn ruby_shorter_than_base_distributes_evenly() {
|
||||
// A ruby line of 2 x 50 equals the base extent (100): one glyph per
|
||||
// slot, with no offset.
|
||||
let tops =
|
||||
distribute_ruby_pieces(0.0, 100.0, &[50.0; 2], RubyAlign::SpaceAround, (50.0, 50.0));
|
||||
assert_eq!(tops.len(), 2);
|
||||
assert!(
|
||||
(tops[0] - 0.0).abs() < 0.001,
|
||||
"first ruby glyph at slot start"
|
||||
);
|
||||
assert!(
|
||||
(tops[1] - 50.0).abs() < 0.001,
|
||||
"second ruby glyph one slot down"
|
||||
);
|
||||
|
||||
// A wide base (extent 200) spreads 2 glyphs of advance 50 into slots
|
||||
// of 100.
|
||||
let spread =
|
||||
distribute_ruby_pieces(0.0, 200.0, &[50.0; 2], RubyAlign::SpaceAround, (50.0, 50.0));
|
||||
assert!(
|
||||
(spread[0] - 25.0).abs() < 0.001,
|
||||
"ruby glyph centred in its slot"
|
||||
);
|
||||
assert!(
|
||||
(spread[1] - spread[0] - 100.0).abs() < 0.001,
|
||||
"even distribution keeps a full slot between glyphs, not the advance"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ruby_longer_than_base_overhangs_symmetrically() {
|
||||
// A ruby line of 80 over a 40 base centres on the base, overhanging
|
||||
// each end by 20 (the one-em cap).
|
||||
let tops =
|
||||
distribute_ruby_pieces(0.0, 40.0, &[20.0; 4], RubyAlign::SpaceAround, (50.0, 50.0));
|
||||
assert_eq!(tops.len(), 4);
|
||||
assert!(tops[0] < 0.0, "long ruby overhangs above the base top");
|
||||
let block_center = (tops[0] + tops[3] + 20.0) / 2.0;
|
||||
assert!(
|
||||
(block_center - 20.0).abs() < 0.001,
|
||||
"the ruby block stays centred on the base"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ruby_alignment_modes_control_short_annotation_distribution() {
|
||||
assert_eq!(
|
||||
distribute_ruby_pieces(0.0, 20.0, &[4.0; 2], RubyAlign::SpaceAround, (4.0, 4.0),),
|
||||
vec![3.0, 13.0]
|
||||
);
|
||||
assert_eq!(
|
||||
distribute_ruby_pieces(0.0, 20.0, &[4.0; 2], RubyAlign::Center, (4.0, 4.0)),
|
||||
vec![6.0, 10.0]
|
||||
);
|
||||
assert_eq!(
|
||||
distribute_ruby_pieces(0.0, 20.0, &[4.0; 2], RubyAlign::Start, (4.0, 4.0)),
|
||||
vec![0.0, 4.0]
|
||||
);
|
||||
assert_eq!(
|
||||
distribute_ruby_pieces(0.0, 20.0, &[4.0; 2], RubyAlign::SpaceBetween, (4.0, 4.0),),
|
||||
vec![0.0, 16.0]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ruby_overhang_none_keeps_long_annotation_at_base_start() {
|
||||
let automatic = distribute_ruby_pieces(0.0, 10.0, &[4.0; 4], RubyAlign::Center, (4.0, 4.0));
|
||||
let constrained =
|
||||
distribute_ruby_pieces(0.0, 10.0, &[4.0; 4], RubyAlign::Center, (0.0, 0.0));
|
||||
|
||||
assert_eq!(automatic, vec![-3.0, 1.0, 5.0, 9.0]);
|
||||
assert_eq!(constrained, vec![0.0, 4.0, 8.0, 12.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_ruby_gap_spreads_the_overflow_left_after_the_room() {
|
||||
// 60 of reading over 40 of base, 4 of room on each side: 12 left
|
||||
// over two characters, one gap each, half of it at each end.
|
||||
assert_eq!(long_ruby_gap(60.0, 40.0, (4.0, 4.0), 2), 6.0);
|
||||
assert_eq!(long_ruby_gap(20.0, 40.0, (0.0, 0.0), 2), 0.0);
|
||||
assert_eq!(long_ruby_gap(60.0, 40.0, (0.0, 0.0), 0), 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ruby_overhangs_follow_the_distribution() {
|
||||
assert_eq!(ruby_overhangs(0.0, (5.0, 5.0)), (0.0, 0.0));
|
||||
assert_eq!(ruby_overhangs(6.0, (5.0, 5.0)), (3.0, 3.0));
|
||||
assert_eq!(ruby_overhangs(6.0, (5.0, 0.0)), (5.0, 0.0));
|
||||
assert_eq!(ruby_overhangs(6.0, (0.0, 5.0)), (0.0, 5.0));
|
||||
assert_eq!(ruby_overhangs(20.0, (5.0, 5.0)), (5.0, 5.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ruby_pieces_keep_a_western_word_whole() {
|
||||
// エディ + "tor": three upright slots, then one word at its advances.
|
||||
let glyphs = [
|
||||
(false, 9.0),
|
||||
(false, 9.0),
|
||||
(true, 4.0),
|
||||
(true, 5.0),
|
||||
(true, 3.0),
|
||||
];
|
||||
let (extents, placement) = ruby_pieces(&glyphs, 10.0);
|
||||
assert_eq!(extents, vec![10.0, 10.0, 12.0]);
|
||||
assert_eq!(
|
||||
placement,
|
||||
vec![(0, 0.0), (1, 0.0), (2, 0.0), (2, 4.0), (2, 9.0)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn distributing_a_word_never_spreads_its_letters() {
|
||||
// JLREQ Fig 112: "editor" over 編集者 is one piece centred on the base.
|
||||
let tops = distribute_ruby_pieces(0.0, 60.0, &[30.0], RubyAlign::SpaceAround, (0.0, 0.0));
|
||||
assert_eq!(tops, vec![15.0]);
|
||||
let tops =
|
||||
distribute_ruby_pieces(0.0, 60.0, &[10.0, 30.0], RubyAlign::SpaceAround, (0.0, 0.0));
|
||||
assert_eq!(tops, vec![5.0, 25.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ruby_overhang_room_allows_kana_and_punctuation_without_ruby() {
|
||||
assert_eq!(
|
||||
ruby_overhang_room(RubyOverhang::Auto, Some('か'), false, 20.0, 10.0),
|
||||
10.0
|
||||
);
|
||||
// JLREQ Fig 135: 偏、冠、脚 hangs かんむり over the commas; Fig 136:
|
||||
// never over a bracket.
|
||||
for ch in ['、', '。', '・'] {
|
||||
assert_eq!(
|
||||
ruby_overhang_room(RubyOverhang::Auto, Some(ch), false, 20.0, 10.0),
|
||||
10.0
|
||||
);
|
||||
}
|
||||
for ch in ['「', '」'] {
|
||||
assert_eq!(
|
||||
ruby_overhang_room(RubyOverhang::Auto, Some(ch), false, 20.0, 10.0),
|
||||
0.0
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
ruby_overhang_room(RubyOverhang::Auto, Some('か'), false, 12.0, 10.0),
|
||||
6.0
|
||||
);
|
||||
assert_eq!(
|
||||
ruby_overhang_room(RubyOverhang::Auto, Some('漢'), false, 20.0, 10.0),
|
||||
0.0
|
||||
);
|
||||
assert_eq!(
|
||||
ruby_overhang_room(RubyOverhang::Auto, Some('か'), true, 20.0, 10.0),
|
||||
0.0
|
||||
);
|
||||
assert_eq!(
|
||||
ruby_overhang_room(RubyOverhang::Auto, None, false, 20.0, 10.0),
|
||||
0.0
|
||||
);
|
||||
assert_eq!(
|
||||
ruby_overhang_room(RubyOverhang::None, Some('か'), false, 20.0, 10.0),
|
||||
0.0
|
||||
);
|
||||
}
|
||||
}
|
||||
226
render-wasm/src/shapes/text_shaping/font_tables.rs
Normal file
226
render-wasm/src/shapes/text_shaping/font_tables.rs
Normal file
@ -0,0 +1,226 @@
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
use std::thread::LocalKey;
|
||||
|
||||
use skia_safe::{Font, GlyphId};
|
||||
|
||||
use crate::shapes::gpos_vpal::{parse_vpal, VpalDelta};
|
||||
|
||||
/// Parsed font tables keyed by `SkTypefaceID`. `None` is cached too: a face
|
||||
/// without the table never gains it.
|
||||
type TableCache<T> = RefCell<HashMap<u32, Option<Rc<T>>>>;
|
||||
|
||||
thread_local! {
|
||||
// Layout runs on every paint; each face's tables are parsed once.
|
||||
static VERTICAL_METRICS: TableCache<VerticalMetrics> = RefCell::default();
|
||||
static VPAL_TABLES: TableCache<VpalTable> = RefCell::default();
|
||||
static TYPO_METRICS: TableCache<TypoMetrics> = RefCell::default();
|
||||
}
|
||||
|
||||
fn cached_table<T: 'static>(
|
||||
cache: &'static LocalKey<TableCache<T>>,
|
||||
font: &Font,
|
||||
parse: impl FnOnce(&Font) -> Option<T>,
|
||||
) -> Option<Rc<T>> {
|
||||
let id = font.typeface().unique_id();
|
||||
cache.with(|cache| {
|
||||
cache
|
||||
.borrow_mut()
|
||||
.entry(id)
|
||||
.or_insert_with(|| parse(font).map(Rc::new))
|
||||
.clone()
|
||||
})
|
||||
}
|
||||
|
||||
fn units_per_em(font: &Font) -> Option<f32> {
|
||||
let units_per_em = font.typeface().units_per_em()? as f32;
|
||||
(units_per_em > 0.0).then_some(units_per_em)
|
||||
}
|
||||
|
||||
fn table_data(font: &Font, tag: &[u8; 4]) -> Option<skia_safe::Data> {
|
||||
font.typeface().copy_table_data(u32::from_be_bytes(*tag))
|
||||
}
|
||||
|
||||
/// Vertical advances from a font's `vhea`/`vmtx` tables. Upright cells
|
||||
/// advance by the glyph's vertical advance, not its shaped horizontal one;
|
||||
/// the two differ for vertical alternates and proportional glyphs (e.g. the
|
||||
/// vertical kana repeat marks).
|
||||
pub(crate) struct VerticalMetrics {
|
||||
units_per_em: f32,
|
||||
/// Advance heights of the first `advances.len()` glyph ids.
|
||||
advances: Vec<u16>,
|
||||
/// Advance for glyph ids at or beyond the long-metric count.
|
||||
last: u16,
|
||||
}
|
||||
|
||||
impl VerticalMetrics {
|
||||
/// Parses `vhea`/`vmtx`. `None` when the font has no vertical metrics
|
||||
/// (callers keep horizontal advances).
|
||||
pub(crate) fn from_font(font: &Font) -> Option<Self> {
|
||||
let units_per_em = units_per_em(font)?;
|
||||
let vhea = table_data(font, b"vhea")?;
|
||||
let vmtx = table_data(font, b"vmtx")?;
|
||||
let vhea = vhea.as_bytes();
|
||||
let vmtx = vmtx.as_bytes();
|
||||
// `numberOfLongVerMetrics` is the trailing u16 of the 36-byte header.
|
||||
let num_long = u16::from_be_bytes([*vhea.get(34)?, *vhea.get(35)?]) as usize;
|
||||
if num_long == 0 {
|
||||
return None;
|
||||
}
|
||||
// Each long metric is { advanceHeight: u16, topSideBearing: i16 }.
|
||||
let mut advances = Vec::with_capacity(num_long);
|
||||
for i in 0..num_long {
|
||||
let o = i * 4;
|
||||
let (Some(&hi), Some(&lo)) = (vmtx.get(o), vmtx.get(o + 1)) else {
|
||||
break;
|
||||
};
|
||||
advances.push(u16::from_be_bytes([hi, lo]));
|
||||
}
|
||||
let last = *advances.last()?;
|
||||
Some(Self {
|
||||
units_per_em,
|
||||
advances,
|
||||
last,
|
||||
})
|
||||
}
|
||||
|
||||
/// Vertical advance of `glyph` at `font_size`, in pixels (same units as
|
||||
/// the horizontal advances).
|
||||
pub(crate) fn advance(&self, glyph: GlyphId, font_size: f32) -> f32 {
|
||||
let raw = self
|
||||
.advances
|
||||
.get(glyph as usize)
|
||||
.copied()
|
||||
.unwrap_or(self.last);
|
||||
raw as f32 * font_size / self.units_per_em
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn vertical_metrics(font: &Font) -> Option<Rc<VerticalMetrics>> {
|
||||
cached_table(&VERTICAL_METRICS, font, VerticalMetrics::from_font)
|
||||
}
|
||||
|
||||
/// GPOS `vpal` deltas for one typeface, in font units.
|
||||
pub(crate) struct VpalTable {
|
||||
units_per_em: f32,
|
||||
deltas: HashMap<u16, VpalDelta>,
|
||||
}
|
||||
|
||||
impl VpalTable {
|
||||
fn from_font(font: &Font) -> Option<Self> {
|
||||
let units_per_em = units_per_em(font)?;
|
||||
let gpos = table_data(font, b"GPOS")?;
|
||||
let deltas = parse_vpal(gpos.as_bytes())?;
|
||||
Some(Self {
|
||||
units_per_em,
|
||||
deltas,
|
||||
})
|
||||
}
|
||||
|
||||
/// Pixel deltas for a cluster at `font_size`: summed advance delta
|
||||
/// (negative when the cell tightens) and flow-axis ink shift (positive
|
||||
/// down the column; GPOS `yPlacement` is y-up, so its sign flips).
|
||||
/// `None` when no glyph of the cluster is covered.
|
||||
pub(crate) fn cluster_delta(&self, glyphs: &[GlyphId], font_size: f32) -> Option<(f32, f32)> {
|
||||
let scale = font_size / self.units_per_em;
|
||||
let mut advance = 0.0f32;
|
||||
let mut flow_shift = None;
|
||||
for glyph in glyphs {
|
||||
if let Some(delta) = self.deltas.get(glyph) {
|
||||
advance += delta.y_advance as f32 * scale;
|
||||
flow_shift.get_or_insert(-(delta.y_placement as f32) * scale);
|
||||
}
|
||||
}
|
||||
flow_shift.map(|shift| (advance, shift))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn vpal_table(font: &Font) -> Option<Rc<VpalTable>> {
|
||||
cached_table(&VPAL_TABLES, font, VpalTable::from_font)
|
||||
}
|
||||
|
||||
/// OS/2 typographic ascender/descender over unitsPerEm. They bound the
|
||||
/// ideographic em box, which the browser uses for the vertical central
|
||||
/// baseline. `hhea`/`Font::metrics` ascent exceeds the em in CJK faces and
|
||||
/// would push an upright glyph off the centre of its cell.
|
||||
struct TypoMetrics {
|
||||
/// sTypoAscender / unitsPerEm (positive, above the baseline).
|
||||
ascender: f32,
|
||||
/// sTypoDescender / unitsPerEm (negative, below the baseline).
|
||||
descender: f32,
|
||||
}
|
||||
|
||||
impl TypoMetrics {
|
||||
fn from_font(font: &Font) -> Option<Self> {
|
||||
let units_per_em = units_per_em(font)?;
|
||||
let os2 = table_data(font, b"OS/2")?;
|
||||
let os2 = os2.as_bytes();
|
||||
// sTypoAscender @ 68, sTypoDescender @ 70 (i16), in every OS/2 version.
|
||||
let ascender = i16::from_be_bytes([*os2.get(68)?, *os2.get(69)?]) as f32;
|
||||
let descender = i16::from_be_bytes([*os2.get(70)?, *os2.get(71)?]) as f32;
|
||||
Some(Self {
|
||||
ascender: ascender / units_per_em,
|
||||
descender: descender / units_per_em,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Ascent/descent for centring an upright cell, in Skia's sign convention
|
||||
/// (ascent negative, descent positive) at the font's size. Uses the OS/2
|
||||
/// typographic metrics, which match the browser's vertical central baseline
|
||||
/// in the SVG/foreignObject export, or `Font::metrics` without an OS/2 table.
|
||||
pub(crate) fn upright_centre_metrics(font: &Font) -> (f32, f32) {
|
||||
if let Some(typo) = cached_table(&TYPO_METRICS, font, TypoMetrics::from_font) {
|
||||
let size = font.size();
|
||||
return (-typo.ascender * size, -typo.descender * size);
|
||||
}
|
||||
let (_, metrics) = font.metrics();
|
||||
(metrics.ascent, metrics.descent)
|
||||
}
|
||||
|
||||
/// Offset from an upright cell's top edge to the glyph baseline. Centres the
|
||||
/// glyph's line box in the em cell, as Tate-chu-yoko does. CJK faces have an
|
||||
/// ascent larger than the em, so hanging glyphs from the ascent would push
|
||||
/// them below their cells and overflow the column.
|
||||
pub(crate) fn upright_baseline_offset(ascent: f32, descent: f32, font_size: f32) -> f32 {
|
||||
font_size / 2.0 - (ascent + descent) / 2.0
|
||||
}
|
||||
|
||||
/// Baseline offset for an upright glyph of `font` in an em cell of `font_size`.
|
||||
pub(crate) fn upright_baseline(font: &Font, font_size: f32) -> f32 {
|
||||
let (ascent, descent) = upright_centre_metrics(font);
|
||||
upright_baseline_offset(ascent, descent, font_size)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::shapes::text_vertical::test_support::{TEST_FONT, VMTX_TEST_FONT};
|
||||
use skia_safe::FontMgr;
|
||||
|
||||
#[test]
|
||||
fn vertical_metrics_parse_vmtx() {
|
||||
let font_mgr = FontMgr::new();
|
||||
let typeface = font_mgr
|
||||
.new_from_data(skia_safe::Data::new_copy(VMTX_TEST_FONT), None)
|
||||
.unwrap();
|
||||
let font = Font::new(typeface, 20.0);
|
||||
let vm = VerticalMetrics::from_font(&font).expect("vmtx present");
|
||||
// gid 1 = 〱 (2000 units), gid 3+ fall back to the last long metric.
|
||||
assert!((vm.advance(1, 20.0) - 40.0).abs() < 0.01);
|
||||
assert!((vm.advance(3, 20.0) - 20.0).abs() < 0.01);
|
||||
assert!((vm.advance(99, 20.0) - 20.0).abs() < 0.01);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vertical_metrics_absent_without_vmtx() {
|
||||
// The bundled Source Sans face carries no `vmtx`.
|
||||
let font_mgr = FontMgr::new();
|
||||
let typeface = font_mgr
|
||||
.new_from_data(skia_safe::Data::new_copy(TEST_FONT), None)
|
||||
.unwrap();
|
||||
let font = Font::new(typeface, 20.0);
|
||||
assert!(VerticalMetrics::from_font(&font).is_none());
|
||||
}
|
||||
}
|
||||
12
render-wasm/src/shapes/text_shaping/mod.rs
Normal file
12
render-wasm/src/shapes/text_shaping/mod.rs
Normal file
@ -0,0 +1,12 @@
|
||||
//! Glyph shaping shared by the vertical engine and the horizontal annotation
|
||||
//! painters (ruby, Zhuyin, emphasis): SkShaper runs with per-span font
|
||||
//! fallback, upright/sideways orientation and the font tables (vmtx, vpal,
|
||||
//! OS/2 typo metrics) the upright em box comes from.
|
||||
|
||||
mod font_tables;
|
||||
mod orientation;
|
||||
mod shaping;
|
||||
|
||||
pub(crate) use font_tables::*;
|
||||
pub(crate) use orientation::*;
|
||||
pub(crate) use shaping::*;
|
||||
188
render-wasm/src/shapes/text_shaping/orientation.rs
Normal file
188
render-wasm/src/shapes/text_shaping/orientation.rs
Normal file
@ -0,0 +1,188 @@
|
||||
use crate::shapes::TextOrientation;
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
/// Emoji ranges recognized by the frontend font-loader. Emoji stay upright
|
||||
/// in vertical flow; Latin text rotates under `text-orientation: mixed`.
|
||||
pub(crate) fn is_emoji_char(c: char) -> bool {
|
||||
matches!(u32::from(c),
|
||||
0x2300..=0x23FF
|
||||
| 0x2600..=0x27BF
|
||||
| 0x2B00..=0x2BFF
|
||||
| 0x1F000..=0x1FAFF
|
||||
)
|
||||
}
|
||||
|
||||
/// Characters that stay upright in vertical flow: kana, kanji, hangul,
|
||||
/// bopomofo, CJK punctuation, full-width forms and emoji. Everything else
|
||||
/// rotates sideways under `text-orientation: mixed`. A table of its own: it
|
||||
/// leaves half-width katakana sideways, unlike `cjk_chars::is_kana`.
|
||||
pub(crate) fn is_upright_char(c: char) -> bool {
|
||||
is_emoji_char(c)
|
||||
|| matches!(u32::from(c),
|
||||
0x2E80..=0x2FDF // CJK radicals, Kangxi radicals
|
||||
| 0x1100..=0x11FF // conjoining Hangul Jamo
|
||||
| 0x3100..=0x318F // Bopomofo and compatibility Jamo
|
||||
| 0x31A0..=0x31BF // Bopomofo extended
|
||||
| 0xA960..=0xA97F // Hangul Jamo extended A
|
||||
| 0xD7B0..=0xD7FF // Hangul Jamo extended B
|
||||
| 0x3000..=0x303F // CJK symbols and punctuation
|
||||
| 0x3040..=0x30FF // hiragana, katakana
|
||||
| 0x31C0..=0x31EF // CJK strokes
|
||||
| 0x31F0..=0x31FF // katakana phonetic extensions
|
||||
| 0x3200..=0x33FF // enclosed CJK, CJK compatibility
|
||||
| 0x3400..=0x4DBF // CJK unified ideographs extension A
|
||||
| 0x4E00..=0x9FFF // CJK unified ideographs
|
||||
| 0xAC00..=0xD7AF // hangul syllables
|
||||
| 0xF900..=0xFAFF // CJK compatibility ideographs
|
||||
| 0xFE30..=0xFE4F // CJK compatibility forms
|
||||
| 0xFF00..=0xFF60 // full-width forms
|
||||
| 0xFFE0..=0xFFE6 // full-width signs
|
||||
| 0x20000..=0x2FA1F // CJK extensions B..F
|
||||
)
|
||||
}
|
||||
|
||||
/// Characters whose horizontal glyph needs a vertical alternate, normally
|
||||
/// from `vert` / `vrt2`. When the face has no such substitution, the
|
||||
/// horizontal glyph rotates clockwise as a fallback. Covers UAX #50 `Tr`,
|
||||
/// plus comma/full-stop punctuation whose plain glyph sits in the wrong
|
||||
/// half of the vertical em box.
|
||||
pub(crate) fn uses_rotated_vertical_fallback(c: char) -> bool {
|
||||
matches!(u32::from(c),
|
||||
0x2018..=0x2019 // single quotation marks
|
||||
| 0x201C..=0x201D // double quotation marks
|
||||
| 0x2329..=0x232A // angle brackets
|
||||
| 0x3001..=0x3002 // ideographic comma / full stop
|
||||
| 0x3008..=0x301F // CJK brackets, wave dash and quotation marks
|
||||
| 0x3030 // wavy dash
|
||||
| 0x30A0 // katakana-hiragana double hyphen
|
||||
| 0x30FC // prolonged sound mark
|
||||
| 0xFE50..=0xFE52 // small comma / ideographic comma / full stop
|
||||
| 0xFE59..=0xFE5E // small brackets
|
||||
| 0xFF08..=0xFF09 // full-width parentheses
|
||||
| 0xFF0C // full-width comma
|
||||
| 0xFF0D // full-width hyphen-minus
|
||||
| 0xFF0E // full-width full stop
|
||||
| 0xFF1A..=0xFF1B // full-width colon / semicolon
|
||||
| 0xFF1C..=0xFF1E // full-width comparison signs
|
||||
| 0xFF3B // full-width left square bracket
|
||||
| 0xFF3D // full-width right square bracket
|
||||
| 0xFF3F // full-width low line
|
||||
| 0xFF5B..=0xFF60 // full-width braces, bars, tilde and white parentheses
|
||||
| 0xFFE3 // full-width macron
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub(crate) struct Segment {
|
||||
pub text: String,
|
||||
/// UTF-16 offset of the segment start within its span text.
|
||||
pub utf16_start: usize,
|
||||
pub upright: bool,
|
||||
}
|
||||
|
||||
/// Split text into maximal runs of same orientation. Under
|
||||
/// `TextOrientation::Upright` every character is upright.
|
||||
pub(crate) fn segment_by_orientation(text: &str, orientation: TextOrientation) -> Vec<Segment> {
|
||||
let mut segments: Vec<Segment> = Vec::new();
|
||||
let mut utf16_offset = 0;
|
||||
for grapheme in text.graphemes(true) {
|
||||
let c = grapheme.chars().next().unwrap();
|
||||
let upright = orientation == TextOrientation::Upright || is_upright_char(c);
|
||||
let emoji = is_emoji_char(c);
|
||||
// CJK and emoji are both upright, but split them so the segment
|
||||
// probe can select the emoji family: the provider's generic fallback
|
||||
// iterator does not reliably switch from a CJK face to a color-emoji
|
||||
// face.
|
||||
match segments.last_mut() {
|
||||
Some(last)
|
||||
if last.upright == upright
|
||||
&& last.text.chars().next_back().is_some_and(is_emoji_char) == emoji =>
|
||||
{
|
||||
last.text.push_str(grapheme)
|
||||
}
|
||||
_ => segments.push(Segment {
|
||||
text: grapheme.to_string(),
|
||||
utf16_start: utf16_offset,
|
||||
upright,
|
||||
}),
|
||||
}
|
||||
utf16_offset += grapheme.encode_utf16().count();
|
||||
}
|
||||
segments
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn upright_classification() {
|
||||
assert!(is_upright_char('あ'));
|
||||
assert!(is_upright_char('ア'));
|
||||
assert!(is_upright_char('漢'));
|
||||
assert!(is_upright_char('。'));
|
||||
assert!(is_upright_char('「'));
|
||||
assert!(is_upright_char('ー'));
|
||||
assert!(is_upright_char('!'));
|
||||
assert!(!is_upright_char('A'));
|
||||
assert!(!is_upright_char('1'));
|
||||
assert!(!is_upright_char(' '));
|
||||
assert!(!is_upright_char('.'));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn emoji_stay_upright_in_vertical_text() {
|
||||
for emoji in ['😆', '💦', '🙇'] {
|
||||
assert!(is_upright_char(emoji));
|
||||
}
|
||||
|
||||
let segments = segment_by_orientation("久😆元💦🙇", TextOrientation::Mixed);
|
||||
assert_eq!(segments.len(), 4);
|
||||
assert!(segments.iter().all(|segment| segment.upright));
|
||||
assert_eq!(segments[1].text, "😆");
|
||||
assert_eq!(segments[3].text, "💦🙇");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transformed_japanese_punctuation_has_a_rotated_font_fallback() {
|
||||
for character in [
|
||||
'「', '」', '『', '』', '(', ')', '[', ']', '【', '】', '、', '。', 'ー', '〜',
|
||||
':', ';',
|
||||
] {
|
||||
assert!(
|
||||
uses_rotated_vertical_fallback(character),
|
||||
"{character} needs a transformed vertical glyph"
|
||||
);
|
||||
}
|
||||
assert!(!uses_rotated_vertical_fallback('あ'));
|
||||
assert!(!uses_rotated_vertical_fallback('漢'));
|
||||
assert!(!uses_rotated_vertical_fallback('!'));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn segments_mixed_text() {
|
||||
let segments = segment_by_orientation("縦書きABC123です", TextOrientation::Mixed);
|
||||
assert_eq!(segments.len(), 3);
|
||||
assert_eq!(segments[0].text, "縦書き");
|
||||
assert!(segments[0].upright);
|
||||
assert_eq!(segments[0].utf16_start, 0);
|
||||
assert_eq!(segments[1].text, "ABC123");
|
||||
assert!(!segments[1].upright);
|
||||
assert_eq!(segments[1].utf16_start, 3);
|
||||
assert_eq!(segments[2].text, "です");
|
||||
assert!(segments[2].upright);
|
||||
assert_eq!(segments[2].utf16_start, 9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn segments_upright_orientation_keeps_latin_upright() {
|
||||
let segments = segment_by_orientation("縦ABC", TextOrientation::Upright);
|
||||
assert_eq!(segments.len(), 1);
|
||||
assert!(segments[0].upright);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn segments_empty_text() {
|
||||
assert!(segment_by_orientation("", TextOrientation::Mixed).is_empty());
|
||||
}
|
||||
}
|
||||
648
render-wasm/src/shapes/text_shaping/shaping.rs
Normal file
648
render-wasm/src/shapes/text_shaping/shaping.rs
Normal file
@ -0,0 +1,648 @@
|
||||
use std::cell::RefCell;
|
||||
use std::ops::Range;
|
||||
|
||||
use skia_safe::{
|
||||
self as skia,
|
||||
shaper::{
|
||||
run_handler::{Buffer, RunInfo},
|
||||
Feature, RunHandler,
|
||||
},
|
||||
textlayout::TypefaceFontProvider,
|
||||
Font, FontMgr, GlyphId, Point as SkPoint, TextBlob, TextBlobBuilder, Typeface,
|
||||
};
|
||||
|
||||
use crate::render::{FallbackFonts, DEFAULT_EMOJI_FONT};
|
||||
use crate::shapes::{FontFeatures, TextSpan};
|
||||
|
||||
use super::font_tables::upright_centre_metrics;
|
||||
|
||||
/// JLREQ preferred width of a word space inside sideways Western text.
|
||||
pub(crate) const WESTERN_WORD_SPACING_EM: f32 = 1.0 / 3.0;
|
||||
|
||||
/// Candidate families for a span: its own font first, then emoji and the
|
||||
/// registered fallback fonts (Noto etc.) in the span language's order.
|
||||
pub(crate) fn span_font_families(span: &TextSpan, fallback_fonts: &FallbackFonts) -> Vec<String> {
|
||||
let mut families = vec![
|
||||
format!("{}", span.font_family),
|
||||
DEFAULT_EMOJI_FONT.to_string(),
|
||||
];
|
||||
families.extend(fallback_fonts.for_text(&span.language, &span.text));
|
||||
families
|
||||
}
|
||||
|
||||
/// One shaped run: glyphs from a single font, pen-relative positions,
|
||||
/// per-glyph horizontal advances and per-glyph UTF-8 cluster starts
|
||||
/// (relative to the shaped segment text).
|
||||
#[derive(Clone)]
|
||||
pub struct ShapedRun {
|
||||
pub font: Font,
|
||||
pub glyphs: Vec<GlyphId>,
|
||||
pub positions: Vec<SkPoint>,
|
||||
pub advances: Vec<f32>,
|
||||
pub clusters: Vec<u32>,
|
||||
pub advance: f32,
|
||||
pub utf8_range: Range<usize>,
|
||||
/// Source text of `utf8_range`, attached to the run's text blobs.
|
||||
pub text: String,
|
||||
/// Local-y offset that puts the font's central baseline on the column
|
||||
/// axis after 90° rotation.
|
||||
pub rotated_baseline_shift: f32,
|
||||
}
|
||||
|
||||
impl ShapedRun {
|
||||
/// Glyph clusters as (first glyph, glyph count). A combining sequence
|
||||
/// shapes to one cluster and must stay in one cell.
|
||||
pub(crate) fn cluster_spans(&self) -> impl Iterator<Item = (usize, usize)> + '_ {
|
||||
let mut glyph = 0usize;
|
||||
std::iter::from_fn(move || {
|
||||
let cluster = *self.clusters.get(glyph)?;
|
||||
let count = self.clusters[glyph..]
|
||||
.iter()
|
||||
.take_while(|other| **other == cluster)
|
||||
.count();
|
||||
let span = (glyph, count);
|
||||
glyph += count;
|
||||
Some(span)
|
||||
})
|
||||
}
|
||||
|
||||
/// Flow offset from the run start of each scalar boundary (0..=`chars`)
|
||||
/// of a sideways run, from its glyph positions; the last boundary is
|
||||
/// `extent`. A scalar inside a ligature takes the next glyph's position.
|
||||
pub(crate) fn scalar_flow_offsets(&self, extent: f32, chars: usize) -> Option<Vec<f32>> {
|
||||
let origin = self.positions.first()?.x;
|
||||
let mut offsets = Vec::with_capacity(chars + 1);
|
||||
let mut utf8 = self.utf8_range.start;
|
||||
for ch in self.text.chars().take(chars) {
|
||||
let flow = self
|
||||
.clusters
|
||||
.iter()
|
||||
.position(|cluster| *cluster as usize >= utf8)
|
||||
.map_or(extent, |glyph| self.positions[glyph].x - origin);
|
||||
offsets.push(flow);
|
||||
utf8 += ch.len_utf8();
|
||||
}
|
||||
offsets.push(extent);
|
||||
(offsets.len() == chars + 1).then_some(offsets)
|
||||
}
|
||||
|
||||
/// Summed horizontal advance of a cluster.
|
||||
pub(crate) fn cluster_advance(&self, glyph: usize, count: usize) -> f32 {
|
||||
self.advances[glyph..glyph + count].iter().sum()
|
||||
}
|
||||
|
||||
/// Positions of a cluster's glyphs relative to its first glyph's pen x.
|
||||
fn cluster_positions(&self, glyph: usize, count: usize) -> Vec<SkPoint> {
|
||||
let base_x = self.positions[glyph].x;
|
||||
self.positions[glyph..glyph + count]
|
||||
.iter()
|
||||
.map(|position| SkPoint::new(position.x - base_x, position.y))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Ink bounds of a cluster, relative to its first glyph's pen position.
|
||||
pub(crate) fn cluster_ink_bounds(&self, glyph: usize, count: usize) -> Option<skia::Rect> {
|
||||
glyph_run_ink_bounds(
|
||||
&self.font,
|
||||
&self.glyphs[glyph..glyph + count],
|
||||
&self.cluster_positions(glyph, count),
|
||||
)
|
||||
}
|
||||
|
||||
/// Ink bounds of the whole run at its shaped positions.
|
||||
pub(crate) fn ink_bounds(&self) -> Option<skia::Rect> {
|
||||
glyph_run_ink_bounds(&self.font, &self.glyphs, &self.positions)
|
||||
}
|
||||
|
||||
/// Byte range in `text` of the clusters covering `glyph..glyph + count`.
|
||||
fn text_range(&self, glyph: usize, count: usize) -> Range<usize> {
|
||||
let base = self.utf8_range.start as u32;
|
||||
let clusters = &self.clusters[glyph..glyph + count];
|
||||
let first = clusters.iter().copied().min().unwrap_or(base);
|
||||
let last = clusters.iter().copied().max().unwrap_or(base);
|
||||
// Clusters ascend along the run: the next larger one ends the text.
|
||||
let end = self.clusters[glyph + count..]
|
||||
.iter()
|
||||
.copied()
|
||||
.find(|cluster| *cluster > last)
|
||||
.map_or(self.text.len(), |cluster| {
|
||||
cluster.saturating_sub(base) as usize
|
||||
});
|
||||
(first.saturating_sub(base) as usize).min(end)..end
|
||||
}
|
||||
|
||||
/// Text blob of `glyph..glyph + count` at `positions`, carrying the
|
||||
/// clusters' source text so PDF output maps vertical alternates back to
|
||||
/// the original characters.
|
||||
fn text_blob(&self, glyph: usize, count: usize, positions: &[SkPoint]) -> Option<TextBlob> {
|
||||
let range = self.text_range(glyph, count);
|
||||
let Some(text) = self
|
||||
.text
|
||||
.as_bytes()
|
||||
.get(range.clone())
|
||||
.filter(|t| !t.is_empty())
|
||||
else {
|
||||
let mut builder = TextBlobBuilder::new();
|
||||
let (glyphs, points) = builder.alloc_run_pos(&self.font, count, None);
|
||||
glyphs.copy_from_slice(&self.glyphs[glyph..glyph + count]);
|
||||
points.copy_from_slice(positions);
|
||||
return builder.make();
|
||||
};
|
||||
let text_start = self.utf8_range.start as u32 + range.start as u32;
|
||||
let mut builder = TextBlobBuilder::new();
|
||||
let (glyphs, points, utf8_text, clusters) =
|
||||
builder.alloc_run_text_pos(&self.font, count, text.len(), None);
|
||||
glyphs.copy_from_slice(&self.glyphs[glyph..glyph + count]);
|
||||
points.copy_from_slice(positions);
|
||||
utf8_text.copy_from_slice(text);
|
||||
for (cluster, source) in clusters
|
||||
.iter_mut()
|
||||
.zip(&self.clusters[glyph..glyph + count])
|
||||
{
|
||||
*cluster = source.saturating_sub(text_start);
|
||||
}
|
||||
builder.make()
|
||||
}
|
||||
|
||||
/// Text blob of one cluster, positioned from its first glyph's pen x.
|
||||
pub(crate) fn cluster_blob(&self, glyph: usize, count: usize) -> Option<TextBlob> {
|
||||
self.text_blob(glyph, count, &self.cluster_positions(glyph, count))
|
||||
}
|
||||
|
||||
/// Text blob of the whole run at its shaped positions.
|
||||
pub(crate) fn blob(&self) -> Option<TextBlob> {
|
||||
self.text_blob(0, self.glyphs.len(), &self.positions)
|
||||
}
|
||||
|
||||
/// Text blob of one glyph at its pen origin. The first glyph of a
|
||||
/// cluster carries the cluster's source text; the rest are glyph-only.
|
||||
pub(crate) fn glyph_blob(&self, glyph: usize) -> Option<TextBlob> {
|
||||
self.glyphs.get(glyph)?;
|
||||
let starts_cluster = glyph == 0 || self.clusters.get(glyph) != self.clusters.get(glyph - 1);
|
||||
if starts_cluster {
|
||||
self.text_blob(glyph, 1, &[SkPoint::default()])
|
||||
} else {
|
||||
single_glyph_blob(&self.font, self.glyphs[glyph])
|
||||
}
|
||||
}
|
||||
|
||||
/// Local-y shift that centres a cluster's ink after a 90° rotation.
|
||||
pub(crate) fn cluster_rotated_baseline_shift(&self, glyph: usize, count: usize) -> f32 {
|
||||
rotated_run_baseline_shift(
|
||||
&self.font,
|
||||
&self.glyphs[glyph..glyph + count],
|
||||
&self.cluster_positions(glyph, count),
|
||||
)
|
||||
}
|
||||
|
||||
/// Spreads the glyphs by `letter_spacing` each, like horizontal
|
||||
/// per-glyph spacing. Returns the spaced run advance.
|
||||
pub(crate) fn spread_glyphs(&mut self, letter_spacing: f32) -> f32 {
|
||||
if letter_spacing != 0.0 {
|
||||
for (i, position) in self.positions.iter_mut().enumerate() {
|
||||
position.x += letter_spacing * i as f32;
|
||||
}
|
||||
}
|
||||
self.advance + letter_spacing * self.glyphs.len() as f32
|
||||
}
|
||||
|
||||
/// Sets ASCII word spaces in a sideways Western run to JLREQ's one-third
|
||||
/// em. The space keeps its scalar and break opportunity; only its advance
|
||||
/// and the following glyph positions change.
|
||||
pub(crate) fn normalize_word_spaces(&mut self, segment_text: &str, font_size: f32) {
|
||||
let clusters: Vec<(usize, usize)> = self.cluster_spans().collect();
|
||||
let mut accumulated_shift = 0.0f32;
|
||||
for (glyph, count) in clusters {
|
||||
for position in &mut self.positions[glyph..glyph + count] {
|
||||
position.x += accumulated_shift;
|
||||
}
|
||||
let is_word_space = segment_text
|
||||
.get(self.clusters[glyph] as usize..)
|
||||
.and_then(|text| text.chars().next())
|
||||
== Some(' ');
|
||||
if is_word_space {
|
||||
let delta =
|
||||
font_size * WESTERN_WORD_SPACING_EM - self.cluster_advance(glyph, count);
|
||||
self.advances[glyph + count - 1] += delta;
|
||||
accumulated_shift += delta;
|
||||
}
|
||||
}
|
||||
self.advance += accumulated_shift;
|
||||
}
|
||||
}
|
||||
|
||||
/// Text blob holding a single glyph drawn at its pen origin.
|
||||
pub(crate) fn single_glyph_blob(font: &Font, glyph: GlyphId) -> Option<TextBlob> {
|
||||
let mut builder = TextBlobBuilder::new();
|
||||
let (glyphs, points) = builder.alloc_run_pos(font, 1, None);
|
||||
glyphs[0] = glyph;
|
||||
points[0] = SkPoint::default();
|
||||
builder.make()
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct RunCollector {
|
||||
runs: Vec<ShapedRun>,
|
||||
scratch_glyphs: Vec<GlyphId>,
|
||||
scratch_positions: Vec<SkPoint>,
|
||||
scratch_clusters: Vec<u32>,
|
||||
}
|
||||
|
||||
impl RunHandler for RunCollector {
|
||||
fn begin_line(&mut self) {}
|
||||
|
||||
fn run_info(&mut self, _info: &RunInfo) {}
|
||||
|
||||
fn commit_run_info(&mut self) {}
|
||||
|
||||
fn run_buffer(&mut self, info: &RunInfo) -> Buffer<'_> {
|
||||
self.scratch_glyphs.resize(info.glyph_count, 0);
|
||||
self.scratch_positions
|
||||
.resize(info.glyph_count, SkPoint::default());
|
||||
self.scratch_clusters.resize(info.glyph_count, 0);
|
||||
Buffer {
|
||||
glyphs: &mut self.scratch_glyphs,
|
||||
positions: &mut self.scratch_positions,
|
||||
offsets: None,
|
||||
clusters: Some(&mut self.scratch_clusters),
|
||||
point: SkPoint::default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn commit_run_buffer(&mut self, info: &RunInfo) {
|
||||
if info.glyph_count == 0 {
|
||||
return;
|
||||
}
|
||||
let origin = self.scratch_positions[0];
|
||||
let positions: Vec<SkPoint> = self
|
||||
.scratch_positions
|
||||
.iter()
|
||||
.map(|p| SkPoint::new(p.x - origin.x, p.y))
|
||||
.collect();
|
||||
let end = positions[0].x + info.advance.x;
|
||||
let advances: Vec<f32> = positions
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, position)| positions.get(i + 1).map_or(end, |next| next.x) - position.x)
|
||||
.collect();
|
||||
self.runs.push(ShapedRun {
|
||||
rotated_baseline_shift: central_baseline_shift(info.font),
|
||||
font: info.font.clone(),
|
||||
glyphs: self.scratch_glyphs.clone(),
|
||||
positions,
|
||||
advances,
|
||||
clusters: self.scratch_clusters.clone(),
|
||||
advance: info.advance.x,
|
||||
utf8_range: info.utf8_range.clone(),
|
||||
text: String::new(),
|
||||
});
|
||||
}
|
||||
|
||||
fn commit_line(&mut self) {}
|
||||
}
|
||||
|
||||
fn feature(tag: &[u8; 4]) -> Feature {
|
||||
Feature {
|
||||
tag: u32::from_be_bytes(*tag),
|
||||
value: 1,
|
||||
start: 0,
|
||||
end: usize::MAX,
|
||||
}
|
||||
}
|
||||
|
||||
/// Omits `vpal`: SkShaper shapes on a horizontal line, where HarfBuzz would
|
||||
/// apply its y-placement deltas as glyph offsets without the advance deltas.
|
||||
/// Vertical layout applies the parsed GPOS `vpal` to upright cells itself.
|
||||
fn font_feature(font_features: FontFeatures) -> Option<Feature> {
|
||||
match font_features {
|
||||
FontFeatures::None => None,
|
||||
FontFeatures::Palt => Some(feature(b"palt")),
|
||||
FontFeatures::Vpal => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Shape one orientation segment on a single unbounded line. Upright
|
||||
/// segments get the OpenType vertical substitution features so CJK
|
||||
/// punctuation and brackets take their vertical forms.
|
||||
pub(crate) fn shape_segment(
|
||||
text: &str,
|
||||
font: &Font,
|
||||
upright: bool,
|
||||
font_features: FontFeatures,
|
||||
fallback: FontMgr,
|
||||
language: &str,
|
||||
) -> Vec<ShapedRun> {
|
||||
let mut font_iter = skia::Shaper::new_font_mgr_run_iterator(text, font, Some(fallback.clone()));
|
||||
let mut bidi_iter = skia::shapers::primitive::trivial_bidi_run_iterator(0, text.len());
|
||||
let mut script_iter = skia::Shaper::new_hb_icu_script_run_iterator(text);
|
||||
let mut lang_iter = skia::Shaper::new_trivial_language_run_iterator(language, text.len());
|
||||
|
||||
let mut features = if upright {
|
||||
vec![feature(b"vert"), feature(b"vrt2")]
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
if let Some(font_feature) = font_feature(font_features) {
|
||||
features.push(font_feature);
|
||||
}
|
||||
|
||||
let mut collector = RunCollector::default();
|
||||
with_shaper(&fallback, |shaper| {
|
||||
shaper.shape_with_iterators_and_features(
|
||||
text,
|
||||
&mut font_iter,
|
||||
&mut bidi_iter,
|
||||
&mut script_iter,
|
||||
&mut lang_iter,
|
||||
&features,
|
||||
f32::MAX,
|
||||
&mut collector,
|
||||
)
|
||||
});
|
||||
for run in &mut collector.runs {
|
||||
run.text = text
|
||||
.get(run.utf8_range.clone())
|
||||
.unwrap_or_default()
|
||||
.to_string();
|
||||
}
|
||||
collector.runs
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static SHAPER: RefCell<Option<skia::Shaper>> = const { RefCell::new(None) };
|
||||
}
|
||||
|
||||
/// Runs `f` with this thread's shaper, built on first use. One shaper serves
|
||||
/// every font manager: shaping with explicit iterators takes the fallback
|
||||
/// from the font run iterator, never from the shaper.
|
||||
fn with_shaper<R>(fallback: &FontMgr, f: impl FnOnce(&skia::Shaper) -> R) -> R {
|
||||
SHAPER.with(|cache| {
|
||||
let mut cache = cache.borrow_mut();
|
||||
f(cache.get_or_insert_with(|| skia::Shaper::new(fallback.clone())))
|
||||
})
|
||||
}
|
||||
|
||||
/// Combining marks, variation selectors and ZWJ belong to the preceding
|
||||
/// grapheme. They stay in that font run even without a standalone cmap
|
||||
/// entry, so HarfBuzz gets the complete shaping sequence.
|
||||
fn extends_previous_grapheme(ch: char) -> bool {
|
||||
matches!(u32::from(ch),
|
||||
0x0300..=0x036F
|
||||
| 0x1AB0..=0x1AFF
|
||||
| 0x1DC0..=0x1DFF
|
||||
| 0x200D
|
||||
| 0x20D0..=0x20FF
|
||||
| 0x3099..=0x309A
|
||||
| 0xFE00..=0xFE0F
|
||||
| 0xFE20..=0xFE2F
|
||||
| 0xE0100..=0xE01EF
|
||||
)
|
||||
}
|
||||
|
||||
/// Split `text` into byte ranges covered by one typeface each: the first of
|
||||
/// `typefaces` with a glyph for the character, or the primary face.
|
||||
fn typeface_chunks(text: &str, typefaces: &[Typeface]) -> Vec<(Range<usize>, usize)> {
|
||||
let mut chunks: Vec<(Range<usize>, usize)> = Vec::new();
|
||||
for (byte, ch) in text.char_indices() {
|
||||
let current = chunks.last().map(|(_, typeface)| *typeface);
|
||||
let selected = match current {
|
||||
Some(typeface) if extends_previous_grapheme(ch) => typeface,
|
||||
_ => typefaces
|
||||
.iter()
|
||||
.position(|typeface| typeface.unichar_to_glyph(ch as i32) != 0)
|
||||
.unwrap_or(0),
|
||||
};
|
||||
match chunks.last_mut() {
|
||||
Some((range, typeface)) if *typeface == selected => range.end = byte + ch.len_utf8(),
|
||||
_ => chunks.push((byte..byte + ch.len_utf8(), selected)),
|
||||
}
|
||||
}
|
||||
chunks
|
||||
}
|
||||
|
||||
/// Shape a segment with explicit per-character font fallback.
|
||||
/// `TypefaceFontProvider` resolves named families, but some Skia builds lack
|
||||
/// its character fallback hook, so glyph coverage is resolved here.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(crate) fn shape_segment_with_fallbacks(
|
||||
text: &str,
|
||||
font_size: f32,
|
||||
families: &[String],
|
||||
font_provider: &TypefaceFontProvider,
|
||||
upright: bool,
|
||||
font_features: FontFeatures,
|
||||
fallback_mgr: &FontMgr,
|
||||
language: &str,
|
||||
) -> Vec<ShapedRun> {
|
||||
shape_with_typefaces(
|
||||
text,
|
||||
font_size,
|
||||
&resolve_typefaces(families, font_provider),
|
||||
upright,
|
||||
font_features,
|
||||
fallback_mgr,
|
||||
language,
|
||||
)
|
||||
}
|
||||
|
||||
/// The typefaces of `families` that `font_provider` holds, in order.
|
||||
pub(crate) fn resolve_typefaces(
|
||||
families: &[String],
|
||||
font_provider: &TypefaceFontProvider,
|
||||
) -> Vec<Typeface> {
|
||||
families
|
||||
.iter()
|
||||
.filter_map(|family| font_provider.match_family_style(family, skia::FontStyle::default()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// `shape_segment_with_fallbacks` with the families already resolved.
|
||||
pub(crate) fn shape_with_typefaces(
|
||||
text: &str,
|
||||
font_size: f32,
|
||||
typefaces: &[Typeface],
|
||||
upright: bool,
|
||||
font_features: FontFeatures,
|
||||
fallback_mgr: &FontMgr,
|
||||
language: &str,
|
||||
) -> Vec<ShapedRun> {
|
||||
if typefaces.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let mut result = Vec::new();
|
||||
for (range, typeface_index) in typeface_chunks(text, typefaces) {
|
||||
let font = Font::new(typefaces[typeface_index].clone(), font_size);
|
||||
let mut shaped = shape_segment(
|
||||
&text[range.clone()],
|
||||
&font,
|
||||
upright,
|
||||
font_features,
|
||||
fallback_mgr.clone(),
|
||||
language,
|
||||
);
|
||||
for run in &mut shaped {
|
||||
run.utf8_range =
|
||||
(run.utf8_range.start + range.start)..(run.utf8_range.end + range.start);
|
||||
for cluster in &mut run.clusters {
|
||||
*cluster += range.start as u32;
|
||||
}
|
||||
}
|
||||
result.extend(shaped);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn glyph_run_ink_bounds(
|
||||
font: &Font,
|
||||
glyphs: &[GlyphId],
|
||||
positions: &[SkPoint],
|
||||
) -> Option<skia::Rect> {
|
||||
let mut bounds = vec![skia::Rect::default(); glyphs.len()];
|
||||
font.get_bounds(glyphs, &mut bounds, None);
|
||||
|
||||
let mut ink = skia::Rect::new(f32::MAX, f32::MAX, f32::MIN, f32::MIN);
|
||||
for (bound, position) in bounds.iter().zip(positions) {
|
||||
if bound.right > bound.left && bound.bottom > bound.top {
|
||||
ink.left = ink.left.min(bound.left + position.x);
|
||||
ink.top = ink.top.min(bound.top + position.y);
|
||||
ink.right = ink.right.max(bound.right + position.x);
|
||||
ink.bottom = ink.bottom.max(bound.bottom + position.y);
|
||||
}
|
||||
}
|
||||
|
||||
(ink.right > ink.left && ink.bottom > ink.top).then_some(ink)
|
||||
}
|
||||
|
||||
/// Offset that centres a local-y band on the column axis after the run is
|
||||
/// rotated 90°.
|
||||
pub(crate) fn rotated_baseline_shift(top: f32, bottom: f32) -> f32 {
|
||||
-(top + bottom) / 2.0
|
||||
}
|
||||
|
||||
/// Shift that puts the font's central baseline on the column axis: the
|
||||
/// middle of the em box upright glyphs centre on (`upright_centre_metrics`),
|
||||
/// scaled to 1em. It depends only on the font, so every sideways run of a
|
||||
/// face shares one baseline whatever its ink.
|
||||
fn central_baseline_shift(font: &Font) -> f32 {
|
||||
let (ascent, descent) = upright_centre_metrics(font);
|
||||
let band = descent - ascent;
|
||||
if band <= 0.0 {
|
||||
return 0.0;
|
||||
}
|
||||
let em_over = -ascent * font.size() / band;
|
||||
em_over - font.size() / 2.0
|
||||
}
|
||||
|
||||
/// Centres the glyphs' actual ink; ascent/descent metrics are only the
|
||||
/// fallback for glyphs without visible ink.
|
||||
fn rotated_run_baseline_shift(font: &Font, glyphs: &[GlyphId], positions: &[SkPoint]) -> f32 {
|
||||
if let Some(ink) = glyph_run_ink_bounds(font, glyphs, positions) {
|
||||
rotated_baseline_shift(ink.top, ink.bottom)
|
||||
} else {
|
||||
let (_, metrics) = font.metrics();
|
||||
rotated_baseline_shift(metrics.ascent, metrics.descent)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::shapes::text_vertical::test_support::*;
|
||||
use crate::shapes::text_vertical::CellKind;
|
||||
|
||||
#[test]
|
||||
fn selected_language_reaches_horizontal_and_vertical_localized_glyphs() {
|
||||
use crate::shapes::{GrowType, TextAlign};
|
||||
use skia_safe::textlayout::{FontCollection, ParagraphBuilder};
|
||||
|
||||
let font_provider = provider(include_bytes!("../../fonts/cjk-locl-test.ttf"));
|
||||
let mut fonts = FontCollection::new();
|
||||
fonts.set_asset_font_manager(Some(font_provider.clone().into()));
|
||||
for (language, glyph, width) in [("ja", 2, 12.0), ("zh-Hans", 3, 14.0), ("ko", 4, 16.0)] {
|
||||
let mut paragraph = vertical_paragraph(vec![make_span("中")], TextAlign::Left, 1.0);
|
||||
paragraph.set_language(language.to_string());
|
||||
let content = content_of(vec![paragraph.clone()], 100.0, GrowType::Fixed);
|
||||
let layout = layout_with(&font_provider, &content);
|
||||
assert_eq!(layout.runs[0].glyphs, vec![glyph], "vertical {language}");
|
||||
|
||||
let span = ¶graph.children()[0];
|
||||
let style = span.to_style(&content.bounds(), &FallbackFonts::default(), false, 1.0);
|
||||
assert_eq!(style.locale(), language);
|
||||
let mut builder = ParagraphBuilder::new(¶graph.paragraph_to_style(), &fonts);
|
||||
builder.push_style(&style);
|
||||
builder.add_text(&span.text);
|
||||
let mut horizontal = builder.build();
|
||||
horizontal.layout(100.0);
|
||||
assert!(
|
||||
(horizontal.longest_line() - width).abs() < 0.01,
|
||||
"horizontal {language}: {}",
|
||||
horizontal.longest_line()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn span_families_try_emoji_after_the_span_font() {
|
||||
let families = span_font_families(
|
||||
&make_span("😆"),
|
||||
&FallbackFonts::from_families(["fallback".to_string()]),
|
||||
);
|
||||
assert_eq!(families[1], DEFAULT_EMOJI_FONT);
|
||||
assert_eq!(families[2], "fallback");
|
||||
}
|
||||
|
||||
fn rotated_shift(text: &str) -> f32 {
|
||||
let layout = layout_content(&make_content(&[text], 1000.0), 1000.0);
|
||||
let CellKind::Rotated { run } = layout.cells[0].kind else {
|
||||
panic!("expected a rotated run");
|
||||
};
|
||||
layout.runs[run].rotated_baseline_shift
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rotated_runs_share_a_baseline_whatever_their_ink() {
|
||||
// Ascenders and descenders must not move the baseline across the
|
||||
// column.
|
||||
let plain = rotated_shift("aaa");
|
||||
for text in ["aaaf", "aaag", "aaagf", "AAA"] {
|
||||
assert!(
|
||||
(rotated_shift(text) - plain).abs() < 0.01,
|
||||
"{text} shifted from {plain} to {}",
|
||||
rotated_shift(text)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rotated_runs_centre_on_the_upright_em_box() {
|
||||
// Noto Sans JP's ascent overshoots the em; sideways runs must centre
|
||||
// on the same em box as upright glyphs, or full-width marks such as
|
||||
// `…` sit off the column axis.
|
||||
let typeface = FontMgr::new()
|
||||
.new_from_data(skia_safe::Data::new_copy(VMTX_TEST_FONT), None)
|
||||
.unwrap();
|
||||
let font = Font::new(typeface, EM);
|
||||
let (ascent, descent) = upright_centre_metrics(&font);
|
||||
let em_box_middle = -(ascent + descent) / 2.0;
|
||||
let shift = central_baseline_shift(&font);
|
||||
assert!(
|
||||
(shift - em_box_middle).abs() < 0.01,
|
||||
"sideways baseline shift {shift}, upright em box middle {em_box_middle}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rotated_lowercase_sits_on_the_column_axis() {
|
||||
// Source Sans: the em-box middle falls within 2% of an em of the
|
||||
// x-height middle, so lowercase looks centred in the column.
|
||||
let layout = layout_content(&make_content(&["a"], 1000.0), 1000.0);
|
||||
let CellKind::Rotated { run } = layout.cells[0].kind else {
|
||||
panic!("expected a rotated run");
|
||||
};
|
||||
let run = &layout.runs[run];
|
||||
let ink = run.ink_bounds().expect("visible ink");
|
||||
let ink_middle = (ink.top + ink.bottom) / 2.0 + run.rotated_baseline_shift;
|
||||
assert!(ink_middle.abs() < 0.02 * EM, "ink middle at {ink_middle}");
|
||||
}
|
||||
}
|
||||
53
render-wasm/src/shapes/text_warichu.rs
Normal file
53
render-wasm/src/shapes/text_warichu.rs
Normal file
@ -0,0 +1,53 @@
|
||||
//! Warichu (割注): inline notes set in two half-size sub-lines.
|
||||
|
||||
use crate::shapes::jlreq::{forbidden_at_line_end, forbidden_at_line_start};
|
||||
|
||||
pub const WARICHU_FONT_SCALE: f32 = 0.5;
|
||||
|
||||
/// Char index where a warichu run splits into its two sub-lines: the
|
||||
/// midpoint (first line longer), moved to the nearest split that keeps
|
||||
/// kinsoku. At equal distance a forward move wins, pulling the mark up into
|
||||
/// the first sub-line (jlreq). Falls back to the midpoint.
|
||||
pub(crate) fn warichu_split_chars(text: &str) -> usize {
|
||||
let chars: Vec<char> = text.chars().collect();
|
||||
let n = chars.len();
|
||||
let mid = n.div_ceil(2);
|
||||
let valid = |split: usize| {
|
||||
split >= 1
|
||||
&& split < n
|
||||
&& !forbidden_at_line_start(chars[split])
|
||||
&& !forbidden_at_line_end(chars[split - 1])
|
||||
};
|
||||
if valid(mid) {
|
||||
return mid;
|
||||
}
|
||||
for distance in 1..n {
|
||||
if valid(mid + distance) {
|
||||
return mid + distance;
|
||||
}
|
||||
if mid > distance && valid(mid - distance) {
|
||||
return mid - distance;
|
||||
}
|
||||
}
|
||||
mid
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn warichu_split_balances_and_respects_kinsoku() {
|
||||
// Balanced midpoint when nothing forbids it, first line longer.
|
||||
assert_eq!(warichu_split_chars("あいうえおか"), 3);
|
||||
assert_eq!(warichu_split_chars("あいうえお"), 3);
|
||||
// A comma at the midpoint may end the first sub-line...
|
||||
assert_eq!(warichu_split_chars("あい、うえ"), 3);
|
||||
// ...but must not start the second one: the split moves forward.
|
||||
assert_eq!(warichu_split_chars("あいう、えお"), 4);
|
||||
// An opening bracket must not end the first sub-line.
|
||||
assert_eq!(warichu_split_chars("あい「うえお"), 4);
|
||||
// Pathological all-forbidden text keeps the midpoint.
|
||||
assert_eq!(warichu_split_chars("、、、、"), 2);
|
||||
}
|
||||
}
|
||||
196
render-wasm/src/shapes/text_zhuyin.rs
Normal file
196
render-wasm/src/shapes/text_zhuyin.rs
Normal file
@ -0,0 +1,196 @@
|
||||
// Mandarin Zhuyin annotations: one Han base and one syllable per span.
|
||||
|
||||
use std::ops::Range;
|
||||
|
||||
use skia_safe::{self as skia, textlayout::TypefaceFontProvider, FontMgr};
|
||||
|
||||
use super::cjk_chars::is_zhuyin_base;
|
||||
use super::text_shaping::{shape_segment_with_fallbacks, span_font_families, ShapedRun};
|
||||
use super::TextSpan;
|
||||
use crate::render::FallbackFonts;
|
||||
|
||||
pub(crate) const FONT_SCALE: f32 = 0.3;
|
||||
pub(crate) const STRIP_WIDTH: f32 = 0.6;
|
||||
|
||||
pub(crate) struct Slot {
|
||||
pub ch: char,
|
||||
pub range: Range<usize>,
|
||||
/// Centre of the glyph's ink, in base em, relative to the strip top-left.
|
||||
pub x: f32,
|
||||
pub y: f32,
|
||||
}
|
||||
|
||||
pub(crate) struct Syllable {
|
||||
chars: Vec<char>,
|
||||
symbols: Vec<usize>,
|
||||
tone: Option<usize>,
|
||||
}
|
||||
|
||||
impl Syllable {
|
||||
pub fn parse(reading: &str) -> Option<Self> {
|
||||
let chars: Vec<char> = reading.chars().take(5).collect();
|
||||
if chars.len() > 4 {
|
||||
return None;
|
||||
}
|
||||
let mut symbols = Vec::new();
|
||||
let mut tone = None;
|
||||
for (index, ch) in chars.iter().enumerate() {
|
||||
match ch {
|
||||
'\u{3105}'..='\u{3129}' => symbols.push(index),
|
||||
'ˉ' | 'ˊ' | 'ˇ' | 'ˋ' | '˙' if tone.is_none() => tone = Some(index),
|
||||
_ => return None,
|
||||
}
|
||||
}
|
||||
if !(1..=3).contains(&symbols.len())
|
||||
|| tone.is_some_and(|i| i + 1 != chars.len() && !(i == 0 && chars[i] == '˙'))
|
||||
{
|
||||
return None;
|
||||
}
|
||||
Some(Self {
|
||||
chars,
|
||||
symbols,
|
||||
tone,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn slots(&self) -> Vec<Slot> {
|
||||
let neutral = self.tone.is_some_and(|i| self.chars[i] == '˙');
|
||||
let lead = if neutral { 1.0 / 15.0 } else { 0.0 };
|
||||
let top = (1.0 - self.symbols.len() as f32 * FONT_SCALE - lead) / 2.0;
|
||||
self.chars
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, ch)| {
|
||||
let (x, y) = if let Some(symbol) = self.symbols.iter().position(|s| *s == i) {
|
||||
(
|
||||
FONT_SCALE / 2.0,
|
||||
top + lead + (symbol as f32 + 0.5) * FONT_SCALE,
|
||||
)
|
||||
} else if neutral {
|
||||
(FONT_SCALE / 2.0, top + lead / 2.0)
|
||||
} else {
|
||||
(0.45, top + (self.symbols.len() as f32 - 1.0) * FONT_SCALE)
|
||||
};
|
||||
Slot {
|
||||
ch: *ch,
|
||||
range: i..i + 1,
|
||||
x,
|
||||
y,
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// A Han grapheme, including an optional variation selector. Do not attach
|
||||
/// a syllable to a group, Latin text, or an incomplete surrogate sequence.
|
||||
pub(crate) fn single_han(text: &str) -> bool {
|
||||
let mut chars = text.chars();
|
||||
let Some(first) = chars.next() else {
|
||||
return false;
|
||||
};
|
||||
is_zhuyin_base(first)
|
||||
&& chars.next().is_none_or(|ch| {
|
||||
matches!(ch as u32, 0xfe00..=0xfe0f | 0xe0100..=0xe01ef) && chars.next().is_none()
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn syllable(span: &TextSpan) -> Option<Syllable> {
|
||||
(span.ruby_zhuyin && span.has_ruby() && single_han(&span.text))
|
||||
.then(|| Syllable::parse(span.ruby_text()))
|
||||
.flatten()
|
||||
}
|
||||
|
||||
pub(crate) struct Glyph {
|
||||
pub run: ShapedRun,
|
||||
pub glyph: usize,
|
||||
pub range: Range<usize>,
|
||||
/// Drawing baseline relative to the base em's top-right corner.
|
||||
pub origin: skia::Point,
|
||||
}
|
||||
|
||||
/// Shape each symbol upright, retaining each fallback font and source range.
|
||||
/// Ink-centred placement also works for fonts with narrow spacing tone marks.
|
||||
pub(crate) fn shape(
|
||||
span: &TextSpan,
|
||||
syllable: &Syllable,
|
||||
provider: &TypefaceFontProvider,
|
||||
fallbacks: &FallbackFonts,
|
||||
) -> Vec<Glyph> {
|
||||
let manager = FontMgr::from(provider.clone());
|
||||
let leading = span.ruby.encode_utf16().count() - span.ruby.trim_start().encode_utf16().count();
|
||||
syllable
|
||||
.slots()
|
||||
.into_iter()
|
||||
.flat_map(|slot| {
|
||||
shape_segment_with_fallbacks(
|
||||
&slot.ch.to_string(),
|
||||
span.font_size * FONT_SCALE,
|
||||
&span_font_families(span, fallbacks),
|
||||
provider,
|
||||
false,
|
||||
span.font_features,
|
||||
&manager,
|
||||
&span.language,
|
||||
)
|
||||
.into_iter()
|
||||
.flat_map(move |run| {
|
||||
(0..run.glyphs.len()).map(move |glyph| {
|
||||
let mut bounds = [skia::Rect::default()];
|
||||
run.font.get_bounds(&[run.glyphs[glyph]], &mut bounds, None);
|
||||
Glyph {
|
||||
origin: skia::Point::new(
|
||||
slot.x * span.font_size - bounds[0].center_x(),
|
||||
slot.y * span.font_size - bounds[0].center_y(),
|
||||
),
|
||||
run: run.clone(),
|
||||
glyph,
|
||||
range: leading + slot.range.start..leading + slot.range.end,
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn zhuyin_keeps_neutral_tone_above_and_other_tones_beside_the_last_symbol() {
|
||||
for reading in ["ㄓ", "ㄋㄧˇ", "ㄓㄨㄤˋ", "˙ㄉㄜ", "ㄉㄜ˙", "ㄏㄠˊ", "ㄇㄚˉ"]
|
||||
{
|
||||
let syllable = Syllable::parse(reading).expect(reading);
|
||||
let slots = syllable.slots();
|
||||
assert_eq!(slots.len(), reading.chars().count());
|
||||
let symbols: Vec<_> = slots.iter().filter(|s| s.ch >= '\u{3105}').collect();
|
||||
assert!(symbols.windows(2).all(|s| s[0].y < s[1].y));
|
||||
for tone in slots.iter().filter(|s| s.ch < '\u{3105}') {
|
||||
if tone.ch == '˙' {
|
||||
assert!(tone.y < symbols[0].y);
|
||||
assert_eq!(tone.x, symbols[0].x);
|
||||
} else {
|
||||
assert!(tone.x > symbols.last().unwrap().x);
|
||||
assert!(tone.y < symbols.last().unwrap().y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zhuyin_rejects_malformed_or_multiple_syllables_without_guessing() {
|
||||
for reading in [
|
||||
"",
|
||||
"nǐ",
|
||||
"ˇ",
|
||||
"ㄋˇㄧ",
|
||||
"ㄋㄧˇˋ",
|
||||
"ㄋ ㄧ",
|
||||
"ㄅㄆㄇㄈ",
|
||||
"ㄋ\nㄧ",
|
||||
] {
|
||||
assert!(Syllable::parse(reading).is_none(), "{reading:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -270,6 +270,17 @@ impl ShapesPoolImpl {
|
||||
})
|
||||
}
|
||||
|
||||
/// Drop both the base bounds and any transformed snapshot after a direct edit.
|
||||
pub fn invalidate_shape_extrect(&mut self, id: &Uuid) {
|
||||
let Some(idx) = self.uuid_to_idx.get(id).copied() else {
|
||||
return;
|
||||
};
|
||||
self.shapes[idx].invalidate_extrect();
|
||||
if let Some(cell) = self.modified_shape_cache.get_mut(&idx) {
|
||||
*cell = OnceCell::new();
|
||||
}
|
||||
}
|
||||
|
||||
/// Drops the extrect cache of every ancestor whose extrect is affected by this shape
|
||||
/// stopping at the first ancestor that clips.
|
||||
pub fn invalidate_ancestors_extrect(&mut self, id: &Uuid) {
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
use crate::get_resources;
|
||||
use crate::render::FallbackFonts;
|
||||
use crate::skia::textlayout::FontCollection;
|
||||
use crate::skia::Image;
|
||||
use crate::uuid::Uuid;
|
||||
use crate::with_state;
|
||||
use std::collections::HashSet;
|
||||
|
||||
pub fn uuid_from_u32_quartet(a: u32, b: u32, c: u32, d: u32) -> Uuid {
|
||||
let hi: u64 = ((a as u64) << 32) | b as u64;
|
||||
@ -29,7 +29,7 @@ pub fn get_image(image_id: &Uuid) -> Option<&Image> {
|
||||
}
|
||||
|
||||
// FIXME: move to a different place ?
|
||||
pub fn get_fallback_fonts() -> &'static HashSet<String> {
|
||||
pub fn get_fallback_fonts() -> &'static FallbackFonts {
|
||||
get_resources().fonts.get_fallback()
|
||||
}
|
||||
|
||||
|
||||
@ -124,3 +124,11 @@ pub extern "C" fn is_font_uploaded(
|
||||
let family = FontFamily::new(id, weight, font_style.into());
|
||||
get_resources().fonts.has_family(&family, is_emoji)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn mark_font_as_fallback(a: u32, b: u32, c: u32, d: u32, weight: u32, style: u8) {
|
||||
let id = uuid_from_u32_quartet(a, b, c, d);
|
||||
let font_style = RawFontStyle::from(style);
|
||||
let family = FontFamily::new(id, weight, font_style.into());
|
||||
get_resources().fonts.mark_as_fallback(&family);
|
||||
}
|
||||
|
||||
@ -13,6 +13,9 @@ use crate::{with_current_shape, with_current_shape_mut, with_state};
|
||||
use crate::error::Error;
|
||||
|
||||
pub mod helpers;
|
||||
mod layout_attrs;
|
||||
|
||||
pub use layout_attrs::*;
|
||||
|
||||
const RAW_SPAN_DATA_SIZE: usize = std::mem::size_of::<RawTextSpan>();
|
||||
const RAW_PARAGRAPH_DATA_SIZE: usize = std::mem::size_of::<RawParagraphData>();
|
||||
@ -26,6 +29,7 @@ pub enum RawTextAlign {
|
||||
Center = 1,
|
||||
Right = 2,
|
||||
Justify = 3,
|
||||
Distribute = 4,
|
||||
}
|
||||
|
||||
impl From<RawTextAlign> for TextAlign {
|
||||
@ -35,6 +39,7 @@ impl From<RawTextAlign> for TextAlign {
|
||||
RawTextAlign::Center => TextAlign::Center,
|
||||
RawTextAlign::Right => TextAlign::Right,
|
||||
RawTextAlign::Justify => TextAlign::Justify,
|
||||
RawTextAlign::Distribute => TextAlign::Distribute,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -132,7 +137,10 @@ pub struct RawParagraphData {
|
||||
line_height: f32,
|
||||
letter_spacing: f32,
|
||||
list_style_type: RawListStyleType,
|
||||
_padding: [u8; 3],
|
||||
writing_mode: u8,
|
||||
text_orientation: u8,
|
||||
korean_wrap: u8,
|
||||
language_length: u32,
|
||||
}
|
||||
|
||||
impl From<[u8; RAW_PARAGRAPH_DATA_SIZE]> for RawParagraphData {
|
||||
@ -159,6 +167,19 @@ pub struct RawTextSpan {
|
||||
text_decoration: RawTextDecoration,
|
||||
text_transform: RawTextTransform,
|
||||
text_direction: RawTextDirection,
|
||||
text_orientation: u8,
|
||||
text_combine_upright: u8,
|
||||
text_emphasis: u8,
|
||||
warichu: u8,
|
||||
font_features: u8,
|
||||
annotation_clearance: u8,
|
||||
ruby_size: u8,
|
||||
ruby_align: u8,
|
||||
ruby_overhang: u8,
|
||||
/// Bit 0: over/under side of normal ruby. Bit 1: Zhuyin.
|
||||
ruby_flags: u8,
|
||||
text_emphasis_position: u8,
|
||||
text_underline_position: u8,
|
||||
font_size: f32,
|
||||
line_height: f32,
|
||||
letter_spacing: f32,
|
||||
@ -167,6 +188,7 @@ pub struct RawTextSpan {
|
||||
font_family: [u8; 4],
|
||||
font_variant_id: [u32; 4], // TODO: maybe add RawUUID type
|
||||
text_length: u32,
|
||||
ruby_length: u32,
|
||||
fill_count: u32,
|
||||
fills: [RawFillData; MAX_TEXT_FILLS],
|
||||
}
|
||||
@ -205,7 +227,7 @@ impl From<RawTextSpan> for shapes::TextSpan {
|
||||
.map(|fill| fill.into())
|
||||
.collect();
|
||||
|
||||
Self::new(
|
||||
let mut span = Self::new(
|
||||
text,
|
||||
font_family,
|
||||
value.font_size,
|
||||
@ -217,7 +239,32 @@ impl From<RawTextSpan> for shapes::TextSpan {
|
||||
value.font_weight,
|
||||
uuid_from_u32(value.font_variant_id),
|
||||
fills,
|
||||
)
|
||||
);
|
||||
span.text_orientation = RawTextOrientation::from(value.text_orientation).into();
|
||||
span.text_combine_upright = RawTextCombineUpright::from(value.text_combine_upright).into();
|
||||
span.text_emphasis = RawTextEmphasis::from(value.text_emphasis).into();
|
||||
span.text_emphasis_position =
|
||||
RawTextEmphasisPosition::from(value.text_emphasis_position).into();
|
||||
span.text_underline_position =
|
||||
RawTextUnderlinePosition::from(value.text_underline_position).into();
|
||||
span.warichu = RawWarichu::from(value.warichu).into();
|
||||
span.font_features = RawFontFeatures::from(value.font_features).into();
|
||||
span.annotation_clearance = RawAnnotationClearance::from(value.annotation_clearance).into();
|
||||
span.ruby_size = RawRubySize::from(value.ruby_size).into();
|
||||
span.ruby_align = RawRubyAlign::from(value.ruby_align).into();
|
||||
span.ruby_overhang = RawRubyOverhang::from(value.ruby_overhang).into();
|
||||
// Bit 2 joins the span to the reading of the span before it, bit 1
|
||||
// selects Zhuyin and bit 0 retains the existing over/under side.
|
||||
// Reject unknown flags instead of treating corrupt bytes as Zhuyin.
|
||||
let ruby_flags = if value.ruby_flags <= 7 {
|
||||
value.ruby_flags
|
||||
} else {
|
||||
0
|
||||
};
|
||||
span.ruby_side = RawRubySide::from(ruby_flags & 1).into();
|
||||
span.ruby_zhuyin = ruby_flags & 2 != 0;
|
||||
span.ruby_joins_previous = ruby_flags & 4 != 0;
|
||||
span
|
||||
}
|
||||
}
|
||||
|
||||
@ -258,21 +305,31 @@ impl From<RawParagraph> for shapes::Paragraph {
|
||||
fn from(value: RawParagraph) -> Self {
|
||||
let mut spans = vec![];
|
||||
|
||||
// Layout: [<all span texts> <all span ruby texts>]. Annotation blobs
|
||||
// begin after all base text.
|
||||
let mut offset = 0;
|
||||
let mut ruby_offset: usize = value.spans.iter().map(|s| s.text_length as usize).sum();
|
||||
for raw_span in value.spans.into_iter() {
|
||||
let delta = raw_span.text_length as usize;
|
||||
let text_buffer = &value.text_buffer[offset..offset + delta];
|
||||
|
||||
let text_buffer = value.text_buffer.get(offset..offset + delta).unwrap_or(&[]);
|
||||
let ruby_delta = raw_span.ruby_length as usize;
|
||||
let ruby_buffer = value
|
||||
.text_buffer
|
||||
.get(ruby_offset..ruby_offset + ruby_delta)
|
||||
.unwrap_or(&[]);
|
||||
let mut span = shapes::TextSpan::from(raw_span);
|
||||
if !text_buffer.is_empty() {
|
||||
span.set_text(String::from_utf8_lossy(text_buffer).to_string());
|
||||
}
|
||||
|
||||
if !ruby_buffer.is_empty() {
|
||||
span.ruby = String::from_utf8_lossy(ruby_buffer).to_string();
|
||||
}
|
||||
spans.push(span);
|
||||
offset += delta;
|
||||
ruby_offset += ruby_delta;
|
||||
}
|
||||
|
||||
shapes::Paragraph::new(
|
||||
let mut paragraph = shapes::Paragraph::new(
|
||||
value.attrs.text_align.into(),
|
||||
value.attrs.text_direction.into(),
|
||||
value.attrs.text_decoration.into(),
|
||||
@ -281,7 +338,17 @@ impl From<RawParagraph> for shapes::Paragraph {
|
||||
value.attrs.letter_spacing,
|
||||
spans,
|
||||
)
|
||||
.with_list_style_type(value.attrs.list_style_type.into())
|
||||
.with_list_style_type(value.attrs.list_style_type.into());
|
||||
paragraph.set_writing_mode(RawWritingMode::from(value.attrs.writing_mode).into());
|
||||
paragraph
|
||||
.set_text_orientation(RawTextOrientation::from(value.attrs.text_orientation).into());
|
||||
let language = ruby_offset
|
||||
.checked_add(value.attrs.language_length as usize)
|
||||
.and_then(|end| value.text_buffer.get(ruby_offset..end))
|
||||
.unwrap_or(&[]);
|
||||
paragraph.set_language(String::from_utf8_lossy(language).into_owned());
|
||||
paragraph.set_korean_wrap(RawKoreanWrap::from(value.attrs.korean_wrap).into());
|
||||
paragraph
|
||||
}
|
||||
}
|
||||
|
||||
@ -349,6 +416,15 @@ pub extern "C" fn set_shape_grow_type(grow_type: u8) {
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn set_shape_line_adjustment(line_adjustment: u8) {
|
||||
with_current_shape_mut!(state, |shape: &mut Shape| {
|
||||
if let Type::Text(text_content) = &mut shape.shape_type {
|
||||
text_content.set_line_adjustment(RawLineAdjustment::from(line_adjustment).into());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn get_text_dimensions() -> *mut u8 {
|
||||
let mut ptr = std::ptr::null_mut();
|
||||
@ -474,3 +550,208 @@ pub extern "C" fn calculate_position_data() -> *mut u8 {
|
||||
});
|
||||
mem::write_vec(result)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::shapes::{
|
||||
AnnotationClearance, AnnotationSide, FontFeatures, RubyAlign, RubyOverhang, RubySize,
|
||||
};
|
||||
|
||||
/// The shared CLJS writer writes PARAGRAPH-ATTR-U8-SIZE (20)
|
||||
/// attr bytes after the u32 span count, and SPAN-ATTR-U8-SIZE (80)
|
||||
/// attr bytes before the fills block. These sizes must match the struct
|
||||
/// layouts.
|
||||
#[test]
|
||||
fn zhuyin_flags_keep_legacy_side_and_header_size() {
|
||||
for (flags, zhuyin, side, joins) in [
|
||||
(0, false, AnnotationSide::Over, false),
|
||||
(1, false, AnnotationSide::Under, false),
|
||||
(2, true, AnnotationSide::Over, false),
|
||||
(3, true, AnnotationSide::Under, false),
|
||||
(4, false, AnnotationSide::Over, true),
|
||||
(5, false, AnnotationSide::Under, true),
|
||||
(255, false, AnnotationSide::Over, false),
|
||||
] {
|
||||
let mut bytes = [0u8; std::mem::size_of::<RawTextSpan>()];
|
||||
bytes[13] = flags;
|
||||
let span: shapes::TextSpan = RawTextSpan::from(bytes).into();
|
||||
assert_eq!(span.ruby_zhuyin, zhuyin);
|
||||
assert_eq!(span.ruby_side, side);
|
||||
assert_eq!(span.ruby_joins_previous, joins);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raw_struct_sizes_match_cljs_writer() {
|
||||
const PARAGRAPH_ATTR_U8_SIZE: usize = 20;
|
||||
const SPAN_ATTR_U8_SIZE: usize = 80;
|
||||
assert_eq!(RAW_PARAGRAPH_DATA_SIZE, 4 + PARAGRAPH_ATTR_U8_SIZE);
|
||||
assert_eq!(
|
||||
RAW_SPAN_DATA_SIZE,
|
||||
SPAN_ATTR_U8_SIZE + MAX_TEXT_FILLS * std::mem::size_of::<RawFillData>()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn word_wrap_byte_is_distinct_from_the_experiment_and_unknown_defaults() {
|
||||
use crate::shapes::text_policy::KoreanWrap;
|
||||
for (byte, expected) in [
|
||||
(0, KoreanWrap::Syllable),
|
||||
(1, KoreanWrap::SpacesOnly),
|
||||
(2, KoreanWrap::KeepWords),
|
||||
(255, KoreanWrap::Syllable),
|
||||
] {
|
||||
let mut attrs = [0u8; RAW_PARAGRAPH_DATA_SIZE];
|
||||
attrs[19] = byte;
|
||||
let paragraph = shapes::Paragraph::from(RawParagraph {
|
||||
attrs: RawParagraphData::from(attrs),
|
||||
spans: vec![],
|
||||
text_buffer: vec![],
|
||||
});
|
||||
assert_eq!(paragraph.korean_wrap(), expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn language_decodes_after_base_and_ruby_text() {
|
||||
let mut attrs = [0u8; RAW_PARAGRAPH_DATA_SIZE];
|
||||
attrs[19] = 1; // Korean spaces-only POC; byte 15 after span count.
|
||||
attrs[20..24].copy_from_slice(&2u32.to_le_bytes());
|
||||
let mut span = [0u8; RAW_SPAN_DATA_SIZE];
|
||||
span[68..72].copy_from_slice(&3u32.to_le_bytes());
|
||||
span[72..76].copy_from_slice(&3u32.to_le_bytes());
|
||||
let paragraph = shapes::Paragraph::from(RawParagraph {
|
||||
attrs: RawParagraphData::from(attrs),
|
||||
spans: vec![RawTextSpan::from(span)],
|
||||
text_buffer: "韓한ko".as_bytes().to_vec(),
|
||||
});
|
||||
assert_eq!(paragraph.language(), "ko");
|
||||
assert_eq!(
|
||||
paragraph.korean_wrap(),
|
||||
crate::shapes::text_policy::KoreanWrap::SpacesOnly
|
||||
);
|
||||
assert_eq!(paragraph.children()[0].text, "韓");
|
||||
assert_eq!(paragraph.children()[0].ruby, "한");
|
||||
assert_eq!(paragraph.children()[0].language, "ko");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raw_text_combine_upright_counts_deserialize() {
|
||||
// Byte 5 of the span attr block; the counted digits variants map
|
||||
// onto their max run length.
|
||||
let mut bytes = [0u8; RAW_SPAN_DATA_SIZE];
|
||||
bytes[5] = RawTextCombineUpright::Digits2 as u8;
|
||||
let span = shapes::TextSpan::from(RawTextSpan::from(bytes));
|
||||
assert_eq!(span.text_combine_upright.digits_max(), Some(2));
|
||||
|
||||
bytes[5] = RawTextCombineUpright::Digits3 as u8;
|
||||
let span = shapes::TextSpan::from(RawTextSpan::from(bytes));
|
||||
assert_eq!(span.text_combine_upright.digits_max(), Some(3));
|
||||
|
||||
bytes[5] = RawTextCombineUpright::Digits as u8;
|
||||
let span = shapes::TextSpan::from(RawTextSpan::from(bytes));
|
||||
assert_eq!(span.text_combine_upright.digits_max(), Some(4));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn annotation_positions_decode_from_former_padding_and_unknown_defaults() {
|
||||
let mut bytes = [0u8; RAW_SPAN_DATA_SIZE];
|
||||
bytes[14] = 4;
|
||||
bytes[15] = 5;
|
||||
let span = shapes::TextSpan::from(RawTextSpan::from(bytes));
|
||||
assert_eq!(
|
||||
span.text_emphasis_position,
|
||||
shapes::TextEmphasisPosition::UnderLeft
|
||||
);
|
||||
assert_eq!(
|
||||
span.text_underline_position,
|
||||
shapes::TextUnderlinePosition::UnderRight
|
||||
);
|
||||
bytes[14] = 255;
|
||||
bytes[15] = 255;
|
||||
let span = shapes::TextSpan::from(RawTextSpan::from(bytes));
|
||||
assert_eq!(
|
||||
span.text_emphasis_position,
|
||||
shapes::TextEmphasisPosition::Auto
|
||||
);
|
||||
assert_eq!(
|
||||
span.text_underline_position,
|
||||
shapes::TextUnderlinePosition::Auto
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raw_font_features_deserializes_from_reserved_span_byte() {
|
||||
let mut bytes = [0u8; RAW_SPAN_DATA_SIZE];
|
||||
bytes[8] = RawFontFeatures::Vpal as u8;
|
||||
|
||||
let raw = RawTextSpan::from(bytes);
|
||||
let span = shapes::TextSpan::from(raw);
|
||||
|
||||
assert_eq!(span.font_features, FontFeatures::Vpal);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raw_annotation_clearance_deserializes_from_reserved_span_byte() {
|
||||
let mut bytes = [0u8; RAW_SPAN_DATA_SIZE];
|
||||
bytes[9] = RawAnnotationClearance::Auto as u8;
|
||||
|
||||
let span = shapes::TextSpan::from(RawTextSpan::from(bytes));
|
||||
|
||||
assert_eq!(span.annotation_clearance, AnnotationClearance::Auto);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raw_ruby_customization_deserializes_from_span_bytes() {
|
||||
let mut bytes = [0u8; RAW_SPAN_DATA_SIZE];
|
||||
bytes[10] = RawRubySize::Quarter as u8;
|
||||
bytes[11] = RawRubyAlign::SpaceBetween as u8;
|
||||
bytes[12] = RawRubyOverhang::None as u8;
|
||||
bytes[13] = RawRubySide::Under as u8;
|
||||
|
||||
let span = shapes::TextSpan::from(RawTextSpan::from(bytes));
|
||||
|
||||
assert_eq!(span.ruby_size, RubySize::Quarter);
|
||||
assert_eq!(span.ruby_align, RubyAlign::SpaceBetween);
|
||||
assert_eq!(span.ruby_overhang, RubyOverhang::None);
|
||||
assert_eq!(span.ruby_side, AnnotationSide::Under);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn out_of_range_japanese_span_bytes_fall_back_to_defaults() {
|
||||
let mut bytes = [0u8; RAW_SPAN_DATA_SIZE];
|
||||
bytes[4..14].fill(0xFF);
|
||||
|
||||
let span = shapes::TextSpan::from(RawTextSpan::from(bytes));
|
||||
|
||||
assert_eq!(span.text_orientation, shapes::TextOrientation::Mixed);
|
||||
assert_eq!(span.text_combine_upright, shapes::TextCombineUpright::None);
|
||||
assert_eq!(span.text_emphasis, shapes::TextEmphasis::None);
|
||||
assert!(!span.warichu);
|
||||
assert_eq!(span.font_features, FontFeatures::None);
|
||||
assert_eq!(span.annotation_clearance, AnnotationClearance::None);
|
||||
assert_eq!(span.ruby_size, RubySize::Half);
|
||||
assert_eq!(span.ruby_align, RubyAlign::SpaceAround);
|
||||
assert_eq!(span.ruby_overhang, RubyOverhang::Auto);
|
||||
assert_eq!(span.ruby_side, AnnotationSide::Over);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn out_of_range_paragraph_writing_bytes_fall_back_to_defaults() {
|
||||
let mut bytes = [0u8; RAW_PARAGRAPH_DATA_SIZE];
|
||||
bytes[17] = 0xFF;
|
||||
bytes[18] = 0xFF;
|
||||
|
||||
let attrs = RawParagraphData::from(bytes);
|
||||
|
||||
assert_eq!(
|
||||
shapes::WritingMode::from(RawWritingMode::from(attrs.writing_mode)),
|
||||
shapes::WritingMode::HorizontalTb
|
||||
);
|
||||
assert_eq!(
|
||||
shapes::TextOrientation::from(RawTextOrientation::from(attrs.text_orientation)),
|
||||
shapes::TextOrientation::Mixed
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -472,9 +472,7 @@ pub fn delete_selection_range(text_content: &mut TextContent, selection: &TextSe
|
||||
delete_range_in_paragraph(&mut paragraphs[end_paragraph], 0, end.offset);
|
||||
|
||||
let end_para_children: Vec<_> = std::mem::take(paragraphs[end_paragraph].children_mut());
|
||||
paragraphs[start.paragraph]
|
||||
.children_mut()
|
||||
.extend(end_para_children);
|
||||
paragraphs[start.paragraph].extend_children(end_para_children);
|
||||
|
||||
paragraphs.drain((start.paragraph + 1)..=end_paragraph);
|
||||
|
||||
@ -587,9 +585,7 @@ pub fn delete_char_before(
|
||||
let prev_para_len = paragraph_char_count(¶graphs[prev_para_idx]);
|
||||
|
||||
let current_children: Vec<_> = std::mem::take(paragraphs[cursor.paragraph].children_mut());
|
||||
paragraphs[prev_para_idx]
|
||||
.children_mut()
|
||||
.extend(current_children);
|
||||
paragraphs[prev_para_idx].extend_children(current_children);
|
||||
|
||||
paragraphs.remove(cursor.paragraph);
|
||||
|
||||
@ -761,9 +757,7 @@ pub fn delete_char_after(text_content: &mut TextContent, cursor: &TextPositionWi
|
||||
} else if cursor.paragraph < paragraphs.len() - 1 {
|
||||
let next_para_idx = cursor.paragraph + 1;
|
||||
let next_children: Vec<_> = std::mem::take(paragraphs[next_para_idx].children_mut());
|
||||
paragraphs[cursor.paragraph]
|
||||
.children_mut()
|
||||
.extend(next_children);
|
||||
paragraphs[cursor.paragraph].extend_children(next_children);
|
||||
|
||||
paragraphs.remove(next_para_idx);
|
||||
}
|
||||
@ -813,6 +807,11 @@ pub fn split_paragraph_at_cursor(
|
||||
let text_direction = para.text_direction();
|
||||
let text_decoration = para.text_decoration();
|
||||
let text_transform = para.text_transform();
|
||||
let writing_mode = para.writing_mode();
|
||||
let text_orientation = para.text_orientation();
|
||||
let language = para.language().to_owned();
|
||||
let korean_wrap = para.korean_wrap();
|
||||
let source_position = para.source_position();
|
||||
let line_height = para.line_height();
|
||||
let letter_spacing = para.letter_spacing();
|
||||
let list_style_type = para.list_style_type();
|
||||
@ -829,7 +828,7 @@ pub fn split_paragraph_at_cursor(
|
||||
span.set_text(new_text);
|
||||
}
|
||||
|
||||
let new_para = crate::shapes::Paragraph::new(
|
||||
let mut new_para = crate::shapes::Paragraph::new(
|
||||
text_align,
|
||||
text_direction,
|
||||
text_decoration,
|
||||
@ -839,6 +838,11 @@ pub fn split_paragraph_at_cursor(
|
||||
new_para_children,
|
||||
)
|
||||
.with_list_style_type(list_style_type);
|
||||
new_para.set_writing_mode(writing_mode);
|
||||
new_para.set_text_orientation(text_orientation);
|
||||
new_para.set_language(language);
|
||||
new_para.set_korean_wrap(korean_wrap);
|
||||
new_para.set_source_position(source_position);
|
||||
|
||||
paragraphs.insert(cursor.paragraph + 1, new_para);
|
||||
|
||||
@ -852,6 +856,56 @@ mod tests {
|
||||
use crate::shapes::{FontFamily, FontStyle, GrowType, ListStyleType, TextAlign, TextSpan};
|
||||
use crate::uuid::Uuid;
|
||||
|
||||
#[test]
|
||||
fn paragraph_split_keeps_language_and_korean_wrap() {
|
||||
for wrap in [
|
||||
crate::shapes::text_policy::KoreanWrap::SpacesOnly,
|
||||
crate::shapes::text_policy::KoreanWrap::KeepWords,
|
||||
] {
|
||||
let mut para = paragraph(&["한국어"]);
|
||||
para.set_language("ko".to_string());
|
||||
para.set_korean_wrap(wrap);
|
||||
let mut content = content(vec![para]);
|
||||
let cursor = TextPositionWithAffinity {
|
||||
paragraph: 0,
|
||||
offset: 1,
|
||||
..Default::default()
|
||||
};
|
||||
assert!(split_paragraph_at_cursor(&mut content, &cursor));
|
||||
assert_eq!(content.paragraphs().len(), 2);
|
||||
for para in content.paragraphs() {
|
||||
assert_eq!(para.language(), "ko");
|
||||
assert_eq!(para.korean_wrap(), wrap);
|
||||
assert_eq!(para.source_position(), 0);
|
||||
assert!(para.children().iter().all(|span| span.language == "ko"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paragraph_merge_inherits_the_surviving_paragraph_language() {
|
||||
let mut first = paragraph(&["中"]);
|
||||
first.set_language("ja".to_string());
|
||||
let mut second = paragraph(&["中"]);
|
||||
second.set_language("ko".to_string());
|
||||
let mut content = content(vec![first, second]);
|
||||
delete_char_after(
|
||||
&mut content,
|
||||
&TextPositionWithAffinity {
|
||||
paragraph: 0,
|
||||
offset: 1,
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
assert_eq!(content.paragraphs().len(), 1);
|
||||
let paragraph = &content.paragraphs()[0];
|
||||
assert_eq!(paragraph.language(), "ja");
|
||||
assert!(paragraph
|
||||
.children()
|
||||
.iter()
|
||||
.all(|span| span.language == "ja"));
|
||||
}
|
||||
|
||||
fn span(text: &str) -> TextSpan {
|
||||
TextSpan::new(
|
||||
text.to_string(),
|
||||
|
||||
365
render-wasm/src/wasm/text/layout_attrs.rs
Normal file
365
render-wasm/src/wasm/text/layout_attrs.rs
Normal file
@ -0,0 +1,365 @@
|
||||
use crate::shapes::text_policy::KoreanWrap;
|
||||
use crate::shapes::{
|
||||
self, AnnotationClearance, AnnotationSide, FontFeatures, LineAdjustment, RubyAlign,
|
||||
RubyOverhang, RubySize, TextCombineUpright, TextEmphasis,
|
||||
};
|
||||
use macros::ToJs;
|
||||
|
||||
/// Reads a style byte as `$raw`, falling back to `$default` when the byte
|
||||
/// names no variant, so a bad byte from the CLJS writer never becomes an
|
||||
/// invalid enum value.
|
||||
macro_rules! raw_enum_from_byte {
|
||||
($raw:ident, $default:ident, [$($variant:ident),+ $(,)?]) => {
|
||||
impl From<u8> for $raw {
|
||||
fn from(value: u8) -> Self {
|
||||
[$($raw::$variant),+]
|
||||
.into_iter()
|
||||
.find(|variant| *variant as u8 == value)
|
||||
.unwrap_or($raw::$default)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, ToJs)]
|
||||
#[repr(u8)]
|
||||
pub enum RawWritingMode {
|
||||
HorizontalTb = 0,
|
||||
VerticalRl = 1,
|
||||
}
|
||||
|
||||
raw_enum_from_byte!(RawWritingMode, HorizontalTb, [HorizontalTb, VerticalRl]);
|
||||
|
||||
impl From<RawWritingMode> for shapes::WritingMode {
|
||||
fn from(value: RawWritingMode) -> Self {
|
||||
match value {
|
||||
RawWritingMode::HorizontalTb => shapes::WritingMode::HorizontalTb,
|
||||
RawWritingMode::VerticalRl => shapes::WritingMode::VerticalRl,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, ToJs)]
|
||||
#[repr(u8)]
|
||||
pub enum RawTextOrientation {
|
||||
Mixed = 0,
|
||||
Upright = 1,
|
||||
}
|
||||
|
||||
raw_enum_from_byte!(RawTextOrientation, Mixed, [Mixed, Upright]);
|
||||
|
||||
impl From<RawTextOrientation> for shapes::TextOrientation {
|
||||
fn from(value: RawTextOrientation) -> Self {
|
||||
match value {
|
||||
RawTextOrientation::Mixed => shapes::TextOrientation::Mixed,
|
||||
RawTextOrientation::Upright => shapes::TextOrientation::Upright,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, ToJs)]
|
||||
#[repr(u8)]
|
||||
pub enum RawTextCombineUpright {
|
||||
None = 0,
|
||||
All = 1,
|
||||
Digits = 2,
|
||||
Digits2 = 3,
|
||||
Digits3 = 4,
|
||||
}
|
||||
|
||||
raw_enum_from_byte!(
|
||||
RawTextCombineUpright,
|
||||
None,
|
||||
[None, All, Digits, Digits2, Digits3]
|
||||
);
|
||||
|
||||
impl From<RawTextCombineUpright> for TextCombineUpright {
|
||||
fn from(value: RawTextCombineUpright) -> Self {
|
||||
match value {
|
||||
RawTextCombineUpright::None => TextCombineUpright::None,
|
||||
RawTextCombineUpright::All => TextCombineUpright::All,
|
||||
RawTextCombineUpright::Digits => TextCombineUpright::Digits,
|
||||
RawTextCombineUpright::Digits2 => TextCombineUpright::Digits2,
|
||||
RawTextCombineUpright::Digits3 => TextCombineUpright::Digits3,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, ToJs)]
|
||||
#[repr(u8)]
|
||||
pub enum RawTextEmphasis {
|
||||
None = 0,
|
||||
FilledDot = 1,
|
||||
OpenDot = 2,
|
||||
FilledCircle = 3,
|
||||
OpenCircle = 4,
|
||||
FilledSesame = 5,
|
||||
OpenSesame = 6,
|
||||
}
|
||||
|
||||
raw_enum_from_byte!(
|
||||
RawTextEmphasis,
|
||||
None,
|
||||
[
|
||||
None,
|
||||
FilledDot,
|
||||
OpenDot,
|
||||
FilledCircle,
|
||||
OpenCircle,
|
||||
FilledSesame,
|
||||
OpenSesame
|
||||
]
|
||||
);
|
||||
|
||||
impl From<RawTextEmphasis> for TextEmphasis {
|
||||
fn from(value: RawTextEmphasis) -> Self {
|
||||
match value {
|
||||
RawTextEmphasis::None => TextEmphasis::None,
|
||||
RawTextEmphasis::FilledDot => TextEmphasis::FilledDot,
|
||||
RawTextEmphasis::OpenDot => TextEmphasis::OpenDot,
|
||||
RawTextEmphasis::FilledCircle => TextEmphasis::FilledCircle,
|
||||
RawTextEmphasis::OpenCircle => TextEmphasis::OpenCircle,
|
||||
RawTextEmphasis::FilledSesame => TextEmphasis::FilledSesame,
|
||||
RawTextEmphasis::OpenSesame => TextEmphasis::OpenSesame,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, ToJs)]
|
||||
#[repr(u8)]
|
||||
pub enum RawTextEmphasisPosition {
|
||||
Auto = 0,
|
||||
OverRight = 1,
|
||||
OverLeft = 2,
|
||||
UnderRight = 3,
|
||||
UnderLeft = 4,
|
||||
}
|
||||
|
||||
raw_enum_from_byte!(
|
||||
RawTextEmphasisPosition,
|
||||
Auto,
|
||||
[Auto, OverRight, OverLeft, UnderRight, UnderLeft]
|
||||
);
|
||||
|
||||
impl From<RawTextEmphasisPosition> for shapes::TextEmphasisPosition {
|
||||
fn from(value: RawTextEmphasisPosition) -> Self {
|
||||
match value {
|
||||
RawTextEmphasisPosition::Auto => Self::Auto,
|
||||
RawTextEmphasisPosition::OverRight => Self::OverRight,
|
||||
RawTextEmphasisPosition::OverLeft => Self::OverLeft,
|
||||
RawTextEmphasisPosition::UnderRight => Self::UnderRight,
|
||||
RawTextEmphasisPosition::UnderLeft => Self::UnderLeft,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, ToJs)]
|
||||
#[repr(u8)]
|
||||
pub enum RawTextUnderlinePosition {
|
||||
Auto = 0,
|
||||
Under = 1,
|
||||
Left = 2,
|
||||
Right = 3,
|
||||
UnderLeft = 4,
|
||||
UnderRight = 5,
|
||||
}
|
||||
|
||||
raw_enum_from_byte!(
|
||||
RawTextUnderlinePosition,
|
||||
Auto,
|
||||
[Auto, Under, Left, Right, UnderLeft, UnderRight]
|
||||
);
|
||||
|
||||
impl From<RawTextUnderlinePosition> for shapes::TextUnderlinePosition {
|
||||
fn from(value: RawTextUnderlinePosition) -> Self {
|
||||
match value {
|
||||
RawTextUnderlinePosition::Auto => Self::Auto,
|
||||
RawTextUnderlinePosition::Under => Self::Under,
|
||||
RawTextUnderlinePosition::Left => Self::Left,
|
||||
RawTextUnderlinePosition::Right => Self::Right,
|
||||
RawTextUnderlinePosition::UnderLeft => Self::UnderLeft,
|
||||
RawTextUnderlinePosition::UnderRight => Self::UnderRight,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, ToJs)]
|
||||
#[repr(u8)]
|
||||
pub enum RawWarichu {
|
||||
None = 0,
|
||||
Warichu = 1,
|
||||
}
|
||||
|
||||
raw_enum_from_byte!(RawWarichu, None, [None, Warichu]);
|
||||
|
||||
impl From<RawWarichu> for bool {
|
||||
fn from(value: RawWarichu) -> Self {
|
||||
value == RawWarichu::Warichu
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, ToJs)]
|
||||
#[repr(u8)]
|
||||
pub enum RawFontFeatures {
|
||||
None = 0,
|
||||
Palt = 1,
|
||||
Vpal = 2,
|
||||
}
|
||||
|
||||
raw_enum_from_byte!(RawFontFeatures, None, [None, Palt, Vpal]);
|
||||
|
||||
impl From<RawFontFeatures> for FontFeatures {
|
||||
fn from(value: RawFontFeatures) -> Self {
|
||||
match value {
|
||||
RawFontFeatures::None => FontFeatures::None,
|
||||
RawFontFeatures::Palt => FontFeatures::Palt,
|
||||
RawFontFeatures::Vpal => FontFeatures::Vpal,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, ToJs)]
|
||||
#[repr(u8)]
|
||||
pub enum RawAnnotationClearance {
|
||||
None = 0,
|
||||
Auto = 1,
|
||||
}
|
||||
|
||||
raw_enum_from_byte!(RawAnnotationClearance, None, [None, Auto]);
|
||||
|
||||
impl From<RawAnnotationClearance> for AnnotationClearance {
|
||||
fn from(value: RawAnnotationClearance) -> Self {
|
||||
match value {
|
||||
RawAnnotationClearance::None => AnnotationClearance::None,
|
||||
RawAnnotationClearance::Auto => AnnotationClearance::Auto,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, ToJs)]
|
||||
#[repr(u8)]
|
||||
pub enum RawRubySize {
|
||||
Half = 0,
|
||||
Third = 1,
|
||||
Quarter = 2,
|
||||
}
|
||||
|
||||
raw_enum_from_byte!(RawRubySize, Half, [Half, Third, Quarter]);
|
||||
|
||||
impl From<RawRubySize> for RubySize {
|
||||
fn from(value: RawRubySize) -> Self {
|
||||
match value {
|
||||
RawRubySize::Half => RubySize::Half,
|
||||
RawRubySize::Third => RubySize::Third,
|
||||
RawRubySize::Quarter => RubySize::Quarter,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, ToJs)]
|
||||
#[repr(u8)]
|
||||
pub enum RawRubyAlign {
|
||||
SpaceAround = 0,
|
||||
Center = 1,
|
||||
Start = 2,
|
||||
SpaceBetween = 3,
|
||||
}
|
||||
|
||||
raw_enum_from_byte!(
|
||||
RawRubyAlign,
|
||||
SpaceAround,
|
||||
[SpaceAround, Center, Start, SpaceBetween]
|
||||
);
|
||||
|
||||
impl From<RawRubyAlign> for RubyAlign {
|
||||
fn from(value: RawRubyAlign) -> Self {
|
||||
match value {
|
||||
RawRubyAlign::SpaceAround => RubyAlign::SpaceAround,
|
||||
RawRubyAlign::Center => RubyAlign::Center,
|
||||
RawRubyAlign::Start => RubyAlign::Start,
|
||||
RawRubyAlign::SpaceBetween => RubyAlign::SpaceBetween,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, ToJs)]
|
||||
#[repr(u8)]
|
||||
pub enum RawRubyOverhang {
|
||||
Auto = 0,
|
||||
None = 1,
|
||||
}
|
||||
|
||||
raw_enum_from_byte!(RawRubyOverhang, Auto, [Auto, None]);
|
||||
|
||||
impl From<RawRubyOverhang> for RubyOverhang {
|
||||
fn from(value: RawRubyOverhang) -> Self {
|
||||
match value {
|
||||
RawRubyOverhang::Auto => RubyOverhang::Auto,
|
||||
RawRubyOverhang::None => RubyOverhang::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, ToJs)]
|
||||
#[repr(u8)]
|
||||
pub enum RawRubySide {
|
||||
Over = 0,
|
||||
Under = 1,
|
||||
}
|
||||
|
||||
raw_enum_from_byte!(RawRubySide, Over, [Over, Under]);
|
||||
|
||||
impl From<RawRubySide> for AnnotationSide {
|
||||
fn from(value: RawRubySide) -> Self {
|
||||
match value {
|
||||
RawRubySide::Over => AnnotationSide::Over,
|
||||
RawRubySide::Under => AnnotationSide::Under,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The variants are Adobe's Kinsoku Type values and the JS keys.
|
||||
#[allow(clippy::enum_variant_names)]
|
||||
#[derive(Debug, PartialEq, Clone, Copy, ToJs)]
|
||||
#[repr(u8)]
|
||||
pub enum RawLineAdjustment {
|
||||
PushInFirst = 0,
|
||||
PushOutFirst = 1,
|
||||
PushOutOnly = 2,
|
||||
}
|
||||
|
||||
raw_enum_from_byte!(
|
||||
RawLineAdjustment,
|
||||
PushInFirst,
|
||||
[PushInFirst, PushOutFirst, PushOutOnly]
|
||||
);
|
||||
|
||||
impl From<RawLineAdjustment> for LineAdjustment {
|
||||
fn from(value: RawLineAdjustment) -> Self {
|
||||
match value {
|
||||
RawLineAdjustment::PushInFirst => LineAdjustment::PushInFirst,
|
||||
RawLineAdjustment::PushOutFirst => LineAdjustment::PushOutFirst,
|
||||
RawLineAdjustment::PushOutOnly => LineAdjustment::PushOutOnly,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, ToJs)]
|
||||
#[repr(u8)]
|
||||
pub enum RawKoreanWrap {
|
||||
Syllable = 0,
|
||||
SpacesOnly = 1,
|
||||
KeepWords = 2,
|
||||
}
|
||||
|
||||
raw_enum_from_byte!(RawKoreanWrap, Syllable, [Syllable, SpacesOnly, KeepWords]);
|
||||
|
||||
impl From<RawKoreanWrap> for KoreanWrap {
|
||||
fn from(value: RawKoreanWrap) -> Self {
|
||||
match value {
|
||||
RawKoreanWrap::Syllable => KoreanWrap::Syllable,
|
||||
RawKoreanWrap::SpacesOnly => KoreanWrap::SpacesOnly,
|
||||
RawKoreanWrap::KeepWords => KoreanWrap::KeepWords,
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user