From 7fa10612e703717875c66febda7e732d962d8d96 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Thu, 30 Mar 2023 08:45:17 -0700 Subject: [PATCH] drawString function --- src/main.zig | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/main.zig b/src/main.zig index ca7baf6..8add0be 100644 --- a/src/main.zig +++ b/src/main.zig @@ -275,18 +275,8 @@ fn convertImage(alloc: std.mem.Allocator, filename: [:0]u8, pixels: *[WIDTH * HE if (status == c.MagickFalse) return error.CouldNotSetExtent; - mw = try drawCharacter( - mw.?, - '4', - -5 * 3, - -8, - ); - mw = try drawCharacter( - mw.?, - '2', - -5 * 4, - -8, - ); + mw = try drawString(mw, "hello, world!", 30, 38); + // We make the image monochrome by quantizing the image with 2 colors in the // gray colorspace. See: // https://www.imagemagick.org/Usage/quantize/#monochrome @@ -319,6 +309,18 @@ fn convertImage(alloc: std.mem.Allocator, filename: [:0]u8, pixels: *[WIDTH * HE } } } +fn drawString(mw: ?*c.MagickWand, str: []const u8, x: isize, y: isize) !?*c.MagickWand { + var rc = mw; + for (str, 0..) |ch, i| { + rc = try drawCharacter( + rc, + ch, + -(x + @intCast(isize, FONT_WIDTH * i)), + -y, + ); + } + return rc; +} fn drawCharacter(mw: ?*c.MagickWand, char: u8, x: isize, y: isize) !?*c.MagickWand { // Create a second wand. Does this need to exist after the block?