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 
71 #include "gui/fonts/textchunklist.h"
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 
84 /**
85  * A wrapper around SDL_ttf for allowing the use of TrueType fonts.
86  *
87  * <b>NOTE:</b> This class initializes SDL_ttf as necessary.
88  */
89 class Font final
90 {
91     public:
92         Font(std::string filename,
93              int size,
94              const int style);
95 
96         A_DELETE_COPY(Font)
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 
108         const TextChunkList *getCache() const restrict2 noexcept2
109                                       A_CONST A_WARN_UNUSED;
110 
111         /**
112          * @see Font::drawString
113          */
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 
getCreateCounter()127         unsigned int getCreateCounter() const restrict2 noexcept2 A_WARN_UNUSED
128         { return mCreateCounter; }
129 
getDeleteCounter()130         unsigned int getDeleteCounter() const restrict2 noexcept2 A_WARN_UNUSED
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;
152         mutable TextChunkList mCache[CACHES_NUMBER];
153 };
154 
155 #ifdef UNITTESTS
156 extern int textChunkCnt;
157 #endif  // UNITTESTS
158 
159 #endif  // GUI_FONTS_FONT_H
160