Repository navigation
feat(fonts,render,io): add pluggable legacy 8-bit font transcoding framework (Vietnamese TCVN3/VNI profile) #1877
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vantruong-dang
wants to merge
13
commits into
storytold:main
Choose a base branch
from
vantruong-dang:feature/pr1-vietnamese-fonts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
62650c3
feat(fonts): add context-aware Vietnamese legacy transcoding (TCVN3 &…
vantruong-dang d0857af
feat(render,io): integrate legacy transcoding into text, dimensions, …
vantruong-dang 1f451da
fix(render): preserve MTEXT formatting control codes during legacy tr…
vantruong-dang 3283058
fix(render): handle unterminated \U+/\M+ escapes and use markup-aware…
vantruong-dang 2d7dc0f
fix(render): fully generalize MTEXT argument-less escapes (\X, \L, \O…
vantruong-dang fe301e6
fix(fonts): sync argument-less \X and unicode escapes in MTEXT parser
vantruong-dang 2877a8f
fix(dim): correctly split \X dimension text into top and bottom portions
vantruong-dang b9153b0
fix(dim): correctly detect \X control vs escaped backslash in linear …
vantruong-dang 26a26ac
feat(fonts): add font_hint off-switch, transcode status APIs, and sta…
vantruong-dang 554169d
test(fonts): pin VNIcons font prefix behavior with Unicode guard
vantruong-dang 6353abd
fix(fonts): use is_vietnamese_font in transcode_summary
vantruong-dang 9aee0a9
refactor(fonts,app): move CADCRAFT_DISABLE_LEGACY_FONT_HINT to startu…
vantruong-dang 40a40c2
docs(fonts): align vietnamese table provenance with clean-room policy
vantruong-dang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,267 @@ | ||
| //! Pluggable framework for transcoding legacy 8-bit CAD font encodings to Unicode NFC. | ||
| //! | ||
| //! Many regional CAD workflows from the 1990s–2000s paired custom 8-bit TrueType or SHX fonts | ||
| //! with single-byte text stored under `$DWGCODEPAGE ANSI_1252`. Those fonts repurpose Latin-1 | ||
| //! code points for regional scripts and lack proper Unicode CMAP tables. | ||
| //! | ||
| //! This module separates the general dispatch mechanism (`LegacyProfile`, `font_hint`, | ||
| //! `transcode`, `substitute_face`) from language-specific rule sets. [`vietnamese`] (supporting | ||
| //! TCVN3, TCVN3H, and VNI Windows) is the first registered profile. | ||
|
|
||
| use std::borrow::Cow; | ||
|
|
||
| pub mod vietnamese; | ||
|
|
||
| /// Classification of the modern Unicode substitute typeface for a legacy 8-bit font. | ||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| pub enum FontClass { | ||
| Serif, | ||
| Sans, | ||
| } | ||
|
|
||
| /// A matched legacy encoding profile and its sub-variant identifier. | ||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| pub struct ProfileHit { | ||
| pub profile: &'static str, | ||
| pub variant: u8, | ||
| } | ||
|
|
||
| /// Pluggable profile for a legacy 8-bit CAD font encoding family. | ||
| pub trait LegacyProfile: Sync { | ||
| /// Unique identifier of this profile (e.g. `"vietnamese"`). | ||
| fn name(&self) -> &'static str; | ||
|
|
||
| /// Check whether `font` (a font filename, family name, or CAD style name) belongs to this | ||
| /// legacy profile. | ||
| fn match_font(&self, font: &str) -> Option<ProfileHit>; | ||
|
|
||
| /// Conservatively inspect text `s` when no recognized legacy font name was provided. | ||
| fn detect(&self, s: &str) -> Option<ProfileHit>; | ||
|
|
||
| /// Transcode legacy-encoded text `s` into Unicode NFC according to `hit`. | ||
| fn decode<'a>(&self, s: &'a str, hit: ProfileHit) -> Cow<'a, str>; | ||
|
|
||
| /// Choose the modern Unicode fallback class (`Serif` or `Sans`) for a matched legacy font. | ||
| fn substitute(&self, font: &str) -> FontClass; | ||
| } | ||
|
|
||
| static PROFILES: &[&dyn LegacyProfile] = &[&vietnamese::Vietnamese]; | ||
|
|
||
| /// Environment variable name to disable legacy font hint detection. | ||
| pub const DISABLE_LEGACY_FONT_HINT_ENV: &str = "CADCRAFT_DISABLE_LEGACY_FONT_HINT"; | ||
|
|
||
| /// Check whether the legacy font hint mechanism is disabled via the | ||
| /// `CADCRAFT_DISABLE_LEGACY_FONT_HINT` environment variable. | ||
| pub fn is_font_hint_disabled() -> bool { | ||
| is_font_hint_disabled_with(|k| std::env::var(k).ok()) | ||
| } | ||
|
|
||
| pub(crate) fn is_font_hint_disabled_with(mut get_var: impl FnMut(&str) -> Option<String>) -> bool { | ||
| match get_var(DISABLE_LEGACY_FONT_HINT_ENV) { | ||
| Some(v) => match v.trim().to_ascii_lowercase().as_str() { | ||
| "0" | "false" | "off" | "no" => false, | ||
| _ => !v.trim().is_empty(), | ||
| }, | ||
| None => false, | ||
| } | ||
| } | ||
|
|
||
| /// Return the first candidate in `candidates` (such as `&[&ts.font, &style_name]`) that matches | ||
| /// a registered [`LegacyProfile::match_font`]. | ||
| /// | ||
| /// Returns `None` if the `CADCRAFT_DISABLE_LEGACY_FONT_HINT` environment variable is set. | ||
| pub fn font_hint<'a>(candidates: &[&'a str]) -> Option<&'a str> { | ||
| if is_font_hint_disabled() { | ||
| return None; | ||
| } | ||
| font_hint_with(PROFILES, candidates) | ||
| } | ||
|
|
||
| /// Like [`font_hint`], using an explicit slice of profiles. | ||
| pub fn font_hint_with<'a>(profiles: &[&dyn LegacyProfile], candidates: &[&'a str]) -> Option<&'a str> { | ||
| for &c in candidates { | ||
| let trimmed = c.trim(); | ||
| if !trimmed.is_empty() && profiles.iter().any(|p| p.match_font(trimmed).is_some()) { | ||
| return Some(c); | ||
| } | ||
| } | ||
| None | ||
| } | ||
|
|
||
| /// Transcode legacy 8-bit CAD text to Unicode NFC using registered encoding profiles. | ||
| /// | ||
| /// If `hint` matches a registered profile's font naming rules, that profile decodes the text; | ||
| /// otherwise each profile's conservative [`LegacyProfile::detect`] heuristic is tried in order. | ||
| /// Returns `Cow::Borrowed(s)` with zero allocation when no legacy encoding applies. | ||
| /// | ||
| /// If the `CADCRAFT_DISABLE_LEGACY_FONT_HINT` environment variable is set, `hint` is ignored. | ||
| pub fn transcode<'a>(s: &'a str, hint: Option<&str>) -> Cow<'a, str> { | ||
| let effective_hint = if is_font_hint_disabled() { None } else { hint }; | ||
| transcode_with(PROFILES, s, effective_hint) | ||
| } | ||
|
|
||
| /// Like [`transcode`], using an explicit slice of profiles. | ||
| pub fn transcode_with<'a>(profiles: &[&dyn LegacyProfile], s: &'a str, hint: Option<&str>) -> Cow<'a, str> { | ||
| if s.is_empty() { | ||
| return Cow::Borrowed(s); | ||
| } | ||
| if let Some(h) = hint.map(str::trim).filter(|h| !h.is_empty()) { | ||
| for p in profiles { | ||
| if let Some(hit) = p.match_font(h) { | ||
| return p.decode(s, hit); | ||
| } | ||
| } | ||
| } | ||
| for p in profiles { | ||
| if let Some(hit) = p.detect(s) { | ||
| return p.decode(s, hit); | ||
| } | ||
| } | ||
| Cow::Borrowed(s) | ||
| } | ||
|
|
||
| /// If `font` belongs to a registered legacy 8-bit encoding profile, return its modern Unicode | ||
| /// substitute typeface class (`Serif` or `Sans`). | ||
| /// | ||
| /// Returns `None` if the `CADCRAFT_DISABLE_LEGACY_FONT_HINT` environment variable is set. | ||
| pub fn substitute_face(font: &str) -> Option<FontClass> { | ||
| if is_font_hint_disabled() { | ||
| return None; | ||
| } | ||
| substitute_face_with(PROFILES, font) | ||
| } | ||
|
|
||
| /// Like [`substitute_face`], using an explicit slice of profiles. | ||
| pub fn substitute_face_with(profiles: &[&dyn LegacyProfile], font: &str) -> Option<FontClass> { | ||
| let n = font.trim(); | ||
| if n.is_empty() { | ||
| return None; | ||
| } | ||
| for p in profiles { | ||
| if p.match_font(n).is_some() { | ||
| return Some(p.substitute(n)); | ||
| } | ||
| } | ||
| None | ||
| } | ||
|
|
||
| /// Return true if `text` contains legacy-encoded characters that would be | ||
| /// transcoded under `hint`. | ||
| /// | ||
| /// UI and engine layers can query this to detect whether a text entity requires | ||
| /// transcoding before or after rendering. | ||
| pub fn is_legacy_text(text: &str, hint: Option<&str>) -> bool { | ||
| transcode(text, hint) != text | ||
| } | ||
|
|
||
| /// Return a human-readable summary of the matched legacy encoding family. | ||
| pub fn transcode_summary(hint: Option<&str>) -> &'static str { | ||
| match hint.and_then(vietnamese::is_vietnamese_font) { | ||
| Some(vietnamese::VietnameseEncoding::Vni) => "VNI Windows", | ||
| Some(vietnamese::VietnameseEncoding::Tcvn3) | Some(vietnamese::VietnameseEncoding::Tcvn3H) => "TCVN3", | ||
| None => "legacy 8-bit", | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| struct DummyProfile; | ||
|
|
||
| impl LegacyProfile for DummyProfile { | ||
| fn name(&self) -> &'static str { | ||
| "dummy" | ||
| } | ||
|
|
||
| fn match_font(&self, font: &str) -> Option<ProfileHit> { | ||
| if font.trim().eq_ignore_ascii_case("dummy-serif.ttf") { Some(ProfileHit { profile: "dummy", variant: 1 }) } else { None } | ||
| } | ||
|
|
||
| fn detect(&self, s: &str) -> Option<ProfileHit> { | ||
| if s.contains("\u{00BF}\u{00BF}") { Some(ProfileHit { profile: "dummy", variant: 1 }) } else { None } | ||
| } | ||
|
|
||
| fn decode<'a>(&self, s: &'a str, _hit: ProfileHit) -> Cow<'a, str> { | ||
| if s.contains('\u{00BF}') { Cow::Owned(s.replace('\u{00BF}', "Ω")) } else { Cow::Borrowed(s) } | ||
| } | ||
|
|
||
| fn substitute(&self, _font: &str) -> FontClass { | ||
| FontClass::Serif | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn european_text_under_standard_and_arial_stays_borrowed() { | ||
| let samples = ["Piñón", "Façade", "Übergang", "Straße", "Piñón, Façade, Übergang, Straße", "Größe, Österreich, Niño, Señora, Garçon"]; | ||
| for sample in samples { | ||
| for hint in [None, Some("Standard"), Some("Arial"), Some("arial.ttf")] { | ||
| let res = transcode(sample, hint); | ||
| assert!(matches!(res, Cow::Borrowed(_)), "Expected Cow::Borrowed for {sample:?} with hint {hint:?}, got Owned({res:?})"); | ||
| assert_eq!(res, sample); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn custom_profile_plugs_into_framework_without_changing_callers() { | ||
| let profiles: &[&dyn LegacyProfile] = &[&vietnamese::Vietnamese, &DummyProfile]; | ||
|
|
||
| // Dummy profile matched via font_hint and substitute_face | ||
| let hint = font_hint_with(profiles, &["Standard", "dummy-serif.ttf"]); | ||
| assert_eq!(hint, Some("dummy-serif.ttf")); | ||
| assert_eq!(substitute_face_with(profiles, hint.unwrap_or("")), Some(FontClass::Serif)); | ||
| assert_eq!(transcode_with(profiles, "R=10\u{00BF}", hint), "R=10Ω"); | ||
|
|
||
| // Dummy profile matched via unhinted detect | ||
| assert_eq!(transcode_with(profiles, "VAL \u{00BF}\u{00BF}", Some("Standard")), "VAL ΩΩ"); | ||
|
|
||
| // Vietnamese profile continues to work alongside DummyProfile | ||
| let vn_hint = font_hint_with(profiles, &[".VnTime", "Standard"]); | ||
| assert_eq!(vn_hint, Some(".VnTime")); | ||
| assert_eq!(substitute_face_with(profiles, vn_hint.unwrap_or("")), Some(FontClass::Serif)); | ||
| assert_eq!(transcode_with(profiles, "B¶n vÏ", vn_hint), "Bản vẽ"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn font_hint_selects_first_matching_legacy_candidate() { | ||
| assert_eq!(font_hint(&["", ".VnTime"]), Some(".VnTime")); | ||
| assert_eq!(font_hint(&["CADCraft Stroke", "VNI-Helve"]), Some("VNI-Helve")); | ||
| assert_eq!(font_hint(&[".VnArialH", "Standard"]), Some(".VnArialH")); | ||
| assert_eq!(font_hint(&["arial.ttf", "Standard"]), None); | ||
| assert_eq!(font_hint(&["", ""]), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn font_hint_disabled_env_switch_parsing() { | ||
| assert!(is_font_hint_disabled_with(|_| Some("1".into()))); | ||
| assert!(is_font_hint_disabled_with(|_| Some("true".into()))); | ||
| assert!(is_font_hint_disabled_with(|_| Some("on".into()))); | ||
| assert!(is_font_hint_disabled_with(|_| Some("yes".into()))); | ||
| assert!(is_font_hint_disabled_with(|_| Some("YES".into()))); | ||
| assert!(is_font_hint_disabled_with(|_| Some(" 1 \n".into()))); | ||
|
|
||
| assert!(!is_font_hint_disabled_with(|_| Some("0".into()))); | ||
| assert!(!is_font_hint_disabled_with(|_| Some("false".into()))); | ||
| assert!(!is_font_hint_disabled_with(|_| Some("off".into()))); | ||
| assert!(!is_font_hint_disabled_with(|_| Some("no".into()))); | ||
| assert!(!is_font_hint_disabled_with(|_| Some("".into()))); | ||
| assert!(!is_font_hint_disabled_with(|_| None)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn is_legacy_text_and_transcode_summary_surface_status() { | ||
| assert!(is_legacy_text("B¶n vÏ", Some(".VnTime"))); | ||
| assert!(!is_legacy_text("Bản vẽ", Some(".VnTime"))); | ||
| assert!(!is_legacy_text("Hello", None)); | ||
|
|
||
| assert_eq!(transcode_summary(Some(".VnTime")), "TCVN3"); | ||
| assert_eq!(transcode_summary(Some(".VnTimeH")), "TCVN3"); | ||
| assert_eq!(transcode_summary(Some("VnArial")), "TCVN3"); | ||
| assert_eq!(transcode_summary(Some("vnsimli.shx")), "TCVN3"); | ||
| assert_eq!(transcode_summary(Some("VNI-Helve")), "VNI Windows"); | ||
| assert_eq!(transcode_summary(Some("VHELVCN.TTF")), "VNI Windows"); | ||
| assert_eq!(transcode_summary(Some("Standard")), "legacy 8-bit"); | ||
| assert_eq!(transcode_summary(None), "legacy 8-bit"); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.