gfx_layout.h

Go to the documentation of this file.
00001 /* $Id: gfx_layout.h 25583 2013-07-10 19:41:31Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #ifndef GFX_LAYOUT_H
00013 #define GFX_LAYOUT_H
00014 
00015 #include "fontcache.h"
00016 #include "gfx_func.h"
00017 #include "core/smallmap_type.hpp"
00018 
00019 #include <map>
00020 #include <string>
00021 
00022 #ifdef WITH_ICU
00023 #include "layout/ParagraphLayout.h"
00024 #define ICU_FONTINSTANCE : public LEFontInstance
00025 #else /* WITH_ICU */
00026 #define ICU_FONTINSTANCE
00027 #endif /* WITH_ICU */
00028 
00033 struct FontState {
00034   FontSize fontsize;       
00035   TextColour cur_colour;   
00036   TextColour prev_colour;  
00037 
00038   FontState() : fontsize(FS_END), cur_colour(TC_INVALID), prev_colour(TC_INVALID) {}
00039   FontState(TextColour colour, FontSize fontsize) : fontsize(fontsize), cur_colour(colour), prev_colour(colour) {}
00040 
00045   inline void SetColour(TextColour c)
00046   {
00047     assert(c >= TC_BLUE && c <= TC_BLACK);
00048     this->prev_colour = this->cur_colour;
00049     this->cur_colour = c;
00050   }
00051 
00053   inline void SetPreviousColour()
00054   {
00055     Swap(this->cur_colour, this->prev_colour);
00056   }
00057 
00062   inline void SetFontSize(FontSize f)
00063   {
00064     this->fontsize = f;
00065   }
00066 };
00067 
00071 class Font ICU_FONTINSTANCE {
00072 public:
00073   FontCache *fc;     
00074   TextColour colour; 
00075 
00076   Font(FontSize size, TextColour colour);
00077 
00078 #ifdef WITH_ICU
00079   /* Implementation details of LEFontInstance */
00080 
00081   le_int32 getUnitsPerEM() const;
00082   le_int32 getAscent() const;
00083   le_int32 getDescent() const;
00084   le_int32 getLeading() const;
00085   float getXPixelsPerEm() const;
00086   float getYPixelsPerEm() const;
00087   float getScaleFactorX() const;
00088   float getScaleFactorY() const;
00089   const void *getFontTable(LETag tableTag) const;
00090   const void *getFontTable(LETag tableTag, size_t &length) const;
00091   LEGlyphID mapCharToGlyph(LEUnicode32 ch) const;
00092   void getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const;
00093   le_bool getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const;
00094 #endif /* WITH_ICU */
00095 };
00096 
00098 typedef SmallMap<int, Font *> FontMap;
00099 
00100 #ifndef WITH_ICU
00101 
00121 class ParagraphLayout {
00122 public:
00124   class VisualRun {
00125     Font *font;       
00126     GlyphID *glyphs;  
00127     float *positions; 
00128     int glyph_count;  
00129 
00130   public:
00131     VisualRun(Font *font, const WChar *chars, int glyph_count, int x);
00132     ~VisualRun();
00133     Font *getFont() const;
00134     int getGlyphCount() const;
00135     const GlyphID *getGlyphs() const;
00136     float *getPositions() const;
00137     int getLeading() const;
00138   };
00139 
00141   class Line : public AutoDeleteSmallVector<VisualRun *, 4> {
00142   public:
00143     int getLeading() const;
00144     int getWidth() const;
00145     int countRuns() const;
00146     VisualRun *getVisualRun(int run) const;
00147   };
00148 
00149   const WChar *buffer_begin; 
00150   const WChar *buffer;       
00151   FontMap &runs;             
00152 
00153   ParagraphLayout(WChar *buffer, int length, FontMap &runs);
00154   void reflow();
00155   Line *nextLine(int max_width);
00156 };
00157 #endif /* !WITH_ICU */
00158 
00164 class Layouter : public AutoDeleteSmallVector<ParagraphLayout::Line *, 4> {
00165 #ifdef WITH_ICU
00166   typedef UChar CharType; 
00167 #else /* WITH_ICU */
00168   typedef WChar CharType; 
00169 #endif /* WITH_ICU */
00170 
00171   size_t AppendToBuffer(CharType *buff, const CharType *buffer_last, WChar c);
00172   ParagraphLayout *GetParagraphLayout(CharType *buff, CharType *buff_end, FontMap &fontMapping);
00173 
00175   struct LineCacheKey {
00176     FontState state_before;  
00177     std::string str;         
00178 
00180     bool operator<(const LineCacheKey &other) const
00181     {
00182       if (this->state_before.fontsize != other.state_before.fontsize) return this->state_before.fontsize < other.state_before.fontsize;
00183       if (this->state_before.cur_colour != other.state_before.cur_colour) return this->state_before.cur_colour < other.state_before.cur_colour;
00184       if (this->state_before.prev_colour != other.state_before.prev_colour) return this->state_before.prev_colour < other.state_before.prev_colour;
00185       return this->str < other.str;
00186     }
00187   };
00189   struct LineCacheItem {
00190     /* Stuff that cannot be freed until the ParagraphLayout is freed */
00191     CharType buffer[DRAW_STRING_BUFFER]; 
00192     FontMap runs;                        
00193 
00194     FontState state_after;   
00195     ParagraphLayout *layout; 
00196 
00197     LineCacheItem() : layout(NULL) {}
00198     ~LineCacheItem() { delete layout; }
00199   };
00200   typedef std::map<LineCacheKey, LineCacheItem> LineCache;
00201   static LineCache *linecache;
00202 
00203   static LineCacheItem &GetCachedParagraphLayout(const char *str, size_t len, const FontState &state);
00204 
00205   typedef SmallMap<TextColour, Font *> FontColourMap;
00206   static FontColourMap fonts[FS_END];
00207   static Font *GetFont(FontSize size, TextColour colour);
00208 
00209 public:
00210   Layouter(const char *str, int maxw = INT32_MAX, TextColour colour = TC_FROMSTRING, FontSize fontsize = FS_NORMAL);
00211   Dimension GetBounds();
00212 
00213   static void ResetFontCache(FontSize size);
00214   static void ResetLineCache();
00215   static void ReduceLineCache();
00216 };
00217 
00218 #endif /* GFX_LAYOUT_H */