Use pkg-config (if available) for Xft.
[m17n/m17n-lib.git] / src / font.h
1 /* font.h -- header file for the font module.
2    Copyright (C) 2003, 2004
3      National Institute of Advanced Industrial Science and Technology (AIST)
4      Registration Number H15PRO112
5
6    This file is part of the m17n library.
7
8    The m17n library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Lesser General Public License
10    as published by the Free Software Foundation; either version 2.1 of
11    the License, or (at your option) any later version.
12
13    The m17n 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 GNU
16    Lesser General Public License for more details.
17
18    You should have received a copy of the GNU Lesser General Public
19    License along with the m17n library; if not, write to the Free
20    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21    02111-1307, USA.  */
22
23 #ifndef _M17N_FONT_H_
24 #define _M17N_FONT_H_
25
26 /** Type of font.  Now obsolete.  */
27
28 enum MFontType
29   {
30     /** Fonts supproted by a window system natively.  On X Window
31         System, it is an X font.  */
32     MFONT_TYPE_WIN,
33
34     /** Fonts supported by FreeType library.  */
35     MFONT_TYPE_FT,
36
37     /** anchor */
38     MFONT_TYPE_MAX
39   };
40
41
42 enum MFontProperty
43   {
44     /* The order of MFONT_FOUNDRY to MFONT_ADSTYLE must be the same as
45        enum MFaceProperty.  */
46     MFONT_FOUNDRY,
47     MFONT_FAMILY,
48     MFONT_WEIGHT,
49     MFONT_STYLE,
50     MFONT_STRETCH,
51     MFONT_ADSTYLE,
52     MFONT_REGISTRY,
53     MFONT_SIZE,
54     MFONT_RESY,
55     /* anchor */
56     MFONT_PROPERTY_MAX
57   };
58
59 /** Information about a font.  This structure is used in three ways:
60     FONT-OBJ, FONT-OPENED, and FONT-SPEC.  
61
62     FONT-OBJ: To store information of an existing font.  Instances
63     appear only in <font_list> of MDisplay.
64
65     FONT-OPENED: To store information of an opened font.  Instances
66     appear only in <opend_list> of MDisplay.
67
68     FONT-SPEC: To store specifications of a font.  Instances appear
69     only in <spec_list> of MFontset.  */
70
71 struct MFont
72 {
73   /** Numeric value of each font property.  Also used as an index to
74       the table @c mfont__property_table to get the actual name of the
75       property.
76
77       For FONT-OBJ, FONT-OPENED: The value is greater than zero.
78
79       For FONT-SPEC: The value is equal to or greater than zero.  Zero
80       means that the correponding property is not specified (i.e. wild
81       card).
82
83       <property>[MFONT_SIZE] is the size of the font in 1/10 pixels.
84
85       For FONT-OBJ: If the value is 0, the font is scalable or
86       auto-scaled.
87
88       For FONT-OPENED: The actual size of opened font.
89
90       <prperty>[MFONT_RESY] is the designed resolution of the font in
91       DPI, or zero.  Zero means that the font is scalable.
92
93       For the time being, we mention only Y-resolution (resy) and
94       assume that resx is always equal to resy.  */
95   unsigned short property[MFONT_PROPERTY_MAX];
96 };
97
98 typedef struct
99 {
100   int size, inc, used;
101   MSymbol property;
102   MSymbol *names;
103 } MFontPropertyTable;
104
105 extern MFontPropertyTable mfont__property_table[MFONT_REGISTRY + 1];
106
107 /** Return the symbol of the Nth font property of FONT.  */
108 #define FONT_PROPERTY(font, n)  \
109   mfont__property_table[(n)].names[(font)->property[(n)]]
110
111 typedef struct MFontEncoding MFontEncoding;
112
113 struct MRealizedFont
114 {
115   /* Frame on which the font is realized.  */
116   MFrame *frame;
117
118   /* Font spec used to find the font.  */
119   MFont spec;
120
121   /* Font spec requested by a face.  */
122   MFont request;
123
124   /* The found font.  */
125   MFont font;
126
127   /* How well <font> matches with <request>.  */
128   int score;
129
130   MFontDriver *driver;
131
132   /* Font Layout Table for the font.  */
133   MSymbol layouter;
134
135   /* 0: not yet tried to open
136      -1: failed to open
137      1: succeessfully opened.  */
138   int status;
139
140   /* Extra information set by <driver>->select or <driver>->open.  If
141      non-NULL, it must be a pointer to a managed object.  */
142   void *info;
143
144   short ascent, descent;
145
146   MFontEncoding *encoding;
147
148   /* Type of the font: Mx, Mfreetype, or Mxft.  */
149   MSymbol type;
150
151   /* Pointer to the font structure.  */
152   void *fontp;
153 };
154
155 /** Structure to hold a list of fonts of each registry.  */
156
157 typedef struct
158 {
159   MSymbol tag;
160   int nfonts;
161   MFont *fonts;
162 } MFontList;
163
164
165 struct MFontDriver
166 {
167   /** Return a font satisfying REQUEST and best matching with SPEC.
168       For the moment, LIMITTED_SIZE is ignored.  */
169   MRealizedFont *(*select) (MFrame *frame, MFont *spec, MFont *request,
170                             int limitted_size);
171
172   /** Open a font specified by RFONT.  */
173   int (*open) (MRealizedFont *rfont);
174
175   /** Set metrics of glyphs in GSTRING from FROM to TO.  */
176   void (*find_metric) (MRealizedFont *rfont, MGlyphString *gstring,
177                        int from, int to);
178
179   /** Encode CODE into a glyph code the font.  CODE is a code point of
180       a character in rfont->encoder->encoding_charset.  If the font
181       has no glyph for CODE, return MCHAR_INVALID_CODE.  */
182   unsigned (*encode_char) (MRealizedFont *rfont, unsigned code);
183
184   /** Draw glyphs from FROM to TO (exclusive) on window WIN of FRAME
185       at coordinate (X, Y) relative to WIN.  */
186   void (*render) (MDrawWindow win, int x, int y,
187                   MGlyphString *gstring, MGlyph *from, MGlyph *to,
188                   int reverse, MDrawRegion region);
189
190   /** Push to PLIST fonts matching with FONT.  LANGUAGE, if not Mnil,
191       limits fonts to ones that support LANGUAGE.  MAXNUM if greater
192       than 0 limits the number of listed fonts.  Return how many fonts
193       a listed.  */
194   int (*list) (MFrame *frame, MPlist *plist, MFont *font, MSymbol language,
195                int maxnum);
196 };
197
198 /** Initialize the members of FONT.  */
199
200 #define MFONT_INIT(font) memset ((font), 0, sizeof (MFont))
201
202 extern MSymbol Mlayouter;
203
204 extern int mfont__flt_init ();
205
206 extern void mfont__flt_fini ();
207
208 #ifdef HAVE_FREETYPE
209 #include <ft2build.h>
210 #include FT_FREETYPE_H
211
212 #ifdef HAVE_FONTCONFIG
213 #include <fontconfig/fontconfig.h>
214 #endif
215
216 #ifdef HAVE_OTF
217 #include <otf.h>
218 #endif /* HAVE_OTF */
219
220 typedef struct
221 {
222   M17NObject control;
223   MFont font;
224   char *filename;
225   int otf_flag; /* This font is OTF (1), may be OTF (0), is not OTF (-1).  */
226   MPlist *charmap_list;
227   int charmap_index;
228   FT_Face ft_face;
229   char *languages;
230 #ifdef HAVE_OTF
231   OTF *otf;
232 #endif /* HAVE_OTF */
233 #ifdef HAVE_FONTCONFIG
234   FcLangSet *langset;
235 #else
236   void *langset;
237 #endif
238   void *extra_info;             /* Xft uses this member.  */
239 } MFTInfo;
240
241 extern MFontDriver mfont__ft_driver;
242
243 extern int mfont__ft_init ();
244
245 extern void mfont__ft_fini ();
246
247 extern int mfont__ft_parse_name (char *name, MFont *font);
248
249 extern char *mfont__ft_unparse_name (MFont *font);
250
251 #ifdef HAVE_OTF
252
253 extern int mfont__ft_drive_otf (MGlyphString *gstring, int from, int to,
254                                 MSymbol script, MSymbol langsys,
255                                 MSymbol gsub_features, MSymbol gpos_features);
256
257 extern int mfont__ft_decode_otf (MGlyph *g);
258
259 #endif  /* HAVE_OTF */
260
261 #endif /* HAVE_FREETYPE */
262
263 extern void mfont__free_realized (MRealizedFont *rfont);
264
265 extern int mfont__match_p (MFont *font, MFont *spec, int prop);
266
267 extern int mfont__score (MFont *font, MFont *spec, MFont *request,
268                          int limitted_size);
269
270 extern void mfont__set_spec_from_face (MFont *spec, MFace *face);
271
272 extern MSymbol mfont__set_spec_from_plist (MFont *spec, MPlist *plist);
273
274 extern void mfont__resize (MFont *spec, MFont *request);
275
276 extern unsigned mfont__encode_char (MRealizedFont *rfont, int c);
277
278 extern MRealizedFont *mfont__select (MFrame *frame, MFont *spec,
279                                      MFont *request, int limitted_size,
280                                      MSymbol layouter);
281
282 extern int mfont__open (MRealizedFont *rfont);
283
284 extern void mfont__get_metric (MGlyphString *gstring, int from, int to);
285
286 extern void mfont__set_property (MFont *font, enum MFontProperty key,
287                                  MSymbol val);
288
289 extern int mfont__split_name (char *name, int *property_idx,
290                               unsigned short *point, unsigned short *resy);
291
292 extern void mfont__set_spec (MFont *font,
293                              MSymbol attrs[MFONT_PROPERTY_MAX],
294                              unsigned short size, unsigned short resy);
295
296 extern int mfont__parse_name_into_font (char *name, MSymbol format,
297                                         MFont *font);
298
299 extern MPlist *mfont__encoding_list (void);
300
301 extern unsigned mfont__flt_encode_char (MSymbol layouter_name, int c);
302
303 extern int mfont__flt_run (MGlyphString *gstring, int from, int to,
304                            MRealizedFace *rface);
305
306 #endif /* _M17N_FONT_H_ */