ManaPlus
font.h
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2017 The ManaPlus Developers
6  * Copyright (C) 2009 Aethyra Development Team
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 /* _______ __ __ __ ______ __ __ _______ __ __
25  * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
26  * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
27  * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
28  * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
29  * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
30  * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
31  *
32  * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
33  *
34  *
35  * Per Larsson a.k.a finalman
36  * Olof Naessén a.k.a jansem/yakslem
37  *
38  * Visit: http://guichan.sourceforge.net
39  *
40  * License: (BSD)
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  * notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  * notice, this list of conditions and the following disclaimer in
48  * the documentation and/or other materials provided with the
49  * distribution.
50  * 3. Neither the name of Guichan nor the names of its contributors may
51  * be used to endorse or promote products derived from this software
52  * without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
60  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
61  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
62  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
63  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65  */
66 
67 #ifndef GUI_FONTS_FONT_H
68 #define GUI_FONTS_FONT_H
69 
71 
72 PRAGMA48(GCC diagnostic push)
73 PRAGMA48(GCC diagnostic ignored "-Wshadow")
74 #include <SDL_ttf.h>
75 PRAGMA48(GCC diagnostic pop)
76 
77 #include "localconsts.h"
78 
79 class Graphics;
80 
81 const unsigned int CACHES_NUMBER = 256;
82 
88 class Font final
89 {
90  public:
91  Font(std::string filename,
92  int size,
93  const int style);
94 
96 
97  ~Font();
98 
99  void loadFont(std::string filename,
100  const int size,
101  const int style) restrict2;
102 
103  int getWidth(const std::string &text) const restrict2 A_WARN_UNUSED;
104 
105  int getHeight() const restrict2 A_WARN_UNUSED;
106 
107  const TextChunkList *getCache() const restrict2 noexcept2
108  A_CONST A_WARN_UNUSED;
109 
113  void drawString(Graphics *restrict const graphics,
114  Color col,
115  const Color &restrict col2,
116  const std::string &restrict text,
117  const int x,
118  const int y) restrict2 A_NONNULL(2);
119 
120  void clear() restrict2;
121 
122  void doClean() restrict2;
123 
124  void slowLogic(const int rnd) restrict2;
125 
126  unsigned int getCreateCounter() const restrict2 noexcept2 A_WARN_UNUSED
127  { return mCreateCounter; }
128 
129  unsigned int getDeleteCounter() const restrict2 noexcept2 A_WARN_UNUSED
130  { return mDeleteCounter; }
131 
132  int getStringIndexAt(const std::string &restrict text,
133  const int x) const restrict2 A_WARN_UNUSED;
134 
135  void generate(TextChunk &restrict chunk) restrict2;
136 
137  void insertChunk(TextChunk *const chunk) restrict2;
138 
139  static bool mSoftMode;
140 
141  private:
142  static TTF_Font *openFont(const char *restrict const name,
143  const int size);
144 
145  TTF_Font *restrict mFont;
146  unsigned int mCreateCounter;
147  unsigned int mDeleteCounter;
148 
149  // Word surfaces cache
150  time_t mCleanTime;
152 };
153 
154 #ifdef UNITTESTS
155 extern int textChunkCnt;
156 #endif // UNITTESTS
157 
158 #endif // GUI_FONTS_FONT_H
static bool mSoftMode
Definition: font.h:139
#define A_DELETE_COPY(func)
Definition: localconsts.h:52
unsigned int getCreateCounter() const
Definition: font.h:126
Font(std::string filename, int size, const int style)
Definition: font.cpp:105
Definition: font.h:88
void insertChunk(TextChunk *const chunk)
Definition: font.cpp:476
static TTF_Font * openFont(const char *const name, const int size)
Definition: font.cpp:190
void doClean()
Definition: font.cpp:365
#define A_WARN_UNUSED
Definition: localconsts.h:172
#define final
Definition: localconsts.h:45
void clear()
Definition: font.cpp:247
#define A_NONNULL(...)
Definition: localconsts.h:179
~Font()
Definition: font.cpp:176
TextChunkList mCache[CACHES_NUMBER]
Definition: font.h:151
int getStringIndexAt(const std::string &text, const int x) const
Definition: font.cpp:411
unsigned int mCreateCounter
Definition: font.h:146
void loadFont(std::string filename, const int size, const int style)
Definition: font.cpp:218
void drawString(Graphics *const graphics, Color col, const Color &col2, const std::string &text, const int x, const int y)
Definition: font.cpp:253
TTF_Font * mFont
Definition: font.h:145
const TextChunkList * getCache() const A_CONST
Definition: font.cpp:423
#define PRAGMA48(str)
Definition: localconsts.h:214
void slowLogic(const int rnd)
Definition: font.cpp:318
unsigned int mDeleteCounter
Definition: font.h:147
int getWidth(const std::string &text) const
Definition: font.cpp:333
unsigned int getDeleteCounter() const
Definition: font.h:129
const unsigned int CACHES_NUMBER
Definition: font.h:81
Definition: color.h:74
#define restrict
Definition: localconsts.h:176
int getHeight() const
Definition: font.cpp:360
time_t mCleanTime
Definition: font.h:150
#define noexcept2
Definition: localconsts.h:49
#define restrict2
Definition: localconsts.h:177
void generate(TextChunk &chunk)
Definition: font.cpp:428