| 1 | /** | 
|---|
| 2 | * Copyright (c) 2006-2023 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 | #include "common/Exception.h" | 
|---|
| 24 |  | 
|---|
| 25 | // C | 
|---|
| 26 | #include <math.h> | 
|---|
| 27 |  | 
|---|
| 28 | namespace love | 
|---|
| 29 | { | 
|---|
| 30 | namespace font | 
|---|
| 31 | { | 
|---|
| 32 | namespace freetype | 
|---|
| 33 | { | 
|---|
| 34 |  | 
|---|
| 35 | TrueTypeRasterizer::TrueTypeRasterizer(FT_Library library, love::Data *data, int size, float dpiscale, Hinting hinting) | 
|---|
| 36 | : data(data) | 
|---|
| 37 | , hinting(hinting) | 
|---|
| 38 | { | 
|---|
| 39 | this->dpiScale = dpiscale; | 
|---|
| 40 | size = floorf(size * dpiscale + 0.5f); | 
|---|
| 41 |  | 
|---|
| 42 | if (size <= 0) | 
|---|
| 43 | throw love::Exception( "Invalid TrueType font size: %d", size); | 
|---|
| 44 |  | 
|---|
| 45 | FT_Error err = FT_Err_Ok; | 
|---|
| 46 | err = FT_New_Memory_Face(library, | 
|---|
| 47 | (const FT_Byte *)data->getData(), /* first byte in memory */ | 
|---|
| 48 | data->getSize(),                  /* size in bytes        */ | 
|---|
| 49 | 0,                                /* face_index           */ | 
|---|
| 50 | &face); | 
|---|
| 51 |  | 
|---|
| 52 | if (err != FT_Err_Ok) | 
|---|
| 53 | throw love::Exception( "TrueType Font loading error: FT_New_Face failed: 0x%x (problem with font file?)", err); | 
|---|
| 54 |  | 
|---|
| 55 | err = FT_Set_Pixel_Sizes(face, size, size); | 
|---|
| 56 |  | 
|---|
| 57 | if (err != FT_Err_Ok) | 
|---|
| 58 | throw love::Exception( "TrueType Font loading error: FT_Set_Pixel_Sizes failed: 0x%x (invalid size?)", err); | 
|---|
| 59 |  | 
|---|
| 60 | // Set global metrics | 
|---|
| 61 | FT_Size_Metrics s = face->size->metrics; | 
|---|
| 62 | metrics.advance = (int) (s.max_advance >> 6); | 
|---|
| 63 | metrics.ascent  = (int) (s.ascender >> 6); | 
|---|
| 64 | metrics.descent = (int) (s.descender >> 6); | 
|---|
| 65 | metrics.height  = (int) (s.height >> 6); | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | TrueTypeRasterizer::~TrueTypeRasterizer() | 
|---|
| 69 | { | 
|---|
| 70 | FT_Done_Face(face); | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | int TrueTypeRasterizer::getLineHeight() const | 
|---|
| 74 | { | 
|---|
| 75 | return (int)(getHeight() * 1.25); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | GlyphData *TrueTypeRasterizer::getGlyphData(uint32 glyph) const | 
|---|
| 79 | { | 
|---|
| 80 | love::font::GlyphMetrics glyphMetrics = {}; | 
|---|
| 81 | FT_Glyph ftglyph; | 
|---|
| 82 |  | 
|---|
| 83 | FT_Error err = FT_Err_Ok; | 
|---|
| 84 | FT_UInt loadoption = hintingToLoadOption(hinting); | 
|---|
| 85 |  | 
|---|
| 86 | // Initialize | 
|---|
| 87 | err = FT_Load_Glyph(face, FT_Get_Char_Index(face, glyph), FT_LOAD_DEFAULT | loadoption); | 
|---|
| 88 |  | 
|---|
| 89 | if (err != FT_Err_Ok) | 
|---|
| 90 | throw love::Exception( "TrueType Font glyph error: FT_Load_Glyph failed (0x%x)", err); | 
|---|
| 91 |  | 
|---|
| 92 | err = FT_Get_Glyph(face->glyph, &ftglyph); | 
|---|
| 93 |  | 
|---|
| 94 | if (err != FT_Err_Ok) | 
|---|
| 95 | throw love::Exception( "TrueType Font glyph error: FT_Get_Glyph failed (0x%x)", err); | 
|---|
| 96 |  | 
|---|
| 97 | FT_Render_Mode rendermode = FT_RENDER_MODE_NORMAL; | 
|---|
| 98 | if (hinting == HINTING_MONO) | 
|---|
| 99 | rendermode = FT_RENDER_MODE_MONO; | 
|---|
| 100 |  | 
|---|
| 101 | err = FT_Glyph_To_Bitmap(&ftglyph, rendermode, 0, 1); | 
|---|
| 102 |  | 
|---|
| 103 | if (err != FT_Err_Ok) | 
|---|
| 104 | throw love::Exception( "TrueType Font glyph error: FT_Glyph_To_Bitmap failed (0x%x)", err); | 
|---|
| 105 |  | 
|---|
| 106 | FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph) ftglyph; | 
|---|
| 107 | FT_Bitmap &bitmap = bitmap_glyph->bitmap; //just to make things easier | 
|---|
| 108 |  | 
|---|
| 109 | // Get metrics | 
|---|
| 110 | glyphMetrics.bearingX = bitmap_glyph->left; | 
|---|
| 111 | glyphMetrics.bearingY = bitmap_glyph->top; | 
|---|
| 112 | glyphMetrics.height = bitmap.rows; | 
|---|
| 113 | glyphMetrics.width = bitmap.width; | 
|---|
| 114 | glyphMetrics.advance = (int) (ftglyph->advance.x >> 16); | 
|---|
| 115 |  | 
|---|
| 116 | GlyphData *glyphData = new GlyphData(glyph, glyphMetrics, PIXELFORMAT_LA8); | 
|---|
| 117 |  | 
|---|
| 118 | const uint8 *pixels = bitmap.buffer; | 
|---|
| 119 | uint8 *dest = (uint8 *) glyphData->getData(); | 
|---|
| 120 |  | 
|---|
| 121 | // We treat the luminance of the FreeType bitmap as alpha in the GlyphData. | 
|---|
| 122 | if (bitmap.pixel_mode == FT_PIXEL_MODE_MONO) | 
|---|
| 123 | { | 
|---|
| 124 | for (int y = 0; y < (int) bitmap.rows; y++) | 
|---|
| 125 | { | 
|---|
| 126 | for (int x = 0; x < (int) bitmap.width; x++) | 
|---|
| 127 | { | 
|---|
| 128 | // Extract the 1-bit value and convert it to uint8. | 
|---|
| 129 | uint8 v = ((pixels[x / 8]) & (1 << (7 - (x % 8)))) ? 255 : 0; | 
|---|
| 130 | dest[2 * (y * bitmap.width + x) + 0] = 255; | 
|---|
| 131 | dest[2 * (y * bitmap.width + x) + 1] = v; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | pixels += bitmap.pitch; | 
|---|
| 135 | } | 
|---|
| 136 | } | 
|---|
| 137 | else if (bitmap.pixel_mode == FT_PIXEL_MODE_GRAY) | 
|---|
| 138 | { | 
|---|
| 139 | for (int y = 0; y < (int) bitmap.rows; y++) | 
|---|
| 140 | { | 
|---|
| 141 | for (int x = 0; x < (int) bitmap.width; x++) | 
|---|
| 142 | { | 
|---|
| 143 | dest[2 * (y * bitmap.width + x) + 0] = 255; | 
|---|
| 144 | dest[2 * (y * bitmap.width + x) + 1] = pixels[x]; | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | pixels += bitmap.pitch; | 
|---|
| 148 | } | 
|---|
| 149 | } | 
|---|
| 150 | else | 
|---|
| 151 | { | 
|---|
| 152 | delete glyphData; | 
|---|
| 153 | FT_Done_Glyph(ftglyph); | 
|---|
| 154 | throw love::Exception( "Unknown TrueType glyph pixel mode."); | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | // Having copied the data over, we can destroy the glyph. | 
|---|
| 158 | FT_Done_Glyph(ftglyph); | 
|---|
| 159 |  | 
|---|
| 160 | return glyphData; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | int TrueTypeRasterizer::getGlyphCount() const | 
|---|
| 164 | { | 
|---|
| 165 | return (int) face->num_glyphs; | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | bool TrueTypeRasterizer::hasGlyph(uint32 glyph) const | 
|---|
| 169 | { | 
|---|
| 170 | return FT_Get_Char_Index(face, glyph) != 0; | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | float TrueTypeRasterizer::getKerning(uint32 leftglyph, uint32 rightglyph) const | 
|---|
| 174 | { | 
|---|
| 175 | FT_Vector kerning = {}; | 
|---|
| 176 | FT_Get_Kerning(face, | 
|---|
| 177 | FT_Get_Char_Index(face, leftglyph), | 
|---|
| 178 | FT_Get_Char_Index(face, rightglyph), | 
|---|
| 179 | FT_KERNING_DEFAULT, | 
|---|
| 180 | &kerning); | 
|---|
| 181 | return float(kerning.x >> 6); | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | Rasterizer::DataType TrueTypeRasterizer::getDataType() const | 
|---|
| 185 | { | 
|---|
| 186 | return DATA_TRUETYPE; | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | bool TrueTypeRasterizer::accepts(FT_Library library, love::Data *data) | 
|---|
| 190 | { | 
|---|
| 191 | const FT_Byte *fbase = (const FT_Byte *) data->getData(); | 
|---|
| 192 | FT_Long fsize = (FT_Long) data->getSize(); | 
|---|
| 193 |  | 
|---|
| 194 | // Pasing in -1 for the face index lets us test if the data is valid. | 
|---|
| 195 | return FT_New_Memory_Face(library, fbase, fsize, -1, nullptr) == 0; | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | FT_UInt TrueTypeRasterizer::hintingToLoadOption(Hinting hint) | 
|---|
| 199 | { | 
|---|
| 200 | switch (hint) | 
|---|
| 201 | { | 
|---|
| 202 | case HINTING_NORMAL: | 
|---|
| 203 | default: | 
|---|
| 204 | return FT_LOAD_TARGET_NORMAL; | 
|---|
| 205 | case HINTING_LIGHT: | 
|---|
| 206 | return FT_LOAD_TARGET_LIGHT; | 
|---|
| 207 | case HINTING_MONO: | 
|---|
| 208 | return FT_LOAD_TARGET_MONO; | 
|---|
| 209 | case HINTING_NONE: | 
|---|
| 210 | return FT_LOAD_NO_HINTING; | 
|---|
| 211 | } | 
|---|
| 212 | } | 
|---|
| 213 |  | 
|---|
| 214 | } // freetype | 
|---|
| 215 | } // font | 
|---|
| 216 | } // love | 
|---|
| 217 |  | 
|---|