|
| 1 | +// Copyright © SixtyFPS GmbH <[email protected]> |
| 2 | +// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 |
| 3 | + |
| 4 | +pub use parley; |
| 5 | + |
| 6 | +use std::boxed::Box; |
| 7 | +use std::cell::RefCell; |
| 8 | + |
| 9 | +use crate::{ |
| 10 | + graphics::FontRequest, |
| 11 | + items::TextStrokeStyle, |
| 12 | + lengths::{LogicalLength, ScaleFactor}, |
| 13 | + textlayout::{TextHorizontalAlignment, TextOverflow, TextVerticalAlignment, TextWrap}, |
| 14 | +}; |
| 15 | +use i_slint_common::sharedfontique; |
| 16 | + |
| 17 | +pub const DEFAULT_FONT_SIZE: LogicalLength = LogicalLength::new(12.); |
| 18 | + |
| 19 | +struct Contexts { |
| 20 | + layout: parley::LayoutContext<Brush>, |
| 21 | + font: parley::FontContext, |
| 22 | +} |
| 23 | + |
| 24 | +impl Default for Contexts { |
| 25 | + fn default() -> Self { |
| 26 | + Self { |
| 27 | + font: parley::FontContext { |
| 28 | + collection: sharedfontique::COLLECTION.inner.clone(), |
| 29 | + source_cache: sharedfontique::COLLECTION.source_cache.clone(), |
| 30 | + }, |
| 31 | + layout: Default::default(), |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +std::thread_local! { |
| 37 | + static CONTEXTS: RefCell<Box<Contexts>> = Default::default(); |
| 38 | +} |
| 39 | + |
| 40 | +#[derive(Debug, Default, PartialEq, Clone, Copy)] |
| 41 | +pub struct Brush { |
| 42 | + pub stroke: Option<TextStrokeStyle>, |
| 43 | +} |
| 44 | + |
| 45 | +pub struct LayoutOptions { |
| 46 | + pub max_width: Option<LogicalLength>, |
| 47 | + pub max_height: Option<LogicalLength>, |
| 48 | + pub horizontal_align: TextHorizontalAlignment, |
| 49 | + pub vertical_align: TextVerticalAlignment, |
| 50 | + pub stroke: Option<TextStrokeStyle>, |
| 51 | + pub font_request: Option<FontRequest>, |
| 52 | + pub text_wrap: TextWrap, |
| 53 | + pub text_overflow: TextOverflow, |
| 54 | +} |
| 55 | + |
| 56 | +impl Default for LayoutOptions { |
| 57 | + fn default() -> Self { |
| 58 | + Self { |
| 59 | + max_width: None, |
| 60 | + max_height: None, |
| 61 | + horizontal_align: TextHorizontalAlignment::Left, |
| 62 | + vertical_align: TextVerticalAlignment::Top, |
| 63 | + stroke: None, |
| 64 | + font_request: None, |
| 65 | + text_wrap: TextWrap::WordWrap, |
| 66 | + text_overflow: TextOverflow::Clip, |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +pub fn layout(text: &str, scale_factor: ScaleFactor, options: LayoutOptions) -> Layout { |
| 72 | + let max_physical_width = options.max_width.map(|max_width| (max_width * scale_factor).get()); |
| 73 | + let max_physical_height = options.max_height.map(|max_height| max_height * scale_factor); |
| 74 | + let pixel_size = options |
| 75 | + .font_request |
| 76 | + .as_ref() |
| 77 | + .and_then(|font_request| font_request.pixel_size) |
| 78 | + .unwrap_or(DEFAULT_FONT_SIZE); |
| 79 | + |
| 80 | + let mut layout = CONTEXTS.with_borrow_mut(move |contexts| { |
| 81 | + let mut builder = |
| 82 | + contexts.layout.ranged_builder(&mut contexts.font, text, scale_factor.get(), true); |
| 83 | + if let Some(ref font_request) = options.font_request { |
| 84 | + if let Some(family) = &font_request.family { |
| 85 | + builder.push_default(parley::StyleProperty::FontStack( |
| 86 | + parley::style::FontStack::Single(parley::style::FontFamily::Named( |
| 87 | + family.as_str().into(), |
| 88 | + )), |
| 89 | + )); |
| 90 | + } |
| 91 | + if let Some(weight) = font_request.weight { |
| 92 | + builder.push_default(parley::StyleProperty::FontWeight( |
| 93 | + parley::style::FontWeight::new(weight as f32), |
| 94 | + )); |
| 95 | + } |
| 96 | + if let Some(letter_spacing) = font_request.letter_spacing { |
| 97 | + builder.push_default(parley::StyleProperty::LetterSpacing(letter_spacing.get())); |
| 98 | + } |
| 99 | + builder.push_default(parley::StyleProperty::FontStyle(if font_request.italic { |
| 100 | + parley::style::FontStyle::Italic |
| 101 | + } else { |
| 102 | + parley::style::FontStyle::Normal |
| 103 | + })); |
| 104 | + } |
| 105 | + builder.push_default(parley::StyleProperty::FontSize(pixel_size.get())); |
| 106 | + builder.push_default(parley::StyleProperty::WordBreak(match options.text_wrap { |
| 107 | + TextWrap::NoWrap => parley::style::WordBreakStrength::KeepAll, |
| 108 | + TextWrap::WordWrap => parley::style::WordBreakStrength::Normal, |
| 109 | + TextWrap::CharWrap => parley::style::WordBreakStrength::BreakAll, |
| 110 | + })); |
| 111 | + if options.text_overflow == TextOverflow::Elide { |
| 112 | + todo!(); |
| 113 | + } |
| 114 | + |
| 115 | + builder.push_default(parley::StyleProperty::Brush(Brush { stroke: options.stroke })); |
| 116 | + |
| 117 | + builder.build(text) |
| 118 | + }); |
| 119 | + |
| 120 | + layout.break_all_lines(max_physical_width); |
| 121 | + layout.align( |
| 122 | + max_physical_width, |
| 123 | + match options.horizontal_align { |
| 124 | + TextHorizontalAlignment::Left => parley::Alignment::Left, |
| 125 | + TextHorizontalAlignment::Center => parley::Alignment::Middle, |
| 126 | + TextHorizontalAlignment::Right => parley::Alignment::Right, |
| 127 | + }, |
| 128 | + parley::AlignmentOptions::default(), |
| 129 | + ); |
| 130 | + |
| 131 | + let y_offset = match (max_physical_height, options.vertical_align) { |
| 132 | + (Some(max_height), TextVerticalAlignment::Center) => { |
| 133 | + (max_height.get() - layout.height()) / 2.0 |
| 134 | + } |
| 135 | + (Some(max_height), TextVerticalAlignment::Bottom) => max_height.get() - layout.height(), |
| 136 | + (None, _) | (Some(_), TextVerticalAlignment::Top) => 0.0, |
| 137 | + }; |
| 138 | + |
| 139 | + Layout { inner: layout, y_offset } |
| 140 | +} |
| 141 | + |
| 142 | +pub struct Layout { |
| 143 | + inner: parley::Layout<Brush>, |
| 144 | + pub y_offset: f32, |
| 145 | +} |
| 146 | + |
| 147 | +impl std::ops::Deref for Layout { |
| 148 | + type Target = parley::Layout<Brush>; |
| 149 | + |
| 150 | + fn deref(&self) -> &Self::Target { |
| 151 | + &self.inner |
| 152 | + } |
| 153 | +} |
0 commit comments