From d05d093854bdadad03456fbccc1a2da490ce410a Mon Sep 17 00:00:00 2001 From: Mike Wey Date: Wed, 23 Feb 2011 23:25:17 +0100 Subject: Add Geometry.d --- dmagick/Geometry.d | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++ dmagick/Utils.d | 23 +++++++++++- 2 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 dmagick/Geometry.d (limited to 'dmagick') diff --git a/dmagick/Geometry.d b/dmagick/Geometry.d new file mode 100644 index 0000000..dec6b4f --- /dev/null +++ b/dmagick/Geometry.d @@ -0,0 +1,108 @@ +/** + * A class to specify a geometry argument. + * + * Copyright: Mike Wey 2011 + * License: To be determined + * Authors: Mike Wey + */ + +module dmagick.Geometry; + +import std.conv; +import std.ctype; +import std.string; +import core.sys.posix.sys.types; + +import dmagick.c.geometry; +import dmagick.c.magickString; +import dmagick.c.magickType; + +struct Geometry +{ + size_t width; + size_t height; + ssize_t xOffset; + ssize_t yOffset; + bool percent; + bool minimum; + bool keepAspect = true; + bool greater; + bool less; + + this(string geometry) + { + MagickStatusType flags; + + //If the strin starts with a letter assume it's a Page Geometry. + if ( isalpha(geometry[0]) ) + { + char* geo = GetPageGeometry(toStringz(geometry)); + + if( geo !is null ) + { + geometry = to!(string)(geo); + DestroyString(geo); + } + } + + flags = GetGeometry(toStringz(geometry), &xOffset, &yOffset, &width, &height); + + percent = ( flags & GeometryFlags.PercentValue ) != 0; + minimum = ( flags & GeometryFlags.MinimumValue ) != 0; + keepAspect = ( flags & GeometryFlags.AspectValue ) == 0; + greater = ( flags & GeometryFlags.GreaterValue ) != 0; + less = ( flags & GeometryFlags.LessValue ) != 0; + } + + unittest + { + Geometry geo = Geometry("200x150-50+25!"); + assert( geo.width == 200 && geo.xOffset == -50 ); + assert( geo.keepAspect == false ); + + geo = Geometry("A4"); + assert( geo.width == 595 && geo.height == 842); + } + + this(size_t width, size_t height, ssize_t xOffset, ssize_t yOffset) + { + this.width = width; + this.height = height; + this.xOffset = xOffset; + this.yOffset = yOffset; + } + + string toString() + { + string geometry; + + if ( width > 0 ) + geometry ~= to!(string)(width); + + if ( height > 0 ) + geometry ~= "x" ~ to!(string)(height); + + if ( xOffset != 0 && yOffset != 0 ) + geometry ~= format("%+s%+s", xOffset, yOffset); + + geometry ~= format("%s%s%s%s%s", + percent ? "%" : "", + minimum ? "^" : "", + keepAspect ? "" : "!", + less ? "<" : "", + greater ? ">" : ""); + + return geometry; + } + + unittest + { + Geometry geo = Geometry("200x150-50+25!"); + assert( geo.toString == "200x150-50+25!"); + } + + int opCmp(ref const Geometry geometry) + { + return width*height - geometry.width*geometry.height; + } +} diff --git a/dmagick/Utils.d b/dmagick/Utils.d index b7ebd33..2429626 100644 --- a/dmagick/Utils.d +++ b/dmagick/Utils.d @@ -35,13 +35,15 @@ void copyString(ref char[MaxTextExtent] dest, string source) * We use this since using CloneString forces us to * append a \0 to the end of the string, and the realocation * whould be wastefull if we are just going to copy it + * + * used for copying a string into a Imagemagick struct */ void copyString(ref char* dest, string source) { if ( source is null ) { if ( dest !is null ) - DestroyString(dest); + dest = DestroyString(dest); return; } @@ -62,6 +64,25 @@ void copyString(ref char* dest, string source) dest[source.length] = '\0'; } +unittest +{ + char* dest; + string source = "test"; + + copyString(dest, source); + + assert( dest !is source.ptr ); + assert( dest[0..5] == "test\0" ); + + copyString(dest, "unit"); + assert( dest[0..5] == "unit\0" ); + + copyString(dest, null); + assert( dest is null ); +} + +void main(){} + /** */ real degreesToRadians(real deg) { -- cgit v1.2.3