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