3 #include <fontconfig/fontconfig.h>
13 static FT_Library lib;
23 if(!FcInit() || (fc=FcInitLoadConfigAndFonts()) == NULL) {
24 fprint(2, "fontconfig initialization failed\n");
25 exits("fontconfig failed");
28 e = FT_Init_FreeType(&lib);
30 fprint(2, "freetype initialization failed: %d\n", e);
31 exits("freetype failed");
34 sysfonts = FcConfigGetFonts(fc, FcSetSystem);
36 xfont = emalloc9p(sysfonts->nfont*sizeof xfont[0]);
37 memset(xfont, 0, sysfonts->nfont*sizeof xfont[0]);
38 for(i=0; i<sysfonts->nfont; i++) {
39 FcChar8 *fullname, *fontfile;
41 FcPattern *pat = sysfonts->fonts[i];
43 if(FcPatternGetString(pat, FC_FULLNAME, 0, &fullname) != FcResultMatch ||
44 FcPatternGetString(pat, FC_FILE, 0, &fontfile) != FcResultMatch ||
45 FcPatternGetInteger(pat, FC_INDEX, 0, &index) != FcResultMatch)
48 xfont[nxfont].name = strdup((char*)fullname);
49 xfont[nxfont].fontfile = strdup((char*)fontfile);
50 xfont[nxfont].index = index;
54 FcFontSetDestroy(sysfonts);
68 e = FT_New_Face(lib, f->fontfile, f->index, &face);
71 fprint(2, "load failed for %s (%s) index:%d\n", f->name, f->fontfile, f->index);
75 if(!FT_IS_SCALABLE(face)) {
76 fprint(2, "%s is a non scalable font, skipping\n", f->name);
82 f->unit = face->units_per_EM;
83 f->height = (int)((face->ascender - face->descender) * 1.2);
84 f->originy = face->descender; // bbox.yMin (or descender) is negative, becase the baseline is y-coord 0
86 for(charcode=FT_Get_First_Char(face, &glyph_index); glyph_index != 0;
87 charcode=FT_Get_Next_Char(face, charcode, &glyph_index)) {
89 int idx = charcode>>8;
105 mksubfont(char *name, int lo, int hi, int size, int antialias)
107 XFont *xf, *xfp, *xfe;
110 Memimage *m, *mc, *m1;
116 Point rect_points[4];
119 for(xfp=xfont, xfe=xfont+nxfont; xfp != xfe; xfp++) {
120 if(strcmp(xfp->name, name) == 0) {
129 e = FT_New_Face(lib, xf->fontfile, xf->index, &face);
132 fprint(2, "load failed for %s (%s) index:%d\n", xf->name, xf->fontfile, xf->index);
136 e = FT_Set_Char_Size(face, 0, size<<6, dpi, dpi);
138 fprint(2, "FT_Set_Char_Size failed\n");
143 pixel_size = (dpi*size)/72.0;
144 x = (int)((face->max_advance_width) * pixel_size/xf->unit + 0.99999999);
145 y = (int)((face->ascender - face->descender) * pixel_size/xf->unit + 0.99999999);
146 y0 = (int)(-face->descender * pixel_size/xf->unit + 0.99999999);
148 m = allocmemimage(Rect(0, 0, x*(hi+1-lo), y), antialias ? GREY8 : GREY1);
153 mc = allocmemimage(Rect(0, 0, x, y), antialias ? GREY8 : GREY1);
159 memfillcolor(m, DBlack);
160 memfillcolor(mc, DBlack);
161 fc = malloc((hi+2 - lo) * sizeof fc[0]);
162 sf = malloc(sizeof *sf);
163 if(fc == nil || sf == nil) {
173 //rect_points[0] = mc->r.min;
174 //rect_points[1] = Pt(mc->r.max.x, mc->r.min.y);
175 //rect_points[2] = mc->r.max;
176 //rect_points[3] = Pt(mc->r.min.x, mc->r.max.y);
179 for(i=lo; i<=hi; i++, fc++) {
183 memfillcolor(mc, DBlack);
185 e = FT_Load_Char(face, i, FT_LOAD_RENDER|(antialias ? 0:FT_LOAD_TARGET_MONO));
187 fprint(2, "FT_Load_Char failed for %d\n", i);
188 //mempoly(mc, rect_points, 4, Endsquare, Endsquare, 0, memopaque, ZP, S);
189 memimageline(mc, m->r.min, Pt(m->r.max.x, m->r.min.y), Endsquare, Endsquare, 0, memopaque, ZP, S);
190 memimageline(mc, m->r.min, Pt(m->r.min.x, m->r.max.y), Endsquare, Endsquare, 0, memopaque, ZP, S);
191 memimageline(mc, Pt(m->r.max.x, m->r.min.y), m->r.max, Endsquare, Endsquare, 0, memopaque, ZP, S);
192 memimageline(mc, Pt(m->r.min.x, m->r.max.y), m->r.max, Endsquare, Endsquare, 0, memopaque, ZP, S);
193 memimageline(mc, m->r.min, m->r.max, Endsquare, Endsquare, 0, memopaque, ZP, S);
196 memimagedraw(m, Rect(x, 0, x + advance, y), mc, ZP, memopaque, ZP, S);
198 FT_Bitmap *bitmap = &face->glyph->bitmap;
199 uchar *base = byteaddr(mc, mc->r.min);
200 advance = (face->glyph->advance.x+32) >> 6;
202 for(r=0; r < bitmap->rows; r++)
203 memmove(base + r*mc->width*sizeof(u32int), bitmap->buffer + r*bitmap->pitch, bitmap->pitch);
205 memimagedraw(m, Rect(x, 0, x + advance, y), mc,
206 Pt(-face->glyph->bitmap_left, -(y - y0 - face->glyph->bitmap_top)),
217 #ifdef DEBUG_FT_BITMAP
218 for(r=0; r < bitmap->rows; r++) {
220 uchar *span = bitmap->buffer+(r*bitmap->pitch);
221 for(c = 0; c < bitmap->width; c++) {
222 fprint(1, "%02x", span[c]);
228 #ifdef DEBUG_9_BITMAP
229 for(r=0; r < mc->r.max.y; r++) {
231 uchar *span = base+(r*mc->width*sizeof(u32int));
232 for(c = 0; c < Dx(mc->r); c++) {
233 fprint(1, "%02x", span[c]);
241 // round up to 32-bit boundary
242 // so that in-memory data is same
243 // layout as in-file data.
248 m1 = allocmemimage(Rect(0, 0, x, y), antialias ? GREY8 : GREY1);
249 memimagedraw(m1, m1->r, m, m->r.min, memopaque, ZP, S);
254 sf->height = Dy(m1->r);
255 sf->ascent = Dy(m1->r) - y0;