1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-06-30 19:12:48 -05:00

Change the semantics of 'FontSpec' so that it's a dynamically

allocated type.

The main reason for this is to stop it from taking up a fixed large
amount of space in every 'struct value' subunion in conf.c, although
that makes little difference so far because Filename is still doing
the same thing (and is therefore next on my list). However, the
removal of its arbitrary length limit is not to be sneezed at.

[originally from svn r9314]
This commit is contained in:
Simon Tatham
2011-10-01 17:38:59 +00:00
parent f69591412c
commit 9c75fe9a3f
19 changed files with 220 additions and 139 deletions

View File

@ -1639,8 +1639,8 @@ void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
shortcuts[nshortcuts++] = ctrl->fontselect.shortcut;
statictext(&pos, escaped, 1, base_id);
staticbtn(&pos, "", base_id+1, "Change...", base_id+2);
data = fontspec_new("", 0, 0, 0);
sfree(escaped);
data = snew(FontSpec);
break;
default:
assert(!"Can't happen");
@ -1934,21 +1934,21 @@ int winctrl_handle_command(struct dlgparam *dp, UINT msg,
CHOOSEFONT cf;
LOGFONT lf;
HDC hdc;
FontSpec fs = *(FontSpec *)c->data;
FontSpec *fs = (FontSpec *)c->data;
hdc = GetDC(0);
lf.lfHeight = -MulDiv(fs.height,
lf.lfHeight = -MulDiv(fs->height,
GetDeviceCaps(hdc, LOGPIXELSY), 72);
ReleaseDC(0, hdc);
lf.lfWidth = lf.lfEscapement = lf.lfOrientation = 0;
lf.lfItalic = lf.lfUnderline = lf.lfStrikeOut = 0;
lf.lfWeight = (fs.isbold ? FW_BOLD : 0);
lf.lfCharSet = fs.charset;
lf.lfWeight = (fs->isbold ? FW_BOLD : 0);
lf.lfCharSet = fs->charset;
lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lf.lfQuality = DEFAULT_QUALITY;
lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
strncpy(lf.lfFaceName, fs.name,
strncpy(lf.lfFaceName, fs->name,
sizeof(lf.lfFaceName) - 1);
lf.lfFaceName[sizeof(lf.lfFaceName) - 1] = '\0';
@ -1959,13 +1959,11 @@ int winctrl_handle_command(struct dlgparam *dp, UINT msg,
CF_FORCEFONTEXIST | CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
if (ChooseFont(&cf)) {
strncpy(fs.name, lf.lfFaceName,
sizeof(fs.name) - 1);
fs.name[sizeof(fs.name) - 1] = '\0';
fs.isbold = (lf.lfWeight == FW_BOLD);
fs.charset = lf.lfCharSet;
fs.height = cf.iPointSize / 10;
fs = fontspec_new(lf.lfFaceName, (lf.lfWeight == FW_BOLD),
cf.iPointSize / 10, lf.lfCharSet);
dlg_fontsel_set(ctrl, dp, fs);
fontspec_free(fs);
ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
}
}
@ -2313,34 +2311,35 @@ void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn)
fn->path[lenof(fn->path)-1] = '\0';
}
void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fs)
void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec *fs)
{
char *buf, *boldstr;
struct dlgparam *dp = (struct dlgparam *)dlg;
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
assert(c && c->ctrl->generic.type == CTRL_FONTSELECT);
*(FontSpec *)c->data = fs; /* structure copy */
fontspec_free((FontSpec *)c->data);
c->data = fontspec_copy(fs);
boldstr = (fs.isbold ? "bold, " : "");
if (fs.height == 0)
buf = dupprintf("Font: %s, %sdefault height", fs.name, boldstr);
boldstr = (fs->isbold ? "bold, " : "");
if (fs->height == 0)
buf = dupprintf("Font: %s, %sdefault height", fs->name, boldstr);
else
buf = dupprintf("Font: %s, %s%d-%s", fs.name, boldstr,
(fs.height < 0 ? -fs.height : fs.height),
(fs.height < 0 ? "pixel" : "point"));
buf = dupprintf("Font: %s, %s%d-%s", fs->name, boldstr,
(fs->height < 0 ? -fs->height : fs->height),
(fs->height < 0 ? "pixel" : "point"));
SetDlgItemText(dp->hwnd, c->base_id+1, buf);
sfree(buf);
dlg_auto_set_fixed_pitch_flag(dp);
}
void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fs)
FontSpec *dlg_fontsel_get(union control *ctrl, void *dlg)
{
struct dlgparam *dp = (struct dlgparam *)dlg;
struct winctrl *c = dlg_findbyctrl(dp, ctrl);
assert(c && c->ctrl->generic.type == CTRL_FONTSELECT);
*fs = *(FontSpec *)c->data; /* structure copy */
return fontspec_copy((FontSpec *)c->data);
}
/*
@ -2482,7 +2481,7 @@ void dlg_auto_set_fixed_pitch_flag(void *dlg)
{
struct dlgparam *dp = (struct dlgparam *)dlg;
Conf *conf = (Conf *)dp->data;
FontSpec *font;
FontSpec *fs;
int quality;
HFONT hfont;
HDC hdc;
@ -2500,14 +2499,14 @@ void dlg_auto_set_fixed_pitch_flag(void *dlg)
*/
quality = conf_get_int(conf, CONF_font_quality);
font = conf_get_fontspec(conf, CONF_font);
fs = conf_get_fontspec(conf, CONF_font);
hfont = CreateFont(0, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, FONT_QUALITY(quality),
FIXED_PITCH | FF_DONTCARE, font->name);
FIXED_PITCH | FF_DONTCARE, fs->name);
hdc = GetDC(NULL);
if (font && hdc && SelectObject(hdc, hfont) && GetTextMetrics(hdc, &tm)) {
if (hdc && SelectObject(hdc, hfont) && GetTextMetrics(hdc, &tm)) {
/* Note that the TMPF_FIXED_PITCH bit is defined upside down :-( */
is_var = (tm.tmPitchAndFamily & TMPF_FIXED_PITCH);
} else {

View File

@ -6,18 +6,12 @@
#include <commctrl.h>
FontSpec platform_default_fontspec(const char *name)
FontSpec *platform_default_fontspec(const char *name)
{
FontSpec ret;
if (!strcmp(name, "Font")) {
strcpy(ret.name, "Courier New");
ret.isbold = 0;
ret.charset = ANSI_CHARSET;
ret.height = 10;
} else {
ret.name[0] = '\0';
}
return ret;
if (!strcmp(name, "Font"))
return fontspec_new("Courier New", 0, 10, ANSI_CHARSET);
else
return fontspec_new("", 0, 0, 0);
}
Filename platform_default_filename(const char *name)

View File

@ -2315,7 +2315,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
{
FontSpec *font = conf_get_fontspec(conf, CONF_font);
FontSpec *prev_font = conf_get_fontspec(prev_conf,
CONF_font);
CONF_font);
if (!strcmp(font->name, prev_font->name) ||
!strcmp(conf_get_str(conf, CONF_line_codepage),

View File

@ -379,3 +379,51 @@ void *minefield_c_realloc(void *p, size_t size)
}
#endif /* MINEFIELD */
FontSpec *fontspec_new(const char *name,
int bold, int height, int charset)
{
FontSpec *f = snew(FontSpec);
f->name = dupstr(name);
f->isbold = bold;
f->height = height;
f->charset = charset;
return f;
}
FontSpec *fontspec_copy(const FontSpec *f)
{
return fontspec_new(f->name, f->isbold, f->height, f->charset);
}
void fontspec_free(FontSpec *f)
{
sfree(f->name);
sfree(f);
}
int fontspec_serialise(FontSpec *f, void *vdata)
{
char *data = (char *)vdata;
int len = strlen(f->name) + 1; /* include trailing NUL */
if (data) {
strcpy(data, f->name);
PUT_32BIT_MSB_FIRST(data + len, f->isbold);
PUT_32BIT_MSB_FIRST(data + len + 4, f->height);
PUT_32BIT_MSB_FIRST(data + len + 8, f->charset);
}
return len + 12; /* also include three 4-byte ints */
}
FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
{
char *data = (char *)vdata;
char *end;
if (maxsize < 13)
return NULL;
end = memchr(data, '\0', maxsize-12);
if (!end)
return NULL;
end++;
*used = end - data + 12;
return fontspec_new(data,
GET_32BIT_MSB_FIRST(end),
GET_32BIT_MSB_FIRST(end + 4),
GET_32BIT_MSB_FIRST(end + 8));
}

View File

@ -186,50 +186,47 @@ int read_setting_i(void *handle, const char *key, int defvalue)
return val;
}
int read_setting_fontspec(void *handle, const char *name, FontSpec *result)
FontSpec *read_setting_fontspec(void *handle, const char *name)
{
char *settingname;
FontSpec ret;
char *fontname;
int isbold, height, charset;
fontname = read_setting_s(handle, name);
if (!fontname)
return 0;
strncpy(ret.name, fontname, sizeof(ret.name)-1);
ret.name[sizeof(ret.name)-1] = '\0';
sfree(fontname);
return NULL;
settingname = dupcat(name, "IsBold", NULL);
ret.isbold = read_setting_i(handle, settingname, -1);
isbold = read_setting_i(handle, settingname, -1);
sfree(settingname);
if (ret.isbold == -1) return 0;
if (isbold == -1) return NULL;
settingname = dupcat(name, "CharSet", NULL);
ret.charset = read_setting_i(handle, settingname, -1);
charset = read_setting_i(handle, settingname, -1);
sfree(settingname);
if (ret.charset == -1) return 0;
if (charset == -1) return NULL;
settingname = dupcat(name, "Height", NULL);
ret.height = read_setting_i(handle, settingname, INT_MIN);
height = read_setting_i(handle, settingname, INT_MIN);
sfree(settingname);
if (ret.height == INT_MIN) return 0;
*result = ret;
return 1;
if (height == INT_MIN) return NULL;
return fontspec_new(fontname, isbold, height, charset);
}
void write_setting_fontspec(void *handle, const char *name, FontSpec font)
void write_setting_fontspec(void *handle, const char *name, FontSpec *font)
{
char *settingname;
write_setting_s(handle, name, font.name);
write_setting_s(handle, name, font->name);
settingname = dupcat(name, "IsBold", NULL);
write_setting_i(handle, settingname, font.isbold);
write_setting_i(handle, settingname, font->isbold);
sfree(settingname);
settingname = dupcat(name, "CharSet", NULL);
write_setting_i(handle, settingname, font.charset);
write_setting_i(handle, settingname, font->charset);
sfree(settingname);
settingname = dupcat(name, "Height", NULL);
write_setting_i(handle, settingname, font.height);
write_setting_i(handle, settingname, font->height);
sfree(settingname);
}

View File

@ -21,11 +21,13 @@ struct Filename {
#define f_open(filename, mode, isprivate) ( fopen((filename).path, (mode)) )
struct FontSpec {
char name[64];
char *name;
int isbold;
int height;
int charset;
};
struct FontSpec *fontspec_new(const char *name,
int bold, int height, int charset);
#ifndef CLEARTYPE_QUALITY
#define CLEARTYPE_QUALITY 5