Skip to main content

Formatted variables and branches

Preserve text sections through let, var and match, and fall back on actual null results

A matching development SDK with commonLineSymbols also supports shared planar line symbols, including true per-glyph curved geometry, target ownership, joint icon/text groups and shared budgets. Earlier point-only scope and pending line-symbol notes below are superseded for this bounded subset. Complex shaping, tilted/globe/terrain placement and complete road stitching remain pending.

Shared point labels support let, var, match, case and coalesce returning plain strings or formatted values. A formatted variable preserves its sections' actual sizes and colors instead of converting to a string. Ordinary numeric and string variables remain usable in section text and options.

map.setLayoutProperty('formatted', 'text-field', [
  'let', 'scale', 1.5,
  ['let', 'label',
    ['format', ['get', 'name'],
      {'font-scale': ['var', 'scale'], 'text-color': 'red'},
      ' BB', {'font-scale': 0.5, 'text-color': 'blue'}],
    ['match', ['get', 'mode'],
      ['primary', 'selected'], ['var', 'label'],
      ['coalesce', ['get', 'fallback'], ['var', 'label']]
    ]
  ]
]);
await map.sqs.flush({render: true});

This assumes an existing point source, a formatted text layer, and name, mode, fallback properties. See formatted text sections for the basic setup. The commonFormattedTextScopes capability covers these scope and branch operations.

Each let binding captures the parent environment as it was before that let; it is evaluated only when referenced by var. Sibling bindings cannot refer to newly declared names, so use nested scopes as shown. Later inner bindings with the same name do not alter an already captured formatted value. An unused valid binding is not evaluated eagerly, while variable names and types are still checked before use.

match accepts numeric or string labels and label arrays, evaluating only the selected result. coalesce skips actual null, including null returned by a case or match. Empty strings and empty formatted values do not trigger fallback. A formatted variable cannot be used as a font scale, a boolean condition or an input to ordinary string operations; such uses fail explicitly.

Control flow, variable expansion and section expressions share one 8192-step / 64-level evaluation budget. Existing limits of 32 sections, 256 characters / 4096 bytes, font sizes and glyph resources still apply. Failed updates preserve the previous label and image. Per-section font stacks now use the PBF path. Registered RGBA image sections are available. SDF image sections, font files, complex shaping, line placement and 3D combinations remain unfinished.

Copy portable-format-scope.js, or use the complete browser page. The same script passes 11 checks and actual colored glyph size checks in the native runtime, browser main thread and ordinary rendering Worker.