const epsilon = 0.01; export enum UnitType { Default = 0, Pixel = 1, Point, Pica, Inch, Mm, Cm } export const UnitTypeLabel = { [UnitType.Pixel] : "像素", [UnitType.Inch] : "英寸", [UnitType.Mm] : "毫米", [UnitType.Cm] : "厘米" }; export class Unit { static Empty:Unit; static Zero:Unit; private static _dotsPerInch = 0; private static _defaultType = UnitType.Default; private _value: number; private _type: UnitType; constructor(value:number, type?:UnitType) { if (! type) { this._value = UnitHelper.convertToUnits(value, Unit.defaultType); this._type = Unit.defaultType; } else { this._value = value; this._type = type; } } static from(value:string | number):Unit { if (!value) { return Unit.pixel(0); } if (typeof value === 'number') { return Unit.pixel(value); } const str:string = value as any; let index = str.indexOf('px'); if (index > 0) { return Unit.pixel(parseInt(str.substr(0, index))); } index = str.indexOf('cm'); if (index > 0) { return Unit.cm(parseInt(str.substr(0, index))); } index = str.indexOf('mm'); if (index > 0) { return Unit.mm(parseInt(str.substr(0, index))); } index = str.indexOf('in'); if (index > 0) { return Unit.inch(parseInt(str.substr(0, index))); } return Unit.pixel(parseInt(str)); } static get defaultType(): UnitType { if (Unit._defaultType === UnitType.Default) { Unit._defaultType = UnitType.Cm; } return Unit._defaultType; } static set defaultType(value:UnitType) { Unit._defaultType = value; } static get dotsPerInch():number { if (0 === Unit._dotsPerInch) { // TODO: 计算出适合的 DPI Unit._dotsPerInch = 96; } return Unit._dotsPerInch; } static pixel(value: number): Unit { return new Unit(value, UnitType.Pixel); } static point(value: number): Unit { return new Unit(value, UnitType.Point); } static inch(value: number): Unit { return new Unit(value, UnitType.Inch); } static pica(value: number): Unit { return new Unit(value, UnitType.Pica); } static cm(value: number): Unit { return new Unit(value, UnitType.Cm); } static mm(value: number): Unit { return new Unit(value, UnitType.Mm); } static add(unit1:Unit, unit2:Unit):Unit { if (unit1.isEmpty) { unit1 = Unit.Zero; } if (unit2.isEmpty) { unit2 = Unit.Zero; } const targetType = unit1.type; const value = unit1.value + unit2.changeType(targetType).value; return new Unit(value, targetType); } static subtract(unit1:Unit, unit2:Unit):Unit { if (unit1.isEmpty) { unit1 = Unit.Zero; } if (unit2.isEmpty) { unit2 = Unit.Zero; } const targetType = unit1.type; const value = unit1.value - unit2.changeType(targetType).value; return new Unit(value, targetType); } static fromPixels(pixels:number, type:UnitType):Unit { return new Unit(UnitHelper.convertToUnits(pixels, type), type); } changeType(type:UnitType|any):Unit { if (this.isEmpty) { return this; } if (this.type === type) { return this; } if (type === UnitType.Pixel) { return Unit.pixel(UnitHelper.convertToPixels(this.value, this.type)); } if (this.type === UnitType.Pixel) { return new Unit(UnitHelper.convertToUnits(this.value, type), type); } switch (type) { case UnitType.Pixel: return Unit.pixel(this.toPixel()); case UnitType.Point: return Unit.point(this.toPoint()); case UnitType.Pica: return Unit.pica(this.toPica()); case UnitType.Inch: return Unit.inch(this.toInch()); case UnitType.Mm: return Unit.mm(this.toMm()); case UnitType.Cm: return Unit.cm(this.toCm()); default: throw new Error('argument out of range'); } } toPica():number { switch (this.type) { case UnitType.Pixel: return this.value * 0.0625; case UnitType.Point: return this.value * 0.083333333; case UnitType.Pica: return this.value; case UnitType.Inch: return this.value * 6; case UnitType.Mm: return this.value * 0.236220472; case UnitType.Cm: return this.value * 2.362204724; default: throw new Error('argument out of range'); } } toCm():number { switch (this.type) { case UnitType.Pixel: return this.value * 0.026458333; case UnitType.Point: return this.value * 0.035277778; case UnitType.Pica: return this.value * 0.423333333; case UnitType.Inch: return this.value * 2.54; case UnitType.Mm: return this.value * 0.1; case UnitType.Cm: return this.value; default: throw new Error('argument out of range'); } } toInch():number { switch (this.type) { case UnitType.Pixel: return this.value * 0.010416667; case UnitType.Point: return this.value * 0.013888889; case UnitType.Pica: return this.value * 0.166666667; case UnitType.Inch: return this.value; case UnitType.Mm: return this.value * 0.039370079; case UnitType.Cm: return this.value * 0.393700787; default: throw new Error('argument out of range'); } } toPoint():number { switch (this.type) { case UnitType.Pixel: return this.value * 0.75; case UnitType.Point: return this.value; case UnitType.Pica: return this.value * 12; case UnitType.Inch: return this.value * 72; case UnitType.Mm: return this.value * 2.834645669; case UnitType.Cm: return this.value * 28.346456693; default: throw new Error('argument out of range'); } } toPixel():number { switch (this.type) { case UnitType.Pixel: return this.value; case UnitType.Point: return this.value * 1.333333333; case UnitType.Pica: return this.value * 16; case UnitType.Inch: return this.value * 96; case UnitType.Mm: return this.value * 3.779527559; case UnitType.Cm: return this.value * 37.795275591; default: throw new Error('argument out of range'); } } toMm():number { if (this.isEmpty) { return 0; } switch (this.type) { case UnitType.Pixel: return (this.value * 0.264583333); case UnitType.Point: return (this.value * 0.352777778); case UnitType.Pica: return (this.value * 4.233333333); case UnitType.Inch: return this.value * 25.4; case UnitType.Mm: return this.value; case UnitType.Cm: return this.value * 10.0; default: throw new Error('argument out of range'); } } add(unit:Unit):Unit { return Unit.add(this, unit); } subtract(unit:Unit):Unit { return Unit.subtract(this, unit); } multiply(value:number):Unit { return new Unit(this.value * value, this.type); } divide(value:number):Unit { return new Unit(this.value / value, this.type); } get isEmpty():boolean { return this._type === UnitType.Default; } get value():number { return this._value; } get type():UnitType { return this._type; } get pixels():number { return Math.ceil(this.toPixel()); } get fPixels():number { return this.toPixel(); } clone():Unit { return new Unit(this._value, this._type); } toString():string { if (this.isEmpty) { return ''; } if (this.type == UnitType.Pixel) { return `${this.value.toFixed(0)} ${UnitHelper.getStringFromType(this.type)}`; } else { const formatValue = this.value === 0 ? '0' : this.value.toFixed(1); return `${formatValue} ${UnitHelper.getStringFromType(this.type)}`; } } } const pxInCm = 37.795276; const pxInMm = 3.7795276; export class UnitHelper { static getStringFromType(type:UnitType):string { switch (type) { case UnitType.Mm: return "毫米"; case UnitType.Cm: return "厘米"; case UnitType.Inch: return "英寸"; case UnitType.Pixel: return "像素"; case UnitType.Point: return "pt"; case UnitType.Pica: return "pc"; } return ''; } static convertToUnits(pixels:number, type:UnitType):number { const value = pixels; switch (type) { case UnitType.Pixel: { return value; } case UnitType.Point: { return (value / (Unit.dotsPerInch / 72)); } case UnitType.Pica: { return (value / ((Unit.dotsPerInch / 96) * 16)); } case UnitType.Inch: { return (value / Unit.dotsPerInch); } case UnitType.Mm: { return (value / ((Unit.dotsPerInch / 96) * pxInMm)); } case UnitType.Cm: { return (value / ((Unit.dotsPerInch / 96) * pxInCm)); } } return value; } static convertToPixels(value:number, type:UnitType):number { switch (type) { case UnitType.Pixel: { return value; } case UnitType.Point: { value *= (Unit.dotsPerInch / 72); return value; } case UnitType.Pica: { value *= ((Unit.dotsPerInch / 96) * 16); return value; } case UnitType.Inch: { value *= Unit.dotsPerInch; return value; } case UnitType.Mm: { value *= ((Unit.dotsPerInch / 96) * pxInMm); return value; } case UnitType.Cm: { value *= ((Unit.dotsPerInch / 96) * pxInCm); return value; } } return value; } } Unit.Empty = new Unit(0, UnitType.Pixel); Unit.Zero = new Unit(0, UnitType.Pixel); export class PointF { private _x: number; private _y: number; static Empty: PointF; constructor(x?:number, y?:number) { this._x = x; this._y = y; } get isEmpty():boolean { return this._x === 0 && this._y === 0; } get x():number { return this._x; } set x(value:number) { this._x = value; } get y():number { return this._y; } set y(value:number) { this._y = value; } clone():PointF { return new PointF(this.x, this.y); } equals(pt:PointF):boolean { return this._x === pt.x && this._y === pt.y; } add(offset:SizeF):PointF { return new PointF(this.x + offset.width, this.y + offset.height); } } PointF.Empty = new PointF(0, 0); export class SizeF { private _width: number; private _height: number; static Empty:SizeF; constructor(width = 0, height = 0) { this._width = width; this._height = height; } get width():number { return this._width; } set width(value:number) { this._width = value; } get height():number { return this._height; } set height(value:number) { this._height = value; } get isEmpty():boolean { return this['_isEmpty']; } } SizeF.Empty = new SizeF(); SizeF.Empty['_isEmpty'] = true; export class RectF { private _x: number = 0; private _y: number = 0; private _width: number = 0; private _height: number = 0; private _isEmpty: boolean; static Empty:RectF; constructor(x?:number, y?:number, width?:number, height?:number) { this._x = x || 0; this._y = y || 0; this._width = width || 0; this._height = height || 0; } clone(): RectF { return new RectF(this._x, this._y, this._width, this._height); } equals(rect:RectF): boolean { return this._x === rect.x && this._y === rect.y && this._width === rect.width && this.height === rect.height; } get center():PointF { return new PointF(this._x + this.width / 2.0, this._y + this.height / 2.0); } get isEmpty():boolean { return this['_isEmpty']; } get location():PointF { return new PointF(this._x, this._y); } get left():number { return this._x; } get top():number { return this._y; } get x():number { return this._x; } set x(value:number) { this._x = value; } get y():number { return this._y; } set y(value:number) { this._y = value; } get width():number { return this._width; } set width(value:number) { this._width = value; } get height():number { return this._height; } set height(value:number) { this._height = value; } get right():number { return this._x + this._width; } get bottom():number { return this._y + this._height; } set size(size:SizeF) { this._width = size.width; this._height = size.height; } contains(pt:PointF):boolean { return pt.x >= this._x && pt.y >= this._y && pt.x < this.right && pt.y < this.bottom; } offset(offset:PointF) { this._x += offset.x; this._y += offset.y; } } RectF.Empty = new RectF(); RectF.Empty['_isEmpty'] = true; export class PointUnit { private _x: Unit; private _y: Unit; constructor(x?:Unit, y?:Unit) { this._x = x; this._y = y; } get x():Unit { return this._x; } set x(value:Unit) { this._x = value; } get y():Unit { return this._y; } set y(value:Unit) { this._y = value; } } export class SizeUnit { private _width: Unit; private _height: Unit; static Empty(): SizeUnit { return new SizeUnit(Unit.Empty, Unit.Empty); } constructor(width?:Unit, height?:Unit) { this._width = width; this._height = height; } get width():Unit { return this._width; } set width(value:Unit) { this._width = value; } get height():Unit { return this._height; } set height(value:Unit) { this._height = value; } } export class RectUnit { private _x: Unit; private _y: Unit; private _width: Unit; private _height: Unit; constructor(x:Unit , y:Unit, width:Unit, height:Unit) { this._x = x; this._y = y; this._width = width; this._height = height; } static fromLocationAndSize(location: PointUnit, size:SizeUnit):RectUnit { return new RectUnit(location.x, location.y, size.width, size.height); } get location():PointUnit { return new PointUnit(this.x, this.y); } set location(value:PointUnit) { this.x = value.x; this.y = value.y; } get size():SizeUnit { return new SizeUnit(this.width, this.height); } set size(value:SizeUnit) { this.width = value.width; this.height = value.height; } get left():Unit { return this.x; } get top():Unit { return this.y; } get x():Unit { return this._x; } set x(value:Unit) { this._x = value; } get y():Unit { return this._y; } set y(value:Unit) { this._y = value; } get width():Unit { return this._width; } set width(value:Unit) { this._width = value; } get height():Unit { return this._height; } set height(value:Unit) { this._height = value; } } export class UnitMargins { left = Unit.Zero; right = Unit.Zero; top = Unit.Zero; bottom = Unit.Zero; constructor(left?:Unit, right?:Unit, top?:Unit, bottom?:Unit) { this.left = left || Unit.Zero.clone(); this.right = right || Unit.Zero.clone(); this.top = top || Unit.Zero.clone(); this.bottom = bottom || Unit.Zero.clone(); } }