1 | /* |
---|
2 | * font.c: font handling |
---|
3 | * $Id: font.c 2315 2008-04-26 07:25:03Z 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 *file, char *chars) |
---|
21 | { |
---|
22 | char fontname[BUFSIZ]; |
---|
23 | struct font *font; |
---|
24 | struct image *img; |
---|
25 | int i; |
---|
26 | |
---|
27 | sprintf(fontname, "%s/%s", share, file); |
---|
28 | img = image_load(fontname); |
---|
29 | if(!img) |
---|
30 | { |
---|
31 | fprintf(stderr, "%s: cannot load font %s\n", argv0, fontname); |
---|
32 | return NULL; |
---|
33 | } |
---|
34 | |
---|
35 | font = malloc(sizeof(struct font)); |
---|
36 | font->img = img; |
---|
37 | font->size = strlen(chars); |
---|
38 | font->glyphs = malloc(font->size * sizeof(struct glyph)); |
---|
39 | |
---|
40 | for(i = 0; i < font->size; i++) |
---|
41 | { |
---|
42 | font->glyphs[i].xmin = i * font->img->width / font->size; |
---|
43 | font->glyphs[i].xmax = (i + 1) * font->img->width / font->size; |
---|
44 | font->glyphs[i].ymin = 0; |
---|
45 | font->glyphs[i].ymax = font->img->height; |
---|
46 | font->glyphs[i].count = 0; /* They all have the same width anyway */ |
---|
47 | font->glyphs[i].c = chars[i]; |
---|
48 | } |
---|
49 | |
---|
50 | return font; |
---|
51 | } |
---|
52 | |
---|
53 | struct font *font_load_variable(char *file, char *chars) |
---|
54 | { |
---|
55 | char fontname[BUFSIZ]; |
---|
56 | struct font *font; |
---|
57 | struct image *img; |
---|
58 | int count = 0, incell = 0, xmin, xmax, ymin, ymax; |
---|
59 | int x, y, i; |
---|
60 | int r, g, b; |
---|
61 | |
---|
62 | sprintf(fontname, "%s/%s", share, file); |
---|
63 | img = image_load(fontname); |
---|
64 | if(!img) |
---|
65 | { |
---|
66 | fprintf(stderr, "%s: cannot load font %s\n", argv0, fontname); |
---|
67 | return NULL; |
---|
68 | } |
---|
69 | |
---|
70 | font = malloc(sizeof(struct font)); |
---|
71 | font->img = img; |
---|
72 | font->size = strlen(chars); |
---|
73 | font->glyphs = malloc(font->size * sizeof(struct glyph)); |
---|
74 | |
---|
75 | for(x = 0, i = 0, xmin = 0; x <= font->img->width && i < font->size; x++) |
---|
76 | { |
---|
77 | int found = 0; |
---|
78 | if(x != font->img->width) |
---|
79 | for(y = 0; y < font->img->height; y++) |
---|
80 | { |
---|
81 | getpixel(font->img, x, y, &r, &g, &b); |
---|
82 | if(r < 250) |
---|
83 | { |
---|
84 | found = 1; |
---|
85 | count += (255 - r); |
---|
86 | } |
---|
87 | } |
---|
88 | else |
---|
89 | found = 0; |
---|
90 | |
---|
91 | if(found && !incell) |
---|
92 | { |
---|
93 | incell = 1; |
---|
94 | xmin = x; |
---|
95 | } |
---|
96 | else if(!found && incell) |
---|
97 | { |
---|
98 | incell = 0; |
---|
99 | xmax = x; |
---|
100 | #if 0 |
---|
101 | ymin = font->img->height; |
---|
102 | ymax = 0; |
---|
103 | for(y = 0; y < font->img->height; y++) |
---|
104 | { |
---|
105 | int newx; |
---|
106 | int gotit = 0; |
---|
107 | for(newx = xmin; newx < xmax; newx++) |
---|
108 | { |
---|
109 | getpixel(font->img, newx, y, &r, &g, &b); |
---|
110 | if(r < 250) |
---|
111 | { |
---|
112 | gotit = 1; |
---|
113 | break; |
---|
114 | } |
---|
115 | } |
---|
116 | if(gotit) |
---|
117 | { |
---|
118 | if(ymin > y) ymin = y; |
---|
119 | if(ymax <= y) ymax = y + 1; |
---|
120 | } |
---|
121 | } |
---|
122 | #else |
---|
123 | ymin = 0; |
---|
124 | ymax = font->img->height; |
---|
125 | #endif |
---|
126 | font->glyphs[i].xmin = xmin; |
---|
127 | font->glyphs[i].xmax = xmax; |
---|
128 | font->glyphs[i].ymin = ymin; |
---|
129 | font->glyphs[i].ymax = ymax; |
---|
130 | font->glyphs[i].count = count; |
---|
131 | font->glyphs[i].c = chars[i]; |
---|
132 | count = 0; |
---|
133 | i++; |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | if(i != font->size) |
---|
138 | { |
---|
139 | fprintf(stderr, "%s: could only find %i of %i glyphs in font %s\n", |
---|
140 | argv0, i, font->size, file); |
---|
141 | image_free(font->img); |
---|
142 | free(font->glyphs); |
---|
143 | free(font); |
---|
144 | return NULL; |
---|
145 | } |
---|
146 | |
---|
147 | return font; |
---|
148 | } |
---|
149 | |
---|
150 | void font_free(struct font *font) |
---|
151 | { |
---|
152 | image_free(font->img); |
---|
153 | free(font->glyphs); |
---|
154 | free(font); |
---|
155 | } |
---|
156 | |
---|