+ f->rbearing = f->match->max_advance_width;
+
+ f->height = f->match->height;
+ f->width = f->lbearing + f->rbearing;
+
+ return 0;
+}
+
+void
+xloadfonts(char *fontstr, int fontsize) {
+ FcPattern *pattern;
+ FcResult result;
+ double fontval;
+
+ if(fontstr[0] == '-') {
+ pattern = XftXlfdParse(fontstr, False, False);
+ } else {
+ pattern = FcNameParse((FcChar8 *)fontstr);
+ }
+
+ if(!pattern)
+ die("st: can't open font %s\n", fontstr);
+
+ if(fontsize > 0) {
+ FcPatternDel(pattern, FC_PIXEL_SIZE);
+ FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)fontsize);
+ usedfontsize = fontsize;
+ } else {
+ result = FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval);
+ if(result == FcResultMatch) {
+ usedfontsize = (int)fontval;
+ } else {
+ /*
+ * Default font size is 12, if none given. This is to
+ * have a known usedfontsize value.
+ */
+ FcPatternAddDouble(pattern, FC_PIXEL_SIZE, 12);
+ usedfontsize = 12;
+ }
+ }
+
+ FcConfigSubstitute(0, pattern, FcMatchPattern);
+ FcDefaultSubstitute(pattern);
+
+ if(xloadfont(&dc.font, pattern))
+ die("st: can't open font %s\n", fontstr);
+
+ /* Setting character width and height. */
+ xw.cw = dc.font.width;
+ xw.ch = dc.font.height;
+
+ FcPatternDel(pattern, FC_SLANT);
+ FcPatternDel(pattern, FC_WEIGHT);
+ FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
+ FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
+ if(xloadfont(&dc.bfont, pattern))
+ die("st: can't open font %s\n", fontstr);
+
+ FcPatternDel(pattern, FC_SLANT);
+ FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
+ if(xloadfont(&dc.ibfont, pattern))
+ die("st: can't open font %s\n", fontstr);
+
+ FcPatternDel(pattern, FC_WEIGHT);
+ FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_MEDIUM);
+ if(xloadfont(&dc.ifont, pattern))
+ die("st: can't open font %s\n", fontstr);
+
+ FcPatternDestroy(pattern);
+}
+
+void
+xunloadfonts(void) {
+ int i, ip;
+
+ /*
+ * Free the loaded fonts in the font cache. This is done backwards
+ * from the frccur.
+ */
+ for (i = 0, ip = frccur; i < frclen; i++, ip--) {
+ if (ip < 0)
+ ip = LEN(frc) - 1;
+ XftFontClose(xw.dpy, frc[ip].font);
+ }
+ frccur = -1;
+ frclen = 0;
+
+ XftFontClose(xw.dpy, dc.font.match);
+ FcPatternDestroy(dc.font.pattern);
+ FcFontSetDestroy(dc.font.set);
+ XftFontClose(xw.dpy, dc.bfont.match);
+ FcPatternDestroy(dc.bfont.pattern);
+ FcFontSetDestroy(dc.bfont.set);
+ XftFontClose(xw.dpy, dc.ifont.match);
+ FcPatternDestroy(dc.ifont.pattern);
+ FcFontSetDestroy(dc.ifont.set);
+ XftFontClose(xw.dpy, dc.ibfont.match);
+ FcPatternDestroy(dc.ibfont.pattern);
+ FcFontSetDestroy(dc.ibfont.set);