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 _WBExtent_ 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 
296 void WBFreeFont(Display *pDisplay, WB_FONT pFont);
297 
312 WB_FONT WBCopyFont(Display *pDisplay, WB_FONTC pFont);
313 
336 WB_FONT WBCopyModifyFont(Display *pDisplay, WB_FONTC pOriginal,
337  int iFontSize, int iFlags);
338 
361 WB_FONT WBLoadFont(Display *pDisplay, const char *szFontName,
362  int iFontSize, int iFlags);
363 
373 int WBFontAvgCharWidth(WB_FONTC pFont);
374 
375 
385 int WBFontDescent(WB_FONTC pFont);
386 
387 
397 int WBFontAscent(WB_FONTC pFont);
398 
399 
409 int WBFontHeight(WB_FONTC pFont);
410 
411 
422 XCharStruct WBFontMaxBounds(WB_FONTC pFont);
423 
424 
438 int WBTextWidth(WB_FONTC pFont, const char *szText, int cbText);
439 
440 
456 void WBTextExtent(WB_FONTC pFont, const char *szText, int cbText, WB_EXTENT *pExtent);
457 
458 
469 {
475 
482 
488 
489  // if the font size is > 0 then these bits apply
495 
504 
510 
516 
517  // reserved - font registry and encoding (installed X11 fonts typically include most if not all of these)
520  WBFontFlag_REG_ASCII = 0x300000,
522  WBFontFlag_REG_MISC = 0x500000,
524  WBFontFlag_REG_ADOBE = 0x700000,
533  WBFontFlag_REG_MASK = 0xf00000,
534 
535  // TODO: flags for 'encoding' ?
536 
537  WBFontFlag_WHATEVER = 0 // don't force anything (zero, default)
538 };
539 
540 
541 
543 // //
544 // _ _____ _ ____ _ //
545 // | | ___ __ _ __ _ ___ _ _ | ___|___ _ __ | |_ / ___| _ _ _ __ _ __ ___ _ __ | |_ //
546 // | | / _ \ / _` | / _` | / __|| | | | | |_ / _ \ | '_ \ | __| \___ \ | | | || '_ \ | '_ \ / _ \ | '__|| __| //
547 // | |___| __/| (_| || (_| || (__ | |_| | | _|| (_) || | | || |_ ___) || |_| || |_) || |_) || (_) || | | |_ //
548 // |_____|\___| \__, | \__,_| \___| \__, | |_| \___/ |_| |_| \__| |____/ \__,_|| .__/ | .__/ \___/ |_| \__| //
549 // |___/ |___/ |_| |_| //
550 // //
552 
553 
576 XFontStruct *WBLoadFontX(Display *pDisplay, const char *szFontName,
577  int iFontSize, int iFlags);
578 
592 XFontStruct *WBCopyFontX(XFontStruct *pFont);
593 
616 XFontStruct *WBLoadModifyFontX(Display *pDisplay, const XFontStruct *pOriginal,
617  int iFontSize, int iFlags);
618 
641 XFontSet WBCopyModifyFontSet(Display *pDisplay, XFontSet fsOrig, int iFontSize, int iFlags);
642 
643 
654 int WBFontAvgCharWidthX(Display *pDisplay, const XFontStruct *pFont);
655 
656 
668 int WBFontSetDescent(Display *pDisplay, XFontSet fontSet);
669 
670 
682 int WBFontSetAscent(Display *pDisplay, XFontSet fontSet);
683 
684 
695 int WBFontSetHeight(Display *pDisplay, XFontSet fontSet);
696 
697 
709 int WBFontSetAvgCharWidth(Display *pDisplay, XFontSet fontSet);
710 
711 
723 XCharStruct WBFontSetMaxBounds(Display *pDisplay, XFontSet fontSet);
724 
725 
742 XFontSet WBFontSetFromFont(Display *pDisplay, const XFontStruct *pFont);
743 
744 
758 XFontStruct * WBFontFromFontSet(Display *pDisplay, XFontSet fontSet);
759 
760 
781 XFontSet WBFontSetFromFontSingle(Display *pDisplay, const XFontStruct *pFont);
782 
783 
797 int WBTextWidthX(XFontSet fontSet, const char *szText, int cbText);
798 
799 
815 void WBTextExtentX(XFontSet fontSet, const char *szText, int cbText, WB_EXTENT *pExtent);
816 
817 
818 #ifdef NO_DEBUG
819 #define WBDumpFontInfo(X) /* do nothing macro */
820 #else // NO_DEBUG
821 
833 void WBDumpFontInfo(const char *pSpec); // debugging function - dumps a list of fonts and info
834 
835 #endif // NO_DEBUG
836 
837 
838 
839 #ifdef __cplusplus
840 };
841 #endif // __cplusplus
842 
843 
844 #endif // _FONT_HELPER_H_INCLUDED_
845 
font size is in &#39;twips&#39; (when font size > 0)
Definition: font_helper.h:492
int iMaxCharWidth
RESERVED - cached max character width (-1 if not valid). do not access this directly.
Definition: font_helper.h:160
internal wrapper struct for &#39;extent&#39; definition
use a font specific registry (mutually exclusive)
Definition: font_helper.h:521
font size is in &#39;points&#39; (when font size > 0)
Definition: font_helper.h:491
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)
make a copy of an existing font (best when assigning to a window)
Definition: font_helper.c:250
reserved (style)
Definition: font_helper.h:478
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:497
int WBFontAscent(WB_FONTC pFont)
Get the maximum character ascent from a WB_FONT.
Definition: font_helper.c:446
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:500
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 &#39;fixed&#39;)
Definition: font_helper.h:472
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:528
reserved reg flag (reserved for future use)
Definition: font_helper.h:532
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:493
&#39;oblique&#39; slant (mutually exclusive)
Definition: font_helper.h:506
XFontSet WBFontSetFromFont(Display *pDisplay, const XFontStruct *pFont)
Creates an &#39;XFontSet&#39; from an XFontStruct for a given display.
Definition: font_legacy.c:1579
semicondensed width (mutually exclusive)
Definition: font_helper.h:512
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:611
reserved reg flag (reserved for future use)
Definition: font_helper.h:530
&#39;Any&#39; pitch (default, zero)
Definition: font_helper.h:473
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:523
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:531
font weight bit mask
Definition: font_helper.h:503
reserved reg flag (reserved for future use)
Definition: font_helper.h:525
struct WBFont * WB_FONT
An allocated structure containing XFontStruct, XFontInfo, and XftFont [as applicable] for a specified...
&#39;foundry mask&#39;
Definition: font_helper.h:487
reserved (style)
Definition: font_helper.h:479
force fixed pitch
Definition: font_helper.h:470
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
&#39;pitch mask&#39;
Definition: font_helper.h:474
use ADOBE registry - a lot of fonts use this (mutually exclusive)
Definition: font_helper.h:524
medium font weight (mutually exclusive0
Definition: font_helper.h:498
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 &#39;XFontSet&#39; from an XFontStruct for a given display, with only a single font in the set...
Definition: font_legacy.c:1759
&#39;regular&#39; slant (mutually exclusive)
Definition: font_helper.h:505
regular font weight (mutually exclusive0
Definition: font_helper.h:497
force freetype
Definition: font_helper.h:484
force raster (xfree86?)
Definition: font_helper.h:483
font size is in &#39;pixels&#39; (when font size > 0)
Definition: font_helper.h:490
reserved reg flag (reserved for future use)
Definition: font_helper.h:527
reserved font weight (6) (not currently in use)
Definition: font_helper.h:502
static void WBDumpFontSet(Display *pDisplay, XFontSet fontSet)
debug function to dump font set members
XCharStruct max_bounds
RESERVED - cached max bounds (all 0&#39;s if not valid). do not access this directly. ...
Definition: font_helper.h:161
use &#39;MISC&#39; registry (mutually exclusive)
Definition: font_helper.h:522
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:511
demi-bold font weight (mutually exclusive0
Definition: font_helper.h:499
static void WBDumpMatchingFontNames(Display *pDisplay, const char *szFontName)
debug function to dump matching font names
XCharStruct WBFontMaxBounds(WB_FONTC pFont)
Get a &#39;maximized&#39; copy of &#39;max_bounds&#39; (applicable to all font faces in the WB_FONT) ...
Definition: font_helper.c:543
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:496
&#39;size mask&#39; for font size > 0
Definition: font_helper.h:494
force sans-serif
Definition: font_helper.h:476
&#39;italic&#39; slant (mutually exclusive)
Definition: font_helper.h:507
WBFontFlags
Font &#39;flag&#39; enumeration.
Definition: font_helper.h:468
force serif (i.e. &#39;not sans&#39;)
Definition: font_helper.h:477
XFontSet fsFont
legacy &#39;XFontSet&#39; 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
&#39;Any&#39; foundry (default, zero)
Definition: font_helper.h:486
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:519
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:485
XFontStruct * pFontStruct
legacy &#39;XFontStruct&#39; 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:394
XFontStruct * WBFontFromFontSet(Display *pDisplay, XFontSet fontSet)
Creates an &#39;XFontStruct&#39; 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:698
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 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
&#39;default&#39; slant (zero; mutually exclusive; may return an italic or oblique font)
Definition: font_helper.h:508
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:520
&#39;Any&#39; width (zero; mutually exclusive)
Definition: font_helper.h:514
reserved reg flag (reserved for future use)
Definition: font_helper.h:529
const struct WBFont * WB_FONTC
a &#39;const&#39; version of WB_FONT - note that this CAN actually be written to, so it&#39;s not truly &#39;const&#39;...
Definition: font_helper.h:185
&#39;Any&#39; style (default, zero)
Definition: font_helper.h:480
&#39;reserved&#39; width type
Definition: font_helper.h:513
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:741
XCharStruct WBFontSetMaxBounds(Display *pDisplay, XFontSet fontSet)
Get a &#39;maximized&#39; copy of the &#39;max_bounds&#39; 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:526
reserved font weight (5) (not currently in use)
Definition: font_helper.h:501
font registry mask
Definition: font_helper.h:533
force variable pitch
Definition: font_helper.h:471
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:518
int iHeight
RESERVED - cached font height (-1 if not valid). do not access this directly.
Definition: font_helper.h:158