(enum MFontType): Members completely changed.
[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 enum MFontProperty
27   {
28     /* The order of MFONT_FOUNDRY to MFONT_ADSTYLE must be the same as
29        enum MFaceProperty.  */
30     MFONT_FOUNDRY,
31     MFONT_FAMILY,
32     MFONT_WEIGHT,
33     MFONT_STYLE,
34     MFONT_STRETCH,
35     MFONT_ADSTYLE,
36     MFONT_REGISTRY,
37     MFONT_RESY,
38     /* Actually this should not be used as an index to
39        MFont->propperty.  */
40     MFONT_SIZE,
41     /* anchor */
42     MFONT_PROPERTY_MAX = MFONT_SIZE
43   };
44
45 enum MFontType
46   {
47     MFONT_TYPE_SPEC,
48     MFONT_TYPE_OBJECT,
49     MFONT_TYPE_REALIZED,
50     MFONT_TYPE_FAILURE
51   };
52
53 enum MFontSource
54   {
55     MFONT_SOURCE_UNDECIDED = 0,
56     MFONT_SOURCE_X = 1,
57     MFONT_SOURCE_FT = 2
58   };
59
60 typedef struct MFontEncoding MFontEncoding;
61 typedef struct MFontDriver MFontDriver;
62
63 /** Information about a font.  This structure is used in three ways:
64     FONT-OBJ, FONT-OPENED, and FONT-SPEC.  
65
66     FONT-OBJ: To store information of an existing font.  Instances
67     appear only in <font_list> of MDisplay.
68
69     FONT-OPENED: To store information of an opened font.  Instances
70     appear only in <opend_list> of MDisplay.
71
72     FONT-SPEC: To store specifications of a font.  Instances appear
73     only in <spec_list> of MFontset.  */
74
75 struct MFont
76 {
77   /** Numeric value of each font property.  Also used as an index to
78       the table @c mfont__property_table to get the actual name of the
79       property.
80
81       For FONT-OBJ, FONT-OPENED: The value is greater than zero.
82
83       For FONT-SPEC: The value is equal to or greater than zero.  Zero
84       means that the correponding property is not specified (i.e. wild
85       card).
86
87       For FONT-OBJ: If the value is 0, the font is scalable or
88       auto-scaled.
89
90       For FONT-OPENED: The actual size of opened font.
91
92       <prperty>[MFONT_RESY] is the designed resolution of the font in
93       DPI, or zero.  Zero means that the font is scalable.
94
95       For the time being, we mention only Y-resolution (resy) and
96       assume that resx is always equal to resy.  */
97   unsigned short property[MFONT_PROPERTY_MAX];
98   enum MFontType type : 2;
99   enum MFontSource source : 2;
100   /* Size of the font in 1/10 pixels.  */
101   unsigned size : 26;
102   MSymbol file;
103   MSymbol capability;
104   MFontEncoding *encoding;
105 };
106
107 typedef struct
108 {
109   int size, inc, used;
110   MSymbol property;
111   MSymbol *names;
112 } MFontPropertyTable;
113
114 extern MFontPropertyTable mfont__property_table[MFONT_REGISTRY + 1];
115
116 /** Return the symbol of the Nth font property of FONT.  */
117 #define FONT_PROPERTY(font, n)  \
118   (mfont__property_table[(n)].names[(font)->property[(n)]])
119
120 struct MRealizedFont
121 {
122   /* Font spec used to find the font.
123      MRealizedFont::spec.property[MFONT_TYPE] is MFONT_TYPE_REALIZED
124      so that this object can be distingushed from MFont.  */
125   MFont spec;
126
127   /* Frame on which the font is realized.  */
128   MFrame *frame;
129
130   /* The found font.  */
131   MFont *font;
132
133   MFontDriver *driver;
134
135   /* Font Layout Table for the font.  This is just a container to
136      carry the infomation.  */
137   MSymbol layouter;
138
139   /* Extra information set by MRealizedFont::driver->open.  If
140      non-NULL, it must be a pointer to a managed object.  */
141   void *info;
142
143   short ascent, descent;
144
145   /* Pointer to the font structure.  */
146   void *fontp;
147
148   MRealizedFont *next;
149 };
150
151 typedef struct {
152   MFont *font;
153   int score;
154 } MFontScore;
155
156 /** Structure to hold a list of fonts.  */
157
158 typedef struct
159 {
160   MFont object;
161   MFontScore *fonts;
162   int nfonts;
163 } MFontList;
164
165 struct MFontDriver
166 {
167   /** Return a font best matching with FONT.  */
168   MFont *(*select) (MFrame *frame, MFont *font, int limited_size);
169
170   /** Open a font specified by RFONT.  */
171   MRealizedFont *(*open) (MFrame *frame, MFont *font, MFont *spec,
172                           MRealizedFont *rfont);
173
174   /** Set metrics of glyphs in GSTRING from FROM to TO.  */
175   void (*find_metric) (MRealizedFont *rfont, MGlyphString *gstring,
176                        int from, int to);
177
178   /** Check if the font has a glyph for CODE.  CODE is a code point of
179       a character in font->encoder->encoding_charset.  Return nonzero
180       iff the font has the glyph.  */
181   int (*has_char) (MFrame *frame, MFont *font, MFont *spec,
182                    int c, unsigned code);
183
184   /** Encode CODE into a glyph code the font.  CODE is a code point of
185       a character in rfont->encoder->encoding_charset.  If the font
186       has no glyph for CODE, return MCHAR_INVALID_CODE.  */
187   unsigned (*encode_char) (MFrame *frame, MFont *font, MFont *spec,
188                            unsigned code);
189
190   /** Draw glyphs from FROM to TO (exclusive) on window WIN of FRAME
191       at coordinate (X, Y) relative to WIN.  */
192   void (*render) (MDrawWindow win, int x, int y,
193                   MGlyphString *gstring, MGlyph *from, MGlyph *to,
194                   int reverse, MDrawRegion region);
195
196   /** Push to PLIST fonts matching with FONT.  MAXNUM if greater than
197       0 limits the number of listed fonts.  Return the number of fonts
198       listed.  */
199   int (*list) (MFrame *frame, MPlist *plist, MFont *font, int maxnum);
200 };
201
202 /** Initialize the members of FONT.  */
203
204 #define MFONT_INIT(font) memset ((font), 0, sizeof (MFont))
205
206 extern MSymbol Mlayouter;
207 extern MSymbol Miso8859_1, Miso10646_1, Municode_bmp, Municode_full;
208 extern MSymbol Mapple_roman;
209
210 extern int mfont__flt_init ();
211
212 extern void mfont__flt_fini ();
213
214 #ifdef HAVE_FREETYPE
215 #include <ft2build.h>
216 #include FT_FREETYPE_H
217
218 #ifdef HAVE_FONTCONFIG
219 #include <fontconfig/fontconfig.h>
220 #endif
221
222 #ifdef HAVE_OTF
223 #include <otf.h>
224 #else  /* not HAVE_OTF */
225 typedef unsigned OTF_Tag;
226 #endif /* not HAVE_OTF */
227
228 enum MFontOpenTypeTable
229   {
230     MFONT_OTT_GSUB,
231     MFONT_OTT_GPOS,
232     MFONT_OTT_MAX
233   };
234
235 typedef struct
236 {
237   M17NObject control;
238   MSymbol *lang;
239   MSymbol script;
240   OTF_Tag script_tag;
241 #ifdef HAVE_OTF
242   OTF_Tag langsys_tag;
243   struct {
244     char *str;
245     int nfeatures;
246     OTF_Tag *tags;
247   } features[MFONT_OTT_MAX];
248 #endif
249 } MFontCapability;
250
251 extern MFontDriver mfont__ft_driver;
252
253 extern int mfont__ft_init ();
254
255 extern void mfont__ft_fini ();
256
257 extern int mfont__ft_parse_name (char *name, MFont *font);
258
259 extern char *mfont__ft_unparse_name (MFont *font);
260
261 #ifdef HAVE_OTF
262
263 extern int mfont__ft_drive_otf (MGlyphString *gstring, int from, int to,
264                                 MFontCapability *capability);
265
266 extern int mfont__ft_decode_otf (MGlyph *g);
267
268 #endif  /* HAVE_OTF */
269
270 #endif /* HAVE_FREETYPE */
271
272 extern void mfont__free_realized (MRealizedFont *rfont);
273
274 extern int mfont__match_p (MFont *font, MFont *spec, int prop);
275
276 extern int mfont__merge (MFont *dst, MFont *src, int error_on_conflict);
277
278 extern void mfont__set_spec_from_face (MFont *spec, MFace *face);
279
280 extern MSymbol mfont__set_spec_from_plist (MFont *spec, MPlist *plist);
281
282 extern void mfont__resize (MFont *spec, MFont *request);
283
284 extern int mfont__has_char (MFrame *frame, MFont *font, MFont *spec, int c);
285
286 extern unsigned mfont__encode_char (MFrame *frame, MFont *font, MFont *spec,
287                                     int c);
288
289 extern MFont *mfont__select (MFrame *frame, MFont *font, int max_size);
290
291 extern MFontList *mfont__list (MFrame *frame, MFont *spec, MFont *request,
292                                int limited_size);
293
294 extern MRealizedFont *mfont__open (MFrame *frame, MFont *font, MFont *spec);
295
296 extern void mfont__get_metric (MGlyphString *gstring, int from, int to);
297
298 extern void mfont__set_property (MFont *font, enum MFontProperty key,
299                                  MSymbol val);
300
301 extern int mfont__split_name (char *name, int *property_idx,
302                               unsigned short *point, unsigned short *resy);
303
304 extern void mfont__set_spec (MFont *font,
305                              MSymbol attrs[MFONT_PROPERTY_MAX],
306                              unsigned size, unsigned short resy);
307
308 extern int mfont__parse_name_into_font (char *name, MSymbol format,
309                                         MFont *font);
310
311 extern MPlist *mfont__encoding_list (void);
312
313 extern MFontCapability *mfont__get_capability (MSymbol sym);
314
315 extern unsigned mfont__flt_encode_char (MSymbol layouter_name, int c);
316
317 extern int mfont__flt_run (MGlyphString *gstring, int from, int to,
318                            MRealizedFace *rface);
319
320 #endif /* _M17N_FONT_H_ */