Font.hpp
1 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2014 Laurent Gomila (laurent.gom@gmail.com)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
25 #ifndef CPP3DS_FONT_HPP
26 #define CPP3DS_FONT_HPP
27 
29 // Headers
31 #include <cpp3ds/Graphics/Glyph.hpp>
32 #include <cpp3ds/Graphics/Texture.hpp>
33 #include <cpp3ds/Graphics/Rect.hpp>
34 #include <cpp3ds/System/Vector2.hpp>
35 #include <cpp3ds/System/String.hpp>
36 #include <map>
37 #include <string>
38 #include <vector>
39 
40 
41 namespace cpp3ds
42 {
43 class InputStream;
44 
49 class Font
50 {
51 public :
52 
57  struct Info
58  {
59  std::string family;
60  };
61 
62 public :
63 
70  Font();
71 
78  Font(const Font& copy);
79 
86  ~Font();
87 
104  bool loadFromFile(const std::string& filename);
105 
123  bool loadFromMemory(const void* data, std::size_t sizeInBytes);
124 
141  bool loadFromStream(InputStream& stream);
142 
149  const Info& getInfo() const;
150 
161  const Glyph& getGlyph(Uint32 codePoint, unsigned int characterSize, bool bold) const;
162 
179  int getKerning(Uint32 first, Uint32 second, unsigned int characterSize) const;
180 
192  int getLineSpacing(unsigned int characterSize) const;
193 
206  const Texture& getTexture(unsigned int characterSize) const;
207 
216  Font& operator =(const Font& right);
217 
218 private :
219 
224  struct Row
225  {
226  Row(unsigned int rowTop, unsigned int rowHeight) : width(0), top(rowTop), height(rowHeight) {}
227 
228  unsigned int width;
229  unsigned int top;
230  unsigned int height;
231  };
232 
234  // Types
236  typedef std::map<Uint32, Glyph> GlyphTable;
237 
242  struct Page
243  {
244  Page();
245 
246  GlyphTable glyphs;
247  cpp3ds::Texture texture;
248  unsigned int nextRow;
249  std::vector<Row> rows;
250  };
251 
256  void cleanup();
257 
268  Glyph loadGlyph(Uint32 codePoint, unsigned int characterSize, bool bold) const;
269 
280  IntRect findGlyphRect(Page& page, unsigned int width, unsigned int height) const;
281 
290  bool setCurrentSize(unsigned int characterSize) const;
291 
293  // Types
295  typedef std::map<unsigned int, Page> PageTable;
296 
298  // Member data
300  void* m_library;
301  void* m_face;
302  void* m_streamRec;
303  int* m_refCount;
304  Info m_info;
305  mutable PageTable m_pages;
306  mutable std::vector<Uint8> m_pixelBuffer;
307 };
308 
309 }
310 
311 
312 #endif
313 
314 
Class for loading and manipulating character fonts.
Definition: Font.hpp:49
const Glyph & getGlyph(Uint32 codePoint, unsigned int characterSize, bool bold) const
Retrieve a glyph of the font.
const Texture & getTexture(unsigned int characterSize) const
Retrieve the texture containing the loaded glyphs of a certain size.
Abstract class for custom file input streams.
Definition: InputStream.hpp:40
Font & operator=(const Font &right)
Overload of assignment operator.
Holds various information about a font.
Definition: Font.hpp:57
Structure describing a glyph.
Definition: Glyph.hpp:40
Font()
Default constructor.
~Font()
Destructor.
int getLineSpacing(unsigned int characterSize) const
Get the line spacing.
bool loadFromFile(const std::string &filename)
Load the font from a file.
const Info & getInfo() const
Get the font information.
std::string family
The font family.
Definition: Font.hpp:59
bool loadFromStream(InputStream &stream)
Load the font from a custom stream.
some things could easily be broken///////
Definition: Texture.hpp:47
int getKerning(Uint32 first, Uint32 second, unsigned int characterSize) const
Get the kerning offset of two glyphs.
bool loadFromMemory(const void *data, std::size_t sizeInBytes)
Load the font from a file in memory.