1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
#pragma once
#include "globalincs/pstypes.h"
#include <memory>
namespace font {
/**
* @struct TrueTypeFontData
*
* @brief True type font data to save data for already read fonts.
*
* Used to store the data of a true type font which can be used by multiple FTFont objects which are all using the same data.
*
* @author m!m
* @date 24.11.2011
*/
struct TrueTypeFontData {
size_t size; //<! Size of allocated memory
std::unique_ptr<ubyte[]> data; //<! Allocated font data
TrueTypeFontData() : size(0) { }
TrueTypeFontData(TrueTypeFontData &&other) noexcept {
std::swap(data, other.data);
size = other.size;
}
TrueTypeFontData &operator=(TrueTypeFontData &&other) noexcept {
std::swap(data, other.data);
size = other.size;
return *this;
}
TrueTypeFontData(const TrueTypeFontData &) = delete;
TrueTypeFontData &operator=(const TrueTypeFontData &) = delete;
};
// Forward declarations
class FSFont;
class NVGFont;
class VFNTFont;
struct font;
/**
* @brief Manages the fonts used by FreeSpace
*
* This class is responsible for the creation, management and disposal of fonts used
* by the FreeSpace engine. It contains static functions to load a specific font type
* and functions to set or get the current font and can be used to retrieve a font by
* name or index.
*
* @warning Don't @c delete a font which was retrieved from this class as they are managed internally
*/
class FontManager {
public:
/**
* @brief Returns a pointer to the font with the specified name
*
* Searches the internal list of fonts for a font with the name @c name and returns it to the caller.
* If the specified name could not be found then @c NULL is returned instead.
*
* @param name The name that should be searched for
* @return The font pointer or @c NULL when font could not be found.
*/
static FSFont *getFont(const SCP_string &name);
/**
* @brief Returns a pointer to the font with the specified filename
*
* Searches the internal list of fonts for the _first_ font with the filename and returns it to the caller.
* If the specified name could not be found then nullptr is returned instead.
*
* @param filename The filename that should be searched for
* @return The font pointer or nullptr when font could not be found.
*/
static FSFont* getFontByFilename(const SCP_string &filename);
/**
* @brief Returns a pointer to the font at the specified index
*
* Returns the font pointer which is located as the specified index or @c NULL when the specified index is invald
*
* @param index The index that should be returns
* @return Font pointer
*/
inline static FSFont *getFont(int index) {
Assertion(index >= 0 && index < (int) fonts.size(), "Invalid font index %d given!", index);
return fonts[index].get();
}
/**
* @brief Returns the index of the currently used font
*
* Returns the index the currently active font pointer has.
*
* @return The current font index. 0 by default, -1 when FontManager isn't initialized yet
*/
static int getCurrentFontIndex();
/**
* @brief Returns a pointer to the current font
*
* Returns a pointer of the currently active font.
*
* @return Pointer of currently active font or @c NULL when no font is currently active
*/
static FSFont *getCurrentFont();
/**
* @brief Returns the index of the font with the specified @c name.
*
* @param name The name which should be searched
* @return The index or -1 when font could not be found
*/
static int getFontIndex(const SCP_string &name);
/**
* @brief Returns the index of the specified font pointer
*
* Searches through the internal font vector and returns the index for which @verbatim (*fontIterator) == font @endverbatim
* is true.
*
* @param font The font which should be searched
* @return The index of the font or -1 when font could not be found
*/
static int getFontIndex(FSFont *font);
/**
* @brief Returns the number of fonts currently saved in the manager
* @return The number of fonts
*/
static int numberOfFonts();
/**
* @brief Specifies if the font system is ready to be used for rendering text
* @return @c true if ready (there is a current font), @c false if not
*/
static bool isReady();
/**
* @brief Checks if the specified number is a valid font ID
* @return @c true if the number can be used with #getFont(int), @c false otherwise
*/
static bool isFontNumberValid(int fontNumber);
/**
* @brief Sets the currently active font
* @param font The font which should be used, may not be @c NULL
*
* @warning The integrity of the specified pointer is not checked
*/
static void setCurrentFont(FSFont *font);
/**
* @brief Loads a TrueType font
*
* Loads a TrueType font with the specified @c fileName and initializes it with the specified
* @c size and @c type. The size can be changed later where the type is not changeable.
*
* @param fileName The name of the font file which should be loaded
* @param fontSize The initial size of the font
* @param type The type of the font
* @return A FTGLFont pointer or @c NULL when font could not be loaded
*/
static NVGFont *loadNVGFont(const SCP_string &fileName, float fontSize = 12.0f);
/**
* @brief Loads an old VFNT font
*
* Loads the VFNT font with the specified @c fileName and returns it in form of a VFNTFont pointer
*
* @param fileName The name of the font file
* @return The font pointer or @c null on error
*/
static VFNTFont *loadVFNTFont(const SCP_string &fileName);
/**
* @brief Loads old volition font data
*/
static font *loadFontOld(const SCP_string& name);
/**
* @brief Initializes the font system
*/
static void init();
/**
* @brief Disposes the fonts saved in the class
*
* Cleans up the memory used by the font system and sets the current font to @c NULL
*/
static void close();
private:
FontManager() { };
FontManager(const FontManager &) { };
~FontManager() { }
static SCP_map<SCP_string, TrueTypeFontData> allocatedData;
static SCP_map<SCP_string, std::unique_ptr<font>> vfntFontData;
static SCP_vector<std::unique_ptr<FSFont>> fonts;
static FSFont *currentFont;
};
}
|