ScummVM API documentation
font.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef ULTIMA8_GFX_FONTS_FONT_H
23 #define ULTIMA8_GFX_FONTS_FONT_H
24 
25 #include "ultima/shared/std/containers.h"
26 #include "ultima/shared/std/string.h"
27 #include "ultima/ultima8/misc/rect.h"
28 #include "ultima/ultima8/misc/encoding.h"
29 
30 namespace Ultima {
31 namespace Ultima8 {
32 
33 class RenderedText;
34 
36  Std::string _text;
37  Rect _dims;
38  Std::string::size_type _cursor;
39 };
40 
41 class Font {
42 public:
43  Font();
44  virtual ~Font();
45 
46  enum TextAlign {
47  TEXT_LEFT,
48  TEXT_CENTER,
49  TEXT_RIGHT
50  };
51 
53  virtual int getHeight() = 0;
54 
56  virtual int getBaseline() = 0;
57 
59  virtual int getBaselineSkip() = 0;
60 
65  virtual void getStringSize(const Std::string &text,
66  int32 &width, int32 &height) = 0;
67 
77  virtual RenderedText *renderText(const Std::string &text,
78  unsigned int &remaining, int32 width = 0, int32 height = 0,
79  TextAlign align = TEXT_LEFT, bool u8specials = false,
80  bool pagebreaks = false,
81  Std::string::size_type cursor = Std::string::npos) = 0;
82 
93  virtual void getTextSize(const Std::string &text,
94  int32 &resultwidth, int32 &resultheight, unsigned int &remaining,
95  int32 width = 0, int32 height = 0, TextAlign align = TEXT_LEFT,
96  bool u8specials = false, bool pagebreaks = false);
97 
98  void setHighRes(bool hr) {
99  _highRes = hr;
100  }
101  bool isHighRes() const {
102  return _highRes;
103  }
104 
105 protected:
106  bool _highRes;
107 
108  struct Traits {
109  static bool isSpace(Std::string::const_iterator &i, bool u8specials) {
110  char c = *i;
111  return (c == ' ' || c == '\t' || c == '\n' || c == '\r' ||
112  (u8specials && (c == '%' || c == '~' || c == '*' || c == '^')));
113  }
114  static bool isTab(Std::string::const_iterator &i, bool u8specials) {
115  char c = *i;
116  return (c == '\t' ||
117  (u8specials && (c == '\t' || c == '%')));
118  }
119  static bool isBreak(Std::string::const_iterator &i, bool u8specials) {
120  char c = *i;
121  return (c == '\n' ||
122  (u8specials && (c == '\n' || c == '~' || c == '*')));
123  }
124  static bool isPageBreak(Std::string::const_iterator &i, bool u8specials) {
125  char c = *i;
126  return (u8specials && c == '*');
127  }
128  static bool canBreakAfter(Std::string::const_iterator &i);
129  static void advance(Std::string::const_iterator &i) {
130  ++i;
131  }
132  static Std::string::size_type length(const Std::string &t) {
133  return t.size();
134  }
135  static uint32 unicode(Std::string::const_iterator &i) {
136  return encoding[static_cast<uint8>(*i++)];
137  }
138  };
139  struct SJISTraits : public Traits {
140  static bool canBreakAfter(Std::string::const_iterator &i);
141  static void advance(Std::string::const_iterator &i) {
142  // FIXME: this can advance past the end of a malformed string
143  uint8 c = *i;
144  i++;
145  if (c >= 0x80) i++;
146  }
147  static Std::string::size_type length(const Std::string &t) {
148  Std::string::size_type l = 0;
149  Std::string::const_iterator iter = t.begin();
150  while (iter != t.end()) {
151  advance(iter);
152  l++;
153  }
154  return l;
155  }
156  static uint32 unicode(Std::string::const_iterator &i) {
157  uint16 s = static_cast<uint8>(*i);
158  i++;
159  if (s >= 0x80) {
160  uint16 t = static_cast<uint8>(*i++);
161  s |= (t << 8);
162  }
163  return shiftjis_to_unicode(s);
164  }
165  };
166 };
167 
168 template<class T>
169 Std::list<PositionedText> typesetText(Font *font,
170  const Std::string &text, unsigned int &remaining,
171  int32 width, int32 height, Font::TextAlign align,
172  bool u8specials, bool pagebreaks,
173  int32 &resultwidth, int32 &resultheight,
174  Std::string::size_type cursor = Std::string::npos);
175 
176 } // End of namespace Ultima8
177 } // End of namespace Ultima
178 
179 #endif
Definition: font.h:108
Definition: font.h:41
Definition: font.h:139
Definition: rect.h:32
bool isSpace(int c)
Definition: rendered_text.h:30
Definition: detection.h:27
Definition: string.h:30
Definition: containers.h:200
Definition: font.h:35