1 | /* SFont: a simple font-library that uses special .pngs as fonts |
---|
2 | Copyright (C) 2003 Karl Bartel |
---|
3 | |
---|
4 | License: GPL or LGPL (at your choice) |
---|
5 | WWW: http://www.linux-games.com/sfont/ |
---|
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 | Karl Bartel |
---|
22 | Cecilienstr. 14 |
---|
23 | 12307 Berlin |
---|
24 | GERMANY |
---|
25 | karlb@gmx.net |
---|
26 | */ |
---|
27 | #include <SDL.h> |
---|
28 | |
---|
29 | #include <assert.h> |
---|
30 | #include <stdlib.h> |
---|
31 | #include "SFont.h" |
---|
32 | |
---|
33 | static Uint32 GetPixel(SDL_Surface *Surface, Sint32 X, Sint32 Y) |
---|
34 | { |
---|
35 | Uint8 *bits; |
---|
36 | Uint32 Bpp; |
---|
37 | |
---|
38 | assert(X>=0); |
---|
39 | assert(X<Surface->w); |
---|
40 | |
---|
41 | Bpp = Surface->format->BytesPerPixel; |
---|
42 | bits = ((Uint8 *)Surface->pixels)+Y*Surface->pitch+X*Bpp; |
---|
43 | |
---|
44 | // Get the pixel |
---|
45 | switch(Bpp) { |
---|
46 | case 1: |
---|
47 | return *((Uint8 *)Surface->pixels + Y * Surface->pitch + X); |
---|
48 | break; |
---|
49 | case 2: |
---|
50 | return *((Uint16 *)Surface->pixels + Y * Surface->pitch/2 + X); |
---|
51 | break; |
---|
52 | case 3: { // Format/endian independent |
---|
53 | Uint8 r, g, b; |
---|
54 | r = *((bits)+Surface->format->Rshift/8); |
---|
55 | g = *((bits)+Surface->format->Gshift/8); |
---|
56 | b = *((bits)+Surface->format->Bshift/8); |
---|
57 | return SDL_MapRGB(Surface->format, r, g, b); |
---|
58 | } |
---|
59 | break; |
---|
60 | case 4: |
---|
61 | return *((Uint32 *)Surface->pixels + Y * Surface->pitch/4 + X); |
---|
62 | break; |
---|
63 | } |
---|
64 | |
---|
65 | return -1; |
---|
66 | } |
---|
67 | |
---|
68 | SFont_Font* SFont_InitFont(SDL_Surface* Surface) |
---|
69 | { |
---|
70 | int x = 0, i = 0; |
---|
71 | Uint32 pixel; |
---|
72 | SFont_Font* Font; |
---|
73 | Uint32 pink; |
---|
74 | |
---|
75 | if (Surface == NULL) |
---|
76 | return NULL; |
---|
77 | |
---|
78 | Font = (SFont_Font *) malloc(sizeof(SFont_Font)); |
---|
79 | Font->Surface = Surface; |
---|
80 | |
---|
81 | SDL_LockSurface(Surface); |
---|
82 | |
---|
83 | pink = SDL_MapRGB(Surface->format, 255, 0, 255); |
---|
84 | while (x < Surface->w) { |
---|
85 | if (GetPixel(Surface, x, 0) == pink) { |
---|
86 | Font->CharPos[i++]=x; |
---|
87 | while((x < Surface->w) && (GetPixel(Surface, x, 0)== pink)) |
---|
88 | x++; |
---|
89 | Font->CharPos[i++]=x; |
---|
90 | } |
---|
91 | x++; |
---|
92 | } |
---|
93 | Font->MaxPos = x-1; |
---|
94 | |
---|
95 | pixel = GetPixel(Surface, 0, Surface->h-1); |
---|
96 | SDL_UnlockSurface(Surface); |
---|
97 | SDL_SetColorKey(Surface, SDL_SRCCOLORKEY, pixel); |
---|
98 | |
---|
99 | return Font; |
---|
100 | } |
---|
101 | |
---|
102 | void SFont_FreeFont(SFont_Font* FontInfo) |
---|
103 | { |
---|
104 | SDL_FreeSurface(FontInfo->Surface); |
---|
105 | free(FontInfo); |
---|
106 | } |
---|
107 | |
---|
108 | void SFont_Write(SDL_Surface *Surface, const SFont_Font *Font, |
---|
109 | int x, int y, const char *text) |
---|
110 | { |
---|
111 | const char* c; |
---|
112 | int charoffset; |
---|
113 | SDL_Rect srcrect, dstrect; |
---|
114 | |
---|
115 | if(text == NULL) |
---|
116 | return; |
---|
117 | |
---|
118 | // these values won't change in the loop |
---|
119 | srcrect.y = 1; |
---|
120 | dstrect.y = y; |
---|
121 | srcrect.h = dstrect.h = Font->Surface->h - 1; |
---|
122 | |
---|
123 | for(c = text; *c != '\0' && x <= Surface->w ; c++) { |
---|
124 | charoffset = ((int) (*c - 33)) * 2 + 1; |
---|
125 | // skip spaces and nonprintable characters |
---|
126 | if (*c == ' ' || charoffset < 0 || charoffset > Font->MaxPos) { |
---|
127 | x += Font->CharPos[2]-Font->CharPos[1]; |
---|
128 | continue; |
---|
129 | } |
---|
130 | |
---|
131 | srcrect.w = dstrect.w = |
---|
132 | (Font->CharPos[charoffset+2] + Font->CharPos[charoffset+1])/2 - |
---|
133 | (Font->CharPos[charoffset] + Font->CharPos[charoffset-1])/2; |
---|
134 | srcrect.x = (Font->CharPos[charoffset]+Font->CharPos[charoffset-1])/2; |
---|
135 | dstrect.x = x - (float)(Font->CharPos[charoffset] |
---|
136 | - Font->CharPos[charoffset-1])/2; |
---|
137 | |
---|
138 | SDL_BlitSurface(Font->Surface, &srcrect, Surface, &dstrect); |
---|
139 | |
---|
140 | x += Font->CharPos[charoffset+1] - Font->CharPos[charoffset]; |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | int SFont_TextWidth(const SFont_Font *Font, const char *text) |
---|
145 | { |
---|
146 | const char* c; |
---|
147 | int charoffset=0; |
---|
148 | int width = 0; |
---|
149 | |
---|
150 | if(text == NULL) |
---|
151 | return 0; |
---|
152 | |
---|
153 | for(c = text; *c != '\0'; c++) { |
---|
154 | charoffset = ((int) *c - 33) * 2 + 1; |
---|
155 | // skip spaces and nonprintable characters |
---|
156 | if (*c == ' ' || charoffset < 0 || charoffset > Font->MaxPos) { |
---|
157 | width += Font->CharPos[2]-Font->CharPos[1]; |
---|
158 | continue; |
---|
159 | } |
---|
160 | |
---|
161 | width += Font->CharPos[charoffset+1] - Font->CharPos[charoffset]; |
---|
162 | } |
---|
163 | |
---|
164 | return width; |
---|
165 | } |
---|
166 | |
---|
167 | int SFont_TextHeight(const SFont_Font* Font) |
---|
168 | { |
---|
169 | return Font->Surface->h - 1; |
---|
170 | } |
---|
171 | |
---|
172 | void SFont_WriteCenter(SDL_Surface *Surface, const SFont_Font *Font, |
---|
173 | int y, const char *text) |
---|
174 | { |
---|
175 | SFont_Write(Surface, Font, Surface->w/2 - SFont_TextWidth(Font, text)/2, |
---|
176 | y, text); |
---|
177 | } |
---|
178 | |
---|