mln-rs-style 0.0.21

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
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
// The style's text is read into a `serde_json_borrow` tree, whose strings
// borrow from the text and whose objects keep their members in document
// order (conversion/borrowed.rs). The message of a JSON
// syntax error is serde_json's.

use std::sync::Arc;

use crate::collections::VecSet;
use crate::constants::DEFAULT_TRANSITION_DURATION;
use crate::conversion::borrowed::{Json, JsonObject, borrowed_json_to_value, member, members};
use crate::conversion::{Convertible, Error, convert};
use crate::expression::GlobalStateMap;
use crate::font_face::{FontFace, FontFaceRange, FontFaces};
use crate::geo::LatLng;
use crate::hash::{HashMap, HashSet};
use crate::layer::{FontStack, InvalidProperty, Layer, convert_layer_with, set_paint_properties};
use crate::light::{Light, convert_light};
use crate::log_target;
use crate::member::{Members, Place, is_extension_member};
use crate::projection::{Projection, convert_projection};
use crate::property::transition::TransitionOptions;
use crate::registry::StyleRegistry;
use crate::sky::{Sky, convert_sky};
use crate::source::{Source, convert_source};
use crate::sprite::Sprite;
use crate::terrain::{Terrain, convert_terrain};

/// What `Parser::parse` returns: a style that isn't one fails with
/// [`ErrorKind::InvalidArgument`](crate::error::ErrorKind).
pub(crate) type StyleParseResult = Result<(), crate::error::Error>;

/// Reads a style's JSON, against the expression functions and layer types of
/// a [`StyleRegistry`].
pub struct Parser {
    registry: Arc<StyleRegistry>,
    pub sprites: Vec<Sprite>,
    pub glyph_url: String,
    pub font_faces: Option<Arc<FontFaces>>,
    pub sources: Vec<Source>,
    pub(crate) layers: Vec<Layer>,

    pub(crate) transition: TransitionOptions,
    pub(crate) light: Light,
    pub(crate) sky: Option<Sky>,
    pub(crate) terrain: Option<Terrain>,
    pub(crate) projection: Projection,
    /// The root's extension members.
    pub(crate) members: Members,

    /// Default values of the root "state" property, keyed by state property
    /// name, used by the "global-state" expression.
    pub(crate) global_state_defaults: GlobalStateMap,

    pub(crate) name: String,
    pub(crate) lat_lng: LatLng,
    pub(crate) zoom: f64,
    pub(crate) bearing: f64,
    pub(crate) pitch: f64,
    pub(crate) roll: f64,
    pub(crate) center_altitude: f64,
}

impl Parser {
    /// A parser of styles whose layers and expressions are `registry`'s.
    #[must_use]
    pub fn new(registry: &Arc<StyleRegistry>) -> Self {
        Parser {
            registry: Arc::clone(registry),
            sprites: Vec::new(),
            glyph_url: String::new(),
            font_faces: None,
            sources: Vec::new(),
            layers: Vec::new(),
            transition: TransitionOptions::with_duration(DEFAULT_TRANSITION_DURATION),
            light: Light::new(),
            sky: None,
            terrain: None,
            projection: Projection::new(),
            members: Members::default(),
            global_state_defaults: GlobalStateMap::new(),
            name: String::new(),
            lat_lng: LatLng::default(),
            zoom: 0.0,
            bearing: 0.0,
            pitch: 0.0,
            roll: 0.0,
            center_altitude: 0.0,
        }
    }
}

impl Parser {
    /// The style's projection.
    #[must_use]
    pub fn projection(&self) -> &Projection {
        &self.projection
    }
}

fn is_number(value: &Json<'_>) -> bool {
    value.is_number()
}

fn number_or_zero(value: &Json<'_>) -> f64 {
    value.as_f64().unwrap_or(0.0)
}

impl Parser {
    pub fn parse(&mut self, json: &str) -> StyleParseResult {
        let document: Json<'_> = match serde_json::from_str(json) {
            Ok(document) => document,
            Err(error) => return Err(crate::error::Error::invalid_argument(error.to_string())),
        };

        let Some(object) = document.as_object() else {
            return Err(crate::error::Error::invalid_argument("style must be an object"));
        };

        if let Some(version_value) = member(object, "version") {
            // A number's integer part; anything else is no version.
            let version = if is_number(version_value) {
                version_value.as_i64().unwrap_or_else(|| number_or_zero(version_value) as i64)
            } else {
                0
            };
            if version != 8 {
                tracing::warn!(
                    target: log_target::PARSE_STYLE,
                    "current renderer implementation only supports style spec version 8; using an outdated style will cause rendering errors"
                );
            }
        }

        if let Some(value) = member(object, "name")
            && let Some(name) = value.as_str()
        {
            self.name = name.to_owned();
        }

        if let Some(value) = member(object, "center") {
            let mut error = Error::default();
            match convert::<LatLng>(&Convertible::from(value), &mut error) {
                Some(lat_lng) => self.lat_lng = lat_lng,
                None => {
                    tracing::warn!(target: log_target::PARSE_STYLE, "center coordinate must be a longitude, latitude pair");
                }
            }
        }

        if let Some(value) = member(object, "centerAltitude")
            && is_number(value)
        {
            self.center_altitude = number_or_zero(value);
        }

        if let Some(value) = member(object, "zoom")
            && is_number(value)
        {
            self.zoom = number_or_zero(value);
        }

        if let Some(value) = member(object, "bearing")
            && is_number(value)
        {
            self.bearing = number_or_zero(value);
        }

        if let Some(value) = member(object, "pitch")
            && is_number(value)
        {
            self.pitch = number_or_zero(value);
        }

        if let Some(value) = member(object, "roll")
            && is_number(value)
        {
            self.roll = number_or_zero(value);
        }

        if let Some(value) = member(object, "transition") {
            self.parse_transition(value);
        }

        if let Some(value) = member(object, "light") {
            self.parse_light(value);
        }

        if let Some(value) = member(object, "terrain") {
            self.parse_terrain(value);
        }
        if let Some(value) = member(object, "sky") {
            self.parse_sky(value);
        }

        if let Some(value) = member(object, "projection") {
            self.parse_projection(value);
        }

        if let Some(value) = member(object, "state") {
            self.parse_state(value);
        }

        if let Some(value) = member(object, "sources") {
            self.parse_sources(value);
        }

        if let Some(value) = member(object, "layers") {
            self.parse_layers(value);
        }

        if let Some(value) = member(object, "sprite") {
            self.parse_sprites(value);
        }

        if let Some(glyphs) = member(object, "glyphs")
            && let Some(glyphs) = glyphs.as_str()
        {
            self.glyph_url = glyphs.to_owned();
        }

        if let Some(faces) = member(object, "font-faces")
            && let Some(faces) = faces.as_object()
        {
            self.font_faces = Some(Arc::new(parse_font_faces(faces)));
        }

        for (name, value) in members(object) {
            if is_extension_member(name) {
                self.parse_member(name, value);
            }
        }

        // Call for side effect of logging warnings for invalid values.
        let _ = self.font_stacks();

        Ok(())
    }

    fn parse_transition(&mut self, value: &Json<'_>) {
        let mut error = Error::default();
        let Some(converted) = convert::<TransitionOptions>(&Convertible::from(value), &mut error) else {
            tracing::warn!(target: log_target::PARSE_STYLE, error = %error.message, "Style transition is invalid");
            return;
        };

        self.transition = converted;
    }

    fn parse_light(&mut self, value: &Json<'_>) {
        let mut error = Error::default();
        let Some(converted) = convert_light(&Convertible::from(value), self.registry.functions(), &mut error) else {
            tracing::warn!(target: log_target::PARSE_STYLE, error = %error.message, "Style light is invalid");
            return;
        };

        self.light = converted;
    }

    fn parse_sky(&mut self, value: &Json<'_>) {
        let mut error = Error::default();
        let Some(converted) = convert_sky(&Convertible::from(value), self.registry.functions(), &mut error) else {
            tracing::warn!(target: log_target::PARSE_STYLE, error = %error.message, "Style sky is invalid");
            return;
        };

        self.sky = Some(converted);
    }

    fn parse_terrain(&mut self, value: &Json<'_>) {
        let mut error = Error::default();
        let Some(converted) = convert_terrain(&Convertible::from(value), &mut error) else {
            tracing::warn!(target: log_target::PARSE_STYLE, error = %error.message, "Style terrain is invalid");
            return;
        };

        self.terrain = Some(converted);
    }

    fn parse_projection(&mut self, value: &Json<'_>) {
        let mut error = Error::default();
        let Some(converted) = convert_projection(&Convertible::from(value), self.registry.functions(), &mut error)
        else {
            tracing::warn!(target: log_target::PARSE_STYLE, error = %error.message, "Style projection is invalid");
            return;
        };

        self.projection = converted;
    }

    fn parse_member(&mut self, name: &str, value: &Json<'_>) {
        match self.registry.member_value(Place::Root, name, &Convertible::from(value)) {
            Ok(value) => self.members.set(name, value),
            Err(error) => {
                tracing::warn!(target: log_target::PARSE_STYLE, error = %error.message, "Style member is invalid");
            }
        }
    }

    fn parse_state(&mut self, value: &Json<'_>) {
        let Some(object) = value.as_object() else {
            tracing::warn!(target: log_target::PARSE_STYLE, "state must be an object");
            return;
        };

        for (property_name, definition) in members(object) {
            let Some(member_object) = definition.as_object() else {
                tracing::warn!(target: log_target::PARSE_STYLE, property = property_name, "state property must be an object");
                continue;
            };
            let Some(default) = member(member_object, "default") else {
                continue;
            };
            // emplace: the first definition wins.
            self.global_state_defaults
                .entry(property_name.to_owned())
                .or_insert_with(|| borrowed_json_to_value(default));
        }
    }

    fn parse_sources(&mut self, value: &Json<'_>) {
        let Some(object) = value.as_object() else {
            tracing::warn!(target: log_target::PARSE_STYLE, "sources must be an object");
            return;
        };

        for (id, property) in members(object) {
            let mut error = Error::default();
            let Some(source) = convert_source(&Convertible::from(property), &mut error, id, &self.registry) else {
                tracing::warn!(target: log_target::PARSE_STYLE, error = %error.message, "Style source is invalid");
                continue;
            };

            self.sources.push(source);
        }
    }

    fn parse_sprites(&mut self, value: &Json<'_>) {
        if let Some(url) = value.as_str() {
            let sprite = Sprite::new("default", url);
            self.sprites.push(sprite);
        } else if let Some(array) = value.as_array() {
            let mut sprite_ids = HashSet::default();
            for sprite_value in array {
                if !sprite_value.is_object() {
                    tracing::warn!(target: log_target::PARSE_STYLE, "sprite child must be an object");
                    continue;
                }

                let mut error = Error::default();
                let Some(sprite) = convert::<Sprite>(&Convertible::from(sprite_value), &mut error) else {
                    tracing::warn!(target: log_target::PARSE_STYLE, error = %error.message, "Style sprite is invalid");
                    continue;
                };

                if sprite_ids.contains(&sprite.id) {
                    tracing::warn!(target: log_target::PARSE_STYLE, "sprite ids must be unique");
                    continue;
                }

                sprite_ids.insert(sprite.id.clone());
                self.sprites.push(sprite);
            }
        } else {
            tracing::warn!(target: log_target::PARSE_STYLE, "sprite must be an object or string");
        }
    }

    fn parse_layers(&mut self, value: &Json<'_>) {
        let mut ids: Vec<String> = Vec::new();

        let Some(array) = value.as_array() else {
            tracing::warn!(target: log_target::PARSE_STYLE, "layers must be an array");
            return;
        };

        let mut layers_map: HashMap<String, LayerEntry<'_>> = HashMap::default();

        for layer_value in array {
            let Some(layer_object) = layer_value.as_object() else {
                tracing::warn!(target: log_target::PARSE_STYLE, "layer must be an object");
                continue;
            };

            let Some(id) = member(layer_object, "id") else {
                tracing::warn!(target: log_target::PARSE_STYLE, "layer must have an id");
                continue;
            };

            let Some(id) = id.as_str() else {
                tracing::warn!(target: log_target::PARSE_STYLE, "layer id must be a string");
                continue;
            };

            let layer_id = id.to_owned();
            if layers_map.contains_key(&layer_id) {
                tracing::warn!(target: log_target::PARSE_STYLE, layer_id = %layer_id, "duplicate layer id");
                continue;
            }

            layers_map.insert(layer_id.clone(), LayerEntry { value: layer_value, layer: None });
            ids.push(layer_id);
        }

        // Store a stack of layer IDs we're parsing right now. This is to
        // prevent reference cycles.
        let mut stack: Vec<String> = Vec::new();

        for id in &ids {
            if layers_map.contains_key(id) {
                parse_layer(&self.registry, id, &mut layers_map, &mut stack);
            }
        }

        for id in &ids {
            if let Some(entry) = layers_map.get_mut(id)
                && let Some(layer) = entry.layer.take()
            {
                self.layers.push(layer);
            }
        }
    }

    /// Statically evaluate layer properties to determine what font stacks are used.
    #[must_use]
    pub fn font_stacks(&self) -> VecSet<FontStack> {
        crate::layer::font_stacks(self.layers.iter().map(Layer::style))
    }
}

struct LayerEntry<'a> {
    value: &'a Json<'a>,
    layer: Option<Layer>,
}

fn parse_layer(
    registry: &StyleRegistry,
    id: &str,
    layers_map: &mut HashMap<String, LayerEntry<'_>>,
    stack: &mut Vec<String>,
) {
    let Some(entry) = layers_map.get(id) else {
        return;
    };
    if entry.layer.is_some() {
        // Skip parsing this again. We already have a valid layer definition.
        return;
    }
    let value = entry.value;

    // Make sure we have not previously attempted to parse this layer.
    if stack.iter().any(|s| s == id) {
        tracing::warn!(target: log_target::PARSE_STYLE, layer_id = id, "layer reference is circular");
        return;
    }

    if let Some(ref_val) = value.as_object().and_then(|object| member(object, "ref")) {
        // This layer is referencing another layer. Recursively parse that layer.
        let Some(ref_) = ref_val.as_str() else {
            tracing::warn!(target: log_target::PARSE_STYLE, layer_id = id, "layer ref must be a string");
            return;
        };

        if !layers_map.contains_key(ref_) {
            tracing::warn!(target: log_target::PARSE_STYLE, layer_id = id, ref_layer_id = ref_, "layer references an unknown layer");
            return;
        }

        // Recursively parse the referenced layer.
        stack.insert(0, id.to_owned());
        parse_layer(registry, ref_, layers_map, stack);
        stack.remove(0);

        let Some(reference) = layers_map.get(ref_).and_then(|e| e.layer.as_ref()) else {
            return;
        };

        let mut layer = reference.clone_ref(id);
        if let Some(error) = set_paint_properties(&mut layer, &Convertible::from(value), InvalidProperty::Skip) {
            tracing::warn!(target: log_target::PARSE_STYLE, error = %error.message, "Layer paint properties are invalid");
        }
        if let Some(entry) = layers_map.get_mut(id) {
            entry.layer = Some(layer);
        }
    } else {
        let mut error = Error::default();
        let Some(converted) =
            convert_layer_with(registry, &Convertible::from(value), &mut error, InvalidProperty::Skip)
        else {
            tracing::warn!(target: log_target::PARSE_STYLE, error = %error.message, "Style layer is invalid");
            return;
        };
        if let Some(entry) = layers_map.get_mut(id) {
            entry.layer = Some(converted);
        }
    }
}

/// Parses a font file of a font face: an object with a `url`, and with an
/// optional `unicode-range` that limits the code points the font draws. Adds a
/// warning for each range it can't read, and skips the range.
fn parse_font_file(face_name: &str, font_file: &JsonObject<'_>, warnings: &mut Vec<String>) -> FontFace {
    let mut font_face = FontFace { name: face_name.to_owned(), url: String::new(), ranges: vec![ALL_CODE_POINTS] };

    if let Some(url) = member(font_file, "url").and_then(Json::as_str) {
        font_face.url = url.to_owned();
    }

    if let Some(unicode_range) = member(font_file, "unicode-range") {
        font_face.ranges.clear();
        if let Some(ranges) = unicode_range.as_array() {
            for range in ranges {
                let Some(range_string) = range.as_str() else {
                    continue;
                };
                if range_string.len() <= 2 {
                    continue;
                }
                // Skips the `U+` prefix's bytes; the range is ASCII if it's
                // valid.
                let range_string = &range_string.as_bytes()[2..];
                let Some(pos) = range_string.iter().position(|&b| b == b'-') else {
                    continue;
                };
                let (start, end) = (&range_string[..pos], &range_string[pos + 1..]);
                if start.is_empty() || end.is_empty() {
                    continue;
                }
                // Each end is a whole hex number.
                let parse = |s: &[u8]| std::str::from_utf8(s).ok().and_then(|s| u32::from_str_radix(s, 16).ok());
                match (parse(start), parse(end)) {
                    (Some(start), Some(end)) => font_face.ranges.push((start, end)),
                    _ => warnings
                        .push(format!("Invalid unicode-range in font-face: {}", String::from_utf8_lossy(range_string))),
                }
            }
        }
    }

    font_face
}

/// Parses fallback font faces from `json`, an array of font files in the format
/// of a font face in a style's `font-faces`, without a font name. It fails with
/// [`ErrorKind::InvalidArgument`](crate::error::ErrorKind) if `json` isn't such an
/// array.
pub fn parse_fallback_font_faces(json: &str) -> Result<FontFaces, crate::error::Error> {
    let document: Json<'_> =
        serde_json::from_str(json).map_err(|error| crate::error::Error::invalid_argument(error.to_string()))?;
    let Some(files) = document.as_array() else {
        return Err(crate::error::Error::invalid_argument("fallback font faces must be an array"));
    };

    let mut font_faces = FontFaces::new();
    for font_file in files {
        let Some(font_file) = font_file.as_object() else {
            return Err(crate::error::Error::invalid_argument("each fallback font face must be an object"));
        };
        let mut warnings = Vec::new();
        let font_face = parse_font_file("", font_file, &mut warnings);
        if let Some(warning) = warnings.into_iter().next() {
            return Err(crate::error::Error::invalid_argument(warning));
        }
        if font_face.url.is_empty() {
            return Err(crate::error::Error::invalid_argument("each fallback font face must have a url"));
        }
        if font_face.ranges.is_empty() {
            return Err(crate::error::Error::invalid_argument("each fallback font face must cover a unicode-range"));
        }
        font_faces.push(font_face);
    }
    Ok(font_faces)
}

/// The "font-faces" block of `Parser::parse` (`MLN_TEXT_SHAPING_HARFBUZZ`).
fn parse_font_faces(faces: &JsonObject<'_>) -> FontFaces {
    let mut font_faces = FontFaces::new();
    for (face_name, face_value) in members(faces) {
        if let Some(files) = face_value.as_array() {
            // If the face is an array, we assume it is a list of font file
            // objects.
            for font_file in files {
                let Some(font_file) = font_file.as_object() else {
                    continue;
                };
                let mut warnings = Vec::new();
                let font_face = parse_font_file(face_name, font_file, &mut warnings);
                for warning in warnings {
                    tracing::warn!(target: log_target::PARSE_STYLE, warning = %warning, "Font-face definition is invalid");
                }

                if !font_face.name.is_empty() && !font_face.url.is_empty() && !font_face.ranges.is_empty() {
                    font_faces.push(font_face);
                } else {
                    tracing::warn!(target: log_target::PARSE_STYLE, font_face = face_name, "Invalid font-face definition");
                }
            }
        } else if let Some(url) = face_value.as_str() {
            font_faces.push(FontFace {
                name: face_name.to_owned(),
                url: url.to_owned(),
                ranges: vec![ALL_CODE_POINTS],
            });
        } else {
            tracing::warn!(target: log_target::PARSE_STYLE, "font-face must be an object or array");
        }
    }
    font_faces
}

/// The range of a font face without a `unicode-range`: every code point.
const ALL_CODE_POINTS: FontFaceRange = (0, 0x0010_FFFF);