Skip to content

Commit b4eb01f

Browse files
committed
New Typography type
1 parent 485152b commit b4eb01f

13 files changed

Lines changed: 350 additions & 12 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

editor/src/messages/portfolio/document/data_panel/data_panel_message_handler.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use graphene_std::gradient::GradientStops;
1212
use graphene_std::memo::IORecord;
1313
use graphene_std::raster_types::{CPU, GPU, Raster};
1414
use graphene_std::table::Table;
15+
use graphene_std::text::Typography;
1516
use graphene_std::vector::Vector;
1617
use graphene_std::vector::style::{Fill, FillChoice};
1718
use graphene_std::{Artboard, Graphic};
@@ -266,6 +267,7 @@ impl TableRowLayout for Graphic {
266267
Self::RasterGPU(table) => table.identifier(),
267268
Self::Color(table) => table.identifier(),
268269
Self::Gradient(table) => table.identifier(),
270+
Self::Typography(table) => table.identifier(),
269271
}
270272
}
271273
// Don't put a breadcrumb for Graphic
@@ -280,6 +282,7 @@ impl TableRowLayout for Graphic {
280282
Self::RasterGPU(table) => table.layout_with_breadcrumb(data),
281283
Self::Color(table) => table.layout_with_breadcrumb(data),
282284
Self::Gradient(table) => table.layout_with_breadcrumb(data),
285+
Self::Typography(table) => table.layout_with_breadcrumb(data),
283286
}
284287
}
285288
}
@@ -504,6 +507,21 @@ impl TableRowLayout for GradientStops {
504507
}
505508
}
506509

510+
impl TableRowLayout for Typography {
511+
fn type_name() -> &'static str {
512+
"Typography"
513+
}
514+
fn identifier(&self) -> String {
515+
"Typography".to_string()
516+
}
517+
fn element_widget(&self, _index: usize) -> WidgetHolder {
518+
TextLabel::new("Not supported").widget_holder()
519+
}
520+
fn element_page(&self, _data: &mut LayoutData) -> Vec<LayoutGroup> {
521+
vec![LayoutGroup::Row { widgets: Vec::new() }]
522+
}
523+
}
524+
507525
impl TableRowLayout for f64 {
508526
fn type_name() -> &'static str {
509527
"Number (f64)"

editor/src/messages/portfolio/document/overlays/utility_types_vello.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use core::borrow::Borrow;
99
use core::f64::consts::{FRAC_PI_2, PI, TAU};
1010
use glam::{DAffine2, DVec2};
1111
use graphene_std::Color;
12+
use graphene_std::consts::SOURCE_SANS_FONT_DATA;
1213
use graphene_std::math::quad::Quad;
1314
use graphene_std::subpath::{self, Subpath};
1415
use graphene_std::table::Table;
@@ -1021,10 +1022,7 @@ impl OverlayContextInternal {
10211022
align: TextAlign::Left,
10221023
};
10231024

1024-
// Load Source Sans Pro font data
1025-
// TODO: Grab this from the node_modules folder (either with `include_bytes!` or ideally at runtime) instead of checking the font file into the repo.
1026-
// TODO: And maybe use the WOFF2 version (if it's supported) for its smaller, compressed file size.
1027-
const FONT_DATA: &[u8] = include_bytes!("source-sans-pro-regular.ttf");
1025+
const FONT_DATA: &[u8] = SOURCE_SANS_FONT_DATA;
10281026
let font_blob = Some(load_font(FONT_DATA));
10291027

10301028
// Convert text to paths and calculate actual bounds
@@ -1048,10 +1046,7 @@ impl OverlayContextInternal {
10481046
align: TextAlign::Left, // We'll handle alignment manually via pivot
10491047
};
10501048

1051-
// Load Source Sans Pro font data
1052-
// TODO: Grab this from the node_modules folder (either with `include_bytes!` or ideally at runtime) instead of checking the font file into the repo.
1053-
// TODO: And maybe use the WOFF2 version (if it's supported) for its smaller, compressed file size.
1054-
const FONT_DATA: &[u8] = include_bytes!("source-sans-pro-regular.ttf");
1049+
const FONT_DATA: &[u8] = SOURCE_SANS_FONT_DATA;
10551050
let font_blob = Some(load_font(FONT_DATA));
10561051

10571052
// Convert text to vector paths using the existing text system

node-graph/gcore/src/bounds.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Color, gradient::GradientStops};
1+
use crate::{Color, gradient::GradientStops, text::Typography};
22
use glam::{DAffine2, DVec2};
33

44
#[derive(Clone, Copy, Default, Debug, PartialEq)]
@@ -38,3 +38,9 @@ impl BoundingBox for GradientStops {
3838
RenderBoundingBox::Infinite
3939
}
4040
}
41+
impl BoundingBox for Typography {
42+
fn bounding_box(&self, transform: DAffine2, _include_stroke: bool) -> RenderBoundingBox {
43+
let bbox = DVec2::new(self.layout.full_width() as f64, self.layout.height() as f64);
44+
RenderBoundingBox::Rectangle([transform.translation, transform.transform_point2(bbox)])
45+
}
46+
}

node-graph/gcore/src/consts.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ pub const LAYER_OUTLINE_STROKE_WEIGHT: f64 = 0.5;
77
// Fonts
88
pub const DEFAULT_FONT_FAMILY: &str = "Cabin";
99
pub const DEFAULT_FONT_STYLE: &str = "Regular (400)";
10+
11+
// Load Source Sans Pro font data
12+
// TODO: Grab this from the node_modules folder (either with `include_bytes!` or ideally at runtime) instead of checking the font file into the repo.
13+
// TODO: And maybe use the WOFF2 version (if it's supported) for its smaller, compressed file size.
14+
pub const SOURCE_SANS_FONT_DATA: &[u8] = include_bytes!("text/source-sans-pro-regular.ttf");
15+
pub const SOURCE_SANS_FONT_FAMILY: &str = "Source Sans Pro";
16+
pub const SOURCE_SANS_FONT_STYLE: &str = "Regular (400)";

node-graph/gcore/src/graphic.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::bounds::{BoundingBox, RenderBoundingBox};
33
use crate::gradient::GradientStops;
44
use crate::raster_types::{CPU, GPU, Raster};
55
use crate::table::{Table, TableRow};
6+
use crate::text::Typography;
67
use crate::uuid::NodeId;
78
use crate::vector::Vector;
89
use crate::{Artboard, Color, Ctx};
@@ -11,14 +12,34 @@ use glam::{DAffine2, DVec2};
1112
use std::hash::Hash;
1213

1314
/// The possible forms of graphical content that can be rendered by the Render node into either an image or SVG syntax.
14-
#[derive(Clone, Debug, Hash, PartialEq, DynAny, serde::Serialize, serde::Deserialize)]
15+
#[derive(Clone, Debug, Hash, PartialEq, DynAny)]
1516
pub enum Graphic {
1617
Graphic(Table<Graphic>),
1718
Vector(Table<Vector>),
1819
RasterCPU(Table<Raster<CPU>>),
1920
RasterGPU(Table<Raster<GPU>>),
2021
Color(Table<Color>),
2122
Gradient(Table<GradientStops>),
23+
Typography(Table<Typography>),
24+
}
25+
26+
impl serde::Serialize for Graphic {
27+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
28+
where
29+
S: serde::Serializer,
30+
{
31+
let default: Table<Graphic> = Table::new();
32+
default.serialize(serializer)
33+
}
34+
}
35+
36+
impl<'de> serde::Deserialize<'de> for Graphic {
37+
fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error>
38+
where
39+
D: serde::Deserializer<'de>,
40+
{
41+
Ok(Graphic::Graphic(Table::new()))
42+
}
2243
}
2344

2445
impl Default for Graphic {
@@ -232,6 +253,7 @@ impl Graphic {
232253
Graphic::RasterGPU(raster) => raster.iter().all(|row| row.alpha_blending.clip),
233254
Graphic::Color(color) => color.iter().all(|row| row.alpha_blending.clip),
234255
Graphic::Gradient(gradient) => gradient.iter().all(|row| row.alpha_blending.clip),
256+
Graphic::Typography(typography) => typography.iter().all(|row| row.alpha_blending.clip),
235257
}
236258
}
237259

@@ -256,6 +278,7 @@ impl BoundingBox for Graphic {
256278
Graphic::Graphic(graphic) => graphic.bounding_box(transform, include_stroke),
257279
Graphic::Color(color) => color.bounding_box(transform, include_stroke),
258280
Graphic::Gradient(gradient) => gradient.bounding_box(transform, include_stroke),
281+
Graphic::Typography(typography) => typography.bounding_box(transform, include_stroke),
259282
}
260283
}
261284
}

node-graph/gcore/src/render_complexity.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::gradient::GradientStops;
22
use crate::raster_types::{CPU, GPU, Raster};
33
use crate::table::Table;
4+
use crate::text::Typography;
45
use crate::vector::Vector;
56
use crate::{Artboard, Color, Graphic};
67

@@ -31,6 +32,7 @@ impl RenderComplexity for Graphic {
3132
Self::RasterGPU(table) => table.render_complexity(),
3233
Self::Color(table) => table.render_complexity(),
3334
Self::Gradient(table) => table.render_complexity(),
35+
Self::Typography(table) => table.render_complexity(),
3436
}
3537
}
3638
}
@@ -65,3 +67,9 @@ impl RenderComplexity for GradientStops {
6567
1
6668
}
6769
}
70+
71+
impl RenderComplexity for Typography {
72+
fn render_complexity(&self) -> usize {
73+
1
74+
}
75+
}

node-graph/gcore/src/text.rs

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
mod font_cache;
22
mod to_path;
33

4+
use std::{
5+
borrow::Cow,
6+
collections::{HashMap, hash_map::Entry},
7+
fmt,
8+
sync::{Arc, Mutex},
9+
};
10+
411
use dyn_any::DynAny;
512
pub use font_cache::*;
13+
use graphene_core_shaders::color::Color;
14+
use parley::{Layout, StyleProperty};
15+
use rustc_hash::FxBuildHasher;
16+
use std::hash::BuildHasher;
17+
use std::hash::{Hash, Hasher};
618
pub use to_path::*;
719

20+
use crate::{consts::*, table::Table, vector::Vector};
21+
822
/// Alignment of lines of type within a text block.
923
#[repr(C)]
1024
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize, Hash, DynAny, specta::Type, node_macro::ChoiceType)]
@@ -29,3 +43,139 @@ impl From<TextAlign> for parley::Alignment {
2943
}
3044
}
3145
}
46+
47+
#[derive(Clone, DynAny)]
48+
pub struct Typography {
49+
pub layout: Layout<()>,
50+
pub font_family: String,
51+
pub color: Color,
52+
pub stroke: Option<(Color, f64)>,
53+
}
54+
55+
impl fmt::Debug for Typography {
56+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57+
f.debug_struct("Typography")
58+
.field("font_family", &self.font_family)
59+
.field("color", &self.color)
60+
.field("stroke", &self.stroke)
61+
.finish()
62+
}
63+
}
64+
65+
impl PartialEq for Typography {
66+
fn eq(&self, _other: &Self) -> bool {
67+
true
68+
}
69+
}
70+
71+
impl Hash for Typography {
72+
fn hash<H: Hasher>(&self, state: &mut H) {
73+
self.layout.len().hash(state);
74+
}
75+
}
76+
77+
impl Typography {
78+
pub fn to_vector(&self) -> Table<Vector> {
79+
// To implement this function, a clone of the `NewFontCacheWrapper` must be included in the typography data type
80+
Table::new()
81+
}
82+
}
83+
84+
#[derive(Clone)]
85+
pub struct NewFontCacheWrapper(pub Arc<Mutex<NewFontCache>>);
86+
87+
impl fmt::Debug for NewFontCacheWrapper {
88+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89+
f.debug_struct("font cache").finish()
90+
}
91+
}
92+
93+
impl PartialEq for NewFontCacheWrapper {
94+
fn eq(&self, _other: &Self) -> bool {
95+
log::error!("Font cache should not be compared");
96+
false
97+
}
98+
}
99+
100+
unsafe impl dyn_any::StaticType for NewFontCacheWrapper {
101+
type Static = NewFontCacheWrapper;
102+
}
103+
104+
pub struct NewFontCache {
105+
pub font_context: parley::FontContext,
106+
pub layout_context: parley::LayoutContext<()>,
107+
pub font_mapping: HashMap<Font, (String, parley::fontique::FontInfo)>,
108+
pub hash: u64,
109+
}
110+
111+
impl NewFontCache {
112+
pub fn new() -> Self {
113+
let mut new = NewFontCache {
114+
font_context: parley::FontContext::new(),
115+
layout_context: parley::LayoutContext::new(),
116+
font_mapping: HashMap::new(),
117+
hash: 0,
118+
};
119+
120+
let source_sans_font = Font::new(SOURCE_SANS_FONT_FAMILY.to_string(), SOURCE_SANS_FONT_STYLE.to_string());
121+
new.register_font(source_sans_font, SOURCE_SANS_FONT_DATA.to_vec());
122+
new
123+
}
124+
125+
pub fn register_font(&mut self, font: Font, data: Vec<u8>) {
126+
match self.font_mapping.entry(font) {
127+
Entry::Occupied(occupied_entry) => {
128+
log::error!("Trying to register font that already is added: {:?}", occupied_entry.key());
129+
}
130+
Entry::Vacant(vacant_entry) => {
131+
let registered_font = self.font_context.collection.register_fonts(parley::fontique::Blob::from(data), None);
132+
if registered_font.len() > 1 {
133+
log::error!("Registered multiple fonts for {:?}. Only the first is accessible", vacant_entry.key());
134+
};
135+
match registered_font.into_iter().next() {
136+
Some((family_id, font_info)) => {
137+
let Some(family_name) = self.font_context.collection.family_name(family_id) else {
138+
log::error!("Could not get family name for font: {:?}", vacant_entry.key());
139+
return;
140+
};
141+
let Some(font_info) = font_info.into_iter().next() else {
142+
log::error!("Could not get font info for font: {:?}", vacant_entry.key());
143+
return;
144+
};
145+
// Hash the Font for a unique id and add it to the cached hash
146+
let hash_value = FxBuildHasher.hash_one(vacant_entry.key());
147+
self.hash = self.hash.wrapping_add(hash_value);
148+
149+
vacant_entry.insert((family_name.to_string(), font_info));
150+
}
151+
None => log::error!("Could not register font for {:?}", vacant_entry.key()),
152+
}
153+
}
154+
}
155+
}
156+
157+
pub fn generate_typography(&mut self, font: &Font, font_size: f32, text: &str) -> Option<Typography> {
158+
let Some((font_family, font_info)) = self.font_mapping.get(font) else {
159+
log::error!("Font not loaded: {:?}", font);
160+
return None;
161+
};
162+
let font_family = font_family.to_string();
163+
164+
let mut builder = self.layout_context.ranged_builder(&mut self.font_context, text, 1., false);
165+
166+
builder.push_default(StyleProperty::FontStack(parley::FontStack::Single(parley::FontFamily::Named(Cow::Owned(font_family.clone())))));
167+
builder.push_default(StyleProperty::FontSize(font_size));
168+
builder.push_default(StyleProperty::FontWeight(font_info.weight()));
169+
builder.push_default(StyleProperty::FontStyle(font_info.style()));
170+
builder.push_default(StyleProperty::FontWidth(font_info.width()));
171+
172+
let mut layout: Layout<()> = builder.build(text);
173+
layout.break_all_lines(None);
174+
Some(Typography {
175+
layout,
176+
font_family,
177+
color: Color::BLACK,
178+
stroke: None,
179+
})
180+
}
181+
}

editor/src/messages/portfolio/document/overlays/source-sans-pro-regular.ttf renamed to node-graph/gcore/src/text/source-sans-pro-regular.ttf

File renamed without changes.

node-graph/gpath-bool/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ fn flatten_vector(graphic_table: &Table<Graphic>) -> Table<Vector> {
318318
}
319319
})
320320
.collect::<Vec<_>>(),
321+
Graphic::Typography(typography) => typography.into_iter().flat_map(|row| row.element.to_vector()).collect::<Vec<_>>(),
321322
}
322323
})
323324
.collect()

0 commit comments

Comments
 (0)