import { Injectable } from '@nestjs/common'; import type { DrawDataDto, DrawDto, DrawShapeOptionenDto } from '../../_dto/draw.dto'; import { DrawEllipse } from '../lib/drawEllipse'; import { DrawGroup } from '../lib/drawGroup'; import { DrawHeader } from '../lib/drawHeader'; import type { GraphicGenerator } from './graphic-generator'; import type { PunktConfig, PunktOptions } from './types'; import { parsePunktRequest } from '../../_dto/punkt.dto'; @Injectable() export class DrawPunkt implements GraphicGenerator { private create(config: PunktConfig, fill: string) { const size = 3 * config.raster * config.scale; const strokeWidth = Math.min(1, size / 4); const radius = (size - strokeWidth) / 2; const circle = new DrawEllipse({ cx: size / 2, cy: size / 2, rx: radius, ry: radius }); circle.setAtrr({ stroke: '#000000', strokeWidth, fill }); const group = new DrawGroup({}); group.add(circle); const header = new DrawHeader({ lenght: size, height: size }); header.add(group); return { header, size }; } getObject(config: PunktConfig, options?: Partial): DrawDataDto { const parsed = parsePunktRequest({ config, options }); const { scale, raster } = parsed.config; return { name: 'punkt', width: 3 * scale * raster, height: 3 * scale * raster, varianten: (['inactive', 'active'] as const).map(state => { const fill = state === 'active' ? parsed.options?.colorActive ?? '#00f000' : parsed.options?.colorInactive ?? '#808080'; const { header } = this.create(parsed.config, fill); const xaml = header.getChildString(true, 0); return { state, svg: header.getChildString(false, 0), xaml, xamlBase64: Buffer.from(xaml, 'utf8').toString('base64') }; }) }; } private legacy(options: DrawShapeOptionenDto = {}) { const number = (value: unknown, fallback: number) => value === undefined ? fallback : Number(value); const colors = { ...(options.colorInactive !== undefined ? { colorInactive: options.colorInactive } : {}), ...(options.colorActive !== undefined ? { colorActive: options.colorActive } : {}) }; const result = this.getObject({ scale: number(options.scale, 2), raster: number(options.raster, 4) }, colors); return { result, variant: result.varianten.find(v => v.state === options.state) ?? result.varianten[0] }; } getDrawObject(options?: DrawShapeOptionenDto): DrawDto { const { result, variant } = this.legacy(options); return { name: 'punkt', height: 3, length: 3, px_height: result.height, px_width: result.width, raster: Number(options?.raster ?? 4), scale: Number(options?.scale ?? 2), offset_x: 0, offset_y: 0, svg: variant.svg }; } getSvg(options?: DrawShapeOptionenDto): string { const { variant } = this.legacy(options); return options?.xaml ? variant.xaml : variant.svg; } }