svg2glif 0.3.1

Convert SVG to UFO glif file
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
use anyhow::{Context, Result, anyhow};
use norad::{Anchor, Codepoints, Contour, ContourPoint, Glyph, Name, PointType};
use std::fs;
use std::path::Path;
use std::str::FromStr;
use svgtypes::{Length, LengthUnit, SimplePathSegment, SimplifyingPathParser, Transform};

/// Configuration for SVG to GLIF conversion
pub struct ConversionConfig {
    /// Units per em (typically 1000 or 2048)
    pub em_size: f32,
    /// Descent value to place the glyph above baseline
    pub descent: f32,
    /// Optional Unicode codepoint in hex format
    pub unicode: Option<String>,
    /// Optional name for the glyph, if not given, filename will be used.
    pub name: Option<String>,
}

impl ConversionConfig {
    /// Create a new conversion configuration
    pub fn new(em_size: f32, descent: f32) -> Self {
        Self {
            em_size,
            descent,
            unicode: None,
            name: None,
        }
    }

    /// Set the unicode codepoint (in hex format, e.g., "0041")
    pub fn with_unicode(mut self, unicode: String) -> Self {
        self.unicode = Some(unicode);
        self
    }

    /// Set the name
    pub fn with_name(mut self, name: String) -> Self {
        self.name = Some(name);
        self
    }
}

/// Convert an SVG file to a GLIF glyph
pub fn convert_svg_to_glyph(svg_path: &Path, config: &ConversionConfig) -> Result<Glyph> {
    // Load SVG
    let svg_data = fs::read_to_string(svg_path).context("reading input svg")?;
    convert_svg_string_to_glyph(&svg_data, svg_path, config)
}

/// Convert SVG string data to a GLIF glyph
///
/// # Examples
///
/// ```
/// use svg2glif::{convert_svg_string_to_glyph, ConversionConfig};
/// use std::path::Path;
///
/// let svg_data = r#"<?xml version="1.0" encoding="UTF-8"?>
/// <svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
///   <path d="M 10 10 L 90 10 L 90 90 L 10 90 Z" fill="black"/>
/// </svg>"#;
///
/// let config = ConversionConfig::new(1000.0, 200.0)
///     .with_unicode("0041".to_string());
///
/// let glyph = convert_svg_string_to_glyph(
///     svg_data,
///     Path::new("example.svg"),
///     &config
/// ).unwrap();
///
/// ```
pub fn convert_svg_string_to_glyph(
    svg_data: &str,
    svg_path: &Path,
    config: &ConversionConfig,
) -> Result<Glyph> {
    let doc = roxmltree::Document::parse(svg_data).context("parsing svg")?;
    let root = doc.root_element();

    // Get SVG dimensions
    let svg_width = parse_length(root.attribute("width").unwrap_or("100"))?;
    let svg_height = parse_length(root.attribute("height").unwrap_or("100"))?;

    // Scale to font units
    let scale = config.em_size / svg_height;
    let advance_width = (svg_width * scale).round();
    let advance_height = (svg_height * scale).round();

    let glyph_name = config
        .name
        .as_deref()
        .or_else(|| svg_path.file_stem().and_then(|s| s.to_str()))
        .unwrap_or("svgglyph")
        .to_string();

    // Create glyph
    let mut glyph = Glyph::new(glyph_name.as_str());
    glyph.width = advance_width as f64;
    glyph.height = advance_height as f64;

    // Add unicode if provided
    if let Some(ref unicode_hex) = config.unicode
        && let Ok(codepoint) = u32::from_str_radix(unicode_hex, 16)
        && let Some(c) = char::from_u32(codepoint)
    {
        let codepoints = Codepoints::new([c]);
        glyph.codepoints = codepoints;
    }

    // Process nodes
    process_svg_node(
        &root,
        &mut glyph,
        svg_height,
        config.descent,
        scale,
        &Transform::default(),
    )?;

    Ok(glyph)
}

/// Convert an SVG file to GLIF and write to output file
///
/// # Examples
///
/// ```no_run
/// use svg2glif::{convert_svg_to_glif_file, ConversionConfig};
/// use std::path::Path;
///
/// let config = ConversionConfig::new(1000.0, 200.0)
///     .with_unicode("0041".to_string());
///
/// convert_svg_to_glif_file(
///     Path::new("input.svg"),
///     Path::new("output.glif"),
///     &config
/// ).unwrap();
/// ```
pub fn convert_svg_to_glif_file(
    svg_path: &Path,
    glif_path: &Path,
    config: &ConversionConfig,
) -> Result<()> {
    let glyph = convert_svg_to_glyph(svg_path, config)?;
    let glif_data = glyph.encode_xml()?;
    fs::write(glif_path, glif_data)?;
    Ok(())
}

fn multiply(ts1: &Transform, ts2: &Transform) -> Transform {
    Transform {
        a: ts1.a * ts2.a + ts1.c * ts2.b,
        b: ts1.b * ts2.a + ts1.d * ts2.b,
        c: ts1.a * ts2.c + ts1.c * ts2.d,
        d: ts1.b * ts2.c + ts1.d * ts2.d,
        e: ts1.a * ts2.e + ts1.c * ts2.f + ts1.e,
        f: ts1.b * ts2.e + ts1.d * ts2.f + ts1.f,
    }
}

fn apply_transform(transform: &Transform, x: f32, y: f32) -> (f32, f32) {
    let new_x = transform.a as f32 * x + transform.c as f32 * y + transform.e as f32;
    let new_y = transform.b as f32 * x + transform.d as f32 * y + transform.f as f32;
    (new_x, new_y)
}

fn process_svg_node(
    node: &roxmltree::Node,
    glyph: &mut Glyph,
    svg_height: f32,
    descent: f32,
    scale: f32,
    parent_transform: &Transform,
) -> Result<()> {
    // Compute current transform
    let current_transform = if let Some(transform_str) = node.attribute("transform") {
        let transform = Transform::from_str(transform_str).context("parsing transform")?;
        multiply(parent_transform, &transform)
    } else {
        *parent_transform
    };

    match node.tag_name().name() {
        "path" => {
            if let Some(d) = node.attribute("d") {
                let contours =
                    process_path_data(d, svg_height, descent, scale, &current_transform)?;
                if !glyph.contours.is_empty() {
                    glyph.contours.extend(contours);
                } else {
                    glyph.contours = contours;
                }
            }
        }
        "text" => {
            if let Some(anchor) =
                process_text_as_anchor(node, svg_height, descent, scale, &current_transform)
            {
                glyph.anchors.push(anchor);
            }
        }
        "g" | "svg" => {
            // Process children
            for child in node.children() {
                if child.is_element() {
                    process_svg_node(
                        &child,
                        glyph,
                        svg_height,
                        descent,
                        scale,
                        &current_transform,
                    )?;
                }
            }
        }
        _ => {
            // Process children for unknown elements too
            for child in node.children() {
                if child.is_element() {
                    process_svg_node(
                        &child,
                        glyph,
                        svg_height,
                        descent,
                        scale,
                        &current_transform,
                    )?;
                }
            }
        }
    }

    Ok(())
}

fn parse_length(length_str: &str) -> Result<f32> {
    let length = Length::from_str(length_str).context("parsing length")?;
    match length.unit {
        LengthUnit::None | LengthUnit::Px => Ok(length.number as f32),
        _ => Err(anyhow!("unsupported length unit: {:?}", length.unit)),
    }
}

fn process_path_data(
    path_data: &str,
    svg_height: f32,
    descent: f32,
    scale: f32,
    transform: &Transform,
) -> Result<Vec<Contour>> {
    let mut contours = Vec::new();
    let mut current_contour: Vec<ContourPoint> = Vec::new();

    // Use SimplifyingPathParser - all coordinates are absolute!
    // This automatically handles:
    // - Relative to absolute conversion
    // - H/V line to LineTo conversion
    // - SmoothCurveTo control point reflection
    // - Arc to Bezier curve conversion
    for segment in SimplifyingPathParser::from(path_data) {
        let segment = segment.context("parsing path segment")?;

        match segment {
            SimplePathSegment::MoveTo { x, y } => {
                // Start new contour
                if !current_contour.is_empty() {
                    contours.push(Contour::new(current_contour, None));
                    current_contour = Vec::new();
                }
                let (tx, ty) = apply_transform(transform, x as f32, y as f32);
                let (ux, uy) = svg_to_ufo(tx, ty, svg_height, descent, scale);
                current_contour.push(ContourPoint::new(
                    ux,
                    uy,
                    PointType::Curve,
                    true,
                    None,
                    None,
                ));
            }
            SimplePathSegment::LineTo { x, y } => {
                let (tx, ty) = apply_transform(transform, x as f32, y as f32);
                let (ux, uy) = svg_to_ufo(tx, ty, svg_height, descent, scale);
                current_contour.push(ContourPoint::new(
                    ux,
                    uy,
                    PointType::Line,
                    false,
                    None,
                    None,
                ));
            }
            SimplePathSegment::CurveTo {
                x1,
                y1,
                x2,
                y2,
                x,
                y,
            } => {
                // Add two off-curve control points
                let (tx1, ty1) = apply_transform(transform, x1 as f32, y1 as f32);
                let (ux1, uy1) = svg_to_ufo(tx1, ty1, svg_height, descent, scale);
                current_contour.push(ContourPoint::new(
                    ux1,
                    uy1,
                    PointType::OffCurve,
                    false,
                    None,
                    None,
                ));

                let (tx2, ty2) = apply_transform(transform, x2 as f32, y2 as f32);
                let (ux2, uy2) = svg_to_ufo(tx2, ty2, svg_height, descent, scale);
                current_contour.push(ContourPoint::new(
                    ux2,
                    uy2,
                    PointType::OffCurve,
                    false,
                    None,
                    None,
                ));

                // Add on-curve point
                let (tx, ty) = apply_transform(transform, x as f32, y as f32);
                let (ux, uy) = svg_to_ufo(tx, ty, svg_height, descent, scale);
                current_contour.push(ContourPoint::new(
                    ux,
                    uy,
                    PointType::Curve,
                    true,
                    None,
                    None,
                ));
            }
            SimplePathSegment::Quadratic { .. } => {
                // Skip quadratic curves as they're not supported in UFO/GLIF
                // If needed, they could be converted to cubic Bezier curves
            }
            SimplePathSegment::ClosePath => {
                // Finish current contour
                if !current_contour.is_empty() {
                    contours.push(Contour::new(current_contour, None));
                    current_contour = Vec::new();
                }
            }
        }
    }

    // Add any remaining contour
    if !current_contour.is_empty() {
        contours.push(Contour::new(current_contour, None));
    }

    // Remove duplicate last point if it matches the first point
    for contour in &mut contours {
        if contour.points.len() > 1 {
            let first = &contour.points[0];
            let last = &contour.points[contour.points.len() - 1];

            if first.x == last.x && first.y == last.y {
                contour.points.pop();
            }
        }
    }

    Ok(contours)
}

fn process_text_as_anchor(
    node: &roxmltree::Node,
    svg_height: f32,
    descent: f32,
    scale: f32,
    transform: &Transform,
) -> Option<Anchor> {
    // Get the text content as the anchor name
    let anchor_name = node.text()?;

    if anchor_name.trim().is_empty() {
        return None;
    }

    // Get position from x, y attributes (default to 0, 0)
    let x = node
        .attribute("x")
        .and_then(|s| parse_length(s).ok())
        .unwrap_or(0.0);
    let y = node
        .attribute("y")
        .and_then(|s| parse_length(s).ok())
        .unwrap_or(0.0);

    // Apply transform
    let (tx, ty) = apply_transform(transform, x, y);

    // Convert to UFO coordinates
    let (ufo_x, ufo_y) = svg_to_ufo(tx, ty, svg_height, descent, scale);
    let anchor_name = Name::new(anchor_name.trim()).ok()?;
    Some(Anchor::new(ufo_x, ufo_y, Some(anchor_name), None, None))
}

fn svg_to_ufo(sx: f32, sy: f32, svg_height: f32, descent: f32, scale: f32) -> (f64, f64) {
    // Flip Y (SVG origin is top-left; UFO origin baseline is bottom-left)
    let x = sx * scale;
    let y = (svg_height - descent - sy) * scale;
    (x.round() as f64, y.round() as f64)
}