25 #include <boost/scope_exit.hpp> 38 inline int ftFloor26_6(FT_Pos x) {
42 inline int ftCeil26_6(FT_Pos x) {
49 _library(0), _face(0),
_width(0),
_height(0), _ascent(0), _descent(0) {
56 BOOST_SCOPE_EXIT( (&success) (&
_library)) {
59 } BOOST_SCOPE_EXIT_END
61 const size_t size = ttfFile.
size();
70 BOOST_SCOPE_EXIT( (&success) (&
_face)) {
73 } BOOST_SCOPE_EXIT_END
76 if (!FT_IS_SCALABLE(
_face))
80 if (FT_Set_Char_Size(
_face, 0, height * 64, 0, 0))
83 FT_Fixed yScale =
_face->size->metrics.y_scale;
85 _ascent = ftCeil26_6(FT_MulFix(
_face->ascender, yScale));
88 _width = ftCeil26_6(FT_MulFix(
_face->max_advance_width,
_face->size->metrics.x_scale));
108 FT_Glyph_Metrics &metrics =
_face->glyph->metrics;
110 xMin = ftFloor26_6(metrics.horiBearingX);
111 yOffset =
_ascent - ftFloor26_6(metrics.horiBearingY);
112 advance = ftCeil26_6(metrics.horiAdvance);
116 return FT_Get_Char_Index(
_face, ch) != 0;
120 FT_UInt slot = FT_Get_Char_Index(
_face, ch);
125 if ((
error = FT_Load_Glyph(
_face, slot, FT_LOAD_DEFAULT)))
128 int advance, yOffset, xMin;
135 FT_UInt slot = FT_Get_Char_Index(
_face, ch);
140 if ((
error = FT_Load_Glyph(
_face, slot, FT_LOAD_DEFAULT)))
143 if ((
error = FT_Render_Glyph(
_face->glyph, FT_RENDER_MODE_NORMAL)))
146 if (
_face->glyph->format != FT_GLYPH_FORMAT_BITMAP)
149 int advance, yOffset, xMin;
152 const FT_Bitmap &bitmap =
_face->glyph->bitmap;
153 const uint8 *src = bitmap.buffer;
154 int srcPitch = bitmap.pitch;
156 src += (bitmap.rows - 1) * srcPitch;
157 srcPitch = -srcPitch;
160 uint bitmapWidth = (
uint) bitmap.width;
174 const uint width = MIN<uint>(surface.
getWidth () - x, bitmapWidth);
175 const uint height = MIN<uint>(surface.
getHeight() - y, bitmap.rows);
179 switch (bitmap.pixel_mode) {
180 case FT_PIXEL_MODE_GRAY:
181 for (
uint i = 0; i < height; ++i) {
182 for (
uint j = 0; j < width; ++j, dst += 4) {
184 dst[0] = dst[1] = dst[2] = 0xFF;
188 dst += (surface.
getWidth() - width) * 4;
194 throw Common::Exception(
"TTFRenderer: Unsupported pixel mode %d", bitmap.pixel_mode);
void reset(PointerType o=0)
Resets the pointer with the new value.
bool hasChar(uint32 ch) const
Does the font have the specified character?
A simple TTF renderer outputting BGRA.
Basic exceptions to throw.
Utility templates and functions.
int getMaxWidth() const
Return the max width of a character in pixels.
virtual size_t read(void *dataPtr, size_t dataSize)=0
Read data from the stream.
int getCharWidth(uint32 ch) const
Return the width of a specific character in pixels.
const Exception kReadError("Read error")
Exception when reading from a stream failed.
virtual size_t size() const =0
Obtains the total size of the stream, measured in bytes.
void getFaceMetrics(int &advance, int &yOffset, int &xMin) const
PointerType get() const
Returns the plain pointer value.
An image surface, in BGRA format.
Common::ScopedArray< byte > _fileBuffer
TTFRenderer(Common::SeekableReadStream &ttfFile, int height)
Create a TTF renderer with this TTF in the specified line height in pixels.
void NORETURN_PRE error(const char *s,...)
int getHeight() const
Return the height of a character in pixels.
Interface for a seekable & readable data stream.
void drawCharacter(uint32 ch, Surface &surface, int x, int y)
Draw a specific character onto a BGRA surface.