1 /**
2 Copyright: Copyright (c) 2013-2018 Andrey Penechko.
3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 
7 module voxelman.graphics.font.font;
8 
9 import voxelman.math;
10 
11 struct Glyph
12 {
13 	ivec2 atlasPosition;
14 	GlyphMetrics metrics;
15 }
16 
17 struct GlyphMetrics
18 {
19 	uint width;
20 	uint height;
21 	int  offsetX; // bearingX
22 	int  offsetY; // bearingY
23 	uint advanceX;
24 	uint advanceY;
25 }
26 
27 struct FontMetrics
28 {
29 	uint width; // monospaced width
30 	uint height; // max glyph height
31 	uint ascent; // vertical offset from baseline to highest point (positive)
32 	uint descent; // vertical offset from baseline to lowest point (negative)
33 	uint advanceX; // mono width + glyph spacing
34 	uint advanceY; // max glyph height + line spacing
35 }
36 
37 struct Font
38 {
39 	int getKerning(in dchar leftGlyph, in dchar rightGlyph) const
40 	{
41 		const(int[dchar]) rightGlyps = *(leftGlyph in kerningTable);
42 		int kerning = *(rightGlyph in rightGlyps);
43 		return kerning;
44 	}
45 
46 	Glyph* getGlyph(in dchar chr) const
47 	{
48 		import std.utf : replacementDchar;
49 		if (auto glyph = chr in glyphs)
50 		{
51 			return cast(Glyph*)glyph;
52 		}
53 		else
54 			return cast(Glyph*)(replacementDchar in glyphs);
55 	}
56 
57 	void sanitize()
58 	{
59 		metrics.width = max(metrics.width, 1);
60 		metrics.height = max(metrics.height, 1);
61 	}
62 
63 	string filename;
64 	FontMetrics metrics;
65 	Glyph[dchar] glyphs;
66 
67 	int[dchar][dchar] kerningTable;
68 	bool kerningEnabled = false;
69 }
70 
71 alias FontRef = Font*;