gfx_layout.h

Go to the documentation of this file.
00001 /* $Id: gfx_layout.h 26032 2013-11-17 17:31:51Z 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 
00103 class ParagraphLayouter {
00104 public:
00105   virtual ~ParagraphLayouter() {}
00106 
00108   class VisualRun {
00109   public:
00110     virtual ~VisualRun() {}
00111     virtual const Font *GetFont() const = 0;
00112     virtual int GetGlyphCount() const = 0;
00113     virtual const GlyphID *GetGlyphs() const = 0;
00114     virtual const float *GetPositions() const = 0;
00115     virtual int GetLeading() const = 0;
00116     virtual const int *GetGlyphToCharMap() const = 0;
00117   };
00118 
00120   class Line {
00121   public:
00122     virtual ~Line() {}
00123     virtual int GetLeading() const = 0;
00124     virtual int GetWidth() const = 0;
00125     virtual int CountRuns() const = 0;
00126     virtual const VisualRun *GetVisualRun(int run) const = 0;
00127     virtual int GetInternalCharLength(WChar c) const = 0;
00128   };
00129 
00130   virtual void Reflow() = 0;
00131   virtual const Line *NextLine(int max_width) = 0;
00132 };
00133 
00139 class Layouter : public AutoDeleteSmallVector<const ParagraphLayouter::Line *, 4> {
00140   const char *string; 
00141 
00143   struct LineCacheKey {
00144     FontState state_before;  
00145     std::string str;         
00146 
00148     bool operator<(const LineCacheKey &other) const
00149     {
00150       if (this->state_before.fontsize != other.state_before.fontsize) return this->state_before.fontsize < other.state_before.fontsize;
00151       if (this->state_before.cur_colour != other.state_before.cur_colour) return this->state_before.cur_colour < other.state_before.cur_colour;
00152       if (this->state_before.prev_colour != other.state_before.prev_colour) return this->state_before.prev_colour < other.state_before.prev_colour;
00153       return this->str < other.str;
00154     }
00155   };
00156 public:
00158   struct LineCacheItem {
00159     /* Stuff that cannot be freed until the ParagraphLayout is freed */
00160     void *buffer;              
00161     FontMap runs;              
00162 
00163     FontState state_after;     
00164     ParagraphLayouter *layout; 
00165 
00166     LineCacheItem() : buffer(NULL), layout(NULL) {}
00167     ~LineCacheItem() { delete layout; free(buffer); }
00168   };
00169 private:
00170   typedef std::map<LineCacheKey, LineCacheItem> LineCache;
00171   static LineCache *linecache;
00172 
00173   static LineCacheItem &GetCachedParagraphLayout(const char *str, size_t len, const FontState &state);
00174 
00175   typedef SmallMap<TextColour, Font *> FontColourMap;
00176   static FontColourMap fonts[FS_END];
00177 public:
00178   static Font *GetFont(FontSize size, TextColour colour);
00179 
00180   Layouter(const char *str, int maxw = INT32_MAX, TextColour colour = TC_FROMSTRING, FontSize fontsize = FS_NORMAL);
00181   Dimension GetBounds();
00182   Point GetCharPosition(const char *ch) const;
00183   const char *GetCharAtPosition(int x) const;
00184 
00185   static void ResetFontCache(FontSize size);
00186   static void ResetLineCache();
00187   static void ReduceLineCache();
00188 };
00189 
00190 #endif /* GFX_LAYOUT_H */