X11workbench Toolkit  1.0
font_helper.h
Go to the documentation of this file.
1 // __ _ _ _ _ //
3 // / _| ___ _ __ | |_ | |__ ___ | | _ __ ___ _ __ | |__ //
4 // | |_ / _ \ | '_ \ | __| | '_ \ / _ \| || '_ \ / _ \| '__|| '_ \ //
5 // | _|| (_) || | | || |_ | | | || __/| || |_) || __/| | _ | | | | //
6 // |_| \___/ |_| |_| \__|_____|_| |_| \___||_|| .__/ \___||_|(_)|_| |_| //
7 // |_____| |_| //
8 // basic font enumeration and selection //
9 // //
11 
12 /*****************************************************************************
13 
14  X11workbench - X11 programmer's 'work bench' application and toolkit
15  Copyright (c) 2010-2019 by Bob Frazier (aka 'Big Bad Bombastic Bob')
16 
17 
18  DISCLAIMER: The X11workbench application and toolkit software are supplied
19  'as-is', with no warranties, either implied or explicit.
20  Any claims to alleged functionality or features should be
21  considered 'preliminary', and might not function as advertised.
22 
23  MIT-like license:
24 
25  There is no restriction as to what you can do with this software, so long
26  as you include the above copyright notice and DISCLAIMER for any distributed
27  work that is equal to or derived from this one, along with this paragraph
28  that explains the terms of the license if the source is also being made
29  available. A "derived work" describes a work that uses a significant portion
30  of the source files or algorithms that are included with this one.
31  Specifically excluded from this are files that were generated by the software,
32  or anything that is included with the software that is part of another package
33  (such as files that were created or added during the 'configure' process).
34  Specifically included is the use of part or all of any of the X11 workbench
35  toolkit source or header files in your distributed application. If you do not
36  ship the source, the above copyright statement is still required to be placed
37  in a reasonably prominent place, such as documentation, splash screens, and/or
38  'about the application' dialog boxes.
39 
40  Use and distribution are in accordance with GPL, LGPL, and/or the above
41  MIT-like license. See COPYING and README files for more information.
42 
43 
44  Additional information at http://sourceforge.net/projects/X11workbench
45 
46 ******************************************************************************/
47 
66 #ifndef _FONT_HELPER_H_INCLUDED_
67 #define _FONT_HELPER_H_INCLUDED_
68 
69 #ifdef __cplusplus
70 extern "C" {
71 #endif // __cplusplus
72 
73 
74 #ifdef X11WORKBENCH_TOOLKIT_HAVE_XFT
75 #include <X11/Xft/Xft.h> /* required header for Xft definitions */
76 #endif // X11WORKBENCH_TOOLKIT_HAVE_XFT
77 
78 #ifndef _WINDOW_HELPER_H_INCLUDED_
79 typedef struct s_WB_EXTENT WB_EXTENT; // forward reference on WB_EXTENT structure for WBTextExtent()
80 #endif // _WINDOW_HELPER_H_INCLUDED_
81 
82 
86 // UTF-8 HANDLING
87 // X11 has XFree86 extensions with 'Xutf8' versions of a lot of things
88 // which is only applicable with the following
89 //#ifdef X_HAVE_UTF8_STRING
90 // I use the utf8 version
91 // etc.
92 //
93 // Unfortunately many things that I use don't have that available
94 // like XTextWidth for one (maybe I could use Xutf8TextExtents?)
95 // Using Xmb and Xwc equivalent methods should work on Interix
96 //
97 #if defined(X_HAVE_UTF8_STRING) || defined(__DOXYGEN__)
98 
99 #define WB_TEXT_EXTENTS Xutf8TextExtents
100 #define WB_TEXT_ESCAPEMENT Xutf8TextEscapement
101 #define WB_DRAW_STRING Xutf8DrawString
102 #define WB_DRAW_TEXT Xutf8DrawText
103 #define WB_LOOKUP_STRING Xutf8LookupString
105 #else // X_HAVE_UTF8_STRING
106 
107 #warning UTF8 not supported, using 'MB' equivalents
108 
109 #define WB_TEXT_EXTENTS XmbTextExtents
110 #define WB_TEXT_ESCAPEMENT XmbTextEscapement
111 #define WB_DRAW_STRING XmbDrawString
112 #define WB_DRAW_TEXT XmbDrawText
113 #define WB_LOOKUP_STRING XmbLookupString
115 #endif // X_HAVE_UTF8_STRING
116 
117 
152 typedef struct WBFont
153 {
154  struct WBFont *pShareNext;
155  struct WBFont *pSharePrev;
156  int iAscent;
157  int iDescent;
158  int iHeight;
161  XCharStruct max_bounds;
162  Display *pDisplay;
163 #ifdef X11WORKBENCH_TOOLKIT_HAVE_XFT
164  XftFont *pxftFont;
165  XftFontInfo *pxftFontInfo;
166 #endif // X11WORKBENCH_TOOLKIT_HAVE_XFT
167  XFontStruct *pFontStruct;
168  XFontSet fsFont;
169 } * WB_FONT; // WB_FONT will always point to this struct, allocated using WBAlloc
170 
185 typedef const struct WBFont *WB_FONTC;
186 
187 
188 // -----------------------------------------
189 // FONT INTERNAL-ONLY AND DEBUGGING FEATURES
190 // -----------------------------------------
191 
214 typedef struct WBFontInfo
215 {
216  char *szFoundry, *szFamily;
217  int iWeight, iSlant, iWidth;
218  char *szAdStyle;
219  int iPixelSize, iPointSize, iResX, iResY, iSpacing, iAvgWidth;
220  char *szRegistry, *szEncoding;
221  char data[8];
222 } WB_FONT_INFO;
223 
224 
225 #ifndef NO_DEBUG
226 
229 static void WBDumpFontStruct(const XFontStruct *pFont);
230 
231 
235 static void WBDumpMatchingFontNames(Display *pDisplay, const char *szFontName);
236 
237 
241 static void WBDumpFontSet(Display *pDisplay, XFontSet fontSet);
242 #endif // NO_DEBUG
243 
244 
248 void __internal_font_helper_init(void);
249 
250 
254 void __internal_font_helper_exit(void);
255 
256 
268 int WBFontEnableAntiAlias(void);
269 
270 
282 void WBFontSetEnableAntiAlias(int bEnable);
283 
295 void WBFreeFont(Display *pDisplay, WB_FONT pFont);
296 
311 WB_FONT WBCopyFont(Display *pDisplay, WB_FONTC pFont);
312 
335 WB_FONT WBCopyModifyFont(Display *pDisplay, WB_FONTC pOriginal,
336  int iFontSize, int iFlags);
337 
360 WB_FONT WBLoadFont(Display *pDisplay, const char *szFontName,
361  int iFontSize, int iFlags);
362 
372 int WBFontAvgCharWidth(WB_FONTC pFont);
373 
374 
384 int WBFontMaxCharWidth(WB_FONTC pFont);
385 
386 
396 int WBFontDescent(WB_FONTC pFont);
397 
398 
408 int WBFontAscent(WB_FONTC pFont);
409 
410 
420 int WBFontHeight(WB_FONTC pFont);
421 
422 
433 XCharStruct WBFontMaxBounds(WB_FONTC pFont);
434 
435 
449 int WBTextWidth(WB_FONTC pFont, const char *szText, int cbText);
450 
451 
467 void WBTextExtent(WB_FONTC pFont, const char *szText, int cbText, WB_EXTENT *pExtent);
468 
469 
480 {
486 
493 
499 
500  // if the font size is > 0 then these bits apply
506 
515 
521 
527 
528  // reserved - font registry and encoding (installed X11 fonts typically include most if not all of these)
531  WBFontFlag_REG_ASCII = 0x300000,
533  WBFontFlag_REG_MISC = 0x500000,
535  WBFontFlag_REG_ADOBE = 0x700000,
544  WBFontFlag_REG_MASK = 0xf00000,
545 
546  // TODO: flags for 'encoding' ?
547 
548  WBFontFlag_WHATEVER = 0 // don't force anything (zero, default)
549 };
550 
551 
552 
554 // //
555 // _ _____ _ ____ _ //
556 // | | ___ __ _ __ _ ___ _ _ | ___|___ _ __ | |_ / ___| _ _ _ __ _ __ ___ _ __ | |_ //
557 // | | / _ \ / _` | / _` | / __|| | | | | |_ / _ \ | '_ \ | __| \___ \ | | | || '_ \ | '_ \ / _ \ | '__|| __| //
558 // | |___| __/| (_| || (_| || (__ | |_| | | _|| (_) || | | || |_ ___) || |_| || |_) || |_) || (_) || | | |_ //
559 // |_____|\___| \__, | \__,_| \___| \__, | |_| \___/ |_| |_| \__| |____/ \__,_|| .__/ | .__/ \___/ |_| \__| //
560 // |___/ |___/ |_| |_| //
561 // //
563 
564 
587 XFontStruct *WBLoadFontX(Display *pDisplay, const char *szFontName,
588  int iFontSize, int iFlags);
589 
603 XFontStruct *WBCopyFontX(XFontStruct *pFont);
604 
627 XFontStruct *WBLoadModifyFontX(Display *pDisplay, const XFontStruct *pOriginal,
628  int iFontSize, int iFlags);
629 
652 XFontSet WBCopyModifyFontSet(Display *pDisplay, XFontSet fsOrig, int iFontSize, int iFlags);
653 
654 
665 int WBFontAvgCharWidthX(Display *pDisplay, const XFontStruct *pFont);
666 
667 
679 int WBFontSetDescent(Display *pDisplay, XFontSet fontSet);
680 
681 
693 int WBFontSetAscent(Display *pDisplay, XFontSet fontSet);
694 
695 
706 int WBFontSetHeight(Display *pDisplay, XFontSet fontSet);
707 
708 
720 int WBFontSetAvgCharWidth(Display *pDisplay, XFontSet fontSet);
721 
722 
734 XCharStruct WBFontSetMaxBounds(Display *pDisplay, XFontSet fontSet);
735 
736 
753 XFontSet WBFontSetFromFont(Display *pDisplay, const XFontStruct *pFont);
754 
755 
769 XFontStruct * WBFontFromFontSet(Display *pDisplay, XFontSet fontSet);
770 
771 
792 XFontSet WBFontSetFromFontSingle(Display *pDisplay, const XFontStruct *pFont);
793 
794 
808 int WBTextWidthX(XFontSet fontSet, const char *szText, int cbText);
809 
810 
826 void WBTextExtentX(XFontSet fontSet, const char *szText, int cbText, WB_EXTENT *pExtent);
827 
828 
829 #ifdef NO_DEBUG
830 #define WBDumpFontInfo(X) /* do nothing macro */
831 #else // NO_DEBUG
832 
844 void WBDumpFontInfo(const char *pSpec); // debugging function - dumps a list of fonts and info
845 
846 #endif // NO_DEBUG
847 
848 
849 
850 #ifdef __cplusplus
851 };
852 #endif // __cplusplus
853 
854 
855 #endif // _FONT_HELPER_H_INCLUDED_
856 
font size is in 'twips' (when font size > 0)
Definition: font_helper.h:503
int iMaxCharWidth
RESERVED - cached max character width (-1 if not valid). do not access this directly.
Definition: font_helper.h:160
use a font specific registry (mutually exclusive)
Definition: font_helper.h:532
font size is in 'points' (when font size > 0)
Definition: font_helper.h:502
void WBFontSetEnableAntiAlias(int bEnable)
returns non-zero value if certain fonts should be anti-aliased when rendered
Definition: font_helper.c:162
void WBFreeFont(Display *pDisplay, WB_FONT pFont)
free a WB_FONT that was created using one of the WBFont APIs
Definition: font_helper.c:250
reserved (style)
Definition: font_helper.h:489
void __internal_font_helper_exit(void)
un-initialization for font helper - call once at end of program (WBExit() does this for you)
Definition: font_helper.c:145
int WBFontHeight(WB_FONTC pFont)
Get the maximum character height from a WB_FONT.
Definition: font_helper.c:546
int WBFontAscent(WB_FONTC pFont)
Get the maximum character ascent from a WB_FONT.
Definition: font_helper.c:495
XFontStruct * WBLoadModifyFontX(Display *pDisplay, const XFontStruct *pOriginal, int iFontSize, int iFlags)
load and modify a font according to the specified size and flags
Definition: font_legacy.c:1269
bold font weight (mutually exclusive0
Definition: font_helper.h:511
struct WBFont * pShareNext
RESERVED - next in a chain of WB_FONTs that share the same "stuff", NULL if this is a stand-alone cop...
Definition: font_helper.h:154
condensed (alternate to 'fixed')
Definition: font_helper.h:483
int WBFontAvgCharWidth(WB_FONTC pFont)
Get the average character width for a font.
Definition: font_helper.c:343
reserved reg flag (reserved for future use)
Definition: font_helper.h:539
reserved reg flag (reserved for future use)
Definition: font_helper.h:543
WB_FONT WBCopyFont(Display *pDisplay, WB_FONTC pFont)
make a copy of an existing font (best when assigning to a window)
Definition: font_helper.c:168
reserved flags for font size > 0
Definition: font_helper.h:504
'oblique' slant (mutually exclusive)
Definition: font_helper.h:517
XFontSet WBFontSetFromFont(Display *pDisplay, const XFontStruct *pFont)
Creates an 'XFontSet' from an XFontStruct for a given display.
Definition: font_legacy.c:1579
semicondensed width (mutually exclusive)
Definition: font_helper.h:523
WB_FONT WBCopyModifyFont(Display *pDisplay, WB_FONTC pOriginal, int iFontSize, int iFlags)
load and modify a font according to the specified size and flags
Definition: font_helper.c:660
reserved reg flag (reserved for future use)
Definition: font_helper.h:541
'Any' pitch (default, zero)
Definition: font_helper.h:484
int iDescent
RESERVED - cached font descent (-1 if not valid). do not access this directly.
Definition: font_helper.h:157
use ADOBE registry (mutually exclusive)
Definition: font_helper.h:534
int WBFontSetAvgCharWidth(Display *pDisplay, XFontSet fontSet)
Get the average character width for a font set.
Definition: font_legacy.c:1146
reserved reg flag (reserved for future use)
Definition: font_helper.h:542
font weight bit mask
Definition: font_helper.h:514
reserved reg flag (reserved for future use)
Definition: font_helper.h:536
struct WBFont * WB_FONT
An allocated structure containing XFontStruct, XFontInfo, and XftFont [as applicable] for a specified...
'foundry mask'
Definition: font_helper.h:498
reserved (style)
Definition: font_helper.h:490
force fixed pitch
Definition: font_helper.h:481
WB_FONT WBLoadFont(Display *pDisplay, const char *szFontName, int iFontSize, int iFlags)
load a WB_FONT font object based on a font name, size, and font flags
Definition: font_helper.c:291
use ADOBE registry - a lot of fonts use this (mutually exclusive)
Definition: font_helper.h:535
medium font weight (mutually exclusive0
Definition: font_helper.h:509
void WBTextExtentX(XFontSet fontSet, const char *szText, int cbText, WB_EXTENT *pExtent)
Obtain the pixel extent of specified text for a specified XFontSet.
Definition: font_legacy.c:1941
int WBFontAvgCharWidthX(Display *pDisplay, const XFontStruct *pFont)
Get the average character width for a font.
Definition: font_legacy.c:912
Display * pDisplay
The Display pointer associated with this font (to be used internally)
Definition: font_helper.h:162
int WBFontEnableAntiAlias(void)
returns non-zero value if certain fonts should be anti-aliased when rendered
Definition: font_helper.c:157
XFontStruct * WBCopyFontX(XFontStruct *pFont)
make a copy of an existing font (best when assigning to a window)
Definition: font_legacy.c:106
XFontSet WBFontSetFromFontSingle(Display *pDisplay, const XFontStruct *pFont)
Creates an 'XFontSet' from an XFontStruct for a given display, with only a single font in the set.
Definition: font_legacy.c:1759
'regular' slant (mutually exclusive)
Definition: font_helper.h:516
regular font weight (mutually exclusive0
Definition: font_helper.h:508
force freetype
Definition: font_helper.h:495
force raster (xfree86?)
Definition: font_helper.h:494
font size is in 'pixels' (when font size > 0)
Definition: font_helper.h:501
reserved reg flag (reserved for future use)
Definition: font_helper.h:538
reserved font weight (6) (not currently in use)
Definition: font_helper.h:513
static void WBDumpFontSet(Display *pDisplay, XFontSet fontSet)
debug function to dump font set members
XCharStruct max_bounds
RESERVED - cached max bounds (all 0's if not valid). do not access this directly.
Definition: font_helper.h:161
use 'MISC' registry (mutually exclusive)
Definition: font_helper.h:533
int WBFontSetHeight(Display *pDisplay, XFontSet fontSet)
Get the maximum character height from a font set.
Definition: font_legacy.c:1097
normal width (mutually exclusive)
Definition: font_helper.h:522
demi-bold font weight (mutually exclusive0
Definition: font_helper.h:510
static void WBDumpMatchingFontNames(Display *pDisplay, const char *szFontName)
debug function to dump matching font names
XCharStruct WBFontMaxBounds(WB_FONTC pFont)
Get a 'maximized' copy of 'max_bounds' (applicable to all font faces in the WB_FONT)
Definition: font_helper.c:592
int WBFontSetDescent(Display *pDisplay, XFontSet fontSet)
Get the maximum character descent from a font set.
Definition: font_legacy.c:1013
Any font weight specification (mutually exclusive)
Definition: font_helper.h:507
'size mask' for font size > 0
Definition: font_helper.h:505
force sans-serif
Definition: font_helper.h:487
'italic' slant (mutually exclusive)
Definition: font_helper.h:518
WBFontFlags
Font 'flag' enumeration.
Definition: font_helper.h:479
force serif (i.e. 'not sans')
Definition: font_helper.h:488
XFontSet fsFont
legacy 'XFontSet' for X11 raster fonts (UTF-8)
Definition: font_helper.h:168
int iAvgCharWidth
RESERVED - cached average character width (-1 if not valid). do not access this directly.
Definition: font_helper.h:159
'Any' foundry (default, zero)
Definition: font_helper.h:497
int WBTextWidthX(XFontSet fontSet, const char *szText, int cbText)
Obtain the pixel width of specified text for a specified XFontSet.
Definition: font_legacy.c:1906
struct WBFontInfo WB_FONT_INFO
Internal structure, caching font information (mostly for legacy font support)
use ISO646-1991 registry (mutually exclusive)
Definition: font_helper.h:530
struct WBFont * pSharePrev
RESERVED - previous in a chain of WB_FONTs that share the same "stuff", NULL if this is a stand-alone...
Definition: font_helper.h:155
reserved (foundry)
Definition: font_helper.h:496
internal wrapper struct for 'extent' definition
XFontStruct * pFontStruct
legacy 'XFontStruct' for X11 raster fonts
Definition: font_helper.h:167
int WBFontDescent(WB_FONTC pFont)
Get the maximum character descent from a WB_FONT.
Definition: font_helper.c:443
XFontStruct * WBFontFromFontSet(Display *pDisplay, XFontSet fontSet)
Creates an 'XFontStruct' from the first font assigned to a Font Set.
Definition: font_legacy.c:1866
int WBTextWidth(WB_FONTC pFont, const char *szText, int cbText)
Obtain the pixel width of specified text for a specified WB_FONT.
Definition: font_helper.c:747
XFontSet WBCopyModifyFontSet(Display *pDisplay, XFontSet fsOrig, int iFontSize, int iFlags)
copy and modify a font set according to the specified size and flags
Definition: font_legacy.c:1359
int WBFontMaxCharWidth(WB_FONTC pFont)
Get the maximum character width for a font.
Definition: font_helper.c:394
int iAscent
RESERVED - cached font ascent (-1 if not valid). do not access this directly.
Definition: font_helper.h:156
Internal structure, caching font information (mostly for legacy font support)
Definition: font_helper.h:214
void __internal_font_helper_init(void)
initialization for font helper - call once at start of program (WBInit() does this for you)
Definition: font_helper.c:96
static void WBDumpFontStruct(const XFontStruct *pFont)
debug function to dump font struct members
'default' slant (zero; mutually exclusive; may return an italic or oblique font)
Definition: font_helper.h:519
void WBDumpFontInfo(const char *pSpec)
Dump debug information about fonts according to pSpec.
Definition: font_legacy.c:2212
An allocated structure containing XFontStruct, XFontInfo, and XftFont [as applicable] for a specified...
Definition: font_helper.h:152
use ASCII registry (mutually exclusive)
Definition: font_helper.h:531
'Any' width (zero; mutually exclusive)
Definition: font_helper.h:525
reserved reg flag (reserved for future use)
Definition: font_helper.h:540
const struct WBFont * WB_FONTC
a 'const' version of WB_FONT - note that this CAN actually be written to, so it's not truly 'const'.
Definition: font_helper.h:185
'Any' style (default, zero)
Definition: font_helper.h:491
'reserved' width type
Definition: font_helper.h:524
void WBTextExtent(WB_FONTC pFont, const char *szText, int cbText, WB_EXTENT *pExtent)
Obtain the pixel extent of specified text for a specified XFontSet.
Definition: font_helper.c:790
XCharStruct WBFontSetMaxBounds(Display *pDisplay, XFontSet fontSet)
Get a 'maximized' copy of the 'max_bounds' member for the font set.
Definition: font_legacy.c:1206
int WBFontSetAscent(Display *pDisplay, XFontSet fontSet)
Get the maximum character ascent from a font set.
Definition: font_legacy.c:1055
reserved reg flag (reserved for future use)
Definition: font_helper.h:537
reserved font weight (5) (not currently in use)
Definition: font_helper.h:512
font registry mask
Definition: font_helper.h:544
force variable pitch
Definition: font_helper.h:482
XFontStruct * WBLoadFontX(Display *pDisplay, const char *szFontName, int iFontSize, int iFlags)
load a font based on a font name, size, and font flags
Definition: font_legacy.c:772
use ISO8859 registry (mutually exclusive)
Definition: font_helper.h:529
int iHeight
RESERVED - cached font height (-1 if not valid). do not access this directly.
Definition: font_helper.h:158