omap 0.6.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
use std::str::FromStr;

use quick_xml::{
    Reader, Writer,
    events::{BytesEnd, BytesStart, BytesText, Event},
};

use super::{PointSymbol, SymbolCommon};
use crate::{
    Code, Error, NonNegativeF64, Result,
    colors::{ColorSet, SymbolColor},
    utils::{parse_attr, parse_attr_raw, try_get_attr_raw},
};

/// A line symbol definition.
#[derive(Debug, Clone)]
pub struct LineSymbol {
    /// Common symbol properties.
    pub common: SymbolCommon,

    /// Optional border styling.
    pub border: Option<BorderStyle>,

    /// Optional point symbol placed at the start of the line.
    pub start_symbol: Option<PointSymbol>,
    /// Optional point symbol placed repeatedly along the line.
    pub mid_symbol: Option<MidSymbol>,
    /// Optional point symbol placed at the end of the line.
    pub end_symbol: Option<PointSymbol>,
    /// Optional point symbol placed on dashes.
    pub dash_symbol: Option<DashSymbol>,

    /// The line colour.
    pub color: SymbolColor,
    /// The line width in mm.
    pub line_width: NonNegativeF64,
    /// Minimum length required for the line to be drawn.
    pub minimum_length: NonNegativeF64,
    /// Offset applied at the start of the line.
    pub start_offset: NonNegativeF64,
    /// Offset applied at the end of the line.
    pub end_offset: NonNegativeF64,

    /// The dash style for this line.
    pub dash_style: DashStyle,
    /// The cap style for line ends.
    pub cap_style: CapStyle,
    /// The join style at line vertices.
    pub join_style: JoinStyle,
}

impl LineSymbol {
    /// Create a new empty line symbol with the given code and name.
    pub fn new(code: Code, name: impl Into<String>) -> LineSymbol {
        let common = SymbolCommon {
            code,
            name: name.into(),
            ..Default::default()
        };
        LineSymbol {
            common,
            border: None,
            start_symbol: None,
            mid_symbol: None,
            end_symbol: None,
            dash_symbol: None,
            color: SymbolColor::NoColor,
            line_width: NonNegativeF64::default(),
            minimum_length: NonNegativeF64::default(),
            start_offset: NonNegativeF64::default(),
            end_offset: NonNegativeF64::default(),
            dash_style: DashStyle::default(),
            cap_style: CapStyle::default(),
            join_style: JoinStyle::default(),
        }
    }

    /// Get the display name of this line symbol.
    pub fn get_name(&self) -> &str {
        &self.common.name
    }

    /// Set the line colour (builder-style).
    pub fn with_color(mut self, color: SymbolColor) -> Self {
        self.color = color;
        self
    }

    /// Set the line width in mm (builder-style).
    pub fn with_line_width(mut self, width: NonNegativeF64) -> Self {
        self.line_width = width;
        self
    }

    /// Set the start symbol (builder-style).
    pub fn with_start_symbol(mut self, symbol: PointSymbol) -> Self {
        self.start_symbol = Some(symbol);
        self
    }

    /// Set the mid symbol (builder-style).
    pub fn with_mid_symbol(mut self, symbol: MidSymbol) -> Self {
        self.mid_symbol = Some(symbol);
        self
    }

    /// Set the end symbol (builder-style).
    pub fn with_end_symbol(mut self, symbol: PointSymbol) -> Self {
        self.end_symbol = Some(symbol);
        self
    }

    /// Set the dash symbol (builder-style).
    pub fn with_dash_symbol(mut self, symbol: DashSymbol) -> Self {
        self.dash_symbol = Some(symbol);
        self
    }

    /// Set the border style (builder-style).
    pub fn with_border(mut self, border: BorderStyle) -> Self {
        self.border = Some(border);
        self
    }

    /// Set the cap style (builder-style).
    pub fn with_cap_style(mut self, cap: CapStyle) -> Self {
        self.cap_style = cap;
        self
    }

    /// Set the join style (builder-style).
    pub fn with_join_style(mut self, join: JoinStyle) -> Self {
        self.join_style = join;
        self
    }

    /// Set the dash style (builder-style).
    pub fn with_dash_style(mut self, dash: DashStyle) -> Self {
        self.dash_style = dash;
        self
    }

    /// Mark as a helper symbol (builder-style).
    pub fn as_helper_symbol(mut self) -> Self {
        self.common.is_helper_symbol = true;
        self
    }
}

/// A dash symbol placed on dashes of a dashed line.
#[derive(Debug, Clone)]
pub struct DashSymbol {
    /// Whether to suppress the dash symbol at the line ends.
    pub suppress_dash_symbol_at_ends: bool,
    /// Whether the dash symbol is scaled with the dash length.
    pub scale_dash_symbol: bool,
    /// The point symbol placed on each dash.
    pub dash_symbol: PointSymbol,
}

#[derive(Debug, Clone)]
pub struct MidSymbol {
    /// Number of mid symbols per
    pub mid_symbols_per_spot: u16,
    /// Distance in mm
    pub mid_symbol_distance: NonNegativeF64,
    /// Min number of mid symbols
    pub minimum_mid_symbol_count: u16,
    /// Min number of mid symbols if the line is closed
    pub minimum_mid_symbol_count_when_closed: u16,
    /// Whether to always show at least 1 mid symbol
    pub show_at_least_one_mid_symbol: bool,
    /// How to place the mid symbols
    pub mid_symbol_placement: MidSymbolPlacement,
    /// The mid symbol point symbol
    pub mid_symbol: PointSymbol,
}

/// Cap style for line endpoints.
#[derive(Debug, Clone, Copy, Default)]
pub enum CapStyle {
    /// Flat cap (no extension beyond endpoint).
    #[default]
    Flat = 0,
    /// Round cap (semi-circular extension).
    Round = 1,
    /// Square cap (rectangular extension).
    Square = 2,
    /// Pointed cap (tapered extension).
    Pointed = 3,
}

impl FromStr for CapStyle {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        match s {
            "0" => Ok(CapStyle::Flat),
            "1" => Ok(CapStyle::Round),
            "2" => Ok(CapStyle::Square),
            "3" => Ok(CapStyle::Pointed),
            _ => Err(Error::SymbolError(format!("Unknown CapStyle {s}"))),
        }
    }
}

/// Join style at line vertices.
#[derive(Debug, Clone, Copy, Default)]
pub enum JoinStyle {
    /// Bevel join.
    Bevel = 0,
    /// Miter join.
    #[default]
    Miter = 1,
    /// Round join.
    Round = 2,
}

impl FromStr for JoinStyle {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        match s {
            "0" => Ok(JoinStyle::Bevel),
            "1" => Ok(JoinStyle::Miter),
            "2" => Ok(JoinStyle::Round),
            _ => Err(Error::SymbolError(format!("Unknown JoinStyle {s}"))),
        }
    }
}

#[allow(clippy::enum_variant_names)]
#[derive(Debug, Clone, Copy, Default)]
pub enum MidSymbolPlacement {
    /// Mid symbols on every dash
    #[default]
    CenterOfDash = 0,
    /// Mid symbols on the center of a dash group
    CenterOfDashGroup = 1,
    /// Mid symbols on the main gap (i.e. not between dashes in a group)
    CenterOfGap = 2,
    //NoMidSymbols = 99,
}

impl FromStr for MidSymbolPlacement {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        match s {
            "0" => Ok(MidSymbolPlacement::CenterOfDash),
            "1" => Ok(MidSymbolPlacement::CenterOfDashGroup),
            "2" => Ok(MidSymbolPlacement::CenterOfGap),
            _ => Err(Error::SymbolError(format!(
                "Unknown MidSymbolPlacement {s}"
            ))),
        }
    }
}

/// Whether the border is symmetric (same on both sides) or asymmetric.
#[derive(Debug, Clone)]
pub enum BorderStyle {
    /// Both sides share the same border definition.
    SymmetricBorder {
        /// The shared border.
        both: LineSymbolBorder,
    },
    /// Left and right borders have different definitions.
    AsymmetricBorder {
        /// The left border.
        left: LineSymbolBorder,
        /// The right border.
        right: LineSymbolBorder,
    },
}

/// A single border line definition alongside the main line.
#[derive(Debug, Clone)]
pub struct LineSymbolBorder {
    /// Border colour.
    pub color: SymbolColor,
    /// Border width in mm.
    pub width: NonNegativeF64,
    /// Shift from the main line in mm.
    pub shift: NonNegativeF64,
    /// Optional dashed pattern for the border.
    pub dashed: Option<BorderDash>,
}

impl LineSymbolBorder {
    fn parse(element: &BytesStart<'_>, color_set: &ColorSet) -> Result<Self> {
        let color_index = try_get_attr_raw(element, "color").unwrap_or(-1);
        let color = SymbolColor::from_index(color_index, color_set);
        let width =
            NonNegativeF64::from_file_value(try_get_attr_raw(element, "width").unwrap_or(0));
        let shift =
            NonNegativeF64::from_file_value(try_get_attr_raw(element, "shift").unwrap_or(0));
        let is_dashed = try_get_attr_raw(element, "dashed").unwrap_or(false);
        let dashed = if is_dashed {
            Some(BorderDash {
                dash_length: NonNegativeF64::from_file_value(
                    try_get_attr_raw(element, "dash_length").unwrap_or(0),
                ),
                break_length: NonNegativeF64::from_file_value(
                    try_get_attr_raw(element, "break_length").unwrap_or(0),
                ),
            })
        } else {
            None
        };
        Ok(LineSymbolBorder {
            color,
            width,
            shift,
            dashed,
        })
    }

    fn write<W: std::io::Write>(&self, writer: &mut Writer<W>, color_set: &ColorSet) -> Result<()> {
        let mut bs = BytesStart::new("border").with_attributes([
            (
                "color",
                self.color.get_priority(color_set).to_string().as_str(),
            ),
            ("width", self.width.to_file_value()?.to_string().as_str()),
            ("shift", self.shift.to_file_value()?.to_string().as_str()),
        ]);
        if let Some(border_dash) = &self.dashed {
            bs.push_attribute(("dashed", "true"));
            bs.push_attribute((
                "break_length",
                border_dash
                    .break_length
                    .to_file_value()?
                    .to_string()
                    .as_str(),
            ));
            bs.push_attribute((
                "dash_length",
                border_dash
                    .dash_length
                    .to_file_value()?
                    .to_string()
                    .as_str(),
            ));
        }
        writer.write_event(Event::Empty(bs))?;
        Ok(())
    }
}

/// Dash parameters for a border line.
#[derive(Debug, Clone)]
pub struct BorderDash {
    /// Length of each dash in mm.
    pub dash_length: NonNegativeF64,
    /// Length of the gap between dashes in mm.
    pub break_length: NonNegativeF64,
}

/// The dash style of a line symbol.
#[derive(Debug, Clone)]
pub enum DashStyle {
    /// The line is dashed.
    Dashed {
        /// Length of each dash in mm.
        dash_length: NonNegativeF64,
        /// Length of the gap between dashes in mm.
        break_length: NonNegativeF64,
        /// Grouping of dashes.
        dash_group: GroupDashes,
    },
    /// The line is not dashed.
    NotDashed {
        /// Segment length in mm.
        segment_length: NonNegativeF64,
        /// End length in mm.
        end_length: NonNegativeF64,
    },
}

impl Default for DashStyle {
    fn default() -> Self {
        DashStyle::NotDashed {
            segment_length: NonNegativeF64::clamped_from(4.),
            end_length: NonNegativeF64::default(),
        }
    }
}

/// Grouping of dashes in a dash style.
#[derive(Debug, Clone)]
pub enum GroupDashes {
    /// Dashes are grouped together.
    Grouped {
        /// allowed to be in range 2..=4
        dashes_in_group: u8,
        in_group_break_length: NonNegativeF64,
    },
    /// Dashes are not grouped.
    UnGrouped { half_outer_dashes: bool },
}

impl Default for GroupDashes {
    fn default() -> Self {
        GroupDashes::UnGrouped {
            half_outer_dashes: false,
        }
    }
}

impl LineSymbol {
    pub(super) fn parse<R: std::io::BufRead>(
        reader: &mut Reader<R>,
        color_set: &ColorSet,
        attributes: SymbolCommon,
    ) -> Result<LineSymbol> {
        let mut common = attributes;
        let mut color = SymbolColor::NoColor;
        let mut line_width = NonNegativeF64::default();
        let mut minimum_length = NonNegativeF64::default();
        let mut start_offset = NonNegativeF64::default();
        let mut end_offset = NonNegativeF64::default();
        let mut cap_style = CapStyle::default();
        let mut join_style = JoinStyle::default();
        let mut dashed = false;
        let mut segment_length = 4000;
        let mut end_length = 0;
        let mut dash_length = 4000;
        let mut break_length = 1000;
        let mut dashes_in_group = 1;
        let mut in_group_break_length = 500;
        let mut half_outer_dashes = false;
        let mut show_at_least_one_symbol = true;
        let mut minimum_mid_symbol_count = 0;
        let mut minimum_mid_symbol_count_when_closed = 0;
        let mut mid_symbols_per_spot = 1;
        let mut mid_symbol_distance = 0;
        let mut mid_symbol_placement = MidSymbolPlacement::default();
        let mut suppress_dash_symbol_at_ends = false;
        let mut scale_dash_symbol = true;

        let mut start_symbol = None;
        let mut mid_symbol_point = None;
        let mut end_symbol = None;
        let mut dash_symbol_point = None;
        let mut border = None;

        let mut buf = Vec::new();
        loop {
            match reader.read_event_into(&mut buf)? {
                Event::Start(e) => match e.local_name().as_ref() {
                    b"description" => {
                        if let Event::Text(text) = reader.read_event_into(&mut buf)? {
                            common.description = String::from_utf8(text.to_vec())?;
                        }
                    }
                    b"line_symbol" => {
                        let color_index = try_get_attr_raw(&e, "color").unwrap_or(-1);
                        color = SymbolColor::from_index(color_index, color_set);
                        line_width = NonNegativeF64::from_file_value(
                            try_get_attr_raw(&e, "line_width").unwrap_or(0),
                        );
                        minimum_length = NonNegativeF64::from_file_value(
                            try_get_attr_raw(&e, "minimum_length").unwrap_or(0),
                        );
                        join_style = try_get_attr_raw(&e, "join_style").unwrap_or_default();
                        cap_style = try_get_attr_raw(&e, "cap_style").unwrap_or_default();
                        start_offset = NonNegativeF64::from_file_value(
                            try_get_attr_raw(&e, "start_offset").unwrap_or(0),
                        );
                        end_offset = NonNegativeF64::from_file_value(
                            try_get_attr_raw(&e, "end_offset").unwrap_or(0),
                        );
                        dashed = try_get_attr_raw(&e, "dashed").unwrap_or(false);
                        segment_length = try_get_attr_raw(&e, "segment_length").unwrap_or(4000);
                        end_length = try_get_attr_raw(&e, "end_length").unwrap_or(0);
                        dash_length = try_get_attr_raw(&e, "dash_length").unwrap_or(4000);
                        break_length = try_get_attr_raw(&e, "break_length").unwrap_or(1000);
                        dashes_in_group = try_get_attr_raw(&e, "dashes_in_group").unwrap_or(1);
                        in_group_break_length =
                            try_get_attr_raw(&e, "in_group_break_length").unwrap_or(500);
                        half_outer_dashes =
                            try_get_attr_raw(&e, "half_outer_dashes").unwrap_or(false);
                        show_at_least_one_symbol =
                            try_get_attr_raw(&e, "show_at_least_one_symbol").unwrap_or(false);
                        minimum_mid_symbol_count =
                            try_get_attr_raw(&e, "minimum_mid_symbol_count").unwrap_or(0);
                        minimum_mid_symbol_count_when_closed =
                            try_get_attr_raw(&e, "minimum_mid_symbol_count_when_closed")
                                .unwrap_or(0);
                        mid_symbols_per_spot =
                            try_get_attr_raw(&e, "mid_symbols_per_spot").unwrap_or(1);
                        mid_symbol_distance =
                            try_get_attr_raw(&e, "mid_symbol_distance").unwrap_or(0);
                        mid_symbol_placement =
                            try_get_attr_raw(&e, "mid_symbol_placement").unwrap_or_default();
                        suppress_dash_symbol_at_ends =
                            try_get_attr_raw(&e, "suppress_dash_symbol_at_ends").unwrap_or(false);
                        scale_dash_symbol =
                            try_get_attr_raw(&e, "scale_dash_symbol").unwrap_or(true);
                    }
                    b"start_symbol" => {
                        start_symbol = Self::parse_sub_point_symbol(reader, color_set)?;
                    }
                    b"mid_symbol" => {
                        mid_symbol_point = Self::parse_sub_point_symbol(reader, color_set)?;
                    }
                    b"end_symbol" => {
                        end_symbol = Self::parse_sub_point_symbol(reader, color_set)?;
                    }
                    b"dash_symbol" => {
                        dash_symbol_point = Self::parse_sub_point_symbol(reader, color_set)?;
                    }
                    b"borders" => {
                        border = Self::parse_borders(reader, &e, color_set)?;
                    }
                    _ => {}
                },
                Event::Empty(e) => {
                    if e.local_name().as_ref() == b"icon"
                        && let Some(src) = try_get_attr_raw(&e, "src")
                    {
                        common.custom_icon = Some(src);
                    }
                }
                Event::End(e) => {
                    if e.local_name().as_ref() == b"symbol" {
                        break;
                    }
                }
                Event::Eof => {
                    return Err(Error::ParseOmapFileError(
                        "Unexpected EOF in LineSymbol parsing".to_string(),
                    ));
                }
                _ => {}
            }
        }

        let dash_style = if dashed {
            let dash_group = if dashes_in_group >= 2 {
                GroupDashes::Grouped {
                    dashes_in_group,
                    in_group_break_length: NonNegativeF64::from_file_value(in_group_break_length),
                }
            } else {
                GroupDashes::UnGrouped { half_outer_dashes }
            };
            DashStyle::Dashed {
                dash_length: NonNegativeF64::from_file_value(dash_length),
                break_length: NonNegativeF64::from_file_value(break_length),
                dash_group,
            }
        } else {
            DashStyle::NotDashed {
                segment_length: NonNegativeF64::from_file_value(segment_length),
                end_length: NonNegativeF64::from_file_value(end_length),
            }
        };

        let mid_symbol = mid_symbol_point.map(|ps| MidSymbol {
            mid_symbols_per_spot,
            mid_symbol_distance: NonNegativeF64::from_file_value(mid_symbol_distance),
            minimum_mid_symbol_count,
            minimum_mid_symbol_count_when_closed,
            show_at_least_one_mid_symbol: show_at_least_one_symbol,
            mid_symbol_placement,
            mid_symbol: ps,
        });

        let dash_symbol = dash_symbol_point.map(|ps| DashSymbol {
            suppress_dash_symbol_at_ends,
            scale_dash_symbol,
            dash_symbol: ps,
        });

        Ok(LineSymbol {
            common,
            border,
            start_symbol,
            mid_symbol,
            end_symbol,
            dash_symbol,
            color,
            line_width,
            minimum_length,
            start_offset,
            end_offset,
            dash_style,
            cap_style,
            join_style,
        })
    }

    /// Parse a sub point symbol (start/mid/end/dash) wrapped in its container element
    fn parse_sub_point_symbol<R: std::io::BufRead>(
        reader: &mut Reader<R>,
        color_set: &ColorSet,
    ) -> Result<Option<PointSymbol>> {
        let mut buf = Vec::new();
        let mut result = None;
        loop {
            match reader.read_event_into(&mut buf)? {
                Event::Start(e) => {
                    if e.local_name().as_ref() == b"symbol" {
                        let mut sub_common = SymbolCommon::default();
                        for attr in e.attributes().filter_map(std::result::Result::ok) {
                            match attr.key.local_name().as_ref() {
                                b"name" => {
                                    sub_common.name =
                                        parse_attr(attr, e.decoder()).unwrap_or(sub_common.name);
                                }
                                b"code" => {
                                    sub_common.code =
                                        parse_attr_raw(attr.value).unwrap_or_default();
                                }
                                _ => {}
                            }
                        }
                        result = Some(PointSymbol::parse(reader, color_set, sub_common)?);
                    }
                }
                Event::End(e) => match e.local_name().as_ref() {
                    b"start_symbol" | b"mid_symbol" | b"end_symbol" | b"dash_symbol" => break,
                    _ => (),
                },
                Event::Eof => {
                    return Err(Error::ParseOmapFileError(
                        "Unexpected EOF parsing sub point symbol".to_string(),
                    ));
                }
                _ => {}
            }
        }
        // If the point symbol is empty. Drop it
        if let Some(ps) = &result
            && ps.inner_color == SymbolColor::NoColor
            && ps.outer_color == SymbolColor::NoColor
            && ps.elements.is_empty()
        {
            return Ok(None);
        }
        Ok(result)
    }

    /// Parse borders element
    fn parse_borders<R: std::io::BufRead>(
        reader: &mut Reader<R>,
        element: &BytesStart<'_>,
        color_set: &ColorSet,
    ) -> Result<Option<BorderStyle>> {
        let borders_different = try_get_attr_raw(element, "borders_different").unwrap_or(false);
        let mut left = None;
        let mut right = None;
        let mut buf = Vec::new();
        loop {
            match reader.read_event_into(&mut buf)? {
                Event::Empty(e) | Event::Start(e) => {
                    if e.local_name().as_ref() == b"border" {
                        let b = LineSymbolBorder::parse(&e, color_set)?;
                        if left.is_none() {
                            left = Some(b);
                        } else {
                            right = Some(b);
                        }
                    }
                }
                Event::End(e) => {
                    if e.local_name().as_ref() == b"borders" {
                        break;
                    }
                }
                Event::Eof => {
                    return Err(Error::ParseOmapFileError(
                        "Unexpected EOF parsing borders".to_string(),
                    ));
                }
                _ => {}
            }
        }
        match (left, right) {
            (Some(l), Some(r)) if borders_different => {
                Ok(Some(BorderStyle::AsymmetricBorder { left: l, right: r }))
            }
            (Some(both), _) => Ok(Some(BorderStyle::SymmetricBorder { both })),
            _ => Ok(None),
        }
    }

    pub(super) fn write<W: std::io::Write>(
        &self,
        writer: &mut Writer<W>,
        color_set: &ColorSet,
        id: Option<usize>,
    ) -> Result<()> {
        let mut bs = BytesStart::new("symbol").with_attributes([
            ("type", "2"),
            ("code", self.common.code.to_string().as_str()),
            (
                "name",
                quick_xml::escape::escape(self.common.name.as_str()).as_ref(),
            ),
        ]);
        if let Some(id) = id {
            bs.push_attribute(("id", id.to_string().as_str()));
        }
        if self.common.is_hidden {
            bs.push_attribute(("is_hidden", "true"));
        }
        if self.common.is_helper_symbol {
            bs.push_attribute(("is_helper_symbol", "true"));
        }
        if self.common.is_protected {
            bs.push_attribute(("is_protected", "true"));
        }

        writer.write_event(Event::Start(bs))?;
        if !self.common.description.is_empty() {
            writer.write_event(Event::Start(BytesStart::new("description")))?;
            writer.write_event(Event::Text(BytesText::new(&self.common.description)))?;
            writer.write_event(Event::End(BytesEnd::new("description")))?;
        }

        let main_color = self.color.get_priority(color_set);

        let mut bs = BytesStart::new("line_symbol").with_attributes([
            ("color", main_color.to_string().as_str()),
            (
                "line_width",
                self.line_width.to_file_value()?.to_string().as_str(),
            ),
            (
                "minimum_length",
                self.minimum_length.to_file_value()?.to_string().as_str(),
            ),
            ("join_style", (self.join_style as u8).to_string().as_str()),
            ("cap_style", (self.cap_style as u8).to_string().as_str()),
            (
                "start_offset",
                self.start_offset.to_file_value()?.to_string().as_str(),
            ),
            (
                "end_offset",
                self.end_offset.to_file_value()?.to_string().as_str(),
            ),
        ]);

        match &self.dash_style {
            DashStyle::Dashed {
                dash_length,
                break_length,
                dash_group,
            } => {
                bs.push_attribute(("dashed", "true"));
                bs.push_attribute((
                    "dash_length",
                    dash_length.to_file_value()?.to_string().as_str(),
                ));
                bs.push_attribute((
                    "break_length",
                    break_length.to_file_value()?.to_string().as_str(),
                ));
                match dash_group {
                    GroupDashes::Grouped {
                        dashes_in_group,
                        in_group_break_length,
                    } => {
                        bs.push_attribute((
                            "dashes_in_group",
                            dashes_in_group.to_string().as_str(),
                        ));
                        bs.push_attribute((
                            "in_group_break_length",
                            in_group_break_length.to_file_value()?.to_string().as_str(),
                        ));
                    }
                    GroupDashes::UnGrouped { half_outer_dashes } => {
                        bs.push_attribute(("dashes_in_group", "1"));
                        if *half_outer_dashes {
                            bs.push_attribute(("half_outer_dashes", "true"));
                        }
                    }
                }
            }
            DashStyle::NotDashed {
                segment_length,
                end_length,
            } => {
                bs.push_attribute((
                    "segment_length",
                    segment_length.to_file_value()?.to_string().as_str(),
                ));
                bs.push_attribute((
                    "end_length",
                    end_length.to_file_value()?.to_string().as_str(),
                ));
            }
        }
        if let Some(mid_symbol) = &self.mid_symbol {
            bs.push_attribute((
                "mid_symbol_distance",
                mid_symbol
                    .mid_symbol_distance
                    .to_file_value()?
                    .to_string()
                    .as_str(),
            ));
            bs.push_attribute((
                "mid_symbol_placement",
                (mid_symbol.mid_symbol_placement as u8).to_string().as_str(),
            ));
            bs.push_attribute((
                "mid_symbols_per_spot",
                mid_symbol.mid_symbols_per_spot.to_string().as_str(),
            ));
            bs.push_attribute((
                "minimum_mid_symbol_count",
                mid_symbol.minimum_mid_symbol_count.to_string().as_str(),
            ));
            bs.push_attribute((
                "minimum_mid_symbol_count_when_closed",
                mid_symbol
                    .minimum_mid_symbol_count_when_closed
                    .to_string()
                    .as_str(),
            ));
            if mid_symbol.show_at_least_one_mid_symbol {
                bs.push_attribute(("show_at_least_one_mid_symbol", "true"));
            }
        }
        if let Some(dash_symbol) = &self.dash_symbol {
            if dash_symbol.scale_dash_symbol {
                bs.push_attribute(("scale_dash_symbol", "true"));
            }
            if dash_symbol.suppress_dash_symbol_at_ends {
                bs.push_attribute(("suppress_dash_symbol_at_ends", "true"));
            }
        }
        writer.write_event(Event::Start(bs))?;

        if let Some(border) = &self.border {
            match border {
                BorderStyle::SymmetricBorder { both } => {
                    writer.write_event(Event::Start(BytesStart::new("borders")))?;
                    both.write(writer, color_set)?;
                    writer.write_event(Event::End(BytesEnd::new("borders")))?;
                }
                BorderStyle::AsymmetricBorder { left, right } => {
                    writer.write_event(Event::Start(
                        BytesStart::new("borders").with_attributes([("borders_different", "true")]),
                    ))?;
                    left.write(writer, color_set)?;
                    right.write(writer, color_set)?;
                    writer.write_event(Event::End(BytesEnd::new("borders")))?;
                }
            }
        }
        if let Some(start_symbol) = &self.start_symbol {
            writer.write_event(Event::Start(BytesStart::new("start_symbol")))?;
            start_symbol.write(writer, color_set, None)?;
            writer.write_event(Event::End(BytesEnd::new("start_symbol")))?;
        }
        if let Some(mid_symbol) = &self.mid_symbol {
            writer.write_event(Event::Start(BytesStart::new("mid_symbol")))?;
            mid_symbol.mid_symbol.write(writer, color_set, None)?;
            writer.write_event(Event::End(BytesEnd::new("mid_symbol")))?;
        }
        if let Some(dash_symbol) = &self.dash_symbol {
            writer.write_event(Event::Start(BytesStart::new("dash_symbol")))?;
            dash_symbol.dash_symbol.write(writer, color_set, None)?;
            writer.write_event(Event::End(BytesEnd::new("dash_symbol")))?;
        }
        if let Some(end_symbol) = &self.end_symbol {
            writer.write_event(Event::Start(BytesStart::new("end_symbol")))?;
            end_symbol.write(writer, color_set, None)?;
            writer.write_event(Event::End(BytesEnd::new("end_symbol")))?;
        }
        writer.write_event(Event::End(BytesEnd::new("line_symbol")))?;

        if let Some(icon) = &self.common.custom_icon {
            writer.write_event(Event::Empty(
                BytesStart::new("icon").with_attributes([("src", icon.as_str())]),
            ))?;
        }
        writer.write_event(Event::End(BytesEnd::new("symbol")))?;
        Ok(())
    }
}