1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-04-19 03:58:05 -05:00

Switch the unifont system over to using FROMFIELD.

Now that I'm doing that in so many of the new classes as a more
type-safe alternative to ordinary C casts, I should make sure all the
old code is also reaping the benefits. This commit converts the system
of unifont vtables in the GTK front end, and also the 'unifontsel'
structure that exposes only a few of its fields outside gtkfont.c.
This commit is contained in:
Simon Tatham 2018-10-05 07:02:19 +01:00
parent 96ec2c2500
commit e0130a48ca

View File

@ -52,10 +52,8 @@
* polymorphic. * polymorphic.
* *
* Any instance of `unifont' used in the vtable functions will * Any instance of `unifont' used in the vtable functions will
* actually be the first element of a larger structure containing * actually be an element of a larger structure containing data
* data specific to the subtype. This is permitted by the ISO C * specific to the subtype.
* provision that one may safely cast between a pointer to a
* structure and a pointer to its first element.
*/ */
#define FONTFLAG_CLIENTSIDE 0x0001 #define FONTFLAG_CLIENTSIDE 0x0001
@ -177,7 +175,6 @@ typedef struct x11font_individual {
} x11font_individual; } x11font_individual;
struct x11font { struct x11font {
struct unifont u;
/* /*
* Copy of the X display handle, so we don't have to keep * Copy of the X display handle, so we don't have to keep
* extracting it from GDK. * extracting it from GDK.
@ -212,6 +209,8 @@ struct x11font {
* Data passed in to unifont_create(). * Data passed in to unifont_create().
*/ */
int wide, bold, shadowoffset, shadowalways; int wide, bold, shadowoffset, shadowalways;
unifont u;
}; };
static const struct unifont_vtable x11font_vtable = { static const struct unifont_vtable x11font_vtable = {
@ -537,12 +536,12 @@ static unifont *x11font_create(GtkWidget *widget, const char *name,
xfont->fonts[0].xfs = xfs; xfont->fonts[0].xfs = xfs;
xfont->fonts[0].allocated = TRUE; xfont->fonts[0].allocated = TRUE;
return (unifont *)xfont; return &xfont->u;
} }
static void x11font_destroy(unifont *font) static void x11font_destroy(unifont *font)
{ {
struct x11font *xfont = (struct x11font *)font; struct x11font *xfont = FROMFIELD(font, struct x11font, u);
Display *disp = xfont->disp; Display *disp = xfont->disp;
int i; int i;
@ -564,7 +563,7 @@ static void x11font_destroy(unifont *font)
} }
#endif #endif
} }
sfree(font); sfree(xfont);
} }
static void x11_alloc_subfont(struct x11font *xfont, int sfid) static void x11_alloc_subfont(struct x11font *xfont, int sfid)
@ -580,7 +579,7 @@ static void x11_alloc_subfont(struct x11font *xfont, int sfid)
static int x11font_has_glyph(unifont *font, wchar_t glyph) static int x11font_has_glyph(unifont *font, wchar_t glyph)
{ {
struct x11font *xfont = (struct x11font *)font; struct x11font *xfont = FROMFIELD(font, struct x11font, u);
if (xfont->sixteen_bit) { if (xfont->sixteen_bit) {
/* /*
@ -894,7 +893,7 @@ static void x11font_draw_text(unifont_drawctx *ctx, unifont *font,
int x, int y, const wchar_t *string, int len, int x, int y, const wchar_t *string, int len,
int wide, int bold, int cellwidth) int wide, int bold, int cellwidth)
{ {
struct x11font *xfont = (struct x11font *)font; struct x11font *xfont = FROMFIELD(font, struct x11font, u);
int sfid; int sfid;
int shadowoffset = 0; int shadowoffset = 0;
int mult = (wide ? 2 : 1); int mult = (wide ? 2 : 1);
@ -1201,7 +1200,7 @@ static char *x11font_scale_fontname(GtkWidget *widget, const char *name,
static char *x11font_size_increment(unifont *font, int increment) static char *x11font_size_increment(unifont *font, int increment)
{ {
struct x11font *xfont = (struct x11font *)font; struct x11font *xfont = FROMFIELD(font, struct x11font, u);
Display *disp = xfont->disp; Display *disp = xfont->disp;
Atom fontprop = XInternAtom(disp, "FONT", False); Atom fontprop = XInternAtom(disp, "FONT", False);
char *returned_name = NULL; char *returned_name = NULL;
@ -1324,7 +1323,6 @@ static char *pangofont_scale_fontname(GtkWidget *widget, const char *name,
static char *pangofont_size_increment(unifont *font, int increment); static char *pangofont_size_increment(unifont *font, int increment);
struct pangofont { struct pangofont {
struct unifont u;
/* /*
* Pango objects. * Pango objects.
*/ */
@ -1345,6 +1343,8 @@ struct pangofont {
*/ */
int *widthcache; int *widthcache;
unsigned nwidthcache; unsigned nwidthcache;
struct unifont u;
}; };
static const struct unifont_vtable pangofont_vtable = { static const struct unifont_vtable pangofont_vtable = {
@ -1475,7 +1475,7 @@ static unifont *pangofont_create_internal(GtkWidget *widget,
pango_font_metrics_unref(metrics); pango_font_metrics_unref(metrics);
return (unifont *)pfont; return &pfont->u;
} }
static unifont *pangofont_create(GtkWidget *widget, const char *name, static unifont *pangofont_create(GtkWidget *widget, const char *name,
@ -1523,11 +1523,11 @@ static unifont *pangofont_create_fallback(GtkWidget *widget, int height,
static void pangofont_destroy(unifont *font) static void pangofont_destroy(unifont *font)
{ {
struct pangofont *pfont = (struct pangofont *)font; struct pangofont *pfont = FROMFIELD(font, struct pangofont, u);
pango_font_description_free(pfont->desc); pango_font_description_free(pfont->desc);
sfree(pfont->widthcache); sfree(pfont->widthcache);
g_object_unref(pfont->fset); g_object_unref(pfont->fset);
sfree(font); sfree(pfont);
} }
static int pangofont_char_width(PangoLayout *layout, struct pangofont *pfont, static int pangofont_char_width(PangoLayout *layout, struct pangofont *pfont,
@ -1587,7 +1587,7 @@ static void pangofont_draw_internal(unifont_drawctx *ctx, unifont *font,
int len, int wide, int bold, int cellwidth, int len, int wide, int bold, int cellwidth,
int combining) int combining)
{ {
struct pangofont *pfont = (struct pangofont *)font; struct pangofont *pfont = FROMFIELD(font, struct pangofont, u);
PangoLayout *layout; PangoLayout *layout;
PangoRectangle rect; PangoRectangle rect;
char *utfstring, *utfptr; char *utfstring, *utfptr;
@ -2018,7 +2018,7 @@ static char *pangofont_scale_fontname(GtkWidget *widget, const char *name,
static char *pangofont_size_increment(unifont *font, int increment) static char *pangofont_size_increment(unifont *font, int increment)
{ {
struct pangofont *pfont = (struct pangofont *)font; struct pangofont *pfont = FROMFIELD(font, struct pangofont, u);
PangoFontDescription *desc; PangoFontDescription *desc;
int size; int size;
char *newname, *retname; char *newname, *retname;
@ -2177,9 +2177,10 @@ static void multifont_destroy(unifont *font);
static char *multifont_size_increment(unifont *font, int increment); static char *multifont_size_increment(unifont *font, int increment);
struct multifont { struct multifont {
struct unifont u;
unifont *main; unifont *main;
unifont *fallback; unifont *fallback;
struct unifont u;
}; };
static const struct unifont_vtable multifont_vtable = { static const struct unifont_vtable multifont_vtable = {
@ -2238,16 +2239,16 @@ unifont *multifont_create(GtkWidget *widget, const char *name,
mfont->main = font; mfont->main = font;
mfont->fallback = fallback; mfont->fallback = fallback;
return (unifont *)mfont; return &mfont->u;
} }
static void multifont_destroy(unifont *font) static void multifont_destroy(unifont *font)
{ {
struct multifont *mfont = (struct multifont *)font; struct multifont *mfont = FROMFIELD(font, struct multifont, u);
unifont_destroy(mfont->main); unifont_destroy(mfont->main);
if (mfont->fallback) if (mfont->fallback)
unifont_destroy(mfont->fallback); unifont_destroy(mfont->fallback);
sfree(font); sfree(mfont);
} }
typedef void (*unifont_draw_func_t)(unifont_drawctx *ctx, unifont *font, typedef void (*unifont_draw_func_t)(unifont_drawctx *ctx, unifont *font,
@ -2260,7 +2261,7 @@ static void multifont_draw_main(unifont_drawctx *ctx, unifont *font, int x,
int wide, int bold, int cellwidth, int wide, int bold, int cellwidth,
int cellinc, unifont_draw_func_t draw) int cellinc, unifont_draw_func_t draw)
{ {
struct multifont *mfont = (struct multifont *)font; struct multifont *mfont = FROMFIELD(font, struct multifont, u);
unifont *f; unifont *f;
int ok, i; int ok, i;
@ -2306,7 +2307,7 @@ static void multifont_draw_combining(unifont_drawctx *ctx, unifont *font,
static char *multifont_size_increment(unifont *font, int increment) static char *multifont_size_increment(unifont *font, int increment)
{ {
struct multifont *mfont = (struct multifont *)font; struct multifont *mfont = FROMFIELD(font, struct multifont, u);
return unifont_size_increment(mfont->main, increment); return unifont_size_increment(mfont->main, increment);
} }
@ -2320,8 +2321,6 @@ static char *multifont_size_increment(unifont *font, int increment)
typedef struct fontinfo fontinfo; typedef struct fontinfo fontinfo;
typedef struct unifontsel_internal { typedef struct unifontsel_internal {
/* This must be the structure's first element, for cross-casting */
unifontsel u;
GtkListStore *family_model, *style_model, *size_model; GtkListStore *family_model, *style_model, *size_model;
GtkWidget *family_list, *style_list, *size_entry, *size_list; GtkWidget *family_list, *style_list, *size_entry, *size_list;
GtkWidget *filter_buttons[4]; GtkWidget *filter_buttons[4];
@ -2337,6 +2336,8 @@ typedef struct unifontsel_internal {
fontinfo *selected; fontinfo *selected;
int selsize, intendedsize; int selsize, intendedsize;
int inhibit_response; /* inhibit callbacks when we change GUI controls */ int inhibit_response; /* inhibit callbacks when we change GUI controls */
unifontsel u;
} unifontsel_internal; } unifontsel_internal;
/* /*
@ -3674,12 +3675,12 @@ unifontsel *unifontsel_new(const char *wintitle)
fs->selsize = fs->intendedsize = 13; /* random default */ fs->selsize = fs->intendedsize = 13; /* random default */
gtk_widget_set_sensitive(fs->u.ok_button, FALSE); gtk_widget_set_sensitive(fs->u.ok_button, FALSE);
return (unifontsel *)fs; return &fs->u;
} }
void unifontsel_destroy(unifontsel *fontsel) void unifontsel_destroy(unifontsel *fontsel)
{ {
unifontsel_internal *fs = (unifontsel_internal *)fontsel; unifontsel_internal *fs = FROMFIELD(fontsel, unifontsel_internal, u);
fontinfo *info; fontinfo *info;
#ifndef NO_BACKING_PIXMAPS #ifndef NO_BACKING_PIXMAPS
@ -3698,7 +3699,7 @@ void unifontsel_destroy(unifontsel *fontsel)
void unifontsel_set_name(unifontsel *fontsel, const char *fontname) void unifontsel_set_name(unifontsel *fontsel, const char *fontname)
{ {
unifontsel_internal *fs = (unifontsel_internal *)fontsel; unifontsel_internal *fs = FROMFIELD(fontsel, unifontsel_internal, u);
int i, start, end, size, flags; int i, start, end, size, flags;
const char *fontname2 = NULL; const char *fontname2 = NULL;
fontinfo *info; fontinfo *info;
@ -3758,7 +3759,7 @@ void unifontsel_set_name(unifontsel *fontsel, const char *fontname)
char *unifontsel_get_name(unifontsel *fontsel) char *unifontsel_get_name(unifontsel *fontsel)
{ {
unifontsel_internal *fs = (unifontsel_internal *)fontsel; unifontsel_internal *fs = FROMFIELD(fontsel, unifontsel_internal, u);
char *name; char *name;
if (!fs->selected) if (!fs->selected)