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