Created
March 29, 2019 20:37
-
-
Save raphlinus/7b669e6af16e69877da3a5287c47a506 to your computer and use it in GitHub Desktop.
example code for dwrote-rs to exercise fallback
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
| extern crate dwrote; | |
| extern crate winapi; | |
| use std::borrow::Cow; | |
| use winapi::um::dwrite::{DWRITE_NUMBER_SUBSTITUTION_METHOD_NONE, DWRITE_READING_DIRECTION, | |
| DWRITE_READING_DIRECTION_LEFT_TO_RIGHT | |
| }; | |
| use dwrote::{FontCollection, FontFallback, FontStretch, FontStyle, FontWeight, NumberSubstitution, | |
| TextAnalysisSource, TextAnalysisSourceImpl, | |
| }; | |
| struct MyTextAnalysisSource(u32); | |
| impl TextAnalysisSourceImpl for MyTextAnalysisSource { | |
| fn get_locale_name<'a>(&'a self, text_pos: u32) -> (Cow<'a, str>, u32) { | |
| ("en-US".into(), self.0 - text_pos) | |
| } | |
| fn get_paragraph_reading_direction(&self) -> DWRITE_READING_DIRECTION { | |
| DWRITE_READING_DIRECTION_LEFT_TO_RIGHT | |
| } | |
| } | |
| fn main() { | |
| let collection = FontCollection::get_system(true); | |
| let count = collection.get_font_family_count(); | |
| println!("system font: {} families", count); | |
| /* | |
| for i in 0..count { | |
| let family = collection.get_font_family(i); | |
| println!("font {}: {}", i, family.name()); | |
| } | |
| */ | |
| let fallback = FontFallback::get_system_fallback().expect("error getting fallback"); | |
| let text = "\u{4000}"; | |
| let text: Vec<_> = text.encode_utf16().collect(); | |
| println!("text: {:x?}", text); | |
| let number_subst = NumberSubstitution::new( | |
| DWRITE_NUMBER_SUBSTITUTION_METHOD_NONE, | |
| "en-US", | |
| true, | |
| ); | |
| let len = text.len(); | |
| let text_analysis = TextAnalysisSource::new( | |
| Box::new(MyTextAnalysisSource(text.len() as u32)), | |
| text, | |
| number_subst, | |
| ); | |
| let fallback_result = fallback.map_characters( | |
| &text_analysis, | |
| 0, | |
| len as u32, | |
| &collection, | |
| Some("Arial"), | |
| FontWeight::Regular, | |
| FontStyle::Normal, | |
| FontStretch::Normal, | |
| ); | |
| println!("fallback result: {}, {:?}, {}", | |
| fallback_result.mapped_length, | |
| fallback_result.mapped_font.map(|f| f.family_name()), | |
| fallback_result.scale | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment