00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef FONT_H
00020 #define FONT_H
00021
00022 #include <list>
00023 #include "Rect.h"
00024 #include "encoding.h"
00025
00026 class RenderedText;
00027
00028 struct PositionedText {
00029 std::string text;
00030 Pentagram::Rect dims;
00031 std::string::size_type cursor;
00032 };
00033
00034 namespace Pentagram
00035 {
00036
00037 class Font
00038 {
00039 public:
00040 Font();
00041 virtual ~Font();
00042
00043 ENABLE_RUNTIME_CLASSTYPE();
00044
00045 enum TextAlign {
00046 TEXT_LEFT,
00047 TEXT_CENTER,
00048 TEXT_RIGHT
00049 };
00050
00052 virtual int getHeight()=0;
00053
00055 virtual int getBaseline()=0;
00056
00058 virtual int getBaselineSkip()=0;
00059
00064 virtual void getStringSize(const std::string& text,
00065 int& width, int& height)=0;
00066
00075 virtual RenderedText* renderText(const std::string& text,
00076 unsigned int& remaining,
00077 int width=0, int height=0,
00078 TextAlign align=TEXT_LEFT,
00079 bool u8specials=false,
00080 std::string::size_type cursor
00081 =std::string::npos)=0;
00082
00092 virtual void getTextSize(const std::string& text,
00093 int& resultwidth, int& resultheight,
00094 unsigned int& remaining,
00095 int width=0, int height=0,
00096 TextAlign align=TEXT_LEFT, bool u8specials=false);
00097
00098 void setHighRes(bool hr) { highRes = hr; }
00099 bool isHighRes() const { return highRes; }
00100
00101 protected:
00102 bool highRes;
00103
00104
00105 protected:
00106
00107 struct Traits
00108 {
00109 static bool isSpace(std::string::const_iterator& i, bool u8specials) {
00110 char c = *i;
00111 return (c == ' ' || c == '\t' || c == '\n' || c == '\r' ||
00112 (u8specials && (c == '%' || c == '~' || c == '*')));
00113 }
00114 static bool isTab(std::string::const_iterator& i, bool u8specials) {
00115 char c = *i;
00116 return (c == '\t' ||
00117 (u8specials && (c == '\t' || c == '%')));
00118 }
00119 static bool isBreak(std::string::const_iterator& i, bool u8specials) {
00120 char c = *i;
00121 return (c == '\n' ||
00122 (u8specials && (c == '\n' || c == '~' || c == '*')));
00123 }
00124 static bool canBreakAfter(std::string::const_iterator& i);
00125 static void advance(std::string::const_iterator& i) {
00126 ++i;
00127 }
00128 static std::string::size_type length(const std::string& t) {
00129 return t.size();
00130 }
00131 static uint32 unicode(std::string::const_iterator& i) {
00132 return Pentagram::encoding[static_cast<uint8>(*i++)];
00133 }
00134 };
00135 struct SJISTraits : public Traits
00136 {
00137 static bool canBreakAfter(std::string::const_iterator& i);
00138 static void advance(std::string::const_iterator& i) {
00139
00140 uint8 c = *i;
00141 i++;
00142 if (c >= 0x80) i++;
00143 }
00144 static std::string::size_type length(const std::string& t) {
00145 std::string::size_type l = 0;
00146 std::string::const_iterator iter = t.begin();
00147 while (iter != t.end()) {
00148 advance(iter);
00149 l++;
00150 }
00151 return l;
00152 }
00153 static uint32 unicode(std::string::const_iterator& i) {
00154 uint16 s = static_cast<uint8>(*i);
00155 i++;
00156 if (s >= 0x80) {
00157 uint16 t = static_cast<uint8>(*i++);
00158 s |= (t << 8);
00159 }
00160 return Pentagram::shiftjis_to_unicode(s);
00161 }
00162 };
00163 };
00164
00165 }
00166
00167 template<class T>
00168 std::list<PositionedText> typesetText(Pentagram::Font* font,
00169 const std::string& text,
00170 unsigned int& remaining,
00171 int width, int height,
00172 Pentagram::Font::TextAlign align,
00173 bool u8specials,
00174 int& resultwidth, int& resultheight,
00175 std::string::size_type cursor
00176 =std::string::npos);
00177
00178 #endif