1 /* font.cc
2    Manage textured-font for drawing text
3 
4    Copyright (C) 2000  Mathias Broxvall
5                        Yannick Perret
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 */
21 
22 #include "font.h"
23 #include "glHelp.h"
24 
25 #include <SDL2/SDL_ttf.h>
26 
27 // returns the effective width of the text
getTextWidth(const char * text,int size,int maxwidth)28 int getTextWidth(const char *text, int size, int maxwidth) {
29   if (!text || (strlen(text) == 0)) return (0);
30 
31   int w, h;
32   TTF_Font *active = menuFontForSize(size);
33   TTF_SetFontOutline(active, 2);
34   TTF_SizeUTF8(active, text, &w, &h);
35   TTF_SetFontOutline(active, 0);
36   if (maxwidth > 0 && maxwidth < w) return maxwidth;
37   return w;
38 }
39 
drawSimpleText(const char * text,int x,int y,int sz,float r,float g,float b,float a,int maxwidth)40 int drawSimpleText(const char *text, int x, int y, int sz, float r, float g, float b, float a,
41                    int maxwidth) {
42   if (!text || (strlen(text) == 0)) return 0;
43 
44   int size = (int)sz;
45   TTF_Font *active = menuFontForSize(size);
46   return draw2DString(active, text, x, y, r, g, b, a, sz >= 10, 0, maxwidth);
47 }
48 
drawRightSimpleText(const char * text,int x,int y,int sz,float r,float g,float b,float a,int maxwidth)49 int drawRightSimpleText(const char *text, int x, int y, int sz, float r, float g, float b,
50                         float a, int maxwidth) {
51   if (!text || (strlen(text) == 0)) return 0;
52 
53   int size = (int)sz;
54   TTF_Font *active = menuFontForSize(size);
55   return draw2DString(active, text, x, y, r, g, b, a, sz >= 10, 2, maxwidth);
56 }
57 
drawCenterSimpleText(const char * text,int x,int y,int sz,float r,float g,float b,float a,int maxwidth)58 int drawCenterSimpleText(const char *text, int x, int y, int sz, float r, float g, float b,
59                          float a, int maxwidth) {
60   if (!text || (strlen(text) == 0)) return 0;
61 
62   int size = (int)sz;
63   TTF_Font *active = menuFontForSize(size);
64   return draw2DString(active, text, x, y, r, g, b, a, sz >= 10, 1, maxwidth);
65 }
66