From 689a166f6d0fb265a1fcddbeac098a9343734a64 Mon Sep 17 00:00:00 2001 From: Alex Wood Date: Sun, 29 Dec 2013 13:13:49 -0500 Subject: [PATCH] Initial support for newlines in labels. --- Apps/Sandcastle/gallery/Labels.html | 2 +- Source/Scene/LabelCollection.js | 45 +++++++++++++++++++++++------ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/Apps/Sandcastle/gallery/Labels.html b/Apps/Sandcastle/gallery/Labels.html index 0a155c95e3c9..550ce10b00ad 100644 --- a/Apps/Sandcastle/gallery/Labels.html +++ b/Apps/Sandcastle/gallery/Labels.html @@ -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), diff --git a/Source/Scene/LabelCollection.js b/Source/Scene/LabelCollection.js index a135c62a1c9f..4d41bb6573d7 100644 --- a/Source/Scene/LabelCollection.js +++ b/Source/Scene/LabelCollection.js @@ -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);