themes: Workaround for bug where a background color of RGB 0,0,0 in Black color schem...
[ntk.git] / src / fl_set_fonts_win32.cxx
blob18aeb26bed6dfcdd9314158944417523a5ffaf6c
1 //
2 // "$Id: fl_set_fonts_win32.cxx 7913 2010-11-29 18:18:27Z greg.ercolano $"
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; 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
21 // USA.
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,
31 // and bold italic.
32 #include <FL/fl_utf8.h>
33 #ifdef __CYGWIN__
34 # include <wchar.h>
35 #endif
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 "";}
49 int type;
50 switch (*p) {
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];
62 return f->fontname;
65 static int fl_free_font = FL_FREE_FONT;
67 static int CALLBACK
68 enumcbw(CONST LOGFONTW *lpelf,
69 CONST TEXTMETRICW * /*lpntm*/,
70 DWORD /*FontType*/,
71 LPARAM p) {
72 if (!p && lpelf->lfCharSet != ANSI_CHARSET) return 1;
73 char *n = NULL;
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
79 n[dstlen] = 0;
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];
83 strcpy(buffer+1, n);
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));
90 free(n);
91 return 1;
92 } /* enumcbw */
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;
105 static int nbSize;
106 static int cyPerInch;
107 static int sizes[128];
108 static int CALLBACK
110 EnumSizeCbW(CONST LOGFONTW * /*lpelf*/,
111 CONST TEXTMETRICW *lpntm,
112 DWORD fontType,
113 LPARAM /*p*/) {
114 if ((fontType & RASTER_FONTTYPE) == 0) {
115 sizes[0] = 0;
116 nbSize = 1;
118 // Scalable font
119 return 0;
122 int add = lpntm->tmHeight - lpntm->tmInternalLeading;
123 add = MulDiv(add, 72, cyPerInch);
125 int start = 0;
126 while ((start < nbSize) && (sizes[start] < add)) {
127 start++;
130 if ((start < nbSize) && (sizes[start] == add)) {
131 return 1;
134 for (int i=nbSize; i>start; i--) sizes[i] = sizes[i - 1];
136 sizes[start] = add;
137 nbSize++;
139 // Stop enum if buffer overflow
140 return nbSize < 128;
145 Fl::get_font_sizes(Fl_Font fnum, int*& sizep) {
146 nbSize = 0;
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
162 b[l] = 0;
163 EnumFontFamiliesW(fl_gc, (WCHAR*)b, (FONTENUMPROCW)EnumSizeCbW, 0);
164 free(b);
166 sizep = sizes;
167 return nbSize;
172 // End of "$Id: fl_set_fonts_win32.cxx 7913 2010-11-29 18:18:27Z greg.ercolano $".