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
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#ifndef _CFONTTEXTURE_H
#define _CFONTTEXTURE_H
#include <string>
#include <memory>
#include "Rendering/Textures/Bitmap.h"
#include "Rendering/Textures/IAtlasAllocator.h"
#include "Rendering/Textures/RowAtlasAlloc.h"
#include "System/UnorderedMap.hpp"
#include "System/UnorderedSet.hpp"
struct FT_FaceRec_;
typedef struct FT_FaceRec_* FT_Face;
class CBitmap;
struct FontFace;
struct IGlyphRect { //FIXME use SRect or float4
IGlyphRect():
x(0),y(0),
w(0),h(0) {
};
IGlyphRect(float _x,float _y,float _w,float _h):
x(_x),y(_y),
w(_w),h(_h) {
};
float x0() const {
return x;
};
float x1() const {
return x+w;
};
float y0() const {
return y;
};
float y1() const {
return y+h;
};
float x,y;
float w,h;
};
struct GlyphInfo {
GlyphInfo()
: advance(0)
, height(0)
, descender(0)
, index(0)
, utf16(0)
, face(NULL)
{ };
IGlyphRect size;
IGlyphRect texCord;
IGlyphRect shadowTexCord;
float advance, height, descender;
unsigned index;
char32_t utf16;
FT_Face face;
};
/**
This class just store glyphs and load new glyphs if requred
It works with image and don't care about rendering these glyphs
It works only and only with UTF32 chars
**/
class CFontTexture
{
public:
static void Update();
static bool GenFontConfig();
public:
CFontTexture(const std::string& fontfile, int size, int outlinesize, float outlineweight);
virtual ~CFontTexture();
public:
int GetSize() const { return fontSize; }
int GetTextureWidth() const { return texWidth; }
int GetTextureHeight() const { return texHeight; }
int GetOutlineWidth() const { return outlineSize; }
float GetOutlineWeight() const { return outlineWeight; }
float GetLineHeight() const { return lineHeight; }
float GetDescender() const { return fontDescender; }
int GetTexture() const { return glyphAtlasTextureID; }
const std::string& GetFamily() const { return fontFamily; }
const std::string& GetStyle() const { return fontStyle; }
const GlyphInfo& GetGlyph(char32_t ch); //< Get or load a glyph
public:
void ReallocAtlases(bool pre);
protected:
void UpdateGlyphAtlasTexture();
private:
void CreateTexture(const int width, const int height);
// Load all chars in block's range
void LoadBlock(char32_t start, char32_t end);
void LoadGlyph(std::shared_ptr<FontFace>& f, char32_t ch, unsigned index);
protected:
float GetKerning(const GlyphInfo& lgl, const GlyphInfo& rgl);
protected:
float kerningPrecached[128 * 128]; // contains ASCII kerning
int outlineSize;
float outlineWeight;
float lineHeight;
float fontDescender;
float normScale;
int fontSize;
std::string fontFamily;
std::string fontStyle;
int texWidth;
int texHeight;
int wantedTexWidth;
int wantedTexHeight;
unsigned int glyphAtlasTextureID = 0;
private:
int curTextureUpdate = 0;
#ifndef HEADLESS
int lastTextureUpdate = 0;
FT_Face face;
#endif
std::shared_ptr<FontFace> shFace;
spring::unsynced_set<std::shared_ptr<FontFace>> usedFallbackFonts;
spring::unsynced_map<char32_t, GlyphInfo> glyphs; // UTF16 -> GlyphInfo
spring::unsynced_map<uint32_t, float> kerningDynamic; // contains unicode kerning
std::vector<CBitmap> atlasGlyphs;
CRowAtlasAlloc atlasAlloc;
CBitmap atlasUpdate;
CBitmap atlasUpdateShadow;
};
#endif // CFONTTEXTURE_H
|