UNPKG

@obliczeniowo/elementary

Version:
377 lines (369 loc) 17.2 kB
import { Point2D, ColorType } from '@obliczeniowo/elementary/classes'; import { Size } from '@obliczeniowo/elementary/resize-window'; import { Renderer2 } from '@angular/core'; declare enum TextAlign { LEFT = "left", CENTER = "center", RIGHT = "right", JUSTIFY = "justify" } declare enum TextBaseline { ALPHABETIC = "alphabetic", IDEOGRAPHIC = "ideographic", BOTTOM = "bottom", TOP = "top", MIDDLE = "middle", HANGING = "hanging" } declare enum LinePattern { NONE = 0, DOTTED = 1, DASHED = 2, DASH_DOT = 3, DISABLED = 4 } interface LinePatternDef { strokeMiterlimit: number; strokeDasharray: number[]; strokeDashoffset: number; } interface Drawable { draw: (ctx: DrawingContextInterface) => void; } /** * Abstract class to easy cooperate and switch drawing context logic */ declare abstract class DrawingContextInterface { protected lastPoint: Point2D; protected arrowScale: number; /** optional data that can be set after call draw method */ lastSettings: { text?: Size; }; /** * Some object required clear option before redraw (DrawingSvgInterface for example) */ abstract clear(): void; abstract getTextDimension(text: string): { width: number; height: number; }; /** * Draw line * @param startPoint start point * @param endPoint end point * @param stroke stroke width * @param color stroke color as string in format #ffffff or Color object * @param options specific for object under the hood object */ abstract drawLine(startPoint: Point2D, endPoint: Point2D, stroke: number, color: ColorType, options?: any): DrawingContextInterface; /** * Draw text * @param text text to draw * @param handlePosition handle position * @param color color as string in format #ffffff or Color object * @param angle rotate angle in radians * @param options specific options for object under the hood */ abstract drawText(text: string, handlePosition: Point2D, color: ColorType, angle: number, options?: any): DrawingContextInterface; /** * Draw polyline using table of points * @param points table of points * @param stroke stroke width * @param color stroke color as string in format #ffffff or Color object * @param options specific for object under hood */ abstract drawPolyline(points: Point2D[], stroke: number, color: ColorType, options?: { close?: boolean; [key: string]: any; }): DrawingContextInterface; abstract drawPolygon(points: Point2D[], stroke: number, color: ColorType, fill: ColorType, options?: any): DrawingContextInterface; /** * Draw circle * @param point center point * @param ray ray * @param stroke stroke width * @param strokeColor stroke color as string in format #ffffff or Color object * @param fillColor fill color as string in format #ffffff or Color object * @param options options specific for object under the hood */ abstract drawCircle(point: Point2D, ray: number, stroke: number, strokeColor: ColorType, fillColor: ColorType, options?: any): DrawingContextInterface; /** * Draw pie * @param center center * @param rx rx * @param ry ry (some object could not draw this correctly when rx !== ry) * @param start start angle in radians * @param end end angle in radians * @param stroke stroke width * @param strokeColor stroke color as string in format #ffffff or Color object * @param fillColor fill color as string in format #ffffff or Color object * @param options options specific for object under the hood */ abstract drawPie(center: Point2D, rx: number, ry: number, start: number, end: number, stroke: number, strokeColor: ColorType, fillColor: ColorType, options?: any): DrawingContextInterface; /** * Draw ellipse * @param point center * @param xRay x ray * @param yRay y ray * @param stroke stroke width * @param strokeColor stroke color as string in format #ffffff or as Color object * @param fillColor fill color as string in format #ffffff or as Color object * @param options external options specific for different object under the hood */ abstract drawEllipse(point: Point2D, xRay: number, yRay: number, stroke: number, strokeColor: ColorType, fillColor: ColorType, options?: any): DrawingContextInterface; /** * Draw rectangle * @param x coordinate * @param y coordinate * @param width rect width * @param height rect height * @param stroke stroke width * @param strokeColor stroke color * @param fillColor fill color * @param options extra options (specific for object under the hood) */ abstract drawRect(x: number, y: number, width: number, height: number, stroke: number, strokeColor: ColorType, fillColor: ColorType, options?: any): DrawingContextInterface; abstract lineTo(point: Point2D, stroke: number, color: ColorType): DrawingContextInterface; /** * Set font size * @param fontSize font size */ abstract setFontSize(fontSize: number): DrawingContextInterface; /** */ abstract getFontSize(): number; /** * Set text align flag * @param align text align enum */ abstract setTextAlign(align: TextAlign): DrawingContextInterface; /** * Set text baseline * @param textBaseline text baseline enum */ abstract setTextBaseline(textBaseline: TextBaseline): DrawingContextInterface; /** * Return text width of given text */ abstract getTextWidth(text: string): number; group(options?: any): this; endGroup(): this; /** * Set line pattern * @param linePattern LinePattern enum or string or undefined to set default patterns * @param linePatternDef to set own pattern definition */ abstract setLinePattern(linePattern: LinePattern | string | undefined, linePatternDef?: LinePatternDef): DrawingContextInterface; moveTo(point: Point2D): this; drawArrow(startPoint: Point2D, endPoint: Point2D, stroke: number, color: ColorType, drawArrow?: boolean, options?: any): this; abstract getLineStrokeSize(size?: number): number; abstract save(fileName: string): void; /** * Draw all kind of object that implements Drawable interface * @param drawable table of object that implements Drawable interface */ draw(drawable: Drawable[]): this; } declare class DrawingCanvasInterface extends DrawingContextInterface { ctx: CanvasRenderingContext2D; protected _stroke: number; get stroke(): number; set stroke(value: number); constructor(ctx: CanvasRenderingContext2D); getTextDimension(text: string): { width: number; height: number; }; clear(): void; drawLine(startPoint: Point2D, endPoint: Point2D, stroke: number, color: ColorType, options?: any): this; drawText(text: string, handlePosition: Point2D, color: ColorType, angle: number, options?: any): this; drawPolyline(points: Point2D[], stroke: number, color: ColorType, options?: { close?: boolean; [key: string]: any; }): this; drawPolygon(points: Point2D[], stroke: number, color: ColorType | 'none', fill: ColorType | 'none', options?: { close?: boolean; [key: string]: any; }): this; drawCircle(point: Point2D, ray: number, stroke: number, strokeColor: ColorType, fillColor: ColorType, options?: any): this; drawPie(center: Point2D, rx: number, ry: number, start: number, end: number, stroke: number, strokeColor: ColorType, fillColor: ColorType, options?: any): this; drawEllipse(point: Point2D, xRay: number, yRay: number, stroke: number, strokeColor: ColorType, fillColor: ColorType, options?: any): this; drawRect(x: number, y: number, width: number, height: number, stroke: number, strokeColor: ColorType, fillColor: ColorType, options?: any): this; lineTo(point: Point2D, stroke: number, color: ColorType): this; setFontSize(fontSize: number): this; getFontSize(): number; setTextAlign(align: TextAlign): this; setTextBaseline(textBaseline: TextBaseline): this; getTextWidth(text: string): number; setLinePattern(linePattern: string | LinePattern | undefined, linePatternDef?: LinePatternDef): this; getLineStrokeSize(size?: number): number; transform(point: Point2D): Point2D; save(fileName: string): void; protected path(cb: () => void, fillOrStroke?: boolean): void; } interface SvgOnly { classes?: string[]; events?: { [name: string]: (event: any) => void; }; attributes?: { [name: string]: any; }; } interface SvgOptions { svgOnly?: SvgOnly; [key: string]: any; } interface SvgPolylineOptions extends SvgOptions { close?: boolean; } declare class DrawingSvgInterface extends DrawingContextInterface { getTextDimension(text: string): Size; static archPath(center: Point2D, rx: number, ry: number, start: number, end: number, reverse?: boolean): string | undefined; private readonly svg; private readonly root?; private xMinimum; private xMaximum; private yMinimum; private yMaximum; private textAnchor; private linePatternName; private defs; private readonly linesDefs; private readonly groups; private readonly renderer; fontSize: number; constructor(svg: SVGSVGElement, renderer: Renderer2); private setXMin; private setYMin; private setXMax; private setYMax; private setMinMax; protected setSvgOptions(element: SVGElement, options?: any): void; protected createSvgPoint(point2d: Point2D): DOMPoint; protected setSvgElementLinePattern(element: SVGElement): void; lineTo(point: Point2D, stroke: number, color: ColorType): this; setFontSize(fontSize: number): this; getFontSize(): number; setTextAlign(align: TextAlign): this; setTextBaseline(textBaseline: TextBaseline): this; getTextWidth(text: string): number; clear(): this; setLinePattern(linePattern: string | LinePattern | undefined, linePatternDef?: LinePatternDef): this; drawLine(startPoint: Point2D, endPoint: Point2D, stroke?: number, color?: ColorType, options?: SvgOptions): this; drawCircle(point: Point2D, ray: number, stroke?: number, strokeColor?: ColorType, fillColor?: ColorType, options?: SvgOptions): this; drawPie(center: Point2D, rx: number, ry: number, start: number, end: number, stroke: number, strokeColor?: ColorType, fillColor?: ColorType, options?: SvgOptions): this; drawArch(center: Point2D, rx: number, ry: number, start: number, end: number, stroke: number, strokeColor?: ColorType, options?: SvgOptions): this; drawEllipse(point: Point2D, xRay: number, yRay: number, stroke: number, strokeColor: ColorType, fillColor: ColorType, options?: SvgOptions): this; drawRect(x: number, y: number, width: number, height: number, stroke: number, strokeColor: ColorType, fillColor: ColorType, options?: SvgOptions): this; get svgContainer(): SVGGElement; drawText(text: string, handlePosition: Point2D, color?: ColorType, angle?: number, options?: SvgOptions): this; drawPolyline(points: Point2D[], stroke?: number, color?: ColorType, options?: SvgPolylineOptions): this; drawPolygon(points: Point2D[], stroke: number, color: ColorType | 'none', fill: ColorType | 'none', options?: any): this; get width(): number; get height(): number; getSvg(): string; group(options?: { name?: string; data?: { [key: string]: string; }; clipRect?: { pos: Point2D; width: number; height: number; }; svgOnly?: SvgOnly; onMouseOver?: (g: SVGElement) => void; onMouseOut?: () => void; }): this; endGroup(): this; getLineStrokeSize(size?: number): number; save(fileName: string): void; protected setClasses(element: SVGElement, options?: SvgOptions): boolean; protected color(color: ColorType): string; } interface ModuleDef { value: string; added: boolean; } declare class DrawingOpenScadInterface extends DrawingContextInterface { protected openScad: string; protected textAlign: TextAlign; protected textBaseline: TextBaseline; protected fontSize: number; protected groups: boolean[]; protected modules: { [module: string]: ModuleDef; }; clear(): void; getTextDimension(text: string): { width: number; height: number; }; drawLine(startPoint: Point2D, endPoint: Point2D, stroke: number, color: ColorType, options?: any): DrawingContextInterface; setColor(color: ColorType): DrawingContextInterface; drawText(text: string, handlePosition: Point2D, color: ColorType, angle: number, options?: any): DrawingContextInterface; drawPolyline(points: Point2D[], stroke: number, color: ColorType, options?: { close?: boolean; [key: string]: any; }): DrawingContextInterface; drawPolygon(points: Point2D[], stroke: number, color: ColorType, fill: ColorType, options?: any): DrawingContextInterface; drawCircle(point: Point2D, ray: number, stroke: number, strokeColor: ColorType, fillColor: ColorType, options?: any): DrawingContextInterface; drawPie(center: Point2D, rx: number, ry: number, start: number, end: number, stroke: number, strokeColor: ColorType, fillColor: ColorType, options?: any): DrawingContextInterface; drawEllipse(point: Point2D, xRay: number, yRay: number, stroke: number, strokeColor: ColorType, fillColor: ColorType, options?: any): DrawingContextInterface; drawRect(x: number, y: number, width: number, height: number, stroke: number, strokeColor: ColorType, fillColor: ColorType, options?: any): DrawingContextInterface; lineTo(point: Point2D, stroke: number, color: ColorType): DrawingContextInterface; setFontSize(fontSize: number): DrawingContextInterface; getFontSize(): number; setTextAlign(align: TextAlign): DrawingContextInterface; setTextBaseline(textBaseline: TextBaseline): DrawingContextInterface; getTextWidth(text: string): number; setLinePattern(linePattern: LinePattern | string | undefined, linePatternDef?: LinePatternDef): DrawingContextInterface; getLineStrokeSize(size?: number): number; save(fileName: string): this; } declare class DrawingZwCadCommandInterface extends DrawingContextInterface { protected data: string; protected textAlign: TextAlign; protected fontSize: number; clear(): void; getTextDimension(text: string): { width: number; height: number; }; drawLine(startPoint: Point2D, endPoint: Point2D, stroke: number, color: ColorType, options?: any): DrawingContextInterface; drawText(text: string, handlePosition: Point2D, color: ColorType, angle: number, options?: any): DrawingContextInterface; drawPolyline(points: Point2D[], stroke: number, color: ColorType, options?: { close?: boolean; [key: string]: any; }): DrawingContextInterface; drawPolygon(points: Point2D[], stroke: number, color: ColorType, fill: ColorType, options?: any): DrawingContextInterface; drawCircle(point: Point2D, ray: number, stroke: number, strokeColor: ColorType, fillColor: ColorType, options?: any): DrawingContextInterface; drawPie(center: Point2D, rx: number, ry: number, start: number, end: number, stroke: number, strokeColor: ColorType, fillColor: ColorType, options?: any): DrawingContextInterface; drawEllipse(point: Point2D, xRay: number, yRay: number, stroke: number, strokeColor: ColorType, fillColor: ColorType, options?: any): DrawingContextInterface; drawRect(x: number, y: number, width: number, height: number, stroke: number, strokeColor: ColorType, fillColor: ColorType, options?: any): DrawingContextInterface; lineTo(point: Point2D, stroke: number, color: ColorType): DrawingContextInterface; setFontSize(fontSize: number): DrawingContextInterface; getFontSize(): number; setTextAlign(align: TextAlign): DrawingContextInterface; setTextBaseline(textBaseline: TextBaseline): DrawingContextInterface; getTextWidth(text: string): number; setLinePattern(linePattern: LinePattern | string | undefined, linePatternDef?: LinePatternDef): DrawingContextInterface; getLineStrokeSize(size?: number): number; save(fileName: string): this; } declare class Rect { x: number; y: number; width: number; height: number; constructor(firstPoint: Point2D, secondPoint: Point2D, dx?: number, dy?: number); isPtInRect(point: Point2D): boolean; add(rect: Rect): Rect; addOffset(left: number, right: number, top: number, bottom: number): void; fillRect(ctx: CanvasRenderingContext2D): void; strokeRect(ctx: CanvasRenderingContext2D): void; } export { DrawingCanvasInterface, DrawingContextInterface, DrawingOpenScadInterface, DrawingSvgInterface, DrawingZwCadCommandInterface, LinePattern, Rect, TextAlign, TextBaseline }; export type { Drawable, LinePatternDef };