Fonts: - fonts/hack: Hack 3.003 (Expat + Bitstream Vera, from Debian fonts-hack), installed to dt/fonts; config/fonts.conf (the system's fontconfig plus that directory) exported by Xsession.d/0070.dtfonts and Dtlogin*environment. - sys.resources and dtlogin Xresources: Xft render tables in Hack 10 for renderTable, textRenderTable, labelRenderTable, buttonRenderTable (a render table wins over sys.font's *FontList; dtterm and text fields read the text table). dtterm (ported from MakroDE -- setlocale and the Xft renderer -- then fixed): - configure: Xft on by default, --disable-xft for machines without it. - TermPrimRenderXft.c: reads wchar_t in multibyte locales (it read the buffer as UTF-8 bytes: three boxes after every character), places every character in its cell on the baseline, fills the background, draws underline/overstrike/secure, wcwidth for wide and combining characters, borrows glyphs from fontconfig fallbacks (check mark, CJK), and makes its XftDraw at the first drawing (made in Initialize it was window 0, and the first string a BadDrawable that ended dtterm). - TermPrim.c AdjustWindowUnits: the cell from the renderer's extents when there is no XFontStruct (crashed with Xft); _DtTermPrimRenderFontExtents. - TermPrimRenderFont.c: with setlocale the buffer is wchar_t in UTF-8, the single-font renderer draws it as XChar2b (it drew boxes too). Screenshots: build/mcde-shots/dtterm-utf8b-crop.png, dtterm-classic-crop.png. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
355 lines
11 KiB
C
355 lines
11 KiB
C
/*
|
|
* TermPrimRenderXft.c -- drawing text through Xft, behind the TermFont interface.
|
|
*
|
|
* MakroDE addition. SPDX-License-Identifier: BSD-3-Clause
|
|
* Copyright 2026 Konrad Lother
|
|
*
|
|
* WHAT THIS IS. The third TermFont implementation, next to the font set and the single font. dtterm
|
|
* asks for a TermFont and does not care which one it gets; this one renders through freetype, so the
|
|
* terminal can use the same fonts the rest of a modern desktop uses instead of an XLFD bitmap.
|
|
*
|
|
* ONE XftDraw, FOR THE LIFE OF THE TERMINAL. An XftDraw is bound to a Drawable and a visual, and
|
|
* rebuilding it per painted string is exactly the cost this fork cannot pay on the hardware it must
|
|
* keep running on. It is created here, in the create function, and kept in the record.
|
|
*
|
|
* WHAT ARRIVES (MCDE, 2026-11-09). In a multibyte locale (UTF-8) the buffer is wchar_t, one per
|
|
* character -- as the fontset renderer's XwcDrawString reads it; otherwise bytes. The first
|
|
* version read it as UTF-8 bytes and drew three empty boxes after every character
|
|
* (build/mcde-shots/dtterm-xft.png); each character is now placed in its own cell.
|
|
*
|
|
* AND THE CLASSIC PATH STAYS. This file does not replace the fontset renderer; it is one of the
|
|
* choices the caller makes, so a machine that cannot afford freetype per glyph still has a terminal.
|
|
*/
|
|
|
|
/* wcwidth() is XSI (POSIX.1-2008): ask for it before the first system header */
|
|
#ifndef _XOPEN_SOURCE
|
|
#define _XOPEN_SOURCE 700
|
|
#endif
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <wchar.h>
|
|
|
|
#include "cde_config.h" /* HAVE_XFT, written by configure */
|
|
|
|
#ifdef HAVE_XFT
|
|
#include <X11/Xft/Xft.h>
|
|
|
|
/* Same include order as the two renderers beside this one: TermHeader.h FIRST, because it is
|
|
* the project entry point that brings in the Motif and Intrinsic types the render headers
|
|
* expect. Without it, TermPrimRenderP.h pulls TermPrimRender.h in ahead of them and Widget,
|
|
* Pixel and the rest are unknown types. */
|
|
#include "TermHeader.h"
|
|
#include "TermPrimP.h"
|
|
#include "TermPrimDebug.h"
|
|
#include "TermPrimRenderP.h"
|
|
#include "TermPrimRenderXft.h"
|
|
|
|
/* MCDE: how many other fonts one terminal font may borrow glyphs from */
|
|
#define MCDE_XFT_FALLBACKS 8
|
|
|
|
typedef struct _TermFontXftRec {
|
|
XftFont *font;
|
|
XftDraw *draw; /* created ONCE, kept here, never per string */
|
|
XftFont *fallback[MCDE_XFT_FALLBACKS]; /* MCDE: fonts for what the font lacks */
|
|
int nfallback;
|
|
int ascent;
|
|
int height;
|
|
int width;
|
|
} TermFontXftRec, *TermFontXft;
|
|
|
|
/*
|
|
* MCDE: the cells a run covers, and the code point of one character. In a
|
|
* multibyte locale (UTF-8) the buffer holds wchar_t, one per character, and
|
|
* LEN counts characters; otherwise it holds bytes (the fontset renderer
|
|
* makes the same distinction, XmbDrawString or XwcDrawString). A wide
|
|
* character (wcwidth 2) takes two cells, a combining one (0) none.
|
|
*/
|
|
static FcChar32
|
|
XftCharAt(struct termData *tpd, unsigned char *string, int i, int *cells)
|
|
{
|
|
if (tpd->mbCurMax > 1) {
|
|
wchar_t wc = ((wchar_t *) string)[i];
|
|
int n = wcwidth(wc);
|
|
|
|
*cells = n < 0 ? 1 : n;
|
|
return (FcChar32) wc;
|
|
}
|
|
*cells = 1;
|
|
return (FcChar32) string[i]; /* single-byte locales: ISO 8859-1 */
|
|
}
|
|
|
|
static Bool
|
|
XftColorFromPixel(Widget w, Pixel pixel, Visual *visual, Colormap cmap,
|
|
XftColor *out)
|
|
{
|
|
XColor xc;
|
|
XRenderColor xr;
|
|
|
|
xc.pixel = pixel;
|
|
if (!XQueryColor(XtDisplay(w), cmap, &xc))
|
|
return False;
|
|
xr.red = xc.red;
|
|
xr.green = xc.green;
|
|
xr.blue = xc.blue;
|
|
xr.alpha = 0xffff;
|
|
return XftColorAllocValue(XtDisplay(w), visual, cmap, &xr, out);
|
|
}
|
|
|
|
/*
|
|
* MCDE: draws like XwcDrawImageString does for the fontset renderer -- the
|
|
* background of the cells first, then every character at its own cell
|
|
* (x + column * cellWidth) on the baseline (y + ascent). Placing each
|
|
* character keeps the grid even where a glyph comes from a fallback font of
|
|
* another width. Underline, overstrike (the bold of terminals) and secure
|
|
* (hidden) text as the other renderers draw them.
|
|
*/
|
|
/*
|
|
* MCDE: the font that draws C -- the terminal font, or a font fontconfig
|
|
* finds for that character at the same pixel size (a check mark, CJK ...).
|
|
* Up to MCDE_XFT_FALLBACKS of them are kept per terminal font; beyond that,
|
|
* or if nothing has the character, the terminal font draws its box.
|
|
*/
|
|
static XftFont *
|
|
XftFontFor(Display *dpy, TermFontXft xf, FcChar32 c)
|
|
{
|
|
FcPattern *pat, *match;
|
|
FcCharSet *cs;
|
|
FcResult res;
|
|
XftFont *f;
|
|
double px = 0;
|
|
int i;
|
|
|
|
if (XftCharExists(dpy, xf->font, c))
|
|
return xf->font;
|
|
for (i = 0; i < xf->nfallback; i++)
|
|
if (XftCharExists(dpy, xf->fallback[i], c))
|
|
return xf->fallback[i];
|
|
if (xf->nfallback == MCDE_XFT_FALLBACKS)
|
|
return xf->font;
|
|
|
|
(void) FcPatternGetDouble(xf->font->pattern, FC_PIXEL_SIZE, 0, &px);
|
|
if ((pat = FcPatternCreate()) == 0)
|
|
return xf->font;
|
|
if ((cs = FcCharSetCreate()) != 0) {
|
|
(void) FcCharSetAddChar(cs, c);
|
|
(void) FcPatternAddCharSet(pat, FC_CHARSET, cs);
|
|
FcCharSetDestroy(cs);
|
|
}
|
|
if (px > 0)
|
|
(void) FcPatternAddDouble(pat, FC_PIXEL_SIZE, px);
|
|
match = XftFontMatch(dpy, DefaultScreen(dpy), pat, &res);
|
|
FcPatternDestroy(pat);
|
|
if (match == 0)
|
|
return xf->font;
|
|
f = XftFontOpenPattern(dpy, match); /* owns MATCH from here */
|
|
if (f == 0) {
|
|
FcPatternDestroy(match);
|
|
return xf->font;
|
|
}
|
|
if (!XftCharExists(dpy, f, c)) {
|
|
XftFontClose(dpy, f);
|
|
return xf->font;
|
|
}
|
|
xf->fallback[xf->nfallback++] = f;
|
|
return f;
|
|
}
|
|
|
|
/* the XftDraw of the widget's window, made once the window exists */
|
|
static XftDraw *
|
|
XftEnsureDraw(Widget w, TermFontXft xf)
|
|
{
|
|
if (xf->draw == 0 && XtIsRealized(w))
|
|
xf->draw = XftDrawCreate(XtDisplay(w), XtWindow(w),
|
|
DefaultVisual(XtDisplay(w), DefaultScreen(XtDisplay(w))),
|
|
DefaultColormap(XtDisplay(w), DefaultScreen(XtDisplay(w))));
|
|
return xf->draw;
|
|
}
|
|
|
|
static void
|
|
XftRenderFunction(
|
|
Widget w,
|
|
TermFont font,
|
|
Pixel fg,
|
|
Pixel bg,
|
|
unsigned long flags,
|
|
int x,
|
|
int y,
|
|
unsigned char *string,
|
|
int len
|
|
)
|
|
{
|
|
TermFontXft xf = (TermFontXft) font->fontInfo;
|
|
DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
|
|
struct termData *tpd = tw->term.tpd;
|
|
Display *dpy = XtDisplay(w);
|
|
Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
|
|
Colormap cmap = DefaultColormap(dpy, DefaultScreen(dpy));
|
|
XftColor fgc, bgc;
|
|
int cw, i, col, cells, n, total = 0;
|
|
FcChar32 c;
|
|
|
|
if (xf == 0 || xf->font == 0 || string == 0 || len <= 0 ||
|
|
XftEnsureDraw(w, xf) == 0)
|
|
return;
|
|
cw = tpd->cellWidth > 0 ? tpd->cellWidth : xf->width;
|
|
|
|
if (!XftColorFromPixel(w, fg, visual, cmap, &fgc))
|
|
return;
|
|
if (!XftColorFromPixel(w, bg, visual, cmap, &bgc)) {
|
|
XftColorFree(dpy, visual, cmap, &fgc);
|
|
return;
|
|
}
|
|
|
|
for (i = 0; i < len; i++) {
|
|
(void) XftCharAt(tpd, string, i, &cells);
|
|
total += cells;
|
|
}
|
|
XftDrawRect(xf->draw, &bgc, x, y, (unsigned) (total * cw),
|
|
(unsigned) (tpd->cellHeight > 0 ? tpd->cellHeight : xf->height));
|
|
|
|
/* hidden text (secure): the cells only */
|
|
if (TermIS_SECURE(flags))
|
|
len = 0;
|
|
|
|
for (i = 0, col = 0; i < len; i++) {
|
|
c = XftCharAt(tpd, string, i, &cells);
|
|
/* a combining character sits on the cell before it */
|
|
n = cells == 0 && col > 0 ? col - 1 : col;
|
|
if (c != ' ' && c != 0) {
|
|
XftFont *f = XftFontFor(dpy, xf, c);
|
|
|
|
XftDrawString32(xf->draw, &fgc, f, x + n * cw,
|
|
y + xf->ascent, &c, 1);
|
|
if (TermIS_OVERSTRIKE(flags))
|
|
XftDrawString32(xf->draw, &fgc, f, x + n * cw + 1,
|
|
y + xf->ascent, &c, 1);
|
|
}
|
|
col += cells;
|
|
}
|
|
|
|
if (TermIS_UNDERLINE(flags))
|
|
XftDrawRect(xf->draw, &fgc, x, y + tpd->cellHeight - 1,
|
|
(unsigned) (total * cw), 1);
|
|
|
|
XftColorFree(dpy, visual, cmap, &bgc);
|
|
XftColorFree(dpy, visual, cmap, &fgc);
|
|
}
|
|
|
|
static void
|
|
XftDestroyFunction(
|
|
Widget w,
|
|
TermFont font
|
|
)
|
|
{
|
|
TermFontXft xf;
|
|
|
|
(void) w;
|
|
if (font == 0)
|
|
return;
|
|
xf = (TermFontXft) font->fontInfo;
|
|
if (xf != 0)
|
|
{
|
|
if (xf->draw != 0)
|
|
XftDrawDestroy(xf->draw);
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < xf->nfallback; i++)
|
|
XftFontClose(XtDisplay(w), xf->fallback[i]);
|
|
}
|
|
(void) XtFree((char *) xf);
|
|
}
|
|
(void) XtFree((char *) font);
|
|
}
|
|
|
|
static void
|
|
XftExtentsFunction(
|
|
Widget w,
|
|
TermFont font,
|
|
unsigned char *string,
|
|
int len,
|
|
int *widthReturn,
|
|
int *heightReturn,
|
|
int *ascentReturn
|
|
)
|
|
{
|
|
TermFontXft xf = (TermFontXft) font->fontInfo;
|
|
|
|
if (xf == 0)
|
|
return;
|
|
|
|
if (widthReturn != 0)
|
|
{
|
|
/* MCDE: the cells the run covers, as XftRenderFunction draws it */
|
|
DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
|
|
struct termData *tpd = tw->term.tpd;
|
|
int cw = tpd->cellWidth > 0 ? tpd->cellWidth : xf->width;
|
|
int i, cells, total = 0;
|
|
|
|
for (i = 0; string != 0 && i < len; i++) {
|
|
(void) XftCharAt(tpd, string, i, &cells);
|
|
total += cells;
|
|
}
|
|
*widthReturn = total * cw;
|
|
}
|
|
if (heightReturn != 0)
|
|
*heightReturn = xf->height;
|
|
if (ascentReturn != 0)
|
|
*ascentReturn = xf->ascent;
|
|
}
|
|
|
|
TermFont
|
|
_DtTermPrimRenderXftCreate(
|
|
Widget w,
|
|
XftFont *xftFont
|
|
)
|
|
{
|
|
TermFont font;
|
|
TermFontXft xf;
|
|
|
|
if (w == 0 || xftFont == 0)
|
|
return 0;
|
|
|
|
xf = (TermFontXft) XtCalloc(1, sizeof(TermFontXftRec));
|
|
if (xf == 0)
|
|
return 0;
|
|
|
|
xf->font = xftFont;
|
|
xf->ascent = xftFont->ascent;
|
|
xf->height = xftFont->ascent + xftFont->descent;
|
|
xf->width = xftFont->max_advance_width;
|
|
|
|
/* THE ONE DRAWABLE, made at the first drawing (XftEnsureDraw): the font is
|
|
created in the widget's Initialize, before the widget has a window, and an
|
|
XftDraw on window 0 made the first string a BadDrawable that ended dtterm
|
|
(MCDE, 2026-11-09). */
|
|
xf->draw = 0;
|
|
|
|
font = (TermFont) XtCalloc(1, sizeof(struct _TermFontRec));
|
|
if (font == 0)
|
|
{
|
|
(void) XtFree((char *) xf);
|
|
return 0;
|
|
}
|
|
|
|
font->fontInfo = (XtPointer) xf;
|
|
font->renderFunction = XftRenderFunction;
|
|
font->destroyFunction = XftDestroyFunction;
|
|
font->extentsFunction = XftExtentsFunction;
|
|
|
|
return font;
|
|
}
|
|
|
|
/*
|
|
* NO #else BRANCH, AND THAT IS THE POINT. The first version had one, and it NAMED TermFont, Widget
|
|
* and XftFont while defining a function that would never exist -- which is exactly the dependency
|
|
* this file exists to avoid. Without Xft those three types do not exist, so the "there is no
|
|
* renderer" stub could not compile, and a source in _SOURCES is always compiled. A function that
|
|
* does not exist must not be written down.
|
|
*
|
|
* An empty translation unit is legal C, the caller already guards its call with the same macro
|
|
* (TermPrim.c), and nothing links against a stub that pretends to create something.
|
|
*/
|
|
#endif /* HAVE_XFT */
|