mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-05 19:12:08 -10:00
87 lines
2.9 KiB
Rust
87 lines
2.9 KiB
Rust
use crate::metrics::{ERROR_LINE_HEIGHT, ERROR_MAX_WIDTH, GRID_SIZE, TEXT_SIZE};
|
|
use core_types::list::{ATTR_GEOMETRY_CACHE, List};
|
|
use glam::DVec2;
|
|
use graphic_types::Vector;
|
|
use std::collections::HashMap;
|
|
use std::collections::hash_map::Entry;
|
|
use text_nodes::{FALLBACK_FONT_RESOURCE, TextToVectorError, TypesettingConfig};
|
|
use vector_types::vector::GeometryCache;
|
|
|
|
#[derive(Debug)]
|
|
pub struct Label {
|
|
pub outline: List<Vector>,
|
|
pub width: f64,
|
|
}
|
|
|
|
#[derive(Debug, Default)]
|
|
pub struct Labels(HashMap<(String, u64), Label>);
|
|
|
|
impl Labels {
|
|
pub fn label(&mut self, text: &str, max_width: f64) -> Result<&Label, TextToVectorError> {
|
|
match self.0.entry((text.to_string(), max_width.to_bits())) {
|
|
Entry::Occupied(label) => Ok(label.into_mut()),
|
|
Entry::Vacant(label) => {
|
|
let text = ellipsized(text, max_width);
|
|
let (outline, width) = (outline(&text)?, label_width(&text));
|
|
Ok(label.insert(Label { outline, width }))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn label_width(text: &str) -> f64 {
|
|
text_nodes::bounding_box(text, &FALLBACK_FONT_RESOURCE, typesetting(), false).x
|
|
}
|
|
|
|
fn ellipsized(text: &str, max_width: f64) -> String {
|
|
if label_width(text) <= max_width {
|
|
return text.to_string();
|
|
}
|
|
let cut = |end: usize| format!("{}…", &text[..end]);
|
|
let ends: Vec<usize> = text.char_indices().map(|(end, _)| end).collect();
|
|
let fitting = ends.partition_point(|&end| label_width(&cut(end)) <= max_width);
|
|
cut(ends[fitting.saturating_sub(1)])
|
|
}
|
|
|
|
fn outline(text: &str) -> Result<List<Vector>, TextToVectorError> {
|
|
text_nodes::to_path(text, &FALLBACK_FONT_RESOURCE, typesetting(), false).map(with_geometry_caches)
|
|
}
|
|
|
|
fn with_geometry_caches(mut outline: List<Vector>) -> List<Vector> {
|
|
for index in 0..outline.len() {
|
|
outline.set_attribute(ATTR_GEOMETRY_CACHE, index, GeometryCache::default());
|
|
}
|
|
outline
|
|
}
|
|
|
|
pub fn paragraph_size(text: &str, line_height: f64, max_width: f64) -> DVec2 {
|
|
let unwrapped = text_nodes::bounding_box(text, &FALLBACK_FONT_RESOURCE, paragraph_typesetting(line_height, None), true);
|
|
let wrapped = text_nodes::bounding_box(text, &FALLBACK_FONT_RESOURCE, paragraph_typesetting(line_height, Some(max_width)), true);
|
|
DVec2::new(unwrapped.x.min(max_width), wrapped.y)
|
|
}
|
|
|
|
pub fn error_text_size(text: &str) -> DVec2 {
|
|
paragraph_size(text, ERROR_LINE_HEIGHT, ERROR_MAX_WIDTH)
|
|
}
|
|
|
|
pub fn paragraph_outline(text: &str, line_height: f64, max_width: f64) -> Result<List<Vector>, TextToVectorError> {
|
|
text_nodes::to_path(text, &FALLBACK_FONT_RESOURCE, paragraph_typesetting(line_height, Some(max_width)), false).map(with_geometry_caches)
|
|
}
|
|
|
|
fn typesetting() -> TypesettingConfig {
|
|
TypesettingConfig {
|
|
font_size: TEXT_SIZE,
|
|
line_height_ratio: GRID_SIZE / TEXT_SIZE,
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
fn paragraph_typesetting(line_height: f64, max_width: Option<f64>) -> TypesettingConfig {
|
|
TypesettingConfig {
|
|
font_size: TEXT_SIZE,
|
|
line_height_ratio: line_height / TEXT_SIZE,
|
|
max_width,
|
|
..Default::default()
|
|
}
|
|
}
|