2 // "$Id: fl_set_fonts_win32.cxx 7913 2010-11-29 18:18:27Z greg.ercolano $"
4 // WIN32 font utilities for the Fast Light Tool Kit (FLTK).
6 // Copyright 1998-2010 by Bill Spitzak and others.
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
18 // You should have received a copy of the GNU Library General Public
19 // License along with this7 library; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
28 // This function fills in the FLTK font table with all the fonts that
29 // are found on the X server. It tries to place the fonts into families
30 // and to sort them so the first 4 in a family are normal, bold, italic,
32 #include <FL/fl_utf8.h>
37 // Bug: older versions calculated the value for *ap as a side effect of
38 // making the name, and then forgot about it. To avoid having to change
39 // the header files I decided to store this value in the last character
40 // of the font name array.
41 #define ENDOFBUFFER 127 // sizeof(Fl_Font.fontname)-1
43 // turn a stored font name into a pretty name:
44 const char* Fl::get_font_name(Fl_Font fnum
, int* ap
) {
45 Fl_Fontdesc
*f
= fl_fonts
+ fnum
;
46 if (!f
->fontname
[0]) {
47 const char* p
= f
->name
;
48 if (!p
|| !*p
) {if (ap
) *ap
= 0; return "";}
51 case 'B': type
= FL_BOLD
; break;
52 case 'I': type
= FL_ITALIC
; break;
53 case 'P': type
= FL_BOLD
| FL_ITALIC
; break;
54 default: type
= 0; break;
56 strlcpy(f
->fontname
, p
+1, ENDOFBUFFER
);
57 if (type
& FL_BOLD
) strlcat(f
->fontname
, " bold", ENDOFBUFFER
);
58 if (type
& FL_ITALIC
) strlcat(f
->fontname
, " italic", ENDOFBUFFER
);
59 f
->fontname
[ENDOFBUFFER
] = (char)type
;
61 if (ap
) *ap
= f
->fontname
[ENDOFBUFFER
];
65 static int fl_free_font
= FL_FREE_FONT
;
68 enumcbw(CONST LOGFONTW
*lpelf
,
69 CONST TEXTMETRICW
* /*lpntm*/,
72 if (!p
&& lpelf
->lfCharSet
!= ANSI_CHARSET
) return 1;
74 int l
= wcslen(lpelf
->lfFaceName
);
75 unsigned dstlen
= fl_utf8fromwc(n
, 0, (xchar
*)lpelf
->lfFaceName
, l
) + 1; // measure the string
76 n
= (char*) malloc(dstlen
);
77 //n[fl_unicode2utf((xchar*)lpelf->lfFaceName, l, n)] = 0;
78 dstlen
= fl_utf8fromwc(n
, dstlen
, (xchar
*)lpelf
->lfFaceName
, l
); // convert the string
80 for (int i
=0; i
<FL_FREE_FONT
; i
++) // skip if one of our built-in fonts
81 if (!strcmp(Fl::get_font_name((Fl_Font
)i
),n
)) {free(n
);return 1;}
82 char buffer
[LF_FACESIZE
+ 1];
84 buffer
[0] = ' '; Fl::set_font((Fl_Font
)(fl_free_font
++), strdup(buffer
));
85 if (lpelf
->lfWeight
<= 400)
86 buffer
[0] = 'B', Fl::set_font((Fl_Font
)(fl_free_font
++), strdup(buffer
));
87 buffer
[0] = 'I'; Fl::set_font((Fl_Font
)(fl_free_font
++), strdup(buffer
));
88 if (lpelf
->lfWeight
<= 400)
89 buffer
[0] = 'P', Fl::set_font((Fl_Font
)(fl_free_font
++), strdup(buffer
));
94 Fl_Font
Fl::set_fonts(const char* xstarname
) {
95 if (fl_free_font
== FL_FREE_FONT
) {// if not already been called
96 if (!fl_gc
) fl_GetDC(0);
98 EnumFontFamiliesW(fl_gc
, NULL
, (FONTENUMPROCW
)enumcbw
, xstarname
!= 0);
101 return (Fl_Font
)fl_free_font
;
106 static int cyPerInch
;
107 static int sizes
[128];
110 EnumSizeCbW(CONST LOGFONTW
* /*lpelf*/,
111 CONST TEXTMETRICW
*lpntm
,
114 if ((fontType
& RASTER_FONTTYPE
) == 0) {
122 int add
= lpntm
->tmHeight
- lpntm
->tmInternalLeading
;
123 add
= MulDiv(add
, 72, cyPerInch
);
126 while ((start
< nbSize
) && (sizes
[start
] < add
)) {
130 if ((start
< nbSize
) && (sizes
[start
] == add
)) {
134 for (int i
=nbSize
; i
>start
; i
--) sizes
[i
] = sizes
[i
- 1];
139 // Stop enum if buffer overflow
145 Fl::get_font_sizes(Fl_Font fnum
, int*& sizep
) {
147 Fl_Fontdesc
*s
= fl_fonts
+fnum
;
148 if (!s
->name
) s
= fl_fonts
; // empty slot in table, use entry 0
150 if (!fl_gc
) fl_GetDC(0);
151 cyPerInch
= GetDeviceCaps(fl_gc
, LOGPIXELSY
);
152 if (cyPerInch
< 1) cyPerInch
= 1;
154 // int l = fl_utf_nb_char((unsigned char*)s->name+1, strlen(s->name+1));
155 // unsigned short *b = (unsigned short*) malloc((l + 1) * sizeof(short));
156 // fl_utf2unicode((unsigned char*)s->name+1, l, (xchar*)b);
157 const char *nm
= (const char*)s
->name
+1;
158 int len
= strlen(s
->name
+1);
159 int l
= fl_utf8toUtf16(nm
, len
, NULL
, 0); // Pass NULL to query length required
160 unsigned short *b
= (unsigned short*) malloc((l
+ 1) * sizeof(short));
161 l
= fl_utf8toUtf16(nm
, len
, b
, (l
+1)); // Now do the conversion
163 EnumFontFamiliesW(fl_gc
, (WCHAR
*)b
, (FONTENUMPROCW
)EnumSizeCbW
, 0);
172 // End of "$Id: fl_set_fonts_win32.cxx 7913 2010-11-29 18:18:27Z greg.ercolano $".