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
use super::error::Error; use super::glyph::Glyph; use rusttype::Point; use serde::{Deserialize, Serialize}; use std::str::FromStr; #[derive(Debug)] pub enum MetaFormat { Default, } impl FromStr for MetaFormat { type Err = &'static str; fn from_str(meta_format: &str) -> Result<MetaFormat, &'static str> { match meta_format { _ => Ok(MetaFormat::Default), } } } pub fn create_meta_file( fmt: &MetaFormat, glyphs: &Vec<Glyph<'_>>, ) -> Result<String, Error> { match fmt { MetaFormat::Default => make_default(glyphs), } } #[derive(Serialize, Deserialize, Debug)] struct SerdePoint<N> { x: N, y: N, } impl From<Point<i32>> for SerdePoint<i32> { fn from(p: Point<i32>) -> SerdePoint<i32> { SerdePoint { x: p.x, y: p.y } } } impl From<Point<f32>> for SerdePoint<f32> { fn from(p: Point<f32>) -> SerdePoint<f32> { SerdePoint { x: p.x, y: p.y } } } #[derive(Serialize, Deserialize, Debug)] struct DefaultMetaGlyph { codepoint: char, min: SerdePoint<i32>, max: SerdePoint<i32>, offset: SerdePoint<f32>, } impl From<&Glyph<'_>> for DefaultMetaGlyph { fn from(glyph: &Glyph<'_>) -> DefaultMetaGlyph { DefaultMetaGlyph { codepoint: glyph.codepoint, min: glyph.min.into(), max: glyph.max.into(), offset: glyph.offset.into(), } } } #[derive(Serialize, Deserialize, Debug)] struct DefaultMetaObject { glyphs: Vec<DefaultMetaGlyph>, } fn make_default(glyphs: &Vec<Glyph<'_>>) -> Result<String, Error> { let mut obj = DefaultMetaObject { glyphs: vec![] }; for glyph in glyphs.iter() { obj.glyphs.push(glyph.into()); } serde_json::to_string(&obj).map_err(Error::SERDE) } #[cfg(test)] mod tests { use super::*; #[test] fn test_serialize() { let glyphs = vec![ Glyph { pglyph: None, codepoint: 'a', min: rusttype::point(0, 0), max: rusttype::point(31, 31), offset: rusttype::point(10.0, 0.0), }, Glyph { pglyph: None, codepoint: '1', min: rusttype::point(32, 0), max: rusttype::point(63, 31), offset: rusttype::point(11.0, 1.0), }, ]; let result_string = make_default(&glyphs).unwrap(); let result_t: DefaultMetaObject = serde_json::from_str(&result_string).unwrap(); let result_v = serde_json::to_value(&result_t).unwrap(); let expected = json!({ "glyphs": [ { "codepoint": "a", "min": { "x": 0, "y": 0 }, "max": { "x": 31, "y": 31 }, "offset": { "x": 10.0, "y": 0.0 } }, { "codepoint": "1", "min": { "x": 32, "y": 0 }, "max": { "x": 63, "y": 31 }, "offset": { "x": 11.0, "y": 1.0 } } ] }); assert_eq!(result_v, expected); } }