Move XFontSet based text rendering module to separate file

Like the other font handlers.
This commit is contained in:
Kim Woelders 2022-02-17 15:38:48 +01:00
parent 01eb2aef59
commit dcfb5efba1
3 changed files with 177 additions and 144 deletions

View File

@ -92,6 +92,7 @@ e16_SOURCES = \
x.c xwin.h \
xtypes.h
e16_SOURCES += text_xfs.c
if ENABLE_SOUND
e16_SOURCES += sound.c sound.h
endif

View File

@ -23,35 +23,11 @@
*/
#include "config.h"
#include <X11/Xlib.h>
#include "E.h"
#include "eimage.h"
#include "tclass.h"
#include "xwin.h"
static GC
_get_gc(Win win)
{
static GC gc = NULL;
static Visual *last_vis = NULL;
Visual *vis;
vis = WinGetVisual(win);
if (vis != last_vis)
{
if (gc)
EXFreeGC(gc);
gc = NULL;
last_vis = vis;
}
if (!gc)
gc = EXCreateGC(WinGetXwin(win), 0, NULL);
return gc;
}
static void
TextDrawRotTo(Win win, EX_Drawable src, EX_Drawable dst, int x, int y,
int w, int h, TextState * ts)
@ -362,124 +338,6 @@ TextstateTextFitMB(TextState * ts, char **ptext, int *pw, int textwidth_limit)
EwcClose();
}
#if FONT_TYPE_XFS
/*
* XFontSet - XCreateFontSet
*/
extern const FontOps FontOpsXfs;
typedef struct {
XFontSet font;
int ascent;
Win win;
EX_Drawable draw;
GC gc;
} FontCtxXfs;
static int
_xfs_Load(TextState * ts, const char *name)
{
XFontSet font;
FontCtxXfs *fdc;
int i, missing_cnt, font_cnt;
char **missing_list, *def_str, **fnlr;
XFontStruct **fs;
font = XCreateFontSet(disp, name, &missing_list, &missing_cnt, &def_str);
if (missing_cnt)
XFreeStringList(missing_list);
if (!font)
return -1;
if (EDebug(EDBUG_TYPE_FONTS) >= 2)
{
Eprintf("- XBaseFontNameListOfFontSet %s\n",
XBaseFontNameListOfFontSet(font));
font_cnt = XFontsOfFontSet(font, &fs, &fnlr);
for (i = 0; i < font_cnt; i++)
Eprintf("- XFontsOfFontSet %d: %s\n", i, fnlr[i]);
}
fdc = EMALLOC(FontCtxXfs, 1);
if (!fdc)
return -1;
fdc->font = font;
ts->fdc = fdc;
fdc->ascent = 0;
font_cnt = XFontsOfFontSet(font, &fs, &fnlr);
for (i = 0; i < font_cnt; i++)
fdc->ascent = MAX(fs[i]->ascent, fdc->ascent);
ts->type = FONT_TYPE_XFS;
ts->ops = &FontOpsXfs;
return 0;
}
static void
_xfs_Unload(TextState * ts)
{
FontCtxXfs *fdc = (FontCtxXfs *) ts->fdc;
XFreeFontSet(disp, fdc->font);
}
static void
_xfs_TextSize(TextState * ts, const char *text, int len,
int *width, int *height, int *ascent)
{
FontCtxXfs *fdc = (FontCtxXfs *) ts->fdc;
XRectangle ret2;
if (len == 0)
len = strlen(text);
XmbTextExtents(fdc->font, text, len, NULL, &ret2);
*height = ret2.height;
*width = ret2.width;
*ascent = fdc->ascent;
}
static void
_xfs_TextDraw(TextState * ts, int x, int y, const char *text, int len)
{
FontCtxXfs *fdc = (FontCtxXfs *) ts->fdc;
XmbDrawString(disp, fdc->draw, fdc->font, fdc->gc, x, y, text, len);
}
static int
_xfs_FdcInit(TextState * ts, Win win, EX_Drawable draw)
{
FontCtxXfs *fdc = (FontCtxXfs *) ts->fdc;
fdc->win = win;
fdc->draw = draw;
fdc->gc = _get_gc(win);
return 0;
}
static void
_xfs_FdcSetDrawable(TextState * ts, unsigned long draw)
{
FontCtxXfs *fdc = (FontCtxXfs *) ts->fdc;
fdc->draw = draw;
}
static void
_xfs_FdcSetColor(TextState * ts, unsigned int color)
{
FontCtxXfs *fdc = (FontCtxXfs *) ts->fdc;
unsigned int pixel;
pixel = EAllocColor(WinGetCmap(fdc->win), color);
XSetForeground(disp, fdc->gc, pixel);
}
const FontOps FontOpsXfs = {
_xfs_Load, _xfs_Unload, _xfs_TextSize, TextstateTextFit, _xfs_TextDraw,
_xfs_FdcInit, NULL, _xfs_FdcSetDrawable, _xfs_FdcSetColor
};
#endif /* FONT_TYPE_XFS */
static void
TsTextDraw(TextState * ts, int x, int y, const char *text, int len)
{
@ -508,6 +366,10 @@ typedef struct {
#endif
} FontHandler;
#if FONT_TYPE_XFS
extern const FontOps FontOps_xfs;
#endif
#if USE_MODULES
#define FONT(type, ops, opsm) { type, opsm, 0 }
@ -530,7 +392,7 @@ extern const FontOps FontOps_pango;
static FontHandler fhs[] = {
#if FONT_TYPE_XFS
FONT("xfs", &FontOpsXfs, &FontOpsXfs), /* XFontSet - XCreateFontSet */
FONT("xfs", &FontOps_xfs, &FontOps_xfs), /* XFontSet - XCreateFontSet */
#endif
#if FONT_TYPE_IFT
FONT("ift", &FontOps_ift, NULL), /* Imlib2/FreeType */
@ -607,7 +469,7 @@ TextStateLoadFont(TextState * ts)
}
fallback:
#if FONT_TYPE_XFS
if (!FontOpsXfs.Load(ts, "fixed")) /* XFontSet - XCreateFontSet */
if (!FontOps_xfs.Load(ts, "fixed")) /* XFontSet - XCreateFontSet */
goto done;
#endif

170
src/text_xfs.c Normal file
View File

@ -0,0 +1,170 @@
/*
* Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
* Copyright (C) 2004-2022 Kim Woelders
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of the Software, its documentation and marketing & publicity
* materials, and acknowledgment shall be given in the documentation, materials
* and software packages that this Software was used.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "config.h"
#include "tclass.h"
#if FONT_TYPE_XFS
#include <X11/Xlib.h>
#include "E.h"
static GC
_get_gc(Win win)
{
static GC gc = NULL;
static Visual *last_vis = NULL;
Visual *vis;
vis = WinGetVisual(win);
if (vis != last_vis)
{
if (gc)
EXFreeGC(gc);
gc = NULL;
last_vis = vis;
}
if (!gc)
gc = EXCreateGC(WinGetXwin(win), 0, NULL);
return gc;
}
/*
* XFontSet - XCreateFontSet
*/
__EXPORT__ extern const FontOps FontOps_xfs;
typedef struct {
XFontSet font;
int ascent;
Win win;
EX_Drawable draw;
GC gc;
} FontCtxXfs;
static int
_xfs_Load(TextState * ts, const char *name)
{
XFontSet font;
FontCtxXfs *fdc;
int i, missing_cnt, font_cnt;
char **missing_list, *def_str, **fnlr;
XFontStruct **fs;
font = XCreateFontSet(disp, name, &missing_list, &missing_cnt, &def_str);
if (missing_cnt)
XFreeStringList(missing_list);
if (!font)
return -1;
if (EDebug(EDBUG_TYPE_FONTS) >= 2)
{
Eprintf("- XBaseFontNameListOfFontSet %s\n",
XBaseFontNameListOfFontSet(font));
font_cnt = XFontsOfFontSet(font, &fs, &fnlr);
for (i = 0; i < font_cnt; i++)
Eprintf("- XFontsOfFontSet %d: %s\n", i, fnlr[i]);
}
fdc = EMALLOC(FontCtxXfs, 1);
if (!fdc)
return -1;
fdc->font = font;
ts->fdc = fdc;
fdc->ascent = 0;
font_cnt = XFontsOfFontSet(font, &fs, &fnlr);
for (i = 0; i < font_cnt; i++)
fdc->ascent = MAX(fs[i]->ascent, fdc->ascent);
ts->type = FONT_TYPE_XFS;
ts->ops = &FontOps_xfs;
return 0;
}
static void
_xfs_Unload(TextState * ts)
{
FontCtxXfs *fdc = (FontCtxXfs *) ts->fdc;
XFreeFontSet(disp, fdc->font);
}
static void
_xfs_TextSize(TextState * ts, const char *text, int len,
int *width, int *height, int *ascent)
{
FontCtxXfs *fdc = (FontCtxXfs *) ts->fdc;
XRectangle ret2;
if (len == 0)
len = strlen(text);
XmbTextExtents(fdc->font, text, len, NULL, &ret2);
*height = ret2.height;
*width = ret2.width;
*ascent = fdc->ascent;
}
static void
_xfs_TextDraw(TextState * ts, int x, int y, const char *text, int len)
{
FontCtxXfs *fdc = (FontCtxXfs *) ts->fdc;
XmbDrawString(disp, fdc->draw, fdc->font, fdc->gc, x, y, text, len);
}
static int
_xfs_FdcInit(TextState * ts, Win win, EX_Drawable draw)
{
FontCtxXfs *fdc = (FontCtxXfs *) ts->fdc;
fdc->win = win;
fdc->draw = draw;
fdc->gc = _get_gc(win);
return 0;
}
static void
_xfs_FdcSetDrawable(TextState * ts, unsigned long draw)
{
FontCtxXfs *fdc = (FontCtxXfs *) ts->fdc;
fdc->draw = draw;
}
static void
_xfs_FdcSetColor(TextState * ts, unsigned int color)
{
FontCtxXfs *fdc = (FontCtxXfs *) ts->fdc;
unsigned int pixel;
pixel = EAllocColor(WinGetCmap(fdc->win), color);
XSetForeground(disp, fdc->gc, pixel);
}
const FontOps FontOps_xfs = {
_xfs_Load, _xfs_Unload, _xfs_TextSize, TextstateTextFit, _xfs_TextDraw,
_xfs_FdcInit, NULL, _xfs_FdcSetDrawable, _xfs_FdcSetColor
};
#endif /* FONT_TYPE_XFS */