From 495ddb649b5347f480413e0a19a193bf091ed51b Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Sat, 25 Nov 2023 14:19:52 +0100 Subject: [PATCH] Added DrawableStrokeWidth. --- src/drawables/drawable-stroke-width.ts | 24 ++++++++++++++++ src/drawables/drawing-wand.ts | 7 +++++ tests/drawables/drawable-stroke-width.spec.ts | 28 +++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 src/drawables/drawable-stroke-width.ts create mode 100644 tests/drawables/drawable-stroke-width.spec.ts diff --git a/src/drawables/drawable-stroke-width.ts b/src/drawables/drawable-stroke-width.ts new file mode 100644 index 00000000..e468adb7 --- /dev/null +++ b/src/drawables/drawable-stroke-width.ts @@ -0,0 +1,24 @@ +// Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm. +// Licensed under the Apache License, Version 2.0. + +import { IDrawable } from './drawable'; +import { IDrawingWand } from './drawing-wand'; + +/** + * Sets the width of the stroke used to draw object outlines. + */ +export class DrawableStrokeWidth implements IDrawable { + private readonly _width: number; + + /** + * Initializes a new instance of the {@link DrawableStrokeWidth} class. + * @param width - The width. + */ + constructor(width: number) { + this._width = width; + } + + draw(wand: IDrawingWand): void { + wand.strokeWidth(this._width); + } +} diff --git a/src/drawables/drawing-wand.ts b/src/drawables/drawing-wand.ts index 6b766649..aa2fe1ee 100644 --- a/src/drawables/drawing-wand.ts +++ b/src/drawables/drawing-wand.ts @@ -32,6 +32,7 @@ export interface IDrawingWand extends IDisposable { rectangle(upperLeftX: number, upperLeftY: number, lowerRightX: number, lowerRightY: number): void; roundRectangle(upperLeftX: number, upperLeftY: number, lowerRightX: number, lowerRightY: number, cornerWidth: number, cornerHeight: number): void; strokeColor(value: IMagickColor): void; + strokeWidth(value: number): void text(x: number, y: number, value: string): void; textAlignment(value: TextAlignment): void; textAntialias(value: boolean): void; @@ -134,6 +135,12 @@ export class DrawingWand extends NativeInstance implements IDrawingWand { }); } + strokeWidth(value: number): void { + Exception.usePointer(exception => { + ImageMagick._api._DrawingWand_StrokeWidth(this._instance, value, exception); + }); + } + text(x: number, y: number, value: string): void { Exception.usePointer(exception => { _withString(value, valuePtr => { diff --git a/tests/drawables/drawable-stroke-width.spec.ts b/tests/drawables/drawable-stroke-width.spec.ts new file mode 100644 index 00000000..bd75fe0b --- /dev/null +++ b/tests/drawables/drawable-stroke-width.spec.ts @@ -0,0 +1,28 @@ +// Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm. +// Licensed under the Apache License, Version 2.0. + +import { DrawableLine } from '@src/drawables/drawable-line'; +import { DrawableStrokeColor } from '@src/drawables/drawable-stroke-color'; +import { DrawableStrokeWidth } from '@src/drawables/drawable-stroke-width'; +import { MagickColors } from '@src/magick-colors'; +import { TestImages } from '@test/test-images'; + +describe('DrawableStrokeWidth', () => { + it('should change the width of a line', () => { + TestImages.empty150x150Canvas.use((image) => { + const strokeColor = MagickColors.Green; + + image.draw([ + new DrawableStrokeColor(strokeColor), + new DrawableStrokeWidth(10), + new DrawableLine(10, 10, 40, 50) + ]); + + expect(image).toHavePixelWithColor(9, 9, MagickColors.White); + expect(image).toHavePixelWithColor(10, 10, strokeColor); + expect(image).toHavePixelWithColor(6, 13, strokeColor); + expect(image).toHavePixelWithColor(44, 47, strokeColor); + expect(image).toHavePixelWithColor(40, 50, strokeColor); + }); + }); +});