2 * This file is part of NumptyPhysics
3 * Copyright (C) 2008 Tim Edmonds
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 3 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
19 #include <SDL/SDL_ttf.h>
21 #define FONT(fONTpTR) ((TTF_Font*)((fONTpTR)->m_state))
23 struct FontCanvas
: public Canvas
25 FontCanvas( SDL_Surface
* s
)
31 Font::Font( const std::string
& file
, int ptsize
)
34 std::string fname
= Config::findFile(file
);
35 m_state
= TTF_OpenFont( fname
.c_str(), ptsize
);
39 Vec2
Font::metrics( const std::string
& text
) const
42 TTF_SizeText( FONT(this), text
.c_str(), &m
.x
, &m
.y
);
46 Canvas
* Font::renderFont(const std::string
& text
, int colour
) const
48 return new FontCanvas( TTF_RenderText_Blended( FONT(this),
53 void Font::drawLeft( Canvas
* canvas
, Vec2 pt
,
54 const std::string
& text
, int colour
) const
56 Canvas
* temp
= renderFont(text
, colour
);
57 canvas
->drawImage(temp
, pt
.x
, pt
.y
);
61 void Font::drawCenter( Canvas
* canvas
, Vec2 pt
,
62 const std::string
& text
, int colour
) const
64 drawLeft( canvas
, pt
- metrics(text
)/2, text
, colour
);
67 void Font::drawWrap( Canvas
* canvas
, Rect area
,
68 const std::string
& text
, int colour
) const
72 while ( i
< text
.length() ) {
73 e
= text
.find_first_of(" \n\r\t",i
);
77 std::string word
= text
.substr(i
,i
+e
);
78 Vec2 wm
= metrics( word
);
79 if ( pos
.x
+ wm
.x
> area
.br
.x
) {
83 drawLeft( canvas
, pos
, word
, colour
);
87 drawLeft( canvas
, pos
, text
.substr(i
), colour
);
91 const Font
* Font::titleFont()
93 static Font
* f
= new Font("femkeklaver.ttf",36);
97 const Font
* Font::headingFont()
99 static Font
* f
= new Font("femkeklaver.ttf",24);
103 const Font
* Font::blurbFont()
105 static Font
* f
= new Font("femkeklaver.ttf",16);