/** * $Id: cairodocument.h 2 2009-10-31 02:48:23Z l0728348 $ * * Copyright 2009 * * @author Manuel Mausz (0728348) * @brief implements some classes to better use cairomm. * however, the design is however crappy. * maybe try using papyrus. */ #ifndef CAIRODOCUMENT_H #define CAIRODOCUMENT_H #include #include #include #include /* forward declerations */ class CairoObject; class CairoFont; class CairoColor; class CairoRectangle; class CairoTextBox; class CairoDocument; /** * abstract class for all classes of this file * design: every object can contain other objects, which will * get applied before applying the actual object */ class CairoObject { public: /** objectlist to contain */ typedef std::list CairoObjectList; /** * @brief Default dtor. virtual */ virtual ~CairoObject() {} /** * @brief adds another CairoObject to this CairoObject * @param obj reference to the CairoObject to add * @return reference to this object */ CairoObject& add(CairoObject& obj) { if (&obj != this) m_objects.push_back(&obj); return *this; } /** * @brief adds another CairoObject to this CairoObject * @param obj reference to the CairoObject to add * @return reference to this object */ CairoObject& operator()(CairoObject& obj) { return add(obj); } /** * @brief applys this CairoObject to the current context * all containing objects will be applied before * @param context cairomm context */ void apply(const Cairo::RefPtr context); /** * @brief prepares this object for applying * will be called be applyObject() * @param context cairomm context */ virtual void prepareObject(const Cairo::RefPtr context) {} /** * @brief applies this object to the current context * @param context cairomm context */ virtual void applyObject(const Cairo::RefPtr context) = 0; protected: /** list of cairoobjects in this cairoobject */ CairoObjectList m_objects; }; /*----------------------------------------------------------------------------*/ /** * represents a font in cairomm */ class CairoFont : public CairoObject { public: /** * @brief Default dtor * @param size fontsize * @param family fontfamily * @param slant type of slant * @param weight font weight */ CairoFont(const double size, const std::string& family, Cairo::FontSlant slant = Cairo::FONT_SLANT_NORMAL, Cairo::FontWeight weight = Cairo::FONT_WEIGHT_NORMAL) : m_size(size), m_family(family), m_slant(slant), m_weight(weight), m_height(-1) {} /** * Default dtor */ ~CairoFont() {} /** * @brief get height of current object * @return height of current object */ double height(); /** * @brief applies this object to the current context * @param context cairomm context */ void applyObject(const Cairo::RefPtr context); private: double m_size; std::string m_family; Cairo::FontSlant m_slant; Cairo::FontWeight m_weight; double m_height; }; /*----------------------------------------------------------------------------*/ /** * represents a color in cairomm */ class CairoColor : public CairoObject { public: /** * @brief Default dtor * @param red value for red part (0 to 1) * @param green value for green part (0 to 1) * @param blue value for blue part (0 to 1) * @param alpha value for alpha (0 to 1) */ CairoColor(const double red = 0.0, const double green = 0.0, const double blue = 0.0, const double alpha = 1.0) : m_red(red), m_green(green), m_blue(blue), m_alpha(alpha) {} /** * Default dtor */ ~CairoColor() {} /** * @brief applies this object to the current context * @param context cairomm context */ void applyObject(const Cairo::RefPtr context); private: double m_red, m_green, m_blue; double m_alpha; }; /*----------------------------------------------------------------------------*/ /** * creates a rectangle in cairomm */ class CairoRectangle : public CairoObject { public: /** * @brief Default dtor * @param x x-coordinates to start * @param y y-coordinates to start * @param width rectangle width * @param height rectangle height * @param linewidth linewidth for rectangle */ CairoRectangle(const double x, const double y, const double width, const double height, const double linewidth = 0.2) : m_x(x), m_y(y), m_width(width), m_height(height), m_linewidth(linewidth) {} /** * Default dtor */ CairoRectangle() {} /** * @brief get x-coordinate of current object * @return x-coordinate of current object */ double x() { return m_x; } /** * @brief get y-coordinate of current object * @return y-coordinate of current object */ double y() { return m_y; } /** * @brief get height of current object * @return height of current object */ double height() { return m_height; } /** * @brief get width of current object * @return width of current object */ double width() { return m_width; } /** * @brief applies this object to the current context * @param context cairomm context */ void applyObject(const Cairo::RefPtr context); private: double m_x, m_y; double m_width, m_height; double m_linewidth; }; /*----------------------------------------------------------------------------*/ /** * creates a textbox using cairomm */ class CairoTextBox : public CairoObject { public: /** * @brief Default dtor * @param x x-coordinates to start * @param y y-coordinates to start * @param width rectangle width * @param height rectangle height * @param padding padding for textbox */ CairoTextBox(const double x, const double y, const double width, const double height, const double padding = 0) : m_x(x), m_y(y), m_width(width), m_height(height), m_padding(padding), m_text("") {} /** * Default dtor * deletes allocated rectangle objects for borders */ ~CairoTextBox() { for(CairoRectangleList::iterator it = m_rectangles.begin(); it != m_rectangles.end(); ++it) delete *it; } /** * @brief add text to this textbox * @param text text to add */ void addText(const std::string& text) { m_text << text; } /** * @brief istream operator to add text to this textbox * @param text text to add * @return reference to this object */ CairoTextBox& operator<<(std::string text) { m_text << text; return *this; } /** * @brief istream operator to add a integer to this textbox * @param num integer to add * @return reference to this object */ CairoTextBox& operator<<(int num) { m_text << num; return *this; } /** * @brief adds border around this textbox * using a dynamically allocated rectangle object * @param linewidth linewidth for the border * @param obj pointer to object which will be added to the * dynamically allocated border * @return reference to this object */ CairoTextBox& addBorder(const double linewidth = 0.2, CairoObject *obj = NULL); /** * @brief get x-coordinate of current object * @return x-coordinate of current object */ double x() { return m_x; } /** * @brief get y-coordinate of current object * @return y-coordinate of current object */ double y() { return m_y; } /** * @brief get height of current object * @return height of current object */ double height() { return m_height; } /** * @brief get width of current object * @return width of current object */ double width() { return m_width; } /** * @brief get padding of current object * @return padding of current object */ double padding() { return m_padding; } /** * @brief applies this object to the current context * @param context cairomm context */ void applyObject(const Cairo::RefPtr context); private: /** rectangelist for added borders */ typedef std::list CairoRectangleList; double m_x, m_y; double m_width, m_height; double m_padding; std::stringstream m_text; CairoRectangleList m_rectangles; }; /*----------------------------------------------------------------------------*/ /** * creates a document using cairomm */ class CairoDocument : public CairoObject { public: /** valid outout formats for the document */ enum OutputFormat { #ifdef CAIRO_HAS_PDF_SURFACE PDF, #endif }; /** * @brief Default dtor * @param width width of document * @param height height of document */ CairoDocument(const unsigned width, const unsigned height) : m_width(width), m_height(height) {} /** * Default dtor */ ~CairoDocument() {} /** * @brief applies this object to the current context * @param context cairomm context */ void applyObject(const Cairo::RefPtr context) {} /** * @brief generates and saves the current document * @param filename filename for document * @param format output format for document */ bool save(const std::string& filename, const OutputFormat format); /** * @brief get width of current object * @return width of current object */ unsigned width() { return m_width; } /** * @brief get height of current object * @return height of current object */ unsigned height() { return m_height; } private: unsigned m_width, m_height; }; #endif /* vim: set et sw=2 ts=2: */