Label.js 59 KB

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