1 /**
2  * Copyright (c) 2006-2016 LOVE Development Team
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty.  In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  *    claim that you wrote the original software. If you use this software
14  *    in a product, an acknowledgment in the product documentation would be
15  *    appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  *    misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  **/
20 
21 // LOVE
22 #include "TrueTypeRasterizer.h"
23 
24 #include "common/Exception.h"
25 
26 namespace love
27 {
28 namespace font
29 {
30 namespace freetype
31 {
32 
TrueTypeRasterizer(FT_Library library,love::Data * data,int size,Hinting hinting)33 TrueTypeRasterizer::TrueTypeRasterizer(FT_Library library, love::Data *data, int size, Hinting hinting)
34 	: data(data)
35 	, hinting(hinting)
36 {
37 	if (size <= 0)
38 		throw love::Exception("Invalid TrueType font size: %d", size);
39 
40 	FT_Error err = FT_Err_Ok;
41 	err = FT_New_Memory_Face(library,
42 	                         (const FT_Byte *)data->getData(), /* first byte in memory */
43 	                         data->getSize(),                  /* size in bytes        */
44 	                         0,                                /* face_index           */
45 	                         &face);
46 
47 	if (err != FT_Err_Ok)
48 		throw love::Exception("TrueType Font loading error: FT_New_Face failed: 0x%x (problem with font file?)", err);
49 
50 	err = FT_Set_Pixel_Sizes(face, size, size);
51 
52 	if (err != FT_Err_Ok)
53 		throw love::Exception("TrueType Font loading error: FT_Set_Pixel_Sizes failed: 0x%x (invalid size?)", err);
54 
55 	// Set global metrics
56 	FT_Size_Metrics s = face->size->metrics;
57 	metrics.advance = (int) (s.max_advance >> 6);
58 	metrics.ascent  = (int) (s.ascender >> 6);
59 	metrics.descent = (int) (s.descender >> 6);
60 	metrics.height  = (int) (s.height >> 6);
61 }
62 
~TrueTypeRasterizer()63 TrueTypeRasterizer::~TrueTypeRasterizer()
64 {
65 	FT_Done_Face(face);
66 }
67 
getLineHeight() const68 int TrueTypeRasterizer::getLineHeight() const
69 {
70 	return (int)(getHeight() * 1.25);
71 }
72 
getGlyphData(uint32 glyph) const73 GlyphData *TrueTypeRasterizer::getGlyphData(uint32 glyph) const
74 {
75 	love::font::GlyphMetrics glyphMetrics = {};
76 	FT_Glyph ftglyph;
77 
78 	FT_Error err = FT_Err_Ok;
79 	FT_ULong loadoption = hintingToLoadOption(hinting);
80 
81 	// Initialize
82 	err = FT_Load_Glyph(face, FT_Get_Char_Index(face, glyph), FT_LOAD_DEFAULT | loadoption);
83 
84 	if (err != FT_Err_Ok)
85 		throw love::Exception("TrueType Font glyph error: FT_Load_Glyph failed (0x%x)", err);
86 
87 	err = FT_Get_Glyph(face->glyph, &ftglyph);
88 
89 	if (err != FT_Err_Ok)
90 		throw love::Exception("TrueType Font glyph error: FT_Get_Glyph failed (0x%x)", err);
91 
92 	FT_Render_Mode rendermode = FT_RENDER_MODE_NORMAL;
93 	if (hinting == HINTING_MONO)
94 		rendermode = FT_RENDER_MODE_MONO;
95 
96 	err = FT_Glyph_To_Bitmap(&ftglyph, rendermode, 0, 1);
97 
98 	if (err != FT_Err_Ok)
99 		throw love::Exception("TrueType Font glyph error: FT_Glyph_To_Bitmap failed (0x%x)", err);
100 
101 	FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph) ftglyph;
102 	FT_Bitmap &bitmap = bitmap_glyph->bitmap; //just to make things easier
103 
104 	// Get metrics
105 	glyphMetrics.bearingX = bitmap_glyph->left;
106 	glyphMetrics.bearingY = bitmap_glyph->top;
107 	glyphMetrics.height = bitmap.rows;
108 	glyphMetrics.width = bitmap.width;
109 	glyphMetrics.advance = (int) (ftglyph->advance.x >> 16);
110 
111 	GlyphData *glyphData = new GlyphData(glyph, glyphMetrics, GlyphData::FORMAT_LUMINANCE_ALPHA);
112 
113 	const uint8 *pixels = bitmap.buffer;
114 	uint8 *dest = (uint8 *) glyphData->getData();
115 
116 	// We treat the luminance of the FreeType bitmap as alpha in the GlyphData.
117 	if (bitmap.pixel_mode == FT_PIXEL_MODE_MONO)
118 	{
119 		for (int y = 0; y < (int) bitmap.rows; y++)
120 		{
121 			for (int x = 0; x < (int) bitmap.width; x++)
122 			{
123 				// Extract the 1-bit value and convert it to uint8.
124 				uint8 v = ((pixels[x / 8]) & (1 << (7 - (x % 8)))) ? 255 : 0;
125 				dest[2 * (y * bitmap.width + x) + 0] = 255;
126 				dest[2 * (y * bitmap.width + x) + 1] = v;
127 			}
128 
129 			pixels += bitmap.pitch;
130 		}
131 	}
132 	else if (bitmap.pixel_mode == FT_PIXEL_MODE_GRAY)
133 	{
134 		for (int y = 0; y < (int) bitmap.rows; y++)
135 		{
136 			for (int x = 0; x < (int) bitmap.width; x++)
137 			{
138 				dest[2 * (y * bitmap.width + x) + 0] = 255;
139 				dest[2 * (y * bitmap.width + x) + 1] = pixels[x];
140 			}
141 
142 			pixels += bitmap.pitch;
143 		}
144 	}
145 	else
146 	{
147 		delete glyphData;
148 		FT_Done_Glyph(ftglyph);
149 		throw love::Exception("Unknown TrueType glyph pixel mode.");
150 	}
151 
152 	// Having copied the data over, we can destroy the glyph.
153 	FT_Done_Glyph(ftglyph);
154 
155 	return glyphData;
156 }
157 
getGlyphCount() const158 int TrueTypeRasterizer::getGlyphCount() const
159 {
160 	return (int) face->num_glyphs;
161 }
162 
hasGlyph(uint32 glyph) const163 bool TrueTypeRasterizer::hasGlyph(uint32 glyph) const
164 {
165 	return FT_Get_Char_Index(face, glyph) != 0;
166 }
167 
getKerning(uint32 leftglyph,uint32 rightglyph) const168 float TrueTypeRasterizer::getKerning(uint32 leftglyph, uint32 rightglyph) const
169 {
170 	FT_Vector kerning = {};
171 	FT_Get_Kerning(face,
172 	               FT_Get_Char_Index(face, leftglyph),
173 	               FT_Get_Char_Index(face, rightglyph),
174 	               FT_KERNING_DEFAULT,
175 	               &kerning);
176 	return float(kerning.x >> 6);
177 }
178 
accepts(FT_Library library,love::Data * data)179 bool TrueTypeRasterizer::accepts(FT_Library library, love::Data *data)
180 {
181 	const FT_Byte *fbase = (const FT_Byte *) data->getData();
182 	FT_Long fsize = (FT_Long) data->getSize();
183 
184 	// Pasing in -1 for the face index lets us test if the data is valid.
185 	return FT_New_Memory_Face(library, fbase, fsize, -1, nullptr) == 0;
186 }
187 
hintingToLoadOption(Hinting hint)188 FT_ULong TrueTypeRasterizer::hintingToLoadOption(Hinting hint)
189 {
190 	switch (hint)
191 	{
192 	case HINTING_NORMAL:
193 	default:
194 		return FT_LOAD_TARGET_NORMAL;
195 	case HINTING_LIGHT:
196 		return FT_LOAD_TARGET_LIGHT;
197 	case HINTING_MONO:
198 		return FT_LOAD_TARGET_MONO;
199 	case HINTING_NONE:
200 		return FT_LOAD_NO_HINTING;
201 	}
202 }
203 
204 } // freetype
205 } // font
206 } // love
207