grafia 0.7.0

Interactive 2D cubic Bezier contour, glyph, and vector canvas engine for egui with variable-font master compatibility and stylus brush support.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Layout and rendering tokens loaded from JSON databases.

pub mod archetypes_tokens;
pub mod cursor_tokens;
pub mod v020_tokens;
pub mod v030_tokens;
pub mod v040_tokens;
pub mod v050_tokens;
pub mod v060_tokens;
pub mod v070_tokens;
pub use archetypes_tokens::{
    load_archetype_preset, ArchetypeContourPreset, ArchetypeGlyphPreset, ArchetypeNodePreset,
    GlyphAGoldenMaster, GlyphArchetype, GlyphPointToken,
};
pub use cursor_tokens::*;
pub use v020_tokens::*;
pub use v030_tokens::*;
pub use v040_tokens::*;
pub use v050_tokens::*;
pub use v060_tokens::*;
pub use v070_tokens::*;

use serde::{Deserialize, Serialize};

/// Parses a hex color string into an `egui::Color32`.
///
/// Supports 3-digit (`#RGB`), 4-digit (`#RGBA`), 6-digit (`#RRGGBB`),
/// and 8-digit (`#RRGGBBAA`) hex representations, with or without a leading `#`.
#[must_use]
pub fn hex_to_color32(hex: &str) -> egui::Color32 {
    let clean = hex.trim().trim_start_matches('#');
    if !(clean.is_ascii() && clean.bytes().all(|b| b.is_ascii_hexdigit())) {
        return egui::Color32::from_rgb(0, 0, 0);
    }
    let parse_pair = |idx: usize| {
        if clean.len() <= 4 {
            u8::from_str_radix(&clean[idx..=idx].repeat(2), 16).unwrap_or(0)
        } else {
            u8::from_str_radix(&clean[idx * 2..idx * 2 + 2], 16).unwrap_or(0)
        }
    };
    match clean.len() {
        3 | 6 => egui::Color32::from_rgb(parse_pair(0), parse_pair(1), parse_pair(2)),
        4 | 8 => egui::Color32::from_rgba_unmultiplied(
            parse_pair(0),
            parse_pair(1),
            parse_pair(2),
            parse_pair(3),
        ),
        _ => egui::Color32::from_rgb(0, 0, 0),
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct EditCanvasTokens {
    pub node_size_dp: f32,
    pub bcp_handle_size_dp: f32,
    pub bcp_line_width_dp: f32,
    pub contour_stroke_width_dp: f32,
    pub corner_node_color: String,
    pub smooth_node_color: String,
    pub bcp_handle_color: String,
    pub bcp_line_color: String,
    pub selected_glow_color: String,
    pub rubberband_fill_color: String,
    pub rubberband_stroke_color: String,
    pub rubberband_stroke_width_dp: f32,
    #[serde(default = "default_local_guide_rgba")]
    pub local_guide_color: RgbaColorToken,
    #[serde(default = "default_global_guide_rgba")]
    pub global_guide_color: RgbaColorToken,
    #[serde(default = "default_alignment_zone_rgba")]
    pub alignment_zone_fill: RgbaColorToken,
    #[serde(default = "default_snap_indicator_rgba")]
    pub snap_indicator_color: RgbaColorToken,
    #[serde(default = "default_grid_main_rgba")]
    pub grid_main_color: RgbaColorToken,
    #[serde(default = "default_grid_sub_rgba")]
    pub grid_sub_color: RgbaColorToken,
}

const fn default_local_guide_rgba() -> RgbaColorToken {
    LOCAL_GUIDE_COLOR_DEFAULT
}
const fn default_global_guide_rgba() -> RgbaColorToken {
    GLOBAL_GUIDE_COLOR_DEFAULT
}
const fn default_alignment_zone_rgba() -> RgbaColorToken {
    ALIGNMENT_ZONE_FILL_DEFAULT
}
const fn default_snap_indicator_rgba() -> RgbaColorToken {
    SNAP_INDICATOR_COLOR_DEFAULT
}
const fn default_grid_main_rgba() -> RgbaColorToken {
    GRID_MAIN_COLOR_DEFAULT
}
const fn default_grid_sub_rgba() -> RgbaColorToken {
    GRID_SUB_COLOR_DEFAULT
}

impl EditCanvasTokens {
    #[must_use]
    pub fn corner_node_color32(&self) -> egui::Color32 {
        hex_to_color32(&self.corner_node_color)
    }

    #[must_use]
    pub fn smooth_node_color32(&self) -> egui::Color32 {
        hex_to_color32(&self.smooth_node_color)
    }

    #[must_use]
    pub fn bcp_handle_color32(&self) -> egui::Color32 {
        hex_to_color32(&self.bcp_handle_color)
    }

    #[must_use]
    pub fn bcp_line_color32(&self) -> egui::Color32 {
        hex_to_color32(&self.bcp_line_color)
    }

    #[must_use]
    pub fn selected_glow_color32(&self) -> egui::Color32 {
        hex_to_color32(&self.selected_glow_color)
    }

    #[must_use]
    pub fn rubberband_fill_color32(&self) -> egui::Color32 {
        hex_to_color32(&self.rubberband_fill_color)
    }

    #[must_use]
    pub fn rubberband_stroke_color32(&self) -> egui::Color32 {
        hex_to_color32(&self.rubberband_stroke_color)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PreviewCanvasTokens {
    pub fill_color: String,
    pub baseline_color: String,
    pub baseline_stroke_width_dp: f32,
}

impl PreviewCanvasTokens {
    #[must_use]
    pub fn fill_color32(&self) -> egui::Color32 {
        hex_to_color32(&self.fill_color)
    }

    #[must_use]
    pub fn baseline_color32(&self) -> egui::Color32 {
        hex_to_color32(&self.baseline_color)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ShapeCanvasTokens {
    pub row_height_dp: f32,
    pub header_height_dp: f32,
    pub thumbnail_width_dp: f32,
    pub thumbnail_height_dp: f32,
    pub thumbnail_padding_dp: f32,
    pub accent_selection_color: String,
    pub selection_corner_radius_dp: f32,
    pub text_active_color: String,
    pub text_inactive_color: String,
    pub winding_icon_size_dp: f32,
}

impl ShapeCanvasTokens {
    #[must_use]
    pub fn accent_selection_color32(&self) -> egui::Color32 {
        hex_to_color32(&self.accent_selection_color)
    }

    #[must_use]
    pub fn text_active_color32(&self) -> egui::Color32 {
        hex_to_color32(&self.text_active_color)
    }

    #[must_use]
    pub fn text_inactive_color32(&self) -> egui::Color32 {
        hex_to_color32(&self.text_inactive_color)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SnappingTokens {
    pub snap_threshold_dp: f32,
    pub angle_constraint_step_deg: f32,
}

pub const MIN_VIEWPORT_ZOOM: f32 = 0.01;
pub const MAX_VIEWPORT_ZOOM: f32 = 50.0;
pub const ZOOM_STEP_IN_FACTOR: f32 = std::f32::consts::SQRT_2;
pub const ZOOM_STEP_OUT_FACTOR: f32 = 0.70710677;
pub const WHEEL_ZOOM_SENSITIVITY: f32 = 0.0015;
pub const FULL_CIRCLE_DEG: f64 = 360.0;
pub const DEFAULT_ANGLE_SNAP_DEG: f64 = 45.0;
pub const CURVE_FLATTEN_TOLERANCE: f64 = 0.25;
pub const DEFAULT_CANVAS_WIDTH_DP: f32 = 800.0;
pub const DEFAULT_CANVAS_HEIGHT_DP: f32 = 600.0;

pub const DEFAULT_ASCENDER_UPM: f64 = 800.0;
pub const DEFAULT_CAP_HEIGHT_UPM: f64 = 700.0;
pub const DEFAULT_X_HEIGHT_UPM: f64 = 500.0;
pub const DEFAULT_BASELINE_UPM: f64 = 0.0;
pub const DEFAULT_DESCENDER_UPM: f64 = -200.0;

pub const START_NODE_ARROW_OFFSET_DP: f32 = 6.5;
pub const START_NODE_ARROW_LEN_DP: f32 = 13.0;
pub const START_NODE_ARROW_HALF_WIDTH_DP: f32 = 6.0;
pub const START_NODE_ARROW_OUTLINE_WIDTH_DP: f32 = 1.25;
pub const OPEN_ENDPOINT_TICK_HALF_LEN_DP: f32 = 5.0;
pub const METRICS_LABEL_FONT_SIZE_DP: f32 = 10.0;
pub const METRICS_LABEL_OFFSET_X_DP: f32 = 6.0;
pub const METRICS_LABEL_OFFSET_Y_DP: f32 = 2.0;

pub const EXTREMUM_T_MIN: f64 = 0.01;
pub const EXTREMUM_T_MAX: f64 = 0.99;
pub const EXTREMUM_SNAP_MAX_DELTA_T: f64 = 0.25;
pub const DRAG_CURVE_T_MIN: f64 = 0.05;
pub const DRAG_CURVE_T_MAX: f64 = 0.95;
pub const DRAG_CURVE_MIN_HANDLE_LEN: f64 = 0.9;
pub const SEGMENT_DRAG_FREE_WEIGHT_FACTOR: f64 = 2.65;
pub const SEGMENT_DRAG_MAX_HANDLE_LEN_UPM: f64 = 6000.0;
pub const TENSION_SCALE_MIN: f64 = 0.05;
pub const TENSION_SCALE_MAX: f64 = 10.0;

pub const DEFAULT_GRID_STEP_UPM: f64 = 1.0;
pub const NUDGE_STEP_UPM: f64 = 1.0;
pub const NUDGE_STEP_SHIFT_UPM: f64 = 10.0;
pub const NUDGE_STEP_CMD_UPM: f64 = 100.0;
pub const NUDGE_SPAN_MIN: f64 = 0.1;
pub const NUDGE_PREV_SPAN_Y_MIN: f64 = 1.0;
pub const NUDGE_RATIO_CLAMP_MIN: f64 = -1.45;
pub const NUDGE_RATIO_CLAMP_MAX: f64 = 1.45;
pub const SHIFT_MIDDLE_MAX_T: f64 = 0.98;
pub const SHIFT_MIDDLE_TANGENT_FACTOR: f64 = 1.5;
pub const SEGMENT_HIT_TOLERANCE_DP: f32 = 6.0;
pub const DOUBLE_CLICK_MAX_DELAY_SEC: f64 = 0.4;
pub const DOUBLE_CLICK_MAX_DIST_DP: f32 = 8.0;
pub const PARAM_CLAMP_MIN: f64 = 0.001;
pub const PARAM_CLAMP_MAX: f64 = 0.999;
pub const CUBIC_HEAL_RATIO: f64 = 0.6666666666666666;
pub const CUBIC_ONE_THIRD: f64 = 1.0 / 3.0;
pub const CUBIC_TWO_THIRDS: f64 = 2.0 / 3.0;
pub const CUBIC_HEAL_MAX_SCALE: f64 = 3.0;
pub const BERNSTEIN_CUBIC_COEFF: f64 = 3.0;
pub const BERNSTEIN_SECOND_DERIV_COEFF: f64 = 6.0;
pub const SOLVER_DET_EPSILON: f64 = 1e-9;
pub const SPLIT_RATIO_MIN: f64 = 0.05;
pub const SPLIT_RATIO_MAX: f64 = 0.95;
pub const FIT_SAMPLES_PER_SEGMENT: usize = 8;
pub const NEWTON_REPARAM_ITERATIONS: usize = 4;
pub const COLLINEAR_MIN_LEN_EPSILON: f64 = 1e-6;
pub const GHOST_NODE_STROKE_WIDTH_DP: f32 = 1.5;
pub const AUTOFIT_MARGIN_RATIO: f32 = 0.65;
pub const INITIAL_SAFE_NODE_ID: u64 = 1000;
pub const CURVE_SOLVE_ACCURACY: f64 = 1e-4;

pub const LOCATOR_DEFAULT_WIDTH_DP: f32 = 48.0;
pub const LOCATOR_DEFAULT_HEIGHT_DP: f32 = 36.0;
pub const LOCATOR_DEFAULT_PADDING_DP: f32 = 2.0;
pub const LOCATOR_OPEN_CONTOUR_STROKE_WIDTH_DP: f32 = 1.5;
pub const LOCATOR_CONTEXT_ALPHA: u8 = 31;
pub const LOCATOR_CELL_CORNER_RADIUS_DP: f32 = 4.0;
pub const LOCATOR_CELL_BORDER_WIDTH_DP: f32 = 1.0;
pub const LOCATOR_CELL_BG_ALPHA: u8 = 15;
pub const LOCATOR_CELL_BORDER_ALPHA: u8 = 35;

#[must_use]
pub fn locator_cell_bg_color() -> egui::Color32 {
    egui::Color32::from_black_alpha(LOCATOR_CELL_BG_ALPHA)
}

#[must_use]
pub fn locator_cell_border_color() -> egui::Color32 {
    egui::Color32::from_black_alpha(LOCATOR_CELL_BORDER_ALPHA)
}

#[must_use]
pub fn locator_default_context_color() -> egui::Color32 {
    egui::Color32::from_black_alpha(LOCATOR_CONTEXT_ALPHA)
}

#[must_use]
pub fn locator_default_focus_color() -> egui::Color32 {
    hex_to_color32("#007AFF")
}

#[must_use]
pub fn locator_default_mask_color() -> egui::Color32 {
    hex_to_color32("#FF3B30")
}

pub const GOLDEN_MASTER_WINDOW_WIDTH_DP: f32 = 1223.0;
pub const GOLDEN_MASTER_WINDOW_HEIGHT_DP: f32 = 811.0;
pub const GOLDEN_MASTER_SIDEBAR_WIDTH_DP: f32 = 250.0;
pub const GOLDEN_MASTER_CANVAS_WIDTH_DP: f32 = 735.0;
pub const GOLDEN_MASTER_CANVAS_EDIT_HEIGHT_DP: f32 = 617.0;
pub const GOLDEN_MASTER_CANVAS_PREVIEW_HEIGHT_DP: f32 = 163.0;
pub const GOLDEN_MASTER_INFOBOX_WIDTH_DP: f32 = 237.0;
pub const GOLDEN_MASTER_SPLITTER_WIDTH_DP: f32 = 1.0;
pub const GOLDEN_MASTER_INFOBOX_DIVIDER_Y_DP: f32 = 781.0;
pub const GOLDEN_MASTER_ZOOM: f32 = 0.20425;
pub const GOLDEN_MASTER_PAN_X: f32 = 550.2;
pub const GOLDEN_MASTER_PAN_Y: f32 = 502.0;
pub const GOLDEN_MASTER_CALIBRATED_ZOOM: f32 = 0.200;
pub const GOLDEN_MASTER_FULL_PAN_X: f32 = 548.0;
pub const GOLDEN_MASTER_FULL_PAN_Y: f32 = 501.0;
pub const GOLDEN_MASTER_LOCAL_PAN_X: f32 = 297.0;
pub const GOLDEN_MASTER_LOCAL_PAN_Y: f32 = 501.0;

/// Converts an `egui::Color32` to a `tiny_skia::Color`.
#[must_use]
pub fn color32_to_tiny_skia(color: egui::Color32) -> tiny_skia::Color {
    let [r, g, b, a] = color.to_srgba_unmultiplied();
    tiny_skia::Color::from_rgba8(r, g, b, a)
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct GrafiaTheme {
    pub theme_name: String,
    pub edit_canvas: EditCanvasTokens,
    pub preview_canvas: PreviewCanvasTokens,
    pub shape_canvas: ShapeCanvasTokens,
    pub snapping: SnappingTokens,
    #[serde(default = "default_passive_glyph_fill_rgba")]
    pub passive_glyph_fill: RgbaColorToken,
    #[serde(default = "default_kerning_negative_fill_rgba")]
    pub kerning_negative_fill: RgbaColorToken,
    #[serde(default = "default_kerning_positive_fill_rgba")]
    pub kerning_positive_fill: RgbaColorToken,
    #[serde(default = "default_text_caret_color_rgba")]
    pub text_caret_color: RgbaColorToken,
    #[serde(default = "default_text_selection_fill_rgba")]
    pub text_selection_fill: RgbaColorToken,
    #[serde(default = "default_text_measurement_line_color_rgba")]
    pub text_measurement_line_color: RgbaColorToken,
    #[serde(default = "default_text_measurement_knob_fill_rgba")]
    pub text_measurement_knob_fill: RgbaColorToken,
    #[serde(default = "default_text_metrics_number_color_rgba")]
    pub text_metrics_number_color: RgbaColorToken,
    #[serde(default = "default_preview_bg_normal_rgba")]
    pub preview_bg_normal: RgbaColorToken,
    #[serde(default = "default_preview_fg_normal_rgba")]
    pub preview_fg_normal: RgbaColorToken,
    #[serde(default = "default_preview_bg_inverted_rgba")]
    pub preview_bg_inverted: RgbaColorToken,
    #[serde(default = "default_preview_fg_inverted_rgba")]
    pub preview_fg_inverted: RgbaColorToken,
    #[serde(default = "default_ruler_bg_fill_rgba")]
    pub ruler_bg_fill: RgbaColorToken,
    #[serde(default = "default_ruler_border_stroke_rgba")]
    pub ruler_border_stroke: RgbaColorToken,
    #[serde(default = "default_ruler_tick_color_rgba")]
    pub ruler_tick_color: RgbaColorToken,
    #[serde(default = "default_ruler_hairline_color_rgba")]
    pub ruler_hairline_color: RgbaColorToken,
    #[serde(default = "default_background_layer_stroke_rgba")]
    pub background_layer_stroke: RgbaColorToken,
    #[serde(default = "default_anchor_fill_rgba")]
    pub anchor_fill: RgbaColorToken,
    #[serde(default = "default_anchor_selected_fill_rgba")]
    pub anchor_selected_fill: RgbaColorToken,
    #[serde(default = "default_mark_cloud_fill_rgba")]
    pub mark_cloud_fill: RgbaColorToken,
    #[serde(default = "default_component_fill_rgba")]
    pub component_fill: RgbaColorToken,
    #[serde(default = "default_component_selected_fill_rgba")]
    pub component_selected_fill: RgbaColorToken,
    #[serde(default = "default_component_locked_fill_rgba")]
    pub component_locked_fill: RgbaColorToken,
    #[serde(default = "default_master_compat_ray_color_rgba")]
    pub master_compat_ray_color: RgbaColorToken,
    #[serde(default = "default_master_incompat_highlight_rgba")]
    pub master_incompat_highlight: RgbaColorToken,
}

impl GrafiaTheme {
    /// Loads default theme embedded from `tokens/grafia_theme_sonoma.json`.
    #[must_use]
    pub fn sonoma_default() -> Self {
        let json_str = include_str!("../../tokens/grafia_theme_sonoma.json");
        serde_json::from_str(json_str).expect("grafia_theme_sonoma.json must be valid JSON")
    }

    fn themed_hex(&self, dark: &str, light: &str) -> egui::Color32 {
        hex_to_color32(if self.is_dark() { dark } else { light })
    }

    #[must_use]
    pub fn canvas_background_color32(&self) -> egui::Color32 {
        self.themed_hex(DARK_CANVAS_BACKGROUND_HEX, "#FFFFFF")
    }

    #[must_use]
    pub fn sidebar_background_color32(&self) -> egui::Color32 {
        self.themed_hex(DARK_SIDEBAR_BACKGROUND_HEX, "#F6F6F6")
    }

    #[must_use]
    pub fn infobox_background_color32(&self) -> egui::Color32 {
        self.themed_hex(DARK_INFOBOX_BACKGROUND_HEX, "#ECECEC")
    }

    #[must_use]
    pub fn splitter_color32(&self) -> egui::Color32 {
        self.themed_hex(DARK_SPLITTER_HEX, "#E1E1E1")
    }

    #[must_use]
    pub fn start_node_arrow_color32(&self) -> egui::Color32 {
        hex_to_color32("#0066FFDC")
    }

    #[must_use]
    pub fn metrics_guide_color32(&self) -> egui::Color32 {
        self.themed_hex(DARK_METRICS_GUIDE_HEX, "#EAEAEA")
    }

    #[must_use]
    pub fn metrics_label_color32(&self) -> egui::Color32 {
        self.themed_hex(DARK_METRICS_LABEL_HEX, "#8E8E93")
    }

    #[must_use]
    pub fn open_endpoint_color32(&self) -> egui::Color32 {
        self.themed_hex(DARK_OPEN_ENDPOINT_HEX, "#007AFF")
    }
}