mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-20 11:28:30 +08:00
Add String[] as a graphic type for typography (#4141)
* feat: Render List<String> as raw paths in SVG and Vell mode * chore: code review * chore: change the hardcoded layout bounds to parley's * chore: code review * feat: Split text node to text_layer and text_to_vector node * fix: CI fail because of difference in nature of Mac and github action * chore: fix * chore: replace FontStack as it got removed in parley 0.9 * chore: fmt * chore: migrate the rendering as of new resource architechture * chore: add text_layer node to text tool for testing * code review * Make boolean ops support the Text type * chore: Move fallback_font_resource authority from editor to text node * Fix 'Text Layer' node missing font dropdown * Change node doc comments from Vec<T> to T[] * Add migrations from the old Text node to Text -> Text to Vector * Consolidate * Rename the text attributes and reorder tilt to come before max_width/height * Detect legacy Text nodes in the split migration by their trailing separate_glyphs input * Code review * Frame Text layer thumbnails by laying out their text for bounds * Give Text layers click targets and selection outlines via collect_metadata * Route Text tool through Text to Vector with fill, fixing editing-preview placement --------- Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
co-authored by
Keavon Chambers
parent
13abf9fa8c
commit
5f100946f2
@@ -1,19 +1,71 @@
|
||||
use super::TypesettingConfig;
|
||||
use super::path_builder::PathBuilder;
|
||||
use core::cell::RefCell;
|
||||
use core_types::list::List;
|
||||
use glam::DVec2;
|
||||
use graphene_resource::{Resource, ResourceHash};
|
||||
use parley::fontique::{Blob, FamilyId, FontInfo};
|
||||
use parley::{AlignmentOptions, FontContext, Layout, LayoutContext, LineHeight, PositionedLayoutItem, StyleProperty};
|
||||
use parley::{AlignmentOptions, FontContext, GlyphRun, Layout, LayoutContext, LineHeight, PositionedLayoutItem, StyleProperty};
|
||||
use std::collections::HashMap;
|
||||
use vector_types::Vector;
|
||||
|
||||
use super::path_builder::PathBuilder;
|
||||
|
||||
thread_local! {
|
||||
static THREAD_TEXT: RefCell<TextContext> = RefCell::new(TextContext::default());
|
||||
}
|
||||
|
||||
/// Iterates the glyph runs of a laid-out text in reading order, computing each line's last-line alignment correction
|
||||
/// (`x_offset` and per-space `space_extra`) and skipping runs clipped by `max_height`. Shared by the vector shaper and the
|
||||
/// SVG/Vello text renderers so the alignment logic lives in one place.
|
||||
pub fn for_each_styled_glyph_run(layout: &Layout<()>, text: &str, typesetting: TypesettingConfig, mut visit: impl FnMut(&GlyphRun<'_, ()>, f32, f32)) {
|
||||
let alignment_width = typesetting.max_width.map(|w| w as f32).unwrap_or_else(|| layout.full_width());
|
||||
let last_line_correction = typesetting.align.last_line_correction();
|
||||
|
||||
for line in layout.lines() {
|
||||
let range = line.text_range();
|
||||
// Parley always includes a hard-break `\n` as the last byte of the preceding line's range, so the line is at the end of
|
||||
// a paragraph if it's the very last line of the buffer or its text ends with `\n`.
|
||||
let is_last_para_line = range.end == text.len() || text.get(range.clone()).is_some_and(|s| s.ends_with('\n'));
|
||||
|
||||
let mut x_offset = 0.;
|
||||
let mut space_extra = 0.;
|
||||
|
||||
if is_last_para_line && let Some(correction) = last_line_correction {
|
||||
let metrics = line.metrics();
|
||||
let content_advance = metrics.advance - metrics.trailing_whitespace;
|
||||
let free_space = alignment_width - content_advance;
|
||||
|
||||
match correction {
|
||||
parley::Alignment::Center => x_offset = free_space * 0.5,
|
||||
parley::Alignment::Right => x_offset = free_space,
|
||||
parley::Alignment::Justify => {
|
||||
// Exclude trailing-whitespace clusters from the divisor so the redistribution stretches only the internal spaces.
|
||||
// Parley's `trailing_whitespace` is in advance units, not bytes, so we re-derive the byte boundary here to filter cluster ranges.
|
||||
let line_text = text.get(range.clone()).unwrap_or("");
|
||||
let trailing_len = line_text.len() - line_text.trim_end().len();
|
||||
let visible_end_index = range.end - trailing_len;
|
||||
|
||||
let space_count: usize = line
|
||||
.runs()
|
||||
.map(|run| run.clusters().filter(|c| c.is_space_or_nbsp() && c.text_range().start < visible_end_index).count())
|
||||
.sum();
|
||||
if space_count > 0 {
|
||||
space_extra = free_space / space_count as f32;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
for item in line.items() {
|
||||
if let PositionedLayoutItem::GlyphRun(glyph_run) = item
|
||||
&& typesetting.max_height.filter(|&max_height| glyph_run.baseline() > max_height as f32).is_none()
|
||||
{
|
||||
visit(&glyph_run, x_offset, space_extra);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Unified thread-local text processing context that combines font and layout management
|
||||
/// for efficient text rendering operations.
|
||||
#[derive(Default)]
|
||||
@@ -54,14 +106,14 @@ impl TextContext {
|
||||
}
|
||||
|
||||
/// Create a text layout from the given font resource and typesetting configuration.
|
||||
fn layout_text(&mut self, text: &str, font: &Resource, typesetting: TypesettingConfig) -> Option<Layout<()>> {
|
||||
pub fn layout_text(&mut self, text: &str, font: &Resource, typesetting: TypesettingConfig) -> Option<Layout<()>> {
|
||||
let (font_family, font_info) = self.get_font_info(font)?;
|
||||
|
||||
const DISPLAY_SCALE: f32 = 1.;
|
||||
let mut builder = self.layout_context.ranged_builder(&mut self.font_context, text, DISPLAY_SCALE, false);
|
||||
|
||||
builder.push_default(StyleProperty::FontSize(typesetting.font_size as f32));
|
||||
builder.push_default(StyleProperty::LetterSpacing(typesetting.character_spacing as f32));
|
||||
builder.push_default(StyleProperty::LetterSpacing(typesetting.letter_spacing as f32));
|
||||
builder.push_default(StyleProperty::FontFamily(parley::FontFamily::Single(parley::FontFamilyName::Named(std::borrow::Cow::Owned(
|
||||
font_family,
|
||||
)))));
|
||||
@@ -100,53 +152,11 @@ impl TextContext {
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
let alignment_width = typesetting.max_width.map(|w| w as f32).unwrap_or_else(|| layout.full_width());
|
||||
let last_line_correction = typesetting.align.last_line_correction();
|
||||
|
||||
let mut path_builder = PathBuilder::new(per_glyph_items, layout.scale() as f64, text_frame_size, first_glyph_offset);
|
||||
|
||||
for line in layout.lines() {
|
||||
let range = line.text_range();
|
||||
// Parley always includes a hard-break `\n` as the last byte of the preceding line's range, so the line
|
||||
// is at the end of a paragraph if it's the very last line of the buffer or its text ends with `\n`.
|
||||
let is_last_para_line = range.end == text.len() || text.get(range.clone()).is_some_and(|s| s.ends_with('\n'));
|
||||
|
||||
let (x_offset, space_extra) = if let (true, Some(correction)) = (is_last_para_line, last_line_correction) {
|
||||
let metrics = line.metrics();
|
||||
let content_advance = metrics.advance - metrics.trailing_whitespace;
|
||||
let free_space = alignment_width - content_advance;
|
||||
|
||||
match correction {
|
||||
parley::Alignment::Center => (free_space * 0.5, 0.),
|
||||
parley::Alignment::Right => (free_space, 0.),
|
||||
parley::Alignment::Justify => {
|
||||
// Exclude trailing-whitespace clusters from the divisor so the redistribution stretches only the internal spaces.
|
||||
// Parley's `trailing_whitespace` is in advance units, not bytes, so we re-derive the byte boundary here to filter cluster ranges.
|
||||
let line_text = text.get(range.clone()).unwrap_or("");
|
||||
let trailing_len = line_text.len() - line_text.trim_end().len();
|
||||
let visible_end_index = range.end - trailing_len;
|
||||
|
||||
let space_count: usize = line
|
||||
.runs()
|
||||
.map(|run| run.clusters().filter(|c| c.is_space_or_nbsp() && c.text_range().start < visible_end_index).count())
|
||||
.sum();
|
||||
let extra = if space_count > 0 { free_space / space_count as f32 } else { 0. };
|
||||
(0., extra)
|
||||
}
|
||||
_ => (0., 0.),
|
||||
}
|
||||
} else {
|
||||
(0., 0.)
|
||||
};
|
||||
|
||||
for item in line.items() {
|
||||
if let PositionedLayoutItem::GlyphRun(glyph_run) = item
|
||||
&& typesetting.max_height.filter(|&max_height| glyph_run.baseline() > max_height as f32).is_none()
|
||||
{
|
||||
path_builder.render_glyph_run(&glyph_run, typesetting.tilt, per_glyph_items, x_offset, space_extra);
|
||||
}
|
||||
}
|
||||
}
|
||||
for_each_styled_glyph_run(&layout, text, typesetting, |glyph_run, x_offset, space_extra| {
|
||||
path_builder.render_glyph_run(glyph_run, typesetting.letter_tilt, per_glyph_items, x_offset, space_extra);
|
||||
});
|
||||
|
||||
path_builder.finalize()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user