-
Notifications
You must be signed in to change notification settings - Fork 16
/
Font.cpp
111 lines (95 loc) · 2.56 KB
/
Font.cpp
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
/*
* 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>
#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();
std::string fname = Config::findFile(file);
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.ttf",48);
return f;
}
const Font* Font::headingFont()
{
static Font* f = new Font("femkeklaver.ttf",32);
return f;
}
const Font* Font::blurbFont()
{
static Font* f = new Font("femkeklaver.ttf",24);
return f;
}