Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for multi-line labels #2575

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/Labels.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
image : image
},
label : {
text : 'Label on top of scaling billboard',
text : 'Label on top of \n 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 @@ -213,44 +213,71 @@ define([

function repositionAllGlyphs(label, resolutionScale) {
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.computedWidth;
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 * resolutionScale;
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;

glyphPixelOffset.y *= resolutionScale;

Expand Down