File: XGCommonFont.m

package info (click to toggle)
gnustep-back 0.11.0-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,192 kB
  • ctags: 1,046
  • sloc: objc: 34,114; ansic: 5,990; sed: 314; makefile: 163; sh: 99
file content (282 lines) | stat: -rw-r--r-- 7,178 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
   XGFontInfo

   NSFont helper for GNUstep GUI X/GPS Backend

   Copyright (C) 1996 Free Software Foundation, Inc.

   Author: Scott Christley
   Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>
   Date: February 1997
   Author:  Felipe A. Rodriguez <far@ix.netcom.com>
   Date: May, October 1998
   Author:  Michael Hanni <mhanni@sprintmail.com>
   Date: August 1998
   Author:  Fred Kiefer <fredkiefer@gmx.de>
   Date: September 2000

   This file is part of the GNUstep GUI X/GPS Backend.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; see the file COPYING.LIB.
   If not, write to the Free Software Foundation,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include <config.h>

#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "xlib/XGContext.h"
#include "xlib/XGPrivate.h"
#include <Foundation/NSDebug.h>
// For the encoding functions
#include <GNUstepBase/Unicode.h>

static Atom XA_SLANT = (Atom)0;
static Atom XA_SETWIDTH_NAME = (Atom)0;
static Atom XA_CHARSET_REGISTRY = (Atom)0;
static Atom XA_CHARSET_ENCODING = (Atom)0;
static Atom XA_SPACING = (Atom)0;
static Atom XA_PIXEL_SIZE = (Atom)0;
static Atom XA_WEIGHT_NAME = (Atom)0;

/*
  static Atom XA_RESOLUTION_X = (Atom)0;
  static Atom XA_RESOLUTION_Y = (Atom)0;
  static Atom XA_ADD_STYLE_NAME = (Atom)0;
  static Atom XA_AVERAGE_WIDTH = (Atom)0;
  static Atom XA_FACE_NAME = (Atom)0;
*/

/*
 * Initialise the X atoms we are going to use
 */
static BOOL XGInitAtoms(Display *dpy)
{
  // X atoms used to query a font

  if (!dpy)
    {
      NSDebugLog(@"No Display opened in XGInitAtoms");      
      return NO;
    }

  XA_PIXEL_SIZE = XInternAtom(dpy, "PIXEL_SIZE", False);
  XA_SPACING = XInternAtom(dpy, "SPACING", False);
  XA_WEIGHT_NAME = XInternAtom(dpy, "WEIGHT_NAME", False);
  XA_SLANT = XInternAtom(dpy, "SLANT", False);
  XA_SETWIDTH_NAME = XInternAtom(dpy, "SETWIDTH_NAME", False);
  XA_CHARSET_REGISTRY = XInternAtom(dpy, "CHARSET_REGISTRY", False);
  XA_CHARSET_ENCODING = XInternAtom(dpy, "CHARSET_ENCODING", False);

/*
  XA_ADD_STYLE_NAME = XInternAtom(dpy, "ADD_STYLE_NAME", False);
  XA_RESOLUTION_X = XInternAtom(dpy, "RESOLUTION_X", False);
  XA_RESOLUTION_Y = XInternAtom(dpy, "RESOLUTION_Y", False);
  XA_AVERAGE_WIDTH = XInternAtom(dpy, "AVERAGE_WIDTH", False);
  XA_FACE_NAME = XInternAtom(dpy, "FACE_NAME", False);
*/

  return YES;
}

/*
 * Read an X Font property of type unsigned long
 */
unsigned long XGFontPropULong(Display *dpy, XFontStruct *font_struct, 
			      Atom atom)
{
  unsigned long lvalue;
  
  if (XGetFontProperty(font_struct, atom, &lvalue))
    return lvalue;
  else
    return 0;
}

/*
 * Read an X Font property of type string
 */
NSString *XGFontPropString(Display *dpy, XFontStruct *font_struct, Atom atom)
{
  unsigned long	lvalue;
  char		*value;
  NSString	*ret = nil;

  if (XGetFontProperty(font_struct, atom, &lvalue) && dpy)
    {
      value = XGetAtomName(dpy, lvalue);
      if (value != NULL)
	{
	  // We convert all props to lowercase so comparing is easier
	  ret = [[NSString stringWithCString: value] lowercaseString];
	  XFree(value);
	}
    }
  
  return ret;
}

NSString *XGFontName(Display *dpy, XFontStruct *font_struct)
{
  return XGFontPropString(dpy, font_struct, XA_FONT);
}

float XGFontPointSize(Display *dpy, XFontStruct *font_struct)
{
  float size = 12.0;
  long pointSize;
  
  if (!XA_PIXEL_SIZE)
    XGInitAtoms(dpy);

  /*
   * We use pixel size here not point size, which is about 10 times its size.
   * Perhaps we will change that later on!
   */
  pointSize = XGFontPropULong(dpy, font_struct, XA_PIXEL_SIZE);
  if (pointSize != 0)
    {
      size = (float) pointSize;
    }

  return size;
}

BOOL XGFontIsFixedPitch(Display *dpy, XFontStruct *font_struct)
{
  /* Is this font fixed pitch? default, NO */
  BOOL fixedFont = NO;
  NSString *spacing;

  // If there is no information per character, all must be equal 
  if (!font_struct->per_char)
    {
	return YES;
    }

  if (!XA_SPACING)
    XGInitAtoms(dpy);

  /*
   * We could also check the value of MONOSPACED, but I have never seen it set.
   */
  spacing = XGFontPropString(dpy, font_struct, XA_SPACING);
  if (spacing != nil)
    {
      if ([spacing isEqualToString: @"m"])
	fixedFont = YES;
    }

  /*
   * We could calculate the pitch from the XLFD but that does not seem to be
   * saved. If we can't get the property, say no and cope.
   */
  return fixedFont;
}

NSString *XGFontFamily(Display *dpy, XFontStruct *font_struct)
{
  NSString *family;

  family = XGFontPropString(dpy, font_struct, XA_FAMILY_NAME);
  if (family == nil)
    return @"Unknown";	// FIXME: We should return the font name instead
  
  return [family capitalizedString];
}

// Get the weight of a X font
int XGWeightOfFont(Display *dpy, XFontStruct *info)
{
  int		w = 5;
  NSString	*string;

  if (!XA_WEIGHT_NAME)
    XGInitAtoms(dpy);

  string = XGFontPropString(dpy, info, XA_WEIGHT_NAME);

  if (string != nil)
    {
      w = [GSFontInfo weightForString: string];
    }

  return w;
}

// Get the traits of a X font
NSFontTraitMask XGTraitsOfFont(Display *dpy, XFontStruct *info)
{
  NSFontTraitMask	mask = 0;
  NSString		*string;
  int			w = XGWeightOfFont(dpy, info);
  
  if (w >= 9)
    mask |= NSBoldFontMask;
  else
    mask |= NSUnboldFontMask;
  
  if (XGFontIsFixedPitch(dpy, info))
    mask |= NSFixedPitchFontMask;
  
  string = XGFontPropString(dpy, info, XA_SLANT);
  if (string != nil)
    {
      char c = [string cString][0];
      
      if (c == 'o' || c == 'i')
	mask |= NSItalicFontMask;
      else
	mask |= NSUnitalicFontMask;
    }
  
  string = XGFontPropString(dpy, info, XA_CHARSET_REGISTRY);
  if (string != nil)
    {
      if (![string isEqualToString: @"iso8859"] &&
	  ![string isEqualToString: @"iso10646"])
	mask |= NSNonStandardCharacterSetFontMask;
    }
  string = XGFontPropString(dpy, info, XA_CHARSET_ENCODING);
  if (string != nil)
    {
      if (![string isEqualToString: @"1"])
	mask |= NSNonStandardCharacterSetFontMask;
    }
  
  string = XGFontPropString(dpy, info, XA_SETWIDTH_NAME);
  if (string != nil)
    {
      if ([string isEqualToString: @"narrow"])
	mask |= NSNarrowFontMask;
      else if ([string isEqualToString: @"semicondensed"])
	mask |= NSCondensedFontMask;
    }
    
  string = XGFontPropString(dpy, info, XA_SPACING);
  if (string != nil)
    {
      if ([string isEqualToString: @"c"])
	mask |= NSCompressedFontMask;
    }
  
  //FIXME: How can I find out about the other traits?
  /*
    unsigned long weight = XGFontPropULong(dpy, info, XA_WEIGHT);
  */

  return mask;
}