Struct font::Offset

source ·
pub struct Offset(pub Number, pub Number);
Expand description

An offset.

Tuple Fields§

§0: Number§1: Number

Implementations§

Examples found in repository?
src/format/opentype/truetype.rs (line 126)
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
fn draw_simple(builder: &mut Builder, description: &SimpleDescription) -> Result<()> {
    macro_rules! expect(
        ($condition:expr) => (
            if !$condition {
                raise!(concat!("found a malformed glyph (", stringify!($condition), ")"));
            }
        )
    );

    let &SimpleDescription {
        ref end_points,
        ref flags,
        ref x,
        ref y,
        ..
    } = description;
    let point_count = flags.len();
    expect!(point_count == x.len());
    expect!(point_count == y.len());
    let mut i = 0;
    let mut sum = Offset::default();
    for k in end_points.iter().map(|&k| k as usize) {
        expect!(i < point_count);
        let start = Offset::from((x[i], y[i]));
        let mut control = match flags[i].is_on_curve() {
            false => Some(Offset::default()),
            _ => None,
        };
        let mut sum_delta = start;
        let mut offset = Offset::default();
        for j in (i + 1)..=k {
            expect!(j < point_count);
            let current = (x[j], y[j]).into();
            sum_delta += current;
            match (flags[j].is_on_curve(), &mut control) {
                (false, control @ &mut None) => {
                    *control = Some(current);
                }
                (false, &mut Some(ref mut control)) => {
                    let current = current / 2.0;
                    builder.add_quadratic(*control, current);
                    offset += *control + current;
                    *control = current;
                }
                (true, &mut None) => {
                    builder.add_linear(current);
                    offset += current;
                }
                (true, control @ &mut Some(_)) => {
                    let control = control.take().unwrap();
                    builder.add_quadratic(control, current);
                    offset += control + current;
                }
            }
        }
        match (flags[i].is_on_curve(), control) {
            (false, None) => {
                let control = (sum + start) - (sum + sum_delta);
                builder.move_control(control);
                offset += control;
                builder.move_absolute(sum + sum_delta);
            }
            (false, Some(control)) => {
                let current = ((sum + start) - (sum + sum_delta)) / 2.0;
                if !control.is_zero() || !current.is_zero() {
                    builder.add_quadratic(control, current);
                }
                offset += control + current;
                builder.move_control(current);
                offset += current;
                builder.move_absolute(sum + sum_delta + current);
            }
            (true, None) => {
                let current = -offset;
                if !current.is_zero() {
                    builder.add_linear(current);
                }
                offset += current;
                builder.move_absolute(sum + start);
            }
            (true, Some(control)) => {
                let current = -offset - control;
                if !control.is_zero() || !current.is_zero() {
                    builder.add_quadratic(control, current);
                }
                offset += control + current;
                builder.move_absolute(sum + start);
            }
        }
        debug_assert!(offset.is_zero());
        builder.flush();
        sum += sum_delta;
        i = k + 1;
    }
    Ok(())
}

Create an undefined offset.

Examples found in repository?
src/format/opentype/postscript.rs (line 71)
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
    fn draw(&self, character: char) -> Result<Option<Glyph>> {
        use postscript::compact1::font_set::Record;
        use postscript::type2::Operator::*;

        macro_rules! expect(
            ($condition:expr) => (
                if !$condition {
                    raise!("found a malformed glyph");
                }
            )
        );

        let glyph_index = match self.mapping.find(character) {
            Some(glyph_index) => glyph_index,
            _ => return Ok(None),
        };
        let mut program = match self.font_set.char_strings[self.id].get(glyph_index as usize) {
            Some(char_string) => Program::new(
                char_string,
                &self.font_set.subroutines,
                match &self.font_set.records[self.id] {
                    Record::CharacterNameKeyed(ref record) => &*record.subroutines,
                    _ => unimplemented!(),
                },
            ),
            _ => raise!(
                "found no char string for character {} with glyph {}",
                character,
                glyph_index,
            ),
        };
        let mut builder = Builder::default();
        let mut position = Offset::default();
        let (mut max, mut min) = (Offset::undefined(), Offset::undefined());
        macro_rules! build(
            ($function:ident($(($x:expr, $y:expr)),+ $(,)?)) => (
                builder.$function($(($x, $y)),+);
                build!(@track $function($(($x, $y)),+));
            );
            (@track move_relative($(($x:expr, $y:expr)),+)) => (
                $(position += ($x, $y);)+
            );
            (@track $function:ident($(($x:expr, $y:expr)),+)) => (
                build!(@update);
                $(position += ($x, $y);)+
                build!(@update);
            );
            (@update) => (
                max = max.max(position);
                min = min.min(position);
            );
        );
        let mut clear = false;
        while let Some((operator, operands)) = program.next()? {
            let count = operands.len();
            match operator {
                RMoveTo | HMoveTo | VMoveTo => builder.flush(),
                _ => {}
            }
            match operator {
                RMoveTo => {
                    expect!(count == 2 || !clear && count == 3);
                    build!(move_relative((operands[0], operands[1])));
                }
                HMoveTo => {
                    expect!(count == 1 || !clear && count == 2);
                    build!(move_relative((operands[0], 0.0)));
                }
                VMoveTo => {
                    expect!(count == 1 || !clear && count == 2);
                    build!(move_relative((0.0, operands[0])));
                }
                RLineTo => {
                    expect!(count % 2 == 0);
                    for i in 0..(count / 2) {
                        let j = 2 * i;
                        build!(add_linear((operands[j], operands[j + 1])));
                    }
                }
                HLineTo => {
                    for i in 0..count {
                        if i % 2 == 0 {
                            build!(add_linear((operands[i], 0.0)));
                        } else {
                            build!(add_linear((0.0, operands[i])));
                        }
                    }
                }
                VLineTo => {
                    for i in 0..count {
                        if i % 2 == 1 {
                            build!(add_linear((operands[i], 0.0)));
                        } else {
                            build!(add_linear((0.0, operands[i])));
                        }
                    }
                }
                RRCurveTo => {
                    expect!(count % 6 == 0);
                    for i in 0..(count / 6) {
                        let j = 6 * i;
                        build!(add_cubic(
                            (operands[j], operands[j + 1]),
                            (operands[j + 2], operands[j + 3]),
                            (operands[j + 4], operands[j + 5]),
                        ));
                    }
                }
                HHCurveTo => {
                    let (offset, first) = if count % 4 == 0 {
                        (0, 0.0)
                    } else {
                        expect!((count - 1) % 4 == 0);
                        (1, operands[0])
                    };
                    for i in 0..((count - offset) / 4) {
                        let j = offset + 4 * i;
                        let first = if i == 0 { first } else { 0.0 };
                        build!(add_cubic(
                            (operands[j], first),
                            (operands[j + 1], operands[j + 2]),
                            (operands[j + 3], 0.0),
                        ));
                    }
                }
                HVCurveTo => {
                    let (steps, last) = if count % 4 == 0 {
                        (count / 4, 0.0)
                    } else {
                        expect!((count - 1) % 4 == 0);
                        ((count - 1) / 4, operands[count - 1])
                    };
                    for i in 0..steps {
                        let j = 4 * i;
                        let last = if i + 1 == steps { last } else { 0.0 };
                        if i % 2 == 0 {
                            build!(add_cubic(
                                (operands[j], 0.0),
                                (operands[j + 1], operands[j + 2]),
                                (last, operands[j + 3]),
                            ));
                        } else {
                            build!(add_cubic(
                                (0.0, operands[j]),
                                (operands[j + 1], operands[j + 2]),
                                (operands[j + 3], last),
                            ));
                        }
                    }
                }
                VHCurveTo => {
                    let (steps, last) = if count % 4 == 0 {
                        (count / 4, 0.0)
                    } else {
                        expect!((count - 1) % 4 == 0);
                        ((count - 1) / 4, operands[count - 1])
                    };
                    for i in 0..steps {
                        let j = 4 * i;
                        let last = if i + 1 == steps { last } else { 0.0 };
                        if i % 2 == 1 {
                            build!(add_cubic(
                                (operands[j], 0.0),
                                (operands[j + 1], operands[j + 2]),
                                (last, operands[j + 3]),
                            ));
                        } else {
                            build!(add_cubic(
                                (0.0, operands[j]),
                                (operands[j + 1], operands[j + 2]),
                                (operands[j + 3], last),
                            ));
                        }
                    }
                }
                VVCurveTo => {
                    let (offset, first) = if count % 4 == 0 {
                        (0, 0.0)
                    } else {
                        expect!((count - 1) % 4 == 0);
                        (1, operands[0])
                    };
                    for i in 0..((count - offset) / 4) {
                        let j = offset + 4 * i;
                        let first = if i == 0 { first } else { 0.0 };
                        build!(add_cubic(
                            (first, operands[j]),
                            (operands[j + 1], operands[j + 2]),
                            (0.0, operands[j + 3]),
                        ));
                    }
                }
                RCurveLine => {
                    expect!(count >= 2 && (count - 2) % 6 == 0);
                    for i in 0..((count - 2) / 6) {
                        let j = 6 * i;
                        build!(add_cubic(
                            (operands[j], operands[j + 1]),
                            (operands[j + 2], operands[j + 3]),
                            (operands[j + 4], operands[j + 5]),
                        ));
                    }
                    let j = count - 2;
                    build!(add_linear((operands[j], operands[j + 1])));
                }
                RLineCurve => {
                    expect!(count >= 6 && (count - 6) % 2 == 0);
                    for i in 0..((count - 6) / 2) {
                        let j = 2 * i;
                        build!(add_linear((operands[j], operands[j + 1])));
                    }
                    let j = count - 6;
                    build!(add_cubic(
                        (operands[j], operands[j + 1]),
                        (operands[j + 2], operands[j + 3]),
                        (operands[j + 4], operands[j + 5]),
                    ));
                }
                HStem | HStemHM | VStem | VStemHM | CntrMask | HintMask => {}
                _ => unreachable!(),
            }
            match operator {
                HMoveTo | VMoveTo | RMoveTo | HStem | HStemHM | VStem | VStemHM | CntrMask
                | HintMask => {
                    clear = true;
                }
                _ => {}
            }
        }
        builder.flush();
        builder.set_bounding_box((min.0, min.1, max.0, max.1));
        builder.set_horizontal_metrics(self.metrics.get(glyph_index));
        Ok(Some(builder.into()))
    }

Return the coordinate-wise maximum ignoring undefined values.

Return the coordinate-wise minimum ignoring undefined values.

Trait Implementations§

The resulting type after applying the + operator.
Performs the + operation. Read more
Performs the += operation. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
Performs the /= operation. Read more
Performs the /= operation. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
Performs the *= operation. Read more
Performs the *= operation. Read more
The resulting type after applying the - operator.
Performs the unary - operation. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
The resulting type after applying the - operator.
Performs the - operation. Read more
Performs the -= operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.