智慧水务管理系统 - 精河县供水工程综合管理平台

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  1. import BoundingRectangle from "../Core/BoundingRectangle.js";
  2. import Cartesian2 from "../Core/Cartesian2.js";
  3. import Cartesian3 from "../Core/Cartesian3.js";
  4. import Color from "../Core/Color.js";
  5. import Frozen from "../Core/Frozen.js";
  6. import defined from "../Core/defined.js";
  7. import DeveloperError from "../Core/DeveloperError.js";
  8. import DistanceDisplayCondition from "../Core/DistanceDisplayCondition.js";
  9. import NearFarScalar from "../Core/NearFarScalar.js";
  10. import Billboard from "./Billboard.js";
  11. import HeightReference from "./HeightReference.js";
  12. import HorizontalOrigin from "./HorizontalOrigin.js";
  13. import LabelStyle from "./LabelStyle.js";
  14. import SDFSettings from "./SDFSettings.js";
  15. import VerticalOrigin from "./VerticalOrigin.js";
  16. const fontInfoCache = {};
  17. let fontInfoCacheLength = 0;
  18. const fontInfoCacheMaxSize = 256;
  19. const defaultBackgroundColor = new Color(0.165, 0.165, 0.165, 0.8);
  20. const defaultBackgroundPadding = new Cartesian2(7, 5);
  21. const textTypes = Object.freeze({
  22. LTR: 0,
  23. RTL: 1,
  24. WEAK: 2,
  25. BRACKETS: 3,
  26. });
  27. function rebindAllGlyphs(label) {
  28. if (!label._rebindAllGlyphs && !label._repositionAllGlyphs) {
  29. // only push label if it's not already been marked dirty
  30. label._labelCollection._labelsToUpdate.push(label);
  31. }
  32. label._rebindAllGlyphs = true;
  33. }
  34. function repositionAllGlyphs(label) {
  35. if (!label._rebindAllGlyphs && !label._repositionAllGlyphs) {
  36. // only push label if it's not already been marked dirty
  37. label._labelCollection._labelsToUpdate.push(label);
  38. }
  39. label._repositionAllGlyphs = true;
  40. }
  41. function getCSSValue(element, property) {
  42. return document.defaultView
  43. .getComputedStyle(element, null)
  44. .getPropertyValue(property);
  45. }
  46. function parseFont(label) {
  47. let fontInfo = fontInfoCache[label._font];
  48. if (!defined(fontInfo)) {
  49. const div = document.createElement("div");
  50. div.style.position = "absolute";
  51. div.style.opacity = 0;
  52. div.style.font = label._font;
  53. document.body.appendChild(div);
  54. let lineHeight = parseFloat(getCSSValue(div, "line-height"));
  55. if (isNaN(lineHeight)) {
  56. // line-height isn't a number, i.e. 'normal'; apply default line-spacing
  57. lineHeight = undefined;
  58. }
  59. fontInfo = {
  60. family: getCSSValue(div, "font-family"),
  61. size: getCSSValue(div, "font-size").replace("px", ""),
  62. style: getCSSValue(div, "font-style"),
  63. weight: getCSSValue(div, "font-weight"),
  64. lineHeight: lineHeight,
  65. };
  66. document.body.removeChild(div);
  67. if (fontInfoCacheLength < fontInfoCacheMaxSize) {
  68. fontInfoCache[label._font] = fontInfo;
  69. fontInfoCacheLength++;
  70. }
  71. }
  72. label._fontFamily = fontInfo.family;
  73. label._fontSize = fontInfo.size;
  74. label._fontStyle = fontInfo.style;
  75. label._fontWeight = fontInfo.weight;
  76. label._lineHeight = fontInfo.lineHeight;
  77. }
  78. /**
  79. * @typedef {object} Label.ConstructorOptions
  80. *
  81. * Initialization options for the Label constructor
  82. *
  83. * @property {Cartesian3} position The cartesian position of the label.
  84. * @property {*} [id] A user-defined object to return when the label is picked with {@link Scene#pick}.
  85. * @property {boolean} [show=true] Determines if this label will be shown.
  86. * @property {string} [text] A string specifying the text of the label.
  87. * @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.
  88. * @property {LabelStyle} [style=LabelStyle.FILL] A {@link LabelStyle} specifying the style of the label.
  89. * @property {number} [scale=1.0] A number specifying the uniform scale that is multiplied with the label size.
  90. * @property {boolean} [showBackground=false] Determines if a background behind this label will be shown.
  91. * @property {Color} [backgroundColor=new Color(0.165, 0.165, 0.165, 0.8)] A {@link Color} specifying the background color of the label.
  92. * @property {Cartesian2} [backgroundPadding=new Cartesian2(7, 5)] A {@link Cartesian2} Specifying the horizontal and vertical background padding in pixels.
  93. * @property {Cartesian2} [pixelOffset=Cartesian2.ZERO] A {@link Cartesian2} specifying the pixel offset in screen space from the origin of this label.
  94. * @property {Cartesian3} [eyeOffset=Cartesian3.ZERO] A {@link Cartesian3} specifying the 3D Cartesian offset applied to this label in eye coordinates.
  95. * @property {HorizontalOrigin} [horizontalOrigin=HorizontalOrigin.LEFT] A {@link HorizontalOrigin} specifying the horizontal origin of this label.
  96. * @property {VerticalOrigin} [verticalOrigin=VerticalOrigin.BASELINE] A {@link VerticalOrigin} specifying the vertical origin of this label.
  97. * @property {HeightReference} [heightReference=HeightReference.NONE] A {@link HeightReference} specifying the height reference of this label.
  98. * @property {Color} [fillColor=Color.WHITE] A {@link Color} specifying the fill color of the label.
  99. * @property {Color} [outlineColor=Color.BLACK] A {@link Color} specifying the outline color of the label.
  100. * @property {number} [outlineWidth=1.0] A number specifying the outline width of the label.
  101. * @property {NearFarScalar} [translucencyByDistance] A {@link NearFarScalar} specifying near and far translucency properties of the label based on the label's distance from the camera.
  102. * @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.
  103. * @property {NearFarScalar} [scaleByDistance] A {@link NearFarScalar} specifying near and far scaling properties of the label based on the label's distance from the camera.
  104. * @property {DistanceDisplayCondition} [distanceDisplayCondition] A {@link DistanceDisplayCondition} specifying at what distance from the camera that this label will be displayed.
  105. * @property {number} [disableDepthTestDistance] The distance from the camera, beyond which, depth testing is disabled—to, for example, prevent clipping against terrain.
  106. */
  107. /**
  108. * <div class="notice">
  109. * Create labels by calling {@link LabelCollection#add}. Do not call the constructor directly.
  110. * </div>
  111. *
  112. * @alias Label
  113. * @internalConstructor
  114. * @class
  115. *
  116. * @param {Label.ConstructorOptions} options Object describing initialization options
  117. * @param {LabelCollection} labelCollection Instance of LabelCollection
  118. *
  119. * @exception {DeveloperError} translucencyByDistance.far must be greater than translucencyByDistance.near
  120. * @exception {DeveloperError} pixelOffsetScaleByDistance.far must be greater than pixelOffsetScaleByDistance.near
  121. * @exception {DeveloperError} distanceDisplayCondition.far must be greater than distanceDisplayCondition.near
  122. *
  123. * @see LabelCollection
  124. * @see LabelCollection#add
  125. *
  126. * @demo {@link https://sandcastle.cesium.com/index.html?id=labels|Cesium Sandcastle Labels Demo}
  127. */
  128. function Label(options, labelCollection) {
  129. options = options ?? Frozen.EMPTY_OBJECT;
  130. //>>includeStart('debug', pragmas.debug);
  131. if (
  132. defined(options.disableDepthTestDistance) &&
  133. options.disableDepthTestDistance < 0.0
  134. ) {
  135. throw new DeveloperError(
  136. "disableDepthTestDistance must be greater than 0.0.",
  137. );
  138. }
  139. //>>includeEnd('debug');
  140. let translucencyByDistance = options.translucencyByDistance;
  141. let pixelOffsetScaleByDistance = options.pixelOffsetScaleByDistance;
  142. let scaleByDistance = options.scaleByDistance;
  143. let distanceDisplayCondition = options.distanceDisplayCondition;
  144. if (defined(translucencyByDistance)) {
  145. //>>includeStart('debug', pragmas.debug);
  146. if (translucencyByDistance.far <= translucencyByDistance.near) {
  147. throw new DeveloperError(
  148. "translucencyByDistance.far must be greater than translucencyByDistance.near.",
  149. );
  150. }
  151. //>>includeEnd('debug');
  152. translucencyByDistance = NearFarScalar.clone(translucencyByDistance);
  153. }
  154. if (defined(pixelOffsetScaleByDistance)) {
  155. //>>includeStart('debug', pragmas.debug);
  156. if (pixelOffsetScaleByDistance.far <= pixelOffsetScaleByDistance.near) {
  157. throw new DeveloperError(
  158. "pixelOffsetScaleByDistance.far must be greater than pixelOffsetScaleByDistance.near.",
  159. );
  160. }
  161. //>>includeEnd('debug');
  162. pixelOffsetScaleByDistance = NearFarScalar.clone(
  163. pixelOffsetScaleByDistance,
  164. );
  165. }
  166. if (defined(scaleByDistance)) {
  167. //>>includeStart('debug', pragmas.debug);
  168. if (scaleByDistance.far <= scaleByDistance.near) {
  169. throw new DeveloperError(
  170. "scaleByDistance.far must be greater than scaleByDistance.near.",
  171. );
  172. }
  173. //>>includeEnd('debug');
  174. scaleByDistance = NearFarScalar.clone(scaleByDistance);
  175. }
  176. if (defined(distanceDisplayCondition)) {
  177. //>>includeStart('debug', pragmas.debug);
  178. if (distanceDisplayCondition.far <= distanceDisplayCondition.near) {
  179. throw new DeveloperError(
  180. "distanceDisplayCondition.far must be greater than distanceDisplayCondition.near.",
  181. );
  182. }
  183. //>>includeEnd('debug');
  184. distanceDisplayCondition = DistanceDisplayCondition.clone(
  185. distanceDisplayCondition,
  186. );
  187. }
  188. this._renderedText = undefined;
  189. this._text = undefined;
  190. this._show = options.show ?? true;
  191. this._font = options.font ?? "30px sans-serif";
  192. this._fillColor = Color.clone(options.fillColor ?? Color.WHITE);
  193. this._outlineColor = Color.clone(options.outlineColor ?? Color.BLACK);
  194. this._outlineWidth = options.outlineWidth ?? 1.0;
  195. this._showBackground = options.showBackground ?? false;
  196. this._backgroundColor = Color.clone(
  197. options.backgroundColor ?? defaultBackgroundColor,
  198. );
  199. this._backgroundPadding = Cartesian2.clone(
  200. options.backgroundPadding ?? defaultBackgroundPadding,
  201. );
  202. this._style = options.style ?? LabelStyle.FILL;
  203. this._verticalOrigin = options.verticalOrigin ?? VerticalOrigin.BASELINE;
  204. this._horizontalOrigin = options.horizontalOrigin ?? HorizontalOrigin.LEFT;
  205. this._pixelOffset = Cartesian2.clone(options.pixelOffset ?? Cartesian2.ZERO);
  206. this._eyeOffset = Cartesian3.clone(options.eyeOffset ?? Cartesian3.ZERO);
  207. this._position = Cartesian3.clone(options.position ?? Cartesian3.ZERO);
  208. this._scale = options.scale ?? 1.0;
  209. this._id = options.id;
  210. this._translucencyByDistance = translucencyByDistance;
  211. this._pixelOffsetScaleByDistance = pixelOffsetScaleByDistance;
  212. this._scaleByDistance = scaleByDistance;
  213. this._heightReference = options.heightReference ?? HeightReference.NONE;
  214. this._distanceDisplayCondition = distanceDisplayCondition;
  215. this._disableDepthTestDistance = options.disableDepthTestDistance;
  216. this._labelCollection = labelCollection;
  217. this._glyphs = [];
  218. this._backgroundBillboard = undefined;
  219. this._batchIndex = undefined; // Used only by Vector3DTilePoints and BillboardCollection
  220. this._rebindAllGlyphs = true;
  221. this._repositionAllGlyphs = true;
  222. this._actualClampedPosition = undefined;
  223. this._removeCallbackFunc = undefined;
  224. this._mode = undefined;
  225. this._clusterShow = true;
  226. this.text = options.text ?? "";
  227. this._relativeSize = 1.0;
  228. parseFont(this);
  229. this._updateClamping();
  230. }
  231. Object.defineProperties(Label.prototype, {
  232. /**
  233. * Determines if this label will be shown. Use this to hide or show a label, instead
  234. * of removing it and re-adding it to the collection.
  235. * @memberof Label.prototype
  236. * @type {boolean}
  237. * @default true
  238. */
  239. show: {
  240. get: function () {
  241. return this._show;
  242. },
  243. set: function (value) {
  244. //>>includeStart('debug', pragmas.debug);
  245. if (!defined(value)) {
  246. throw new DeveloperError("value is required.");
  247. }
  248. //>>includeEnd('debug');
  249. if (this._show !== value) {
  250. this._show = value;
  251. const glyphs = this._glyphs;
  252. for (let i = 0, len = glyphs.length; i < len; i++) {
  253. const billboard = glyphs[i].billboard;
  254. if (defined(billboard)) {
  255. billboard.show = value;
  256. }
  257. }
  258. const backgroundBillboard = this._backgroundBillboard;
  259. if (defined(backgroundBillboard)) {
  260. backgroundBillboard.show = value;
  261. }
  262. }
  263. },
  264. },
  265. /**
  266. * Gets or sets the Cartesian position of this label.
  267. * @memberof Label.prototype
  268. * @type {Cartesian3}
  269. */
  270. position: {
  271. get: function () {
  272. return this._position;
  273. },
  274. set: function (value) {
  275. //>>includeStart('debug', pragmas.debug);
  276. if (!defined(value)) {
  277. throw new DeveloperError("value is required.");
  278. }
  279. //>>includeEnd('debug');
  280. const position = this._position;
  281. if (!Cartesian3.equals(position, value)) {
  282. Cartesian3.clone(value, position);
  283. const glyphs = this._glyphs;
  284. for (let i = 0, len = glyphs.length; i < len; i++) {
  285. const billboard = glyphs[i].billboard;
  286. if (defined(billboard)) {
  287. billboard.position = value;
  288. }
  289. }
  290. const backgroundBillboard = this._backgroundBillboard;
  291. if (defined(backgroundBillboard)) {
  292. backgroundBillboard.position = value;
  293. }
  294. this._updateClamping();
  295. }
  296. },
  297. },
  298. /**
  299. * Gets or sets the height reference of this billboard.
  300. * @memberof Label.prototype
  301. * @type {HeightReference}
  302. * @default HeightReference.NONE
  303. */
  304. heightReference: {
  305. get: function () {
  306. return this._heightReference;
  307. },
  308. set: function (value) {
  309. //>>includeStart('debug', pragmas.debug);
  310. if (!defined(value)) {
  311. throw new DeveloperError("value is required.");
  312. }
  313. //>>includeEnd('debug');
  314. if (value !== this._heightReference) {
  315. this._heightReference = value;
  316. const glyphs = this._glyphs;
  317. for (let i = 0, len = glyphs.length; i < len; i++) {
  318. const billboard = glyphs[i].billboard;
  319. if (defined(billboard)) {
  320. billboard.heightReference = value;
  321. }
  322. }
  323. const backgroundBillboard = this._backgroundBillboard;
  324. if (defined(backgroundBillboard)) {
  325. backgroundBillboard.heightReference = value;
  326. }
  327. repositionAllGlyphs(this);
  328. this._updateClamping();
  329. }
  330. },
  331. },
  332. /**
  333. * Gets or sets the text of this label.
  334. * @memberof Label.prototype
  335. * @type {string}
  336. */
  337. text: {
  338. get: function () {
  339. return this._text;
  340. },
  341. set: function (value) {
  342. //>>includeStart('debug', pragmas.debug);
  343. if (!defined(value)) {
  344. throw new DeveloperError("value is required.");
  345. }
  346. //>>includeEnd('debug');
  347. if (this._text !== value) {
  348. this._text = value;
  349. const renderedValue = Label.filterUnsupportedCharacters(value);
  350. this._renderedText = Label.enableRightToLeftDetection
  351. ? reverseRtl(renderedValue)
  352. : renderedValue;
  353. rebindAllGlyphs(this);
  354. }
  355. },
  356. },
  357. /**
  358. * Gets or sets the font used to draw this label. Fonts are specified using the same syntax as the CSS 'font' property.
  359. * @memberof Label.prototype
  360. * @type {string}
  361. * @default '30px sans-serif'
  362. * @see {@link http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#text-styles|HTML canvas 2D context text styles}
  363. */
  364. font: {
  365. get: function () {
  366. return this._font;
  367. },
  368. set: function (value) {
  369. //>>includeStart('debug', pragmas.debug);
  370. if (!defined(value)) {
  371. throw new DeveloperError("value is required.");
  372. }
  373. //>>includeEnd('debug');
  374. if (this._font !== value) {
  375. this._font = value;
  376. rebindAllGlyphs(this);
  377. parseFont(this);
  378. }
  379. },
  380. },
  381. /**
  382. * Gets or sets the fill color of this label.
  383. * @memberof Label.prototype
  384. * @type {Color}
  385. * @default Color.WHITE
  386. * @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}
  387. */
  388. fillColor: {
  389. get: function () {
  390. return this._fillColor;
  391. },
  392. set: function (value) {
  393. //>>includeStart('debug', pragmas.debug);
  394. if (!defined(value)) {
  395. throw new DeveloperError("value is required.");
  396. }
  397. //>>includeEnd('debug');
  398. const fillColor = this._fillColor;
  399. if (!Color.equals(fillColor, value)) {
  400. Color.clone(value, fillColor);
  401. rebindAllGlyphs(this);
  402. }
  403. },
  404. },
  405. /**
  406. * Gets or sets the outline color of this label.
  407. * @memberof Label.prototype
  408. * @type {Color}
  409. * @default Color.BLACK
  410. * @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}
  411. */
  412. outlineColor: {
  413. get: function () {
  414. return this._outlineColor;
  415. },
  416. set: function (value) {
  417. //>>includeStart('debug', pragmas.debug);
  418. if (!defined(value)) {
  419. throw new DeveloperError("value is required.");
  420. }
  421. //>>includeEnd('debug');
  422. const outlineColor = this._outlineColor;
  423. if (!Color.equals(outlineColor, value)) {
  424. Color.clone(value, outlineColor);
  425. rebindAllGlyphs(this);
  426. }
  427. },
  428. },
  429. /**
  430. * Gets or sets the outline width of this label.
  431. * @memberof Label.prototype
  432. * @type {number}
  433. * @default 1.0
  434. * @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}
  435. */
  436. outlineWidth: {
  437. get: function () {
  438. return this._outlineWidth;
  439. },
  440. set: function (value) {
  441. //>>includeStart('debug', pragmas.debug);
  442. if (!defined(value)) {
  443. throw new DeveloperError("value is required.");
  444. }
  445. //>>includeEnd('debug');
  446. if (this._outlineWidth !== value) {
  447. this._outlineWidth = value;
  448. rebindAllGlyphs(this);
  449. }
  450. },
  451. },
  452. /**
  453. * Determines if a background behind this label will be shown.
  454. * @memberof Label.prototype
  455. * @default false
  456. * @type {boolean}
  457. */
  458. showBackground: {
  459. get: function () {
  460. return this._showBackground;
  461. },
  462. set: function (value) {
  463. //>>includeStart('debug', pragmas.debug);
  464. if (!defined(value)) {
  465. throw new DeveloperError("value is required.");
  466. }
  467. //>>includeEnd('debug');
  468. if (this._showBackground !== value) {
  469. this._showBackground = value;
  470. rebindAllGlyphs(this);
  471. }
  472. },
  473. },
  474. /**
  475. * Gets or sets the background color of this label.
  476. * @memberof Label.prototype
  477. * @type {Color}
  478. * @default new Color(0.165, 0.165, 0.165, 0.8)
  479. */
  480. backgroundColor: {
  481. get: function () {
  482. return this._backgroundColor;
  483. },
  484. set: function (value) {
  485. //>>includeStart('debug', pragmas.debug);
  486. if (!defined(value)) {
  487. throw new DeveloperError("value is required.");
  488. }
  489. //>>includeEnd('debug');
  490. const backgroundColor = this._backgroundColor;
  491. if (!Color.equals(backgroundColor, value)) {
  492. Color.clone(value, backgroundColor);
  493. const backgroundBillboard = this._backgroundBillboard;
  494. if (defined(backgroundBillboard)) {
  495. backgroundBillboard.color = backgroundColor;
  496. }
  497. }
  498. },
  499. },
  500. /**
  501. * Gets or sets the background padding, in pixels, of this label. The <code>x</code> value
  502. * controls horizontal padding, and the <code>y</code> value controls vertical padding.
  503. * @memberof Label.prototype
  504. * @type {Cartesian2}
  505. * @default new Cartesian2(7, 5)
  506. */
  507. backgroundPadding: {
  508. get: function () {
  509. return this._backgroundPadding;
  510. },
  511. set: function (value) {
  512. //>>includeStart('debug', pragmas.debug);
  513. if (!defined(value)) {
  514. throw new DeveloperError("value is required.");
  515. }
  516. //>>includeEnd('debug');
  517. const backgroundPadding = this._backgroundPadding;
  518. if (!Cartesian2.equals(backgroundPadding, value)) {
  519. Cartesian2.clone(value, backgroundPadding);
  520. repositionAllGlyphs(this);
  521. }
  522. },
  523. },
  524. /**
  525. * Gets or sets the style of this label.
  526. * @memberof Label.prototype
  527. * @type {LabelStyle}
  528. * @default LabelStyle.FILL
  529. */
  530. style: {
  531. get: function () {
  532. return this._style;
  533. },
  534. set: function (value) {
  535. //>>includeStart('debug', pragmas.debug);
  536. if (!defined(value)) {
  537. throw new DeveloperError("value is required.");
  538. }
  539. //>>includeEnd('debug');
  540. if (this._style !== value) {
  541. this._style = value;
  542. rebindAllGlyphs(this);
  543. }
  544. },
  545. },
  546. /**
  547. * Gets or sets the pixel offset in screen space from the origin of this label. This is commonly used
  548. * to align multiple labels and billboards at the same position, e.g., an image and text. The
  549. * screen space origin is the top, left corner of the canvas; <code>x</code> increases from
  550. * left to right, and <code>y</code> increases from top to bottom.
  551. * <br /><br />
  552. * <div align='center'>
  553. * <table border='0' cellpadding='5'><tr>
  554. * <td align='center'><code>default</code><br/><img src='Images/Label.setPixelOffset.default.png' width='250' height='188' /></td>
  555. * <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>
  556. * </tr></table>
  557. * The label's origin is indicated by the yellow point.
  558. * </div>
  559. * @memberof Label.prototype
  560. * @type {Cartesian2}
  561. * @default Cartesian2.ZERO
  562. */
  563. pixelOffset: {
  564. get: function () {
  565. return this._pixelOffset;
  566. },
  567. set: function (value) {
  568. //>>includeStart('debug', pragmas.debug);
  569. if (!defined(value)) {
  570. throw new DeveloperError("value is required.");
  571. }
  572. //>>includeEnd('debug');
  573. const pixelOffset = this._pixelOffset;
  574. if (!Cartesian2.equals(pixelOffset, value)) {
  575. Cartesian2.clone(value, pixelOffset);
  576. const glyphs = this._glyphs;
  577. for (let i = 0, len = glyphs.length; i < len; i++) {
  578. const glyph = glyphs[i];
  579. if (defined(glyph.billboard)) {
  580. glyph.billboard.pixelOffset = value;
  581. }
  582. }
  583. const backgroundBillboard = this._backgroundBillboard;
  584. if (defined(backgroundBillboard)) {
  585. backgroundBillboard.pixelOffset = value;
  586. }
  587. }
  588. },
  589. },
  590. /**
  591. * Gets or sets near and far translucency properties of a Label based on the Label's distance from the camera.
  592. * A label's translucency will interpolate between the {@link NearFarScalar#nearValue} and
  593. * {@link NearFarScalar#farValue} while the camera distance falls within the lower and upper bounds
  594. * of the specified {@link NearFarScalar#near} and {@link NearFarScalar#far}.
  595. * Outside of these ranges the label's translucency remains clamped to the nearest bound. If undefined,
  596. * translucencyByDistance will be disabled.
  597. * @memberof Label.prototype
  598. * @type {NearFarScalar}
  599. *
  600. * @example
  601. * // Example 1.
  602. * // Set a label's translucencyByDistance to 1.0 when the
  603. * // camera is 1500 meters from the label and disappear as
  604. * // the camera distance approaches 8.0e6 meters.
  605. * text.translucencyByDistance = new Cesium.NearFarScalar(1.5e2, 1.0, 8.0e6, 0.0);
  606. *
  607. * @example
  608. * // Example 2.
  609. * // disable translucency by distance
  610. * text.translucencyByDistance = undefined;
  611. */
  612. translucencyByDistance: {
  613. get: function () {
  614. return this._translucencyByDistance;
  615. },
  616. set: function (value) {
  617. //>>includeStart('debug', pragmas.debug);
  618. if (defined(value) && value.far <= value.near) {
  619. throw new DeveloperError(
  620. "far distance must be greater than near distance.",
  621. );
  622. }
  623. //>>includeEnd('debug');
  624. const translucencyByDistance = this._translucencyByDistance;
  625. if (!NearFarScalar.equals(translucencyByDistance, value)) {
  626. this._translucencyByDistance = NearFarScalar.clone(
  627. value,
  628. translucencyByDistance,
  629. );
  630. const glyphs = this._glyphs;
  631. for (let i = 0, len = glyphs.length; i < len; i++) {
  632. const glyph = glyphs[i];
  633. if (defined(glyph.billboard)) {
  634. glyph.billboard.translucencyByDistance = value;
  635. }
  636. }
  637. const backgroundBillboard = this._backgroundBillboard;
  638. if (defined(backgroundBillboard)) {
  639. backgroundBillboard.translucencyByDistance = value;
  640. }
  641. }
  642. },
  643. },
  644. /**
  645. * Gets or sets near and far pixel offset scaling properties of a Label based on the Label's distance from the camera.
  646. * A label's pixel offset will be scaled between the {@link NearFarScalar#nearValue} and
  647. * {@link NearFarScalar#farValue} while the camera distance falls within the lower and upper bounds
  648. * of the specified {@link NearFarScalar#near} and {@link NearFarScalar#far}.
  649. * Outside of these ranges the label's pixel offset scaling remains clamped to the nearest bound. If undefined,
  650. * pixelOffsetScaleByDistance will be disabled.
  651. * @memberof Label.prototype
  652. * @type {NearFarScalar}
  653. *
  654. * @example
  655. * // Example 1.
  656. * // Set a label's pixel offset scale to 0.0 when the
  657. * // camera is 1500 meters from the label and scale pixel offset to 10.0 pixels
  658. * // in the y direction the camera distance approaches 8.0e6 meters.
  659. * text.pixelOffset = new Cesium.Cartesian2(0.0, 1.0);
  660. * text.pixelOffsetScaleByDistance = new Cesium.NearFarScalar(1.5e2, 0.0, 8.0e6, 10.0);
  661. *
  662. * @example
  663. * // Example 2.
  664. * // disable pixel offset by distance
  665. * text.pixelOffsetScaleByDistance = undefined;
  666. */
  667. pixelOffsetScaleByDistance: {
  668. get: function () {
  669. return this._pixelOffsetScaleByDistance;
  670. },
  671. set: function (value) {
  672. //>>includeStart('debug', pragmas.debug);
  673. if (defined(value) && value.far <= value.near) {
  674. throw new DeveloperError(
  675. "far distance must be greater than near distance.",
  676. );
  677. }
  678. //>>includeEnd('debug');
  679. const pixelOffsetScaleByDistance = this._pixelOffsetScaleByDistance;
  680. if (!NearFarScalar.equals(pixelOffsetScaleByDistance, value)) {
  681. this._pixelOffsetScaleByDistance = NearFarScalar.clone(
  682. value,
  683. pixelOffsetScaleByDistance,
  684. );
  685. const glyphs = this._glyphs;
  686. for (let i = 0, len = glyphs.length; i < len; i++) {
  687. const glyph = glyphs[i];
  688. if (defined(glyph.billboard)) {
  689. glyph.billboard.pixelOffsetScaleByDistance = value;
  690. }
  691. }
  692. const backgroundBillboard = this._backgroundBillboard;
  693. if (defined(backgroundBillboard)) {
  694. backgroundBillboard.pixelOffsetScaleByDistance = value;
  695. }
  696. }
  697. },
  698. },
  699. /**
  700. * Gets or sets near and far scaling properties of a Label based on the label's distance from the camera.
  701. * A label's scale will interpolate between the {@link NearFarScalar#nearValue} and
  702. * {@link NearFarScalar#farValue} while the camera distance falls within the lower and upper bounds
  703. * of the specified {@link NearFarScalar#near} and {@link NearFarScalar#far}.
  704. * Outside of these ranges the label's scale remains clamped to the nearest bound. If undefined,
  705. * scaleByDistance will be disabled.
  706. * @memberof Label.prototype
  707. * @type {NearFarScalar}
  708. *
  709. * @example
  710. * // Example 1.
  711. * // Set a label's scaleByDistance to scale by 1.5 when the
  712. * // camera is 1500 meters from the label and disappear as
  713. * // the camera distance approaches 8.0e6 meters.
  714. * label.scaleByDistance = new Cesium.NearFarScalar(1.5e2, 1.5, 8.0e6, 0.0);
  715. *
  716. * @example
  717. * // Example 2.
  718. * // disable scaling by distance
  719. * label.scaleByDistance = undefined;
  720. */
  721. scaleByDistance: {
  722. get: function () {
  723. return this._scaleByDistance;
  724. },
  725. set: function (value) {
  726. //>>includeStart('debug', pragmas.debug);
  727. if (defined(value) && value.far <= value.near) {
  728. throw new DeveloperError(
  729. "far distance must be greater than near distance.",
  730. );
  731. }
  732. //>>includeEnd('debug');
  733. const scaleByDistance = this._scaleByDistance;
  734. if (!NearFarScalar.equals(scaleByDistance, value)) {
  735. this._scaleByDistance = NearFarScalar.clone(value, scaleByDistance);
  736. const glyphs = this._glyphs;
  737. for (let i = 0, len = glyphs.length; i < len; i++) {
  738. const glyph = glyphs[i];
  739. if (defined(glyph.billboard)) {
  740. glyph.billboard.scaleByDistance = value;
  741. }
  742. }
  743. const backgroundBillboard = this._backgroundBillboard;
  744. if (defined(backgroundBillboard)) {
  745. backgroundBillboard.scaleByDistance = value;
  746. }
  747. }
  748. },
  749. },
  750. /**
  751. * Gets and sets the 3D Cartesian offset applied to this label in eye coordinates. Eye coordinates is a left-handed
  752. * coordinate system, where <code>x</code> points towards the viewer's right, <code>y</code> points up, and
  753. * <code>z</code> points into the screen. Eye coordinates use the same scale as world and model coordinates,
  754. * which is typically meters.
  755. * <br /><br />
  756. * An eye offset is commonly used to arrange multiple label or objects at the same position, e.g., to
  757. * arrange a label above its corresponding 3D model.
  758. * <br /><br />
  759. * Below, the label is positioned at the center of the Earth but an eye offset makes it always
  760. * appear on top of the Earth regardless of the viewer's or Earth's orientation.
  761. * <br /><br />
  762. * <div align='center'>
  763. * <table border='0' cellpadding='5'><tr>
  764. * <td align='center'><img src='Images/Billboard.setEyeOffset.one.png' width='250' height='188' /></td>
  765. * <td align='center'><img src='Images/Billboard.setEyeOffset.two.png' width='250' height='188' /></td>
  766. * </tr></table>
  767. * <code>l.eyeOffset = new Cartesian3(0.0, 8000000.0, 0.0);</code><br /><br />
  768. * </div>
  769. * @memberof Label.prototype
  770. * @type {Cartesian3}
  771. * @default Cartesian3.ZERO
  772. */
  773. eyeOffset: {
  774. get: function () {
  775. return this._eyeOffset;
  776. },
  777. set: function (value) {
  778. //>>includeStart('debug', pragmas.debug);
  779. if (!defined(value)) {
  780. throw new DeveloperError("value is required.");
  781. }
  782. //>>includeEnd('debug');
  783. const eyeOffset = this._eyeOffset;
  784. if (!Cartesian3.equals(eyeOffset, value)) {
  785. Cartesian3.clone(value, eyeOffset);
  786. const glyphs = this._glyphs;
  787. for (let i = 0, len = glyphs.length; i < len; i++) {
  788. const glyph = glyphs[i];
  789. if (defined(glyph.billboard)) {
  790. glyph.billboard.eyeOffset = value;
  791. }
  792. }
  793. const backgroundBillboard = this._backgroundBillboard;
  794. if (defined(backgroundBillboard)) {
  795. backgroundBillboard.eyeOffset = value;
  796. }
  797. }
  798. },
  799. },
  800. /**
  801. * Gets or sets the horizontal origin of this label, which determines if the label is drawn
  802. * to the left, center, or right of its anchor position.
  803. * <br /><br />
  804. * <div align='center'>
  805. * <img src='Images/Billboard.setHorizontalOrigin.png' width='648' height='196' /><br />
  806. * </div>
  807. * @memberof Label.prototype
  808. * @type {HorizontalOrigin}
  809. * @default HorizontalOrigin.LEFT
  810. * @example
  811. * // Use a top, right origin
  812. * l.horizontalOrigin = Cesium.HorizontalOrigin.RIGHT;
  813. * l.verticalOrigin = Cesium.VerticalOrigin.TOP;
  814. */
  815. horizontalOrigin: {
  816. get: function () {
  817. return this._horizontalOrigin;
  818. },
  819. set: function (value) {
  820. //>>includeStart('debug', pragmas.debug);
  821. if (!defined(value)) {
  822. throw new DeveloperError("value is required.");
  823. }
  824. //>>includeEnd('debug');
  825. if (this._horizontalOrigin !== value) {
  826. this._horizontalOrigin = value;
  827. repositionAllGlyphs(this);
  828. }
  829. },
  830. },
  831. /**
  832. * Gets or sets the vertical origin of this label, which determines if the label is
  833. * to the above, below, or at the center of its anchor position.
  834. * <br /><br />
  835. * <div align='center'>
  836. * <img src='Images/Billboard.setVerticalOrigin.png' width='695' height='175' /><br />
  837. * </div>
  838. * @memberof Label.prototype
  839. * @type {VerticalOrigin}
  840. * @default VerticalOrigin.BASELINE
  841. * @example
  842. * // Use a top, right origin
  843. * l.horizontalOrigin = Cesium.HorizontalOrigin.RIGHT;
  844. * l.verticalOrigin = Cesium.VerticalOrigin.TOP;
  845. */
  846. verticalOrigin: {
  847. get: function () {
  848. return this._verticalOrigin;
  849. },
  850. set: function (value) {
  851. //>>includeStart('debug', pragmas.debug);
  852. if (!defined(value)) {
  853. throw new DeveloperError("value is required.");
  854. }
  855. //>>includeEnd('debug');
  856. if (this._verticalOrigin !== value) {
  857. this._verticalOrigin = value;
  858. const glyphs = this._glyphs;
  859. for (let i = 0, len = glyphs.length; i < len; i++) {
  860. const glyph = glyphs[i];
  861. if (defined(glyph.billboard)) {
  862. glyph.billboard.verticalOrigin = value;
  863. }
  864. }
  865. const backgroundBillboard = this._backgroundBillboard;
  866. if (defined(backgroundBillboard)) {
  867. backgroundBillboard.verticalOrigin = value;
  868. }
  869. repositionAllGlyphs(this);
  870. }
  871. },
  872. },
  873. /**
  874. * Gets or sets the uniform scale that is multiplied with the label's size in pixels.
  875. * A scale of <code>1.0</code> does not change the size of the label; a scale greater than
  876. * <code>1.0</code> enlarges the label; a positive scale less than <code>1.0</code> shrinks
  877. * the label.
  878. * <br /><br />
  879. * Applying a large scale value may pixelate the label. To make text larger without pixelation,
  880. * use a larger font size when calling {@link Label#font} instead.
  881. * <br /><br />
  882. * <div align='center'>
  883. * <img src='Images/Label.setScale.png' width='400' height='300' /><br/>
  884. * From left to right in the above image, the scales are <code>0.5</code>, <code>1.0</code>,
  885. * and <code>2.0</code>.
  886. * </div>
  887. * @memberof Label.prototype
  888. * @type {number}
  889. * @default 1.0
  890. */
  891. scale: {
  892. get: function () {
  893. return this._scale;
  894. },
  895. set: function (value) {
  896. //>>includeStart('debug', pragmas.debug);
  897. if (!defined(value)) {
  898. throw new DeveloperError("value is required.");
  899. }
  900. //>>includeEnd('debug');
  901. if (this._scale !== value) {
  902. this._scale = value;
  903. const glyphs = this._glyphs;
  904. for (let i = 0, len = glyphs.length; i < len; i++) {
  905. const glyph = glyphs[i];
  906. if (defined(glyph.billboard)) {
  907. glyph.billboard.scale = value * this._relativeSize;
  908. }
  909. }
  910. const backgroundBillboard = this._backgroundBillboard;
  911. if (defined(backgroundBillboard)) {
  912. backgroundBillboard.scale = value * this._relativeSize;
  913. }
  914. repositionAllGlyphs(this);
  915. }
  916. },
  917. },
  918. /**
  919. * Gets the total scale of the label, which is the label's scale multiplied by the computed relative size
  920. * of the desired font compared to the generated glyph size.
  921. * @memberof Label.prototype
  922. * @type {number}
  923. * @default 1.0
  924. */
  925. totalScale: {
  926. get: function () {
  927. return this._scale * this._relativeSize;
  928. },
  929. },
  930. /**
  931. * Gets or sets the condition specifying at what distance from the camera that this label will be displayed.
  932. * @memberof Label.prototype
  933. * @type {DistanceDisplayCondition}
  934. * @default undefined
  935. */
  936. distanceDisplayCondition: {
  937. get: function () {
  938. return this._distanceDisplayCondition;
  939. },
  940. set: function (value) {
  941. //>>includeStart('debug', pragmas.debug);
  942. if (defined(value) && value.far <= value.near) {
  943. throw new DeveloperError("far must be greater than near");
  944. }
  945. //>>includeEnd('debug');
  946. if (
  947. !DistanceDisplayCondition.equals(value, this._distanceDisplayCondition)
  948. ) {
  949. this._distanceDisplayCondition = DistanceDisplayCondition.clone(
  950. value,
  951. this._distanceDisplayCondition,
  952. );
  953. const glyphs = this._glyphs;
  954. for (let i = 0, len = glyphs.length; i < len; i++) {
  955. const glyph = glyphs[i];
  956. if (defined(glyph.billboard)) {
  957. glyph.billboard.distanceDisplayCondition = value;
  958. }
  959. }
  960. const backgroundBillboard = this._backgroundBillboard;
  961. if (defined(backgroundBillboard)) {
  962. backgroundBillboard.distanceDisplayCondition = value;
  963. }
  964. }
  965. },
  966. },
  967. /**
  968. * Gets or sets the distance from the camera, beyond which, depth testing is disabled—to, for example, prevent clipping against terrain.
  969. * When set to <code>undefined</code> or
  970. * <code>0</code>, the depth test is always applied. When set to Number.<code>POSITIVE_INFINITY</code>, the depth test is never applied.
  971. * @memberof Label.prototype
  972. * @type {number|undefined}
  973. * @default undefined
  974. */
  975. disableDepthTestDistance: {
  976. get: function () {
  977. return this._disableDepthTestDistance;
  978. },
  979. set: function (value) {
  980. if (this._disableDepthTestDistance !== value) {
  981. //>>includeStart('debug', pragmas.debug);
  982. if (defined(value) && value < 0.0) {
  983. throw new DeveloperError(
  984. "disableDepthTestDistance must be greater than 0.0.",
  985. );
  986. }
  987. //>>includeEnd('debug');
  988. this._disableDepthTestDistance = value;
  989. const glyphs = this._glyphs;
  990. for (let i = 0, len = glyphs.length; i < len; i++) {
  991. const glyph = glyphs[i];
  992. if (defined(glyph.billboard)) {
  993. glyph.billboard.disableDepthTestDistance = value;
  994. }
  995. }
  996. const backgroundBillboard = this._backgroundBillboard;
  997. if (defined(backgroundBillboard)) {
  998. backgroundBillboard.disableDepthTestDistance = value;
  999. }
  1000. }
  1001. },
  1002. },
  1003. /**
  1004. * Gets or sets the user-defined value returned when the label is picked.
  1005. * @memberof Label.prototype
  1006. * @type {*}
  1007. */
  1008. id: {
  1009. get: function () {
  1010. return this._id;
  1011. },
  1012. set: function (value) {
  1013. if (this._id !== value) {
  1014. this._id = value;
  1015. const glyphs = this._glyphs;
  1016. for (let i = 0, len = glyphs.length; i < len; i++) {
  1017. const glyph = glyphs[i];
  1018. if (defined(glyph.billboard)) {
  1019. glyph.billboard.id = value;
  1020. }
  1021. }
  1022. const backgroundBillboard = this._backgroundBillboard;
  1023. if (defined(backgroundBillboard)) {
  1024. backgroundBillboard.id = value;
  1025. }
  1026. }
  1027. },
  1028. },
  1029. /**
  1030. * @private
  1031. */
  1032. pickId: {
  1033. get: function () {
  1034. if (this._glyphs.length === 0 || !defined(this._glyphs[0].billboard)) {
  1035. return undefined;
  1036. }
  1037. return this._glyphs[0].billboard.pickId;
  1038. },
  1039. },
  1040. /**
  1041. * Keeps track of the position of the label based on the height reference.
  1042. * @memberof Label.prototype
  1043. * @type {Cartesian3}
  1044. * @private
  1045. */
  1046. _clampedPosition: {
  1047. get: function () {
  1048. return this._actualClampedPosition;
  1049. },
  1050. set: function (value) {
  1051. this._actualClampedPosition = Cartesian3.clone(
  1052. value,
  1053. this._actualClampedPosition,
  1054. );
  1055. const glyphs = this._glyphs;
  1056. for (let i = 0, len = glyphs.length; i < len; i++) {
  1057. const glyph = glyphs[i];
  1058. if (defined(glyph.billboard)) {
  1059. // Set all the private values here, because we already clamped to ground
  1060. // so we don't want to do it again for every glyph
  1061. glyph.billboard._clampedPosition = value;
  1062. }
  1063. }
  1064. const backgroundBillboard = this._backgroundBillboard;
  1065. if (defined(backgroundBillboard)) {
  1066. backgroundBillboard._clampedPosition = value;
  1067. }
  1068. },
  1069. },
  1070. /**
  1071. * Determines whether or not this label will be shown or hidden because it was clustered.
  1072. * @memberof Label.prototype
  1073. * @type {boolean}
  1074. * @default true
  1075. * @private
  1076. */
  1077. clusterShow: {
  1078. get: function () {
  1079. return this._clusterShow;
  1080. },
  1081. set: function (value) {
  1082. if (this._clusterShow !== value) {
  1083. this._clusterShow = value;
  1084. const glyphs = this._glyphs;
  1085. for (let i = 0, len = glyphs.length; i < len; i++) {
  1086. const glyph = glyphs[i];
  1087. if (defined(glyph.billboard)) {
  1088. glyph.billboard.clusterShow = value;
  1089. }
  1090. }
  1091. const backgroundBillboard = this._backgroundBillboard;
  1092. if (defined(backgroundBillboard)) {
  1093. backgroundBillboard.clusterShow = value;
  1094. }
  1095. }
  1096. },
  1097. },
  1098. /**
  1099. * 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.
  1100. * @memberof Label.prototype
  1101. * @type {boolean}
  1102. * @readonly
  1103. * @private
  1104. */
  1105. ready: {
  1106. get: function () {
  1107. if (this._rebindAllGlyphs || this._repositionAllGlyphs) {
  1108. return false;
  1109. }
  1110. if (
  1111. defined(this._backgroundBillboard) &&
  1112. !this._backgroundBillboard.ready
  1113. ) {
  1114. return false;
  1115. }
  1116. const glyphs = this._glyphs;
  1117. for (let i = 0, len = glyphs.length; i < len; i++) {
  1118. const glyph = glyphs[i];
  1119. if (defined(glyph.billboard) && !glyph.billboard.ready) {
  1120. return false;
  1121. }
  1122. }
  1123. return true;
  1124. },
  1125. },
  1126. });
  1127. Label.prototype._updateClamping = function () {
  1128. Billboard._updateClamping(this._labelCollection, this);
  1129. };
  1130. /**
  1131. * Computes the screen-space position of the label's origin, taking into account eye and pixel offsets.
  1132. * The screen space origin is the top, left corner of the canvas; <code>x</code> increases from
  1133. * left to right, and <code>y</code> increases from top to bottom.
  1134. *
  1135. * @param {Scene} scene The scene the label is in.
  1136. * @param {Cartesian2} [result] The object onto which to store the result.
  1137. * @returns {Cartesian2} The screen-space position of the label.
  1138. *
  1139. *
  1140. * @example
  1141. * console.log(l.computeScreenSpacePosition(scene).toString());
  1142. *
  1143. * @see Label#eyeOffset
  1144. * @see Label#pixelOffset
  1145. */
  1146. Label.prototype.computeScreenSpacePosition = function (scene, result) {
  1147. //>>includeStart('debug', pragmas.debug);
  1148. if (!defined(scene)) {
  1149. throw new DeveloperError("scene is required.");
  1150. }
  1151. //>>includeEnd('debug');
  1152. if (!defined(result)) {
  1153. result = new Cartesian2();
  1154. }
  1155. const labelCollection = this._labelCollection;
  1156. const modelMatrix = labelCollection.modelMatrix;
  1157. const actualPosition = defined(this._actualClampedPosition)
  1158. ? this._actualClampedPosition
  1159. : this._position;
  1160. const windowCoordinates = Billboard._computeScreenSpacePosition(
  1161. modelMatrix,
  1162. actualPosition,
  1163. this._eyeOffset,
  1164. this._pixelOffset,
  1165. scene,
  1166. result,
  1167. );
  1168. return windowCoordinates;
  1169. };
  1170. /**
  1171. * Gets a label's screen space bounding box centered around screenSpacePosition.
  1172. * @param {Label} label The label to get the screen space bounding box for.
  1173. * @param {Cartesian2} screenSpacePosition The screen space center of the label.
  1174. * @param {BoundingRectangle} [result] The object onto which to store the result.
  1175. * @returns {BoundingRectangle} The screen space bounding box.
  1176. *
  1177. * @private
  1178. */
  1179. Label.getScreenSpaceBoundingBox = function (
  1180. label,
  1181. screenSpacePosition,
  1182. result,
  1183. ) {
  1184. let x = 0;
  1185. let y = 0;
  1186. let width = 0;
  1187. let height = 0;
  1188. const scale = label.totalScale;
  1189. const backgroundBillboard = label._backgroundBillboard;
  1190. if (defined(backgroundBillboard)) {
  1191. x = screenSpacePosition.x + backgroundBillboard._translate.x;
  1192. y = screenSpacePosition.y - backgroundBillboard._translate.y;
  1193. width = backgroundBillboard.width * scale;
  1194. height = backgroundBillboard.height * scale;
  1195. if (
  1196. label.verticalOrigin === VerticalOrigin.BOTTOM ||
  1197. label.verticalOrigin === VerticalOrigin.BASELINE
  1198. ) {
  1199. y -= height;
  1200. } else if (label.verticalOrigin === VerticalOrigin.CENTER) {
  1201. y -= height * 0.5;
  1202. }
  1203. } else {
  1204. x = Number.POSITIVE_INFINITY;
  1205. y = Number.POSITIVE_INFINITY;
  1206. let maxX = 0;
  1207. let maxY = 0;
  1208. const glyphs = label._glyphs;
  1209. const length = glyphs.length;
  1210. for (let i = 0; i < length; ++i) {
  1211. const glyph = glyphs[i];
  1212. const billboard = glyph.billboard;
  1213. if (!defined(billboard)) {
  1214. continue;
  1215. }
  1216. const glyphX = screenSpacePosition.x + billboard._translate.x;
  1217. let glyphY = screenSpacePosition.y - billboard._translate.y;
  1218. const glyphWidth = glyph.dimensions.width * scale;
  1219. const glyphHeight = glyph.dimensions.height * scale;
  1220. if (
  1221. label.verticalOrigin === VerticalOrigin.BOTTOM ||
  1222. label.verticalOrigin === VerticalOrigin.BASELINE
  1223. ) {
  1224. glyphY -= glyphHeight;
  1225. } else if (label.verticalOrigin === VerticalOrigin.CENTER) {
  1226. glyphY -= glyphHeight * 0.5;
  1227. }
  1228. if (label._verticalOrigin === VerticalOrigin.TOP) {
  1229. glyphY += SDFSettings.PADDING * scale;
  1230. } else if (
  1231. label._verticalOrigin === VerticalOrigin.BOTTOM ||
  1232. label._verticalOrigin === VerticalOrigin.BASELINE
  1233. ) {
  1234. glyphY -= SDFSettings.PADDING * scale;
  1235. }
  1236. x = Math.min(x, glyphX);
  1237. y = Math.min(y, glyphY);
  1238. maxX = Math.max(maxX, glyphX + glyphWidth);
  1239. maxY = Math.max(maxY, glyphY + glyphHeight);
  1240. }
  1241. width = maxX - x;
  1242. height = maxY - y;
  1243. }
  1244. if (!defined(result)) {
  1245. result = new BoundingRectangle();
  1246. }
  1247. result.x = x;
  1248. result.y = y;
  1249. result.width = width;
  1250. result.height = height;
  1251. return result;
  1252. };
  1253. /**
  1254. * 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.
  1255. * @private
  1256. * @param {string} text The original label text
  1257. * @returns {string} The renderable filtered text
  1258. */
  1259. Label.filterUnsupportedCharacters = function (text) {
  1260. const problematicCharactersRegex = new RegExp(
  1261. // eslint-disable-next-line no-control-regex
  1262. /[\u0000-\u0008\u000E-\u001F\u00ad\u202a-\u206f\u200b-\u200f]/,
  1263. "g",
  1264. );
  1265. return text.replace(problematicCharactersRegex, "");
  1266. };
  1267. /**
  1268. * Determines if this label equals another label. Labels are equal if all their properties
  1269. * are equal. Labels in different collections can be equal.
  1270. *
  1271. * @param {Label} [other] The label to compare for equality.
  1272. * @returns {boolean} <code>true</code> if the labels are equal; otherwise, <code>false</code>.
  1273. */
  1274. Label.prototype.equals = function (other) {
  1275. return (
  1276. this === other ||
  1277. (defined(other) &&
  1278. this._show === other._show &&
  1279. this._scale === other._scale &&
  1280. this._outlineWidth === other._outlineWidth &&
  1281. this._showBackground === other._showBackground &&
  1282. this._style === other._style &&
  1283. this._verticalOrigin === other._verticalOrigin &&
  1284. this._horizontalOrigin === other._horizontalOrigin &&
  1285. this._heightReference === other._heightReference &&
  1286. this._renderedText === other._renderedText &&
  1287. this._font === other._font &&
  1288. Cartesian3.equals(this._position, other._position) &&
  1289. Color.equals(this._fillColor, other._fillColor) &&
  1290. Color.equals(this._outlineColor, other._outlineColor) &&
  1291. Color.equals(this._backgroundColor, other._backgroundColor) &&
  1292. Cartesian2.equals(this._backgroundPadding, other._backgroundPadding) &&
  1293. Cartesian2.equals(this._pixelOffset, other._pixelOffset) &&
  1294. Cartesian3.equals(this._eyeOffset, other._eyeOffset) &&
  1295. NearFarScalar.equals(
  1296. this._translucencyByDistance,
  1297. other._translucencyByDistance,
  1298. ) &&
  1299. NearFarScalar.equals(
  1300. this._pixelOffsetScaleByDistance,
  1301. other._pixelOffsetScaleByDistance,
  1302. ) &&
  1303. NearFarScalar.equals(this._scaleByDistance, other._scaleByDistance) &&
  1304. DistanceDisplayCondition.equals(
  1305. this._distanceDisplayCondition,
  1306. other._distanceDisplayCondition,
  1307. ) &&
  1308. this._disableDepthTestDistance === other._disableDepthTestDistance &&
  1309. this._id === other._id)
  1310. );
  1311. };
  1312. /**
  1313. * Returns true if this object was destroyed; otherwise, false.
  1314. * <br /><br />
  1315. * If this object was destroyed, it should not be used; calling any function other than
  1316. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  1317. *
  1318. * @returns {boolean} True if this object was destroyed; otherwise, false.
  1319. */
  1320. Label.prototype.isDestroyed = function () {
  1321. return false;
  1322. };
  1323. /**
  1324. * Determines whether or not run the algorithm, that match the text of the label to right-to-left languages
  1325. * @memberof Label
  1326. * @type {boolean}
  1327. * @default false
  1328. *
  1329. * @example
  1330. * // Example 1.
  1331. * // Set a label's rightToLeft before init
  1332. * Cesium.Label.enableRightToLeftDetection = true;
  1333. * const myLabelEntity = viewer.entities.add({
  1334. * label: {
  1335. * id: 'my label',
  1336. * text: 'זה טקסט בעברית \n ועכשיו יורדים שורה',
  1337. * }
  1338. * });
  1339. *
  1340. * @example
  1341. * // Example 2.
  1342. * const myLabelEntity = viewer.entities.add({
  1343. * label: {
  1344. * id: 'my label',
  1345. * text: 'English text'
  1346. * }
  1347. * });
  1348. * // Set a label's rightToLeft after init
  1349. * Cesium.Label.enableRightToLeftDetection = true;
  1350. * myLabelEntity.text = 'טקסט חדש';
  1351. */
  1352. Label.enableRightToLeftDetection = false;
  1353. function convertTextToTypes(text, rtlChars) {
  1354. const ltrChars = /[a-zA-Z0-9]/;
  1355. const bracketsChars = /[()[\]{}<>]/;
  1356. const parsedText = [];
  1357. let word = "";
  1358. let lastType = textTypes.LTR;
  1359. let currentType = "";
  1360. const textLength = text.length;
  1361. for (let textIndex = 0; textIndex < textLength; ++textIndex) {
  1362. const character = text.charAt(textIndex);
  1363. if (rtlChars.test(character)) {
  1364. currentType = textTypes.RTL;
  1365. } else if (ltrChars.test(character)) {
  1366. currentType = textTypes.LTR;
  1367. } else if (bracketsChars.test(character)) {
  1368. currentType = textTypes.BRACKETS;
  1369. } else {
  1370. currentType = textTypes.WEAK;
  1371. }
  1372. if (textIndex === 0) {
  1373. lastType = currentType;
  1374. }
  1375. if (lastType === currentType && currentType !== textTypes.BRACKETS) {
  1376. word += character;
  1377. } else {
  1378. if (word !== "") {
  1379. parsedText.push({ Type: lastType, Word: word });
  1380. }
  1381. lastType = currentType;
  1382. word = character;
  1383. }
  1384. }
  1385. parsedText.push({ Type: currentType, Word: word });
  1386. return parsedText;
  1387. }
  1388. function reverseWord(word) {
  1389. return word.split("").reverse().join("");
  1390. }
  1391. function spliceWord(result, pointer, word) {
  1392. return result.slice(0, pointer) + word + result.slice(pointer);
  1393. }
  1394. function reverseBrackets(bracket) {
  1395. switch (bracket) {
  1396. case "(":
  1397. return ")";
  1398. case ")":
  1399. return "(";
  1400. case "[":
  1401. return "]";
  1402. case "]":
  1403. return "[";
  1404. case "{":
  1405. return "}";
  1406. case "}":
  1407. return "{";
  1408. case "<":
  1409. return ">";
  1410. case ">":
  1411. return "<";
  1412. }
  1413. }
  1414. // To add another language, add its Unicode block range(s) to the below regex.
  1415. const hebrew = "\u05D0-\u05EA";
  1416. const arabic = "\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF";
  1417. const rtlChars = new RegExp(`[${hebrew}${arabic}]`);
  1418. /**
  1419. *
  1420. * @param {string} value the text to parse and reorder
  1421. * @returns {string} the text as rightToLeft direction
  1422. * @private
  1423. */
  1424. function reverseRtl(value) {
  1425. const texts = value.split("\n");
  1426. let result = "";
  1427. for (let i = 0; i < texts.length; i++) {
  1428. const text = texts[i];
  1429. // first character of the line is a RTL character, so need to manage different cases
  1430. const rtlDir = rtlChars.test(text.charAt(0));
  1431. const parsedText = convertTextToTypes(text, rtlChars);
  1432. let splicePointer = 0;
  1433. let line = "";
  1434. for (let wordIndex = 0; wordIndex < parsedText.length; ++wordIndex) {
  1435. const subText = parsedText[wordIndex];
  1436. const reverse =
  1437. subText.Type === textTypes.BRACKETS
  1438. ? reverseBrackets(subText.Word)
  1439. : reverseWord(subText.Word);
  1440. if (rtlDir) {
  1441. if (subText.Type === textTypes.RTL) {
  1442. line = reverse + line;
  1443. splicePointer = 0;
  1444. } else if (subText.Type === textTypes.LTR) {
  1445. line = spliceWord(line, splicePointer, subText.Word);
  1446. splicePointer += subText.Word.length;
  1447. } else if (
  1448. subText.Type === textTypes.WEAK ||
  1449. subText.Type === textTypes.BRACKETS
  1450. ) {
  1451. // current word is weak, last one was bracket
  1452. if (
  1453. subText.Type === textTypes.WEAK &&
  1454. parsedText[wordIndex - 1].Type === textTypes.BRACKETS
  1455. ) {
  1456. line = reverse + line;
  1457. }
  1458. // current word is weak or bracket, last one was rtl
  1459. else if (parsedText[wordIndex - 1].Type === textTypes.RTL) {
  1460. line = reverse + line;
  1461. splicePointer = 0;
  1462. }
  1463. // current word is weak or bracket, there is at least one more word
  1464. else if (parsedText.length > wordIndex + 1) {
  1465. // next word is rtl
  1466. if (parsedText[wordIndex + 1].Type === textTypes.RTL) {
  1467. line = reverse + line;
  1468. splicePointer = 0;
  1469. } else {
  1470. line = spliceWord(line, splicePointer, subText.Word);
  1471. splicePointer += subText.Word.length;
  1472. }
  1473. }
  1474. // current word is weak or bracket, and it the last in this line
  1475. else {
  1476. line = spliceWord(line, 0, reverse);
  1477. }
  1478. }
  1479. }
  1480. // ltr line, rtl word
  1481. else if (subText.Type === textTypes.RTL) {
  1482. line = spliceWord(line, splicePointer, reverse);
  1483. }
  1484. // ltr line, ltr word
  1485. else if (subText.Type === textTypes.LTR) {
  1486. line += subText.Word;
  1487. splicePointer = line.length;
  1488. }
  1489. // ltr line, weak or bracket word
  1490. else if (
  1491. subText.Type === textTypes.WEAK ||
  1492. subText.Type === textTypes.BRACKETS
  1493. ) {
  1494. // not first word in line
  1495. if (wordIndex > 0) {
  1496. // last word was rtl
  1497. if (parsedText[wordIndex - 1].Type === textTypes.RTL) {
  1498. // there is at least one more word
  1499. if (parsedText.length > wordIndex + 1) {
  1500. // next word is rtl
  1501. if (parsedText[wordIndex + 1].Type === textTypes.RTL) {
  1502. line = spliceWord(line, splicePointer, reverse);
  1503. } else {
  1504. line += subText.Word;
  1505. splicePointer = line.length;
  1506. }
  1507. } else {
  1508. line += subText.Word;
  1509. }
  1510. } else {
  1511. line += subText.Word;
  1512. splicePointer = line.length;
  1513. }
  1514. } else {
  1515. line += subText.Word;
  1516. splicePointer = line.length;
  1517. }
  1518. }
  1519. }
  1520. result += line;
  1521. if (i < texts.length - 1) {
  1522. result += "\n";
  1523. }
  1524. }
  1525. return result;
  1526. }
  1527. export default Label;