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
/*!
PostScript outlines.

*/

mod cff;
mod hint;

pub use cff::{Glyph, GlyphSink, Cff, CffProxy};
pub use hint::HinterState;

use super::{internal, TRACE};

use crate::font::FontRef;

pub struct Scaler {
    entries: Vec<Entry>,
    max_entries: usize,
    epoch: u64,
}

impl Scaler {
    pub fn new(max_entries: usize) -> Self {
        Self {
            entries: Vec::new(),
            max_entries,
            epoch: 0,
        }
    }

    pub fn scale(
        &mut self,
        font: &FontRef,
        id: u64,
        coords: &[i16],
        proxy: &CffProxy,
        scale: f32,
        hint: bool,
        glyph_id: u16,
        sink: &mut impl GlyphSink,
    ) -> Option<()> {
        let cff = proxy.materialize(font);
        let glyph = cff.get(glyph_id)?;
        if hint {
            let dict = glyph.subfont_index();
            let state = self.entry(id, dict, coords, scale, &glyph);
            if glyph.path(scale, coords, Some(state), sink) {
                Some(())
            } else {
                None
            }
        } else {
            if glyph.path(scale, coords, None, sink) {
                Some(())
            } else {
                None
            }
        }
    }

    fn entry(
        &mut self,
        id: u64,
        dict: u16,
        coords: &[i16],
        scale: f32,
        glyph: &Glyph,
    ) -> &HinterState {
        let epoch = self.epoch;
        let (found, index) = self.find_entry(id, dict, coords, scale);
        if found {
            let entry = &mut self.entries[index];
            entry.epoch = epoch;
            &entry.state
        } else {
            self.epoch += 1;
            let state = HinterState::new(glyph, scale, coords);
            if index == self.entries.len() {
                self.entries.push(Entry {
                    epoch,
                    id,
                    dict,
                    state,
                    coords: Vec::from(coords),
                    scale,
                });
                &self.entries[index].state
            } else {
                let entry = &mut self.entries[index];
                entry.epoch = epoch;
                entry.id = id;
                entry.dict = dict;
                entry.state = state;
                entry.coords.clear();
                entry.coords.extend_from_slice(coords);
                entry.scale = scale;
                &entry.state
            }
        }
    }

    fn find_entry(&self, id: u64, dict: u16, coords: &[i16], scale: f32) -> (bool, usize) {
        let mut lowest_epoch = self.epoch;
        let mut lowest_index = 0;
        let vary = coords.len() != 0;
        for (i, entry) in self.entries.iter().enumerate() {
            if entry.id == id
                && entry.dict == dict
                && entry.scale == scale
                && (!vary || (coords == &entry.coords[..]))
            {
                return (true, i);
            }
            if entry.epoch < lowest_epoch {
                lowest_epoch = entry.epoch;
                lowest_index = i;
            }
        }
        if self.entries.len() < self.max_entries {
            lowest_index = self.entries.len();
        }
        (false, lowest_index)
    }
}

struct Entry {
    epoch: u64,
    id: u64,
    dict: u16,
    state: HinterState,
    coords: Vec<i16>,
    scale: f32,
}