1 | /* |
---|
2 | * font.c: font handling |
---|
3 | * $Id: font.c 2318 2008-04-26 08:41:43Z sam $ |
---|
4 | * |
---|
5 | * Copyright: (c) 2005 Sam Hocevar <sam@zoy.org> |
---|
6 | * This program is free software. It comes without any warranty, to |
---|
7 | * the extent permitted by applicable law. You can redistribute it |
---|
8 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
9 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
10 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
11 | */ |
---|
12 | |
---|
13 | #include <stdio.h> |
---|
14 | #include <stdlib.h> |
---|
15 | #include <string.h> |
---|
16 | |
---|
17 | #include "config.h" |
---|
18 | #include "common.h" |
---|
19 | |
---|
20 | struct font *font_load_fixed(char const *decoder, char const *file, |
---|
21 | char const *chars) |
---|
22 | { |
---|
23 | char fontname[BUFSIZ]; |
---|
24 | struct font *font; |
---|
25 | struct image *img; |
---|
26 | int i; |
---|
27 | |
---|
28 | sprintf(fontname, "src/%s/%s", decoder, file); |
---|
29 | img = image_load(fontname); |
---|
30 | if(!img) |
---|
31 | { |
---|
32 | sprintf(fontname, "%s/%s/%s", share, decoder, file); |
---|
33 | img = image_load(fontname); |
---|
34 | } |
---|
35 | if(!img) |
---|
36 | { |
---|
37 | fprintf(stderr, "%s: cannot load font %s\n", argv0, fontname); |
---|
38 | return NULL; |
---|
39 | } |
---|
40 | |
---|
41 | font = malloc(sizeof(struct font)); |
---|
42 | font->img = img; |
---|
43 | font->size = strlen(chars); |
---|
44 | font->glyphs = malloc(font->size * sizeof(struct glyph)); |
---|
45 | |
---|
46 | for(i = 0; i < font->size; i++) |
---|
47 | { |
---|
48 | font->glyphs[i].xmin = i * font->img->width / font->size; |
---|
49 | font->glyphs[i].xmax = (i + 1) * font->img->width / font->size; |
---|
50 | font->glyphs[i].ymin = 0; |
---|
51 | font->glyphs[i].ymax = font->img->height; |
---|
52 | font->glyphs[i].count = 0; /* They all have the same width anyway */ |
---|
53 | font->glyphs[i].c = chars[i]; |
---|
54 | } |
---|
55 | |
---|
56 | return font; |
---|
57 | } |
---|
58 | |
---|
59 | struct font *font_load_variable(char const *decoder, char const *file, |
---|
60 | char const *chars) |
---|
61 | { |
---|
62 | char fontname[BUFSIZ]; |
---|
63 | struct font *font; |
---|
64 | struct image *img; |
---|
65 | int count = 0, incell = 0, xmin, xmax, ymin, ymax; |
---|
66 | int x, y, i; |
---|
67 | int r, g, b; |
---|
68 | |
---|
69 | sprintf(fontname, "src/%s/%s", decoder, file); |
---|
70 | img = image_load(fontname); |
---|
71 | if(!img) |
---|
72 | { |
---|
73 | sprintf(fontname, "%s/%s/%s", share, decoder, file); |
---|
74 | img = image_load(fontname); |
---|
75 | } |
---|
76 | if(!img) |
---|
77 | { |
---|
78 | fprintf(stderr, "%s: cannot load font %s\n", argv0, fontname); |
---|
79 | return NULL; |
---|
80 | } |
---|
81 | |
---|
82 | font = malloc(sizeof(struct font)); |
---|
83 | font->img = img; |
---|
84 | font->size = strlen(chars); |
---|
85 | font->glyphs = malloc(font->size * sizeof(struct glyph)); |
---|
86 | |
---|
87 | for(x = 0, i = 0, xmin = 0; x <= font->img->width && i < font->size; x++) |
---|
88 | { |
---|
89 | int found = 0; |
---|
90 | if(x != font->img->width) |
---|
91 | for(y = 0; y < font->img->height; y++) |
---|
92 | { |
---|
93 | getpixel(font->img, x, y, &r, &g, &b); |
---|
94 | if(r < 250) |
---|
95 | { |
---|
96 | found = 1; |
---|
97 | count += (255 - r); |
---|
98 | } |
---|
99 | } |
---|
100 | else |
---|
101 | found = 0; |
---|
102 | |
---|
103 | if(found && !incell) |
---|
104 | { |
---|
105 | incell = 1; |
---|
106 | xmin = x; |
---|
107 | } |
---|
108 | else if(!found && incell) |
---|
109 | { |
---|
110 | incell = 0; |
---|
111 | xmax = x; |
---|
112 | #if 0 |
---|
113 | ymin = font->img->height; |
---|
114 | ymax = 0; |
---|
115 | for(y = 0; y < font->img->height; y++) |
---|
116 | { |
---|
117 | int newx; |
---|
118 | int gotit = 0; |
---|
119 | for(newx = xmin; newx < xmax; newx++) |
---|
120 | { |
---|
121 | getpixel(font->img, newx, y, &r, &g, &b); |
---|
122 | if(r < 250) |
---|
123 | { |
---|
124 | gotit = 1; |
---|
125 | break; |
---|
126 | } |
---|
127 | } |
---|
128 | if(gotit) |
---|
129 | { |
---|
130 | if(ymin > y) ymin = y; |
---|
131 | if(ymax <= y) ymax = y + 1; |
---|
132 | } |
---|
133 | } |
---|
134 | #else |
---|
135 | ymin = 0; |
---|
136 | ymax = font->img->height; |
---|
137 | #endif |
---|
138 | font->glyphs[i].xmin = xmin; |
---|
139 | font->glyphs[i].xmax = xmax; |
---|
140 | font->glyphs[i].ymin = ymin; |
---|
141 | font->glyphs[i].ymax = ymax; |
---|
142 | font->glyphs[i].count = count; |
---|
143 | font->glyphs[i].c = chars[i]; |
---|
144 | count = 0; |
---|
145 | i++; |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | if(i != font->size) |
---|
150 | { |
---|
151 | fprintf(stderr, "%s: could only find %i of %i glyphs in font %s\n", |
---|
152 | argv0, i, font->size, file); |
---|
153 | image_free(font->img); |
---|
154 | free(font->glyphs); |
---|
155 | free(font); |
---|
156 | return NULL; |
---|
157 | } |
---|
158 | |
---|
159 | return font; |
---|
160 | } |
---|
161 | |
---|
162 | void font_free(struct font *font) |
---|
163 | { |
---|
164 | image_free(font->img); |
---|
165 | free(font->glyphs); |
---|
166 | free(font); |
---|
167 | } |
---|
168 | |
---|