omap 0.10.1

Interact with or write new Open Orienteering Mapper omap-files
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
mod area_object;
mod line_object;
mod point_object;
mod text_object;

mod map_object;

use geo_types::Coord;
pub use linestring2bezier::{BezierSegment, BezierString};
use quick_xml::{
    Reader, Writer,
    events::{BytesEnd, BytesStart, BytesText, Event},
};
use std::collections::HashMap;

pub use area_object::{AreaObject, BezierPolygon, PatternRotation};
pub use line_object::LineObject;
pub use point_object::PointObject;
pub use text_object::{HorizontalAlign, TextGeometry, TextObject, VerticalAlign, WrapBox};

pub use map_object::MapObject;

use crate::{
    notes,
    utils::{from_file_coords, to_file_coords, try_get_attr},
};

use super::{Error, OmapSection, Result};

type FileCoord = (Coord<i32>, u8);

/// A coordinate starts a cubic Bézier segment.
pub const COORD_FLAG_CURVE_START: u8 = 1;
/// A coordinate closes the current path.
pub const COORD_FLAG_CLOSE_POINT: u8 = 2;
/// A coordinate is the endpoint of a line-symbol gap.
pub const COORD_FLAG_GAP_POINT: u8 = 4;
/// A coordinate closes an interior polygon ring.
pub const COORD_FLAG_HOLE_POINT: u8 = 16;
/// A coordinate is a forced dash point.
pub const COORD_FLAG_DASH_POINT: u8 = 32;

const COORD_FLAGS_RING_END: u8 = COORD_FLAG_CLOSE_POINT | COORD_FLAG_HOLE_POINT;

/// A mixed straight/cubic Bézier path with dash-point metadata on its
/// vertices.
///
/// `vertex_is_dash_point` contains one entry for the initial vertex followed
/// by one entry for every segment end, so its length is always
/// `geometry.num_segments() + 1`. For a closed path, its first and final
/// entries describe the same seam vertex and therefore have the same value.
#[derive(Debug, Clone)]
pub struct BezierPath {
    /// The straight and cubic segments forming the path.
    geometry: BezierString,
    /// Whether each path vertex carries [`COORD_FLAG_DASH_POINT`].
    vertex_is_dash_point: Vec<bool>,
}

impl BezierPath {
    fn new(geometry: BezierString, vertex_is_dash_point: Vec<bool>) -> Option<Self> {
        let segments = geometry.num_segments();
        if segments == 0 {
            return None;
        }

        let expected = segments + 1;
        if vertex_is_dash_point.len() != expected {
            return None;
        }

        let first_segment = geometry.segments().next()?;
        let last_segment = geometry.segments().last()?;
        if first_segment.start() == last_segment.end()
            && vertex_is_dash_point.first() != vertex_is_dash_point.last()
        {
            return None;
        }

        Some(Self {
            geometry,
            vertex_is_dash_point,
        })
    }

    /// Get the mixed straight/cubic geometry.
    pub fn geometry(&self) -> &BezierString {
        &self.geometry
    }

    /// Get the dash-point state of the initial vertex and every segment end.
    pub fn vertex_is_dash_point(&self) -> &[bool] {
        &self.vertex_is_dash_point
    }

    /// Transform every endpoint and Bézier handle while preserving path
    /// topology and dash-point metadata.
    ///
    /// `transform` should map equal coordinates to equal coordinates, as
    /// coordinate reprojection functions normally do.
    pub fn map_coords(mut self, transform: impl Fn(Coord) -> Coord) -> Self {
        for segment in self.geometry.segments_mut() {
            match segment {
                BezierSegment::Bezier(curve) => {
                    curve.start = transform(curve.start);
                    curve.handle1 = transform(curve.handle1);
                    curve.handle2 = transform(curve.handle2);
                    curve.end = transform(curve.end);
                }
                BezierSegment::Line(line) => {
                    line.start = transform(line.start);
                    line.end = transform(line.end);
                }
            }
        }
        self
    }

    /// Consume the path and return its geometry and vertex dash flags.
    pub fn into_parts(self) -> (BezierString, Vec<bool>) {
        (self.geometry, self.vertex_is_dash_point)
    }

    /// Iterate over segments paired with the dash-point state of their end
    /// vertices.
    ///
    /// The initial vertex's state is available as
    /// `vertex_is_dash_point().first()`.
    pub fn segments(&self) -> impl ExactSizeIterator<Item = (&BezierSegment, bool)> {
        self.geometry
            .segments()
            .zip(self.vertex_is_dash_point.iter().copied().skip(1))
    }
}

/// Build the exact mixed line/Bézier representation encoded by Mapper's raw
/// coordinate flags.
///
/// A coordinate with bit 0 set starts a cubic Bézier whose two handles and end
/// point are the following three coordinates. An end point may also start the
/// next curve, so it is deliberately visited again in that case.
fn bezier_from_raw_coords(coords: &[FileCoord]) -> Option<BezierPath> {
    let mut segments = Vec::new();
    let mut vertex_is_dash_point = coords
        .first()
        .map(|(_, flag)| flag & COORD_FLAG_DASH_POINT != 0)
        .into_iter()
        .collect::<Vec<_>>();
    let mut previous_anchor = None;
    let mut index = 0;

    while index < coords.len() {
        let (file_coord, flag) = coords[index];
        let coord = from_file_coords(file_coord);

        if let Some((previous_index, previous_coord)) = previous_anchor
            && previous_index != index
        {
            segments.push(BezierSegment::new(previous_coord, None, coord));
            vertex_is_dash_point.push(flag & COORD_FLAG_DASH_POINT != 0);
        }

        if flag & COORD_FLAG_CURVE_START != 0 && index + 3 < coords.len() {
            let handle1 = from_file_coords(coords[index + 1].0);
            let handle2 = from_file_coords(coords[index + 2].0);
            let end_index = index + 3;
            let end = from_file_coords(coords[end_index].0);
            segments.push(BezierSegment::new(coord, Some((handle1, handle2)), end));
            vertex_is_dash_point.push(coords[end_index].1 & COORD_FLAG_DASH_POINT != 0);
            previous_anchor = Some((end_index, end));

            if coords[end_index].1 & COORD_FLAG_CURVE_START != 0 {
                index = end_index;
            } else {
                index = end_index + 1;
            }
        } else {
            previous_anchor = Some((index, coord));
            index += 1;
        }
    }

    // A closed path repeats its first vertex as its final endpoint. Treat the
    // dash flag on either raw representation as metadata for that shared
    // vertex and expose the folded value at both ends of the vertex vector.
    if let (Some((first_coord, first_flag)), Some((last_coord, last_flag))) =
        (coords.first(), coords.last())
        && last_flag & COORD_FLAGS_RING_END != 0
        && first_coord == last_coord
        && (first_flag & COORD_FLAG_DASH_POINT != 0 || last_flag & COORD_FLAG_DASH_POINT != 0)
    {
        if let Some(first_is_dash_point) = vertex_is_dash_point.first_mut() {
            *first_is_dash_point = true;
        }
        if let Some(last_is_dash_point) = vertex_is_dash_point.last_mut() {
            *last_is_dash_point = true;
        }
    }

    BezierPath::new(BezierString::new(segments), vertex_is_dash_point)
}

fn file_coords_from_bezier(
    geometry: &BezierString,
    final_vertex_flags: u8,
) -> Result<Vec<FileCoord>> {
    let final_vertex = geometry
        .segments()
        .last()
        .map(BezierSegment::end)
        .ok_or(Error::ObjectError)?;

    let mut coords = Vec::with_capacity(geometry.num_points());
    for segment in geometry.segments() {
        match segment {
            BezierSegment::Bezier(curve) => {
                coords.push((to_file_coords(curve.start)?, COORD_FLAG_CURVE_START));
                coords.push((to_file_coords(curve.handle1)?, 0));
                coords.push((to_file_coords(curve.handle2)?, 0));
            }
            BezierSegment::Line(line) => {
                coords.push((to_file_coords(line.start)?, 0));
            }
        }
    }
    coords.push((to_file_coords(final_vertex)?, final_vertex_flags));
    Ok(coords)
}

fn parse_tags<R: std::io::BufRead>(reader: &mut Reader<R>) -> Result<HashMap<String, String>> {
    let mut buf = Vec::new();

    let mut tags = HashMap::new();
    loop {
        match reader.read_event_into(&mut buf)? {
            Event::Start(bytes_start) => {
                if matches!(bytes_start.local_name().as_ref(), b"t") {
                    let key = try_get_attr(&bytes_start, "k")?.unwrap_or(String::new());
                    let value = notes::parse(reader)?;
                    if !key.is_empty() && !value.is_empty() {
                        let _ = tags.insert(key, value);
                    }
                }
            }
            Event::End(bytes_end) => {
                if bytes_end.local_name().as_ref() == b"tags" {
                    break;
                }
            }
            Event::Eof => {
                return Err(Error::UnexpectedEof(OmapSection::Tags));
            }
            _ => (),
        }
    }
    Ok(tags)
}

fn write_tags<W: std::io::Write>(
    writer: &mut Writer<W>,
    tags: &HashMap<String, String>,
) -> Result<()> {
    writer.write_event(Event::Start(BytesStart::new("tags")))?;
    for (key, value) in tags {
        writer.write_event(Event::Start(
            BytesStart::new("t").with_attributes([("k", key.as_str())]),
        ))?;
        writer.write_event(Event::Text(BytesText::new(value)))?;
        writer.write_event(Event::End(BytesEnd::new("t")))?;
    }
    writer.write_event(Event::End(BytesEnd::new("tags")))?;
    Ok(())
}

/// Write raw map coords as the content of a `<coords>` element
fn write_raw_coords<W: std::io::Write>(writer: &mut Writer<W>, coords: &[FileCoord]) -> Result<()> {
    let bs =
        BytesStart::new("coords").with_attributes([("count", coords.len().to_string().as_str())]);
    writer.write_event(Event::Start(bs))?;
    let mut content = String::new();
    for (coord, flag) in coords {
        content.push_str(&coord.x.to_string());
        content.push(' ');
        content.push_str(&coord.y.to_string());
        if *flag != 0 {
            content.push(' ');
            content.push_str(&flag.to_string());
        }
        content.push(';');
    }
    writer.write_event(Event::Text(BytesText::new(&content)))?;
    writer.write_event(Event::End(BytesEnd::new("coords")))?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use geo_types::Coord;
    use linestring2bezier::{BezierCurve, BezierSegment, BezierString};

    use super::{
        BezierPath, COORD_FLAG_CURVE_START, COORD_FLAG_DASH_POINT, COORD_FLAGS_RING_END,
        bezier_from_raw_coords, file_coords_from_bezier,
    };

    #[test]
    fn dash_flags_align_with_segment_end_vertices() {
        let path = bezier_from_raw_coords(&[
            (Coord { x: 0, y: 0 }, COORD_FLAG_DASH_POINT),
            (Coord { x: 1_000, y: 0 }, 33),
            (Coord { x: 1_000, y: 1_000 }, 0),
            (Coord { x: 2_000, y: 1_000 }, 0),
            (Coord { x: 2_000, y: 0 }, 32),
            (Coord { x: 3_000, y: 0 }, 0),
        ]);
        assert!(path.is_some());
        let Some(path) = path else {
            return;
        };

        assert_eq!(path.geometry().num_segments(), 3);
        assert_eq!(path.vertex_is_dash_point(), [true, true, true, false]);
        assert!(matches!(path.geometry().0[0], BezierSegment::Line(_)));
        assert!(matches!(path.geometry().0[1], BezierSegment::Bezier(_)));
        assert!(matches!(path.geometry().0[2], BezierSegment::Line(_)));
        assert_eq!(
            path.segments()
                .map(|(_, end_is_dash_point)| end_is_dash_point)
                .collect::<Vec<_>>(),
            [true, true, false]
        );
    }

    #[test]
    fn closed_path_combines_first_and_closing_vertex_dash_flags() {
        let path = bezier_from_raw_coords(&[
            (Coord { x: 0, y: 0 }, 32),
            (Coord { x: 1_000, y: 0 }, 0),
            (Coord { x: 0, y: 0 }, 2),
        ]);
        assert!(path.is_some());
        let Some(path) = path else {
            return;
        };

        assert_eq!(path.geometry().num_segments(), 2);
        assert_eq!(path.vertex_is_dash_point(), [true, false, true]);
    }

    #[test]
    fn open_path_preserves_both_endpoint_dash_flags() {
        let path = bezier_from_raw_coords(&[
            (Coord { x: 0, y: 0 }, COORD_FLAG_DASH_POINT),
            (Coord { x: 1_000, y: 0 }, COORD_FLAG_DASH_POINT),
            (Coord { x: 2_000, y: 0 }, COORD_FLAG_DASH_POINT),
            (Coord { x: 3_000, y: 0 }, COORD_FLAG_DASH_POINT),
        ]);
        assert!(path.is_some());
        let Some(path) = path else {
            return;
        };

        assert_eq!(path.geometry().num_segments(), 3);
        assert_eq!(path.vertex_is_dash_point(), [true, true, true, true]);
    }

    #[test]
    fn internal_bezier_path_construction_enforces_invariants() {
        assert!(BezierPath::new(BezierString::empty(), Vec::new()).is_none());

        let open_geometry = BezierString::new(vec![BezierSegment::new(
            Coord { x: 0.0, y: 0.0 },
            None,
            Coord { x: 1.0, y: 0.0 },
        )]);
        assert!(BezierPath::new(open_geometry, vec![false]).is_none());

        let closed_geometry = BezierString::new(vec![
            BezierSegment::new(Coord { x: 0.0, y: 0.0 }, None, Coord { x: 1.0, y: 0.0 }),
            BezierSegment::new(Coord { x: 1.0, y: 0.0 }, None, Coord { x: 0.0, y: 0.0 }),
        ]);
        assert!(BezierPath::new(closed_geometry, vec![false, false, true]).is_none());
    }

    #[test]
    fn mapping_bezier_path_coords_preserves_structure_and_flags() {
        let geometry = BezierString::new(vec![BezierSegment::Bezier(BezierCurve::new(
            Coord { x: 0.0, y: 0.0 },
            Coord { x: 0.0, y: 1.0 },
            Coord { x: 1.0, y: 1.0 },
            Coord { x: 1.0, y: 0.0 },
        ))]);
        let path = BezierPath::new(geometry, vec![true, false]);
        assert!(path.is_some());
        let Some(path) = path else {
            return;
        };

        let mapped = path.map_coords(|coord| Coord {
            x: coord.x + 2.0,
            y: coord.y - 3.0,
        });

        assert_eq!(mapped.vertex_is_dash_point(), [true, false]);
        let Some(BezierSegment::Bezier(curve)) = mapped.geometry().segments().next() else {
            return;
        };
        assert_eq!(curve.start, Coord { x: 2.0, y: -3.0 });
        assert_eq!(curve.handle1, Coord { x: 2.0, y: -2.0 });
        assert_eq!(curve.handle2, Coord { x: 3.0, y: -2.0 });
        assert_eq!(curve.end, Coord { x: 3.0, y: -3.0 });
    }

    #[test]
    fn bezier_serialization_uses_exported_coordinate_flags() {
        let geometry = BezierString::new(vec![
            BezierSegment::Line(geo_types::Line::new(
                Coord { x: 0.0, y: 0.0 },
                Coord { x: 1.0, y: 0.0 },
            )),
            BezierSegment::Bezier(BezierCurve::new(
                Coord { x: 1.0, y: 0.0 },
                Coord { x: 1.0, y: 1.0 },
                Coord { x: 2.0, y: 1.0 },
                Coord { x: 2.0, y: 0.0 },
            )),
        ]);

        let coords = file_coords_from_bezier(&geometry, COORD_FLAGS_RING_END);
        assert!(coords.is_ok());
        let Ok(coords) = coords else {
            return;
        };
        assert_eq!(
            coords,
            [
                (Coord { x: 0, y: 0 }, 0),
                (Coord { x: 1_000, y: 0 }, COORD_FLAG_CURVE_START),
                (
                    Coord {
                        x: 1_000,
                        y: -1_000
                    },
                    0
                ),
                (
                    Coord {
                        x: 2_000,
                        y: -1_000
                    },
                    0
                ),
                (Coord { x: 2_000, y: 0 }, COORD_FLAGS_RING_END),
            ]
        );
    }
}