summaryrefslogtreecommitdiffstats
path: root/task1/cairodocument.h
diff options
context:
space:
mode:
Diffstat (limited to 'task1/cairodocument.h')
-rw-r--r--task1/cairodocument.h459
1 files changed, 459 insertions, 0 deletions
diff --git a/task1/cairodocument.h b/task1/cairodocument.h
new file mode 100644
index 0000000..7769c7e
--- /dev/null
+++ b/task1/cairodocument.h
@@ -0,0 +1,459 @@
1/**
2 * $Id: cairodocument.h 2 2009-10-31 02:48:23Z l0728348 $
3 *
4 * Copyright 2009
5 *
6 * @author Manuel Mausz (0728348)
7 * @brief implements some classes to better use cairomm.
8 * however, the design is however crappy.
9 * maybe try using papyrus.
10 */
11
12#ifndef CAIRODOCUMENT_H
13#define CAIRODOCUMENT_H
14
15#include <string>
16#include <sstream>
17#include <list>
18#include <cairomm/cairomm.h>
19
20/* forward declerations */
21class CairoObject;
22class CairoFont;
23class CairoColor;
24class CairoRectangle;
25class CairoTextBox;
26class CairoDocument;
27
28/**
29 * abstract class for all classes of this file
30 * design: every object can contain other objects, which will
31 * get applied before applying the actual object
32 */
33class CairoObject
34{
35 public:
36 /** objectlist to contain */
37 typedef std::list<CairoObject *> CairoObjectList;
38
39 /**
40 * @brief Default dtor. virtual
41 */
42 virtual ~CairoObject()
43 {}
44
45 /**
46 * @brief adds another CairoObject to this CairoObject
47 * @param obj reference to the CairoObject to add
48 * @return reference to this object
49 */
50 CairoObject& add(CairoObject& obj)
51 {
52 if (&obj != this)
53 m_objects.push_back(&obj);
54 return *this;
55 }
56
57 /**
58 * @brief adds another CairoObject to this CairoObject
59 * @param obj reference to the CairoObject to add
60 * @return reference to this object
61 */
62 CairoObject& operator()(CairoObject& obj)
63 {
64 return add(obj);
65 }
66
67 /**
68 * @brief applys this CairoObject to the current context
69 * all containing objects will be applied before
70 * @param context cairomm context
71 */
72 void apply(const Cairo::RefPtr<Cairo::Context> context);
73
74 /**
75 * @brief prepares this object for applying
76 * will be called be applyObject()
77 * @param context cairomm context
78 */
79 virtual void prepareObject(const Cairo::RefPtr<Cairo::Context> context)
80 {}
81
82 /**
83 * @brief applies this object to the current context
84 * @param context cairomm context
85 */
86 virtual void applyObject(const Cairo::RefPtr<Cairo::Context> context) = 0;
87
88 protected:
89 /** list of cairoobjects in this cairoobject */
90 CairoObjectList m_objects;
91};
92
93/*----------------------------------------------------------------------------*/
94
95/**
96 * represents a font in cairomm
97 */
98class CairoFont
99: public CairoObject
100{
101 public:
102 /**
103 * @brief Default dtor
104 * @param size fontsize
105 * @param family fontfamily
106 * @param slant type of slant
107 * @param weight font weight
108 */
109 CairoFont(const double size, const std::string& family, Cairo::FontSlant slant = Cairo::FONT_SLANT_NORMAL,
110 Cairo::FontWeight weight = Cairo::FONT_WEIGHT_NORMAL)
111 : m_size(size), m_family(family), m_slant(slant), m_weight(weight), m_height(-1)
112 {}
113
114 /**
115 * Default dtor
116 */
117 ~CairoFont()
118 {}
119
120 /**
121 * @brief get height of current object
122 * @return height of current object
123 */
124 double height();
125
126 /**
127 * @brief applies this object to the current context
128 * @param context cairomm context
129 */
130 void applyObject(const Cairo::RefPtr<Cairo::Context> context);
131
132 private:
133 double m_size;
134 std::string m_family;
135 Cairo::FontSlant m_slant;
136 Cairo::FontWeight m_weight;
137 double m_height;
138};
139
140/*----------------------------------------------------------------------------*/
141
142/**
143 * represents a color in cairomm
144 */
145class CairoColor
146: public CairoObject
147{
148 public:
149 /**
150 * @brief Default dtor
151 * @param red value for red part (0 to 1)
152 * @param green value for green part (0 to 1)
153 * @param blue value for blue part (0 to 1)
154 * @param alpha value for alpha (0 to 1)
155 */
156 CairoColor(const double red = 0.0, const double green = 0.0, const double blue = 0.0,
157 const double alpha = 1.0)
158 : m_red(red), m_green(green), m_blue(blue), m_alpha(alpha)
159 {}
160
161 /**
162 * Default dtor
163 */
164 ~CairoColor()
165 {}
166
167 /**
168 * @brief applies this object to the current context
169 * @param context cairomm context
170 */
171 void applyObject(const Cairo::RefPtr<Cairo::Context> context);
172
173 private:
174 double m_red, m_green, m_blue;
175 double m_alpha;
176};
177
178/*----------------------------------------------------------------------------*/
179
180/**
181 * creates a rectangle in cairomm
182 */
183class CairoRectangle
184: public CairoObject
185{
186 public:
187 /**
188 * @brief Default dtor
189 * @param x x-coordinates to start
190 * @param y y-coordinates to start
191 * @param width rectangle width
192 * @param height rectangle height
193 * @param linewidth linewidth for rectangle
194 */
195 CairoRectangle(const double x, const double y, const double width, const double height,
196 const double linewidth = 0.2)
197 : m_x(x), m_y(y), m_width(width), m_height(height), m_linewidth(linewidth)
198 {}
199
200 /**
201 * Default dtor
202 */
203 CairoRectangle()
204 {}
205
206 /**
207 * @brief get x-coordinate of current object
208 * @return x-coordinate of current object
209 */
210 double x()
211 {
212 return m_x;
213 }
214
215 /**
216 * @brief get y-coordinate of current object
217 * @return y-coordinate of current object
218 */
219 double y()
220 {
221 return m_y;
222 }
223
224 /**
225 * @brief get height of current object
226 * @return height of current object
227 */
228 double height()
229 {
230 return m_height;
231 }
232
233 /**
234 * @brief get width of current object
235 * @return width of current object
236 */
237 double width()
238 {
239 return m_width;
240 }
241
242 /**
243 * @brief applies this object to the current context
244 * @param context cairomm context
245 */
246 void applyObject(const Cairo::RefPtr<Cairo::Context> context);
247
248 private:
249 double m_x, m_y;
250 double m_width, m_height;
251 double m_linewidth;
252};
253
254/*----------------------------------------------------------------------------*/
255
256/**
257 * creates a textbox using cairomm
258 */
259class CairoTextBox
260: public CairoObject
261{
262 public:
263 /**
264 * @brief Default dtor
265 * @param x x-coordinates to start
266 * @param y y-coordinates to start
267 * @param width rectangle width
268 * @param height rectangle height
269 * @param padding padding for textbox
270 */
271 CairoTextBox(const double x, const double y, const double width, const double height,
272 const double padding = 0)
273 : m_x(x), m_y(y), m_width(width), m_height(height), m_padding(padding), m_text("")
274 {}
275
276 /**
277 * Default dtor
278 * deletes allocated rectangle objects for borders
279 */
280 ~CairoTextBox()
281 {
282 for(CairoRectangleList::iterator it = m_rectangles.begin(); it != m_rectangles.end(); ++it)
283 delete *it;
284 }
285
286 /**
287 * @brief add text to this textbox
288 * @param text text to add
289 */
290 void addText(const std::string& text)
291 {
292 m_text << text;
293 }
294
295 /**
296 * @brief istream operator to add text to this textbox
297 * @param text text to add
298 * @return reference to this object
299 */
300 CairoTextBox& operator<<(std::string text)
301 {
302 m_text << text;
303 return *this;
304 }
305
306 /**
307 * @brief istream operator to add a integer to this textbox
308 * @param num integer to add
309 * @return reference to this object
310 */
311 CairoTextBox& operator<<(int num)
312 {
313 m_text << num;
314 return *this;
315 }
316
317 /**
318 * @brief adds border around this textbox
319 * using a dynamically allocated rectangle object
320 * @param linewidth linewidth for the border
321 * @param obj pointer to object which will be added to the
322 * dynamically allocated border
323 * @return reference to this object
324 */
325 CairoTextBox& addBorder(const double linewidth = 0.2, CairoObject *obj = NULL);
326
327 /**
328 * @brief get x-coordinate of current object
329 * @return x-coordinate of current object
330 */
331 double x()
332 {
333 return m_x;
334 }
335
336 /**
337 * @brief get y-coordinate of current object
338 * @return y-coordinate of current object
339 */
340 double y()
341 {
342 return m_y;
343 }
344
345 /**
346 * @brief get height of current object
347 * @return height of current object
348 */
349 double height()
350 {
351 return m_height;
352 }
353
354 /**
355 * @brief get width of current object
356 * @return width of current object
357 */
358 double width()
359 {
360 return m_width;
361 }
362
363 /**
364 * @brief get padding of current object
365 * @return padding of current object
366 */
367 double padding()
368 {
369 return m_padding;
370 }
371
372 /**
373 * @brief applies this object to the current context
374 * @param context cairomm context
375 */
376 void applyObject(const Cairo::RefPtr<Cairo::Context> context);
377
378 private:
379 /** rectangelist for added borders */
380 typedef std::list<CairoRectangle *> CairoRectangleList;
381
382 double m_x, m_y;
383 double m_width, m_height;
384 double m_padding;
385 std::stringstream m_text;
386 CairoRectangleList m_rectangles;
387};
388
389/*----------------------------------------------------------------------------*/
390
391/**
392 * creates a document using cairomm
393 */
394class CairoDocument
395: public CairoObject
396{
397 public:
398 /** valid outout formats for the document */
399 enum OutputFormat
400 {
401#ifdef CAIRO_HAS_PDF_SURFACE
402 PDF,
403#endif
404 };
405
406 /**
407 * @brief Default dtor
408 * @param width width of document
409 * @param height height of document
410 */
411 CairoDocument(const unsigned width, const unsigned height)
412 : m_width(width), m_height(height)
413 {}
414
415 /**
416 * Default dtor
417 */
418 ~CairoDocument()
419 {}
420
421 /**
422 * @brief applies this object to the current context
423 * @param context cairomm context
424 */
425 void applyObject(const Cairo::RefPtr<Cairo::Context> context)
426 {}
427
428 /**
429 * @brief generates and saves the current document
430 * @param filename filename for document
431 * @param format output format for document
432 */
433 bool save(const std::string& filename, const OutputFormat format);
434
435 /**
436 * @brief get width of current object
437 * @return width of current object
438 */
439 unsigned width()
440 {
441 return m_width;
442 }
443
444 /**
445 * @brief get height of current object
446 * @return height of current object
447 */
448 unsigned height()
449 {
450 return m_height;
451 }
452
453 private:
454 unsigned m_width, m_height;
455};
456
457#endif
458
459/* vim: set et sw=2 ts=2: */