maplibre-gl 3.2.2 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/generate-struct-arrays.ts +3 -1
- package/build/generate-style-code.ts +7 -8
- package/dist/maplibre-gl-csp-worker.js +1 -1
- package/dist/maplibre-gl-csp-worker.js.map +1 -1
- package/dist/maplibre-gl-csp.js +1 -1
- package/dist/maplibre-gl-csp.js.map +1 -1
- package/dist/maplibre-gl-dev.js +424 -168
- package/dist/maplibre-gl-dev.js.map +1 -1
- package/dist/maplibre-gl.d.ts +44 -7
- package/dist/maplibre-gl.js +3 -3
- package/dist/maplibre-gl.js.map +1 -1
- package/package.json +10 -10
- package/src/data/array_types.g.ts +78 -14
- package/src/data/bucket/symbol_attributes.ts +7 -1
- package/src/data/bucket/symbol_bucket.ts +4 -1
- package/src/render/draw_symbol.ts +8 -9
- package/src/style/properties.ts +4 -0
- package/src/style/style_layer/background_style_layer_properties.g.ts +1 -6
- package/src/style/style_layer/circle_style_layer_properties.g.ts +1 -6
- package/src/style/style_layer/fill_extrusion_style_layer_properties.g.ts +1 -6
- package/src/style/style_layer/fill_style_layer_properties.g.ts +1 -6
- package/src/style/style_layer/heatmap_style_layer_properties.g.ts +1 -6
- package/src/style/style_layer/hillshade_style_layer_properties.g.ts +1 -6
- package/src/style/style_layer/line_style_layer_properties.g.ts +1 -6
- package/src/style/style_layer/raster_style_layer_properties.g.ts +1 -6
- package/src/style/style_layer/symbol_style_layer_properties.g.ts +4 -6
- package/src/style/style_layer/variable_text_anchor.test.ts +117 -0
- package/src/style/style_layer/variable_text_anchor.ts +163 -0
- package/src/symbol/placement.ts +52 -40
- package/src/symbol/symbol_layout.ts +42 -116
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import {VariableAnchorOffsetCollection, VariableAnchorOffsetCollectionSpecification} from '@maplibre/maplibre-gl-style-spec';
|
|
2
|
+
import {SymbolFeature} from '../../data/bucket/symbol_bucket';
|
|
3
|
+
import {CanonicalTileID} from '../../source/tile_id';
|
|
4
|
+
import ONE_EM from '../../symbol/one_em';
|
|
5
|
+
import {SymbolStyleLayer} from './symbol_style_layer';
|
|
6
|
+
|
|
7
|
+
export enum TextAnchorEnum {
|
|
8
|
+
'center' = 1,
|
|
9
|
+
'left' = 2,
|
|
10
|
+
'right' = 3,
|
|
11
|
+
'top' = 4,
|
|
12
|
+
'bottom' = 5,
|
|
13
|
+
'top-left' = 6,
|
|
14
|
+
'top-right' = 7,
|
|
15
|
+
'bottom-left' = 8,
|
|
16
|
+
'bottom-right' = 9
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type TextAnchor = keyof typeof TextAnchorEnum;
|
|
20
|
+
|
|
21
|
+
// The radial offset is to the edge of the text box
|
|
22
|
+
// In the horizontal direction, the edge of the text box is where glyphs start
|
|
23
|
+
// But in the vertical direction, the glyphs appear to "start" at the baseline
|
|
24
|
+
// We don't actually load baseline data, but we assume an offset of ONE_EM - 17
|
|
25
|
+
// (see "yOffset" in shaping.js)
|
|
26
|
+
const baselineOffset = 7;
|
|
27
|
+
export const INVALID_TEXT_OFFSET = Number.POSITIVE_INFINITY;
|
|
28
|
+
|
|
29
|
+
export function evaluateVariableOffset(anchor: TextAnchor, offset: [number, number]): [number, number] {
|
|
30
|
+
|
|
31
|
+
function fromRadialOffset(anchor: TextAnchor, radialOffset: number): [number, number] {
|
|
32
|
+
let x = 0, y = 0;
|
|
33
|
+
if (radialOffset < 0) radialOffset = 0; // Ignore negative offset.
|
|
34
|
+
// solve for r where r^2 + r^2 = radialOffset^2
|
|
35
|
+
const hypotenuse = radialOffset / Math.sqrt(2);
|
|
36
|
+
switch (anchor) {
|
|
37
|
+
case 'top-right':
|
|
38
|
+
case 'top-left':
|
|
39
|
+
y = hypotenuse - baselineOffset;
|
|
40
|
+
break;
|
|
41
|
+
case 'bottom-right':
|
|
42
|
+
case 'bottom-left':
|
|
43
|
+
y = -hypotenuse + baselineOffset;
|
|
44
|
+
break;
|
|
45
|
+
case 'bottom':
|
|
46
|
+
y = -radialOffset + baselineOffset;
|
|
47
|
+
break;
|
|
48
|
+
case 'top':
|
|
49
|
+
y = radialOffset - baselineOffset;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
switch (anchor) {
|
|
54
|
+
case 'top-right':
|
|
55
|
+
case 'bottom-right':
|
|
56
|
+
x = -hypotenuse;
|
|
57
|
+
break;
|
|
58
|
+
case 'top-left':
|
|
59
|
+
case 'bottom-left':
|
|
60
|
+
x = hypotenuse;
|
|
61
|
+
break;
|
|
62
|
+
case 'left':
|
|
63
|
+
x = radialOffset;
|
|
64
|
+
break;
|
|
65
|
+
case 'right':
|
|
66
|
+
x = -radialOffset;
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return [x, y];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function fromTextOffset(anchor: TextAnchor, offsetX: number, offsetY: number): [number, number] {
|
|
74
|
+
let x = 0, y = 0;
|
|
75
|
+
// Use absolute offset values.
|
|
76
|
+
offsetX = Math.abs(offsetX);
|
|
77
|
+
offsetY = Math.abs(offsetY);
|
|
78
|
+
|
|
79
|
+
switch (anchor) {
|
|
80
|
+
case 'top-right':
|
|
81
|
+
case 'top-left':
|
|
82
|
+
case 'top':
|
|
83
|
+
y = offsetY - baselineOffset;
|
|
84
|
+
break;
|
|
85
|
+
case 'bottom-right':
|
|
86
|
+
case 'bottom-left':
|
|
87
|
+
case 'bottom':
|
|
88
|
+
y = -offsetY + baselineOffset;
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
switch (anchor) {
|
|
93
|
+
case 'top-right':
|
|
94
|
+
case 'bottom-right':
|
|
95
|
+
case 'right':
|
|
96
|
+
x = -offsetX;
|
|
97
|
+
break;
|
|
98
|
+
case 'top-left':
|
|
99
|
+
case 'bottom-left':
|
|
100
|
+
case 'left':
|
|
101
|
+
x = offsetX;
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return [x, y];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return (offset[1] !== INVALID_TEXT_OFFSET) ? fromTextOffset(anchor, offset[0], offset[1]) : fromRadialOffset(anchor, offset[0]);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Helper to support both text-variable-anchor and text-variable-anchor-offset. Offset values converted from EMs to PXs
|
|
112
|
+
export function getTextVariableAnchorOffset(layer: SymbolStyleLayer, feature: SymbolFeature, canonical: CanonicalTileID): VariableAnchorOffsetCollection | null {
|
|
113
|
+
const layout = layer.layout;
|
|
114
|
+
// If style specifies text-variable-anchor-offset, just return it
|
|
115
|
+
const variableAnchorOffset = layout.get('text-variable-anchor-offset')?.evaluate(feature, {}, canonical);
|
|
116
|
+
|
|
117
|
+
if (variableAnchorOffset) {
|
|
118
|
+
const sourceValues = variableAnchorOffset.values;
|
|
119
|
+
const destValues: VariableAnchorOffsetCollectionSpecification = [];
|
|
120
|
+
|
|
121
|
+
// Convert offsets from EM to PX, and apply baseline shift
|
|
122
|
+
for (let i = 0; i < sourceValues.length; i += 2) {
|
|
123
|
+
const anchor = destValues[i] = sourceValues[i] as TextAnchor;
|
|
124
|
+
const offset = (sourceValues[i + 1] as [number, number]).map(t => t * ONE_EM) as [number, number];
|
|
125
|
+
|
|
126
|
+
if (anchor.startsWith('top')) {
|
|
127
|
+
offset[1] -= baselineOffset;
|
|
128
|
+
} else if (anchor.startsWith('bottom')) {
|
|
129
|
+
offset[1] += baselineOffset;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
destValues[i + 1] = offset;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return new VariableAnchorOffsetCollection(destValues);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// If style specifies text-variable-anchor, convert to the new format
|
|
139
|
+
const variableAnchor = layout.get('text-variable-anchor');
|
|
140
|
+
|
|
141
|
+
if (variableAnchor) {
|
|
142
|
+
let textOffset: [number, number];
|
|
143
|
+
const unevaluatedLayout = layer._unevaluatedLayout;
|
|
144
|
+
|
|
145
|
+
// The style spec says don't use `text-offset` and `text-radial-offset` together
|
|
146
|
+
// but doesn't actually specify what happens if you use both. We go with the radial offset.
|
|
147
|
+
if (unevaluatedLayout.getValue('text-radial-offset') !== undefined) {
|
|
148
|
+
textOffset = [layout.get('text-radial-offset').evaluate(feature, {}, canonical) * ONE_EM, INVALID_TEXT_OFFSET];
|
|
149
|
+
} else {
|
|
150
|
+
textOffset = layout.get('text-offset').evaluate(feature, {}, canonical).map(t => t * ONE_EM) as [number, number];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const anchorOffsets: VariableAnchorOffsetCollectionSpecification = [];
|
|
154
|
+
|
|
155
|
+
for (const anchor of variableAnchor) {
|
|
156
|
+
anchorOffsets.push(anchor, evaluateVariableOffset(anchor, textOffset));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return new VariableAnchorOffsetCollection(anchorOffsets);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return null;
|
|
163
|
+
}
|
package/src/symbol/placement.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type {FeatureKey} from './collision_index';
|
|
|
3
3
|
import {EXTENT} from '../data/extent';
|
|
4
4
|
import * as symbolSize from './symbol_size';
|
|
5
5
|
import * as projection from './projection';
|
|
6
|
-
import {getAnchorJustification
|
|
6
|
+
import {getAnchorJustification} from './symbol_layout';
|
|
7
7
|
import {getAnchorAlignment, WritingMode} from './shaping';
|
|
8
8
|
import {mat4} from 'gl-matrix';
|
|
9
9
|
import {pixelsToTileUnits} from '../source/pixels_to_tile_units';
|
|
@@ -17,12 +17,12 @@ import {getOverlapMode, OverlapMode} from '../style/style_layer/overlap_mode';
|
|
|
17
17
|
import type {Tile} from '../source/tile';
|
|
18
18
|
import {SymbolBucket, CollisionArrays, SingleCollisionBox} from '../data/bucket/symbol_bucket';
|
|
19
19
|
|
|
20
|
-
import type {CollisionBoxArray, CollisionVertexArray, SymbolInstance} from '../data/array_types.g';
|
|
20
|
+
import type {CollisionBoxArray, CollisionVertexArray, SymbolInstance, TextAnchorOffset} from '../data/array_types.g';
|
|
21
21
|
import type {FeatureIndex} from '../data/feature_index';
|
|
22
22
|
import type {OverscaledTileID} from '../source/tile_id';
|
|
23
|
-
import type {TextAnchor} from './symbol_layout';
|
|
24
23
|
import {Terrain} from '../render/terrain';
|
|
25
24
|
import {warnOnce} from '../util/util';
|
|
25
|
+
import {TextAnchor, TextAnchorEnum} from '../style/style_layer/variable_text_anchor';
|
|
26
26
|
|
|
27
27
|
class OpacityState {
|
|
28
28
|
opacity: number;
|
|
@@ -147,10 +147,9 @@ function calculateVariableLayoutShift(
|
|
|
147
147
|
const {horizontalAlign, verticalAlign} = getAnchorAlignment(anchor);
|
|
148
148
|
const shiftX = -(horizontalAlign - 0.5) * width;
|
|
149
149
|
const shiftY = -(verticalAlign - 0.5) * height;
|
|
150
|
-
const offset = evaluateVariableOffset(anchor, textOffset);
|
|
151
150
|
return new Point(
|
|
152
|
-
shiftX +
|
|
153
|
-
shiftY +
|
|
151
|
+
shiftX + textOffset[0] * textBoxScale,
|
|
152
|
+
shiftY + textOffset[1] * textBoxScale
|
|
154
153
|
);
|
|
155
154
|
}
|
|
156
155
|
|
|
@@ -339,7 +338,7 @@ export class Placement {
|
|
|
339
338
|
}
|
|
340
339
|
|
|
341
340
|
attemptAnchorPlacement(
|
|
342
|
-
|
|
341
|
+
textAnchorOffset: TextAnchorOffset,
|
|
343
342
|
textBox: SingleCollisionBox,
|
|
344
343
|
width: number,
|
|
345
344
|
height: number,
|
|
@@ -363,7 +362,8 @@ export class Placement {
|
|
|
363
362
|
};
|
|
364
363
|
} {
|
|
365
364
|
|
|
366
|
-
const
|
|
365
|
+
const anchor = TextAnchorEnum[textAnchorOffset.textAnchor] as TextAnchor;
|
|
366
|
+
const textOffset = [textAnchorOffset.textOffset0, textAnchorOffset.textOffset1] as [number, number];
|
|
367
367
|
const shift = calculateVariableLayoutShift(anchor, width, height, textOffset, textBoxScale);
|
|
368
368
|
|
|
369
369
|
const placedGlyphBoxes = this.collisionIndex.placeCollisionBox(
|
|
@@ -528,7 +528,11 @@ export class Placement {
|
|
|
528
528
|
}
|
|
529
529
|
};
|
|
530
530
|
|
|
531
|
-
|
|
531
|
+
const textAnchorOffsetStart = symbolInstance.textAnchorOffsetStartIndex;
|
|
532
|
+
const textAnchorOffsetEnd = symbolInstance.textAnchorOffsetEndIndex;
|
|
533
|
+
|
|
534
|
+
// If start+end indices match, text-variable-anchor is not in play.
|
|
535
|
+
if (textAnchorOffsetEnd === textAnchorOffsetStart) {
|
|
532
536
|
const placeBox = (collisionTextBox, orientation) => {
|
|
533
537
|
const placedFeature = this.collisionIndex.placeCollisionBox(
|
|
534
538
|
collisionTextBox,
|
|
@@ -561,47 +565,54 @@ export class Placement {
|
|
|
561
565
|
updatePreviousOrientationIfNotPlaced(placed && placed.box && placed.box.length);
|
|
562
566
|
|
|
563
567
|
} else {
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
// If this symbol was in the last placement, shift the previously used
|
|
567
|
-
// anchor to the front of the anchor list, only if the previous anchor
|
|
568
|
-
// is still in the anchor list
|
|
569
|
-
if (this.prevPlacement && this.prevPlacement.variableOffsets[symbolInstance.crossTileID]) {
|
|
570
|
-
const prevOffsets = this.prevPlacement.variableOffsets[symbolInstance.crossTileID];
|
|
571
|
-
if (anchors.indexOf(prevOffsets.anchor) > 0) {
|
|
572
|
-
anchors = anchors.filter(anchor => anchor !== prevOffsets.anchor);
|
|
573
|
-
anchors.unshift(prevOffsets.anchor);
|
|
574
|
-
}
|
|
575
|
-
}
|
|
568
|
+
// If this symbol was in the last placement, prefer placement using same anchor, if it's still available
|
|
569
|
+
let prevAnchor = TextAnchorEnum[this.prevPlacement?.variableOffsets[symbolInstance.crossTileID]?.anchor];
|
|
576
570
|
|
|
577
571
|
const placeBoxForVariableAnchors = (collisionTextBox, collisionIconBox, orientation) => {
|
|
578
572
|
const width = collisionTextBox.x2 - collisionTextBox.x1;
|
|
579
573
|
const height = collisionTextBox.y2 - collisionTextBox.y1;
|
|
580
574
|
const textBoxScale = symbolInstance.textBoxScale;
|
|
581
|
-
|
|
582
575
|
const variableIconBox = hasIconTextFit && (iconOverlapMode === 'never') ? collisionIconBox : null;
|
|
583
576
|
|
|
584
577
|
let placedBox: {
|
|
585
578
|
box: Array<number>;
|
|
586
579
|
offscreen: boolean;
|
|
587
580
|
} = {box: [], offscreen: false};
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
if (
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
581
|
+
let placementPasses = (textOverlapMode === 'never') ? 1 : 2;
|
|
582
|
+
let overlapMode: OverlapMode = 'never';
|
|
583
|
+
|
|
584
|
+
if (prevAnchor) {
|
|
585
|
+
placementPasses++;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
for (let pass = 0; pass < placementPasses; pass++) {
|
|
589
|
+
for (let i = textAnchorOffsetStart; i < textAnchorOffsetEnd; i++) {
|
|
590
|
+
const textAnchorOffset = bucket.textAnchorOffsets.get(i);
|
|
591
|
+
|
|
592
|
+
if (prevAnchor && textAnchorOffset.textAnchor !== prevAnchor) {
|
|
593
|
+
continue;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
const result = this.attemptAnchorPlacement(
|
|
597
|
+
textAnchorOffset, collisionTextBox, width, height,
|
|
598
|
+
textBoxScale, rotateWithMap, pitchWithMap, textPixelRatio, posMatrix,
|
|
599
|
+
collisionGroup, overlapMode, symbolInstance, bucket, orientation, variableIconBox, getElevation);
|
|
600
|
+
|
|
601
|
+
if (result) {
|
|
602
|
+
placedBox = result.placedGlyphBoxes;
|
|
603
|
+
if (placedBox && placedBox.box && placedBox.box.length) {
|
|
604
|
+
placeText = true;
|
|
605
|
+
shift = result.shift;
|
|
606
|
+
return placedBox;
|
|
607
|
+
}
|
|
603
608
|
}
|
|
604
609
|
}
|
|
610
|
+
|
|
611
|
+
if (prevAnchor) {
|
|
612
|
+
prevAnchor = null;
|
|
613
|
+
} else {
|
|
614
|
+
overlapMode = textOverlapMode;
|
|
615
|
+
}
|
|
605
616
|
}
|
|
606
617
|
|
|
607
618
|
return placedBox;
|
|
@@ -952,11 +963,12 @@ export class Placement {
|
|
|
952
963
|
if (bucket.hasIconCollisionBoxData()) bucket.iconCollisionBox.collisionVertexArray.clear();
|
|
953
964
|
if (bucket.hasTextCollisionBoxData()) bucket.textCollisionBox.collisionVertexArray.clear();
|
|
954
965
|
|
|
955
|
-
const
|
|
966
|
+
const layer = bucket.layers[0];
|
|
967
|
+
const layout = layer.layout;
|
|
956
968
|
const duplicateOpacityState = new JointOpacityState(null, 0, false, false, true);
|
|
957
969
|
const textAllowOverlap = layout.get('text-allow-overlap');
|
|
958
970
|
const iconAllowOverlap = layout.get('icon-allow-overlap');
|
|
959
|
-
const
|
|
971
|
+
const hasVariablePlacement = layer._unevaluatedLayout.hasValue('text-variable-anchor') || layer._unevaluatedLayout.hasValue('text-variable-anchor-offset');
|
|
960
972
|
const rotateWithMap = layout.get('text-rotation-alignment') === 'map';
|
|
961
973
|
const pitchWithMap = layout.get('text-pitch-alignment') === 'map';
|
|
962
974
|
const hasIconTextFit = layout.get('icon-text-fit') !== 'none';
|
|
@@ -1074,7 +1086,7 @@ export class Placement {
|
|
|
1074
1086
|
let shift = new Point(0, 0);
|
|
1075
1087
|
if (collisionArrays.textBox || collisionArrays.verticalTextBox) {
|
|
1076
1088
|
let used = true;
|
|
1077
|
-
if (
|
|
1089
|
+
if (hasVariablePlacement) {
|
|
1078
1090
|
const variableOffset = this.variableOffsets[crossTileID];
|
|
1079
1091
|
if (variableOffset) {
|
|
1080
1092
|
// This will show either the currently placed position or the last
|
|
@@ -19,7 +19,7 @@ import {SIZE_PACK_FACTOR, MAX_PACKED_SIZE, MAX_GLYPH_ICON_SIZE} from './symbol_s
|
|
|
19
19
|
import ONE_EM from './one_em';
|
|
20
20
|
import type {CanonicalTileID} from '../source/tile_id';
|
|
21
21
|
import type {Shaping, PositionedIcon, TextJustify} from './shaping';
|
|
22
|
-
import type {CollisionBoxArray} from '../data/array_types.g';
|
|
22
|
+
import type {CollisionBoxArray, TextAnchorOffsetArray} from '../data/array_types.g';
|
|
23
23
|
import type {SymbolFeature} from '../data/bucket/symbol_bucket';
|
|
24
24
|
import type {StyleImage} from '../style/style_image';
|
|
25
25
|
import type {StyleGlyph} from '../style/style_glyph';
|
|
@@ -31,6 +31,8 @@ import type {PossiblyEvaluatedPropertyValue} from '../style/properties';
|
|
|
31
31
|
import Point from '@mapbox/point-geometry';
|
|
32
32
|
import murmur3 from 'murmurhash-js';
|
|
33
33
|
import {getIconPadding, SymbolPadding} from '../style/style_layer/symbol_style_layer';
|
|
34
|
+
import {VariableAnchorOffsetCollection} from '@maplibre/maplibre-gl-style-spec';
|
|
35
|
+
import {getTextVariableAnchorOffset, evaluateVariableOffset, INVALID_TEXT_OFFSET, TextAnchor, TextAnchorEnum} from '../style/style_layer/variable_text_anchor';
|
|
34
36
|
|
|
35
37
|
// The symbol layout process needs `text-size` evaluated at up to five different zoom levels, and
|
|
36
38
|
// `icon-size` at up to three:
|
|
@@ -59,98 +61,6 @@ type ShapedTextOrientations = {
|
|
|
59
61
|
horizontal: Record<TextJustify, Shaping>;
|
|
60
62
|
};
|
|
61
63
|
|
|
62
|
-
export type TextAnchor = 'center' | 'left' | 'right' | 'top' | 'bottom' | 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
|
|
63
|
-
|
|
64
|
-
// The radial offset is to the edge of the text box
|
|
65
|
-
// In the horizontal direction, the edge of the text box is where glyphs start
|
|
66
|
-
// But in the vertical direction, the glyphs appear to "start" at the baseline
|
|
67
|
-
// We don't actually load baseline data, but we assume an offset of ONE_EM - 17
|
|
68
|
-
// (see "yOffset" in shaping.js)
|
|
69
|
-
const baselineOffset = 7;
|
|
70
|
-
const INVALID_TEXT_OFFSET = Number.POSITIVE_INFINITY;
|
|
71
|
-
|
|
72
|
-
export function evaluateVariableOffset(anchor: TextAnchor, offset: [number, number]) {
|
|
73
|
-
|
|
74
|
-
function fromRadialOffset(anchor: TextAnchor, radialOffset: number) {
|
|
75
|
-
let x = 0, y = 0;
|
|
76
|
-
if (radialOffset < 0) radialOffset = 0; // Ignore negative offset.
|
|
77
|
-
// solve for r where r^2 + r^2 = radialOffset^2
|
|
78
|
-
const hypotenuse = radialOffset / Math.sqrt(2);
|
|
79
|
-
switch (anchor) {
|
|
80
|
-
case 'top-right':
|
|
81
|
-
case 'top-left':
|
|
82
|
-
y = hypotenuse - baselineOffset;
|
|
83
|
-
break;
|
|
84
|
-
case 'bottom-right':
|
|
85
|
-
case 'bottom-left':
|
|
86
|
-
y = -hypotenuse + baselineOffset;
|
|
87
|
-
break;
|
|
88
|
-
case 'bottom':
|
|
89
|
-
y = -radialOffset + baselineOffset;
|
|
90
|
-
break;
|
|
91
|
-
case 'top':
|
|
92
|
-
y = radialOffset - baselineOffset;
|
|
93
|
-
break;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
switch (anchor) {
|
|
97
|
-
case 'top-right':
|
|
98
|
-
case 'bottom-right':
|
|
99
|
-
x = -hypotenuse;
|
|
100
|
-
break;
|
|
101
|
-
case 'top-left':
|
|
102
|
-
case 'bottom-left':
|
|
103
|
-
x = hypotenuse;
|
|
104
|
-
break;
|
|
105
|
-
case 'left':
|
|
106
|
-
x = radialOffset;
|
|
107
|
-
break;
|
|
108
|
-
case 'right':
|
|
109
|
-
x = -radialOffset;
|
|
110
|
-
break;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return [x, y];
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function fromTextOffset(anchor: TextAnchor, offsetX: number, offsetY: number) {
|
|
117
|
-
let x = 0, y = 0;
|
|
118
|
-
// Use absolute offset values.
|
|
119
|
-
offsetX = Math.abs(offsetX);
|
|
120
|
-
offsetY = Math.abs(offsetY);
|
|
121
|
-
|
|
122
|
-
switch (anchor) {
|
|
123
|
-
case 'top-right':
|
|
124
|
-
case 'top-left':
|
|
125
|
-
case 'top':
|
|
126
|
-
y = offsetY - baselineOffset;
|
|
127
|
-
break;
|
|
128
|
-
case 'bottom-right':
|
|
129
|
-
case 'bottom-left':
|
|
130
|
-
case 'bottom':
|
|
131
|
-
y = -offsetY + baselineOffset;
|
|
132
|
-
break;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
switch (anchor) {
|
|
136
|
-
case 'top-right':
|
|
137
|
-
case 'bottom-right':
|
|
138
|
-
case 'right':
|
|
139
|
-
x = -offsetX;
|
|
140
|
-
break;
|
|
141
|
-
case 'top-left':
|
|
142
|
-
case 'bottom-left':
|
|
143
|
-
case 'left':
|
|
144
|
-
x = offsetX;
|
|
145
|
-
break;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
return [x, y];
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
return (offset[1] !== INVALID_TEXT_OFFSET) ? fromTextOffset(anchor, offset[0], offset[1]) : fromRadialOffset(anchor, offset[0]);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
64
|
export function performSymbolLayout(args: {
|
|
155
65
|
bucket: SymbolBucket;
|
|
156
66
|
glyphMap: {
|
|
@@ -175,8 +85,9 @@ export function performSymbolLayout(args: {
|
|
|
175
85
|
args.bucket.compareText = {};
|
|
176
86
|
args.bucket.iconsNeedLinear = false;
|
|
177
87
|
|
|
178
|
-
const
|
|
179
|
-
const
|
|
88
|
+
const layer = args.bucket.layers[0];
|
|
89
|
+
const layout = layer.layout;
|
|
90
|
+
const unevaluatedLayoutValues = layer._unevaluatedLayout._values;
|
|
180
91
|
|
|
181
92
|
const sizes: Sizes = {
|
|
182
93
|
// Filled in below, if *SizeData.kind is 'composite'
|
|
@@ -226,16 +137,16 @@ export function performSymbolLayout(args: {
|
|
|
226
137
|
const spacingIfAllowed = allowsLetterSpacing(unformattedText) ? spacing : 0;
|
|
227
138
|
|
|
228
139
|
const textAnchor = layout.get('text-anchor').evaluate(feature, {}, args.canonical);
|
|
229
|
-
const
|
|
140
|
+
const variableAnchorOffset = getTextVariableAnchorOffset(layer, feature, args.canonical);
|
|
230
141
|
|
|
231
|
-
if (!
|
|
142
|
+
if (!variableAnchorOffset) {
|
|
232
143
|
const radialOffset = layout.get('text-radial-offset').evaluate(feature, {}, args.canonical);
|
|
233
144
|
// Layers with variable anchors use the `text-radial-offset` property and the [x, y] offset vector
|
|
234
145
|
// is calculated at placement time instead of layout time
|
|
235
146
|
if (radialOffset) {
|
|
236
147
|
// The style spec says don't use `text-offset` and `text-radial-offset` together
|
|
237
148
|
// but doesn't actually specify what happens if you use both. We go with the radial offset.
|
|
238
|
-
textOffset = evaluateVariableOffset(textAnchor, [radialOffset * ONE_EM, INVALID_TEXT_OFFSET])
|
|
149
|
+
textOffset = evaluateVariableOffset(textAnchor, [radialOffset * ONE_EM, INVALID_TEXT_OFFSET]);
|
|
239
150
|
} else {
|
|
240
151
|
textOffset = (layout.get('text-offset').evaluate(feature, {}, args.canonical).map(t => t * ONE_EM) as [number, number]);
|
|
241
152
|
}
|
|
@@ -261,14 +172,19 @@ export function performSymbolLayout(args: {
|
|
|
261
172
|
};
|
|
262
173
|
|
|
263
174
|
// If this layer uses text-variable-anchor, generate shapings for all justification possibilities.
|
|
264
|
-
if (!textAlongLine &&
|
|
265
|
-
const justifications =
|
|
266
|
-
|
|
267
|
-
|
|
175
|
+
if (!textAlongLine && variableAnchorOffset) {
|
|
176
|
+
const justifications = new Set<TextJustify>();
|
|
177
|
+
|
|
178
|
+
if (textJustify === 'auto') {
|
|
179
|
+
for (let i = 0; i < variableAnchorOffset.values.length; i += 2) {
|
|
180
|
+
justifications.add(getAnchorJustification(variableAnchorOffset.values[i] as TextAnchor));
|
|
181
|
+
}
|
|
182
|
+
} else {
|
|
183
|
+
justifications.add(textJustify);
|
|
184
|
+
}
|
|
268
185
|
|
|
269
186
|
let singleLine = false;
|
|
270
|
-
for (
|
|
271
|
-
const justification: TextJustify = justifications[i];
|
|
187
|
+
for (const justification of justifications) {
|
|
272
188
|
if (shapedTextOrientations.horizontal[justification]) continue;
|
|
273
189
|
if (singleLine) {
|
|
274
190
|
// If the shaping for the first justification was only a single line, we
|
|
@@ -486,6 +402,22 @@ function addFeature(bucket: SymbolBucket,
|
|
|
486
402
|
}
|
|
487
403
|
}
|
|
488
404
|
|
|
405
|
+
function addTextVariableAnchorOffsets(textAnchorOffsets: TextAnchorOffsetArray, variableAnchorOffset: VariableAnchorOffsetCollection): [number, number] {
|
|
406
|
+
const startIndex = textAnchorOffsets.length;
|
|
407
|
+
const values = variableAnchorOffset?.values;
|
|
408
|
+
|
|
409
|
+
if (values?.length > 0) {
|
|
410
|
+
for (let i = 0; i < values.length; i += 2) {
|
|
411
|
+
const anchor = TextAnchorEnum[values[i] as TextAnchor];
|
|
412
|
+
const offset = values[i + 1] as [number, number];
|
|
413
|
+
|
|
414
|
+
textAnchorOffsets.emplaceBack(anchor, offset[0], offset[1]);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
return [startIndex, textAnchorOffsets.length];
|
|
419
|
+
}
|
|
420
|
+
|
|
489
421
|
function addTextVertices(bucket: SymbolBucket,
|
|
490
422
|
anchor: Point,
|
|
491
423
|
shapedText: Shaping,
|
|
@@ -602,15 +534,6 @@ function addSymbol(bucket: SymbolBucket,
|
|
|
602
534
|
const placedTextSymbolIndices: {[k: string]: number} = {};
|
|
603
535
|
let key = murmur3('');
|
|
604
536
|
|
|
605
|
-
let textOffset0 = 0;
|
|
606
|
-
let textOffset1 = 0;
|
|
607
|
-
if (layer._unevaluatedLayout.getValue('text-radial-offset') === undefined) {
|
|
608
|
-
[textOffset0, textOffset1] = (layer.layout.get('text-offset').evaluate(feature, {}, canonical).map(t => t * ONE_EM) as [number, number]);
|
|
609
|
-
} else {
|
|
610
|
-
textOffset0 = layer.layout.get('text-radial-offset').evaluate(feature, {}, canonical) * ONE_EM;
|
|
611
|
-
textOffset1 = INVALID_TEXT_OFFSET;
|
|
612
|
-
}
|
|
613
|
-
|
|
614
537
|
if (bucket.allowVerticalPlacement && shapedTextOrientations.vertical) {
|
|
615
538
|
const textRotation = layer.layout.get('text-rotate').evaluate(feature, {}, canonical);
|
|
616
539
|
const verticalTextRotation = textRotation + 90.0;
|
|
@@ -763,6 +686,9 @@ function addSymbol(bucket: SymbolBucket,
|
|
|
763
686
|
bucket.addToSortKeyRanges(bucket.symbolInstances.length, feature.sortKey as number);
|
|
764
687
|
}
|
|
765
688
|
|
|
689
|
+
const variableAnchorOffset = getTextVariableAnchorOffset(layer, feature, canonical);
|
|
690
|
+
const [textAnchorOffsetStartIndex, textAnchorOffsetEndIndex] = addTextVariableAnchorOffsets(bucket.textAnchorOffsets, variableAnchorOffset);
|
|
691
|
+
|
|
766
692
|
bucket.symbolInstances.emplaceBack(
|
|
767
693
|
anchor.x,
|
|
768
694
|
anchor.y,
|
|
@@ -789,9 +715,9 @@ function addSymbol(bucket: SymbolBucket,
|
|
|
789
715
|
useRuntimeCollisionCircles,
|
|
790
716
|
0,
|
|
791
717
|
textBoxScale,
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
718
|
+
collisionCircleDiameter,
|
|
719
|
+
textAnchorOffsetStartIndex,
|
|
720
|
+
textAnchorOffsetEndIndex);
|
|
795
721
|
}
|
|
796
722
|
|
|
797
723
|
function anchorIsTooClose(bucket: SymbolBucket, text: string, repeatDistance: number, anchor: Point) {
|