/*
SDL_ttf: A companion library to SDL for working with TrueType (tm) fonts
Copyright (C) 1997-2009 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
[email protected]
*/
/* $Id: SDL_ttf.c 5226 2009-11-09 15:11:11Z slouken $ */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
#ifdef HAVE_ALLOCA
#define ALLOCA(n) ((void*)alloca(n))
#define FREEA(p)
#else
#define ALLOCA(n) malloc(n)
#define FREEA(p) free(p)
#endif
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_OUTLINE_H
#include FT_STROKER_H
#include FT_GLYPH_H
#include FT_TRUETYPE_IDS_H
#include "SDL.h"
#include "SDL_endian.h"
#include "SDL_ttf.h"
/* FIXME: Right now we assume the gray-scale renderer Freetype is using
supports 256 shades of gray, but we should instead key off of num_grays
in the result FT_Bitmap after the FT_Render_Glyph() call. */
#define NUM_GRAYS 256
/* Handy routines for converting from fixed point */
#define FT_FLOOR(X) ((X & -64) / 64)
#define FT_CEIL(X) (((X + 63) & -64) / 64)
#define CACHED_METRICS 0x10
#define CACHED_BITMAP 0x01
#define CACHED_PIXMAP 0x02
/* Cached glyph information */
typedef struct cached_glyph {
int stored;
FT_UInt index;
FT_Bitmap bitmap;
FT_Bitmap pixmap;
int minx;
int maxx;
int miny;
int maxy;
int yoffset;
int advance;
Uint16 cached;
} c_glyph;
/* The structure used to hold internal font information */
struct _TTF_Font {
/* Freetype2 maintains all sorts of useful info itself */
FT_Face face;
/* We'll cache these ourselves */
int height;
int ascent;
int descent;
int lineskip;
/* The font style */
int face_style;
int style;
int outline;
/* Whether kerning is desired */
int kerning;
/* Extra width in glyph bounds for text styles */
int glyph_overhang;
float glyph_italics;
/* Information in the font for underlining */
int underline_offset;
int underline_height;
/* Cache for style-transformed glyphs */
c_glyph *current;
c_glyph cache[256];
c_glyph scratch;
/* We are responsible for closing the font stream */
SDL_RWops *src;
int freesrc;
FT_Open_Args args;
/* For non-scalable formats, we must remember which font index size */
int font_size_family;
/* really just flags passed into FT_Load_Glyph */
int hinting;
};
/* Handle a style only if the font does not already handle it */
#define TTF_HANDLE_STYLE_BOLD(font) (((font)->style & TTF_STYLE_BOLD) && \
!((font)->face_style & TTF_STYLE_BOLD))
#define TTF_HANDLE_STYLE_ITALIC(font) (((font)->style & TTF_STYLE_ITALIC) && \
!((font)->face_style & TTF_STYLE_ITALIC))
#define TTF_HANDLE_STYLE_UNDERLINE(font) ((font)->style & TTF_STYLE_UNDERLINE)
#define TTF_HANDLE_STYLE_STRIKETHROUGH(font) ((font)->style & TTF_STYLE_STRIKETHROUGH)
/* Font styles that does not impact glyph drawing */
#define TTF_STYLE_NO_GLYPH_CHANGE (TTF_STYLE_UNDERLINE | TTF_STYLE_STRIKETHROUGH)
/* The FreeType font engine/library */
static FT_Library library;
static int TTF_initialized = 0;
static int TTF_byteswapped = 0;
/* Gets the top row of the underline. The outline
is taken into account.
*/
static __inline__ int TTF_underline_top_row(TTF_Font *font)
{
/* With outline, the underline_offset is underline_offset+outline. */
/* So, we don't have to remove the top part of the outline height. */
return font->ascent - font->underline_offset - 1;
}
/* Gets the top row of the underline. for a given glyph. The outline
is taken into account.
Need to update row according to height difference between font and glyph:
font_value - font->ascent + glyph->maxy
*/
static __inline__ int TTF_Glyph_underline_top_row(TTF_Font *font, c_glyph *glyph)
{
return glyph->maxy - font->underline_offset - 1;
}
/* Gets the bottom row of the underline. The outline
is taken into account.
*/
static __inline__ int TTF_underline_bottom_row(TTF_Font *font)
{
int row = TTF_underline_top_row(font) + font->underline_height;
if( font->outline > 0 ) {
/* Add underline_offset outline offset and */
/* the bottom part of the outline. */
row += font->outline * 2;
}
return row;
}
/* Gets the bottom row of the underline. for a given glyph. The outline
is taken into account.
Need to update row according to height difference between font and glyph:
font_value - font->ascent + glyph->maxy
*/
static __inline__ int TTF_Glyph_underline_bottom_row(TTF_Font *font, c_glyph *glyph)
{
return TTF_underline_bottom_row(font) - font->ascent + glyph->maxy;
}
/* Gets the top row of the strikethrough. The outline
is taken into account.
*/
static __inline__ int TTF_strikethrough_top_row(TTF_Font *font)
{
/* With outline, the first text row is 'outline'. */
/* So, we don't have to remove the top part of the outline height. */
return font->height / 2;
}
/* Gets the top row of the strikethrough for a given glyph. The outline
is taken into account.
Need to update row according to height difference between font and glyph:
font_value - font->ascent + glyph->maxy
*/
static __inline__ int TTF_Glyph_strikethrough_top_row(TTF_Font *font, c_glyph *glyph)
{
return TTF_strikethrough_top_row(font) - font->ascent + glyph->maxy;
}
static void TTF_initLineMectrics(const TTF_Font *font, const SDL_Surface *textbuf, const int row, Uint8 **pdst, int *pheight)
{
Uint8 *dst;
int height;
dst = (Uint8 *)textbuf->pixels;
if( row > 0 ) {
dst += row * textbuf->pitch;
}
height = font->underline_height;
/* Take outline into account */
if( font->outline > 0 ) {
height += font->outline * 2;
}
*pdst = dst;
*pheight = height;
}
/* Draw a solid line of underline_height (+ optional outline)
at the given row. The row value must take the
outline into account.
*/
static void TTF_drawLine_Solid(const TTF_Font *font, const SDL_Surface *textbuf, const int row)
{
int line;
Uint8 *dst_check = (Uint8*)textbuf->pixels + textbuf->pitch * textbuf->h;
Uint8 *dst;
int height;
TTF_initLineMectrics(font, textbuf, row, &dst, &height);
/* Draw line */
for ( line=height; line>0 && dst < dst_check; --line ) {
/* 1 because 0 is the bg color */
memset( dst, 1, textbuf->w );
dst += textbuf->pitch;
}
}
/* Draw a shaded line of underline_height (+ optional outline)
at the given row. The row value must take the
outline into account.
*/
static void TTF_drawLine_Shaded(const TTF_Font *font, const SDL_Surface *textbuf, const int row)
{
int line;
Uint8 *dst_check = (Uint8*)textbuf->pixels + textbuf->pitch * textbuf->h;
Uint8 *dst;
int height;
TTF_initLineMectrics(font, textbuf, row, &dst, &height);
/* Draw line */
for ( line=height; line>0 && dst < dst_check; --line ) {
memset( dst, NUM_GRAYS - 1, textbuf->w );
dst += textbuf->pitch;
}
}
/* Draw a blended line of underline_height (+ optional outline)
at the given row. The row value must take the
outline into account.
*/
static void TTF_drawLine_Blended(const TTF_Font *font, const SDL_S