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 "common/rect.h"
26 
27 #include "ultima/shared/std/containers.h"
28 #include "ultima/shared/std/string.h"
29 #include "ultima/ultima8/misc/encoding.h"
30 
31 namespace Ultima {
32 namespace Ultima8 {
33 
34 class RenderedText;
35 
37  Std::string _text;
38  Common::Rect32 _dims;
39  Std::string::size_type _cursor;
40 };
41 
42 class Font {
43 public:
44  Font();
45  virtual ~Font();
46 
47  enum TextAlign {
48  TEXT_LEFT,
49  TEXT_CENTER,
50  TEXT_RIGHT
51  };
52 
54  virtual int getHeight() = 0;
55 
57  virtual int getBaseline() = 0;
58 
60  virtual int getBaselineSkip() = 0;
61 
66  virtual void getStringSize(const Std::string &text,
67  int32 &width, int32 &height) = 0;
68 
78  virtual RenderedText *renderText(const Std::string &text,
79  unsigned int &remaining, int32 width = 0, int32 height = 0,
80  TextAlign align = TEXT_LEFT, bool u8specials = false,
81  bool pagebreaks = false,
82  Std::string::size_type cursor = Std::string::npos) = 0;
83 
94  virtual void getTextSize(const Std::string &text,
95  int32 &resultwidth, int32 &resultheight, unsigned int &remaining,
96  int32 width = 0, int32 height = 0, TextAlign align = TEXT_LEFT,
97  bool u8specials = false, bool pagebreaks = false);
98 
99  void setHighRes(bool hr) {
100  _highRes = hr;
101  }
102  bool isHighRes() const {
103  return _highRes;
104  }
105 
106 protected:
107  bool _highRes;
108 
109  struct Traits {
110  static bool isSpace(Std::string::const_iterator &i, bool u8specials) {
111  char c = *i;
112  return (c == ' ' || c == '\t' || c == '\n' || c == '\r' ||
113  (u8specials && (c == '%' || c == '~' || c == '*' || c == '^')));
114  }
115  static bool isTab(Std::string::const_iterator &i, bool u8specials) {
116  char c = *i;
117  return (c == '\t' ||
118  (u8specials && (c == '\t' || c == '%')));
119  }
120  static bool isBreak(Std::string::const_iterator &i, bool u8specials) {
121  char c = *i;
122  return (c == '\n' ||
123  (u8specials && (c == '\n' || c == '~' || c == '*')));
124  }
125  static bool isPageBreak(Std::string::const_iterator &i, bool u8specials) {
126  char c = *i;
127  return (u8specials && c == '*');
128  }
129  static bool canBreakAfter(Std::string::const_iterator &i);
130  static void advance(Std::string::const_iterator &i) {
131  ++i;
132  }
133  static Std::string::size_type length(const Std::string &t) {
134  return t.size();
135  }
136  static uint32 unicode(Std::string::const_iterator &i) {
137  return encoding[static_cast<uint8>(*i++)];
138  }
139  };
140  struct SJISTraits : public Traits {
141  static bool canBreakAfter(Std::string::const_iterator &i);
142  static void advance(Std::string::const_iterator &i) {
143  // FIXME: this can advance past the end of a malformed string
144  uint8 c = *i;
145  i++;
146  if (c >= 0x80) i++;
147  }
148  static Std::string::size_type length(const Std::string &t) {
149  Std::string::size_type l = 0;
150  Std::string::const_iterator iter = t.begin();
151  while (iter != t.end()) {
152  advance(iter);
153  l++;
154  }
155  return l;
156  }
157  static uint32 unicode(Std::string::const_iterator &i) {
158  uint16 s = static_cast<uint8>(*i);
159  i++;
160  if (s >= 0x80) {
161  uint16 t = static_cast<uint8>(*i++);
162  s |= (t << 8);
163  }
164  return shiftjis_to_unicode(s);
165  }
166  };
167 };
168 
169 template<class T>
170 Std::list<PositionedText> typesetText(Font *font,
171  const Std::string &text, unsigned int &remaining,
172  int32 width, int32 height, Font::TextAlign align,
173  bool u8specials, bool pagebreaks,
174  int32 &resultwidth, int32 &resultheight,
175  Std::string::size_type cursor = Std::string::npos);
176 
177 } // End of namespace Ultima8
178 } // End of namespace Ultima
179 
180 #endif
Definition: font.h:109
Definition: font.h:42
Definition: font.h:140
Definition: rect.h:526
bool isSpace(int c)
Definition: rendered_text.h:30
Definition: detection.h:27
Definition: string.h:30
Definition: containers.h:46
Definition: font.h:36