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
use serde::Serialize;
use serde::Deserialize;
use kurbo::Affine;
use plist;

use super::{MFEKOutline, LayerOperation};
use crate::color::Color;
use crate::glif::name_to_filename;
use crate::image::GlifImage;
use crate::point::PointData;

macro_rules! DEFAULT_LAYER_FORMAT_STR {() => {"Layer {}"}}
pub const DEFAULT_LAYER_FORMAT_STR: &str = DEFAULT_LAYER_FORMAT_STR!();

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Layer<PD: PointData> {
    pub name: String,
    pub visible: bool,
    pub color: Option<Color>,
    pub outline: MFEKOutline<PD>,
    pub operation: Option<LayerOperation>,
    pub images: Vec<(GlifImage, Affine)>,
}

impl<PD: PointData> Layer<PD> {
    pub fn to_glyphs_dir(&self, idx: usize) -> String {
        if idx == 0 {
            String::from("glyphs")
        } else {
            format!("glyphs.{}", name_to_filename(&self.name, false))
        }
    }
}

/// Create UFO(3) specification layer layerinfo.plist's for our glyph.
pub trait ToLayerInfoPlist {
    /// # Safety
    ///
    /// plist::Value guaranteed to be of variant plist::Dictionary
    fn to_layerinfo_plist(&self) -> Option<plist::Value>;
}

/// Create UFO(3) specification layer layercontents.plist's for our glyph.
pub trait ToLayerContentsPlist {
    /// # Safety
    ///
    /// plist::Value guaranteed to be of variant plist::Array
    fn to_layercontents_plist(&self) -> plist::Value;

    /// # Safety
    ///
    /// plist::Value guaranteed to be of variant plist::Array
    fn merge_layercontents_plists(&self, other: plist::Value) -> plist::Value;
}

impl<PD: PointData> ToLayerInfoPlist for Layer<PD> {
    fn to_layerinfo_plist(&self) -> Option<plist::Value> {
        let color = if let Some(color) = self.color {
            color
        } else {
            return None
        };

        let mut layerinfo: plist::Dictionary = plist::Dictionary::new();

        layerinfo.insert(String::from("color"), plist::Value::String(color.to_string()));

        Some(plist::Value::Dictionary(layerinfo))
    }
}

impl<PD: PointData> ToLayerContentsPlist for &[Layer<PD>] {
    fn to_layercontents_plist(&self) -> plist::Value {
        let mut ret: Vec<plist::Value> = Vec::new();

        for (i, layer) in self.iter().enumerate() {
            if !layer.visible { continue }
            let key = if layer.name == format!(DEFAULT_LAYER_FORMAT_STR!(), 0) {
                String::from("public.default") // https://unifiedfontobject.org/versions/ufo3/layercontents.plist/#publicdefault
            } else {
                layer.name.clone()
            };
            let value = layer.to_glyphs_dir(i);
            ret.push(plist::Value::Array(vec![plist::Value::String(key), plist::Value::String(value)]));
        }

        plist::Value::Array(ret)
    }
    fn merge_layercontents_plists(&self, other: plist::Value) -> plist::Value {
        let our_lc = self.to_layercontents_plist();
        let inner_ours: Vec<plist::Value> = our_lc.into_array().unwrap(); // Safe. Cf. to_layercontents_plist return type
        let mut inner_theirs: Vec<plist::Value> = other.into_array().unwrap();
        for l in inner_ours.iter() {
            if !inner_theirs.contains(&l) {
                inner_theirs.push(l.clone());
            }
        }
        plist::Value::Array(inner_theirs)
    }
}