sensorDigital, grafik Fest, drawPunkt eingefügt
This commit is contained in:
1 parent
f43053d7b3
commit
6e4bd5339f
51 files changed
+1203
-31
No files matched your search
@@ -0,0 +1,42 @@
|
||||
import { describe, expect, it } from '@jest/globals';
|
||||
import { DrawPunkt } from './drawPunkt';
|
||||
import { parsePunktRequest } from '../../_dto/punkt.dto';
|
||||
|
||||
describe('DrawPunkt', () => {
|
||||
it('liefert einen Kreis ohne Text, mit schwarzem Rand sowie SVG, XAML und Base64 für beide Zustände', () => {
|
||||
const result = new DrawPunkt().getObject({ scale: 2, raster: 4 }, { colorInactive: '#123456', colorActive: '#abcdef' });
|
||||
expect(result.name).toBe('punkt'); expect(result.width).toBe(24); expect(result.height).toBe(24);
|
||||
expect(result.varianten.map(v => v.state)).toEqual(['inactive', 'active']);
|
||||
for (const [i, variant] of result.varianten.entries()) {
|
||||
const color = i === 0 ? '#123456' : '#abcdef';
|
||||
expect(variant.svg).toContain('<ellipse'); expect(variant.svg).toContain('stroke="#000000"');
|
||||
expect(variant.svg).toContain(`fill="${color}"`); expect(variant.svg).not.toContain('<text');
|
||||
expect(variant.xaml).toContain('<Ellipse'); expect(variant.xaml).toContain('Stroke="#000000"'); expect(variant.xaml).toContain(`Fill="${color}"`);
|
||||
expect(Buffer.from(variant.xamlBase64, 'base64').toString('utf8')).toBe(variant.xaml);
|
||||
}
|
||||
});
|
||||
it('hält den Rand vollständig innerhalb der Zeichenfläche, auch bei kleinen Größen', () => {
|
||||
for (const scale of [0.01, 1, 4]) {
|
||||
const result = new DrawPunkt().getObject({ scale, raster: 4 });
|
||||
const svg = result.varianten[0].svg;
|
||||
const radius = Number(/rx="([^"]+)"/.exec(svg)?.[1]);
|
||||
const stroke = Number(/stroke-width="([^"]+)"/.exec(svg)?.[1]);
|
||||
expect(2 * radius + stroke).toBeCloseTo(result.width);
|
||||
expect(svg).toContain(`ry="${radius}"`);
|
||||
}
|
||||
});
|
||||
it('verwendet Motor-kompatible Größenparameter und Standardfarben', () => {
|
||||
const parsed = parsePunktRequest({ config: {} });
|
||||
expect(parsed.config).toEqual({ scale: 2, raster: 4 });
|
||||
const punkt = new DrawPunkt();
|
||||
expect(punkt.getObject(parsed.config).varianten[0].svg).toContain('#808080');
|
||||
expect(punkt.getObject(parsed.config).varianten[1].svg).toContain('#00f000');
|
||||
expect(punkt.getDrawObject({}).px_width).toBe(24);
|
||||
expect(punkt.getSvg({ state: 'active', colorActive: '#112233' })).toContain('#112233');
|
||||
expect(punkt.getSvg({ xaml: true })).toContain('<Viewbox');
|
||||
});
|
||||
it.each([null, {}, { config: { scale: 0 } }, { config: { raster: -1 } }, { config: { diameter: 20 } },
|
||||
{ config: {}, options: { colorActive: 'red' } }, { config: {}, options: { colorError: '#ff0000' } }])('verhindert ungültige Parameter %j', input => {
|
||||
expect(() => parsePunktRequest(input)).toThrow();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,64 @@
|
||||
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<PunktConfig, PunktOptions> {
|
||||
|
||||
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<PunktOptions>): 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;
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,9 @@ export interface MotorConfig {
|
||||
raster: number;
|
||||
}
|
||||
|
||||
export interface PunktConfig { scale: number; raster: number }
|
||||
export interface PunktOptions { colorInactive: string; colorActive: string }
|
||||
|
||||
export interface FahrwagenConfig{
|
||||
length:number,
|
||||
width:number,
|
||||
|
||||
@@ -10,6 +10,7 @@ interface PropEllipse {
|
||||
interface SvgOptions {
|
||||
color?: string
|
||||
storkWidth?: number,
|
||||
strokeWidth?: number,
|
||||
stroke?:string,
|
||||
dasharray?: string,
|
||||
fill?: string,
|
||||
|
||||
Reference in new issue
Block a user