ruviz 0.14.2

High-performance 2D and 3D plotting library for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! Shared font fallback and language settings for plot text.

use std::{borrow::Cow, sync::Arc};

use crate::core::{PlottingError, Result};

/// Dominant paragraph direction. Individual script runs still use Unicode bidi.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub enum TextDirection {
    /// Follow the configured language, or the text when no language is set.
    #[default]
    Auto,
    /// Left-to-right paragraph layout.
    LeftToRight,
    /// Right-to-left paragraph layout.
    RightToLeft,
}

/// International typography settings, shared cheaply between labels.
///
/// The primary family remains [`FontConfig::family`](super::FontConfig::family).
/// Fallback fonts are tried in order before language and system fallbacks.
/// Fonts must be installed or supplied with [`super::register_font_bytes`].
/// Mathematical expressions have a separate OpenType math font.
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
pub struct TextOptions(Option<Arc<Options>>);

#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
struct Options {
    fallbacks: Option<Arc<[String]>>,
    language: Option<String>,
    direction: Option<TextDirection>,
    math_font: Option<String>,
    require_all_glyphs: Option<bool>,
}

impl TextOptions {
    /// Create settings that inherit renderer defaults, without allocating.
    pub fn new() -> Self {
        Self::default()
    }

    fn edit(&mut self) -> &mut Options {
        Arc::make_mut(self.0.get_or_insert_with(|| Arc::new(Options::default())))
    }

    /// Set an ordered list of fallback family names (at most 32).
    pub fn font_fallbacks<I, S>(mut self, families: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.edit().fallbacks = Some(families.into_iter().map(Into::into).collect());
        self
    }

    /// Set an ISO 639 language with optional script/region, such as `ja`, `zh-TW`, or `ar`.
    ///
    /// This selects regional CJK fallback families and the paragraph direction.
    pub fn language(mut self, language: impl Into<String>) -> Self {
        self.edit().language = Some(language.into().replace('_', "-").to_ascii_lowercase());
        self
    }

    /// Set the dominant paragraph direction explicitly.
    pub fn direction(mut self, direction: TextDirection) -> Self {
        self.edit().direction = Some(direction);
        self
    }

    /// Select an OpenType math font for Typst equations.
    ///
    /// For example, `Fira Math` provides sans-serif equations. The font must be
    /// installed or registered; unavailable or non-math fonts produce an error.
    pub fn math_font(mut self, family: impl Into<String>) -> Self {
        self.edit().math_font = Some(family.into());
        self
    }

    /// Fail on missing glyphs instead of exporting replacement boxes.
    /// Disabled by default for compatibility; useful for publication exports.
    pub fn require_all_glyphs(mut self, required: bool) -> Self {
        self.edit().require_all_glyphs = Some(required);
        self
    }

    /// Whether a missing glyph causes an export error.
    pub fn requires_all_glyphs(&self) -> bool {
        self.0
            .as_ref()
            .and_then(|o| o.require_all_glyphs)
            .unwrap_or(false)
    }

    /// Configured fallback family names in priority order.
    pub fn fallback_families(&self) -> &[String] {
        self.0
            .as_ref()
            .and_then(|o| o.fallbacks.as_deref())
            .unwrap_or(&[])
    }

    /// Explicitly selected language, if any.
    pub fn text_language(&self) -> Option<&str> {
        self.0.as_ref().and_then(|o| o.language.as_deref())
    }

    /// Explicit paragraph direction, or automatic direction.
    pub fn text_direction(&self) -> TextDirection {
        self.0
            .as_ref()
            .and_then(|o| o.direction)
            .unwrap_or_default()
    }

    /// Explicitly selected math family, if any.
    pub fn math_font_family(&self) -> Option<&str> {
        self.0.as_ref().and_then(|o| o.math_font.as_deref())
    }

    pub(crate) fn validate(&self) -> Result<()> {
        if self.fallback_families().len() > 32 {
            return Err(PlottingError::InvalidInput(
                "at most 32 fallback font families are supported".into(),
            ));
        }
        if self.fallback_families().iter().any(|f| f.trim().is_empty())
            || self.math_font_family().is_some_and(|f| f.trim().is_empty())
        {
            return Err(PlottingError::InvalidInput(
                "font family names must not be empty".into(),
            ));
        }
        if let Some(language) = self.text_language()
            && (language.len() > 63
                || !language.split('-').next().is_some_and(|primary| {
                    (2..=3).contains(&primary.len())
                        && primary.bytes().all(|b| b.is_ascii_alphabetic())
                })
                || language.split('-').any(|s| {
                    s.is_empty() || s.len() > 8 || !s.bytes().all(|b| b.is_ascii_alphanumeric())
                }))
        {
            return Err(PlottingError::InvalidInput(
                "language must start with a two- or three-letter ISO language code, such as ja, zh-TW, or ar".into(),
            ));
        }
        Ok(())
    }

    pub(crate) fn overlay(&self, child: Option<&Self>) -> Self {
        let Some(child) = child.and_then(|o| o.0.as_ref()) else {
            return self.clone();
        };
        let Some(parent) = self.0.as_ref() else {
            return Self(Some(child.clone()));
        };
        Self(Some(Arc::new(Options {
            fallbacks: child.fallbacks.clone().or_else(|| parent.fallbacks.clone()),
            language: child.language.clone().or_else(|| parent.language.clone()),
            direction: child.direction.or(parent.direction),
            math_font: child.math_font.clone().or_else(|| parent.math_font.clone()),
            require_all_glyphs: child.require_all_glyphs.or(parent.require_all_glyphs),
        })))
    }

    pub(crate) fn needs_font_context(&self) -> bool {
        !self.fallback_families().is_empty() || self.text_language().is_some()
    }

    pub(crate) fn language_components(&self) -> Option<(&str, Option<&str>, Option<&str>)> {
        let mut parts = self.text_language()?.split('-');
        let primary = parts.next()?;
        let mut next = parts.next();
        let script = if next
            .is_some_and(|part| part.len() == 4 && part.bytes().all(|b| b.is_ascii_alphabetic()))
        {
            let script = next;
            next = parts.next();
            script
        } else {
            None
        };
        let region = next.filter(|part| {
            part.len() == 2 && part.bytes().all(|b| b.is_ascii_alphabetic())
                || part.len() == 3 && part.bytes().all(|b| b.is_ascii_digit())
        });
        Some((primary, script, region))
    }

    pub(crate) fn same_font_context(&self, other: &Self) -> bool {
        self.fallback_families() == other.fallback_families()
            && self.text_language() == other.text_language()
    }

    pub(crate) fn resolved_direction(&self) -> Option<TextDirection> {
        match self.text_direction() {
            TextDirection::Auto => self.language_components().map(|(language, script, _)| {
                if let Some(script) = script {
                    return if matches!(
                        script,
                        "arab" | "hebr" | "syrc" | "thaa" | "nkoo" | "adlm" | "rohg"
                    ) {
                        TextDirection::RightToLeft
                    } else {
                        TextDirection::LeftToRight
                    };
                }
                match language {
                    "ar" | "he" | "fa" | "ur" | "ps" | "sd" | "yi" | "dv" | "ug" | "syr"
                    | "nqo" | "ckb" => TextDirection::RightToLeft,
                    _ => TextDirection::LeftToRight,
                }
            }),
            explicit => Some(explicit),
        }
    }

    /// Add bidi embeddings only when the requested direction differs from the
    /// paragraph's natural direction. Avoiding redundant controls preserves the
    /// shaper's fast path for ordinary CJK, Latin, and Arabic labels.
    pub(crate) fn directed_text<'a>(&self, text: &'a str) -> Cow<'a, str> {
        use unicode_bidi::{BidiClass, Direction, bidi_class, get_base_direction};
        let Some(direction) = self.resolved_direction() else {
            return Cow::Borrowed(text);
        };
        let rtl = direction == TextDirection::RightToLeft;
        let is_separator = |character| bidi_class(character) == BidiClass::B;
        let needs_override = |paragraph: &str| {
            !paragraph.trim().is_empty() && (get_base_direction(paragraph) == Direction::Rtl) != rtl
        };
        if text
            .split_inclusive(is_separator)
            .all(|paragraph| !needs_override(paragraph))
        {
            return Cow::Borrowed(text);
        }
        let embedding = if rtl { '\u{202b}' } else { '\u{202a}' };
        let mut directed = String::with_capacity(text.len() + 6);
        for paragraph in text.split_inclusive(is_separator) {
            if !needs_override(paragraph) {
                directed.push_str(paragraph);
                continue;
            }
            let separator_start = paragraph
                .char_indices()
                .next_back()
                .filter(|(_, character)| is_separator(*character))
                .map(|(index, _)| index)
                .unwrap_or(paragraph.len());
            directed.push(embedding);
            directed.push_str(&paragraph[..separator_start]);
            directed.push('\u{202c}');
            directed.push_str(&paragraph[separator_start..]);
        }
        Cow::Owned(directed)
    }

    pub(crate) fn language_fallbacks(&self) -> &'static [&'static str] {
        let Some((language, script, region)) = self.language_components() else {
            return &[];
        };
        match language {
            "ja" => &[
                "Noto Sans CJK JP",
                "Noto Sans JP",
                "Hiragino Sans",
                "Yu Gothic",
                "Meiryo",
            ],
            "ko" => &[
                "Noto Sans CJK KR",
                "Noto Sans KR",
                "Apple SD Gothic Neo",
                "Malgun Gothic",
            ],
            "zh" if matches!(region, Some("hk" | "mo")) => &[
                "Noto Sans CJK HK",
                "Noto Sans HK",
                "PingFang HK",
                "Noto Sans CJK TC",
                "Microsoft JhengHei",
            ],
            "zh" if script == Some("hant") || region == Some("tw") => &[
                "Noto Sans CJK TC",
                "Noto Sans TC",
                "PingFang TC",
                "Microsoft JhengHei",
            ],
            "zh" => &[
                "Noto Sans CJK SC",
                "Noto Sans SC",
                "PingFang SC",
                "Microsoft YaHei",
            ],
            "ar" | "fa" | "ur" => &["Noto Sans Arabic", "Noto Naskh Arabic"],
            "he" | "yi" => &["Noto Sans Hebrew"],
            _ => &[],
        }
    }
}

/// Ignore formatting controls; these intentionally have no visible glyph.
pub(crate) fn missing_glyph_error(cluster: &str) -> Option<PlottingError> {
    let character = cluster.chars().find(|c| !c.is_whitespace() && !c.is_control()
        && !matches!(*c, '\u{00ad}' | '\u{061c}' | '\u{200b}'..='\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{2060}'..='\u{206f}' | '\u{fe00}'..='\u{fe0f}' | '\u{feff}' | '\u{e0100}'..='\u{e01ef}'))?;
    Some(PlottingError::RenderError(format!(
        "No font supplies a glyph for U+{:04X} ({character}); install or register a font and add it to font_fallbacks",
        character as u32
    )))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn direction_only_adds_controls_to_paragraphs_that_need_an_override() {
        for (language, text) in [
            ("ja", "散乱強度 / Signal"),
            ("ar", "العربية / Signal"),
            ("en", "\u{2067}עברית\u{2069} Signal"),
        ] {
            assert!(
                matches!(
                    TextOptions::new().language(language).directed_text(text),
                    Cow::Borrowed(_)
                ),
                "{language}: {text}"
            );
        }
        let options = TextOptions::new().language("ar");
        assert_eq!(
            options.directed_text("Signal 123"),
            "\u{202b}Signal 123\u{202c}"
        );
        assert_eq!(
            options.directed_text("العربية\nSignal\u{2029}עברית"),
            "العربية\n\u{202b}Signal\u{202c}\u{2029}עברית"
        );
        assert_eq!(
            TextOptions::new().language("ar-Latn").resolved_direction(),
            Some(TextDirection::LeftToRight)
        );
        assert_eq!(
            TextOptions::new().language("az-Arab").resolved_direction(),
            Some(TextDirection::RightToLeft)
        );
        assert_eq!(
            TextOptions::new()
                .language("zh-Hant-TW-u-nu-hanidec")
                .language_fallbacks()[0],
            "Noto Sans CJK TC"
        );
    }

    #[test]
    fn regional_fonts_and_direction_follow_the_language() {
        for (language, family) in [
            ("ja", "Noto Sans CJK JP"),
            ("ko", "Noto Sans CJK KR"),
            ("zh-CN", "Noto Sans CJK SC"),
            ("zh-TW", "Noto Sans CJK TC"),
            ("zh-HK", "Noto Sans CJK HK"),
        ] {
            let options = TextOptions::new().language(language);
            assert_eq!(options.language_fallbacks()[0], family);
        }
        let arabic = TextOptions::new().language("ar");
        assert_eq!(
            arabic.resolved_direction(),
            Some(TextDirection::RightToLeft)
        );
        assert_eq!(
            arabic
                .direction(TextDirection::LeftToRight)
                .resolved_direction(),
            Some(TextDirection::LeftToRight)
        );
        assert!(matches!(
            TextOptions::new().directed_text("Å"),
            Cow::Borrowed(_)
        ));
    }

    #[test]
    fn annotation_overrides_keep_unspecified_parent_settings() {
        let parent = TextOptions::new()
            .font_fallbacks(["Alpha", "Beta"])
            .language("ja")
            .math_font("Fira Math")
            .require_all_glyphs(true);
        let child = TextOptions::new()
            .language("zh_TW")
            .direction(TextDirection::Auto);
        let merged = parent.overlay(Some(&child));
        assert_eq!(merged.fallback_families(), ["Alpha", "Beta"]);
        assert_eq!(merged.text_language(), Some("zh-tw"));
        assert_eq!(merged.math_font_family(), Some("Fira Math"));
        assert!(merged.requires_all_glyphs());
        assert!(
            parent
                .overlay(Some(
                    &TextOptions::new().font_fallbacks(Vec::<String>::new())
                ))
                .fallback_families()
                .is_empty()
        );
    }

    #[test]
    fn malformed_options_fail_and_formatting_controls_are_ignored() {
        for options in [
            TextOptions::new().language("ja\""),
            TextOptions::new().language(""),
            TextOptions::new().font_fallbacks([""]),
            TextOptions::new().font_fallbacks(vec!["Font"; 33]),
            TextOptions::new().math_font(" "),
        ] {
            assert!(options.validate().is_err());
        }
        assert!(missing_glyph_error("\u{200d}\u{202b}\u{fe0f}").is_none());
        assert!(
            missing_glyph_error("\u{10ffff}")
                .unwrap()
                .to_string()
                .contains("U+10FFFF")
        );
    }
}