2 * m17n-fc.c: Generic shaper using the m17n library for FreeType-based backends
4 Copyright (C) 2004, 2005, 2006, 2007, 2008
5 National Institute of Advanced Industrial Science and Technology (AIST)
6 Registration Number H16PRO276
8 The library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as
10 published by the Free Software Foundation; either version 2.1 of
11 the License, or (at your option) any later version.
13 The pango-m17n library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with the pango-m17n library; if not, write to the
20 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 MA 02111-1307, USA. */
28 #include <pango/pango-ot.h>
29 #include <pango/pango-engine.h>
30 #include <pango/pangofc-font.h>
35 #define G_LOG_DOMAIN "Pango-M17N-FC"
37 /**************************************/
38 /* Standard preamble for Pango module */
39 /**************************************/
41 /* No extra fields needed for these structures. */
42 typedef PangoEngineShape M17NEngineFc;
43 typedef PangoEngineShapeClass M17NEngineFcClass;
45 #define SCRIPT_ENGINE_NAME "M17NScriptEngineFc"
46 #define RENDER_TYPE PANGO_RENDER_TYPE_FC
48 static PangoEngineScriptInfo m17n_scripts[] = {
49 { PANGO_SCRIPT_ARABIC, "*" },
50 { PANGO_SCRIPT_ARMENIAN, "*" },
51 { PANGO_SCRIPT_BENGALI, "*" },
52 { PANGO_SCRIPT_BOPOMOFO, "*" },
53 { PANGO_SCRIPT_CHEROKEE, "*" },
54 { PANGO_SCRIPT_COPTIC, "*" },
55 { PANGO_SCRIPT_CYRILLIC, "*" },
56 { PANGO_SCRIPT_DESERET, "*" },
57 { PANGO_SCRIPT_DEVANAGARI, "*" },
58 { PANGO_SCRIPT_ETHIOPIC, "*" },
59 { PANGO_SCRIPT_GEORGIAN, "*" },
60 { PANGO_SCRIPT_GOTHIC, "*" },
61 { PANGO_SCRIPT_GREEK, "*" },
62 { PANGO_SCRIPT_GUJARATI, "*" },
63 { PANGO_SCRIPT_GURMUKHI, "*" },
64 { PANGO_SCRIPT_HAN, "*" },
65 { PANGO_SCRIPT_HANGUL, "*" },
66 { PANGO_SCRIPT_HEBREW, "*" },
67 { PANGO_SCRIPT_HIRAGANA, "*" },
68 { PANGO_SCRIPT_KANNADA, "*" },
69 { PANGO_SCRIPT_KATAKANA, "*" },
70 { PANGO_SCRIPT_KHMER, "*" },
71 { PANGO_SCRIPT_LAO, "*" },
72 { PANGO_SCRIPT_MALAYALAM, "*" },
73 { PANGO_SCRIPT_MYANMAR, "*" },
74 { PANGO_SCRIPT_OGHAM, "*" },
75 { PANGO_SCRIPT_OLD_ITALIC, "*" },
76 { PANGO_SCRIPT_ORIYA, "*" },
77 { PANGO_SCRIPT_RUNIC, "*" },
78 { PANGO_SCRIPT_SINHALA, "*" },
79 { PANGO_SCRIPT_SYRIAC, "*" },
80 { PANGO_SCRIPT_TAMIL, "*" },
81 { PANGO_SCRIPT_TELUGU, "*" },
82 { PANGO_SCRIPT_THAANA, "*" },
83 { PANGO_SCRIPT_THAI, "*" },
84 { PANGO_SCRIPT_TIBETAN, "*" },
85 { PANGO_SCRIPT_CANADIAN_ABORIGINAL, "*" },
86 { PANGO_SCRIPT_YI, "*" },
88 { PANGO_SCRIPT_BRAILLE, "*" },
89 { PANGO_SCRIPT_CYPRIOT, "*" },
90 { PANGO_SCRIPT_LIMBU, "*" },
91 { PANGO_SCRIPT_OSMANYA, "*" },
92 { PANGO_SCRIPT_SHAVIAN, "*" },
93 { PANGO_SCRIPT_LINEAR_B, "*" },
94 { PANGO_SCRIPT_TAI_LE, "*" },
95 { PANGO_SCRIPT_UGARITIC, "*" },
97 { PANGO_SCRIPT_NEW_TAI_LUE, "*" },
98 { PANGO_SCRIPT_BUGINESE, "*" },
99 { PANGO_SCRIPT_GLAGOLITIC, "*" },
100 { PANGO_SCRIPT_TIFINAGH, "*" },
101 { PANGO_SCRIPT_SYLOTI_NAGRI, "*" },
102 { PANGO_SCRIPT_OLD_PERSIAN, "*" },
103 { PANGO_SCRIPT_KHAROSHTHI, "*" },
104 { PANGO_SCRIPT_COMMON, "" }
107 static PangoEngineInfo script_engines[] = {
110 PANGO_ENGINE_TYPE_SHAPE,
112 m17n_scripts, G_N_ELEMENTS (m17n_scripts)
116 /***************************/
117 /* Interface with m17n-flt */
118 /***************************/
120 #include <m17n-flt.h>
121 #include <m17n-misc.h>
123 /* Return a pointer to the IDXth glyph in GSTRING. */
124 #define GREF(GSTRING, IDX) ((GSTRING)->glyphs + (IDX))
126 /* Convert Pango scale to 26.6 fractional pixel. */
127 #define PANGO_SCALE_TO_26_6(N) ((N) / (PANGO_SCALE) * (1<<6))
129 /* Convert 26.6 fractional pixel to Pango scale. */
130 #define PANGO_SCALE_FROM_26_6(N) ((N) * (PANGO_SCALE) / (1<<6))
132 /* Control printing of debug information. It is initialized from the
133 environment variable PANGO_M17N_FC_DEBUG. Currently, any positive
134 value turns on all debug messages. */
135 static int debug_level;
138 /* Structure containing information about OT features decoded from
142 /* Rulesets for GSUB (1st element) and GPOS (2nd element). */
143 PangoOTRuleset *ruleset[2];
147 /* Structure containing information about a font. */
151 PangoFcFont *pango_font;
153 /* Cache that maps MFLTOtfSpec->sym to FeatureInfo. */
154 MPlist *otf_spec_cache;
158 /* Prototypes for callback functions of FontInfo.font. */
160 static int get_glyph_id (MFLTFont *, MFLTGlyphString *, int, int);
161 static int get_metrics (MFLTFont *, MFLTGlyphString *, int, int);
162 static int check_otf (MFLTFont *, MFLTOtfSpec *);
163 static int drive_otf (MFLTFont *, MFLTOtfSpec *,
164 MFLTGlyphString *, int, int,
165 MFLTGlyphString *, MFLTGlyphAdjustment *);
168 /* This function works in two ways.
170 If INFO is NULL, it checks if the features specified in SPEC match
171 with the OpenType font in OT_INFO, and return 1 (match) or 0 (no
174 If INFO is non-NULL, it creates a PangoOTRuleset object from SPEC
175 and stores it in INFO. In this case, non-existing features in SPEC
178 Features in SPEC are stored in an array in this format:
179 P P ... [ 0xFFFFFFFF [ N N ... ] ]
181 Ps are positive features that should be in the font (INFO is
182 NULL), or should be applied in this order (INFO is non-NULL),
184 0xFFFFFFFF is a separater between Ps and Ns (INFO is NULL), or
185 is a wildcard meaning that all the other features (except for
186 Ns) must be applied (INFO is non-NULL),
188 Ns are negative features that should not be in the font (INFO
189 is NULL), or should not be applied (INFO is non-NULL). */
192 otf_check_features (PangoOTInfo *ot_info, PangoOTTableType type,
193 MFLTOtfSpec *spec, FeatureInfo *info)
195 unsigned int *features;
196 unsigned int langsys_tag;
197 guint script_idx, langsys_idx;
201 /* Used as an index to spec->features[] and info->ruleset[]. */
202 int for_gpos = type == PANGO_OT_TABLE_GPOS;
204 features = spec->features[for_gpos];
207 if (! pango_ot_info_find_script (ot_info, type, spec->script, &script_idx))
208 /* No feature for the specified script exist. If the first
209 feature in SPEC is a wildcard, the remaining features are all
210 negative, so we can return 1. */
211 return (features[0] == 0xFFFFFFFF);
212 langsys_tag = spec->langsys;
214 langsys_tag = PANGO_OT_DEFAULT_LANGUAGE;
215 pango_ot_info_find_language (ot_info, type, script_idx,
216 langsys_tag, &langsys_idx, &index);
220 info->ruleset[for_gpos] = pango_ot_ruleset_new (ot_info);
221 pango_ot_ruleset_add_feature (info->ruleset[for_gpos], type,
222 index, PANGO_OT_ALL_GLYPHS);
225 for (i = 0; features[i] && features[i] != 0xFFFFFFFF; i++)
227 if (pango_ot_info_find_feature (ot_info, type, features[i],
228 script_idx, langsys_idx, &index))
232 if (! info->ruleset[for_gpos])
233 info->ruleset[for_gpos] = pango_ot_ruleset_new (ot_info);
234 pango_ot_ruleset_add_feature (info->ruleset[for_gpos], type,
235 index, PANGO_OT_ALL_GLYPHS);
245 /* No wildcard, no negative features. */
248 feature_list = pango_ot_info_list_features (ot_info, type, 0,
249 script_idx, langsys_idx);
251 /* No feature for the specified script/language exist. As the
252 remaining features are all negative, we can return 1. */
257 /* Check if the remaining features don't appear in
259 for (i++; features[i]; i++)
260 for (j = 0; feature_list[j]; j++)
261 if (features[i] == feature_list[j])
266 /* Add all features in FEATURE_LIST except for those appearing in
268 for (i = 0; feature_list[i]; i++)
270 guint tag = feature_list[i];
272 for (j = 0; features[j] && tag != features[j]; j++);
275 pango_ot_info_find_feature (ot_info, type, tag,
276 script_idx, langsys_idx, &index);
277 if (! info->ruleset[for_gpos])
278 info->ruleset[for_gpos] = pango_ot_ruleset_new (ot_info);
279 pango_ot_ruleset_add_feature (info->ruleset[for_gpos], type, index,
280 PANGO_OT_ALL_GLYPHS);
287 /* Return a FeatureInfo object (newly created if necessary) for the
288 font in FONT_INFO and for the OpenType feature specification in
292 get_feature_info (FontInfo *font_info, MFLTOtfSpec *spec)
294 FeatureInfo *feature_info;
295 PangoOTInfo *ot_info;
297 if (! spec->features[0] && ! spec->features[1])
299 if (font_info->otf_spec_cache)
301 feature_info = mplist_get (font_info->otf_spec_cache, spec->sym);
306 font_info->otf_spec_cache = mplist ();
307 feature_info = g_new0 (FeatureInfo, 1);
308 mplist_push (font_info->otf_spec_cache, spec->sym, feature_info);
309 ot_info = pango_ot_info_get (font_info->face);
310 otf_check_features (ot_info, PANGO_OT_TABLE_GSUB, spec, feature_info);
311 otf_check_features (ot_info, PANGO_OT_TABLE_GPOS, spec, feature_info);
317 /* Free the object pointed by FONT_INFO. */
320 free_font_info (FontInfo *font_info)
322 if (font_info->otf_spec_cache)
326 for (p = font_info->otf_spec_cache; mplist_key (p) != Mnil;
329 FeatureInfo *feature_info = mplist_value (p);
331 if (feature_info->ruleset[0])
332 g_object_unref (feature_info->ruleset[0]);
333 if (feature_info->ruleset[1])
334 g_object_unref (feature_info->ruleset[1]);
336 m17n_object_unref (font_info->otf_spec_cache);
338 pango_fc_font_unlock_face (font_info->pango_font);
343 /* Return a FontInfo object corresponding to FONT. It is newly
344 created if necessary. */
347 get_font_info (PangoFont *font)
350 static GQuark info_id = 0;
352 if (G_UNLIKELY (! info_id))
353 info_id = g_quark_from_string ("FontInfo");
354 font_info = g_object_get_qdata (G_OBJECT (font), info_id);
355 if (G_UNLIKELY (! font_info))
357 PangoFontDescription *desc;
358 const char *family_name;
362 font_info = g_new0 (FontInfo, 1);
363 font_info->pango_font = PANGO_FC_FONT (font);
364 font_info->face = pango_fc_font_lock_face (font_info->pango_font);
365 desc = pango_font_describe_with_absolute_size (font);
366 family_name = pango_font_description_get_family (desc);
369 len = strlen (family_name);
370 buf = g_alloca (len + 1);
371 for (i = 0; i < len + 1; i++)
372 buf[i] = tolower (family_name[i]);
373 font_info->font.family = msymbol (buf);
375 font_info->font.x_ppem = font_info->font.y_ppem
376 = PANGO_SCALE_TO_26_6 (pango_font_description_get_size (desc));
377 pango_font_description_free (desc);
378 font_info->font.check_otf = check_otf;
379 font_info->font.get_glyph_id = get_glyph_id;
380 font_info->font.get_metrics = get_metrics;
381 font_info->font.drive_otf = drive_otf;
382 g_object_set_qdata_full (G_OBJECT (font), info_id,
383 font_info, (GDestroyNotify) free_font_info);
388 /* Four callback functions for MFLTFont. */
391 get_glyph_id (MFLTFont *font, MFLTGlyphString *gstring, int from, int to)
393 FontInfo *font_info = (FontInfo *) font;
394 MFLTGlyph *g = gstring->glyphs + from, *g_end = g + (to - from);
397 for (; g < g_end; g++)
401 g->code = pango_fc_font_get_glyph (font_info->pango_font, g->code);
410 get_metrics (MFLTFont *font, MFLTGlyphString *gstring, int from, int to)
412 FontInfo *font_info = (FontInfo *) font;
413 MFLTGlyph *g = gstring->glyphs + from, *g_end = g + (to - from);
416 for (; g < g_end; g++)
420 PangoRectangle inc, logical;
423 g->code = pango_fc_font_get_glyph (font_info->pango_font, g->code);
424 pango_font_get_glyph_extents (PANGO_FONT (font_info->pango_font),
425 g->code, &inc, &logical);
426 g->lbearing = PANGO_SCALE_TO_26_6 (inc.x);
427 g->rbearing = PANGO_SCALE_TO_26_6 (inc.x + inc.width);
428 g->xadv = PANGO_SCALE_TO_26_6 (logical.width);
430 g->ascent = - PANGO_SCALE_TO_26_6 (inc.y);
431 g->descent = PANGO_SCALE_TO_26_6 (inc.height + inc.y);
440 check_otf (MFLTFont *font, MFLTOtfSpec *spec)
442 FontInfo *font_info = (FontInfo *) font;
443 PangoOTInfo *ot_info = pango_ot_info_get (font_info->face);
447 if (! otf_check_features (ot_info, PANGO_OT_TABLE_GSUB, spec, NULL)
448 || ! otf_check_features (ot_info, PANGO_OT_TABLE_GPOS, spec, NULL))
454 drive_otf (MFLTFont *font, MFLTOtfSpec *spec,
455 MFLTGlyphString *in, int from, int to,
456 MFLTGlyphString *out, MFLTGlyphAdjustment *adjustment)
458 FontInfo *font_info = (FontInfo *) font;
459 PangoOTBuffer *buffer;
460 FeatureInfo *feature_info;
461 PangoGlyphString *glyphs;
464 buffer = pango_ot_buffer_new (font_info->pango_font);
465 for (i = from; i < to; i++)
466 pango_ot_buffer_add_glyph (buffer, in->glyphs[i].code, 0, i);
467 feature_info = get_feature_info (font_info, spec);
470 if (feature_info->ruleset[0])
471 pango_ot_ruleset_substitute (feature_info->ruleset[0], buffer);
472 if (feature_info->ruleset[1])
473 pango_ot_ruleset_position (feature_info->ruleset[1], buffer);
474 glyphs = pango_glyph_string_new ();
475 pango_ot_buffer_output (buffer, glyphs);
476 for (i = 0; i < glyphs->num_glyphs; i++)
478 PangoGlyphInfo *glyph_info = glyphs->glyphs + i;
481 out->glyphs[out->used] = in->glyphs[glyphs->log_clusters[i]];
482 g = out->glyphs + out->used++;
483 if (g->code != glyph_info->glyph)
486 g->code = glyph_info->glyph;
489 g->xoff = PANGO_SCALE_TO_26_6 (glyph_info->geometry.x_offset);
490 g->yoff = PANGO_SCALE_TO_26_6 (glyph_info->geometry.y_offset);
491 g->xadv = PANGO_SCALE_TO_26_6 (glyph_info->geometry.width);
493 adjustment[i].set = 0;
495 pango_ot_buffer_destroy (buffer);
496 pango_glyph_string_free (glyphs);
500 pango_ot_buffer_destroy (buffer);
501 for (i = from; i < to; i++)
502 out->glyphs[out->used++] = in->glyphs[i];
507 /* Shaper function. */
509 /* Shape TEXT of length GSTRING->used by a font in FONT_INFO, and
510 store the result in GLYPHS. GSTRING->glyphs is not yet allocated
511 but GSTRING->allocated tells how many glyphs to allocate. */
514 shape_text (const char *text, int *offsets, MFLTGlyphString *gstring,
515 FontInfo *font_info, PangoGlyphString *glyphs)
521 gstring->glyphs = g_newa (MFLTGlyph, gstring->allocated);
522 for (i = 0; i < gstring->used; i++)
523 gstring->glyphs[i].c = g_utf8_get_char (text + offsets[i]);
524 i = mflt_run (gstring, 0, gstring->used, (MFLTFont *) font_info, NULL);
528 pango_glyph_string_set_size (glyphs, gstring->used);
530 for (i = 0, g = glyphs->glyphs, mg = gstring->glyphs; i < gstring->used;
534 g->geometry.x_offset = PANGO_SCALE_FROM_26_6 (mg->xoff);
535 g->geometry.y_offset = PANGO_SCALE_FROM_26_6 (mg->yoff);
536 g->geometry.width = PANGO_SCALE_FROM_26_6 (mg->xadv);
537 g->attr.is_cluster_start = (i == 0 || mg->from != mg[-1].from);
538 glyphs->log_clusters[i]
539 = (g->attr.is_cluster_start ? offsets[mg->from]
540 : glyphs->log_clusters[i - 1]);
546 m17n_fc_engine_shape (PangoEngineShape *engine,
550 const PangoAnalysis *analysis,
551 PangoGlyphString *glyphs)
554 MFLTGlyphString gstring;
556 const char *p, *pend;
559 g_return_if_fail (font != NULL);
560 g_return_if_fail (text != NULL);
561 g_return_if_fail (length >= 0);
562 g_return_if_fail (analysis != NULL);
564 font_info = get_font_info (font);
566 gstring.glyph_size = sizeof (MFLTGlyph);
568 gstring.r2l = analysis->level % 2;
570 offsets = g_newa (int, length);
571 for (i = 0, p = text, pend = text + length; p < pend;
572 i++, p = g_utf8_next_char (p))
573 offsets[i] = p - text;
576 /* Try at most three times, each time with larger GSTRING->glyphs. */
577 gstring.allocated = length;
578 for (i = 0; i < 3; i++, gstring.allocated += length)
580 int result = shape_text (text, offsets, &gstring, font_info, glyphs);
584 g_return_if_fail (result >= 0);
592 /* Check if FONT is suitable for the character WC. For the moment,
593 this is commented out because even if it returns
594 PANGO_COVERAGE_NONE, the other engine will use the font. In
595 addition, the caller doesn't handle PANGO_COVERAGE_APPROXIMATE and
596 PANGO_COVERAGE_FALLBACK well. */
598 static PangoCoverageLevel
599 m17n_fc_engine_covers (PangoEngineShape *engine,
601 PangoLanguage *language,
604 FontInfo *font_info = get_font_info (font);
605 MFLT *flt = mflt_find (wc, (MFLTFont *) font_info);
606 const char *lang = pango_language_to_string (language);
611 g_debug (" %04X (%s/%s): no FLT\n", wc, lang,
612 msymbol_name (font_info->font.family));
613 return PANGO_COVERAGE_NONE;
616 g_debug (" %04X (%s/%s): FLT %s found\n", wc, lang,
617 msymbol_name (font_info->font.family), mflt_name (flt));
618 return PANGO_COVERAGE_EXACT;
624 m17n_engine_fc_class_init (PangoEngineShapeClass *class)
626 class->script_shape = m17n_fc_engine_shape;
628 class->covers = m17n_fc_engine_covers;
632 PANGO_ENGINE_SHAPE_DEFINE_TYPE (M17NEngineFc, m17n_engine_fc,
633 m17n_engine_fc_class_init, NULL)
636 PANGO_MODULE_ENTRY(init) (GTypeModule *module)
638 m17n_engine_fc_register_type (module);
640 if (getenv ("PANGO_M17N_DEBUG") != NULL)
645 PANGO_MODULE_ENTRY(exit) (void)
651 PANGO_MODULE_ENTRY(list) (PangoEngineInfo **engines,
654 *engines = script_engines;
655 *n_engines = G_N_ELEMENTS (script_engines);
659 PANGO_MODULE_ENTRY(create) (const char *id)
661 return (!strcmp (id, SCRIPT_ENGINE_NAME)
662 ? g_object_new (m17n_engine_fc_type, NULL)