TextWriter.h

Go to the documentation of this file.
00001 
00002 //Luke Lenhart, 2010
00003 //See /docs/License.txt for details on how this code may be used.
00004 
00005 #pragma once
00006 
00007 #include "../base/Setup.h"
00008 
00009 #ifdef MPMA_COMPILE_GFX
00010 
00011 #include "../base/Types.h"
00012 #include "../base/Vary.h"
00013 #include "../gfxsetup/GL.h"
00014 #include "../gfxsetup/Extensions.h"
00015 #include "Texture.h"
00016 #include "Vertex.h"
00017 #include <string>
00018 
00019 #ifdef GFX_USES_IMAGEMAGICK
00020 
00021 namespace
00022 {
00023     class FontCacheEntry;
00024 }
00025 
00026 namespace GFX
00027 {
00029     enum FontType
00030     {
00031         VARIABLE_SERIF   =0x0000, 
00032         VARIABLE_SANSERIF=0x0001, 
00033         FIXED_SERIF      =0x0100, 
00034         //FIXED_SANSERIF   =0x0101
00035     };
00036 
00038     struct TextColor
00039     {
00040         uint8 Red; 
00041         uint8 Green; 
00042         uint8 Blue; 
00043         uint8 Opacity; 
00044 
00046         TextColor(): Red(255), Green(255), Blue(255), Opacity(255) {}
00048         TextColor(uint8 red, uint8 green, uint8 blue, uint8 opacity=255): Red(red), Green(green), Blue(blue), Opacity(opacity) {}
00049     };
00050 
00052     class EncodedText
00053     {
00054     public:
00056         inline EncodedText() {}
00058         inline EncodedText(const std::string &plainText)
00059         {
00060             AppendPlainText(plainText);
00061         }
00062 
00064         inline void Clear()
00065         {
00066             data.clear();
00067         }
00068 
00070         void AppendColorChange(uint8 red, uint8 green, uint8 blue, uint8 opacity=255);
00072         inline void AppendColorChange(const TextColor &color)
00073         {
00074             AppendColorChange(color.Red, color.Green, color.Blue, color.Opacity);
00075         }
00077         void AppendPlainText(const std::string &text);
00078 
00080         EncodedText& operator<<(const char *sstring);
00082         EncodedText& operator<<(const std::string &sstring);
00084         EncodedText& operator<<(const MPMA::Vary &vstring);
00086         EncodedText& operator<<(const TextColor &color);
00088         EncodedText& operator<<(const EncodedText &et);
00089     private:
00090         std::vector<uint8> data;
00091 
00092         friend class TextWriter;
00093     };
00094 
00096     class TextWriter
00097     {
00098     public:
00100         static void ClearFontCache();
00101 
00102         // -- Step 1: Convert the text to an internal image.
00103 
00105         EncodedText Text;
00106 
00108         nuint FontSize;
00109 
00111         FontType TextFont;
00112 
00114         nuint TextWidth;
00115 
00117         bool CreateTextImage();
00118 
00120         inline nuint GetTextHeight() const { return textHeight; }
00121 
00122         // -- Step 2: Convert the internal image to a texture that can be used for rendering.
00123 
00125         nuint ViewWindowHeight;
00126 
00128         nuint ViewWindowScroll;
00129 
00131         inline nuint GetMaxViewWindowScroll() const
00132         {
00133             nsint actualViewHeight=(nsint)GetActualViewHeight();
00134             nsint extraHeightPixels=textHeight-actualViewHeight;
00135             if (extraHeightPixels<0)
00136                 extraHeightPixels=0;
00137             return (nuint)extraHeightPixels;
00138         }
00139 
00141         bool DebugDrawBorder;
00142 
00144         bool CreateTexture();
00145 
00147         inline nuint GetTextureWidth();
00149         inline nuint GetTextureHeight();
00150 
00151         // -- Step 3: Render the texture using these properties.
00152 
00154         inline const Texture2D& GetTexture() const { return texture; }
00155 
00157         inline float GetTexCoordUMin() const { return renderUMin; }
00159         inline float GetTexCoordUMax() const { return renderUMax; }
00161         inline float GetTexCoordVMin() const { return renderVMin; }
00163         inline float GetTexCoordVMax() const { return renderVMax; }
00164 
00166         inline nuint GetRenderWidth() const { return renderWidth; }
00168         inline nuint GetRenderHeight() const { return renderHeight; }
00169 
00171         void RenderTexture(float leftX, float topY);
00172 
00173         // --
00174 
00175         //
00176         TextWriter(); 
00177         TextWriter(const TextWriter &other); 
00178         TextWriter(TextWriter &&other); 
00179 
00180     private:
00181         void Init();
00182         void CopyValues(const TextWriter &other, bool copyMovableValues);
00183 
00184         std::vector<uint32> textPixels;
00185         nuint textHeight;
00186         nuint sourceTextWidth;
00187 
00188         struct CharacterRenderListEntry
00189         {
00190             nuint CharacterIndex;
00191             nuint X;
00192             nuint Y;
00193             uint32 Color;
00194         };
00195         nuint ComputeWrappedCharacterRendering(const FontCacheEntry &font, std::vector<CharacterRenderListEntry> &outRenderList);
00196         void ComputeUnboundedCharacterRendering(const FontCacheEntry &font, std::vector<CharacterRenderListEntry> &outRenderList, nuint &outTextWidth, nuint &outTextHeight);
00197 
00198         nuint textureWidth, textureHeight;
00199 
00200         Texture2D texture;
00201         VertexBuffer vb;
00202         InterleavedVertexFormat vbFormat;
00203         void InitGL();
00204         void SetVB(float leftX, float bottomY);
00205 
00206         float renderUMin, renderUMax;
00207         float renderVMin, renderVMax;
00208         nuint renderWidth, renderHeight;
00209 
00210         inline nuint GetActualViewHeight() const
00211         {
00212             nuint actualViewHeight=ViewWindowHeight;
00213 
00214             if (actualViewHeight==0)
00215                 actualViewHeight=GetTextHeight();
00216 
00217             if (actualViewHeight>GetTextHeight())
00218                 actualViewHeight=GetTextHeight();
00219 
00220             return actualViewHeight;
00221         }
00222 
00223         std::vector<uint32> texturePixels; //temporary cache during CreateTexture, doesn't need persisted
00224     };
00225 }
00226 
00227 #endif //#ifdef GFX_USES_IMAGEMAGICK
00228 
00229 #endif //#ifdef MPMA_COMPILE_GFX
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends