1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
/*
* This file is part of NumptyPhysics
* Copyright (C) 2008 Tim Edmonds
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This program 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
* General Public License for more details.
*
*/
#include "Font.h"
#include "Canvas.h"
#include "Config.h"
#include <SDL/SDL_ttf.h>
#ifdef HAVE_FONTCONFIG
#include <fontconfig/fontconfig.h>
#endif
#define FONT(fONTpTR) ((TTF_Font*)((fONTpTR)->m_state))
struct FontCanvas : public Canvas
{
FontCanvas( SDL_Surface* s )
: Canvas( s )
{}
};
Font::Font( const std::string& file, int ptsize )
{
TTF_Init();
#ifdef HAVE_FONTCONFIG
std::string fname;
if( FcInit() )
{
FcResult result;
FcChar8 *fontfile;
FcPattern *pat = FcNameParse( (FcChar8 *) file.c_str() );
FcConfigSubstitute (0, pat, FcMatchPattern);
FcDefaultSubstitute (pat);
FcPattern *match = FcFontMatch (0, pat, &result);
FcPatternDestroy (pat);
FcPatternGetString(match, FC_FILE, 0, &fontfile);
fname = std::string((const char*)fontfile);
}
else
{
// no fontconfig, fallback to builtin font
fname = Config::findFile(file+".ttf");
}
#else
std::string fname = Config::findFile(file+".ttf");
#endif
m_state = TTF_OpenFont( fname.c_str(), ptsize );
m_height = metrics("M").y;
}
Vec2 Font::metrics( const std::string& text ) const
{
Vec2 m;
TTF_SizeText( FONT(this), text.c_str(), &m.x, &m.y );
return m;
}
void Font::drawLeft( Canvas* canvas, Vec2 pt,
const std::string& text, int colour ) const
{
SDL_Color fg = { colour>>16, colour>>8, colour };
FontCanvas temp( TTF_RenderText_Blended( FONT(this),
text.c_str(),
fg ) );
canvas->drawImage( &temp, pt.x, pt.y );
}
void Font::drawRight( Canvas* canvas, Vec2 pt,
const std::string& text, int colour ) const
{
drawLeft( canvas, pt - Vec2(metrics(text).x,0), text, colour );
}
void Font::drawCenter( Canvas* canvas, Vec2 pt,
const std::string& text, int colour ) const
{
drawLeft( canvas, pt - metrics(text)/2, text, colour );
}
void Font::drawWrap( Canvas* canvas, Rect area,
const std::string& text, int colour ) const
{
Vec2 pos = area.tl;
size_t i = 0, e=0;
while ( i < text.length() ) {
e = text.find_first_of(" \n\r\t",i);
if ( i == e ) {
i++;
} else {
std::string word = text.substr(i,i+e);
Vec2 wm = metrics( word );
if ( pos.x + wm.x > area.br.x ) {
pos.x = area.tl.x;
pos.y += wm.y;
}
drawLeft( canvas, pos, word, colour );
i = e + 1;
}
}
drawLeft( canvas, pos, text.substr(i), colour );
}
const Font* Font::titleFont()
{
static Font* f = new Font("femkeklaver",48);
return f;
}
const Font* Font::headingFont()
{
static Font* f = new Font("femkeklaver",32);
return f;
}
const Font* Font::blurbFont()
{
static Font* f = new Font("femkeklaver",24);
return f;
}
|