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