summaryrefslogtreecommitdiff
path: root/dmagick/DrawingContext.d
blob: 51f24f39e195548f8994138f39c43a42f4c39d0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
 * Copyright: Mike Wey 2011
 * License:   zlib (See accompanying LICENSE file)
 * Authors:   Mike Wey
 */

module dmagick.DrawingContext;

import dmagick.Color;
import dmagick.Geometry;
import dmagick.Image;
import dmagick.Options;

import dmagick.c.draw;
import dmagick.c.geometry;
import dmagick.c.type;

/**
 * Drawable provides a convenient interface for preparing vector,
 * image, or text arguments.
 */
class DrawingContext
{
	private void delegate(Image)[] actions;

	/**
	 * Apply the drawing context to the image.
	 */
	void draw(Image image)
	{
		Options options = image.options.clone();

		foreach ( action; actions )
			action(image);

		image.options = options;

		//Make sure the ImageInfo an DrawInfo for these a in sync.
		image.backgroundColor = options.backgroundColor;
		image.borderColor = options.borderColor;
		image.fuzz = options.fuzz;
	}

	/**
	 * Specify a transformation matrix to adjust scaling, rotation, and
	 * translation (coordinate transformation) for subsequently drawn
	 * objects in the drawing context. 
	 */
	void affine(AffineMatrix affine)
	{
		actions ~= (Image image)
		{
			image.options.affine = affine;
		};
	}

	/**
	 * Transforms the image as specified by the affine matrix.
	 */
	void affineTransform(AffineMatrix affine)
	{
		actions ~= (Image image)
		{
			image.affineTransform(affine);
		};
	}

	/**
	 * Control antialiasing of rendered Postscript
	 * and Postscript or TrueType fonts. The default is true.
	 */
	void antialias(bool antialias)
	{
		actions ~= (Image image)
		{
			image.options.antialias = antialias;
		};
	}

	/**
	 * Set the image background color. The default is "white".
	 */
	void backgroundColor(Color color)
	{
		actions ~= (Image image)
		{
			image.backgroundColor = color;
		};
	}

	/**
	 * Set the image border color. The default is "#dfdfdf".
	 */
	void borderColor(string color)
	{
		actions ~= (Image image)
		{
			image.borderColor = color;
		};
	}


	/**
	 * If set, causes the text to be drawn over a box of the specified color.
	 */
	void boxColor(Color color)
	{
		actions ~= (Image image)
		{
			image.options.boxColor = color;
		};
	}

	/**
	 * Color to use when filling drawn objects.
	 * The default is "black".
	 */
	void fillColor(Color color)
	{
		actions ~= (Image image)
		{
			image.options.fillColor = color;
		};
	}

	/**
	 * Pattern image to use when filling drawn objects.
	 */
	void fillPattern(Image pattern)
	{
		actions ~= (Image image)
		{
			image.options.fillPattern = pattern;
		};
	}

	/**
	 * Rule to use when filling drawn objects.
	 */
	void fillRule(FillRule rule)
	{
		actions ~= (Image image)
		{
			image.options.fillRule = rule;
		};
	}

	/**
	 * The _font name or filename.
	 * You can tag a _font to specify whether it is a Postscript,
	 * Truetype, or OPTION1 _font. For example, Arial.ttf is a
	 * Truetype _font, ps:helvetica is Postscript, and x:fixed is OPTION1.
	 * 
	 * The _font name can be a complete filename such as
	 * "/mnt/windows/windows/fonts/Arial.ttf". The _font name can
	 * also be a fully qualified X font name such as
	 * "-urw-times-medium-i-normal--0-0-0-0-p-0-iso8859-13".
	 */
	void font(string str)
	{
		actions ~= (Image image)
		{
			image.options.font = str;
		};
	}

	/**
	 * Specify font family, style, weight (one of the set { 100 | 200 |
	 * 300 | 400 | 500 | 600 | 700 | 800 | 900 } with 400 being the normal
	 * size), and stretch to be used to select the font used when drawing
	 * text. Wildcard matches may be applied to style via the AnyStyle
	 * enumeration, applied to weight if weight is zero, and applied to
	 * stretch via the AnyStretch enumeration.
	 */
	void font(string family, StyleType style = StyleType.NormalStyle, size_t weight = 400, StretchType stretch = StretchType.NormalStretch)
	{
		actions ~= (Image image)
		{
			image.options.fontFamily  = family;
			image.options.fontStyle   = style;
			image.options.fontWeight  = weight;
			image.options.fontStretch = stretch;
		};
	}

	/**
	 * Colors within this distance are considered equal. 
	 * A number of algorithms search for a target  color.
	 * By default the color must be exact. Use this option to match
	 * colors that are close to the target color in RGB space.
	 */
	void fuzz(double f)
	{
		actions ~= (Image image)
		{
			image.fuzz = f;
		};
	}

	/**
	 * Text rendering font point size
	 */
	void pointSize(double size)
	{
		actions ~= (Image image)
		{
			image.options.pointSize = size;
		};
	}

	/**
	 * Enable or disable anti-aliasing when drawing object outlines.
	 */
	void strokeAntialias(bool antialias)
	{
		actions ~= (Image image)
		{
			image.options.strokeAntialias = antialias;
		};
	}

	/**
	 * Color to use when drawing object outlines
	 */
	void strokeColor(Color color)
	{
		actions ~= (Image image)
		{
			image.options.strokeColor = color;
		};
	}

	/**
	 * The initial distance into the dash pattern. The units are pixels.
	 */
	void strokeDashOffset(double offset)
	{
		actions ~= (Image image)
		{
			image.options.strokeDashOffset = offset;
		};
	}

	/**
	 * Describe a _pattern of dashes to be used when stroking paths.
	 * The arguments are a list of pixel widths of
	 * alternating dashes and gaps.
	 * All elements must be > 0.
	 */
	void strokeDashPattern(const(double)[] pattern)
	{
		actions ~= (Image image)
		{
			image.options.strokeDashPattern = pattern;
		};
	}

	/**
	 * Specify how the line ends should be drawn.
	 */
	void strokeLineCap(LineCap cap)
	{
		actions ~= (Image image)
		{
			image.options.strokeLineCap = cap;
		};
	}

	/**
	 * Specify how corners are drawn.
	 */
	void strokeLineJoin(LineJoin join)
	{
		actions ~= (Image image)
		{
			image.options.strokeLineJoin = join;
		};
	}

	/**
	 * Specify a constraint on the length of the "miter"
	 * formed by two lines meeting at an angle. If the angle
	 * if very sharp, the miter could be very long relative
	 * to the line thickness. The miter _limit is a _limit on
	 * the ratio of the miter length to the line width.
	 * The default is 4.
	 */
	void strokeMiterlimit(size_t limit)
	{
		actions ~= (Image image)
		{
			image.options.strokeMiterlimit = limit;
		};
	}

	/**
	 * Pattern image to use while drawing object stroke
	 */
	void strokePattern(Image pattern)
	{
		actions ~= (Image image)
		{
			image.options.strokePattern = pattern;
		};
	}

	/**
	 * Stroke _width for use when drawing vector objects
	 */
	void strokeWidth(double width)
	{
		actions ~= (Image image)
		{
			image.options.strokeWidth = width;
		};
	}

	/**
	 * The text density in the x and y directions. The default is "72x72".
	 */
	void textDensity(Geometry geometry)
	{
		actions ~= (Image image)
		{
			image.options.textDensity = geometry;
		};
	}

	/**
	 * Specify the code set to use for text annotations.
	 * The only character encoding which may be specified at
	 * this time is "UTF-8" for representing Unicode as a
	 * sequence of bytes. Specify an empty string to use
	 * ASCII encoding. Successful text annotation using
	 * Unicode may require fonts designed to support Unicode.
	 * The default is "UTF-8"
	 */
	void textEncoding(string encoding)
	{
		actions ~= (Image image)
		{
			image.options.textEncoding = encoding;
		};
	}
}