123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 | /**
* Copyright (c) 2006-2026 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
// LOVE
#include "TrueTypeRasterizer.h"
#include "HarfbuzzShaper.h"
#include "common/Exception.h"
// C
#include <math.h>
namespace love
{
namespace font
{
namespace freetype
{
TrueTypeRasterizer::TrueTypeRasterizer(FT_Library library, love::Data *data, int size, const Settings &settings, float defaultdpiscale)
: data(data)
, hinting(settings.hinting)
{
dpiScale = settings.dpiScale.get(defaultdpiscale);
sdf = settings.sdf;
if (size <= 0)
throw love::Exception("Invalid TrueType font size: %d", size);
FT_Error err = FT_Err_Ok;
err = FT_New_Memory_Face(library,
(const FT_Byte *)data->getData(), /* first byte in memory */
data->getSize(), /* size in bytes */
0, /* face_index */
&face);
if (err != FT_Err_Ok)
throw love::Exception("TrueType Font loading error: FT_New_Face failed: 0x%x (problem with font file?)", err);
err = FT_Set_Char_Size(face, size << 6, size << 6, 72 * dpiScale, 72 * dpiScale);
if (err != FT_Err_Ok)
throw love::Exception("TrueType Font loading error: FT_Set_Pixel_Sizes failed: 0x%x (invalid size?)", err);
// Set global metrics
FT_Size_Metrics s = face->size->metrics;
metrics.advance = (int) (s.max_advance >> 6);
metrics.ascent = (int) (s.ascender >> 6);
metrics.descent = (int) (s.descender >> 6);
metrics.height = (int) (s.height >> 6);
}
TrueTypeRasterizer::~TrueTypeRasterizer()
{
FT_Done_Face(face);
}
int TrueTypeRasterizer::getLineHeight() const
{
return (int)(getHeight() * 1.25);
}
int TrueTypeRasterizer::getGlyphSpacing(uint32 glyph) const
{
FT_Glyph ftglyph;
FT_Error err = FT_Err_Ok;
FT_UInt loadoption = hintingToLoadOption(hinting);
// Initialize
err = FT_Load_Glyph(face, FT_Get_Char_Index(face, glyph), FT_LOAD_DEFAULT | loadoption);
if (err != FT_Err_Ok)
return 0;
err = FT_Get_Glyph(face->glyph, &ftglyph);
if (err != FT_Err_Ok)
return 0;
return (int)(ftglyph->advance.x >> 16);
}
int TrueTypeRasterizer::getGlyphIndex(uint32 glyph) const
{
return FT_Get_Char_Index(face, glyph);
}
GlyphData *TrueTypeRasterizer::getGlyphDataForIndex(int index) const
{
love::font::GlyphMetrics glyphMetrics = {};
FT_Glyph ftglyph;
FT_Error err = FT_Err_Ok;
FT_UInt loadoption = hintingToLoadOption(hinting);
// Initialize
err = FT_Load_Glyph(face, index, FT_LOAD_DEFAULT | loadoption);
if (err != FT_Err_Ok)
throw love::Exception("TrueType Font glyph error: FT_Load_Glyph failed (0x%x)", err);
err = FT_Get_Glyph(face->glyph, &ftglyph);
if (err != FT_Err_Ok)
throw love::Exception("TrueType Font glyph error: FT_Get_Glyph failed (0x%x)", err);
FT_Render_Mode rendermode = FT_RENDER_MODE_NORMAL;
if (sdf)
rendermode = FT_RENDER_MODE_SDF;
else if (hinting == HINTING_MONO)
rendermode = FT_RENDER_MODE_MONO;
err = FT_Glyph_To_Bitmap(&ftglyph, rendermode, 0, 1);
if (err != FT_Err_Ok)
{
if (rendermode == FT_RENDER_MODE_SDF)
{
err = FT_Glyph_To_Bitmap(&ftglyph, FT_RENDER_MODE_NORMAL, 0, 1);
if (err != FT_Err_Ok)
throw love::Exception("TrueType Font glyph error: FT_Glyph_To_Bitmap failed (0x%x)", err);
}
else
{
throw love::Exception("TrueType Font glyph error: FT_Glyph_To_Bitmap failed (0x%x)", err);
}
}
FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph) ftglyph;
const FT_Bitmap &bitmap = bitmap_glyph->bitmap; //just to make things easier
// Get metrics
glyphMetrics.bearingX = bitmap_glyph->left;
glyphMetrics.bearingY = bitmap_glyph->top;
glyphMetrics.height = bitmap.rows;
glyphMetrics.width = bitmap.width;
glyphMetrics.advance = (int) (ftglyph->advance.x >> 16);
// TODO: https://stackoverflow.com/questions/60526004/how-to-get-glyph-unicode-using-freetype/69730502#69730502
GlyphData *glyphData = new GlyphData(0, glyphMetrics, PIXELFORMAT_LA8_UNORM);
const uint8 *pixels = bitmap.buffer;
uint8 *dest = (uint8 *) glyphData->getData();
// We treat the luminance of the FreeType bitmap as alpha in the GlyphData.
if (bitmap.pixel_mode == FT_PIXEL_MODE_MONO)
{
for (int y = 0; y < (int) bitmap.rows; y++)
{
for (int x = 0; x < (int) bitmap.width; x++)
{
// Extract the 1-bit value and convert it to uint8.
uint8 v = ((pixels[x / 8]) & (1 << (7 - (x % 8)))) ? 255 : 0;
dest[2 * (y * bitmap.width + x) + 0] = 255;
dest[2 * (y * bitmap.width + x) + 1] = v;
}
pixels += bitmap.pitch;
}
}
else if (bitmap.pixel_mode == FT_PIXEL_MODE_GRAY)
{
for (int y = 0; y < (int) bitmap.rows; y++)
{
for (int x = 0; x < (int) bitmap.width; x++)
{
dest[2 * (y * bitmap.width + x) + 0] = 255;
dest[2 * (y * bitmap.width + x) + 1] = pixels[x];
}
pixels += bitmap.pitch;
}
}
else
{
delete glyphData;
FT_Done_Glyph(ftglyph);
throw love::Exception("Unknown TrueType glyph pixel mode.");
}
// Having copied the data over, we can destroy the glyph.
FT_Done_Glyph(ftglyph);
return glyphData;
}
int TrueTypeRasterizer::getGlyphCount() const
{
return (int) face->num_glyphs;
}
bool TrueTypeRasterizer::hasGlyph(uint32 glyph) const
{
return FT_Get_Char_Index(face, glyph) != 0;
}
float TrueTypeRasterizer::getKerning(uint32 leftglyph, uint32 rightglyph) const
{
FT_Vector kerning = {};
FT_Get_Kerning(face,
FT_Get_Char_Index(face, leftglyph),
FT_Get_Char_Index(face, rightglyph),
FT_KERNING_DEFAULT,
&kerning);
return float(kerning.x >> 6);
}
Rasterizer::DataType TrueTypeRasterizer::getDataType() const
{
return DATA_TRUETYPE;
}
TextShaper *TrueTypeRasterizer::newTextShaper()
{
return new HarfbuzzShaper(this);
}
bool TrueTypeRasterizer::accepts(FT_Library library, love::Data *data)
{
const FT_Byte *fbase = (const FT_Byte *) data->getData();
FT_Long fsize = (FT_Long) data->getSize();
// Pasing in -1 for the face index lets us test if the data is valid.
return FT_New_Memory_Face(library, fbase, fsize, -1, nullptr) == 0;
}
FT_UInt TrueTypeRasterizer::hintingToLoadOption(Hinting hint)
{
switch (hint)
{
case HINTING_NORMAL:
default:
return FT_LOAD_TARGET_NORMAL;
case HINTING_LIGHT:
return FT_LOAD_TARGET_LIGHT;
case HINTING_MONO:
return FT_LOAD_TARGET_MONO;
case HINTING_NONE:
return FT_LOAD_NO_HINTING;
}
}
} // freetype
} // font
} // love
|