fltk  1.3.5-source
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X.
  Fossies Dox: fltk-1.3.5-source.tar.bz2  ("inofficial" and yet experimental doxygen-generated source code documentation)  

fl_set_fonts_win32.cxx
Go to the documentation of this file.
1 //
2 // "$Id$"
3 //
4 // WIN32 font utilities for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2010 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file. If this
10 // file is missing or damaged, see the license at:
11 //
12 // http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 // http://www.fltk.org/str.php
17 //
18 
19 // This function fills in the FLTK font table with all the fonts that
20 // are found on the X server. It tries to place the fonts into families
21 // and to sort them so the first 4 in a family are normal, bold, italic,
22 // and bold italic.
23 #include <FL/fl_utf8.h>
24 #ifdef __CYGWIN__
25 # include <wchar.h>
26 #endif
27 
28 // Bug: older versions calculated the value for *ap as a side effect of
29 // making the name, and then forgot about it. To avoid having to change
30 // the header files I decided to store this value in the last character
31 // of the font name array.
32 #define ENDOFBUFFER 127 // sizeof(Fl_Font.fontname)-1
33 
34 // turn a stored font name into a pretty name:
35 const char* Fl::get_font_name(Fl_Font fnum, int* ap) {
36  Fl_Fontdesc *f = fl_fonts + fnum;
37  if (!f->fontname[0]) {
38  const char* p = f->name;
39  if (!p || !*p) {if (ap) *ap = 0; return "";}
40  int type;
41  switch (*p) {
42  case 'B': type = FL_BOLD; break;
43  case 'I': type = FL_ITALIC; break;
44  case 'P': type = FL_BOLD | FL_ITALIC; break;
45  default: type = 0; break;
46  }
47  strlcpy(f->fontname, p+1, ENDOFBUFFER);
48  if (type & FL_BOLD) strlcat(f->fontname, " bold", ENDOFBUFFER);
49  if (type & FL_ITALIC) strlcat(f->fontname, " italic", ENDOFBUFFER);
50  f->fontname[ENDOFBUFFER] = (char)type;
51  }
52  if (ap) *ap = f->fontname[ENDOFBUFFER];
53  return f->fontname;
54 }
55 
57 
58 static int CALLBACK
59 enumcbw(CONST LOGFONTW *lpelf,
60  CONST TEXTMETRICW * /*lpntm*/,
61  DWORD /*FontType*/,
62  LPARAM p) {
63  if (!p && lpelf->lfCharSet != ANSI_CHARSET) return 1;
64  char *n = NULL;
65  size_t l = wcslen(lpelf->lfFaceName);
66  unsigned dstlen = fl_utf8fromwc(n, 0, (xchar*)lpelf->lfFaceName, (unsigned) l) + 1; // measure the string
67  n = (char*) malloc(dstlen);
68 //n[fl_unicode2utf((xchar*)lpelf->lfFaceName, l, n)] = 0;
69  dstlen = fl_utf8fromwc(n, dstlen, (xchar*)lpelf->lfFaceName, (unsigned) l); // convert the string
70  n[dstlen] = 0;
71  for (int i=0; i<FL_FREE_FONT; i++) // skip if one of our built-in fonts
72  if (!strcmp(Fl::get_font_name((Fl_Font)i),n)) {free(n);return 1;}
73  char buffer[LF_FACESIZE + 1];
74  strcpy(buffer+1, n);
75  buffer[0] = ' '; Fl::set_font((Fl_Font)(fl_free_font++), strdup(buffer));
76  if (lpelf->lfWeight <= 400)
77  buffer[0] = 'B', Fl::set_font((Fl_Font)(fl_free_font++), strdup(buffer));
78  buffer[0] = 'I'; Fl::set_font((Fl_Font)(fl_free_font++), strdup(buffer));
79  if (lpelf->lfWeight <= 400)
80  buffer[0] = 'P', Fl::set_font((Fl_Font)(fl_free_font++), strdup(buffer));
81  free(n);
82  return 1;
83 } /* enumcbw */
84 
85 Fl_Font Fl::set_fonts(const char* xstarname) {
86  if (fl_free_font == FL_FREE_FONT) {// if not already been called
87  if (!fl_gc) fl_GetDC(0);
88 
89  EnumFontFamiliesW(fl_gc, NULL, (FONTENUMPROCW)enumcbw, xstarname != 0);
90 
91  }
92  return (Fl_Font)fl_free_font;
93 }
94 
95 
96 static int nbSize;
97 static int cyPerInch;
98 static int sizes[128];
99 static int CALLBACK
100 
101 EnumSizeCbW(CONST LOGFONTW * /*lpelf*/,
102  CONST TEXTMETRICW *lpntm,
103  DWORD fontType,
104  LPARAM /*p*/) {
105  if ((fontType & RASTER_FONTTYPE) == 0) {
106  sizes[0] = 0;
107  nbSize = 1;
108 
109  // Scalable font
110  return 0;
111  }
112 
113  int add = lpntm->tmHeight - lpntm->tmInternalLeading;
114  add = MulDiv(add, 72, cyPerInch);
115 
116  int start = 0;
117  while ((start < nbSize) && (sizes[start] < add)) {
118  start++;
119  }
120 
121  if ((start < nbSize) && (sizes[start] == add)) {
122  return 1;
123  }
124 
125  for (int i=nbSize; i>start; i--) sizes[i] = sizes[i - 1];
126 
127  sizes[start] = add;
128  nbSize++;
129 
130  // Stop enum if buffer overflow
131  return nbSize < 128;
132 }
133 
134 
135 int
136 Fl::get_font_sizes(Fl_Font fnum, int*& sizep) {
137  nbSize = 0;
138  Fl_Fontdesc *s = fl_fonts+fnum;
139  if (!s->name) s = fl_fonts; // empty slot in table, use entry 0
140 
141  if (!fl_gc) fl_GetDC(0);
142  cyPerInch = GetDeviceCaps(fl_gc, LOGPIXELSY);
143  if (cyPerInch < 1) cyPerInch = 1;
144 
145 // int l = fl_utf_nb_char((unsigned char*)s->name+1, strlen(s->name+1));
146 // unsigned short *b = (unsigned short*) malloc((l + 1) * sizeof(short));
147 // fl_utf2unicode((unsigned char*)s->name+1, l, (xchar*)b);
148  const char *nm = (const char*)s->name+1;
149  size_t len = strlen(s->name+1);
150  unsigned l = fl_utf8toUtf16(nm, (unsigned) len, NULL, 0); // Pass NULL to query length required
151  unsigned short *b = (unsigned short*) malloc((l + 1) * sizeof(short));
152  l = fl_utf8toUtf16(nm, (unsigned) len, b, (l+1)); // Now do the conversion
153  b[l] = 0;
154  EnumFontFamiliesW(fl_gc, (WCHAR*)b, (FONTENUMPROCW)EnumSizeCbW, 0);
155  free(b);
156 
157  sizep = sizes;
158  return nbSize;
159 }
160 
161 
162 //
163 // End of "$Id$".
164 //
xchar
#define xchar
Definition: fl_utf8.h:68
Fl::get_font_sizes
static int get_font_sizes(Fl_Font, int *&sizep)
Definition: fl_set_fonts_mac.cxx:183
fl_utf8toUtf16
unsigned fl_utf8toUtf16(const char *src, unsigned srclen, unsigned short *dst, unsigned dstlen)
Definition: fl_utf.c:432
EnumSizeCbW
static int CALLBACK EnumSizeCbW(CONST LOGFONTW *, CONST TEXTMETRICW *lpntm, DWORD fontType, LPARAM)
Definition: fl_set_fonts_win32.cxx:101
FL_ITALIC
const Fl_Font FL_ITALIC
add this to helvetica, courier, or times
Definition: Enumerations.H:898
Fl_Fontdesc
Definition: Fl_Font.H:86
Fl::set_fonts
static Fl_Font set_fonts(const char *=0)
Definition: fl_set_fonts_mac.cxx:103
free
void free()
NULL
#define NULL
Definition: forms.H:34
fl_utf8fromwc
unsigned fl_utf8fromwc(char *dst, unsigned dstlen, const wchar_t *src, unsigned srclen)
Definition: fl_utf.c:617
b
long b
Definition: jpegint.h:397
ENDOFBUFFER
#define ENDOFBUFFER
Definition: fl_set_fonts_win32.cxx:32
Fl_Fontdesc::name
const char * name
Definition: Fl_Font.H:87
buffer
static char * buffer
Definition: file.cxx:215
p
static menustate * p
Definition: Fl_Menu.cxx:606
FL_FREE_FONT
const Fl_Font FL_FREE_FONT
first one to allocate
Definition: Enumerations.H:896
cyPerInch
static int cyPerInch
Definition: fl_set_fonts_win32.cxx:97
fl_utf8.h
header for Unicode and UTF-8 character handling
Fl::get_font_name
static const char * get_font_name(Fl_Font, int *attributes=0)
Definition: fl_set_fonts_mac.cxx:31
strlcpy
#define strlcpy
Definition: flstring.h:84
nbSize
static int nbSize
Definition: fl_set_fonts_win32.cxx:96
fl_free_font
static int fl_free_font
Definition: fl_set_fonts_win32.cxx:56
enumcbw
static int CALLBACK enumcbw(CONST LOGFONTW *lpelf, CONST TEXTMETRICW *, DWORD, LPARAM p)
Definition: fl_set_fonts_win32.cxx:59
FL_BOLD
const Fl_Font FL_BOLD
add this to helvetica, courier, or times
Definition: Enumerations.H:897
fl_GetDC
FL_EXPORT HDC fl_GetDC(Window)
sizes
static int sizes[128]
Definition: fl_set_fonts_win32.cxx:98
Fl::set_font
static void set_font(Fl_Font, const char *)
Definition: fl_set_font.cxx:34
Fl_Font
int Fl_Font
Definition: Enumerations.H:877
fl_gc
CGContextRef fl_gc
Definition: Fl_win32.cxx:2568
f
Fl_Box_Draw_F * f
Definition: fl_boxtype.cxx:285
malloc
voidp malloc()
fl_fonts
FL_EXPORT Fl_Fontdesc * fl_fonts
Definition: fl_font_mac.cxx:22
strlcat
#define strlcat
Definition: flstring.h:79
start
static int start(Fl_RGB_Image *img, int XP, int YP, int WP, int HP, int w, int h, int &cx, int &cy, int &X, int &Y, int &W, int &H)
Definition: Fl_Image.cxx:655