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