1 //
2 //  SuperTuxKart - a fun racing game with go-kart
3 //  Copyright (C) 2016 SuperTuxKart-Team
4 //
5 //  This program is free software; you can redistribute it and/or
6 //  modify it under the terms of the GNU General Public License
7 //  as published by the Free Software Foundation; either version 3
8 //  of the License, or (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 
19 #include "guiengine/scalable_font.hpp"
20 
21 #include "font/font_manager.hpp"
22 #include "font/font_settings.hpp"
23 #include "font/font_with_face.hpp"
24 
25 namespace irr
26 {
27 namespace gui
28 {
29 // ----------------------------------------------------------------------------
ScalableFont(FontWithFace * face)30 ScalableFont::ScalableFont(FontWithFace* face)
31 {
32     m_face = face;
33     m_font_settings = new FontSettings();
34 }   // ScalableFont
35 
36 // ----------------------------------------------------------------------------
~ScalableFont()37 ScalableFont::~ScalableFont()
38 {
39     delete m_font_settings;
40 }   // ~ScalableFont
41 
42 // ----------------------------------------------------------------------------
setShadow(const irr::video::SColor & col)43 void ScalableFont::setShadow(const irr::video::SColor &col)
44 {
45     m_font_settings->setShadow(true);
46     m_font_settings->setShadowColor(col);
47 }   // setShadow
48 
49 // ----------------------------------------------------------------------------
disableShadow()50 void ScalableFont::disableShadow()
51 {
52     m_font_settings->setShadow(false);
53 }   // disableShadow
54 
55 // ----------------------------------------------------------------------------
setBlackBorder(bool enabled)56 void ScalableFont::setBlackBorder(bool enabled)
57 {
58     m_font_settings->setBlackBorder(enabled);
59 }   // setBlackBorder
60 
61 // ----------------------------------------------------------------------------
setColoredBorder(const irr::video::SColor & col)62 void ScalableFont::setColoredBorder(const irr::video::SColor &col)
63 {
64     m_font_settings->setColoredBorder(true);
65     m_font_settings->setBorderColor(col);
66 }   // setColoredBorder
67 
68 // ----------------------------------------------------------------------------
setThinBorder(bool thin)69 void ScalableFont::setThinBorder(bool thin)
70 {
71     m_font_settings->setThinBorder(thin);
72 }   // setThinBorder
73 
74 // ----------------------------------------------------------------------------
disableColoredBorder()75 void ScalableFont::disableColoredBorder()
76 {
77     m_font_settings->setColoredBorder(false);
78 }   // setShadow
79 
80 // ----------------------------------------------------------------------------
setScale(float scale)81 void ScalableFont::setScale(float scale)
82 {
83     m_font_settings->setScale(scale);
84 }   // setScale
85 
86 // ----------------------------------------------------------------------------
getScale() const87 float ScalableFont::getScale() const
88 {
89     return m_font_settings->getScale();
90 }   // getScale
91 
92 // ----------------------------------------------------------------------------
getDimension(const wchar_t * text) const93 core::dimension2d<u32> ScalableFont::getDimension(const wchar_t* text) const
94 {
95     return m_face->getDimension(text, m_font_settings);
96 }   // getDimension
97 
98 // ----------------------------------------------------------------------------
draw(const core::stringw & text,const core::rect<s32> & position,video::SColor color,bool hcenter,bool vcenter,const core::rect<s32> * clip)99 void ScalableFont::draw(const core::stringw& text,
100                         const core::rect<s32>& position, video::SColor color,
101                         bool hcenter, bool vcenter,
102                         const core::rect<s32>* clip)
103 {
104     m_face->drawText(text, position, color, hcenter, vcenter, clip,
105         m_font_settings);
106 }   // draw
107 
108 // ----------------------------------------------------------------------------
draw(const std::vector<GlyphLayout> & gls,const core::rect<s32> & position,video::SColor color,bool hcenter,bool vcenter,const core::rect<s32> * clip)109 void ScalableFont::draw(const std::vector<GlyphLayout>& gls,
110                         const core::rect<s32>& position, video::SColor color,
111                         bool hcenter, bool vcenter,
112                         const core::rect<s32>* clip)
113 {
114     m_face->render(gls, position, color, hcenter, vcenter, clip,
115         m_font_settings);
116 }   // draw
117 
118 // ----------------------------------------------------------------------------
draw(const core::stringw & text,const core::rect<s32> & position,const video::SColor & color,bool hcenter,bool vcenter,const core::rect<s32> * clip,bool ignoreRTL)119 void ScalableFont::draw(const core::stringw& text,
120                         const core::rect<s32>& position,
121                         const video::SColor& color, bool hcenter, bool vcenter,
122                         const core::rect<s32>* clip, bool ignoreRTL)
123 {
124     m_face->drawText(text, position, color, hcenter, vcenter, clip,
125         m_font_settings);
126 }   // draw
127 
128 // ----------------------------------------------------------------------------
drawQuick(const core::stringw & text,const core::rect<s32> & position,const video::SColor color,bool hcenter,bool vcenter,const core::rect<s32> * clip)129 void ScalableFont::drawQuick(const core::stringw& text,
130                              const core::rect<s32>& position,
131                              const video::SColor color, bool hcenter,
132                              bool vcenter, const core::rect<s32>* clip)
133 {
134     m_face->drawTextQuick(text, position, color, hcenter, vcenter, clip,
135         m_font_settings);
136 }   // draw
137 
138 // ----------------------------------------------------------------------------
getCharacterFromPos(const wchar_t * text,s32 pixel_x) const139 s32 ScalableFont::getCharacterFromPos(const wchar_t* text, s32 pixel_x) const
140 {
141     return m_face->getCharacterFromPos(text, pixel_x, m_font_settings);
142 }   // getCharacterFromPos
143 
144 // ----------------------------------------------------------------------------
getSpriteBank() const145 IGUISpriteBank* ScalableFont::getSpriteBank() const
146 {
147     return m_face->getSpriteBank();
148 }   // getSpriteBank
149 
150 // ----------------------------------------------------------------------------
getHeightPerLine() const151 s32 ScalableFont::getHeightPerLine() const
152 {
153     return m_face->getFontMaxHeight() * m_font_settings->getScale();
154 }   // getHeightPerLine
155 
156 // ----------------------------------------------------------------------------
157 /** Convert text to glyph layouts for fast rendering with caching enabled
158  *  If line_data is not null, each broken line u32string will be saved and
159  *  can be used for advanced glyph and text mapping, and cache will be
160  *  disabled.
161  */
initGlyphLayouts(const core::stringw & text,std::vector<GlyphLayout> & gls,std::vector<std::u32string> * line_data)162 void ScalableFont::initGlyphLayouts(const core::stringw& text,
163                                     std::vector<GlyphLayout>& gls,
164                                     std::vector<std::u32string>* line_data)
165 {
166 #ifndef SERVER_ONLY
167     font_manager->initGlyphLayouts(text, gls, line_data);
168 #endif
169 }   // initGlyphLayouts
170 
171 // ----------------------------------------------------------------------------
getInverseShaping() const172 f32 ScalableFont::getInverseShaping() const
173 {
174 #ifndef SERVER_ONLY
175     return m_face->getInverseShaping();
176 #else
177     return 1.0f;
178 #endif
179 }   // getShapingScale
180 
181 } // end namespace gui
182 } // end namespace irr
183