Anti-Grain Geometry - AGG (libagg)  2.5
agg-2.5/include/agg_renderer_raster_text.h
Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 // Anti-Grain Geometry (AGG) - Version 2.5
00003 // A high quality rendering engine for C++
00004 // Copyright (C) 2002-2006 Maxim Shemanarev
00005 // Contact: mcseem@antigrain.com
00006 //          mcseemagg@yahoo.com
00007 //          http://antigrain.com
00008 // 
00009 // AGG is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU General Public License
00011 // as published by the Free Software Foundation; either version 2
00012 // of the License, or (at your option) any later version.
00013 // 
00014 // AGG is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 // 
00019 // You should have received a copy of the GNU General Public License
00020 // along with AGG; if not, write to the Free Software
00021 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
00022 // MA 02110-1301, USA.
00023 //----------------------------------------------------------------------------
00024 
00025 #ifndef AGG_RENDERER_RASTER_TEXT_INCLUDED
00026 #define AGG_RENDERER_RASTER_TEXT_INCLUDED
00027 
00028 #include "agg_basics.h"
00029 
00030 namespace agg
00031 {
00032 
00033     //==============================================renderer_raster_htext_solid
00034     template<class BaseRenderer, class GlyphGenerator> 
00035     class renderer_raster_htext_solid
00036     {
00037     public:
00038         typedef BaseRenderer ren_type;
00039         typedef GlyphGenerator glyph_gen_type;
00040         typedef typename glyph_gen_type::glyph_rect glyph_rect;
00041         typedef typename ren_type::color_type color_type;
00042 
00043         renderer_raster_htext_solid(ren_type& ren, glyph_gen_type& glyph) :
00044             m_ren(&ren),
00045             m_glyph(&glyph)
00046         {}
00047         void attach(ren_type& ren) { m_ren = &ren; }
00048 
00049         //--------------------------------------------------------------------
00050         void color(const color_type& c) { m_color = c; }
00051         const color_type& color() const { return m_color; }
00052 
00053         //--------------------------------------------------------------------
00054         template<class CharT>
00055         void render_text(double x, double y, const CharT* str, bool flip=false)
00056         {
00057             glyph_rect r;
00058             while(*str)
00059             {
00060                 m_glyph->prepare(&r, x, y, *str, flip);
00061                 if(r.x2 >= r.x1)
00062                 {
00063                     int i;
00064                     if(flip)
00065                     {
00066                         for(i = r.y1; i <= r.y2; i++)
00067                         {
00068                             m_ren->blend_solid_hspan(r.x1, i, (r.x2 - r.x1 + 1),
00069                                                      m_color,
00070                                                      m_glyph->span(r.y2 - i));
00071                         }
00072                     }
00073                     else
00074                     {
00075                         for(i = r.y1; i <= r.y2; i++)
00076                         {
00077                             m_ren->blend_solid_hspan(r.x1, i, (r.x2 - r.x1 + 1),
00078                                                      m_color,
00079                                                      m_glyph->span(i - r.y1));
00080                         }
00081                     }
00082                 }
00083                 x += r.dx;
00084                 y += r.dy;
00085                 ++str;
00086             }
00087         }
00088 
00089     private:
00090         ren_type* m_ren;
00091         glyph_gen_type* m_glyph;
00092         color_type m_color;
00093     };
00094 
00095 
00096 
00097     //=============================================renderer_raster_vtext_solid
00098     template<class BaseRenderer, class GlyphGenerator> 
00099     class renderer_raster_vtext_solid
00100     {
00101     public:
00102         typedef BaseRenderer ren_type;
00103         typedef GlyphGenerator glyph_gen_type;
00104         typedef typename glyph_gen_type::glyph_rect glyph_rect;
00105         typedef typename ren_type::color_type color_type;
00106 
00107         renderer_raster_vtext_solid(ren_type& ren, glyph_gen_type& glyph) :
00108             m_ren(&ren),
00109             m_glyph(&glyph)
00110         {
00111         }
00112 
00113         //--------------------------------------------------------------------
00114         void color(const color_type& c) { m_color = c; }
00115         const color_type& color() const { return m_color; }
00116 
00117         //--------------------------------------------------------------------
00118         template<class CharT>
00119         void render_text(double x, double y, const CharT* str, bool flip=false)
00120         {
00121             glyph_rect r;
00122             while(*str)
00123             {
00124                 m_glyph->prepare(&r, x, y, *str, !flip);
00125                 if(r.x2 >= r.x1)
00126                 {
00127                     int i;
00128                     if(flip)
00129                     {
00130                         for(i = r.y1; i <= r.y2; i++)
00131                         {
00132                             m_ren->blend_solid_vspan(i, r.x1, (r.x2 - r.x1 + 1),
00133                                                      m_color,
00134                                                      m_glyph->span(i - r.y1));
00135                         }
00136                     }
00137                     else
00138                     {
00139                         for(i = r.y1; i <= r.y2; i++)
00140                         {
00141                             m_ren->blend_solid_vspan(i, r.x1, (r.x2 - r.x1 + 1),
00142                                                      m_color,
00143                                                      m_glyph->span(r.y2 - i));
00144                         }
00145                     }
00146                 }
00147                 x += r.dx;
00148                 y += r.dy;
00149                 ++str;
00150             }
00151         }
00152 
00153     private:
00154         ren_type* m_ren;
00155         glyph_gen_type* m_glyph;
00156         color_type m_color;
00157     };
00158 
00159 
00160 
00161 
00162 
00163 
00164     //===================================================renderer_raster_htext
00165     template<class ScanlineRenderer, class GlyphGenerator> 
00166     class renderer_raster_htext
00167     {
00168     public:
00169         typedef ScanlineRenderer ren_type;
00170         typedef GlyphGenerator glyph_gen_type;
00171         typedef typename glyph_gen_type::glyph_rect glyph_rect;
00172 
00173         class scanline_single_span
00174         {
00175         public:
00176             typedef agg::cover_type cover_type;
00177 
00178             //----------------------------------------------------------------
00179             struct const_span
00180             {
00181                 int x;
00182                 unsigned len;
00183                 const cover_type* covers;
00184 
00185                 const_span() {}
00186                 const_span(int x_, unsigned len_, const cover_type* covers_) :
00187                     x(x_), len(len_), covers(covers_) 
00188                 {}
00189             };
00190 
00191             typedef const const_span* const_iterator;
00192 
00193             //----------------------------------------------------------------
00194             scanline_single_span(int x, int y, unsigned len, 
00195                                  const cover_type* covers) :
00196                 m_y(y),
00197                 m_span(x, len, covers)
00198             {}
00199 
00200             //----------------------------------------------------------------
00201             int      y()           const { return m_y; }
00202             unsigned num_spans()   const { return 1;   }
00203             const_iterator begin() const { return &m_span; }
00204 
00205         private:
00206             //----------------------------------------------------------------
00207             int m_y;
00208             const_span m_span;
00209         };
00210 
00211 
00212 
00213         //--------------------------------------------------------------------
00214         renderer_raster_htext(ren_type& ren, glyph_gen_type& glyph) :
00215             m_ren(&ren),
00216             m_glyph(&glyph)
00217         {
00218         }
00219 
00220 
00221         //--------------------------------------------------------------------
00222         template<class CharT>
00223         void render_text(double x, double y, const CharT* str, bool flip=false)
00224         {
00225             glyph_rect r;
00226             while(*str)
00227             {
00228                 m_glyph->prepare(&r, x, y, *str, flip);
00229                 if(r.x2 >= r.x1)
00230                 {
00231                     m_ren->prepare();
00232                     int i;
00233                     if(flip)
00234                     {
00235                         for(i = r.y1; i <= r.y2; i++)
00236                         {
00237                             m_ren->render(
00238                                 scanline_single_span(r.x1, 
00239                                                      i, 
00240                                                      (r.x2 - r.x1 + 1),
00241                                                      m_glyph->span(r.y2 - i)));
00242                         }
00243                     }
00244                     else
00245                     {
00246                         for(i = r.y1; i <= r.y2; i++)
00247                         {
00248                             m_ren->render(
00249                                 scanline_single_span(r.x1, 
00250                                                      i, 
00251                                                      (r.x2 - r.x1 + 1),
00252                                                      m_glyph->span(i - r.y1)));
00253                         }
00254                     }
00255                 }
00256                 x += r.dx;
00257                 y += r.dy;
00258                 ++str;
00259             }
00260         }
00261 
00262     private:
00263         ren_type* m_ren;
00264         glyph_gen_type* m_glyph;
00265     };
00266 
00267 
00268 
00269 
00270 }
00271 
00272 #endif
00273 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines