1 /********************************************************************************
2 *                                                                               *
3 *                               F o n t   O b j e c t                           *
4 *                                                                               *
5 *********************************************************************************
6 * Copyright (C) 1997,2021 by Jeroen van der Zijp.   All Rights Reserved.        *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify          *
9 * it under the terms of the GNU Lesser General Public License as published by   *
10 * the Free Software Foundation; either version 3 of the License, or             *
11 * (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                 *
16 * GNU Lesser General Public License for more details.                           *
17 *                                                                               *
18 * You should have received a copy of the GNU Lesser General Public License      *
19 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
20 ********************************************************************************/
21 #ifndef FXFONT_H
22 #define FXFONT_H
23 
24 #ifndef FXID_H
25 #include "FXId.h"
26 #endif
27 
28 namespace FX {
29 
30 
31 /// Font character set encoding
32 enum FXFontEncoding {
33   FONTENCODING_DEFAULT,         /// Don't care character encoding
34 
35   FONTENCODING_ISO_8859_1   = 1,        /// West European (Latin1)
36   FONTENCODING_ISO_8859_2   = 2,        /// Central and East European (Latin2)
37   FONTENCODING_ISO_8859_3   = 3,        /// Esperanto (Latin3)
38   FONTENCODING_ISO_8859_4   = 4,
39   FONTENCODING_ISO_8859_5   = 5,        /// Cyrillic (almost obsolete)
40   FONTENCODING_ISO_8859_6   = 6,        /// Arabic
41   FONTENCODING_ISO_8859_7   = 7,        /// Greek
42   FONTENCODING_ISO_8859_8   = 8,        /// Hebrew
43   FONTENCODING_ISO_8859_9   = 9,        /// Turkish (Latin5)
44   FONTENCODING_ISO_8859_10  = 10,
45   FONTENCODING_ISO_8859_11  = 11,       /// Thai
46   FONTENCODING_ISO_8859_13  = 13,       /// Baltic
47   FONTENCODING_ISO_8859_14  = 14,
48   FONTENCODING_ISO_8859_15  = 15,
49   FONTENCODING_ISO_8859_16  = 16,
50   FONTENCODING_KOI8         = 17,
51   FONTENCODING_KOI8_R       = 18,       /// Russian
52   FONTENCODING_KOI8_U       = 19,       /// Ukrainian
53   FONTENCODING_KOI8_UNIFIED = 20,
54 
55   FONTENCODING_CP437        = 437,      /// IBM-PC code page
56   FONTENCODING_CP850        = 850,      /// IBMPC Multilingual
57   FONTENCODING_CP851        = 851,      /// IBM-PC Greek
58   FONTENCODING_CP852        = 852,      /// IBM-PC Latin2
59   FONTENCODING_CP855        = 855,      /// IBM-PC Cyrillic
60   FONTENCODING_CP856        = 856,      /// IBM-PC Hebrew
61   FONTENCODING_CP857        = 857,      /// IBM-PC Turkish
62   FONTENCODING_CP860        = 860,      /// IBM-PC Portugese
63   FONTENCODING_CP861        = 861,      /// IBM-PC Iceland
64   FONTENCODING_CP862        = 862,      /// IBM-PC Israel
65   FONTENCODING_CP863        = 863,      /// IBM-PC Canadian/French
66   FONTENCODING_CP864        = 864,      /// IBM-PC Arabic
67   FONTENCODING_CP865        = 865,      /// IBM-PC Nordic
68   FONTENCODING_CP866        = 866,      /// IBM-PC Cyrillic #2
69   FONTENCODING_CP869        = 869,      /// IBM-PC Greek #2
70   FONTENCODING_CP870        = 870,      /// Latin-2 Multilingual
71 
72   FONTENCODING_CP1250       = 1250,     /// Windows Central European
73   FONTENCODING_CP1251       = 1251,     /// Windows Russian
74   FONTENCODING_CP1252       = 1252,     /// Windows Latin1
75   FONTENCODING_CP1253       = 1253,     /// Windows Greek
76   FONTENCODING_CP1254       = 1254,     /// Windows Turkish
77   FONTENCODING_CP1255       = 1255,     /// Windows Hebrew
78   FONTENCODING_CP1256       = 1256,     /// Windows Arabic
79   FONTENCODING_CP1257       = 1257,     /// Windows Baltic
80   FONTENCODING_CP1258       = 1258,     /// Windows Vietnam
81   FONTENCODING_CP874        = 874,      /// Windows Thai
82 
83   FONTENCODING_UNICODE      = 9999,
84 
85   FONTENCODING_LATIN1       = FONTENCODING_ISO_8859_1,   /// Latin 1 (West European)
86   FONTENCODING_LATIN2       = FONTENCODING_ISO_8859_2,   /// Latin 2 (East European)
87   FONTENCODING_LATIN3       = FONTENCODING_ISO_8859_3,   /// Latin 3 (South European)
88   FONTENCODING_LATIN4       = FONTENCODING_ISO_8859_4,   /// Latin 4 (North European)
89   FONTENCODING_LATIN5       = FONTENCODING_ISO_8859_9,   /// Latin 5 (Turkish)
90   FONTENCODING_LATIN6       = FONTENCODING_ISO_8859_10,  /// Latin 6 (Nordic)
91   FONTENCODING_LATIN7       = FONTENCODING_ISO_8859_13,  /// Latin 7 (Baltic Rim)
92   FONTENCODING_LATIN8       = FONTENCODING_ISO_8859_14,  /// Latin 8 (Celtic)
93   FONTENCODING_LATIN9       = FONTENCODING_ISO_8859_15,  /// Latin 9 AKA Latin 0
94   FONTENCODING_LATIN10      = FONTENCODING_ISO_8859_16,  /// Latin 10
95 
96   FONTENCODING_USASCII      = FONTENCODING_ISO_8859_1,   /// Latin 1
97   FONTENCODING_WESTEUROPE   = FONTENCODING_ISO_8859_1,   /// Latin 1 (West European)
98   FONTENCODING_EASTEUROPE   = FONTENCODING_ISO_8859_2,   /// Latin 2 (East European)
99   FONTENCODING_SOUTHEUROPE  = FONTENCODING_ISO_8859_3,   /// Latin 3 (South European)
100   FONTENCODING_NORTHEUROPE  = FONTENCODING_ISO_8859_4,   /// Latin 4 (North European)
101   FONTENCODING_CYRILLIC     = FONTENCODING_ISO_8859_5,   /// Cyrillic
102   FONTENCODING_RUSSIAN      = FONTENCODING_KOI8,         /// Cyrillic
103   FONTENCODING_ARABIC       = FONTENCODING_ISO_8859_6,   /// Arabic
104   FONTENCODING_GREEK        = FONTENCODING_ISO_8859_7,   /// Greek
105   FONTENCODING_HEBREW       = FONTENCODING_ISO_8859_8,   /// Hebrew
106   FONTENCODING_TURKISH      = FONTENCODING_ISO_8859_9,   /// Latin 5 (Turkish)
107   FONTENCODING_NORDIC       = FONTENCODING_ISO_8859_10,  /// Latin 6 (Nordic)
108   FONTENCODING_THAI         = FONTENCODING_ISO_8859_11,  /// Thai
109   FONTENCODING_BALTIC       = FONTENCODING_ISO_8859_13,  /// Latin 7 (Baltic Rim)
110   FONTENCODING_CELTIC       = FONTENCODING_ISO_8859_14   /// Latin 8 (Celtic)
111   };
112 
113 
114 /// Font description
115 struct FXFontDesc {
116   FXchar          face[116];                /// Face name
117   FXushort        size;                     /// Size in deci-points (one point is 1/72 inch)
118   FXushort        weight;                   /// Weight [light, normal, bold, ...]
119   FXushort        slant;                    /// Slant [normal, italic, oblique, ...]
120   FXushort        setwidth;                 /// Set width [normal, condensed, expanded, ...]
121   FXushort        encoding;                 /// Encoding of character set
122   FXushort        flags;                    /// Flags
123 
124   /// Set font description from a string
125   void setFont(const FXString& string);
126 
127   /// Get string of font description
128   FXString getFont() const;
129   };
130 
131 
132 class FXDC;
133 class FXDCWindow;
134 
135 
136 /// Font class
137 class FXAPI FXFont : public FXId {
138   friend class FXDCWindow;
139   FXDECLARE(FXFont)
140 protected:
141   FXString  wantedName;         // Desired font font name
142   FXString  actualName;         // Matched font font name
143   FXushort  wantedSize;         // Font size (points*10)
144   FXushort  actualSize;         // Actual size that was matched
145   FXushort  wantedWeight;       // Font weight
146   FXushort  actualWeight;       // Font weight
147   FXushort  wantedSlant;        // Font slant
148   FXushort  actualSlant;        // Font slant
149   FXushort  wantedSetwidth;     // Relative setwidth
150   FXushort  actualSetwidth;     // Relative setwidth
151   FXushort  wantedEncoding;     // Character set encoding
152   FXushort  actualEncoding;     // Character set encoding
153   FXushort  hints;              // Matching hint flags
154   FXushort  flags;              // Actual flags
155   FXshort   angle;              // Angle
156   void     *font;               // Info about the font
157 private:
158 #ifdef WIN32
159   FXID      dc;
160 #endif
161 protected:
162   FXFont();
163   void* match(const FXString& wantfamily,const FXString& wantforge,FXuint wantsize,FXuint wantweight,FXuint wantslant,FXuint wantsetwidth,FXuint wantencoding,FXuint wanthints,FXint res);
164 private:
165   FXFont(const FXFont&);
166   FXFont &operator=(const FXFont&);
167 public:
168 
169   /// Font pitch hints
170   enum {
171     Fixed          = 1,         /// Fixed pitch, mono-spaced
172     Variable       = 2          /// Variable pitch, proportional spacing
173     };
174 
175   /// Font style hints
176   enum {
177     Decorative     = 4,         /// Fancy fonts
178     Modern         = 8,         /// Monospace typewriter font
179     Roman          = 16,        /// Variable width times-like font, serif
180     Script         = 32,        /// Script or cursive
181     Swiss          = 64,        /// Helvetica/swiss type font, sans-serif
182     System         = 128,       /// System font
183     X11            = 256,       /// Raw X11 font string
184     Scalable       = 512,       /// Scalable fonts
185     Polymorphic    = 1024,      /// Polymorphic fonts, e.g. parametric weight, slant, etc.
186     Rotatable      = 2048       /// Rotatable fonts
187     };
188 
189   /// Font slant options
190   enum {
191     ReverseOblique = 1,         /// Reversed oblique
192     ReverseItalic  = 2,         /// Reversed italic
193     Straight       = 5,         /// Straight, not slanted
194     Italic         = 8,         /// Italics
195     Oblique        = 9          /// Oblique slant
196     };
197 
198   /// Font weight options
199   enum {
200     Thin           = 10,        /// Thin
201     ExtraLight     = 20,        /// Extra light
202     Light          = 30,        /// Light
203     Normal         = 40,        /// Normal or regular weight
204     Medium         = 50,        /// Medium bold face
205     DemiBold       = 60,        /// Demi bold face
206     Bold           = 70,        /// Bold face
207     ExtraBold      = 80,        /// Extra
208     Black          = 90         /// Black
209     };
210 
211   /// Condensed or expanded options
212   enum {
213     UltraCondensed = 50,        /// Ultra condensed printing
214     ExtraCondensed = 63,        /// Extra condensed
215     Condensed      = 75,        /// Condensed
216     SemiCondensed  = 87,        /// Semi-condensed
217     NonExpanded    = 100,       /// Regular printing
218     SemiExpanded   = 113,       /// Semi expanded
219     Expanded       = 125,       /// Expanded
220     ExtraExpanded  = 150,       /// Extra expanded
221     UltraExpanded  = 200        /// Ultra expanded
222     };
223 
224 public:
225 
226   /**
227   * Construct a font with given font description of the form:
228   *
229   *     fontname [ "[" foundry "]" ] ["," size ["," weight ["," slant ["," setwidth ["," encoding ["," hints]]]]]]
230   *
231   * For example:
232   *
233   *     "helvetica [bitstream],120,bold,italic,normal,iso8859-1,0"
234   *
235   * Typically, at least the font name, and size must be given for
236   * normal font matching.  As a special case, raw X11 fonts can also be
237   * passed, for example:
238   *
239   *     "9x15bold"
240   *
241   * Note: use of the raw X11 fonts is stronly discouraged.
242   */
243   FXFont(FXApp* a,const FXString& string);
244 
245   /**
246   * Construct a font with given name, size in points, weight, slant, character set
247   * encoding, setwidth, and hints.
248   * The font name may be comprised of a family name and optional foundry name enclosed in
249   * square brackets, for example, "helvetica [bitstream]".
250   */
251   FXFont(FXApp* a,const FXString& face,FXuint size,FXuint weight=FXFont::Normal,FXuint slant=FXFont::Straight,FXuint encoding=FONTENCODING_DEFAULT,FXuint setwidth=FXFont::NonExpanded,FXuint h=0);
252 
253   /// Construct font from font description
254   FXFont(FXApp* a,const FXFontDesc& fontdesc);
255 
256   /// Create the font
257   virtual void create();
258 
259   /// Detach the font
260   virtual void detach();
261 
262   /// Destroy the font
263   virtual void destroy();
264 
265   /// Return family part of name
266   FXString getFamily() const;
267 
268   /// Return foundry part of name
269   FXString getFoundry() const;
270 
271   /// Get font family name
getName()272   const FXString& getName() const { return wantedName; }
273 
274   /// Get actual family name
getActualName()275   const FXString& getActualName() const { return actualName; }
276 
277   /// Get size in deci-points
getSize()278   FXuint getSize() const { return wantedSize; }
279 
280   /// Get actual size in deci-points
getActualSize()281   FXuint getActualSize() const { return actualSize; }
282 
283   /// Get font weight
getWeight()284   FXuint getWeight() const { return wantedWeight; }
285 
286   /// Get actual font weight
getActualWeight()287   FXuint getActualWeight() const { return actualWeight; }
288 
289   /// Get slant
getSlant()290   FXuint getSlant() const { return wantedSlant; }
291 
292   /// Get actual slant
getActualSlant()293   FXuint getActualSlant() const { return actualSlant; }
294 
295   /// Get character set encoding
getEncoding()296   FXuint getEncoding() const { return wantedEncoding; }
297 
298   /// Get actual encoding
getActualEncoding()299   FXuint getActualEncoding() const { return actualEncoding; }
300 
301   /// Get setwidth
getSetWidth()302   FXuint getSetWidth() const { return wantedSetwidth; }
303 
304   /// Get actual setwidth
getActualSetWidth()305   FXuint getActualSetWidth() const { return actualSetwidth; }
306 
307   /// Get hints
getHints()308   FXuint getHints() const { return hints; }
309 
310   /// Get flags
getFlags()311   FXuint getFlags() const { return flags; }
312 
313   /// Change font description
314   virtual void setFontDesc(const FXFontDesc& fontdesc);
315 
316   /// Get font description
317   FXFontDesc getFontDesc() const;
318 
319   /// Get actual font description
320   FXFontDesc getActualFontDesc() const;
321 
322   /// Return angle
getAngle()323   FXint getAngle() const { return angle; }
324 
325   /// Set to new angle, in degrees*64 relative to positive x axis
326   virtual void setAngle(FXint ang);
327 
328   /**
329   * Return the font description as a string suitable for
330   * parsing with setFont(), see above.
331   */
332   FXString getFont() const;
333 
334   /**
335   * Change the font to the specified font description string.
336   */
337   virtual void setFont(const FXString& string);
338 
339   /// Find out if the font is monotype or proportional
340   virtual FXbool isFontMono() const;
341 
342   /// See if font has glyph for ch
343   virtual FXbool hasChar(FXwchar ch) const;
344 
345   /// Get first character glyph in font
346   virtual FXwchar getMinChar() const;
347 
348   /// Get last character glyph in font
349   virtual FXwchar getMaxChar() const;
350 
351   /// Left bearing
352   virtual FXint leftBearing(FXwchar ch) const;
353 
354   /// Right bearing
355   virtual FXint rightBearing(FXwchar ch) const;
356 
357   /// Width of widest character in font
358   virtual FXint getFontWidth() const;
359 
360   /// Height of highest character in font
361   virtual FXint getFontHeight() const;
362 
363   /// Ascent from baseline
364   virtual FXint getFontAscent() const;
365 
366   /// Descent from baseline
367   virtual FXint getFontDescent() const;
368 
369   /// Get font leading [that is lead-ing as in Pb!]
370   virtual FXint getFontLeading() const;
371 
372   /// Get font line spacing
373   virtual FXint getFontSpacing() const;
374 
375   /// Calculate width of single wide character in this font
376   virtual FXint getCharWidth(const FXwchar ch) const;
377 
378   /// Calculate width of given text in this font
379   virtual FXint getTextWidth(const FXString& string) const;
380 
381   /// Calculate width of given text in this font
382   virtual FXint getTextWidth(const FXchar* string,FXuint length) const;
383 
384   /// Calculate height of given text in this font
385   virtual FXint getTextHeight(const FXString& string) const;
386 
387   /// Calculate height of given text in this font
388   virtual FXint getTextHeight(const FXchar *string,FXuint length) const;
389 
390   /// Convert style hints to string and vice versa
391   static FXuint styleFromString(const FXString& str);
392   static FXString stringFromStyle(FXuint style);
393 
394   /// Convert slant to string and vice versa
395   static FXuint slantFromString(const FXString& str);
396   static FXString stringFromSlant(FXuint slant);
397 
398   /// Convert weight to string and vice versa
399   static FXuint weightFromString(const FXString& str);
400   static FXString stringFromWeight(FXuint weight);
401 
402   /// Convert setwidth to string and vice versa
403   static FXuint setWidthFromString(const FXString& str);
404   static FXString stringFromSetWidth(FXuint setwidth);
405 
406   /// Convert encoding to string and vice versa
407   static FXuint encodingFromString(const FXString& str);
408   static FXString stringFromEncoding(FXuint encoding);
409 
410   /**
411   * List all fonts matching hints. If listFonts() returns true then
412   * fonts points to a newly-allocated array of length numfonts. It
413   * is the caller's responsibility to free this array using freeElms().
414   */
415   static FXbool listFonts(FXFontDesc*& fonts,FXuint& numfonts,const FXString& face,FXuint wt=0,FXuint sl=0,FXuint sw=0,FXuint en=0,FXuint h=0);
416 
417   /// Save font data into stream
418   virtual void save(FXStream& store) const;
419 
420   /// Load font data from stream
421   virtual void load(FXStream& store);
422 
423   /// Destroy font
424   virtual ~FXFont();
425   };
426 
427 
428 }
429 
430 #endif
431