2022-10-06 13:47:25 -06:00
|
|
|
use super::{Font, FontLineIndex, FontShapeGlyph, FontShapeWord, FontShapeLine, FontShapeSpan};
|
2022-10-05 09:16:51 -06:00
|
|
|
|
|
|
|
|
pub struct FontMatches<'a> {
|
|
|
|
|
pub fonts: Vec<&'a Font<'a>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> FontMatches<'a> {
|
2022-10-06 13:47:25 -06:00
|
|
|
fn shape_word(&self, line: &str, start_word: usize, end_word: usize) -> FontShapeWord {
|
|
|
|
|
let word = &line[start_word..end_word];
|
2022-10-05 09:16:51 -06:00
|
|
|
|
2022-10-06 13:47:25 -06:00
|
|
|
let mut words_by_font = Vec::with_capacity(self.fonts.len());
|
2022-10-05 09:16:51 -06:00
|
|
|
for (font_i, font) in self.fonts.iter().enumerate() {
|
|
|
|
|
let font_scale = font.rustybuzz.units_per_em() as f32;
|
|
|
|
|
|
|
|
|
|
let mut buffer = rustybuzz::UnicodeBuffer::new();
|
2022-10-06 13:47:25 -06:00
|
|
|
buffer.push_str(word);
|
2022-10-05 09:16:51 -06:00
|
|
|
buffer.guess_segment_properties();
|
|
|
|
|
let direction = buffer.direction();
|
|
|
|
|
if font_i == 0 {
|
2022-10-06 13:47:25 -06:00
|
|
|
println!("{:?}, {:?}: '{}'", buffer.script(), direction, word);
|
2022-10-05 09:16:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let glyph_buffer = rustybuzz::shape(&font.rustybuzz, &[], buffer);
|
|
|
|
|
let glyph_infos = glyph_buffer.glyph_infos();
|
|
|
|
|
let glyph_positions = glyph_buffer.glyph_positions();
|
|
|
|
|
|
|
|
|
|
let mut misses = 0;
|
|
|
|
|
let mut glyphs = Vec::with_capacity(glyph_infos.len());
|
|
|
|
|
for (info, pos) in glyph_infos.iter().zip(glyph_positions.iter()) {
|
|
|
|
|
let x_advance = pos.x_advance as f32 / font_scale;
|
|
|
|
|
let y_advance = pos.y_advance as f32 / font_scale;
|
|
|
|
|
let x_offset = pos.x_offset as f32 / font_scale;
|
|
|
|
|
let y_offset = pos.y_offset as f32 / font_scale;
|
|
|
|
|
|
|
|
|
|
//println!(" {:?} {:?}", info, pos);
|
|
|
|
|
if info.glyph_id == 0 {
|
|
|
|
|
misses += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "ab_glyph")]
|
|
|
|
|
let inner = ab_glyph::GlyphId(info.glyph_id as u16);
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "rusttype")]
|
|
|
|
|
let inner = font.rusttype.glyph(rusttype::GlyphId(info.glyph_id as u16));
|
|
|
|
|
|
|
|
|
|
glyphs.push(FontShapeGlyph {
|
2022-10-06 13:47:25 -06:00
|
|
|
start: start_word + info.cluster as usize,
|
|
|
|
|
end: end_word, // Set later
|
2022-10-05 09:16:51 -06:00
|
|
|
x_advance,
|
|
|
|
|
y_advance,
|
|
|
|
|
x_offset,
|
|
|
|
|
y_offset,
|
|
|
|
|
#[cfg(feature = "ab_glyph")]
|
|
|
|
|
font: &font.ab_glyph,
|
|
|
|
|
inner,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Adjust end of glyphs
|
|
|
|
|
match direction {
|
|
|
|
|
rustybuzz::Direction::LeftToRight => {
|
|
|
|
|
for i in (1..glyphs.len()).rev() {
|
|
|
|
|
let next_start = glyphs[i].start;
|
|
|
|
|
let next_end = glyphs[i].end;
|
|
|
|
|
let prev = &mut glyphs[i - 1];
|
|
|
|
|
if prev.start == next_start {
|
|
|
|
|
prev.end = next_end;
|
|
|
|
|
} else {
|
|
|
|
|
prev.end = next_start;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
rustybuzz::Direction::RightToLeft => {
|
|
|
|
|
for i in 1..glyphs.len() {
|
|
|
|
|
let next_start = glyphs[i - 1].start;
|
|
|
|
|
let next_end = glyphs[i - 1].end;
|
|
|
|
|
let prev = &mut glyphs[i];
|
|
|
|
|
if prev.start == next_start {
|
|
|
|
|
prev.end = next_end;
|
|
|
|
|
} else {
|
|
|
|
|
prev.end = next_start;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
//TODO: other directions
|
|
|
|
|
_ => (),
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 13:47:25 -06:00
|
|
|
let word = FontShapeWord { glyphs };
|
2022-10-05 09:16:51 -06:00
|
|
|
if misses == 0 {
|
2022-10-06 13:47:25 -06:00
|
|
|
return word;
|
2022-10-05 09:16:51 -06:00
|
|
|
} else {
|
2022-10-06 13:47:25 -06:00
|
|
|
words_by_font.push((misses, word));
|
2022-10-05 09:16:51 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut least_misses_i = 0;
|
|
|
|
|
let mut least_misses = usize::MAX;
|
2022-10-06 13:47:25 -06:00
|
|
|
for (i, (misses, _)) in words_by_font.iter().enumerate() {
|
2022-10-05 09:16:51 -06:00
|
|
|
if *misses < least_misses {
|
|
|
|
|
least_misses_i = i;
|
|
|
|
|
least_misses = *misses;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if least_misses_i > 0 {
|
|
|
|
|
//println!("MISSES {}, {}", least_misses_i, least_misses);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 13:47:25 -06:00
|
|
|
words_by_font.remove(least_misses_i).1
|
2022-10-05 09:16:51 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-06 13:47:25 -06:00
|
|
|
fn shape_span(&self, line: &str, start_span: usize, end_span: usize, rtl: bool) -> FontShapeSpan {
|
2022-10-05 09:16:51 -06:00
|
|
|
use unicode_script::{Script, UnicodeScript};
|
|
|
|
|
|
2022-10-06 13:47:25 -06:00
|
|
|
let span = &line[start_span..end_span];
|
2022-10-05 09:16:51 -06:00
|
|
|
|
2022-10-06 13:47:25 -06:00
|
|
|
let mut words = Vec::new();
|
2022-10-05 09:16:51 -06:00
|
|
|
|
2022-10-06 13:47:25 -06:00
|
|
|
let mut start = 0;
|
|
|
|
|
/*
|
|
|
|
|
let mut word_script = Script::Unknown;
|
|
|
|
|
for (i, c) in span.char_indices() {
|
|
|
|
|
let next_script = c.script();
|
|
|
|
|
if word_script != next_script && word_script != Script::Unknown {
|
2022-10-05 09:16:51 -06:00
|
|
|
// No combination, start new span
|
2022-10-06 13:47:25 -06:00
|
|
|
words.push(self.shape_word(line, start_span + start, start_span + i));
|
2022-10-05 09:16:51 -06:00
|
|
|
start = i;
|
2022-10-06 13:47:25 -06:00
|
|
|
word_script = Script::Unknown;
|
2022-10-05 09:16:51 -06:00
|
|
|
} else {
|
2022-10-06 13:47:25 -06:00
|
|
|
word_script = next_script;
|
2022-10-05 09:16:51 -06:00
|
|
|
}
|
|
|
|
|
}
|
2022-10-06 13:47:25 -06:00
|
|
|
*/
|
|
|
|
|
words.push(self.shape_word(line, start_span + start, end_span));
|
|
|
|
|
|
|
|
|
|
FontShapeSpan {
|
|
|
|
|
rtl,
|
|
|
|
|
words,
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-05 09:16:51 -06:00
|
|
|
|
2022-10-06 13:47:25 -06:00
|
|
|
pub fn shape_line(&self, line_i: FontLineIndex, line: &str) -> FontShapeLine {
|
|
|
|
|
let mut spans = Vec::new();
|
2022-10-05 09:16:51 -06:00
|
|
|
|
|
|
|
|
let bidi = unicode_bidi::BidiInfo::new(line, None);
|
|
|
|
|
let rtl = if bidi.paragraphs.is_empty() {
|
|
|
|
|
false
|
|
|
|
|
} else {
|
|
|
|
|
assert_eq!(bidi.paragraphs.len(), 1);
|
2022-10-06 13:47:25 -06:00
|
|
|
let para_info = &bidi.paragraphs[0];
|
|
|
|
|
let para_rtl = para_info.level.is_rtl();
|
|
|
|
|
|
|
|
|
|
let paragraph = unicode_bidi::Paragraph::new(&bidi, ¶_info);
|
|
|
|
|
|
|
|
|
|
let mut start = 0;
|
|
|
|
|
let mut span_rtl = para_rtl;
|
|
|
|
|
for i in paragraph.para.range.clone() {
|
|
|
|
|
let next_rtl = paragraph.info.levels[i].is_rtl();
|
|
|
|
|
if span_rtl != next_rtl {
|
|
|
|
|
span_rtl = next_rtl;
|
|
|
|
|
spans.push(self.shape_span(line, start, i, span_rtl));
|
|
|
|
|
start = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
spans.push(self.shape_span(line, start, line.len(), span_rtl));
|
|
|
|
|
|
|
|
|
|
para_rtl
|
2022-10-05 09:16:51 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
FontShapeLine {
|
2022-10-05 11:28:05 -06:00
|
|
|
line_i,
|
2022-10-05 09:16:51 -06:00
|
|
|
rtl,
|
|
|
|
|
spans,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|