(mface__realize): Handle the case of font->size < 0.
[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     /* 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   enum MFontType type : 2;
108   enum MFontSource source : 2;
109   enum MFontSpacing spacing : 2;
110   /* Size of the font in 1/10 pixels.  */
111   unsigned size : 26;
112   MSymbol file;
113   MSymbol capability;
114   MFontEncoding *encoding;
115 };
116
117 typedef struct
118 {
119   int size, inc, used;
120   MSymbol property;
121   MSymbol *names;
122 } MFontPropertyTable;
123
124 extern MFontPropertyTable mfont__property_table[MFONT_REGISTRY + 1];
125
126 /** Return the symbol of the Nth font property of FONT.  */
127 #define FONT_PROPERTY(font, n)  \
128   (mfont__property_table[(n)].names[(font)->property[(n)]])
129
130 struct MRealizedFont
131 {
132   /* Font spec used to find the font.
133      MRealizedFont::spec.property[MFONT_TYPE] is MFONT_TYPE_REALIZED
134      so that this object can be distingushed from MFont.  */
135   MFont spec;
136
137   /* Frame on which the font is realized.  */
138   MFrame *frame;
139
140   /* The found font.  */
141   MFont *font;
142
143   MFontDriver *driver;
144
145   /* Font Layout Table for the font.  This is just a container to
146      carry the infomation.  */
147   MSymbol layouter;
148
149   /* Extra information set by MRealizedFont::driver->open.  If
150      non-NULL, it must be a pointer to a managed object.  */
151   void *info;
152
153   int ascent, descent, max_advance;
154
155   /* Pointer to the font structure.  */
156   void *fontp;
157
158   MRealizedFont *next;
159 };
160
161 typedef struct {
162   MFont *font;
163   int score;
164 } MFontScore;
165
166 /** Structure to hold a list of fonts.  */
167
168 typedef struct
169 {
170   MFont object;
171   MFontScore *fonts;
172   int nfonts;
173 } MFontList;
174
175 struct MFontDriver
176 {
177   /** Return a font best matching with FONT.  */
178   MFont *(*select) (MFrame *frame, MFont *font, int limited_size);
179
180   /** Open a font specified by RFONT.  */
181   MRealizedFont *(*open) (MFrame *frame, MFont *font, MFont *spec,
182                           MRealizedFont *rfont);
183
184   /** Set metrics of glyphs in GSTRING from FROM to TO.  */
185   void (*find_metric) (MRealizedFont *rfont, MGlyphString *gstring,
186                        int from, int to);
187
188   /** Check if the font has a glyph for CODE.  CODE is a code point of
189       a character in font->encoder->encoding_charset.  Return nonzero
190       iff the font has the glyph.  */
191   int (*has_char) (MFrame *frame, MFont *font, MFont *spec,
192                    int c, unsigned code);
193
194   /** Encode CODE into a glyph code the font.  CODE is a code point of
195       a character in rfont->encoder->encoding_charset.  If the font
196       has no glyph for CODE, return MCHAR_INVALID_CODE.  */
197   unsigned (*encode_char) (MFrame *frame, MFont *font, MFont *spec,
198                            unsigned code);
199
200   /** Draw glyphs from FROM to TO (exclusive) on window WIN of FRAME
201       at coordinate (X, Y) relative to WIN.  */
202   void (*render) (MDrawWindow win, int x, int y,
203                   MGlyphString *gstring, MGlyph *from, MGlyph *to,
204                   int reverse, MDrawRegion region);
205
206   /** Push to PLIST fonts matching with FONT.  MAXNUM if greater than
207       0 limits the number of listed fonts.  Return the number of fonts
208       listed.  */
209   int (*list) (MFrame *frame, MPlist *plist, MFont *font, int maxnum);
210 };
211
212 /** Initialize the members of FONT.  */
213
214 #define MFONT_INIT(font) memset ((font), 0, sizeof (MFont))
215
216 extern MSymbol Mlayouter;
217 extern MSymbol Miso8859_1, Miso10646_1, Municode_bmp, Municode_full;
218 extern MSymbol Mapple_roman;
219
220 extern int mfont__flt_init ();
221
222 extern void mfont__flt_fini ();
223
224 #ifdef HAVE_FREETYPE
225 #include <ft2build.h>
226 #include FT_FREETYPE_H
227
228 #ifdef HAVE_FONTCONFIG
229 #include <fontconfig/fontconfig.h>
230 #endif
231
232 #ifdef HAVE_OTF
233 #include <otf.h>
234 #else  /* not HAVE_OTF */
235 typedef unsigned OTF_Tag;
236 #endif /* not HAVE_OTF */
237
238 enum MFontOpenTypeTable
239   {
240     MFONT_OTT_GSUB,
241     MFONT_OTT_GPOS,
242     MFONT_OTT_MAX
243   };
244
245 typedef struct
246 {
247   M17NObject control;
248   MSymbol *lang;
249   MSymbol script;
250   OTF_Tag script_tag;
251 #ifdef HAVE_OTF
252   OTF_Tag langsys_tag;
253   struct {
254     char *str;
255     int nfeatures;
256     OTF_Tag *tags;
257   } features[MFONT_OTT_MAX];
258 #endif
259 } MFontCapability;
260
261 extern MFontDriver mfont__ft_driver;
262
263 extern int mfont__ft_init ();
264
265 extern void mfont__ft_fini ();
266
267 extern int mfont__ft_parse_name (const char *name, MFont *font);
268
269 extern char *mfont__ft_unparse_name (MFont *font);
270
271 #ifdef HAVE_OTF
272
273 extern int mfont__ft_drive_otf (MGlyphString *gstring, int from, int to,
274                                 MFontCapability *capability);
275
276 extern int mfont__ft_decode_otf (MGlyph *g);
277
278 #endif  /* HAVE_OTF */
279
280 #endif /* HAVE_FREETYPE */
281
282 extern void mfont__free_realized (MRealizedFont *rfont);
283
284 extern int mfont__match_p (MFont *font, MFont *spec, int prop);
285
286 extern int mfont__merge (MFont *dst, MFont *src, int error_on_conflict);
287
288 extern void mfont__set_spec_from_face (MFont *spec, MFace *face);
289
290 extern MSymbol mfont__set_spec_from_plist (MFont *spec, MPlist *plist);
291
292 extern void mfont__resize (MFont *spec, MFont *request);
293
294 extern int mfont__has_char (MFrame *frame, MFont *font, MFont *spec, int c);
295
296 extern unsigned mfont__encode_char (MFrame *frame, MFont *font, MFont *spec,
297                                     int c);
298
299 extern MFont *mfont__select (MFrame *frame, MFont *font, int max_size);
300
301 extern MFontList *mfont__list (MFrame *frame, MFont *spec, MFont *request,
302                                int limited_size);
303
304 extern MRealizedFont *mfont__open (MFrame *frame, MFont *font, MFont *spec);
305
306 extern void mfont__get_metric (MGlyphString *gstring, int from, int to);
307
308 extern void mfont__set_property (MFont *font, enum MFontProperty key,
309                                  MSymbol val);
310
311 extern int mfont__split_name (char *name, int *property_idx,
312                               unsigned short *point, unsigned short *resy);
313
314 extern int mfont__parse_name_into_font (const char *name, MSymbol format,
315                                         MFont *font);
316
317 extern MPlist *mfont__encoding_list (void);
318
319 extern MFontCapability *mfont__get_capability (MSymbol sym);
320
321 extern unsigned mfont__flt_encode_char (MSymbol layouter_name, int c);
322
323 extern int mfont__flt_run (MGlyphString *gstring, int from, int to,
324                            MRealizedFace *rface);
325
326 #endif /* _M17N_FONT_H_ */