Skip to content

Commit

Permalink
add alwaysVisible option to disable text collision, close #315
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Mar 15, 2014
1 parent ad95064 commit 02da93b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
3 changes: 2 additions & 1 deletion debug/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ var style_json = {
"text_field": "name",
"path": "horizontal",
"font": "Open Sans, Jomolhari, Siyam Rupali, Alef, Arial Unicode MS",
"fontSize": 18
"fontSize": 18,
"alwaysVisible": true
},
"road_label": {
"source": "mapbox streets",
Expand Down
7 changes: 4 additions & 3 deletions js/text/collision.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function Collision() {

}

Collision.prototype.place = function(boxes, anchor, minPlacementScale, maxPlacementScale, padding, horizontal) {
Collision.prototype.place = function(boxes, anchor, minPlacementScale, maxPlacementScale, padding, horizontal, alwaysVisible) {

var minScale = Infinity;
for (var m = 0; m < boxes.length; m++) {
Expand Down Expand Up @@ -73,13 +73,14 @@ Collision.prototype.place = function(boxes, anchor, minPlacementScale, maxPlacem
}

// Calculate the minimum scale the entire label can be shown without collisions
var scale = this.getPlacementScale(boxes, minPlacementScale, maxPlacementScale, padding);
var scale = alwaysVisible ? minPlacementScale :
this.getPlacementScale(boxes, minPlacementScale, maxPlacementScale, padding);

// Return if the label can never be placed without collision
if (scale === null) return null;

// Calculate the range it is safe to rotate all glyphs
var rotationRange = this.getPlacementRange(boxes, scale, horizontal);
var rotationRange = alwaysVisible ? [2 * Math.PI, 0] : this.getPlacementRange(boxes, scale, horizontal);
this.insert(boxes, anchor, scale, rotationRange, horizontal, padding);

var zoom = Math.log(scale) / Math.LN2;
Expand Down
3 changes: 2 additions & 1 deletion js/text/placement.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ Placement.prototype.addFeature = function(line, info, faces, shaping) {
for (var j = 0, len = anchors.length; j < len; j++) {
var anchor = anchors[j];
var glyphs = getGlyphs(anchor, advance, shaping, faces, fontScale, horizontal, line, maxAngleDelta, rotate);
var place = this.collision.place(glyphs, anchor, anchor.scale, this.maxPlacementScale, padding, horizontal);
var place = this.collision.place(
glyphs, anchor, anchor.scale, this.maxPlacementScale, padding, horizontal, info.alwaysVisible);

if (place) {
this.geometry.addGlyphs(glyphs, place.zoom, place.rotationRange, this.zoom - this.zOffset);
Expand Down

0 comments on commit 02da93b

Please sign in to comment.