UNPKG

thematic-earth

Version:

HTML-based, whole-Earth thematic maps using locally hosted data layers

2 lines 2.58 kB
/* Copyright (c) 2023 Read Write Tools. Legal use subject to the Thematic Earth Software License Agreement. */ import LabelPoint from'../labels/label-point.class.js';import*as EuclideanGeometry from'../spherical-earth/euclidean-geometry.js';import aver from'../dev/aver.js';import expect from'../dev/expect.js';import terminal from'../dev/terminal.js';export default class LabelSlot{constructor(t){expect(t,'Number'),this.y=t,this.xIntersect=[],this.margin=6,this.deltaLeft=-1,this.deltaCenter=-1,this.deltaRight=-1,Object.seal(this)}isComplete(){return 2==this.xIntersect.length&&!(this.xIntersect[0]>this.xIntersect[1])}get xLeft(){return aver(this.isComplete()),this.xIntersect[0]}get xRight(){return aver(this.isComplete()),this.xIntersect[1]}get width(){return this.xRight-this.xLeft}get xCenter(){return Math.round((this.xRight+this.xLeft)/2)}getDelta(t){return'left'==t?this.deltaLeft:'center'==t?this.deltaCenter:'right'==t?this.deltaRight:void terminal.logic(`unexpected alignment ${t}`)}hasIntersections(){return this.xIntersect.length>0}addIntersection(t,e,i){expect(t,'Number'),expect(e,'Number'),expect(i,'Number');var r=Math.round((t+e)/2);r=Math.min(r,i),r=Math.max(r,0),this.xIntersect.push(r)}sortIntersections(){this.xIntersect.sort(((t,e)=>t-e))}discardShorterSegments(){if(0!=this.xIntersect.length)if(this.xIntersect.length%2==0){for(;this.xIntersect.length>2;){this.xIntersect[1]-this.xIntersect[0]<this.xIntersect[3]-this.xIntersect[2]?this.xIntersect.splice(0,2):this.xIntersect.splice(2,2)}aver(2==this.xIntersect.length)}else this.xIntersect=[]}getFullSlotRect(t){return expect(t,'Number'),aver(this.isComplete()),{x:this.xIntersect[0],y:Math.round(this.y-t/2),width:this.width,height:t}}getLabelPosition(t,e,i){expect(t,'Number'),expect(e,'Number'),expect(i,'String'),aver(this.isComplete());var r=Math.round(this.y-e/2);switch(i){case'left':var s=Math.round(this.xLeft+this.margin);return new LabelPoint(s,r);case'center':s=Math.round(this.xCenter-t/2);return new LabelPoint(s,r);case'right':s=Math.round(this.xRight-t-this.margin);return new LabelPoint(s,r);default:return terminal.logic(`unexpected textAlignment ${i}`),new LabelPoint(0,0)}}deltaFromBoundingBox(t,e,i){expect(t,'BoundingBox'),expect(e,'Number'),expect(i,'Number');var r=t.centerX,s=t.centerY,n=Math.round(this.y-i/2),h=.5*e,a=this.xLeft+this.margin+h;this.deltaLeft=EuclideanGeometry.pointToPointDistance(r,s,a,n),a=this.xCenter,this.deltaCenter=EuclideanGeometry.pointToPointDistance(r,s,a,n),a=this.xRight-this.margin-h,this.deltaRight=EuclideanGeometry.pointToPointDistance(r,s,a,n)}}