1 | // |
---|
2 | // "$Id$" |
---|
3 | // |
---|
4 | // WIN32 font selection routines for the Fast Light Tool Kit (FLTK). |
---|
5 | // |
---|
6 | // Copyright 1998-2005 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. |
---|
12 | // |
---|
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. |
---|
17 | // |
---|
18 | // You should have received a copy of the GNU Library General Public |
---|
19 | // License along with this library; if not, write to the Free Software |
---|
20 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
---|
21 | // USA. |
---|
22 | // |
---|
23 | // Please report all bugs and problems on the following page: |
---|
24 | // |
---|
25 | // http://www.fltk.org/str.php |
---|
26 | // |
---|
27 | |
---|
28 | Fl_FontSize::Fl_FontSize(const char* name, int size) { |
---|
29 | int weight = FW_NORMAL; |
---|
30 | int italic = 0; |
---|
31 | switch (*name++) { |
---|
32 | case 'I': italic = 1; break; |
---|
33 | case 'P': italic = 1; |
---|
34 | case 'B': weight = FW_BOLD; break; |
---|
35 | case ' ': break; |
---|
36 | default: name--; |
---|
37 | } |
---|
38 | fid = CreateFont( |
---|
39 | -size, // negative makes it use "char size" |
---|
40 | 0, // logical average character width |
---|
41 | 0, // angle of escapement |
---|
42 | 0, // base-line orientation angle |
---|
43 | weight, |
---|
44 | italic, |
---|
45 | FALSE, // underline attribute flag |
---|
46 | FALSE, // strikeout attribute flag |
---|
47 | DEFAULT_CHARSET, // character set identifier |
---|
48 | OUT_DEFAULT_PRECIS, // output precision |
---|
49 | CLIP_DEFAULT_PRECIS,// clipping precision |
---|
50 | DEFAULT_QUALITY, // output quality |
---|
51 | DEFAULT_PITCH, // pitch and family |
---|
52 | name // pointer to typeface name string |
---|
53 | ); |
---|
54 | if (!fl_gc) fl_GetDC(0); |
---|
55 | SelectObject(fl_gc, fid); |
---|
56 | GetTextMetrics(fl_gc, &metr); |
---|
57 | // BOOL ret = GetCharWidthFloat(fl_gc, metr.tmFirstChar, metr.tmLastChar, font->width+metr.tmFirstChar); |
---|
58 | // ...would be the right call, but is not implemented into Window95! (WinNT?) |
---|
59 | GetCharWidth(fl_gc, 0, 255, width); |
---|
60 | #if HAVE_GL |
---|
61 | listbase = 0; |
---|
62 | #endif |
---|
63 | minsize = maxsize = size; |
---|
64 | } |
---|
65 | |
---|
66 | Fl_FontSize* fl_fontsize; |
---|
67 | |
---|
68 | Fl_FontSize::~Fl_FontSize() { |
---|
69 | #if HAVE_GL |
---|
70 | // Delete list created by gl_draw(). This is not done by this code |
---|
71 | // as it will link in GL unnecessarily. There should be some kind |
---|
72 | // of "free" routine pointer, or a subclass? |
---|
73 | // if (listbase) { |
---|
74 | // int base = font->min_char_or_byte2; |
---|
75 | // int size = font->max_char_or_byte2-base+1; |
---|
76 | // int base = 0; int size = 256; |
---|
77 | // glDeleteLists(listbase+base,size); |
---|
78 | // } |
---|
79 | #endif |
---|
80 | if (this == fl_fontsize) fl_fontsize = 0; |
---|
81 | DeleteObject(fid); |
---|
82 | } |
---|
83 | |
---|
84 | //////////////////////////////////////////////////////////////// |
---|
85 | |
---|
86 | // WARNING: if you add to this table, you must redefine FL_FREE_FONT |
---|
87 | // in Enumerations.H & recompile!! |
---|
88 | static Fl_Fontdesc built_in_table[] = { |
---|
89 | {" Arial"}, |
---|
90 | {"BArial"}, |
---|
91 | {"IArial"}, |
---|
92 | {"PArial"}, |
---|
93 | {" Courier New"}, |
---|
94 | {"BCourier New"}, |
---|
95 | {"ICourier New"}, |
---|
96 | {"PCourier New"}, |
---|
97 | {" Times New Roman"}, |
---|
98 | {"BTimes New Roman"}, |
---|
99 | {"ITimes New Roman"}, |
---|
100 | {"PTimes New Roman"}, |
---|
101 | {" Symbol"}, |
---|
102 | {" Terminal"}, |
---|
103 | {"BTerminal"}, |
---|
104 | {" Wingdings"}, |
---|
105 | }; |
---|
106 | |
---|
107 | Fl_Fontdesc* fl_fonts = built_in_table; |
---|
108 | |
---|
109 | static Fl_FontSize* find(int fnum, int size) { |
---|
110 | Fl_Fontdesc* s = fl_fonts+fnum; |
---|
111 | if (!s->name) s = fl_fonts; // use 0 if fnum undefined |
---|
112 | Fl_FontSize* f; |
---|
113 | for (f = s->first; f; f = f->next) |
---|
114 | if (f->minsize <= size && f->maxsize >= size) return f; |
---|
115 | f = new Fl_FontSize(s->name, size); |
---|
116 | f->next = s->first; |
---|
117 | s->first = f; |
---|
118 | return f; |
---|
119 | } |
---|
120 | |
---|
121 | //////////////////////////////////////////////////////////////// |
---|
122 | // Public interface: |
---|
123 | |
---|
124 | int fl_font_ = 0; |
---|
125 | int fl_size_ = 0; |
---|
126 | //static HDC font_gc; |
---|
127 | |
---|
128 | void fl_font(int fnum, int size) { |
---|
129 | if (fnum==-1) { // just make sure that we will load a new font next time |
---|
130 | fl_font_ = 0; fl_size_ = 0; |
---|
131 | return; |
---|
132 | } |
---|
133 | if (fnum == fl_font_ && size == fl_size_) return; |
---|
134 | fl_font_ = fnum; fl_size_ = size; |
---|
135 | fl_fontsize = find(fnum, size); |
---|
136 | } |
---|
137 | |
---|
138 | int fl_height() { |
---|
139 | if (fl_fontsize) return (fl_fontsize->metr.tmAscent + fl_fontsize->metr.tmDescent); |
---|
140 | else return -1; |
---|
141 | } |
---|
142 | |
---|
143 | int fl_descent() { |
---|
144 | if (fl_fontsize) return fl_fontsize->metr.tmDescent; |
---|
145 | else return -1; |
---|
146 | } |
---|
147 | |
---|
148 | double fl_width(const char* c, int n) { |
---|
149 | if (!fl_fontsize) return -1.0; |
---|
150 | double w = 0.0; |
---|
151 | while (n--) w += fl_fontsize->width[uchar(*c++)]; |
---|
152 | return w; |
---|
153 | } |
---|
154 | |
---|
155 | double fl_width(uchar c) { |
---|
156 | if (fl_fontsize) return fl_fontsize->width[c]; |
---|
157 | else return -1.0; |
---|
158 | } |
---|
159 | |
---|
160 | void fl_draw(const char* str, int n, int x, int y) { |
---|
161 | COLORREF oldColor = SetTextColor(fl_gc, fl_RGB()); |
---|
162 | if (fl_fontsize) SelectObject(fl_gc, fl_fontsize->fid); |
---|
163 | TextOut(fl_gc, x, y, str, n); |
---|
164 | SetTextColor(fl_gc, oldColor); |
---|
165 | } |
---|
166 | |
---|
167 | void fl_draw(const char* str, int n, float x, float y) { |
---|
168 | fl_draw(str, n, (int)x, (int)y); |
---|
169 | } |
---|
170 | |
---|
171 | // |
---|
172 | // End of "$Id$". |
---|
173 | // |
---|