From 4e78dfc0ca5d02ab193a45bc5d6b80e42ba1e91c Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Wed, 19 Jul 2023 18:24:42 +0200 Subject: [PATCH] Added DrawableTextUnderColor. --- src/drawables/drawable-text-under-color.ts | 18 ++++++++ src/drawables/drawing-wand.ts | 9 ++++ src/index.ts | 1 + .../drawable-text-under-color.spec.ts | 41 +++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 src/drawables/drawable-text-under-color.ts create mode 100644 tests/drawables/drawable-text-under-color.spec.ts diff --git a/src/drawables/drawable-text-under-color.ts b/src/drawables/drawable-text-under-color.ts new file mode 100644 index 00000000..a9a640f2 --- /dev/null +++ b/src/drawables/drawable-text-under-color.ts @@ -0,0 +1,18 @@ +// 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'; +import { MagickColor } from '../magick-color'; + +export class DrawableTextUnderColor implements IDrawable { + private readonly _color: MagickColor; + + constructor(color: MagickColor) { + this._color = color; + } + + draw(wand: IDrawingWand): void { + wand.textUnderColor(this._color); + } +} diff --git a/src/drawables/drawing-wand.ts b/src/drawables/drawing-wand.ts index 5880be45..a95c1b61 100644 --- a/src/drawables/drawing-wand.ts +++ b/src/drawables/drawing-wand.ts @@ -33,6 +33,7 @@ export interface IDrawingWand extends IDisposable { textInterlineSpacing(value: number): void; textInterwordspacing(value: number): void; textKerning(value: number): void; + textUnderColor(value: MagickColor): void; } export class DrawingWand extends NativeInstance implements IDrawingWand { @@ -151,6 +152,14 @@ export class DrawingWand extends NativeInstance implements IDrawingWand { }); } + textUnderColor(value: MagickColor): void { + Exception.usePointer(exception => { + value._use(valuePtr => { + ImageMagick._api._DrawingWand_TextUnderColor(this._instance, valuePtr, exception); + }); + }); + } + /** @internal */ static _create(image: IMagickImage, settings: MagickSettings): DrawingWand { return new DrawingWand(image, settings); diff --git a/src/index.ts b/src/index.ts index 00d85186..201c8187 100644 --- a/src/index.ts +++ b/src/index.ts @@ -34,6 +34,7 @@ export * from './drawables/drawable-text-decoration'; export * from './drawables/drawable-text-interline-spacing'; export * from './drawables/drawable-text-interword-spacing'; export * from './drawables/drawable-text-kerning'; +export * from './drawables/drawable-text-under-color'; export * from './drawables/drawable-text'; export * from './drawables/drawable'; export * from './drawables/drawing-wand'; diff --git a/tests/drawables/drawable-text-under-color.spec.ts b/tests/drawables/drawable-text-under-color.spec.ts new file mode 100644 index 00000000..af4a4a71 --- /dev/null +++ b/tests/drawables/drawable-text-under-color.spec.ts @@ -0,0 +1,41 @@ +// Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm. +// Licensed under the Apache License, Version 2.0. + +import { DrawableFont } from "../../src/drawables/drawable-font"; +import { DrawableFontPointSize } from "../../src/drawables/drawable-font-point-size"; +import { DrawableText } from "../../src/drawables/drawable-text"; +import { DrawableTextUnderColor } from "../../src/drawables/drawable-text-under-color"; +import { MagickColors } from "../../src/magick-colors"; +import { TestFonts } from "../test-fonts"; +import { TestImages } from "../test-images"; + +describe("DrawableTextUnderColor", () => { + it("should write text without text under color to the image", () => { + TestImages.emptyCanvas.use((image) => { + image.draw([ + new DrawableFont(TestFonts.kaushanScriptRegularTtf.name), + new DrawableFontPointSize(80), + new DrawableText(0, 100, "I"), + ]); + + expect(image).toHavePixelWithColor(37, 50, "#ffffffff"); + expect(image).toHavePixelWithColor(38, 50, "#ffffffff"); + expect(image).toHavePixelWithColor(39, 50, "#ffffffff"); + }); + }); + + it("should write text with text under color to the image", () => { + TestImages.emptyCanvas.use((image) => { + image.draw([ + new DrawableFont(TestFonts.kaushanScriptRegularTtf.name), + new DrawableFontPointSize(80), + new DrawableTextUnderColor(MagickColors.Pink), + new DrawableText(0, 100, "I"), + ]); + + expect(image).toHavePixelWithColor(37, 50, "#ffc0cbff"); + expect(image).toHavePixelWithColor(38, 50, "#fffcfdff"); + expect(image).toHavePixelWithColor(39, 50, "#ffffffff"); + }); + }); +});