Skip to content

Commit

Permalink
Initial support for newlines in labels.
Browse files Browse the repository at this point in the history
  • Loading branch information
abwood committed Dec 29, 2013
1 parent 7d9e8af commit 689a166
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/Labels.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@

labels.add({
position : ellipsoid.cartographicToCartesian(Cesium.Cartographic.fromDegrees(-75.10, 39.57)),
text : 'Label on top of scaling billboard',
text : 'Label on top\nof scaling billboard',
font : '20px sans-serif',
horizontalOrigin : Cesium.HorizontalOrigin.CENTER,
pixelOffset : new Cesium.Cartesian2(0.0, image.height),
Expand Down
45 changes: 36 additions & 9 deletions Source/Scene/LabelCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,44 +202,71 @@ define([

function repositionAllGlyphs(label) {
var glyphs = label._glyphs;
var text = label._text;
var glyph;
var dimensions;
var totalWidth = 0;
var lineWidth = 0;
var maxWidth = 0;
var maxHeight = 0;
var numberNewlines = 0;

var glyphIndex = 0;
var glyphLength = glyphs.length;
for (glyphIndex = 0; glyphIndex < glyphLength; ++glyphIndex) {
glyph = glyphs[glyphIndex];
dimensions = glyph.dimensions;
totalWidth += dimensions.width;
maxHeight = Math.max(maxHeight, dimensions.height);
lineWidth += dimensions.width;
maxWidth = Math.max(maxWidth, lineWidth);
maxHeight = Math.max(maxHeight, dimensions.height + dimensions.descent);
if (text.charAt(glyphIndex) === '\n') {
numberNewlines += 1;
lineWidth = 0;
}
}

var scale = label._scale;
var horizontalOrigin = label._horizontalOrigin;
var widthOffset = 0;
if (horizontalOrigin === HorizontalOrigin.CENTER) {
widthOffset -= totalWidth / 2 * scale;
widthOffset -= maxWidth / 2 * scale;
} else if (horizontalOrigin === HorizontalOrigin.RIGHT) {
widthOffset -= totalWidth * scale;
widthOffset -= maxWidth * scale;
}

var heightOffset = 0;
var totalHeight = maxHeight * numberNewlines;
var verticalOrigin = label._verticalOrigin;
if (verticalOrigin === VerticalOrigin.CENTER) {
heightOffset -= totalHeight / 2 * scale;
} else if (verticalOrigin === VerticalOrigin.TOP) {
heightOffset -= totalHeight * scale;
}
else if (verticalOrigin === VerticalOrigin.BOTTOM) {
heightOffset += totalHeight * scale;
}

glyphPixelOffset.x = widthOffset;
glyphPixelOffset.y = 0;

var verticalOrigin = label._verticalOrigin;
var glyphNewlineOffset = 0;
for (glyphIndex = 0; glyphIndex < glyphLength; ++glyphIndex) {
glyph = glyphs[glyphIndex];
dimensions = glyph.dimensions;

if (verticalOrigin === VerticalOrigin.BOTTOM || dimensions.height === maxHeight) {
glyphPixelOffset.y = -dimensions.descent * scale;
glyphPixelOffset.y = heightOffset - dimensions.descent * scale;
} else if (verticalOrigin === VerticalOrigin.TOP) {
glyphPixelOffset.y = -(maxHeight - dimensions.height) * scale - dimensions.descent * scale;
glyphPixelOffset.y = heightOffset - (maxHeight - dimensions.height) * scale - dimensions.descent * scale;
} else if (verticalOrigin === VerticalOrigin.CENTER) {
glyphPixelOffset.y = -(maxHeight - dimensions.height) / 2 * scale - dimensions.descent * scale;
glyphPixelOffset.y = heightOffset - (maxHeight - dimensions.height) / 2 * scale - dimensions.descent * scale;
}

if (text.charAt(glyphIndex) === '\n') {
glyphNewlineOffset += maxHeight * scale;
glyphPixelOffset.x = widthOffset;
continue;
}
glyphPixelOffset.y -= glyphNewlineOffset;

if (defined(glyph.billboard)) {
glyph.billboard._setTranslate(glyphPixelOffset);
Expand Down

0 comments on commit 689a166

Please sign in to comment.