| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652 |
- import BoundingRectangle from "../Core/BoundingRectangle.js";
- import Cartesian2 from "../Core/Cartesian2.js";
- import Cartesian3 from "../Core/Cartesian3.js";
- import Color from "../Core/Color.js";
- import Frozen from "../Core/Frozen.js";
- import defined from "../Core/defined.js";
- import DeveloperError from "../Core/DeveloperError.js";
- import DistanceDisplayCondition from "../Core/DistanceDisplayCondition.js";
- import NearFarScalar from "../Core/NearFarScalar.js";
- import Billboard from "./Billboard.js";
- import HeightReference from "./HeightReference.js";
- import HorizontalOrigin from "./HorizontalOrigin.js";
- import LabelStyle from "./LabelStyle.js";
- import SDFSettings from "./SDFSettings.js";
- import VerticalOrigin from "./VerticalOrigin.js";
-
- const fontInfoCache = {};
- let fontInfoCacheLength = 0;
- const fontInfoCacheMaxSize = 256;
- const defaultBackgroundColor = new Color(0.165, 0.165, 0.165, 0.8);
- const defaultBackgroundPadding = new Cartesian2(7, 5);
-
- const textTypes = Object.freeze({
- LTR: 0,
- RTL: 1,
- WEAK: 2,
- BRACKETS: 3,
- });
-
- function rebindAllGlyphs(label) {
- if (!label._rebindAllGlyphs && !label._repositionAllGlyphs) {
- // only push label if it's not already been marked dirty
- label._labelCollection._labelsToUpdate.push(label);
- }
- label._rebindAllGlyphs = true;
- }
-
- function repositionAllGlyphs(label) {
- if (!label._rebindAllGlyphs && !label._repositionAllGlyphs) {
- // only push label if it's not already been marked dirty
- label._labelCollection._labelsToUpdate.push(label);
- }
- label._repositionAllGlyphs = true;
- }
-
- function getCSSValue(element, property) {
- return document.defaultView
- .getComputedStyle(element, null)
- .getPropertyValue(property);
- }
-
- function parseFont(label) {
- let fontInfo = fontInfoCache[label._font];
- if (!defined(fontInfo)) {
- const div = document.createElement("div");
- div.style.position = "absolute";
- div.style.opacity = 0;
- div.style.font = label._font;
- document.body.appendChild(div);
-
- let lineHeight = parseFloat(getCSSValue(div, "line-height"));
- if (isNaN(lineHeight)) {
- // line-height isn't a number, i.e. 'normal'; apply default line-spacing
- lineHeight = undefined;
- }
-
- fontInfo = {
- family: getCSSValue(div, "font-family"),
- size: getCSSValue(div, "font-size").replace("px", ""),
- style: getCSSValue(div, "font-style"),
- weight: getCSSValue(div, "font-weight"),
- lineHeight: lineHeight,
- };
-
- document.body.removeChild(div);
- if (fontInfoCacheLength < fontInfoCacheMaxSize) {
- fontInfoCache[label._font] = fontInfo;
- fontInfoCacheLength++;
- }
- }
- label._fontFamily = fontInfo.family;
- label._fontSize = fontInfo.size;
- label._fontStyle = fontInfo.style;
- label._fontWeight = fontInfo.weight;
- label._lineHeight = fontInfo.lineHeight;
- }
-
- /**
- * @typedef {object} Label.ConstructorOptions
- *
- * Initialization options for the Label constructor
- *
- * @property {Cartesian3} position The cartesian position of the label.
- * @property {*} [id] A user-defined object to return when the label is picked with {@link Scene#pick}.
- * @property {boolean} [show=true] Determines if this label will be shown.
- * @property {string} [text] A string specifying the text of the label.
- * @property {string} [font='30px sans-serif'] A string specifying the font used to draw this label. Fonts are specified using the same syntax as the CSS 'font' property.
- * @property {LabelStyle} [style=LabelStyle.FILL] A {@link LabelStyle} specifying the style of the label.
- * @property {number} [scale=1.0] A number specifying the uniform scale that is multiplied with the label size.
- * @property {boolean} [showBackground=false] Determines if a background behind this label will be shown.
- * @property {Color} [backgroundColor=new Color(0.165, 0.165, 0.165, 0.8)] A {@link Color} specifying the background color of the label.
- * @property {Cartesian2} [backgroundPadding=new Cartesian2(7, 5)] A {@link Cartesian2} Specifying the horizontal and vertical background padding in pixels.
- * @property {Cartesian2} [pixelOffset=Cartesian2.ZERO] A {@link Cartesian2} specifying the pixel offset in screen space from the origin of this label.
- * @property {Cartesian3} [eyeOffset=Cartesian3.ZERO] A {@link Cartesian3} specifying the 3D Cartesian offset applied to this label in eye coordinates.
- * @property {HorizontalOrigin} [horizontalOrigin=HorizontalOrigin.LEFT] A {@link HorizontalOrigin} specifying the horizontal origin of this label.
- * @property {VerticalOrigin} [verticalOrigin=VerticalOrigin.BASELINE] A {@link VerticalOrigin} specifying the vertical origin of this label.
- * @property {HeightReference} [heightReference=HeightReference.NONE] A {@link HeightReference} specifying the height reference of this label.
- * @property {Color} [fillColor=Color.WHITE] A {@link Color} specifying the fill color of the label.
- * @property {Color} [outlineColor=Color.BLACK] A {@link Color} specifying the outline color of the label.
- * @property {number} [outlineWidth=1.0] A number specifying the outline width of the label.
- * @property {NearFarScalar} [translucencyByDistance] A {@link NearFarScalar} specifying near and far translucency properties of the label based on the label's distance from the camera.
- * @property {NearFarScalar} [pixelOffsetScaleByDistance] A {@link NearFarScalar} specifying near and far pixel offset scaling properties of the label based on the label's distance from the camera.
- * @property {NearFarScalar} [scaleByDistance] A {@link NearFarScalar} specifying near and far scaling properties of the label based on the label's distance from the camera.
- * @property {DistanceDisplayCondition} [distanceDisplayCondition] A {@link DistanceDisplayCondition} specifying at what distance from the camera that this label will be displayed.
- * @property {number} [disableDepthTestDistance] The distance from the camera, beyond which, depth testing is disabled—to, for example, prevent clipping against terrain.
- */
-
- /**
- * <div class="notice">
- * Create labels by calling {@link LabelCollection#add}. Do not call the constructor directly.
- * </div>
- *
- * @alias Label
- * @internalConstructor
- * @class
- *
- * @param {Label.ConstructorOptions} options Object describing initialization options
- * @param {LabelCollection} labelCollection Instance of LabelCollection
- *
- * @exception {DeveloperError} translucencyByDistance.far must be greater than translucencyByDistance.near
- * @exception {DeveloperError} pixelOffsetScaleByDistance.far must be greater than pixelOffsetScaleByDistance.near
- * @exception {DeveloperError} distanceDisplayCondition.far must be greater than distanceDisplayCondition.near
- *
- * @see LabelCollection
- * @see LabelCollection#add
- *
- * @demo {@link https://sandcastle.cesium.com/index.html?id=labels|Cesium Sandcastle Labels Demo}
- */
- function Label(options, labelCollection) {
- options = options ?? Frozen.EMPTY_OBJECT;
-
- //>>includeStart('debug', pragmas.debug);
- if (
- defined(options.disableDepthTestDistance) &&
- options.disableDepthTestDistance < 0.0
- ) {
- throw new DeveloperError(
- "disableDepthTestDistance must be greater than 0.0.",
- );
- }
- //>>includeEnd('debug');
-
- let translucencyByDistance = options.translucencyByDistance;
- let pixelOffsetScaleByDistance = options.pixelOffsetScaleByDistance;
- let scaleByDistance = options.scaleByDistance;
- let distanceDisplayCondition = options.distanceDisplayCondition;
- if (defined(translucencyByDistance)) {
- //>>includeStart('debug', pragmas.debug);
- if (translucencyByDistance.far <= translucencyByDistance.near) {
- throw new DeveloperError(
- "translucencyByDistance.far must be greater than translucencyByDistance.near.",
- );
- }
- //>>includeEnd('debug');
- translucencyByDistance = NearFarScalar.clone(translucencyByDistance);
- }
- if (defined(pixelOffsetScaleByDistance)) {
- //>>includeStart('debug', pragmas.debug);
- if (pixelOffsetScaleByDistance.far <= pixelOffsetScaleByDistance.near) {
- throw new DeveloperError(
- "pixelOffsetScaleByDistance.far must be greater than pixelOffsetScaleByDistance.near.",
- );
- }
- //>>includeEnd('debug');
- pixelOffsetScaleByDistance = NearFarScalar.clone(
- pixelOffsetScaleByDistance,
- );
- }
- if (defined(scaleByDistance)) {
- //>>includeStart('debug', pragmas.debug);
- if (scaleByDistance.far <= scaleByDistance.near) {
- throw new DeveloperError(
- "scaleByDistance.far must be greater than scaleByDistance.near.",
- );
- }
- //>>includeEnd('debug');
- scaleByDistance = NearFarScalar.clone(scaleByDistance);
- }
- if (defined(distanceDisplayCondition)) {
- //>>includeStart('debug', pragmas.debug);
- if (distanceDisplayCondition.far <= distanceDisplayCondition.near) {
- throw new DeveloperError(
- "distanceDisplayCondition.far must be greater than distanceDisplayCondition.near.",
- );
- }
- //>>includeEnd('debug');
- distanceDisplayCondition = DistanceDisplayCondition.clone(
- distanceDisplayCondition,
- );
- }
-
- this._renderedText = undefined;
- this._text = undefined;
- this._show = options.show ?? true;
- this._font = options.font ?? "30px sans-serif";
- this._fillColor = Color.clone(options.fillColor ?? Color.WHITE);
- this._outlineColor = Color.clone(options.outlineColor ?? Color.BLACK);
- this._outlineWidth = options.outlineWidth ?? 1.0;
- this._showBackground = options.showBackground ?? false;
- this._backgroundColor = Color.clone(
- options.backgroundColor ?? defaultBackgroundColor,
- );
- this._backgroundPadding = Cartesian2.clone(
- options.backgroundPadding ?? defaultBackgroundPadding,
- );
- this._style = options.style ?? LabelStyle.FILL;
- this._verticalOrigin = options.verticalOrigin ?? VerticalOrigin.BASELINE;
- this._horizontalOrigin = options.horizontalOrigin ?? HorizontalOrigin.LEFT;
- this._pixelOffset = Cartesian2.clone(options.pixelOffset ?? Cartesian2.ZERO);
- this._eyeOffset = Cartesian3.clone(options.eyeOffset ?? Cartesian3.ZERO);
- this._position = Cartesian3.clone(options.position ?? Cartesian3.ZERO);
- this._scale = options.scale ?? 1.0;
- this._id = options.id;
- this._translucencyByDistance = translucencyByDistance;
- this._pixelOffsetScaleByDistance = pixelOffsetScaleByDistance;
- this._scaleByDistance = scaleByDistance;
- this._heightReference = options.heightReference ?? HeightReference.NONE;
- this._distanceDisplayCondition = distanceDisplayCondition;
- this._disableDepthTestDistance = options.disableDepthTestDistance;
-
- this._labelCollection = labelCollection;
- this._glyphs = [];
- this._backgroundBillboard = undefined;
- this._batchIndex = undefined; // Used only by Vector3DTilePoints and BillboardCollection
-
- this._rebindAllGlyphs = true;
- this._repositionAllGlyphs = true;
-
- this._actualClampedPosition = undefined;
- this._removeCallbackFunc = undefined;
- this._mode = undefined;
-
- this._clusterShow = true;
-
- this.text = options.text ?? "";
-
- this._relativeSize = 1.0;
-
- parseFont(this);
-
- this._updateClamping();
- }
-
- Object.defineProperties(Label.prototype, {
- /**
- * Determines if this label will be shown. Use this to hide or show a label, instead
- * of removing it and re-adding it to the collection.
- * @memberof Label.prototype
- * @type {boolean}
- * @default true
- */
- show: {
- get: function () {
- return this._show;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
- //>>includeEnd('debug');
-
- if (this._show !== value) {
- this._show = value;
-
- const glyphs = this._glyphs;
- for (let i = 0, len = glyphs.length; i < len; i++) {
- const billboard = glyphs[i].billboard;
- if (defined(billboard)) {
- billboard.show = value;
- }
- }
- const backgroundBillboard = this._backgroundBillboard;
- if (defined(backgroundBillboard)) {
- backgroundBillboard.show = value;
- }
- }
- },
- },
-
- /**
- * Gets or sets the Cartesian position of this label.
- * @memberof Label.prototype
- * @type {Cartesian3}
- */
- position: {
- get: function () {
- return this._position;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
- //>>includeEnd('debug');
-
- const position = this._position;
- if (!Cartesian3.equals(position, value)) {
- Cartesian3.clone(value, position);
-
- const glyphs = this._glyphs;
- for (let i = 0, len = glyphs.length; i < len; i++) {
- const billboard = glyphs[i].billboard;
- if (defined(billboard)) {
- billboard.position = value;
- }
- }
- const backgroundBillboard = this._backgroundBillboard;
- if (defined(backgroundBillboard)) {
- backgroundBillboard.position = value;
- }
-
- this._updateClamping();
- }
- },
- },
-
- /**
- * Gets or sets the height reference of this billboard.
- * @memberof Label.prototype
- * @type {HeightReference}
- * @default HeightReference.NONE
- */
- heightReference: {
- get: function () {
- return this._heightReference;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
- //>>includeEnd('debug');
-
- if (value !== this._heightReference) {
- this._heightReference = value;
-
- const glyphs = this._glyphs;
- for (let i = 0, len = glyphs.length; i < len; i++) {
- const billboard = glyphs[i].billboard;
- if (defined(billboard)) {
- billboard.heightReference = value;
- }
- }
- const backgroundBillboard = this._backgroundBillboard;
- if (defined(backgroundBillboard)) {
- backgroundBillboard.heightReference = value;
- }
-
- repositionAllGlyphs(this);
-
- this._updateClamping();
- }
- },
- },
-
- /**
- * Gets or sets the text of this label.
- * @memberof Label.prototype
- * @type {string}
- */
- text: {
- get: function () {
- return this._text;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
- //>>includeEnd('debug');
-
- if (this._text !== value) {
- this._text = value;
-
- const renderedValue = Label.filterUnsupportedCharacters(value);
- this._renderedText = Label.enableRightToLeftDetection
- ? reverseRtl(renderedValue)
- : renderedValue;
- rebindAllGlyphs(this);
- }
- },
- },
-
- /**
- * Gets or sets the font used to draw this label. Fonts are specified using the same syntax as the CSS 'font' property.
- * @memberof Label.prototype
- * @type {string}
- * @default '30px sans-serif'
- * @see {@link http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#text-styles|HTML canvas 2D context text styles}
- */
- font: {
- get: function () {
- return this._font;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
- //>>includeEnd('debug');
-
- if (this._font !== value) {
- this._font = value;
- rebindAllGlyphs(this);
- parseFont(this);
- }
- },
- },
-
- /**
- * Gets or sets the fill color of this label.
- * @memberof Label.prototype
- * @type {Color}
- * @default Color.WHITE
- * @see {@link http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#fill-and-stroke-styles|HTML canvas 2D context fill and stroke styles}
- */
- fillColor: {
- get: function () {
- return this._fillColor;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
- //>>includeEnd('debug');
-
- const fillColor = this._fillColor;
- if (!Color.equals(fillColor, value)) {
- Color.clone(value, fillColor);
- rebindAllGlyphs(this);
- }
- },
- },
-
- /**
- * Gets or sets the outline color of this label.
- * @memberof Label.prototype
- * @type {Color}
- * @default Color.BLACK
- * @see {@link http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#fill-and-stroke-styles|HTML canvas 2D context fill and stroke styles}
- */
- outlineColor: {
- get: function () {
- return this._outlineColor;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
- //>>includeEnd('debug');
-
- const outlineColor = this._outlineColor;
- if (!Color.equals(outlineColor, value)) {
- Color.clone(value, outlineColor);
- rebindAllGlyphs(this);
- }
- },
- },
-
- /**
- * Gets or sets the outline width of this label.
- * @memberof Label.prototype
- * @type {number}
- * @default 1.0
- * @see {@link http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#fill-and-stroke-styles|HTML canvas 2D context fill and stroke styles}
- */
- outlineWidth: {
- get: function () {
- return this._outlineWidth;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
- //>>includeEnd('debug');
-
- if (this._outlineWidth !== value) {
- this._outlineWidth = value;
- rebindAllGlyphs(this);
- }
- },
- },
-
- /**
- * Determines if a background behind this label will be shown.
- * @memberof Label.prototype
- * @default false
- * @type {boolean}
- */
- showBackground: {
- get: function () {
- return this._showBackground;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
- //>>includeEnd('debug');
-
- if (this._showBackground !== value) {
- this._showBackground = value;
- rebindAllGlyphs(this);
- }
- },
- },
-
- /**
- * Gets or sets the background color of this label.
- * @memberof Label.prototype
- * @type {Color}
- * @default new Color(0.165, 0.165, 0.165, 0.8)
- */
- backgroundColor: {
- get: function () {
- return this._backgroundColor;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
- //>>includeEnd('debug');
-
- const backgroundColor = this._backgroundColor;
- if (!Color.equals(backgroundColor, value)) {
- Color.clone(value, backgroundColor);
-
- const backgroundBillboard = this._backgroundBillboard;
- if (defined(backgroundBillboard)) {
- backgroundBillboard.color = backgroundColor;
- }
- }
- },
- },
-
- /**
- * Gets or sets the background padding, in pixels, of this label. The <code>x</code> value
- * controls horizontal padding, and the <code>y</code> value controls vertical padding.
- * @memberof Label.prototype
- * @type {Cartesian2}
- * @default new Cartesian2(7, 5)
- */
- backgroundPadding: {
- get: function () {
- return this._backgroundPadding;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
- //>>includeEnd('debug');
-
- const backgroundPadding = this._backgroundPadding;
- if (!Cartesian2.equals(backgroundPadding, value)) {
- Cartesian2.clone(value, backgroundPadding);
- repositionAllGlyphs(this);
- }
- },
- },
-
- /**
- * Gets or sets the style of this label.
- * @memberof Label.prototype
- * @type {LabelStyle}
- * @default LabelStyle.FILL
- */
- style: {
- get: function () {
- return this._style;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
- //>>includeEnd('debug');
-
- if (this._style !== value) {
- this._style = value;
- rebindAllGlyphs(this);
- }
- },
- },
-
- /**
- * Gets or sets the pixel offset in screen space from the origin of this label. This is commonly used
- * to align multiple labels and billboards at the same position, e.g., an image and text. The
- * screen space origin is the top, left corner of the canvas; <code>x</code> increases from
- * left to right, and <code>y</code> increases from top to bottom.
- * <br /><br />
- * <div align='center'>
- * <table border='0' cellpadding='5'><tr>
- * <td align='center'><code>default</code><br/><img src='Images/Label.setPixelOffset.default.png' width='250' height='188' /></td>
- * <td align='center'><code>l.pixeloffset = new Cartesian2(25, 75);</code><br/><img src='Images/Label.setPixelOffset.x50y-25.png' width='250' height='188' /></td>
- * </tr></table>
- * The label's origin is indicated by the yellow point.
- * </div>
- * @memberof Label.prototype
- * @type {Cartesian2}
- * @default Cartesian2.ZERO
- */
- pixelOffset: {
- get: function () {
- return this._pixelOffset;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
- //>>includeEnd('debug');
-
- const pixelOffset = this._pixelOffset;
- if (!Cartesian2.equals(pixelOffset, value)) {
- Cartesian2.clone(value, pixelOffset);
-
- const glyphs = this._glyphs;
- for (let i = 0, len = glyphs.length; i < len; i++) {
- const glyph = glyphs[i];
- if (defined(glyph.billboard)) {
- glyph.billboard.pixelOffset = value;
- }
- }
- const backgroundBillboard = this._backgroundBillboard;
- if (defined(backgroundBillboard)) {
- backgroundBillboard.pixelOffset = value;
- }
- }
- },
- },
-
- /**
- * Gets or sets near and far translucency properties of a Label based on the Label's distance from the camera.
- * A label's translucency will interpolate between the {@link NearFarScalar#nearValue} and
- * {@link NearFarScalar#farValue} while the camera distance falls within the lower and upper bounds
- * of the specified {@link NearFarScalar#near} and {@link NearFarScalar#far}.
- * Outside of these ranges the label's translucency remains clamped to the nearest bound. If undefined,
- * translucencyByDistance will be disabled.
- * @memberof Label.prototype
- * @type {NearFarScalar}
- *
- * @example
- * // Example 1.
- * // Set a label's translucencyByDistance to 1.0 when the
- * // camera is 1500 meters from the label and disappear as
- * // the camera distance approaches 8.0e6 meters.
- * text.translucencyByDistance = new Cesium.NearFarScalar(1.5e2, 1.0, 8.0e6, 0.0);
- *
- * @example
- * // Example 2.
- * // disable translucency by distance
- * text.translucencyByDistance = undefined;
- */
- translucencyByDistance: {
- get: function () {
- return this._translucencyByDistance;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (defined(value) && value.far <= value.near) {
- throw new DeveloperError(
- "far distance must be greater than near distance.",
- );
- }
- //>>includeEnd('debug');
-
- const translucencyByDistance = this._translucencyByDistance;
- if (!NearFarScalar.equals(translucencyByDistance, value)) {
- this._translucencyByDistance = NearFarScalar.clone(
- value,
- translucencyByDistance,
- );
-
- const glyphs = this._glyphs;
- for (let i = 0, len = glyphs.length; i < len; i++) {
- const glyph = glyphs[i];
- if (defined(glyph.billboard)) {
- glyph.billboard.translucencyByDistance = value;
- }
- }
- const backgroundBillboard = this._backgroundBillboard;
- if (defined(backgroundBillboard)) {
- backgroundBillboard.translucencyByDistance = value;
- }
- }
- },
- },
-
- /**
- * Gets or sets near and far pixel offset scaling properties of a Label based on the Label's distance from the camera.
- * A label's pixel offset will be scaled between the {@link NearFarScalar#nearValue} and
- * {@link NearFarScalar#farValue} while the camera distance falls within the lower and upper bounds
- * of the specified {@link NearFarScalar#near} and {@link NearFarScalar#far}.
- * Outside of these ranges the label's pixel offset scaling remains clamped to the nearest bound. If undefined,
- * pixelOffsetScaleByDistance will be disabled.
- * @memberof Label.prototype
- * @type {NearFarScalar}
- *
- * @example
- * // Example 1.
- * // Set a label's pixel offset scale to 0.0 when the
- * // camera is 1500 meters from the label and scale pixel offset to 10.0 pixels
- * // in the y direction the camera distance approaches 8.0e6 meters.
- * text.pixelOffset = new Cesium.Cartesian2(0.0, 1.0);
- * text.pixelOffsetScaleByDistance = new Cesium.NearFarScalar(1.5e2, 0.0, 8.0e6, 10.0);
- *
- * @example
- * // Example 2.
- * // disable pixel offset by distance
- * text.pixelOffsetScaleByDistance = undefined;
- */
- pixelOffsetScaleByDistance: {
- get: function () {
- return this._pixelOffsetScaleByDistance;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (defined(value) && value.far <= value.near) {
- throw new DeveloperError(
- "far distance must be greater than near distance.",
- );
- }
- //>>includeEnd('debug');
-
- const pixelOffsetScaleByDistance = this._pixelOffsetScaleByDistance;
- if (!NearFarScalar.equals(pixelOffsetScaleByDistance, value)) {
- this._pixelOffsetScaleByDistance = NearFarScalar.clone(
- value,
- pixelOffsetScaleByDistance,
- );
-
- const glyphs = this._glyphs;
- for (let i = 0, len = glyphs.length; i < len; i++) {
- const glyph = glyphs[i];
- if (defined(glyph.billboard)) {
- glyph.billboard.pixelOffsetScaleByDistance = value;
- }
- }
- const backgroundBillboard = this._backgroundBillboard;
- if (defined(backgroundBillboard)) {
- backgroundBillboard.pixelOffsetScaleByDistance = value;
- }
- }
- },
- },
-
- /**
- * Gets or sets near and far scaling properties of a Label based on the label's distance from the camera.
- * A label's scale will interpolate between the {@link NearFarScalar#nearValue} and
- * {@link NearFarScalar#farValue} while the camera distance falls within the lower and upper bounds
- * of the specified {@link NearFarScalar#near} and {@link NearFarScalar#far}.
- * Outside of these ranges the label's scale remains clamped to the nearest bound. If undefined,
- * scaleByDistance will be disabled.
- * @memberof Label.prototype
- * @type {NearFarScalar}
- *
- * @example
- * // Example 1.
- * // Set a label's scaleByDistance to scale by 1.5 when the
- * // camera is 1500 meters from the label and disappear as
- * // the camera distance approaches 8.0e6 meters.
- * label.scaleByDistance = new Cesium.NearFarScalar(1.5e2, 1.5, 8.0e6, 0.0);
- *
- * @example
- * // Example 2.
- * // disable scaling by distance
- * label.scaleByDistance = undefined;
- */
- scaleByDistance: {
- get: function () {
- return this._scaleByDistance;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (defined(value) && value.far <= value.near) {
- throw new DeveloperError(
- "far distance must be greater than near distance.",
- );
- }
- //>>includeEnd('debug');
-
- const scaleByDistance = this._scaleByDistance;
- if (!NearFarScalar.equals(scaleByDistance, value)) {
- this._scaleByDistance = NearFarScalar.clone(value, scaleByDistance);
-
- const glyphs = this._glyphs;
- for (let i = 0, len = glyphs.length; i < len; i++) {
- const glyph = glyphs[i];
- if (defined(glyph.billboard)) {
- glyph.billboard.scaleByDistance = value;
- }
- }
- const backgroundBillboard = this._backgroundBillboard;
- if (defined(backgroundBillboard)) {
- backgroundBillboard.scaleByDistance = value;
- }
- }
- },
- },
-
- /**
- * Gets and sets the 3D Cartesian offset applied to this label in eye coordinates. Eye coordinates is a left-handed
- * coordinate system, where <code>x</code> points towards the viewer's right, <code>y</code> points up, and
- * <code>z</code> points into the screen. Eye coordinates use the same scale as world and model coordinates,
- * which is typically meters.
- * <br /><br />
- * An eye offset is commonly used to arrange multiple label or objects at the same position, e.g., to
- * arrange a label above its corresponding 3D model.
- * <br /><br />
- * Below, the label is positioned at the center of the Earth but an eye offset makes it always
- * appear on top of the Earth regardless of the viewer's or Earth's orientation.
- * <br /><br />
- * <div align='center'>
- * <table border='0' cellpadding='5'><tr>
- * <td align='center'><img src='Images/Billboard.setEyeOffset.one.png' width='250' height='188' /></td>
- * <td align='center'><img src='Images/Billboard.setEyeOffset.two.png' width='250' height='188' /></td>
- * </tr></table>
- * <code>l.eyeOffset = new Cartesian3(0.0, 8000000.0, 0.0);</code><br /><br />
- * </div>
- * @memberof Label.prototype
- * @type {Cartesian3}
- * @default Cartesian3.ZERO
- */
- eyeOffset: {
- get: function () {
- return this._eyeOffset;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
- //>>includeEnd('debug');
-
- const eyeOffset = this._eyeOffset;
- if (!Cartesian3.equals(eyeOffset, value)) {
- Cartesian3.clone(value, eyeOffset);
-
- const glyphs = this._glyphs;
- for (let i = 0, len = glyphs.length; i < len; i++) {
- const glyph = glyphs[i];
- if (defined(glyph.billboard)) {
- glyph.billboard.eyeOffset = value;
- }
- }
- const backgroundBillboard = this._backgroundBillboard;
- if (defined(backgroundBillboard)) {
- backgroundBillboard.eyeOffset = value;
- }
- }
- },
- },
-
- /**
- * Gets or sets the horizontal origin of this label, which determines if the label is drawn
- * to the left, center, or right of its anchor position.
- * <br /><br />
- * <div align='center'>
- * <img src='Images/Billboard.setHorizontalOrigin.png' width='648' height='196' /><br />
- * </div>
- * @memberof Label.prototype
- * @type {HorizontalOrigin}
- * @default HorizontalOrigin.LEFT
- * @example
- * // Use a top, right origin
- * l.horizontalOrigin = Cesium.HorizontalOrigin.RIGHT;
- * l.verticalOrigin = Cesium.VerticalOrigin.TOP;
- */
- horizontalOrigin: {
- get: function () {
- return this._horizontalOrigin;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
- //>>includeEnd('debug');
-
- if (this._horizontalOrigin !== value) {
- this._horizontalOrigin = value;
- repositionAllGlyphs(this);
- }
- },
- },
-
- /**
- * Gets or sets the vertical origin of this label, which determines if the label is
- * to the above, below, or at the center of its anchor position.
- * <br /><br />
- * <div align='center'>
- * <img src='Images/Billboard.setVerticalOrigin.png' width='695' height='175' /><br />
- * </div>
- * @memberof Label.prototype
- * @type {VerticalOrigin}
- * @default VerticalOrigin.BASELINE
- * @example
- * // Use a top, right origin
- * l.horizontalOrigin = Cesium.HorizontalOrigin.RIGHT;
- * l.verticalOrigin = Cesium.VerticalOrigin.TOP;
- */
- verticalOrigin: {
- get: function () {
- return this._verticalOrigin;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
- //>>includeEnd('debug');
-
- if (this._verticalOrigin !== value) {
- this._verticalOrigin = value;
-
- const glyphs = this._glyphs;
- for (let i = 0, len = glyphs.length; i < len; i++) {
- const glyph = glyphs[i];
- if (defined(glyph.billboard)) {
- glyph.billboard.verticalOrigin = value;
- }
- }
- const backgroundBillboard = this._backgroundBillboard;
- if (defined(backgroundBillboard)) {
- backgroundBillboard.verticalOrigin = value;
- }
-
- repositionAllGlyphs(this);
- }
- },
- },
-
- /**
- * Gets or sets the uniform scale that is multiplied with the label's size in pixels.
- * A scale of <code>1.0</code> does not change the size of the label; a scale greater than
- * <code>1.0</code> enlarges the label; a positive scale less than <code>1.0</code> shrinks
- * the label.
- * <br /><br />
- * Applying a large scale value may pixelate the label. To make text larger without pixelation,
- * use a larger font size when calling {@link Label#font} instead.
- * <br /><br />
- * <div align='center'>
- * <img src='Images/Label.setScale.png' width='400' height='300' /><br/>
- * From left to right in the above image, the scales are <code>0.5</code>, <code>1.0</code>,
- * and <code>2.0</code>.
- * </div>
- * @memberof Label.prototype
- * @type {number}
- * @default 1.0
- */
- scale: {
- get: function () {
- return this._scale;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (!defined(value)) {
- throw new DeveloperError("value is required.");
- }
- //>>includeEnd('debug');
-
- if (this._scale !== value) {
- this._scale = value;
-
- const glyphs = this._glyphs;
- for (let i = 0, len = glyphs.length; i < len; i++) {
- const glyph = glyphs[i];
- if (defined(glyph.billboard)) {
- glyph.billboard.scale = value * this._relativeSize;
- }
- }
- const backgroundBillboard = this._backgroundBillboard;
- if (defined(backgroundBillboard)) {
- backgroundBillboard.scale = value * this._relativeSize;
- }
-
- repositionAllGlyphs(this);
- }
- },
- },
-
- /**
- * Gets the total scale of the label, which is the label's scale multiplied by the computed relative size
- * of the desired font compared to the generated glyph size.
- * @memberof Label.prototype
- * @type {number}
- * @default 1.0
- */
- totalScale: {
- get: function () {
- return this._scale * this._relativeSize;
- },
- },
-
- /**
- * Gets or sets the condition specifying at what distance from the camera that this label will be displayed.
- * @memberof Label.prototype
- * @type {DistanceDisplayCondition}
- * @default undefined
- */
- distanceDisplayCondition: {
- get: function () {
- return this._distanceDisplayCondition;
- },
- set: function (value) {
- //>>includeStart('debug', pragmas.debug);
- if (defined(value) && value.far <= value.near) {
- throw new DeveloperError("far must be greater than near");
- }
- //>>includeEnd('debug');
- if (
- !DistanceDisplayCondition.equals(value, this._distanceDisplayCondition)
- ) {
- this._distanceDisplayCondition = DistanceDisplayCondition.clone(
- value,
- this._distanceDisplayCondition,
- );
-
- const glyphs = this._glyphs;
- for (let i = 0, len = glyphs.length; i < len; i++) {
- const glyph = glyphs[i];
- if (defined(glyph.billboard)) {
- glyph.billboard.distanceDisplayCondition = value;
- }
- }
- const backgroundBillboard = this._backgroundBillboard;
- if (defined(backgroundBillboard)) {
- backgroundBillboard.distanceDisplayCondition = value;
- }
- }
- },
- },
-
- /**
- * Gets or sets the distance from the camera, beyond which, depth testing is disabled—to, for example, prevent clipping against terrain.
- * When set to <code>undefined</code> or
- * <code>0</code>, the depth test is always applied. When set to Number.<code>POSITIVE_INFINITY</code>, the depth test is never applied.
- * @memberof Label.prototype
- * @type {number|undefined}
- * @default undefined
- */
- disableDepthTestDistance: {
- get: function () {
- return this._disableDepthTestDistance;
- },
- set: function (value) {
- if (this._disableDepthTestDistance !== value) {
- //>>includeStart('debug', pragmas.debug);
- if (defined(value) && value < 0.0) {
- throw new DeveloperError(
- "disableDepthTestDistance must be greater than 0.0.",
- );
- }
- //>>includeEnd('debug');
- this._disableDepthTestDistance = value;
-
- const glyphs = this._glyphs;
- for (let i = 0, len = glyphs.length; i < len; i++) {
- const glyph = glyphs[i];
- if (defined(glyph.billboard)) {
- glyph.billboard.disableDepthTestDistance = value;
- }
- }
- const backgroundBillboard = this._backgroundBillboard;
- if (defined(backgroundBillboard)) {
- backgroundBillboard.disableDepthTestDistance = value;
- }
- }
- },
- },
-
- /**
- * Gets or sets the user-defined value returned when the label is picked.
- * @memberof Label.prototype
- * @type {*}
- */
- id: {
- get: function () {
- return this._id;
- },
- set: function (value) {
- if (this._id !== value) {
- this._id = value;
-
- const glyphs = this._glyphs;
- for (let i = 0, len = glyphs.length; i < len; i++) {
- const glyph = glyphs[i];
- if (defined(glyph.billboard)) {
- glyph.billboard.id = value;
- }
- }
- const backgroundBillboard = this._backgroundBillboard;
- if (defined(backgroundBillboard)) {
- backgroundBillboard.id = value;
- }
- }
- },
- },
-
- /**
- * @private
- */
- pickId: {
- get: function () {
- if (this._glyphs.length === 0 || !defined(this._glyphs[0].billboard)) {
- return undefined;
- }
- return this._glyphs[0].billboard.pickId;
- },
- },
-
- /**
- * Keeps track of the position of the label based on the height reference.
- * @memberof Label.prototype
- * @type {Cartesian3}
- * @private
- */
- _clampedPosition: {
- get: function () {
- return this._actualClampedPosition;
- },
- set: function (value) {
- this._actualClampedPosition = Cartesian3.clone(
- value,
- this._actualClampedPosition,
- );
-
- const glyphs = this._glyphs;
- for (let i = 0, len = glyphs.length; i < len; i++) {
- const glyph = glyphs[i];
- if (defined(glyph.billboard)) {
- // Set all the private values here, because we already clamped to ground
- // so we don't want to do it again for every glyph
- glyph.billboard._clampedPosition = value;
- }
- }
- const backgroundBillboard = this._backgroundBillboard;
- if (defined(backgroundBillboard)) {
- backgroundBillboard._clampedPosition = value;
- }
- },
- },
-
- /**
- * Determines whether or not this label will be shown or hidden because it was clustered.
- * @memberof Label.prototype
- * @type {boolean}
- * @default true
- * @private
- */
- clusterShow: {
- get: function () {
- return this._clusterShow;
- },
- set: function (value) {
- if (this._clusterShow !== value) {
- this._clusterShow = value;
-
- const glyphs = this._glyphs;
- for (let i = 0, len = glyphs.length; i < len; i++) {
- const glyph = glyphs[i];
- if (defined(glyph.billboard)) {
- glyph.billboard.clusterShow = value;
- }
- }
- const backgroundBillboard = this._backgroundBillboard;
- if (defined(backgroundBillboard)) {
- backgroundBillboard.clusterShow = value;
- }
- }
- },
- },
-
- /**
- * When <code>true</code>, this label is ready to render, i.e., the glyphs have been mapped and the WebGL resources are created. This property is exposed for testing purposes.
- * @memberof Label.prototype
- * @type {boolean}
- * @readonly
- * @private
- */
- ready: {
- get: function () {
- if (this._rebindAllGlyphs || this._repositionAllGlyphs) {
- return false;
- }
-
- if (
- defined(this._backgroundBillboard) &&
- !this._backgroundBillboard.ready
- ) {
- return false;
- }
-
- const glyphs = this._glyphs;
- for (let i = 0, len = glyphs.length; i < len; i++) {
- const glyph = glyphs[i];
- if (defined(glyph.billboard) && !glyph.billboard.ready) {
- return false;
- }
- }
-
- return true;
- },
- },
- });
-
- Label.prototype._updateClamping = function () {
- Billboard._updateClamping(this._labelCollection, this);
- };
-
- /**
- * Computes the screen-space position of the label's origin, taking into account eye and pixel offsets.
- * The screen space origin is the top, left corner of the canvas; <code>x</code> increases from
- * left to right, and <code>y</code> increases from top to bottom.
- *
- * @param {Scene} scene The scene the label is in.
- * @param {Cartesian2} [result] The object onto which to store the result.
- * @returns {Cartesian2} The screen-space position of the label.
- *
- *
- * @example
- * console.log(l.computeScreenSpacePosition(scene).toString());
- *
- * @see Label#eyeOffset
- * @see Label#pixelOffset
- */
- Label.prototype.computeScreenSpacePosition = function (scene, result) {
- //>>includeStart('debug', pragmas.debug);
- if (!defined(scene)) {
- throw new DeveloperError("scene is required.");
- }
- //>>includeEnd('debug');
-
- if (!defined(result)) {
- result = new Cartesian2();
- }
-
- const labelCollection = this._labelCollection;
- const modelMatrix = labelCollection.modelMatrix;
- const actualPosition = defined(this._actualClampedPosition)
- ? this._actualClampedPosition
- : this._position;
-
- const windowCoordinates = Billboard._computeScreenSpacePosition(
- modelMatrix,
- actualPosition,
- this._eyeOffset,
- this._pixelOffset,
- scene,
- result,
- );
- return windowCoordinates;
- };
-
- /**
- * Gets a label's screen space bounding box centered around screenSpacePosition.
- * @param {Label} label The label to get the screen space bounding box for.
- * @param {Cartesian2} screenSpacePosition The screen space center of the label.
- * @param {BoundingRectangle} [result] The object onto which to store the result.
- * @returns {BoundingRectangle} The screen space bounding box.
- *
- * @private
- */
- Label.getScreenSpaceBoundingBox = function (
- label,
- screenSpacePosition,
- result,
- ) {
- let x = 0;
- let y = 0;
- let width = 0;
- let height = 0;
- const scale = label.totalScale;
-
- const backgroundBillboard = label._backgroundBillboard;
- if (defined(backgroundBillboard)) {
- x = screenSpacePosition.x + backgroundBillboard._translate.x;
- y = screenSpacePosition.y - backgroundBillboard._translate.y;
- width = backgroundBillboard.width * scale;
- height = backgroundBillboard.height * scale;
-
- if (
- label.verticalOrigin === VerticalOrigin.BOTTOM ||
- label.verticalOrigin === VerticalOrigin.BASELINE
- ) {
- y -= height;
- } else if (label.verticalOrigin === VerticalOrigin.CENTER) {
- y -= height * 0.5;
- }
- } else {
- x = Number.POSITIVE_INFINITY;
- y = Number.POSITIVE_INFINITY;
- let maxX = 0;
- let maxY = 0;
- const glyphs = label._glyphs;
- const length = glyphs.length;
- for (let i = 0; i < length; ++i) {
- const glyph = glyphs[i];
- const billboard = glyph.billboard;
- if (!defined(billboard)) {
- continue;
- }
-
- const glyphX = screenSpacePosition.x + billboard._translate.x;
- let glyphY = screenSpacePosition.y - billboard._translate.y;
- const glyphWidth = glyph.dimensions.width * scale;
- const glyphHeight = glyph.dimensions.height * scale;
-
- if (
- label.verticalOrigin === VerticalOrigin.BOTTOM ||
- label.verticalOrigin === VerticalOrigin.BASELINE
- ) {
- glyphY -= glyphHeight;
- } else if (label.verticalOrigin === VerticalOrigin.CENTER) {
- glyphY -= glyphHeight * 0.5;
- }
-
- if (label._verticalOrigin === VerticalOrigin.TOP) {
- glyphY += SDFSettings.PADDING * scale;
- } else if (
- label._verticalOrigin === VerticalOrigin.BOTTOM ||
- label._verticalOrigin === VerticalOrigin.BASELINE
- ) {
- glyphY -= SDFSettings.PADDING * scale;
- }
-
- x = Math.min(x, glyphX);
- y = Math.min(y, glyphY);
- maxX = Math.max(maxX, glyphX + glyphWidth);
- maxY = Math.max(maxY, glyphY + glyphHeight);
- }
-
- width = maxX - x;
- height = maxY - y;
- }
-
- if (!defined(result)) {
- result = new BoundingRectangle();
- }
-
- result.x = x;
- result.y = y;
- result.width = width;
- result.height = height;
-
- return result;
- };
-
- /**
- * Removes control characters and soft hyphon (auto-wrap) characters, which will cause an error when rendering a glyph. This does not remove tabs, carriage returns, or newlines.
- * @private
- * @param {string} text The original label text
- * @returns {string} The renderable filtered text
- */
- Label.filterUnsupportedCharacters = function (text) {
- const problematicCharactersRegex = new RegExp(
- // eslint-disable-next-line no-control-regex
- /[\u0000-\u0008\u000E-\u001F\u00ad\u202a-\u206f\u200b-\u200f]/,
- "g",
- );
- return text.replace(problematicCharactersRegex, "");
- };
-
- /**
- * Determines if this label equals another label. Labels are equal if all their properties
- * are equal. Labels in different collections can be equal.
- *
- * @param {Label} [other] The label to compare for equality.
- * @returns {boolean} <code>true</code> if the labels are equal; otherwise, <code>false</code>.
- */
- Label.prototype.equals = function (other) {
- return (
- this === other ||
- (defined(other) &&
- this._show === other._show &&
- this._scale === other._scale &&
- this._outlineWidth === other._outlineWidth &&
- this._showBackground === other._showBackground &&
- this._style === other._style &&
- this._verticalOrigin === other._verticalOrigin &&
- this._horizontalOrigin === other._horizontalOrigin &&
- this._heightReference === other._heightReference &&
- this._renderedText === other._renderedText &&
- this._font === other._font &&
- Cartesian3.equals(this._position, other._position) &&
- Color.equals(this._fillColor, other._fillColor) &&
- Color.equals(this._outlineColor, other._outlineColor) &&
- Color.equals(this._backgroundColor, other._backgroundColor) &&
- Cartesian2.equals(this._backgroundPadding, other._backgroundPadding) &&
- Cartesian2.equals(this._pixelOffset, other._pixelOffset) &&
- Cartesian3.equals(this._eyeOffset, other._eyeOffset) &&
- NearFarScalar.equals(
- this._translucencyByDistance,
- other._translucencyByDistance,
- ) &&
- NearFarScalar.equals(
- this._pixelOffsetScaleByDistance,
- other._pixelOffsetScaleByDistance,
- ) &&
- NearFarScalar.equals(this._scaleByDistance, other._scaleByDistance) &&
- DistanceDisplayCondition.equals(
- this._distanceDisplayCondition,
- other._distanceDisplayCondition,
- ) &&
- this._disableDepthTestDistance === other._disableDepthTestDistance &&
- this._id === other._id)
- );
- };
-
- /**
- * Returns true if this object was destroyed; otherwise, false.
- * <br /><br />
- * If this object was destroyed, it should not be used; calling any function other than
- * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
- *
- * @returns {boolean} True if this object was destroyed; otherwise, false.
- */
- Label.prototype.isDestroyed = function () {
- return false;
- };
-
- /**
- * Determines whether or not run the algorithm, that match the text of the label to right-to-left languages
- * @memberof Label
- * @type {boolean}
- * @default false
- *
- * @example
- * // Example 1.
- * // Set a label's rightToLeft before init
- * Cesium.Label.enableRightToLeftDetection = true;
- * const myLabelEntity = viewer.entities.add({
- * label: {
- * id: 'my label',
- * text: 'זה טקסט בעברית \n ועכשיו יורדים שורה',
- * }
- * });
- *
- * @example
- * // Example 2.
- * const myLabelEntity = viewer.entities.add({
- * label: {
- * id: 'my label',
- * text: 'English text'
- * }
- * });
- * // Set a label's rightToLeft after init
- * Cesium.Label.enableRightToLeftDetection = true;
- * myLabelEntity.text = 'טקסט חדש';
- */
- Label.enableRightToLeftDetection = false;
-
- function convertTextToTypes(text, rtlChars) {
- const ltrChars = /[a-zA-Z0-9]/;
- const bracketsChars = /[()[\]{}<>]/;
- const parsedText = [];
- let word = "";
- let lastType = textTypes.LTR;
- let currentType = "";
- const textLength = text.length;
- for (let textIndex = 0; textIndex < textLength; ++textIndex) {
- const character = text.charAt(textIndex);
- if (rtlChars.test(character)) {
- currentType = textTypes.RTL;
- } else if (ltrChars.test(character)) {
- currentType = textTypes.LTR;
- } else if (bracketsChars.test(character)) {
- currentType = textTypes.BRACKETS;
- } else {
- currentType = textTypes.WEAK;
- }
-
- if (textIndex === 0) {
- lastType = currentType;
- }
-
- if (lastType === currentType && currentType !== textTypes.BRACKETS) {
- word += character;
- } else {
- if (word !== "") {
- parsedText.push({ Type: lastType, Word: word });
- }
- lastType = currentType;
- word = character;
- }
- }
- parsedText.push({ Type: currentType, Word: word });
- return parsedText;
- }
-
- function reverseWord(word) {
- return word.split("").reverse().join("");
- }
-
- function spliceWord(result, pointer, word) {
- return result.slice(0, pointer) + word + result.slice(pointer);
- }
-
- function reverseBrackets(bracket) {
- switch (bracket) {
- case "(":
- return ")";
- case ")":
- return "(";
- case "[":
- return "]";
- case "]":
- return "[";
- case "{":
- return "}";
- case "}":
- return "{";
- case "<":
- return ">";
- case ">":
- return "<";
- }
- }
-
- // To add another language, add its Unicode block range(s) to the below regex.
- const hebrew = "\u05D0-\u05EA";
- const arabic = "\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF";
- const rtlChars = new RegExp(`[${hebrew}${arabic}]`);
-
- /**
- *
- * @param {string} value the text to parse and reorder
- * @returns {string} the text as rightToLeft direction
- * @private
- */
- function reverseRtl(value) {
- const texts = value.split("\n");
- let result = "";
- for (let i = 0; i < texts.length; i++) {
- const text = texts[i];
- // first character of the line is a RTL character, so need to manage different cases
- const rtlDir = rtlChars.test(text.charAt(0));
- const parsedText = convertTextToTypes(text, rtlChars);
-
- let splicePointer = 0;
- let line = "";
- for (let wordIndex = 0; wordIndex < parsedText.length; ++wordIndex) {
- const subText = parsedText[wordIndex];
- const reverse =
- subText.Type === textTypes.BRACKETS
- ? reverseBrackets(subText.Word)
- : reverseWord(subText.Word);
- if (rtlDir) {
- if (subText.Type === textTypes.RTL) {
- line = reverse + line;
- splicePointer = 0;
- } else if (subText.Type === textTypes.LTR) {
- line = spliceWord(line, splicePointer, subText.Word);
- splicePointer += subText.Word.length;
- } else if (
- subText.Type === textTypes.WEAK ||
- subText.Type === textTypes.BRACKETS
- ) {
- // current word is weak, last one was bracket
- if (
- subText.Type === textTypes.WEAK &&
- parsedText[wordIndex - 1].Type === textTypes.BRACKETS
- ) {
- line = reverse + line;
- }
- // current word is weak or bracket, last one was rtl
- else if (parsedText[wordIndex - 1].Type === textTypes.RTL) {
- line = reverse + line;
- splicePointer = 0;
- }
- // current word is weak or bracket, there is at least one more word
- else if (parsedText.length > wordIndex + 1) {
- // next word is rtl
- if (parsedText[wordIndex + 1].Type === textTypes.RTL) {
- line = reverse + line;
- splicePointer = 0;
- } else {
- line = spliceWord(line, splicePointer, subText.Word);
- splicePointer += subText.Word.length;
- }
- }
- // current word is weak or bracket, and it the last in this line
- else {
- line = spliceWord(line, 0, reverse);
- }
- }
- }
- // ltr line, rtl word
- else if (subText.Type === textTypes.RTL) {
- line = spliceWord(line, splicePointer, reverse);
- }
- // ltr line, ltr word
- else if (subText.Type === textTypes.LTR) {
- line += subText.Word;
- splicePointer = line.length;
- }
- // ltr line, weak or bracket word
- else if (
- subText.Type === textTypes.WEAK ||
- subText.Type === textTypes.BRACKETS
- ) {
- // not first word in line
- if (wordIndex > 0) {
- // last word was rtl
- if (parsedText[wordIndex - 1].Type === textTypes.RTL) {
- // there is at least one more word
- if (parsedText.length > wordIndex + 1) {
- // next word is rtl
- if (parsedText[wordIndex + 1].Type === textTypes.RTL) {
- line = spliceWord(line, splicePointer, reverse);
- } else {
- line += subText.Word;
- splicePointer = line.length;
- }
- } else {
- line += subText.Word;
- }
- } else {
- line += subText.Word;
- splicePointer = line.length;
- }
- } else {
- line += subText.Word;
- splicePointer = line.length;
- }
- }
- }
-
- result += line;
- if (i < texts.length - 1) {
- result += "\n";
- }
- }
- return result;
- }
- export default Label;
|