Add pointlog2svg utility for the thesis
[numtypysics.git] / Font.cpp
blob1b4a386d00752b857f01f6c0d4b15ab641c7d3f6
1 /*
2 * This file is part of NumptyPhysics
3 * Copyright (C) 2008 Tim Edmonds
4 *
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.
9 *
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.
16 #include "Font.h"
17 #include "Canvas.h"
18 #include "Config.h"
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 )
26 : Canvas( s )
31 Font::Font( const std::string& file, int ptsize )
33 TTF_Init();
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
41 Vec2 m;
42 TTF_SizeText( FONT(this), text.c_str(), &m.x, &m.y );
43 return m;
46 Canvas* Font::renderFont(const std::string& text, int colour) const
48 return new FontCanvas( TTF_RenderText_Blended( FONT(this),
49 text.c_str(),
50 SDL_Color() ) );
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);
58 delete temp;
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
70 Vec2 pos = area.tl;
71 size_t i = 0, e=0;
72 while ( i < text.length() ) {
73 e = text.find_first_of(" \n\r\t",i);
74 if ( i == e ) {
75 i++;
76 } else {
77 std::string word = text.substr(i,i+e);
78 Vec2 wm = metrics( word );
79 if ( pos.x + wm.x > area.br.x ) {
80 pos.x = area.tl.x;
81 pos.y += wm.y;
83 drawLeft( canvas, pos, word, colour );
84 i = e + 1;
87 drawLeft( canvas, pos, text.substr(i), colour );
91 const Font* Font::titleFont()
93 static Font* f = new Font("femkeklaver.ttf",36);
94 return f;
97 const Font* Font::headingFont()
99 static Font* f = new Font("femkeklaver.ttf",24);
100 return f;
103 const Font* Font::blurbFont()
105 static Font* f = new Font("femkeklaver.ttf",16);
106 return f;