The R Project SVN R

Rev

Rev 4606 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4606 Rev 9184
1
/*
1
/*
2
 * GraphApp - Cross-Platform Graphics Programming Library.
2
 * GraphApp - Cross-Platform Graphics Programming Library.
3
 *
3
 *
4
 * File: fonts.c -- font selection functions.
4
 * File: fonts.c -- font selection functions.
5
 * Platform: Windows  Version: 2.35  Date: 1998/04/04
5
 * Platform: Windows  Version: 2.35  Date: 1998/04/04
6
 *
6
 *
7
 * Version: 1.00  Changes: Original version by Lachlan Patrick.
7
 * Version: 1.00  Changes: Original version by Lachlan Patrick.
8
 * Version: 2.00  Changes: New object class system.
8
 * Version: 2.00  Changes: New object class system.
9
 * Version: 2.30  Changes: Now uses font_base.
9
 * Version: 2.30  Changes: Now uses font_base.
10
 * Version: 2.35  Changes: New reference count technique.
10
 * Version: 2.35  Changes: New reference count technique.
11
 */
11
 */
12
 
12
 
13
/* Copyright (C) 1993-1998 Lachlan Patrick
13
/* Copyright (C) 1993-1998 Lachlan Patrick
14
 
14
 
15
   This file is part of GraphApp, a cross-platform C graphics library.
15
   This file is part of GraphApp, a cross-platform C graphics library.
16
 
16
 
17
   GraphApp is free software; you can redistribute it and/or modify it
17
   GraphApp is free software; you can redistribute it and/or modify it
18
   under the terms of the GNU Library General Public License.
18
   under the terms of the GNU Library General Public License.
19
   GraphApp is distributed in the hope that it will be useful, but
19
   GraphApp is distributed in the hope that it will be useful, but
20
   WITHOUT ANY WARRANTY.
20
   WITHOUT ANY WARRANTY.
21
 
21
 
22
   See the file COPYLIB.TXT for details.
22
   See the file COPYLIB.TXT for details.
23
*/
23
*/
24
 
24
 
-
 
25
/* Changes for R:
-
 
26
 
-
 
27
   Use default GUI font
-
 
28
   Do not delete FixedFont
-
 
29
 
-
 
30
 */
-
 
31
 
25
#include "internal.h"
32
#include "internal.h"
26
 
33
 
27
/*
34
/*
28
 *  Pre-defined library fonts.
35
 *  Pre-defined library fonts.
29
 */
36
 */
30
	font SystemFont = NULL;
37
	font SystemFont = NULL;
31
	font FixedFont  = NULL;
38
	font FixedFont  = NULL;
32
	font Times      = NULL;
39
	font Times      = NULL;
33
	font Helvetica  = NULL;
40
	font Helvetica  = NULL;
34
	font Courier    = NULL;
41
	font Courier    = NULL;
35
 
42
 
36
/*
43
/*
37
 *  Dots per inch for the screen.
44
 *  Dots per inch for the screen.
38
 */
45
 */
39
	static int screen_dpix = 96;	/* logical dpi for VGA */
46
	static int screen_dpix = 96;	/* logical dpi for VGA */
40
	static int screen_dpiy = 96;	/* logical dpi for VGA */
47
	static int screen_dpiy = 96;	/* logical dpi for VGA */
41
 
48
 
42
/*
49
/*
43
 *  Private font destructor. Calls the Windows functions needed
50
 *  Private font destructor. Calls the Windows functions needed
44
 *  to free the object from GDI memory.
51
 *  to free the object from GDI memory.
45
 */
52
 */
46
static void private_delfont(font f)
53
static void private_delfont(font f)
47
{
54
{
48
	if (f && (f != SystemFont) && (f != FixedFont) && (f->handle))
55
	if (f && (f != SystemFont) && (f != FixedFont) && (f->handle))
49
			DeleteObject(f->handle);
56
			DeleteObject(f->handle);
50
}
57
}
51
 
58
 
52
/*
59
/*
53
 *  Private object constructor.
60
 *  Private object constructor.
54
 */
61
 */
55
static object get_font_base(void)
62
static object get_font_base(void)
56
{
63
{
57
	static object font_base = NULL;
64
	static object font_base = NULL;
58
 
65
 
59
	if (! font_base)
66
	if (! font_base)
60
		font_base = new_object(BaseObject, 0, NULL);
67
		font_base = new_object(BaseObject, 0, NULL);
61
	return font_base;
68
	return font_base;
62
}
69
}
63
 
70
 
64
PROTECTED font new_font_object(HFONT hf)
71
PROTECTED font new_font_object(HFONT hf)
65
{
72
{
66
	TEXTMETRIC tm;
73
	TEXTMETRIC tm;
67
	HDC dc;
74
	HDC dc;
68
	HFONT old;
75
	HFONT old;
69
	object obj;
76
	object obj;
70
 
77
 
71
	obj = new_object(FontObject, hf, get_font_base());
78
	obj = new_object(FontObject, hf, get_font_base());
72
	if (! obj) {
79
	if (! obj) {
73
		DeleteObject(hf);
80
		DeleteObject(hf);
74
		return NULL;
81
		return NULL;
75
	}
82
	}
76
	obj->die = private_delfont;
83
	obj->die = private_delfont;
77
 
84
 
78
	dc = GetDC(0);
85
	dc = GetDC(0);
79
	old = SelectObject(dc, hf);
86
	old = SelectObject(dc, hf);
80
	GetTextMetrics(dc, &tm);
87
	GetTextMetrics(dc, &tm);
81
 
88
 
82
	obj->depth = 1;
89
	obj->depth = 1;
83
	obj->rect.width = tm.tmAveCharWidth;
90
	obj->rect.width = tm.tmAveCharWidth;
84
	obj->rect.height = tm.tmHeight;
91
	obj->rect.height = tm.tmHeight;
85
	obj->rect.x = tm.tmAscent - tm.tmInternalLeading;
92
	obj->rect.x = tm.tmAscent - tm.tmInternalLeading;
86
	obj->rect.y = tm.tmDescent;
93
	obj->rect.y = tm.tmDescent;
87
 
94
 
88
	SelectObject(dc, old);
95
	SelectObject(dc, old);
89
	ReleaseDC(0, dc);
96
	ReleaseDC(0, dc);
90
 
97
 
91
	return (font) obj;
98
	return (font) obj;
92
}
99
}
93
 
100
 
94
/*
101
/*
95
 *  Private font initialisation function.
102
 *  Private font initialisation function.
96
 */
103
 */
97
PROTECTED
104
PROTECTED
98
void init_fonts(void)
105
void init_fonts(void)
99
{
106
{
100
	HDC info;
107
	HDC info;
101
 
108
 
102
	/* get system information */
109
	/* get system information */
103
	info = CreateIC("DISPLAY", NULL, NULL, NULL);
110
	info = CreateIC("DISPLAY", NULL, NULL, NULL);
104
	screen_dpix = GetDeviceCaps(info, LOGPIXELSX);
111
	screen_dpix = GetDeviceCaps(info, LOGPIXELSX);
105
	screen_dpiy = GetDeviceCaps(info, LOGPIXELSY);
112
	screen_dpiy = GetDeviceCaps(info, LOGPIXELSY);
106
	DeleteDC(info);
113
	DeleteDC(info);
107
 
114
 
108
	/* set up standard fonts */
115
	/* set up standard fonts */
109
	SystemFont = new_font_object(GetStockObject(DEFAULT_GUI_FONT));
116
	SystemFont = new_font_object(GetStockObject(DEFAULT_GUI_FONT));
110
	if (SystemFont)
117
	if (SystemFont)
111
		SystemFont->text = new_string("SystemFont");
118
		SystemFont->text = new_string("SystemFont");
112
 
119
 
113
	FixedFont = new_font_object(GetStockObject(OEM_FIXED_FONT));
120
	FixedFont = new_font_object(GetStockObject(OEM_FIXED_FONT));
114
	Times = newfont("Times New Roman", Plain, -10);
121
	Times = newfont("Times New Roman", Plain, -10);
115
	Helvetica = newfont("Arial", SansSerif, -10);
122
	Helvetica = newfont("Arial", SansSerif, -10);
116
	Courier = newfont("Courier New", FixedWidth, -10);
123
	Courier = newfont("Courier New", FixedWidth, -10);
117
 
124
 
118
	protect_object(SystemFont);
125
	protect_object(SystemFont);
119
	protect_object(FixedFont);
126
	protect_object(FixedFont);
120
	protect_object(Times);
127
	protect_object(Times);
121
	protect_object(Helvetica);
128
	protect_object(Helvetica);
122
	protect_object(Courier);
129
	protect_object(Courier);
123
}
130
}
124
 
131
 
125
/*
132
/*
126
 *  Load a font by name.
133
 *  Load a font by name.
127
 */
134
 */
128
font newfont(char *name, int style, int size)
135
font newfont(char *name, int style, int size)
129
{
136
{
130
	font obj;
137
	font obj;
131
	HFONT hf;
138
	HFONT hf;
132
	LOGFONT lf;
139
	LOGFONT lf;
133
 
140
 
134
	initapp(0,NULL);
141
	initapp(0,NULL);
135
 
142
 
136
	/* This next calculation should convert from point size to
143
	/* This next calculation should convert from point size to
137
	   pixels.  We use this since we always use the MM_TEXT mode,
144
	   pixels.  We use this since we always use the MM_TEXT mode,
138
	   which is in pixels. */
145
	   which is in pixels. */
139
	/* Windows requires the lfHeight field must be negative
146
	/* Windows requires the lfHeight field must be negative
140
	   to specify point size, positive to specify pixel size. */
147
	   to specify point size, positive to specify pixel size. */
141
 
148
 
142
	if (size < 0) /* negative size indicates this is a point size */
149
	if (size < 0) /* negative size indicates this is a point size */
143
		lf.lfHeight = ((screen_dpiy * size)/72);
150
		lf.lfHeight = ((screen_dpiy * size)/72);
144
	else /* positive size indicates a pixel height for the font */
151
	else /* positive size indicates a pixel height for the font */
145
		lf.lfHeight = size;
152
		lf.lfHeight = size;
146
 
153
 
147
	lf.lfWidth = lf.lfEscapement = lf.lfOrientation = 0;
154
	lf.lfWidth = lf.lfEscapement = lf.lfOrientation = 0;
148
	lf.lfWeight = FW_NORMAL;
155
	lf.lfWeight = FW_NORMAL;
149
	lf.lfItalic = lf.lfUnderline = lf.lfStrikeOut = 0;
156
	lf.lfItalic = lf.lfUnderline = lf.lfStrikeOut = 0;
150
	lf.lfCharSet = ANSI_CHARSET;
157
	lf.lfCharSet = ANSI_CHARSET;
151
	if ((! string_diff(name, "Symbol"))
158
	if ((! string_diff(name, "Symbol"))
152
		|| (! string_diff(name, "Wingdings")))
159
		|| (! string_diff(name, "Wingdings")))
153
			lf.lfCharSet = SYMBOL_CHARSET;
160
			lf.lfCharSet = SYMBOL_CHARSET;
154
	lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
161
	lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
155
	lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
162
	lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
156
	lf.lfQuality = DEFAULT_QUALITY;
163
	lf.lfQuality = DEFAULT_QUALITY;
157
	lf.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
164
	lf.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
158
	if ((name != 0) && (*name != '\0'))
165
	if ((name != 0) && (*name != '\0'))
159
		strncpy(lf.lfFaceName, name, LF_FACESIZE-1);
166
		strncpy(lf.lfFaceName, name, LF_FACESIZE-1);
160
 
167
 
161
	if (style & Italic)
168
	if (style & Italic)
162
		lf.lfItalic = 1;
169
		lf.lfItalic = 1;
163
	if (style & Bold)
170
	if (style & Bold)
164
		lf.lfWeight = FW_BOLD;
171
		lf.lfWeight = FW_BOLD;
165
	if (style & FixedWidth)
172
	if (style & FixedWidth)
166
		lf.lfPitchAndFamily |= FIXED_PITCH;
173
		lf.lfPitchAndFamily |= FIXED_PITCH;
167
	if (style & SansSerif)
174
	if (style & SansSerif)
168
		lf.lfPitchAndFamily |= FF_SWISS;
175
		lf.lfPitchAndFamily |= FF_SWISS;
169
 
176
 
170
	if ((hf = CreateFontIndirect(&lf)) == 0)
177
	if ((hf = CreateFontIndirect(&lf)) == 0)
171
		return NULL;
178
		return NULL;
172
 
179
 
173
	obj = new_font_object(hf);
180
	obj = new_font_object(hf);
174
	if (obj)
181
	if (obj)
175
		obj->text = new_string(name);
182
		obj->text = new_string(name);
176
 
183
 
177
	return (font) obj;
184
	return (font) obj;
178
}
185
}
179
 
186
 
180
/*
187
/*
181
 *  Discover the sizes of a font.
188
 *  Discover the sizes of a font.
182
 */
189
 */
183
int fontwidth(font obj)   { return obj->rect.width; }
190
int fontwidth(font obj)   { return obj->rect.width; }
184
int fontheight(font obj)  { return obj->rect.height; }
191
int fontheight(font obj)  { return obj->rect.height; }
185
int fontascent(font obj)  { return obj->rect.x; }
192
int fontascent(font obj)  { return obj->rect.x; }
186
int fontdescent(font obj) { return obj->rect.y; }
193
int fontdescent(font obj) { return obj->rect.y; }