xftint.h revision de3c0529
1/*
2 * Copyright © 2022 Thomas E. Dickey
3 * Copyright © 2000 Keith Packard
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the above copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission.  The above copyright holders make no
12 * representations about the suitability of this software for any purpose.  It
13 * is provided "as is" without express or implied warranty.
14 *
15 * THE ABOVE LISTED COPYRIGHT HOLDER(S) DISCLAIM ALL WARRANTIES WITH REGARD TO
16 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE
18 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24/*
25 * These definitions are solely for use by the implementation of Xft
26 * and constitute no kind of standard.  If you need any of these functions,
27 * please drop me a note.  Either the library needs new functionality,
28 * or there's a way to do what you need using the existing published
29 * interfaces. keithp@freedesktop.org
30 */
31
32#ifndef _XFTINT_H_
33#define _XFTINT_H_
34
35#ifdef HAVE_CONFIG_H
36#include "config.h"
37#else /* X monolithic tree */
38#define HAVE_STDLIB_H 1  /* assumed since all ANSI C platforms require it */
39#include <X11/Xosdefs.h> /* get string.h or strings.h as appropriate */
40#endif
41
42#include <stdio.h>
43#if HAVE_STDLIB_H
44#include <stdlib.h>
45#endif
46#if HAVE_STRING_H
47#include <string.h>
48#else
49#if HAVE_STRINGS_H
50#include <strings.h>
51#endif
52#endif
53#include <ctype.h>
54#include <assert.h>
55
56#include <X11/Xlib.h>
57#include <X11/Xutil.h>
58#include <X11/Xmd.h>
59#include <X11/Xlibint.h>
60#define _XFT_NO_COMPAT_
61#include "Xft.h"
62#include <fontconfig/fcprivate.h>
63#include <fontconfig/fcfreetype.h>
64
65/* Added to <X11/Xfuncproto.h> in X11R6.9 and later */
66#ifndef _X_HIDDEN
67# define _X_HIDDEN /**/
68#endif
69#ifndef _X_EXPORT
70# define _X_EXPORT /**/
71#endif
72
73typedef struct _XftMatcher {
74    char    *object;
75    double  (*compare) (char *object, FcValue value1, FcValue value2);
76} XftMatcher;
77
78typedef struct _XftSymbolic {
79    const char	*name;
80    int		value;
81} XftSymbolic;
82
83/*
84 * Glyphs are stored in this structure
85 */
86typedef struct _XftGlyph {
87    XGlyphInfo	    metrics;
88    void	    *bitmap;
89    unsigned long   glyph_memory;
90    Picture         picture;
91} XftGlyph;
92
93/*
94 * If the "trackmemusage" option is set, glyphs are managed via a doubly-linked
95 * list.  To save space, the links are just array indices.
96 */
97typedef struct _XftGlyphUsage {
98    XftGlyph        contents;
99    FT_UInt	    newer;
100    FT_UInt	    older;
101} XftGlyphUsage;
102
103/*
104 * A hash table translates Unicode values into glyph indices
105 */
106typedef struct _XftUcsHash {
107    FcChar32	    ucs4;
108    FT_UInt	    glyph;
109} XftUcsHash;
110
111/*
112 * Many fonts can share the same underlying face data; this
113 * structure references that.  Note that many faces may in fact
114 * live in the same font file; that is irrelevant to this structure
115 * which is concerned only with the individual faces themselves
116 */
117
118typedef struct _XftFtFile {
119    struct _XftFtFile	*next;
120    int			ref;	    /* number of font infos using this file */
121
122    char		*file;	    /* file name */
123    int			id;	    /* font index within that file */
124
125    FT_F26Dot6		xsize;	    /* current xsize setting */
126    FT_F26Dot6		ysize;	    /* current ysize setting */
127    FT_Matrix		matrix;	    /* current matrix setting */
128
129    int			lock;	    /* lock count; can't unload unless 0 */
130    FT_Face		face;	    /* pointer to face; only valid when lock */
131} XftFtFile;
132
133/*
134 * This structure holds the data extracted from a pattern
135 * needed to create a unique font object.
136 */
137
138struct _XftFontInfo {
139    /*
140     * Hash value (not include in hash value computation)
141     */
142    FcChar32		hash;
143    XftFtFile		*file;		/* face source */
144    /*
145     * Rendering options
146     */
147    FT_F26Dot6		xsize, ysize;	/* pixel size */
148    FcBool		antialias;	/* doing antialiasing */
149    FcBool		embolden;	/* force emboldening */
150    FcBool		color;		/* contains color glyphs */
151    int			rgba;		/* subpixel order */
152    int			lcd_filter;	/* lcd filter */
153    FT_Matrix		matrix;		/* glyph transformation matrix */
154    FcBool		transform;	/* non-identify matrix? */
155    FT_Int		load_flags;	/* glyph load flags */
156    FcBool		render;		/* whether to use the Render extension */
157    /*
158     * Internal fields
159     */
160    int			spacing;
161    FcBool		minspace;
162    int			char_width;
163};
164
165/*
166 * Internal version of the font with private data
167 */
168
169typedef struct _XftFontInt {
170    XftFont		public;		/* public fields */
171    XftFont		*next;		/* all fonts on display */
172    XftFont		*hash_next;	/* fonts in this hash chain */
173    XftFontInfo		info;		/* Data from pattern */
174    int			ref;		/* reference count */
175    /*
176     * Per-glyph information, indexed by glyph ID
177     * This array follows the font in memory
178     */
179    XftGlyph		**glyphs;
180    FT_UInt		num_glyphs;	/* size of glyphs/bitmaps arrays */
181    /*
182     * Hash table to get from Unicode value to glyph ID
183     * This array follows the glyphs in memory
184     */
185    XftUcsHash		*hash_table;
186    int			hash_value;
187    int			rehash_value;
188    /*
189     * X specific fields
190     */
191    GlyphSet		glyphset;	/* Render glyphset */
192    XRenderPictFormat	*format;	/* Render format for glyphs */
193    /*
194     * Glyph memory management fields
195     */
196    unsigned long	glyph_memory;
197    unsigned long	max_glyph_memory;
198    unsigned            sizeof_glyph;	/* sizeof(XftGlyph) or XftGlyphUsage */
199    FT_UInt		newest;		/* index, for tracking usage */
200    FT_UInt		total_inuse;	/* total, for verifying usage */
201    FcBool		track_mem_usage;   /* Use XftGlyphUsage */
202    FcBool		use_free_glyphs;   /* Use XRenderFreeGlyphs */
203} XftFontInt;
204
205typedef enum _XftClipType {
206    XftClipTypeNone, XftClipTypeRegion, XftClipTypeRectangles
207} XftClipType;
208
209typedef struct _XftClipRect {
210    int			xOrigin;
211    int			yOrigin;
212    int			n;
213} XftClipRect;
214
215#define XftClipRects(cr)    ((XRectangle *) ((cr) + 1))
216
217typedef union _XftClip {
218    XftClipRect	    *rect;
219    Region	    region;
220} XftClip;
221
222struct _XftDraw {
223    Display	    *dpy;
224    int		    screen;
225    unsigned int    bits_per_pixel;
226    unsigned int    depth;
227    Drawable	    drawable;
228    Visual	    *visual;	/* NULL for bitmaps */
229    Colormap	    colormap;
230    XftClipType	    clip_type;
231    XftClip	    clip;
232    int		    subwindow_mode;
233    struct {
234	Picture		pict;
235    } render;
236    struct {
237	GC		gc;
238	int		use_pixmap;
239    } core;
240};
241
242/*
243 * Instead of taking two round trips for each blending request,
244 * assume that if a particular drawable fails GetImage that it will
245 * fail for a "while"; use temporary pixmaps to avoid the errors
246 */
247
248#define XFT_ASSUME_PIXMAP	20
249
250typedef struct _XftSolidColor {
251    XRenderColor    color;
252    int		    screen;
253    Picture	    pict;
254} XftSolidColor;
255
256#define XFT_NUM_SOLID_COLOR	16
257
258#define XFT_NUM_FONT_HASH	127
259
260typedef struct _XftDisplayInfo {
261    struct _XftDisplayInfo  *next;
262    Display		    *display;
263    XExtCodes		    *codes;
264    FcPattern		    *defaults;
265    FcBool		    hasRender;
266    FcBool		    hasSolid;
267    XftFont		    *fonts;
268    XRenderPictFormat	    *solidFormat;
269    unsigned long	    glyph_memory;
270    unsigned long	    max_glyph_memory;
271    FcBool		    track_mem_usage;
272    FcBool		    use_free_glyphs;
273    int			    num_unref_fonts;
274    int			    max_unref_fonts;
275    XftSolidColor	    colors[XFT_NUM_SOLID_COLOR];
276    XftFont		    *fontHash[XFT_NUM_FONT_HASH];
277} XftDisplayInfo;
278
279/*
280 * By default, use no more than 4 meg of server memory total, and no
281 * more than 1 meg for any one font
282 */
283#define XFT_DPY_MAX_GLYPH_MEMORY    (4 * 1024 * 1024)
284#define XFT_FONT_MAX_GLYPH_MEMORY   (1024 * 1024)
285
286/*
287 * By default, keep the last 16 unreferenced fonts around to
288 * speed reopening them.  Note that the glyph caching code
289 * will keep the global memory usage reasonably limited
290 */
291#define XFT_DPY_MAX_UNREF_FONTS	    16
292
293extern XftDisplayInfo	*_XftDisplayInfo;
294
295/*
296 * Bits in $XFT_DEBUG, which can be combined.
297 */
298#define XFT_DBG_OPEN	1
299#define XFT_DBG_OPENV	2
300#define XFT_DBG_RENDER	4
301#define XFT_DBG_DRAW	8
302#define XFT_DBG_REF	16
303#define XFT_DBG_GLYPH	32
304#define XFT_DBG_GLYPHV	64
305#define XFT_DBG_CACHE	128
306#define XFT_DBG_CACHEV	256
307#define XFT_DBG_MEMORY	512
308#define XFT_DBG_USAGE	1024
309
310/*
311 * Categories for memory allocation.
312 */
313typedef enum {
314    XFT_MEM_DRAW
315    , XFT_MEM_FONT
316    , XFT_MEM_FILE
317    , XFT_MEM_GLYPH
318    , XFT_MEM_NUM
319} XFT_MEM_KIND;
320
321#define AllocTypedArray(n,type)         malloc ((size_t)(n) * sizeof (type))
322#define AllocUIntArray(n)               AllocTypedArray(n, FT_UInt)
323#define AllocGlyphElt8Array(n)          AllocTypedArray(n, XGlyphElt8)
324#define AllocGlyphSpecArray(n)          AllocTypedArray(n, XftGlyphSpec)
325#define AllocGlyphFontSpecArray(n)      AllocTypedArray(n, XftGlyphFontSpec)
326
327/* xftcore.c */
328void
329XftRectCore (XftDraw		*draw,
330	     _Xconst XftColor	*color,
331	     int		x,
332	     int		y,
333	     unsigned int	width,
334	     unsigned int	height);
335
336void
337XftGlyphCore (XftDraw		*draw,
338	      _Xconst XftColor	*color,
339	      XftFont		*public,
340	      int		x,
341	      int		y,
342	      _Xconst FT_UInt	*glyphs,
343	      int		nglyphs);
344
345void
346XftGlyphSpecCore (XftDraw		*draw,
347		  _Xconst XftColor	*color,
348		  XftFont		*public,
349		  _Xconst XftGlyphSpec	*glyphs,
350		  int			nglyphs);
351
352void
353XftGlyphFontSpecCore (XftDraw			*draw,
354		      _Xconst XftColor		*color,
355		      _Xconst XftGlyphFontSpec	*glyphs,
356		      int			nglyphs);
357
358/* xftdbg.c */
359int
360XftDebug (void);
361
362/* xftdpy.c */
363XftDisplayInfo *
364_XftDisplayInfoGet (Display *dpy, FcBool createIfNecessary);
365
366void
367_XftDisplayManageMemory (Display *dpy);
368
369int
370XftDefaultParseBool (const char *v);
371
372FcBool
373XftDefaultGetBool (Display *dpy, const char *object, int screen, FcBool def);
374
375int
376XftDefaultGetInteger (Display *dpy, const char *object, int screen, int def);
377
378double
379XftDefaultGetDouble (Display *dpy, const char *object, int screen, double def);
380
381FcFontSet *
382XftDisplayGetFontSet (Display *dpy);
383
384/* xftdraw.c */
385unsigned int
386XftDrawDepth (XftDraw *draw);
387
388unsigned int
389XftDrawBitsPerPixel (XftDraw *draw);
390
391FcBool
392XftDrawRenderPrepare (XftDraw	*draw);
393
394/* xftextent.c */
395
396/* xftfont.c */
397
398/* xftfreetype.c */
399FcBool
400_XftSetFace (XftFtFile *f, FT_F26Dot6 xsize, FT_F26Dot6 ysize, FT_Matrix *matrix);
401
402void
403XftFontManageMemory (Display *dpy);
404
405/* xftglyph.c */
406void
407_XftFontUncacheGlyph (Display *dpy, XftFont *public);
408
409void
410_XftFontManageMemory (Display *dpy, XftFont *public);
411
412/* xftinit.c */
413void
414XftMemReport (void);
415
416void
417XftMemAlloc (int kind, size_t size);
418
419void
420XftMemFree (int kind, size_t size);
421
422/* xftlist.c */
423FcFontSet *
424XftListFontsPatternObjects (Display	    *dpy,
425			    int		    screen,
426			    FcPattern	    *pattern,
427			    FcObjectSet    *os);
428
429/* xftname.c */
430
431/* xftrender.c */
432
433/* xftstr.c */
434int
435_XftMatchSymbolic (XftSymbolic *s, int n, const char *name, int def);
436
437/* xftswap.c */
438int
439XftNativeByteOrder (void);
440
441void
442XftSwapCARD32 (CARD32 *data, int n);
443
444void
445XftSwapCARD24 (CARD8 *data, int width, int height);
446
447void
448XftSwapCARD16 (CARD16 *data, int n);
449
450void
451XftSwapImage (XImage *image);
452
453/* xftxlfd.c */
454#endif /* _XFT_INT_H_ */
455