Files
mcde/lib/DtTerm/TermPrim/TermPrimRenderFont.c
lotherkandClaude Opus 5.5 ac0952a29b mcde: anti-aliased Hack everywhere; dtterm draws through Xft with UTF-8 and glyph fallback
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>
2026-11-09 23:55:41 +00:00

219 lines
5.9 KiB
C

/*
* CDE - Common Desktop Environment
*
* Copyright (c) 1993-2012, The Open Group. All rights reserved.
*
* These libraries and programs are free software; you can
* redistribute them and/or modify them under the terms of the GNU
* Lesser General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* These libraries and programs are distributed in the hope that
* they will be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with these libraries and programs; if not, write
* to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301 USA
*/
/* *
* (c) Copyright 1993, 1994 Hewlett-Packard Company *
* (c) Copyright 1993, 1994 International Business Machines Corp. *
* (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
* (c) Copyright 1993, 1994 Novell, Inc. *
*/
#include <wchar.h>
#include "TermHeader.h"
#include "TermPrimP.h"
#include "TermPrimDebug.h"
#include "TermPrimRenderP.h"
#include "TermPrimRenderFont.h"
static void
FontRenderFunction(
Widget w,
TermFont font,
Pixel fg,
Pixel bg,
unsigned long flags,
int x,
int y,
unsigned char *string,
int len
)
{
DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
struct termData *tpd = tw->term.tpd;
XGCValues values;
unsigned long valueMask;
XFontStruct *fontStruct = (XFontStruct *) font->fontInfo;
/* set the renderGC... */
valueMask = (unsigned long) 0;
/* set the foreground... */
if (TermIS_SECURE(flags)) {
if (tpd->renderGC.foreground != bg) {
tpd->renderGC.foreground = bg;
values.foreground = bg;
valueMask |= GCForeground;
}
} else {
if (tpd->renderGC.foreground != fg) {
tpd->renderGC.foreground = fg;
values.foreground = fg;
valueMask |= GCForeground;
}
}
/* set background... */
if (tpd->renderGC.background != bg) {
tpd->renderGC.background = bg;
values.background = bg;
valueMask |= GCBackground;
}
/* set the font for renderGC if necessary */
if (tpd->renderGC.fid != fontStruct->fid) {
tpd->renderGC.fid = fontStruct->fid;
values.font = fontStruct->fid;
valueMask |= GCFont;
}
if (valueMask) {
(void) XChangeGC(XtDisplay(w), tpd->renderGC.gc, valueMask,
&values);
}
/* draw image string a line of text... */
if (isDebugFSet('t', 1)) {
#ifdef BBA
#pragma BBA_IGNORE
#endif /*BBA*/
/* Fill in the text area so we can see what is going to
* be displayed...
*/
(void) XFillRectangle(XtDisplay(w),
XtWindow(w),
tpd->renderGC.gc,
x,
y,
tpd->cellWidth * len,
tpd->cellHeight);
(void) XSync(XtDisplay(w), False);
(void) shortSleep(100000);
}
if (tpd->mbCurMax > 1) {
/* MCDE: in a multibyte locale (UTF-8, since dtterm calls setlocale) the
* buffer holds wchar_t; one font draws them as 16-bit characters --
* the code point for an ISO 10646 font, and byte1 0 for an 8-bit one.
* Characters beyond the 16-bit plane are drawn as '?'.
*/
XChar2b *s2 = (XChar2b *) XtMalloc((Cardinal) (len * sizeof(XChar2b)));
wchar_t *wc = (wchar_t *) string;
int i;
for (i = 0; i < len; i++) {
unsigned long c = (unsigned long) wc[i] > 0xffffUL ?
(unsigned long) '?' : (unsigned long) wc[i];
s2[i].byte1 = (unsigned char) (c >> 8);
s2[i].byte2 = (unsigned char) (c & 0xff);
}
(void) XDrawImageString16(XtDisplay(w), XtWindow(w), tpd->renderGC.gc,
x, y + fontStruct->ascent, s2, len);
if (TermIS_OVERSTRIKE(flags))
(void) XDrawString16(XtDisplay(w), XtWindow(w), tpd->renderGC.gc,
x + 1, y + fontStruct->ascent, s2, len);
XtFree((char *) s2);
} else {
(void) XDrawImageString(XtDisplay(w), /* Display */
XtWindow(w), /* Drawable */
tpd->renderGC.gc, /* GC */
x, /* x */
y + fontStruct->ascent, /* y */
(char *) string, /* string */
len); /* length */
/* handle overstrike... */
if (TermIS_OVERSTRIKE(flags)) {
(void) XDrawString(XtDisplay(w), /* Display */
XtWindow(w), /* Drawable */
tpd->renderGC.gc, /* GC */
x + 1, /* x */
y + fontStruct->ascent, /* y */
(char *) string, /* string */
len); /* length */
}
}
/* handle the underline enhancement... */
/* draw the underline... */
if (TermIS_UNDERLINE(flags)) {
XDrawLine(XtDisplay(w), /* Display */
XtWindow(w), /* Window */
tpd->renderGC.gc, /* GC */
x, /* X1 */
y + tpd->cellHeight - 1, /* Y1 */
x + len * tpd->cellWidth, /* X2 */
y + tpd->cellHeight - 1); /* Y2 */
}
}
static void
FontDestroyFunction(
Widget w,
TermFont font
)
{
(void) XtFree((char *) font);
}
static void
FontExtentsFunction(
Widget w,
TermFont font,
unsigned char *string,
int len,
int *widthReturn,
int *heightReturn,
int *ascentReturn
)
{
XFontStruct *fontStruct = (XFontStruct *) font->fontInfo;
if (widthReturn) {
*widthReturn = len * fontStruct->max_bounds.width;
}
if (heightReturn) {
*heightReturn = fontStruct->ascent + fontStruct->descent;
}
if (ascentReturn) {
*ascentReturn = fontStruct->ascent;
}
return;
}
TermFont
_DtTermPrimRenderFontCreate(
Widget w,
XFontStruct *fontStruct
)
{
TermFont termFont;
termFont = (TermFont) XtMalloc(sizeof(TermFontRec));
termFont->renderFunction = FontRenderFunction;
termFont->destroyFunction = FontDestroyFunction;
termFont->extentsFunction = FontExtentsFunction;
termFont->fontInfo = (XtPointer) fontStruct;
return(termFont);
}