Back to index

nux  3.0.0
CairoGraphics.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010, 2011 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #ifndef CAIROGRAPHICS_H
00024 #define CAIROGRAPHICS_H
00025 
00026 #include "cairo/cairo.h"
00027 #include "BitmapFormats.h"
00028 #include "ImageSurface.h"
00029 
00030 #include <stack>
00031 
00032 namespace nux
00033 {
00034 class CairoFontOptions
00035 {
00036 public:
00037   CairoFontOptions()
00038     : font_options_(::cairo_font_options_create())
00039     {}
00040 
00041   ~CairoFontOptions()
00042   {
00043     ::cairo_font_options_destroy(font_options_);
00044   }
00045 
00046   operator cairo_font_options_t*()
00047   {
00048     return font_options_;
00049   }
00050 
00051 private:
00052   cairo_font_options_t* font_options_;
00053 };
00054 
00055 
00057 
00060   class CairoGraphics
00061   {
00062   public:
00063     CairoGraphics(cairo_format_t format, int width, int height);
00064     ~CairoGraphics();
00065 
00067 
00071     cairo_t *GetContext();
00072 
00074 
00078     cairo_t *GetInternalContext();
00079 
00080     cairo_surface_t* GetSurface();
00082 
00086     NBitmapData *GetBitmap();
00087 
00088     int GetWidth() const;
00089     int GetHeight() const;
00090 
00091     bool PushState();
00092     bool PopState();
00093 
00094     bool ClearCanvas();
00095 
00096     bool ClearRect(double x, double y, double w, double h);
00097 
00098     bool DrawLine(double x0, double y0, double x1, double y1, double width, const Color &c);
00099 
00100     void TranslateCoordinates(double dx, double dy);
00101 
00102     bool DrawFilledRect(double x, double y, double w, double h, const Color &c);
00103 
00104     bool DrawCanvas(double x, double y, CairoGraphics *cg);
00105 
00106     bool DrawRoundedRectangle(cairo_t* cr,
00107                                double   aspect,
00108                                double   x,
00109                                double   y,
00110                                double   cornerRadius,
00111                                double   width,
00112                                double   height,
00113                                bool     align = false);
00114 
00115     bool BlurSurface(unsigned int radius, cairo_surface_t* surf = NULL);
00116 
00117     bool IntersectRectClipRegion(double x, double y, double w, double h);
00118 
00119     bool IntersectGeneralClipRegion(std::list<Rect> &region);
00120 
00124   enum Alignment {
00125     ALIGN_LEFT,
00126     ALIGN_CENTER,
00127     ALIGN_RIGHT,
00128     ALIGN_JUSTIFY
00129   };
00130 
00134   enum VAlignment {
00135     VALIGN_TOP,
00136     VALIGN_MIDDLE,
00137     VALIGN_BOTTOM
00138   };
00139 
00143   enum Trimming {
00144     TRIMMING_NONE,
00145     TRIMMING_CHARACTER,
00146     TRIMMING_WORD,
00147     TRIMMING_CHARACTER_ELLIPSIS,
00148     TRIMMING_WORD_ELLIPSIS,
00149     TRIMMING_PATH_ELLIPSIS
00150   };
00151 
00155   enum TextFlag {
00156     TEXT_FLAGS_NONE = 0,
00157     TEXT_FLAGS_UNDERLINE = 1,
00158     TEXT_FLAGS_STRIKEOUT = 2,
00159     TEXT_FLAGS_WORDWRAP = 4
00160   };
00161 
00162   private:
00164     cairo_format_t m_surface_format;
00166     cairo_surface_t *_cairo_surface;
00167 
00168     cairo_t * _cr;
00169     int _width; 
00170     int _height; 
00171 
00172     double _zoom;
00173     float _opacity;
00174     std::stack<float> _opacity_stack;
00175   };
00176 
00177 }
00178 
00179 #endif // CAIROGRAPHICS_H
00180