Skip to content

Commit

Permalink
consider icon-text-fit when doing variable text placement
Browse files Browse the repository at this point in the history
When icon-text-fit-padding is used the collision box of the icon is
different than the collision box of the text. It now checks whether
the icon box fits when choosing a variable text position.
  • Loading branch information
ansis committed Oct 10, 2019
1 parent aaf7383 commit c1d6294
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/symbol/placement.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export class Placement {
attemptAnchorPlacement(anchor: TextAnchor, textBox: SingleCollisionBox, width: number, height: number,
textBoxScale: number, rotateWithMap: boolean,
pitchWithMap: boolean, textPixelRatio: number, posMatrix: mat4, collisionGroup: CollisionGroup,
textAllowOverlap: boolean, symbolInstance: SymbolInstance, bucket: SymbolBucket, orientation: number): ?{ shift: Point, placedGlyphBoxes: { box: Array<number>, offscreen: boolean } } {
textAllowOverlap: boolean, symbolInstance: SymbolInstance, bucket: SymbolBucket, orientation: number, iconBox: ?SingleCollisionBox): ?{ shift: Point, placedGlyphBoxes: { box: Array<number>, offscreen: boolean } } {

const textOffset = [symbolInstance.textOffset0, symbolInstance.textOffset1];
const shift = calculateVariableLayoutShift(anchor, width, height, textOffset, textBoxScale);
Expand All @@ -250,6 +250,15 @@ export class Placement {
rotateWithMap, pitchWithMap, this.transform.angle),
textAllowOverlap, textPixelRatio, posMatrix, collisionGroup.predicate);

if (iconBox) {
const placedIconBoxes = this.collisionIndex.placeCollisionBox(
shiftVariableCollisionBox(
iconBox, shift.x, shift.y,
rotateWithMap, pitchWithMap, this.transform.angle),
textAllowOverlap, textPixelRatio, posMatrix, collisionGroup.predicate);
if (placedIconBoxes.box.length === 0) return;
}

if (placedGlyphBoxes.box.length > 0) {
let prevAnchor;
// If this label was placed in the previous placement, record the anchor position
Expand Down Expand Up @@ -420,11 +429,13 @@ export class Placement {
}
}

const placeBoxForVariableAnchors = (collisionTextBox, orientation) => {
const placeBoxForVariableAnchors = (collisionTextBox, collisionIconBox, orientation) => {
const width = collisionTextBox.x2 - collisionTextBox.x1;
const height = collisionTextBox.y2 - collisionTextBox.y1;
const textBoxScale = symbolInstance.textBoxScale;

const variableIconBox = hasIconTextFit && !iconAllowOverlap ? collisionIconBox : null;

let placedBox: ?{ box: Array<number>, offscreen: boolean } = {box: [], offscreen: false};
const placementAttempts = textAllowOverlap ? anchors.length * 2 : anchors.length;
for (let i = 0; i < placementAttempts; ++i) {
Expand All @@ -433,7 +444,7 @@ export class Placement {
const result = this.attemptAnchorPlacement(
anchor, collisionTextBox, width, height,
textBoxScale, rotateWithMap, pitchWithMap, textPixelRatio, posMatrix,
collisionGroup, allowOverlap, symbolInstance, bucket, orientation);
collisionGroup, allowOverlap, symbolInstance, bucket, orientation, variableIconBox);

if (result) {
placedBox = result.placedGlyphBoxes;
Expand All @@ -449,14 +460,14 @@ export class Placement {
};

const placeHorizontal = () => {
return placeBoxForVariableAnchors(textBox, WritingMode.horizontal);
return placeBoxForVariableAnchors(textBox, collisionArrays.iconBox, WritingMode.horizontal);
};

const placeVertical = () => {
const verticalTextBox = collisionArrays.verticalTextBox;
const wasPlaced = placed && placed.box && placed.box.length;
if (bucket.allowVerticalPlacement && !wasPlaced && symbolInstance.numVerticalGlyphVertices > 0 && verticalTextBox) {
return placeBoxForVariableAnchors(verticalTextBox, WritingMode.vertical);
return placeBoxForVariableAnchors(verticalTextBox, collisionArrays.verticalIconBox, WritingMode.vertical);
}
return {box: null, offscreen: null};
};
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c1d6294

Please sign in to comment.