annotate libvo/ttf_load.c @ 6576:b8ed96e21f2c

{RGB,BGR}{1,4}
author michael
date Thu, 27 Jun 2002 17:35:37 +0000
parents 9fc7bcadcc1b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5379
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
1 /*
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
2 * Renders antialiased fonts for mplayer using freetype library.
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
3 * Should work with TrueType, Type1 and any other font supported by libfreetype.
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
4 * Can generate font.desc for any encoding.
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
5 *
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
6 *
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
7 * Artur Zaprzala <zybi@fanthom.irc.pl>
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
8 *
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
9 */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
10
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
11 #include <stdio.h>
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
12 #include <stdlib.h>
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
13 #include <iconv.h>
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
14 #include <math.h>
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
15 #include <string.h>
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
16 #include <libgen.h>
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
17
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
18 #ifndef OLD_FREETYPE2
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
19 #include <ft2build.h>
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
20 #include FT_FREETYPE_H
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
21 #include FT_GLYPH_H
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
22 #else /* freetype 2.0.1 */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
23 #include <freetype/freetype.h>
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
24 #include <freetype/ftglyph.h>
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
25 #endif
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
26
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
27 #include "../config.h"
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
28 #include "../mp_msg.h"
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
29
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
30 #include "../bswap.h"
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
31
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
32
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
33 #ifndef DEBUG
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
34 #define DEBUG 0
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
35 #endif
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
36
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
37 #include "font_load.h"
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
38
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
39 //// default values
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
40 static char *encoding = "iso-8859-1"; /* target encoding */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
41 static char *charmap = "ucs-4"; /* font charmap encoding, I hope ucs-4 is always big endian */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
42 /* gcc 2.1.3 doesn't support ucs-4le, but supports ucs-4 (==ucs-4be) */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
43 static float ppem = 28; /* font size in pixels */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
44
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
45 static double radius = 2; /* blur radius */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
46 static double thickness = 2.5; /* outline thickness */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
47
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
48 //char* font_desc = "font.desc";
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
49 //char* font_desc = "/dev/stdout";
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
50
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
51 //char *outdir = ".";
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
52
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
53 static font_desc_t *desc=NULL;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
54
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
55 //// constants
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
56 static int const colors = 256;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
57 static int const maxcolor = 255;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
58 static unsigned const base = 256;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
59 static unsigned const first_char = 33;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
60 #define max_charset_size 60000
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
61 //int const max_charset_size = 256;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
62 static unsigned charset_size = 0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
63
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
64 ////
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
65 //static char *command;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
66 //static char *encoding_name;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
67 static char *font_path=NULL;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
68 //char *font_metrics;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
69 //static int append_mode = 0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
70 static int unicode_desc = 0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
71 static int font_id=0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
72
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
73 static unsigned char *bbuffer, *abuffer;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
74 static int width, height;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
75 static int padding;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
76 static FT_ULong charset[max_charset_size]; /* characters we want to render; Unicode */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
77 static FT_ULong charcodes[max_charset_size]; /* character codes in 'encoding' */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
78 static iconv_t cd; // iconv conversion descriptor
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
79
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
80
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
81 #define eprintf(...) mp_msg(MSGT_CPLAYER,MSGL_INFO, __VA_ARGS__)
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
82 #define ERROR_(msg, ...) (mp_msg(MSGT_CPLAYER,MSGL_ERR,"[font_load] error: " msg "\n", __VA_ARGS__), exit(1))
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
83 #define WARNING_(msg, ...) mp_msg(MSGT_CPLAYER,MSGL_WARN,"[font_load] warning: " msg "\n", __VA_ARGS__)
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
84 #define ERROR(...) ERROR_(__VA_ARGS__, NULL)
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
85 #define WARNING(...) WARNING_(__VA_ARGS__, NULL)
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
86
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
87
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
88 #define f266ToInt(x) (((x)+32)>>6) // round fractional fixed point number to integer
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
89 // coordinates are in 26.6 pixels (i.e. 1/64th of pixels)
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
90 #define f266CeilToInt(x) (((x)+63)>>6) // ceiling
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
91 #define f266FloorToInt(x) ((x)>>6) // floor
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
92 #define f1616ToInt(x) (((x)+0x8000)>>16) // 16.16
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
93 #define floatTof266(x) ((int)((x)*(1<<6)+0.5))
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
94
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
95 #define ALIGN(x) (((x)+7)&~7) // 8 byte align
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
96
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
97
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
98
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
99 static void paste_bitmap(FT_Bitmap *bitmap, int x, int y) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
100 int drow = x+y*width;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
101 int srow = 0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
102 int sp, dp, w, h;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
103 if (bitmap->pixel_mode==ft_pixel_mode_mono)
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
104 for (h = bitmap->rows; h>0; --h, drow+=width, srow+=bitmap->pitch)
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
105 for (w = bitmap->width, sp=dp=0; w>0; --w, ++dp, ++sp)
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
106 bbuffer[drow+dp] = (bitmap->buffer[srow+sp/8] & (0x80>>(sp%8))) ? 255:0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
107 else
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
108 for (h = bitmap->rows; h>0; --h, drow+=width, srow+=bitmap->pitch)
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
109 for (w = bitmap->width, sp=dp=0; w>0; --w, ++dp, ++sp)
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
110 bbuffer[drow+dp] = bitmap->buffer[srow+sp];
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
111 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
112
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
113
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
114 static void render() {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
115 FT_Library library;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
116 FT_Face face;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
117 FT_Error error;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
118 FT_Glyph *glyphs;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
119 FT_BitmapGlyph glyph;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
120 //FILE *f;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
121 int const load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
122 int pen_x = 0, pen_xa;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
123 int ymin = INT_MAX, ymax = INT_MIN;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
124 int i, uni_charmap = 1;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
125 int baseline, space_advance = 20;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
126 int glyphs_count = 0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
127
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
128
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
129 /* initialize freetype */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
130 error = FT_Init_FreeType(&library);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
131 if (error) ERROR("Init_FreeType failed.");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
132 error = FT_New_Face(library, font_path, 0, &face);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
133 if (error) ERROR("New_Face failed. Maybe the font path `%s' is wrong.", font_path);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
134
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
135 /*
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
136 if (font_metrics) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
137 error = FT_Attach_File(face, font_metrics);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
138 if (error) WARNING("FT_Attach_File failed.");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
139 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
140 */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
141
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
142
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
143 #if 0
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
144 /************************************************************/
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
145 eprintf("Font encodings:\n");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
146 for (i = 0; i<face->num_charmaps; ++i)
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
147 eprintf("'%.4s'\n", (char*)&face->charmaps[i]->encoding);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
148
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
149 //error = FT_Select_Charmap(face, ft_encoding_unicode);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
150 //error = FT_Select_Charmap(face, ft_encoding_adobe_standard);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
151 //error = FT_Select_Charmap(face, ft_encoding_adobe_custom);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
152 //error = FT_Set_Charmap(face, face->charmaps[1]);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
153 //if (error) WARNING("FT_Select_Charmap failed.");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
154 #endif
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
155
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
156
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
157 #if 0
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
158 /************************************************************/
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
159 if (FT_HAS_GLYPH_NAMES(face)) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
160 int const max_gname = 128;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
161 char gname[max_gname];
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
162 for (i = 0; i<face->num_glyphs; ++i) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
163 FT_Get_Glyph_Name(face, i, gname, max_gname);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
164 eprintf("%02x `%s'\n", i, gname);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
165 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
166
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
167 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
168 #endif
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
169
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
170
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
171 if (face->charmap==NULL || face->charmap->encoding!=ft_encoding_unicode) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
172 WARNING("Unicode charmap not available for this font. Very bad!");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
173 uni_charmap = 0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
174 error = FT_Set_Charmap(face, face->charmaps[0]);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
175 if (error) WARNING("No charmaps! Strange.");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
176 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
177
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
178
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
179
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
180 /* set size */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
181 if (FT_IS_SCALABLE(face)) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
182 error = FT_Set_Char_Size(face, floatTof266(ppem), 0, 0, 0);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
183 if (error) WARNING("FT_Set_Char_Size failed.");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
184 } else {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
185 int j = 0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
186 int jppem = face->available_sizes[0].height;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
187 /* find closest size */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
188 for (i = 0; i<face->num_fixed_sizes; ++i) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
189 if (fabs(face->available_sizes[i].height - ppem) < abs(face->available_sizes[i].height - jppem)) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
190 j = i;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
191 jppem = face->available_sizes[i].height;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
192 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
193 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
194 WARNING("Selected font is not scalable. Using ppem=%i.", face->available_sizes[j].height);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
195 error = FT_Set_Pixel_Sizes(face, face->available_sizes[j].width, face->available_sizes[j].height);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
196 if (error) WARNING("FT_Set_Pixel_Sizes failed.");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
197 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
198
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
199
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
200 if (FT_IS_FIXED_WIDTH(face))
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
201 WARNING("Selected font is fixed-width.");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
202
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
203
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
204 /* compute space advance */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
205 error = FT_Load_Char(face, ' ', load_flags);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
206 if (error) WARNING("spacewidth set to default.");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
207 else space_advance = f266ToInt(face->glyph->advance.x);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
208
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
209
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
210 /* create font.desc */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
211 if(!font_id){ // first font
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
212 desc->spacewidth=2*padding + space_advance;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
213 desc->charspace=-2*padding;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
214 desc->height=f266ToInt(face->size->metrics.height);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
215 // fprintf(f, "ascender %i\n", f266CeilToInt(face->size->metrics.ascender));
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
216 // fprintf(f, "descender %i\n", f266FloorToInt(face->size->metrics.descender));
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
217 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
218
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
219 // render glyphs, compute bitmap size and [characters] section
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
220 glyphs = (FT_Glyph*)malloc(charset_size*sizeof(FT_Glyph*));
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
221 for (i= 0; i<charset_size; ++i) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
222 FT_GlyphSlot slot;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
223 FT_ULong character, code;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
224 FT_UInt glyph_index;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
225 FT_BBox bbox;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
226
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
227 character = charset[i];
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
228 code = charcodes[i];
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
229
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
230 // get glyph index
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
231 if (character==0)
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
232 glyph_index = 0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
233 else {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
234 glyph_index = FT_Get_Char_Index(face, uni_charmap ? character:code);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
235 if (glyph_index==0) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
236 WARNING("Glyph for char 0x%02x|U+%04X|%c not found.", code, character,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
237 code<' '||code>255 ? '.':code);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
238 continue;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
239 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
240 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
241
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
242 // load glyph
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
243 error = FT_Load_Glyph(face, glyph_index, load_flags);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
244 if (error) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
245 WARNING("FT_Load_Glyph 0x%02x (char 0x%02x|U+%04X) failed.", glyph_index, code, character);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
246 continue;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
247 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
248 slot = face->glyph;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
249
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
250 // render glyph
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
251 if (slot->format != ft_glyph_format_bitmap) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
252 error = FT_Render_Glyph(slot, ft_render_mode_normal);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
253 if (error) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
254 WARNING("FT_Render_Glyph 0x%04x (char 0x%02x|U+%04X) failed.", glyph_index, code, character);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
255 continue;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
256 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
257 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
258
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
259 // extract glyph image
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
260 error = FT_Get_Glyph(slot, (FT_Glyph*)&glyph);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
261 if (error) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
262 WARNING("FT_Get_Glyph 0x%04x (char 0x%02x|U+%04X) failed.", glyph_index, code, character);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
263 continue;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
264 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
265 glyphs[glyphs_count++] = (FT_Glyph)glyph;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
266
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
267 #ifdef NEW_DESC
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
268 // max height
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
269 if (glyph->bitmap.rows > height) height = glyph->bitmap.rows;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
270
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
271 // advance pen
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
272 pen_xa = pen_x + glyph->bitmap.width + 2*padding;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
273
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
274 // font.desc
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
275 fprintf(f, "0x%04x %i %i %i %i %i %i;\tU+%04X|%c\n", unicode_desc ? character:code,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
276 pen_x, // bitmap start
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
277 glyph->bitmap.width + 2*padding, // bitmap width
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
278 glyph->bitmap.rows + 2*padding, // bitmap height
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
279 glyph->left - padding, // left bearing
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
280 glyph->top + padding, // top bearing
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
281 f266ToInt(slot->advance.x), // advance
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
282 character, code<' '||code>255 ? '.':code);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
283 #else
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
284 // max height
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
285 if (glyph->top > ymax) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
286 ymax = glyph->top;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
287 //eprintf("%3i: ymax %i (%c)\n", code, ymax, code);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
288 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
289 if (glyph->top - glyph->bitmap.rows < ymin) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
290 ymin = glyph->top - glyph->bitmap.rows;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
291 //eprintf("%3i: ymin %i (%c)\n", code, ymin, code);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
292 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
293
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
294 /* advance pen */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
295 pen_xa = pen_x + f266ToInt(slot->advance.x) + 2*padding;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
296
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
297 /* font.desc */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
298 // fprintf(f, "0x%04x %i %i;\tU+%04X|%c\n", unicode_desc ? character:code,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
299 // pen_x, // bitmap start
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
300 // pen_xa-1, // bitmap end
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
301 // character, code<' '||code>255 ? '.':code);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
302 desc->start[unicode_desc ? character:code]=pen_x;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
303 desc->width[unicode_desc ? character:code]=pen_xa-1-pen_x;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
304 desc->font[unicode_desc ? character:code]=font_id;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
305
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
306 #endif
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
307 pen_x = ALIGN(pen_xa);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
308 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
309
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
310
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
311 width = pen_x;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
312 pen_x = 0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
313 #ifdef NEW_DESC
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
314 if (height<=0) ERROR("Something went wrong. Use the source!");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
315 height += 2*padding;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
316 #else
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
317 if (ymax<=ymin) ERROR("Something went wrong. Use the source!");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
318 height = ymax - ymin + 2*padding;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
319 baseline = ymax + padding;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
320 #endif
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
321
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
322 // end of font.desc
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
323 if (DEBUG) eprintf("bitmap size: %ix%i\n", width, height);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
324 // fprintf(f, "# bitmap size: %ix%i\n", width, height);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
325 // fclose(f);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
326
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
327 bbuffer = (unsigned char*)malloc(width*height);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
328 if (bbuffer==NULL) ERROR("malloc failed.");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
329 memset(bbuffer, 0, width*height);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
330
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
331
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
332 /* paste glyphs */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
333 for (i= 0; i<glyphs_count; ++i) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
334 glyph = (FT_BitmapGlyph)glyphs[i];
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
335 #ifdef NEW_DESC
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
336 paste_bitmap(&glyph->bitmap,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
337 pen_x + padding,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
338 padding);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
339
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
340 /* advance pen */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
341 pen_x += glyph->bitmap.width + 2*padding;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
342 #else
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
343 paste_bitmap(&glyph->bitmap,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
344 pen_x + padding + glyph->left,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
345 baseline - glyph->top);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
346
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
347 /* advance pen */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
348 pen_x += f1616ToInt(glyph->root.advance.x) + 2*padding;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
349 #endif
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
350 pen_x = ALIGN(pen_x);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
351
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
352 FT_Done_Glyph((FT_Glyph)glyph);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
353 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
354 free(glyphs);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
355
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
356
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
357 error = FT_Done_FreeType(library);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
358 if (error) ERROR("FT_Done_FreeType failed.");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
359 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
360
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
361
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
362 /* decode from 'encoding' to unicode */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
363 static FT_ULong decode_char(char c) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
364 FT_ULong o;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
365 char *inbuf = &c;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
366 char *outbuf = (char*)&o;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
367 int inbytesleft = 1;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
368 int outbytesleft = sizeof(FT_ULong);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
369
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
370 size_t count = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
371
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
372 /* convert unicode BigEndian -> MachineEndian */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
373 o = be2me_32(o);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
374
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
375 // if (count==-1) o = 0; // not OK, at least my iconv() returns E2BIG for all
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
376 if (outbytesleft!=0) o = 0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
377
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
378 /* we don't want control characters */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
379 if (o>=0x7f && o<0xa0) o = 0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
380 return o;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
381 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
382
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
383
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
384 static void prepare_charset() {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
385 FILE *f;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
386 FT_ULong i;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
387
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
388 f = fopen(encoding, "r"); // try to read custom encoding
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
389 if (f==NULL) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
390 int count = 0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
391 // check if ucs-4 is available
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
392 cd = iconv_open(charmap, charmap);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
393 if (cd==(iconv_t)-1) ERROR("iconv doesn't know %s encoding. Use the source!", charmap);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
394 iconv_close(cd);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
395
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
396 cd = iconv_open(charmap, encoding);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
397 if (cd==(iconv_t)-1) ERROR("Unsupported encoding `%s', use iconv --list to list character sets known on your system.", encoding);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
398
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
399 charset_size = 256 - first_char;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
400 for (i = 0; i<charset_size; ++i) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
401 charcodes[count] = i+first_char;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
402 charset[count] = decode_char(i+first_char);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
403 //eprintf("%04X U%04X\n", charcodes[count], charset[count]);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
404 if (charset[count]!=0) ++count;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
405 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
406 charcodes[count] = charset[count] = 0; ++count;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
407 charset_size = count;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
408
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
409 iconv_close(cd);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
410 } else {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
411 unsigned int character, code;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
412 int count;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
413
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
414 eprintf("Reading custom encoding from file '%s'.\n", encoding);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
415
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
416 while ((count = fscanf(f, "%x%*[ \t]%x", &character, &code)) != EOF) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
417 if (charset_size==max_charset_size) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
418 WARNING("There is no place for more than %i characters. Use the source!", max_charset_size);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
419 break;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
420 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
421 if (count==0) ERROR("Unable to parse custom encoding file.");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
422 if (character<32) continue; // skip control characters
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
423 charset[charset_size] = character;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
424 charcodes[charset_size] = count==2 ? code : character;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
425 ++charset_size;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
426 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
427 fclose(f);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
428 // encoding = basename(encoding);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
429 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
430 if (charset_size==0) ERROR("No characters to render!");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
431 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
432
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
433
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
434 // general outline
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
435 static void outline(
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
436 unsigned char *s,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
437 unsigned char *t,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
438 int width,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
439 int height,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
440 int *m,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
441 int r,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
442 int mwidth) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
443
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
444 int x, y;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
445 for (y = 0; y<height; ++y) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
446 for (x = 0; x<width; ++x, ++s, ++t) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
447 unsigned max = 0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
448 unsigned *mrow = m + r;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
449 unsigned char *srow = s -r*width;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
450 int x1=(x<r)?-x:-r;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
451 int x2=(x+r>=width)?(width-x-1):r;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
452 int my;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
453
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
454 for (my = -r; my<=r; ++my, srow+= width, mrow+= mwidth) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
455 int mx;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
456 if (y+my < 0) continue;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
457 if (y+my >= height) break;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
458
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
459 for (mx = x1; mx<=x2; ++mx) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
460 unsigned v = srow[mx] * mrow[mx];
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
461 if (v>max) max = v;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
462 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
463 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
464 *t = (max + base/2) / base;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
465 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
466 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
467 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
468
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
469
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
470 // 1 pixel outline
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
471 static void outline1(
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
472 unsigned char *s,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
473 unsigned char *t,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
474 int width,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
475 int height) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
476
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
477 int x, y, mx, my;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
478
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
479 for (x = 0; x<width; ++x, ++s, ++t) *t = *s;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
480 for (y = 1; y<height-1; ++y) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
481 *t++ = *s++;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
482 for (x = 1; x<width-1; ++x, ++s, ++t) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
483 unsigned v = (
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
484 s[-1-width]+
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
485 s[-1+width]+
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
486 s[+1-width]+
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
487 s[+1+width]
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
488 )/2 + (
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
489 s[-1]+
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
490 s[+1]+
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
491 s[-width]+
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
492 s[+width]+
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
493 s[0]
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
494 );
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
495 *t = v>maxcolor ? maxcolor : v;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
496 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
497 *t++ = *s++;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
498 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
499 for (x = 0; x<width; ++x, ++s, ++t) *t = *s;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
500 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
501
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
502
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
503 // gaussian blur
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
504 static void blur(
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
505 unsigned char *buffer,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
506 unsigned char *tmp,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
507 int width,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
508 int height,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
509 int *m,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
510 int r,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
511 int mwidth,
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
512 unsigned volume) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
513
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
514 int x, y;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
515
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
516 unsigned char *s = buffer - r;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
517 unsigned char *t = tmp;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
518 for (y = 0; y<height; ++y) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
519 for (x = 0; x<width; ++x, ++s, ++t) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
520 unsigned sum = 0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
521 int x1 = (x<r) ? r-x:0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
522 int x2 = (x+r>=width) ? (r+width-x):mwidth;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
523 int mx;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
524 for (mx = x1; mx<x2; ++mx)
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
525 sum+= s[mx] * m[mx];
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
526 *t = (sum + volume/2) / volume;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
527 //*t = sum;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
528 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
529 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
530 tmp -= r*width;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
531 for (x = 0; x<width; ++x, ++tmp, ++buffer) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
532 s = tmp;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
533 t = buffer;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
534 for (y = 0; y<height; ++y, s+= width, t+= width) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
535 unsigned sum = 0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
536 int y1 = (y<r) ? r-y:0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
537 int y2 = (y+r>=height) ? (r+height-y):mwidth;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
538 unsigned char *smy = s + y1*width;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
539 int my;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
540 for (my = y1; my<y2; ++my, smy+= width)
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
541 sum+= *smy * m[my];
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
542 *t = (sum + volume/2) / volume;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
543 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
544 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
545 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
546
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
547
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
548 // Gaussian matrix
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
549 // Maybe for future use.
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
550 static unsigned gmatrix(unsigned *m, int r, int w, double const A) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
551 unsigned volume = 0; // volume under Gaussian area is exactly -pi*base/A
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
552 int mx, my;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
553
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
554 for (my = 0; my<w; ++my) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
555 for (mx = 0; mx<w; ++mx) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
556 m[mx+my*w] = (unsigned)(exp(A * ((mx-r)*(mx-r)+(my-r)*(my-r))) * base + .5);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
557 volume+= m[mx+my*w];
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
558 if (DEBUG) eprintf("%3i ", m[mx+my*w]);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
559 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
560 if (DEBUG) eprintf("\n");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
561 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
562 if (DEBUG) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
563 eprintf("A= %f\n", A);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
564 eprintf("volume: %i; exact: %.0f; volume/exact: %.6f\n\n", volume, -M_PI*base/A, volume/(-M_PI*base/A));
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
565 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
566 return volume;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
567 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
568
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
569
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
570 static void alpha() {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
571 int const g_r = ceil(radius);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
572 int const o_r = ceil(thickness);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
573 int const g_w = 2*g_r+1; // matrix size
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
574 int const o_w = 2*o_r+1; // matrix size
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
575 double const A = log(1.0/base)/(radius*radius*2);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
576
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
577 int mx, my, i;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
578 unsigned volume = 0; // volume under Gaussian area is exactly -pi*base/A
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
579
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
580 unsigned *g = (unsigned*)malloc(g_w * sizeof(unsigned));
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
581 unsigned *om = (unsigned*)malloc(o_w*o_w * sizeof(unsigned));
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
582 if (g==NULL || om==NULL) ERROR("malloc failed.");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
583
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
584 // gaussian curve
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
585 for (i = 0; i<g_w; ++i) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
586 g[i] = (unsigned)(exp(A * (i-g_r)*(i-g_r)) * base + .5);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
587 volume+= g[i];
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
588 if (DEBUG) eprintf("%3i ", g[i]);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
589 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
590 //volume *= volume;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
591 if (DEBUG) eprintf("\n");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
592
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
593 /* outline matrix */
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
594 for (my = 0; my<o_w; ++my) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
595 for (mx = 0; mx<o_w; ++mx) {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
596 // antialiased circle would be perfect here, but this one is good enough
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
597 double d = thickness + 1 - sqrt((mx-o_r)*(mx-o_r)+(my-o_r)*(my-o_r));
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
598 om[mx+my*o_w] = d>=1 ? base : d<=0 ? 0 : (d*base + .5);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
599 if (DEBUG) eprintf("%3i ", om[mx+my*o_w]);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
600 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
601 if (DEBUG) eprintf("\n");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
602 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
603 if (DEBUG) eprintf("\n");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
604
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
605
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
606 if(thickness==1.0)
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
607 outline1(bbuffer, abuffer, width, height); // FAST solid 1 pixel outline
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
608 else
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
609 outline(bbuffer, abuffer, width, height, om, o_r, o_w); // solid outline
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
610
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
611 //outline(bbuffer, abuffer, width, height, gm, g_r, g_w); // Gaussian outline
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
612
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
613 blur(abuffer, bbuffer, width, height, g, g_r, g_w, volume);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
614
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
615 free(g);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
616 free(om);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
617 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
618
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
619 font_desc_t* read_font_desc(char* fname,float factor,int verbose){
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
620 int i=font_id;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
621 int j;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
622
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
623 if(!font_id){
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
624 desc=malloc(sizeof(font_desc_t));if(!desc) return NULL;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
625 memset(desc,0,sizeof(font_desc_t));
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
626 memset(desc->font,255,sizeof(short)*65536);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
627 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
628
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
629 padding = ceil(radius) + ceil(thickness);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
630
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
631 font_path=fname;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
632
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
633 prepare_charset();
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
634
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
635 render();
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
636 desc->pic_b[font_id]=malloc(sizeof(raw_file));
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
637 desc->pic_b[font_id]->bmp=malloc(width*height);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
638 memcpy(desc->pic_b[font_id]->bmp,bbuffer,width*height);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
639 desc->pic_b[font_id]->pal=NULL;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
640 desc->pic_b[font_id]->w=width;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
641 desc->pic_b[font_id]->h=height;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
642 desc->pic_b[font_id]->c=256;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
643 // write_bitmap(bbuffer, 'b');
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
644
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
645 abuffer = (unsigned char*)malloc(width*height);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
646 if (abuffer==NULL) ERROR("malloc failed.");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
647 alpha();
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
648 // write_bitmap(abuffer, 'a');
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
649 desc->pic_a[font_id]=malloc(sizeof(raw_file));
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
650 desc->pic_a[font_id]->bmp=abuffer;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
651 // desc->pic_a[font_id]->bmp=malloc(width*height);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
652 // memcpy(desc->pic_a[font_id]->bmp,abuffer,width*height);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
653 desc->pic_a[font_id]->pal=NULL;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
654 desc->pic_a[font_id]->w=width;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
655 desc->pic_a[font_id]->h=height;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
656 desc->pic_a[font_id]->c=256;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
657
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
658 free(bbuffer);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
659 // free(abuffer);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
660
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
661 //if(factor!=1.0f)
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
662 {
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
663 // re-sample alpha
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
664 int f=factor*256.0f;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
665 int size=desc->pic_a[i]->w*desc->pic_a[i]->h;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
666 int j;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
667 if(verbose) printf("font: resampling alpha by factor %5.3f (%d) ",factor,f);fflush(stdout);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
668 for(j=0;j<size;j++){
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
669 int x=desc->pic_a[i]->bmp[j]; // alpha
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
670 int y=desc->pic_b[i]->bmp[j]; // bitmap
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
671
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
672 #ifdef FAST_OSD
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
673 x=(x<(255-f))?0:1;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
674 #else
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
675
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
676 x=255-((x*f)>>8); // scale
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
677 //if(x<0) x=0; else if(x>255) x=255;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
678 //x^=255; // invert
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
679
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
680 if(x+y>255) x=255-y; // to avoid overflows
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
681
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
682 //x=0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
683 //x=((x*f*(255-y))>>16);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
684 //x=((x*f*(255-y))>>16)+y;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
685 //x=(x*f)>>8;if(x<y) x=y;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
686
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
687 if(x<1) x=1; else
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
688 if(x>=252) x=0;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
689 #endif
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
690
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
691 desc->pic_a[i]->bmp[j]=x;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
692 // desc->pic_b[i]->bmp[j]=0; // hack
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
693 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
694 if(verbose) printf("DONE!\n");
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
695 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
696 if(!desc->height) desc->height=desc->pic_a[i]->h;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
697
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
698 j='_';if(desc->font[j]<0) j='?';
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
699 for(i=0;i<512;i++)
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
700 if(desc->font[i]<0){
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
701 desc->start[i]=desc->start[j];
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
702 desc->width[i]=desc->width[j];
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
703 desc->font[i]=desc->font[j];
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
704 }
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
705 desc->font[' ']=-1;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
706 desc->width[' ']=desc->spacewidth;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
707
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
708 printf("Font %s loaded successfully!\n",fname);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
709
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
710 ++font_id;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
711
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
712 // fflush(stderr);
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
713 return desc;
9fc7bcadcc1b .ttf font loader - based on TOOLS/subfont-c
arpi
parents:
diff changeset
714 }