1998-10-20 00:46:58 +04:00
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// "$Id$"
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
// WIN32 font utilities for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2009-01-02 00:28:26 +03:00
|
|
|
// Copyright 1998-2009 by Bill Spitzak and others.
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
// 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
|
2001-11-26 23:13:29 +03:00
|
|
|
// License along with this7 library; if not, write to the Free Software
|
1998-10-20 00:46:58 +04:00
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
// USA.
|
|
|
|
//
|
2005-04-16 04:13:17 +04:00
|
|
|
// Please report all bugs and problems on the following page:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/str.php
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2001-12-03 21:29:49 +03:00
|
|
|
// This function fills in the FLTK font table with all the fonts that
|
1998-10-06 22:21:25 +04:00
|
|
|
// are found on the X server. It tries to place the fonts into families
|
|
|
|
// and to sort them so the first 4 in a family are normal, bold, italic,
|
|
|
|
// and bold italic.
|
2008-10-04 21:26:07 +04:00
|
|
|
#include <FL/fl_utf8.h>
|
|
|
|
#ifdef __CYGWIN__
|
|
|
|
# include <wchar.h>
|
|
|
|
#endif
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2003-03-09 03:22:20 +03:00
|
|
|
// Bug: older versions calculated the value for *ap as a side effect of
|
|
|
|
// making the name, and then forgot about it. To avoid having to change
|
|
|
|
// the header files I decided to store this value in the last character
|
|
|
|
// of the font name array.
|
|
|
|
#define ENDOFBUFFER 127 // sizeof(Fl_Font.fontname)-1
|
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
// turn a stored font name into a pretty name:
|
|
|
|
const char* Fl::get_font_name(Fl_Font fnum, int* ap) {
|
2002-08-09 05:09:49 +04:00
|
|
|
Fl_Fontdesc *f = fl_fonts + fnum;
|
|
|
|
if (!f->fontname[0]) {
|
|
|
|
const char* p = f->name;
|
|
|
|
if (!p || !*p) {if (ap) *ap = 0; return "";}
|
|
|
|
int type;
|
|
|
|
switch (*p) {
|
|
|
|
case 'B': type = FL_BOLD; break;
|
|
|
|
case 'I': type = FL_ITALIC; break;
|
|
|
|
case 'P': type = FL_BOLD | FL_ITALIC; break;
|
|
|
|
default: type = 0; break;
|
|
|
|
}
|
2003-03-09 03:22:20 +03:00
|
|
|
strlcpy(f->fontname, p+1, ENDOFBUFFER);
|
|
|
|
if (type & FL_BOLD) strlcat(f->fontname, " bold", ENDOFBUFFER);
|
|
|
|
if (type & FL_ITALIC) strlcat(f->fontname, " italic", ENDOFBUFFER);
|
|
|
|
f->fontname[ENDOFBUFFER] = (char)type;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
2003-03-09 03:22:20 +03:00
|
|
|
if (ap) *ap = f->fontname[ENDOFBUFFER];
|
2002-08-09 05:09:49 +04:00
|
|
|
return f->fontname;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int fl_free_font = FL_FREE_FONT;
|
|
|
|
|
2001-12-03 21:29:49 +03:00
|
|
|
static int CALLBACK
|
2008-09-11 03:56:49 +04:00
|
|
|
enumcbw(CONST LOGFONTW *lpelf,
|
|
|
|
CONST TEXTMETRICW * /*lpntm*/,
|
2002-06-29 04:10:05 +04:00
|
|
|
DWORD /*FontType*/,
|
2001-12-03 21:29:49 +03:00
|
|
|
LPARAM p) {
|
|
|
|
if (!p && lpelf->lfCharSet != ANSI_CHARSET) return 1;
|
2008-09-11 03:56:49 +04:00
|
|
|
char *n = NULL;
|
|
|
|
int l = wcslen(lpelf->lfFaceName);
|
|
|
|
unsigned dstlen = fl_utf8fromwc(n, 0, (xchar*)lpelf->lfFaceName, l) + 1; // measure the string
|
|
|
|
n = (char*) malloc(dstlen);
|
|
|
|
//n[fl_unicode2utf((xchar*)lpelf->lfFaceName, l, n)] = 0;
|
|
|
|
dstlen = fl_utf8fromwc(n, dstlen, (xchar*)lpelf->lfFaceName, l); // convert the string
|
|
|
|
n[dstlen] = 0;
|
1998-10-06 22:21:25 +04:00
|
|
|
for (int i=0; i<FL_FREE_FONT; i++) // skip if one of our built-in fonts
|
2008-09-11 03:56:49 +04:00
|
|
|
if (!strcmp(Fl::get_font_name((Fl_Font)i),n)) {free(n);return 1;}
|
2001-12-03 21:29:49 +03:00
|
|
|
char buffer[LF_FACESIZE + 1];
|
1998-10-06 22:21:25 +04:00
|
|
|
strcpy(buffer+1, n);
|
|
|
|
buffer[0] = ' '; Fl::set_font((Fl_Font)(fl_free_font++), strdup(buffer));
|
2001-12-03 21:29:49 +03:00
|
|
|
if (lpelf->lfWeight <= 400)
|
1998-10-06 22:21:25 +04:00
|
|
|
buffer[0] = 'B', Fl::set_font((Fl_Font)(fl_free_font++), strdup(buffer));
|
|
|
|
buffer[0] = 'I'; Fl::set_font((Fl_Font)(fl_free_font++), strdup(buffer));
|
2001-12-03 21:29:49 +03:00
|
|
|
if (lpelf->lfWeight <= 400)
|
1998-10-06 22:21:25 +04:00
|
|
|
buffer[0] = 'P', Fl::set_font((Fl_Font)(fl_free_font++), strdup(buffer));
|
2008-09-11 03:56:49 +04:00
|
|
|
free(n);
|
1998-10-06 22:21:25 +04:00
|
|
|
return 1;
|
2008-09-11 03:56:49 +04:00
|
|
|
} /* enumcbw */
|
1998-10-06 22:21:25 +04:00
|
|
|
|
|
|
|
Fl_Font Fl::set_fonts(const char* xstarname) {
|
2000-10-17 11:03:41 +04:00
|
|
|
if (fl_free_font == FL_FREE_FONT) {// if not already been called
|
|
|
|
if (!fl_gc) fl_GetDC(0);
|
2008-09-11 03:56:49 +04:00
|
|
|
|
|
|
|
EnumFontFamiliesW(fl_gc, NULL, (FONTENUMPROCW)enumcbw, xstarname != 0);
|
|
|
|
|
2000-10-17 11:03:41 +04:00
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
return (Fl_Font)fl_free_font;
|
|
|
|
}
|
|
|
|
|
2001-12-03 21:29:49 +03:00
|
|
|
|
|
|
|
static int nbSize;
|
|
|
|
static int cyPerInch;
|
|
|
|
static int sizes[128];
|
|
|
|
static int CALLBACK
|
2008-09-11 03:56:49 +04:00
|
|
|
|
|
|
|
EnumSizeCbW(CONST LOGFONTW * /*lpelf*/,
|
|
|
|
CONST TEXTMETRICW *lpntm,
|
|
|
|
DWORD fontType,
|
|
|
|
LPARAM /*p*/) {
|
2001-12-03 21:29:49 +03:00
|
|
|
if ((fontType & RASTER_FONTTYPE) == 0) {
|
|
|
|
sizes[0] = 0;
|
|
|
|
nbSize = 1;
|
|
|
|
|
|
|
|
// Scalable font
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int add = lpntm->tmHeight - lpntm->tmInternalLeading;
|
|
|
|
add = MulDiv(add, 72, cyPerInch);
|
|
|
|
|
|
|
|
int start = 0;
|
|
|
|
while ((start < nbSize) && (sizes[start] < add)) {
|
|
|
|
start++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((start < nbSize) && (sizes[start] == add)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=nbSize; i>start; i--) sizes[i] = sizes[i - 1];
|
|
|
|
|
|
|
|
sizes[start] = add;
|
|
|
|
nbSize++;
|
|
|
|
|
|
|
|
// Stop enum if buffer overflow
|
|
|
|
return nbSize < 128;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
1998-10-20 00:46:58 +04:00
|
|
|
|
2001-12-03 21:29:49 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
Fl::get_font_sizes(Fl_Font fnum, int*& sizep) {
|
|
|
|
nbSize = 0;
|
|
|
|
Fl_Fontdesc *s = fl_fonts+fnum;
|
|
|
|
if (!s->name) s = fl_fonts; // empty slot in table, use entry 0
|
|
|
|
|
|
|
|
if (!fl_gc) fl_GetDC(0);
|
|
|
|
cyPerInch = GetDeviceCaps(fl_gc, LOGPIXELSY);
|
|
|
|
if (cyPerInch < 1) cyPerInch = 1;
|
2008-09-11 03:56:49 +04:00
|
|
|
|
|
|
|
// int l = fl_utf_nb_char((unsigned char*)s->name+1, strlen(s->name+1));
|
|
|
|
// unsigned short *b = (unsigned short*) malloc((l + 1) * sizeof(short));
|
|
|
|
// fl_utf2unicode((unsigned char*)s->name+1, l, (xchar*)b);
|
|
|
|
const char *nm = (const char*)s->name+1;
|
|
|
|
int len = strlen(s->name+1);
|
|
|
|
int l = fl_utf8toUtf16(nm, len, NULL, 0); // Pass NULL to query length required
|
|
|
|
unsigned short *b = (unsigned short*) malloc((l + 1) * sizeof(short));
|
|
|
|
l = fl_utf8toUtf16(nm, len, b, (l+1)); // Now do the conversion
|
|
|
|
b[l] = 0;
|
|
|
|
EnumFontFamiliesW(fl_gc, (WCHAR*)b, (FONTENUMPROCW)EnumSizeCbW, 0);
|
|
|
|
free(b);
|
2001-12-03 21:29:49 +03:00
|
|
|
|
|
|
|
sizep = sizes;
|
|
|
|
return nbSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// End of "$Id$".
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|