Update copyright years
[m17n/m17n-lib.git] / src / font.h
1 /* font.h -- header file for the font module.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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., 51 Franklin Street, Fifth Floor,
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     /* The follwoings should not be used as an index to
39        MFont->propperty.  */
40     MFONT_SIZE,
41     MFONT_SPACING,
42     /* anchor */
43     MFONT_PROPERTY_MAX = MFONT_SIZE
44   };
45
46 enum MFontType
47   {
48     MFONT_TYPE_SPEC,
49     MFONT_TYPE_OBJECT,
50     MFONT_TYPE_REALIZED,
51     MFONT_TYPE_FAILURE
52   };
53
54 enum MFontSource
55   {
56     MFONT_SOURCE_UNDECIDED = 0,
57     MFONT_SOURCE_X = 1,
58     MFONT_SOURCE_FT = 2
59   };
60
61 enum MFontSpacing
62   {
63     MFONT_SPACING_UNDECIDED,
64     MFONT_SPACING_PROPORTIONAL,
65     MFONT_SPACING_MONO,
66     MFONT_SPACING_CHARCELL
67   };
68
69 typedef struct MFontEncoding MFontEncoding;
70 typedef struct MFontDriver MFontDriver;
71
72 /** Information about a font.  This structure is used in three ways:
73     FONT-OBJ, FONT-OPENED, and FONT-SPEC.  
74
75     FONT-OBJ: To store information of an existing font.  Instances
76     appear only in <font_list> of MDisplay.
77
78     FONT-OPENED: To store information of an opened font.  Instances
79     appear only in <opend_list> of MDisplay.
80
81     FONT-SPEC: To store specifications of a font.  Instances appear
82     only in <spec_list> of MFontset.  */
83
84 struct MFont
85 {
86   /** Numeric value of each font property.  Also used as an index to
87       the table @c mfont__property_table to get the actual name of the
88       property.
89
90       For FONT-OBJ, FONT-OPENED: The value is greater than zero.
91
92       For FONT-SPEC: The value is equal to or greater than zero.  Zero
93       means that the correponding property is not specified (i.e. wild
94       card).
95
96       For FONT-OBJ: If the value is 0, the font is scalable or
97       auto-scaled.
98
99       For FONT-OPENED: The actual size of opened font.
100
101       <property>[MFONT_RESY] is the designed resolution of the font in
102       DPI, or zero.  Zero means that the font is scalable.
103
104       For the time being, we mention only Y-resolution (resy) and
105       assume that resx is always equal to resy.  */
106   unsigned short property[MFONT_PROPERTY_MAX];
107   unsigned type : 2;
108   unsigned source : 2;
109   unsigned spacing : 2;
110   unsigned for_full_width : 1;
111   /* For FONT-OBJ, 1 means `size' is a logical or of bit masks for
112      available pixel sizes (Nth bit corresponds to (6 + N) pixels), 0
113      means `size' is an actual pixel size * 10.  For FONT-SPEC and
114      FONT-OPENED, this is always 0, and `size' is an actual pixel
115      size * 10.  */
116   unsigned multiple_sizes : 1;
117   unsigned size : 24;
118   MSymbol file;
119   MSymbol capability;
120   MFontEncoding *encoding;
121 };
122
123 typedef struct
124 {
125   int size, inc, used;
126   MSymbol property;
127   MSymbol *names;
128 } MFontPropertyTable;
129
130 extern MFontPropertyTable mfont__property_table[MFONT_REGISTRY + 1];
131
132 /** Return the symbol of the Nth font property of FONT.  */
133 #define FONT_PROPERTY(font, n)  \
134   (mfont__property_table[(n)].names[(font)->property[(n)]])
135
136 struct MRealizedFont
137 {
138   /* Font spec used to find the font.
139      MRealizedFont::spec.property[MFONT_TYPE] is MFONT_TYPE_REALIZED
140      so that this object can be distingushed from MFont.  */
141   MFont spec;
142
143   /* Font identifier. */
144   MSymbol id;
145
146   /* Frame on which the font is realized.  */
147   MFrame *frame;
148
149   /* The found font.  */
150   MFont *font;
151
152   MFontDriver *driver;
153
154   /* Font Layout Table for the font.  This is just a container to
155      carry the infomation.  */
156   MSymbol layouter;
157
158   /* 1 iff the font is opend by encapsulating client-side font data.  */
159   int encapsulating;
160
161   /* Extra information set by MRealizedFont::driver->open.  If
162      non-NULL, it must be a pointer to a managed object.  */
163   void *info;
164
165   int x_ppem, y_ppem;
166
167   int ascent, descent, max_advance, average_width, baseline_offset;
168
169   /* Pointer to the font structure.  */
170   void *fontp;
171
172   MRealizedFont *next;
173 };
174
175 typedef struct MFLTFontForRealized {
176   MFLTFont font;
177   MRealizedFont *rfont;
178 } MFLTFontForRealized;
179
180 typedef struct {
181   MFont *font;
182   int score;
183 } MFontScore;
184
185 /** Structure to hold a list of fonts.  */
186
187 typedef struct
188 {
189   MFont object;
190   MFontScore *fonts;
191   int nfonts;
192 } MFontList;
193
194 struct MFontDriver
195 {
196   /** Return a font best matching with FONT.  */
197   MFont *(*select) (MFrame *frame, MFont *font, int limited_size);
198
199   /** Open a font specified by RFONT.  */
200   MRealizedFont *(*open) (MFrame *frame, MFont *font, MFont *spec,
201                           MRealizedFont *rfont);
202
203   /** Set metrics of glyphs in GSTRING from FROM to TO.  */
204   void (*find_metric) (MRealizedFont *rfont, MGlyphString *gstring,
205                        int from, int to);
206
207   /** Check if the font has a glyph for CODE.  CODE is a code point of
208       a character in font->encoder->encoding_charset.  Return nonzero
209       iff the font has the glyph.  */
210   int (*has_char) (MFrame *frame, MFont *font, MFont *spec,
211                    int c, unsigned code);
212
213   /** Encode CODE into a glyph code the font.  CODE is a code point of
214       a character in rfont->encoder->encoding_charset.  If the font
215       has no glyph for CODE, return MCHAR_INVALID_CODE.  */
216   unsigned (*encode_char) (MFrame *frame, MFont *font, MFont *spec,
217                            unsigned code);
218
219   /** Draw glyphs from FROM to TO (exclusive) on window WIN of FRAME
220       at coordinate (X, Y) relative to WIN.  */
221   void (*render) (MDrawWindow win, int x, int y,
222                   MGlyphString *gstring, MGlyph *from, MGlyph *to,
223                   int reverse, MDrawRegion region);
224
225   /** Push to PLIST fonts matching with FONT.  MAXNUM if greater than
226       0 limits the number of listed fonts.  Return the number of fonts
227       listed.  */
228   int (*list) (MFrame *frame, MPlist *plist, MFont *font, int maxnum);
229
230   /** Push to PLIST font family names (symbol) available on FRAME.  */
231   void (*list_family_names) (MFrame *frame, MPlist *plist);
232
233   /** Check if RFONT support CAPABILITY.  */
234   int (*check_capability) (MRealizedFont *rfont, MSymbol capability);
235
236   /** Open a font by encapsulating DATA.  */
237   MRealizedFont *(*encapsulate) (MFrame *frame, MSymbol source, void *data);
238
239   void (*close) (MRealizedFont *rfont);
240
241   int (*check_otf) (MFLTFont *font, MFLTOtfSpec *spec);
242
243   int (*drive_otf) (MFLTFont *font, MFLTOtfSpec *spec,
244                     MFLTGlyphString *in, int from, int to,
245                     MFLTGlyphString *out, MFLTGlyphAdjustment *adjustment);
246
247   int (*try_otf) (MFLTFont *font, MFLTOtfSpec *spec,
248                   MFLTGlyphString *in, int from, int to);
249
250   int (*iterate_otf_feature) (struct _MFLTFont *font, MFLTOtfSpec *spec,
251                               int from, int to, unsigned char *table);
252 };
253
254 /** Initialize the members of FONT.  */
255
256 #define MFONT_INIT(font) memset ((font), 0, sizeof (MFont))
257
258 extern MSymbol Mlayouter;
259 extern MSymbol Miso8859_1, Miso10646_1, Municode_bmp, Municode_full;
260 extern MSymbol Mapple_roman;
261
262 extern int mfont__flt_init ();
263
264 extern void mfont__flt_fini ();
265
266 #ifdef HAVE_FREETYPE
267 #include <ft2build.h>
268 #include FT_FREETYPE_H
269 #endif
270
271 #ifdef HAVE_FONTCONFIG
272 #include <fontconfig/fontconfig.h>
273 #endif
274
275 #ifdef HAVE_OTF
276 #include <otf.h>
277 #else  /* not HAVE_OTF */
278 typedef unsigned OTF_Tag;
279 #endif /* not HAVE_OTF */
280
281 enum MFontOpenTypeTable
282   {
283     MFONT_OTT_GSUB,
284     MFONT_OTT_GPOS,
285     MFONT_OTT_MAX
286   };
287
288 typedef struct
289 {
290   M17NObject control;
291   MSymbol language;
292   MSymbol script;
293   MSymbol otf;
294   OTF_Tag script_tag;
295   OTF_Tag langsys_tag;
296   struct {
297     char *str;
298     int nfeatures;
299     OTF_Tag *tags;
300   } features[MFONT_OTT_MAX];
301 } MFontCapability;
302
303 #ifdef HAVE_FREETYPE
304 extern MFontDriver mfont__ft_driver;
305
306 extern int mfont__ft_init ();
307
308 extern void mfont__ft_fini ();
309
310 extern int mfont__ft_parse_name (const char *name, MFont *font);
311
312 extern char *mfont__ft_unparse_name (MFont *font);
313
314 #ifdef HAVE_OTF
315
316 extern int mfont__ft_drive_otf (MGlyphString *gstring, int from, int to,
317                                 MFontCapability *capability);
318
319 extern int mfont__ft_decode_otf (MGlyph *g);
320
321 #endif  /* HAVE_OTF */
322
323 #endif /* HAVE_FREETYPE */
324
325 extern void mfont__free_realized (MRealizedFont *rfont);
326
327 extern int mfont__match_p (MFont *font, MFont *spec, int prop);
328
329 extern int mfont__merge (MFont *dst, MFont *src, int error_on_conflict);
330
331 extern void mfont__set_spec_from_face (MFont *spec, MFace *face);
332
333 extern MSymbol mfont__set_spec_from_plist (MFont *spec, MPlist *plist);
334
335 extern int mfont__has_char (MFrame *frame, MFont *font, MFont *spec, int c);
336
337 extern unsigned mfont__encode_char (MFrame *frame, MFont *font, MFont *spec,
338                                     int c);
339
340 extern int mfont__get_glyph_id (MFLTFont *font, MFLTGlyphString *gstring,
341                                 int from, int to);
342
343 extern MFont *mfont__select (MFrame *frame, MFont *font, int max_size);
344
345 extern MFontList *mfont__list (MFrame *frame, MFont *spec, MFont *request,
346                                int limited_size);
347
348 extern MRealizedFont *mfont__open (MFrame *frame, MFont *font, MFont *spec);
349
350 extern void mfont__get_metric (MGlyphString *gstring, int from, int to);
351
352 extern int mfont__get_metrics (MFLTFont *font, MFLTGlyphString *gstring,
353                                int from, int to);
354
355 extern void mfont__set_property (MFont *font, enum MFontProperty key,
356                                  MSymbol val);
357
358 extern int mfont__split_name (char *name, int *property_idx,
359                               unsigned short *point, unsigned short *resy);
360
361 extern int mfont__parse_name_into_font (const char *name, MSymbol format,
362                                         MFont *font);
363
364 extern MPlist *mfont__encoding_list (void);
365
366 extern MFontCapability *mfont__get_capability (MSymbol sym);
367
368 extern int mfont__check_capability (MRealizedFont *rfont, MSymbol capability);
369
370 extern unsigned mfont__flt_encode_char (MSymbol layouter_name, int c);
371
372 extern int mfont__flt_run (MGlyphString *gstring, int from, int to,
373                            MRealizedFace *rface);
374
375 #endif /* _M17N_FONT_H_ */