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

Tidy up Simon's FontSpec abstraction.

Also, make fontspec_to_str not return the address of an automatic variable.
It now has a memory leak instead.

[originally from svn r2767]
This commit is contained in:
Ben Harris 2003-02-01 15:44:08 +00:00
parent 21275897d2
commit 947b70006e
5 changed files with 61 additions and 57 deletions

View File

@ -1,4 +1,4 @@
/* $Id: mac.c,v 1.38 2003/02/01 12:54:40 simon Exp $ */ /* $Id: mac.c,v 1.39 2003/02/01 15:44:08 ben Exp $ */
/* /*
* Copyright (c) 1999 Ben Harris * Copyright (c) 1999 Ben Harris
* All rights reserved. * All rights reserved.
@ -741,30 +741,27 @@ void old_keyfile_warning(void)
} }
FontSpec platform_default_font(char const *name) FontSpec platform_default_fontspec(char const *name)
{ {
FontSpec ret; FontSpec ret;
long smfs; long smfs;
Str255 pname;
static char cname[256];
if (!strcmp(name, "Font")) { if (!strcmp(name, "Font")) {
smfs = GetScriptVariable(smSystemScript, smScriptMonoFondSize); smfs = GetScriptVariable(smSystemScript, smScriptMonoFondSize);
if (smfs == 0) if (smfs == 0)
smfs = GetScriptVariable(smRoman, smScriptMonoFondSize); smfs = GetScriptVariable(smRoman, smScriptMonoFondSize);
if (smfs != 0) { if (smfs != 0) {
GetFontName(HiWord(smfs), pname); GetFontName(HiWord(smfs), ret.name);
if (pname[0] == 0) if (ret.name[0] == 0)
strcpy(ret.name, "Monaco"); memcpy(ret.name, "\pMonaco", 7);
ret.height = LoWord(smfs); ret.size = LoWord(smfs);
p2cstrcpy(ret.name, pname);
} else { } else {
strcpy(ret.name, "Monaco"); memcpy(ret.name, "\pMonaco", 7);
ret.height = 9; ret.size = 9;
} }
ret.isbold = 0; ret.face = 0;
} else { } else {
ret.name[0] = '\0'; ret.name[0] = 0;
} }
return ret; return ret;
@ -787,7 +784,6 @@ char *platform_default_s(char const *name)
int platform_default_i(char const *name, int def) int platform_default_i(char const *name, int def)
{ {
long smfs;
/* Non-raw cut and paste of line-drawing chars works badly on the /* Non-raw cut and paste of line-drawing chars works badly on the
* current Unix stub implementation of the Unicode functions. * current Unix stub implementation of the Unicode functions.
@ -815,7 +811,9 @@ Filename filename_from_str(char *str)
char *filename_to_str(Filename fn) char *filename_to_str(Filename fn)
{ {
return fn.path; /* FIXME: Memory leak! */
return dupstr(fn.path);
} }
int filename_equal(Filename f1, Filename f2) int filename_equal(Filename f1, Filename f2)

View File

@ -13,21 +13,8 @@
#include <Palettes.h> #include <Palettes.h>
#include <UnicodeConverter.h> #include <UnicodeConverter.h>
#include <stdio.h> /* for FILENAME_MAX */
#include "charset.h" #include "charset.h"
struct Filename {
char path[FILENAME_MAX];
};
#define f_open(filename, mode) ( fopen((filename).path, (mode)) )
struct FontSpec {
char name[64];
int isbold;
int height;
};
#define PUTTY_CREATOR FOUR_CHAR_CODE('pTTY') #define PUTTY_CREATOR FOUR_CHAR_CODE('pTTY')
#define INTERNAL_CREATOR FOUR_CHAR_CODE('pTTI') #define INTERNAL_CREATOR FOUR_CHAR_CODE('pTTI')
#define SESS_TYPE FOUR_CHAR_CODE('Sess') #define SESS_TYPE FOUR_CHAR_CODE('Sess')

View File

@ -1,4 +1,4 @@
/* $Id: macstore.c,v 1.13 2003/02/01 12:54:40 simon Exp $ */ /* $Id: macstore.c,v 1.14 2003/02/01 15:44:08 ben Exp $ */
/* /*
* macstore.c: Macintosh-specific impementation of the interface * macstore.c: Macintosh-specific impementation of the interface
@ -327,18 +327,18 @@ int read_setting_fontspec(void *handle, const char *name, FontSpec *result)
{ {
char *settingname; char *settingname;
FontSpec ret; FontSpec ret;
char tmp[256];
if (!read_setting_s(handle, name, ret.name, sizeof(ret.name))) if (!read_setting_s(handle, name, tmp, sizeof(tmp)))
return 0; return 0;
settingname = dupcat(name, "IsBold", NULL); c2pstrcpy(ret.name, tmp);
ret.isbold = read_setting_i(handle, settingname, -1); settingname = dupcat(name, "Face", NULL);
ret.face = read_setting_i(handle, settingname, 0);
sfree(settingname); sfree(settingname);
if (ret.isbold == -1) return 0;
if (ret.charset == -1) return 0;
settingname = dupcat(name, "Height", NULL); settingname = dupcat(name, "Height", NULL);
ret.height = read_setting_i(handle, settingname, INT_MIN); ret.size = read_setting_i(handle, settingname, 0);
sfree(settingname); sfree(settingname);
if (ret.height == INT_MIN) return 0; if (ret.size == 0) return 0;
*result = ret; *result = ret;
return 1; return 1;
} }
@ -346,13 +346,15 @@ int read_setting_fontspec(void *handle, const char *name, FontSpec *result)
void write_setting_fontspec(void *handle, const char *name, FontSpec font) void write_setting_fontspec(void *handle, const char *name, FontSpec font)
{ {
char *settingname; char *settingname;
char tmp[256];
write_setting_s(handle, name, font.name); p2cstrcpy(tmp, font.name);
settingname = dupcat(name, "IsBold", NULL); write_setting_s(handle, name, tmp);
write_setting_i(handle, settingname, font.isbold); settingname = dupcat(name, "Face", NULL);
write_setting_i(handle, settingname, font.face);
sfree(settingname); sfree(settingname);
settingname = dupcat(name, "Height", NULL); settingname = dupcat(name, "Size", NULL);
write_setting_i(handle, settingname, font.height); write_setting_i(handle, settingname, font.size);
sfree(settingname); sfree(settingname);
} }

View File

@ -4,6 +4,21 @@
typedef void *Context; /* FIXME */ typedef void *Context; /* FIXME */
#include <MacTypes.h>
#include <stdio.h> /* for FILENAME_MAX */
struct Filename {
char path[FILENAME_MAX];
};
#define f_open(filename, mode) ( fopen((filename).path, (mode)) )
/* Suspiciously similar to an ICFontRecord */
struct FontSpec {
short size;
Style face;
Str255 name;
};
/* /*
* On the Mac, Unicode text copied to the clipboard has U+2028 line separators. * On the Mac, Unicode text copied to the clipboard has U+2028 line separators.
* Non-Unicode text will have these converted to CR along with the rest of the * Non-Unicode text will have these converted to CR along with the rest of the

View File

@ -1,4 +1,4 @@
/* $Id: macterm.c,v 1.62 2003/02/01 12:54:40 simon Exp $ */ /* $Id: macterm.c,v 1.63 2003/02/01 15:44:08 ben Exp $ */
/* /*
* Copyright (c) 1999 Simon Tatham * Copyright (c) 1999 Simon Tatham
* Copyright (c) 1999, 2002 Ben Harris * Copyright (c) 1999, 2002 Ben Harris
@ -207,17 +207,15 @@ static void mac_workoutfontscale(Session *s, int wantwidth,
static UnicodeToTextFallbackUPP uni_to_font_fallback_upp; static UnicodeToTextFallbackUPP uni_to_font_fallback_upp;
static void mac_initfont(Session *s) { static void mac_initfont(Session *s) {
Str255 macfont;
FontInfo fi; FontInfo fi;
TextEncoding enc; TextEncoding enc;
OptionBits fbflags; OptionBits fbflags;
SetPort(s->window); SetPort(s->window);
c2pstrcpy(macfont, s->cfg.font.name); GetFNum(s->cfg.font.name, &s->fontnum);
GetFNum(macfont, &s->fontnum);
TextFont(s->fontnum); TextFont(s->fontnum);
TextFace(s->cfg.font.isbold ? bold : 0); TextFace(s->cfg.font.face);
TextSize(s->cfg.font.height); TextSize(s->cfg.font.size);
GetFontInfo(&fi); GetFontInfo(&fi);
s->font_width = CharWidth('W'); /* Well, it's what NCSA uses. */ s->font_width = CharWidth('W'); /* Well, it's what NCSA uses. */
s->font_ascent = fi.ascent; s->font_ascent = fi.ascent;
@ -227,10 +225,10 @@ static void mac_initfont(Session *s) {
&s->font_stdnumer, &s->font_stddenom); &s->font_stdnumer, &s->font_stddenom);
mac_workoutfontscale(s, s->font_width * 2, mac_workoutfontscale(s, s->font_width * 2,
&s->font_widenumer, &s->font_widedenom); &s->font_widenumer, &s->font_widedenom);
TextSize(s->cfg.font.height * 2); TextSize(s->cfg.font.size * 2);
mac_workoutfontscale(s, s->font_width * 2, mac_workoutfontscale(s, s->font_width * 2,
&s->font_bignumer, &s->font_bigdenom); &s->font_bignumer, &s->font_bigdenom);
TextSize(s->cfg.font.height); TextSize(s->cfg.font.size);
if (!s->cfg.bold_colour) { if (!s->cfg.bold_colour) {
TextFace(bold); TextFace(bold);
s->font_boldadjust = s->font_width - CharWidth('W'); s->font_boldadjust = s->font_width - CharWidth('W');
@ -242,7 +240,7 @@ static void mac_initfont(Session *s) {
if (mac_gestalts.encvvers != 0 && if (mac_gestalts.encvvers != 0 &&
UpgradeScriptInfoToTextEncoding(kTextScriptDontCare, UpgradeScriptInfoToTextEncoding(kTextScriptDontCare,
kTextLanguageDontCare, kTextLanguageDontCare,
kTextRegionDontCare, macfont, kTextRegionDontCare, s->cfg.font.name,
&enc) == noErr && &enc) == noErr &&
CreateUnicodeToTextInfoByEncoding(enc, &s->uni_to_font) == noErr) { CreateUnicodeToTextInfoByEncoding(enc, &s->uni_to_font) == noErr) {
if (uni_to_font_fallback_upp == NULL) if (uni_to_font_fallback_upp == NULL)
@ -257,12 +255,15 @@ static void mac_initfont(Session *s) {
goto no_encv; goto no_encv;
} }
} else { } else {
char cfontname[256];
no_encv: no_encv:
s->uni_to_font = NULL; s->uni_to_font = NULL;
p2cstrcpy(cfontname, s->cfg.font.name);
s->font_charset = s->font_charset =
charset_from_macenc(FontToScript(s->fontnum), charset_from_macenc(FontToScript(s->fontnum),
GetScriptManagerVariable(smRegionCode), GetScriptManagerVariable(smRegionCode),
mac_gestalts.sysvers, s->cfg.font.name); mac_gestalts.sysvers, cfontname);
} }
mac_adjustsize(s, s->term->rows, s->term->cols); mac_adjustsize(s, s->term->rows, s->term->cols);
@ -581,7 +582,7 @@ void write_clip(void *cookie, wchar_t *data, int len, int must_deselect)
stsc->scrpStyleTab[0].scrpAscent = s->font_ascent; stsc->scrpStyleTab[0].scrpAscent = s->font_ascent;
stsc->scrpStyleTab[0].scrpFont = s->fontnum; stsc->scrpStyleTab[0].scrpFont = s->fontnum;
stsc->scrpStyleTab[0].scrpFace = 0; stsc->scrpStyleTab[0].scrpFace = 0;
stsc->scrpStyleTab[0].scrpSize = s->cfg.font.height; stsc->scrpStyleTab[0].scrpSize = s->cfg.font.size;
stsc->scrpStyleTab[0].scrpColor.red = 0; stsc->scrpStyleTab[0].scrpColor.red = 0;
stsc->scrpStyleTab[0].scrpColor.green = 0; stsc->scrpStyleTab[0].scrpColor.green = 0;
stsc->scrpStyleTab[0].scrpColor.blue = 0; stsc->scrpStyleTab[0].scrpColor.blue = 0;
@ -975,7 +976,7 @@ struct do_text_args {
void do_text(Context ctx, int x, int y, char *text, int len, void do_text(Context ctx, int x, int y, char *text, int len,
unsigned long attr, int lattr) { unsigned long attr, int lattr) {
Session *s = ctx; Session *s = ctx;
int style = 0; int style;
struct do_text_args a; struct do_text_args a;
RgnHandle textrgn, saveclip; RgnHandle textrgn, saveclip;
char mactextbuf[1024]; char mactextbuf[1024];
@ -1029,25 +1030,26 @@ void do_text(Context ctx, int x, int y, char *text, int len,
a.lattr = lattr; a.lattr = lattr;
switch (lattr & LATTR_MODE) { switch (lattr & LATTR_MODE) {
case LATTR_NORM: case LATTR_NORM:
TextSize(s->cfg.font.height); TextSize(s->cfg.font.size);
a.numer = s->font_stdnumer; a.numer = s->font_stdnumer;
a.denom = s->font_stddenom; a.denom = s->font_stddenom;
break; break;
case LATTR_WIDE: case LATTR_WIDE:
TextSize(s->cfg.font.height); TextSize(s->cfg.font.size);
a.numer = s->font_widenumer; a.numer = s->font_widenumer;
a.denom = s->font_widedenom; a.denom = s->font_widedenom;
break; break;
case LATTR_TOP: case LATTR_TOP:
case LATTR_BOT: case LATTR_BOT:
TextSize(s->cfg.font.height * 2); TextSize(s->cfg.font.size * 2);
a.numer = s->font_bignumer; a.numer = s->font_bignumer;
a.denom = s->font_bigdenom; a.denom = s->font_bigdenom;
break; break;
} }
SetPort(s->window); SetPort(s->window);
TextFont(s->fontnum); TextFont(s->fontnum);
if (s->cfg.font.isbold || (attr & ATTR_BOLD) && !s->cfg.bold_colour) style = s->cfg.font.face;
if ((attr & ATTR_BOLD) && !s->cfg.bold_colour)
style |= bold; style |= bold;
if (attr & ATTR_UNDER) if (attr & ATTR_UNDER)
style |= underline; style |= underline;