Otclient  14/8/2020
bitmapfont.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef BITMAPFONT_H
24 #define BITMAPFONT_H
25 
26 #include "declarations.h"
27 #include "texture.h"
28 
31 
33 {
34 public:
35  BitmapFont(const std::string& name) : m_name(name) { }
36 
38  void load(const OTMLNodePtr& fontNode);
39 
41  void drawText(const std::string& text, const Point& startPos);
42 
44  void drawText(const std::string& text, const Rect& screenCoords, Fw::AlignmentFlag align = Fw::AlignTopLeft);
45 
46  void calculateDrawTextCoords(CoordsBuffer& coordsBuffer, const std::string& text, const Rect& screenCoords, Fw::AlignmentFlag align = Fw::AlignTopLeft);
47 
49  const std::vector<Point>& calculateGlyphsPositions(const std::string& text,
51  Size* textBoxSize = nullptr);
52 
54  Size calculateTextRectSize(const std::string& text);
55 
56  std::string wrapText(const std::string& text, int maxWidth);
57 
58  std::string getName() { return m_name; }
59  int getGlyphHeight() { return m_glyphHeight; }
60  const Rect* getGlyphsTextureCoords() { return m_glyphsTextureCoords; }
61  const Size* getGlyphsSize() { return m_glyphsSize; }
62  const TexturePtr& getTexture() { return m_texture; }
63  int getYOffset() { return m_yOffset; }
64  Size getGlyphSpacing() { return m_glyphSpacing; }
65 
66 private:
68  void calculateGlyphsWidthsAutomatically(const ImagePtr& image, const Size& glyphSize);
69 
70  std::string m_name;
71  int m_glyphHeight;
72  int m_firstGlyph;
73  int m_yOffset;
74  Size m_glyphSpacing;
75  TexturePtr m_texture;
76  Rect m_glyphsTextureCoords[256];
77  Size m_glyphsSize[256];
78 };
79 
80 
81 #endif
82 
BitmapFont::load
void load(const OTMLNodePtr &fontNode)
Load font from otml node.
Definition: bitmapfont.cpp:30
BitmapFont::calculateTextRectSize
Size calculateTextRectSize(const std::string &text)
Simulate render and calculate text size.
Definition: bitmapfont.cpp:256
TRect< int >
BitmapFont::BitmapFont
BitmapFont(const std::string &name)
Definition: bitmapfont.h:35
Fw::AlignTopLeft
@ AlignTopLeft
Definition: const.h:200
coordsbuffer.h
declarations.h
BitmapFont
Definition: bitmapfont.h:32
BitmapFont::wrapText
std::string wrapText(const std::string &text, int maxWidth)
Definition: bitmapfont.cpp:293
BitmapFont::getGlyphSpacing
Size getGlyphSpacing()
Definition: bitmapfont.h:64
BitmapFont::calculateDrawTextCoords
void calculateDrawTextCoords(CoordsBuffer &coordsBuffer, const std::string &text, const Rect &screenCoords, Fw::AlignmentFlag align=Fw::AlignTopLeft)
Definition: bitmapfont.cpp:96
BitmapFont::getTexture
const TexturePtr & getTexture()
Definition: bitmapfont.h:62
Fw::AlignmentFlag
AlignmentFlag
Definition: const.h:192
BitmapFont::drawText
void drawText(const std::string &text, const Point &startPos)
Simple text render starting at startPos.
Definition: bitmapfont.cpp:80
BitmapFont::getGlyphsTextureCoords
const Rect * getGlyphsTextureCoords()
Definition: bitmapfont.h:60
BitmapFont::getName
std::string getName()
Definition: bitmapfont.h:58
declarations.h
texture.h
BitmapFont::getYOffset
int getYOffset()
Definition: bitmapfont.h:63
stdext::shared_object_ptr< OTMLNode >
BitmapFont::calculateGlyphsPositions
const std::vector< Point > & calculateGlyphsPositions(const std::string &text, Fw::AlignmentFlag align=Fw::AlignTopLeft, Size *textBoxSize=nullptr)
Calculate glyphs positions to use on render, also calculates textBoxSize if wanted.
Definition: bitmapfont.cpp:172
CoordsBuffer
Definition: coordsbuffer.h:29
BitmapFont::getGlyphHeight
int getGlyphHeight()
Definition: bitmapfont.h:59
TPoint< int >
TSize< int >
BitmapFont::getGlyphsSize
const Size * getGlyphsSize()
Definition: bitmapfont.h:61
stdext::shared_object
Definition: shared_object.h:41