2 * m17n-fc.c: Generic shaper using the m17n library for FreeType-based backends
4 Copyright (C) 2004, 2005, 2006
5 National Institute of Advanced Industrial Science and Technology (AIST)
6 Registration Number H16PRO276
8 This file is part of the pango-m17n library.
10 The pango-m17n library is free software; you can redistribute it
11 and/or modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2.1 of the License, or (at your option) any later version.
15 The pango-m17n library is distributed in the hope that it will be
16 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with the pango-m17n library; if not, write to the
22 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 MA 02111-1307, USA. */
28 #include <pango/pango-engine.h>
29 #include <pango/pango-utils.h>
30 #include <pango/pangofc-font.h>
33 #include <m17n-misc.h>
38 #define G_LOG_DOMAIN "Pango-M17N-FC"
40 /* Control printing of debug information. It is initialized from the
41 environment variable PANGO_M17N_FC_DEBUG. Currently, any positive
42 value turns on all debug messages. */
43 static int debug_level;
45 /* Commonly used frame on nulldevice. */
48 /* Commonly used "generic" fontset. */
49 static MFontset *fontset;
51 /* Initialize this module. The main task is to open a frame on
57 const char *envvar = g_getenv ("PANGO_M17N_FC_DEBUG");
61 debug_level = envvar ? atoi (envvar) : 0;
65 fontset = (mdatabase_find (Mfontset, msymbol ("generic"), Mnil, Mnil)
66 ? mfontset ("generic") : mfontset (NULL));
67 mface_put_prop (face, Mfontset, fontset);
68 mplist_put (param, Mface, face);
69 mplist_put (param, Mdevice, Mnil);
70 frame = mframe (param);
71 m17n_object_unref (face);
72 m17n_object_unref (param);
75 /* Finalize this module by freeing frame. */
80 m17n_object_unref (frame);
81 m17n_object_unref (fontset);
84 /* No extra fields needed for these structures. */
85 typedef PangoEngineShape M17NEngineFc;
86 typedef PangoEngineShapeClass M17NEngineFcClass;
88 #define SCRIPT_ENGINE_NAME "M17NScriptEngineFc"
89 #define RENDER_TYPE PANGO_RENDER_TYPE_FC
91 static PangoEngineScriptInfo m17n_scripts[] = {
92 { PANGO_SCRIPT_ARABIC, "*" },
93 { PANGO_SCRIPT_ARMENIAN, "*" },
94 { PANGO_SCRIPT_BENGALI, "*" },
95 { PANGO_SCRIPT_BOPOMOFO, "*" },
96 { PANGO_SCRIPT_CHEROKEE, "*" },
97 { PANGO_SCRIPT_COPTIC, "*" },
98 { PANGO_SCRIPT_CYRILLIC, "*" },
99 { PANGO_SCRIPT_DESERET, "*" },
100 { PANGO_SCRIPT_DEVANAGARI, "*" },
101 { PANGO_SCRIPT_ETHIOPIC, "*" },
102 { PANGO_SCRIPT_GEORGIAN, "*" },
103 { PANGO_SCRIPT_GOTHIC, "*" },
104 { PANGO_SCRIPT_GREEK, "*" },
105 { PANGO_SCRIPT_GUJARATI, "*" },
106 { PANGO_SCRIPT_GURMUKHI, "*" },
107 { PANGO_SCRIPT_HAN, "*" },
108 { PANGO_SCRIPT_HANGUL, "*" },
109 { PANGO_SCRIPT_HEBREW, "*" },
110 { PANGO_SCRIPT_HIRAGANA, "*" },
111 { PANGO_SCRIPT_KANNADA, "*" },
112 { PANGO_SCRIPT_KATAKANA, "*" },
113 { PANGO_SCRIPT_KHMER, "*" },
114 { PANGO_SCRIPT_LAO, "*" },
115 { PANGO_SCRIPT_MALAYALAM, "*" },
116 { PANGO_SCRIPT_MYANMAR, "*" },
117 { PANGO_SCRIPT_OGHAM, "*" },
118 { PANGO_SCRIPT_OLD_ITALIC, "*" },
119 { PANGO_SCRIPT_ORIYA, "*" },
120 { PANGO_SCRIPT_RUNIC, "*" },
121 { PANGO_SCRIPT_SINHALA, "*" },
122 { PANGO_SCRIPT_SYRIAC, "*" },
123 { PANGO_SCRIPT_TAMIL, "*" },
124 { PANGO_SCRIPT_TELUGU, "*" },
125 { PANGO_SCRIPT_THAANA, "*" },
126 { PANGO_SCRIPT_THAI, "*" },
127 { PANGO_SCRIPT_TIBETAN, "*" },
128 { PANGO_SCRIPT_CANADIAN_ABORIGINAL, "*" },
129 { PANGO_SCRIPT_YI, "*" },
131 #ifdef PANGO_SCRIPT_BRAILLE
132 { PANGO_SCRIPT_BRAILLE, "*" },
133 { PANGO_SCRIPT_CYPRIOT, "*" },
134 { PANGO_SCRIPT_LIMBU, "*" },
135 { PANGO_SCRIPT_OSMANYA, "*" },
136 { PANGO_SCRIPT_SHAVIAN, "*" },
137 { PANGO_SCRIPT_LINEAR_B, "*" },
138 { PANGO_SCRIPT_TAI_LE, "*" },
139 { PANGO_SCRIPT_UGARITIC, "*" },
140 #endif /* PANGO_SCRIPT_BRAILLE */
142 #ifdef PANGO_SCRIPT_NEW_TAI_LUE
143 { PANGO_SCRIPT_NEW_TAI_LUE, "*" },
144 { PANGO_SCRIPT_BUGINESE, "*" },
145 { PANGO_SCRIPT_GLAGOLITIC, "*" },
146 { PANGO_SCRIPT_TIFINAGH, "*" },
147 { PANGO_SCRIPT_SYLOTI_NAGRI, "*" },
148 { PANGO_SCRIPT_OLD_PERSIAN, "*" },
149 { PANGO_SCRIPT_KHAROSHTHI, "*" },
150 #endif /* PANGO_SCRIPT_NEW_TAI_LUE */
152 { PANGO_SCRIPT_COMMON, "" }
155 static PangoEngineInfo script_engines[] = {
158 PANGO_ENGINE_TYPE_SHAPE,
160 m17n_scripts, G_N_ELEMENTS (m17n_scripts)
165 m17n_fc_get_family (PangoFont *pango_font)
167 PangoFcFont *pango_fc_font = PANGO_FC_FONT (pango_font);
170 if (FcPatternGetString (pango_fc_font->font_pattern, FC_FAMILY, 0, &family)
172 return (char *) family;
176 /* Convert PANGO_LANGUAGE to m17n-lib's language symbol. If m17n-lib
177 doesn't know about PANGO_LANGUAGE, return Mnil. */
180 m17n_fc_get_language (PangoLanguage *pango_language)
186 const char *lang = pango_language_to_string (pango_language);
188 language = msymbol (lang);
189 if (! msymbol_get (language, Mlanguage))
191 if (strlen (lang) <= 2)
195 /* Remove region part (e.g. "zh_CN" -> "zh"). */
198 shortlang[0] = lang[0], shortlang[1] = lang[1], shortlang[2] = 0;
199 language = msymbol (shortlang);
200 if (! msymbol_get (language, Mlanguage))
211 /* Return a list of FONT-SPECs specified in the current fontset for
212 displaying SCRIPT in LANGUAGE. Callers must unref the returned
216 m17n_fc_lookup_fontset (MSymbol script, MSymbol language)
218 MPlist *plist = mfontset_lookup (fontset, script, language, Mnil);
220 if (mplist_key (plist) == Mnil)
222 /* List up languages that have entries for SCRIPT. */
223 MPlist *ll = mfontset_lookup (fontset, script, Mt, Mnil);
227 /* No entry for SCRIPT. Return fallback FONT-SPECs. */
228 m17n_object_unref (plist);
229 plist = mfontset_lookup (fontset, Mnil, Mnil, Mnil);
233 if (mplist_length (ll) == 1)
235 /* If there's just one language, return FONT-SPECs for it. */
236 m17n_object_unref (plist);
237 language = mplist_key (ll);
240 plist = mfontset_lookup (fontset, script, mplist_key (ll), Mnil);
243 /* Sumup FONT-SPECs for all languages. */
244 for (; mplist_key (ll) != Mnil; ll = mplist_next (ll))
248 language = mplist_key (ll);
251 for (pl = mfontset_lookup (fontset, script, language, Mnil);
252 mplist_key (pl) != Mnil; pl = mplist_next (pl))
253 mplist_add (plist, mplist_key (pl), mplist_value (pl));
263 PangoFcFont *pango_fc_font;
268 m17n_fc_free_font_info (gpointer data)
270 M17NFontInfo *font_info = data;
274 mfont_close (font_info->font);
275 pango_fc_font_unlock_face (font_info->pango_fc_font);
281 m17n_fc_get_font (PangoFont *pango_font)
283 GQuark info_id = g_quark_from_string ("m17n-font-info");
284 M17NFontInfo *font_info;
286 font_info = g_object_get_qdata (G_OBJECT (pango_font), info_id);
289 PangoFcFont *pango_fc_font = PANGO_FC_FONT (pango_font);
290 FT_Face ft_face = pango_fc_font_lock_face (pango_fc_font);
292 font_info = g_new (M17NFontInfo, 1);
293 font_info->pango_fc_font = pango_fc_font;
294 font_info->font = mfont_encapsulate (frame, Mfreetype, ft_face);
295 g_object_set_qdata_full (G_OBJECT (pango_font), info_id, font_info,
296 (GDestroyNotify) m17n_fc_free_font_info);
298 return font_info->font;
302 /* Shaper function. */
305 m17n_fc_engine_shape (PangoEngineShape *engine,
306 PangoFont *pango_font,
309 const PangoAnalysis *analysis,
310 PangoGlyphString *glyphs)
312 /* Symbols for character property `category'. */
313 static MSymbol MZs, MCf;
314 static MDrawControl control;
318 MDrawGlyph *m_glyphs;
325 g_return_if_fail (pango_font != NULL);
326 g_return_if_fail (text != NULL);
327 g_return_if_fail (length >= 0);
328 g_return_if_fail (analysis != NULL);
332 char *msg = alloca (length + 1);
336 memcpy (msg, text, length);
341 g_debug ("shape \"%s\"", msg);
346 MZs = msymbol ("Zs");
347 MCf = msymbol ("Cf");
350 language = m17n_fc_get_language (analysis->language);
352 mt = mtext_from_data (text, length, MTEXT_FORMAT_UTF_8);
353 nchars = mtext_len (mt);
354 offsets = alloca (sizeof (int) * nchars);
355 g_return_if_fail (offsets != NULL);
356 for (i = 0, p = text; i < nchars; i++, p = g_utf8_next_char (p))
357 offsets[i] = p - text;
359 font = m17n_fc_get_font (pango_font);
360 g_return_if_fail (font != NULL);
362 mtext_put_prop (mt, 0, nchars, Mfont, font);
363 if (language != Mnil)
364 mtext_put_prop (mt, 0, nchars, Mlanguage, language);
366 control.enable_bidi = 1;
367 m_glyphs = alloca (sizeof (MDrawGlyph) * nchars * 2);
368 g_return_if_fail (m_glyphs != NULL);
369 if (mdraw_glyph_list (frame, mt, 0, nchars, &control, m_glyphs,
370 nchars * 2, &nglyphs) < 0)
372 m_glyphs = alloca (sizeof (MDrawGlyph) * nglyphs);
373 g_return_if_fail (m_glyphs != NULL);
374 mdraw_glyph_list (frame, mt, 0, nchars, &control, m_glyphs,
378 pango_glyph_string_set_size (glyphs, nglyphs);
380 for (i = 0, g = glyphs->glyphs; i < nglyphs; i++, g++)
382 if (m_glyphs[i].glyph_code >= 0)
384 g->glyph = m_glyphs[i].glyph_code;
385 g->geometry.x_offset = m_glyphs[i].x_off * PANGO_SCALE;
386 g->geometry.y_offset = m_glyphs[i].y_off * PANGO_SCALE;
387 g->geometry.width = m_glyphs[i].x_advance * PANGO_SCALE;
391 int c = mtext_ref_char (mt, m_glyphs[i].from);
392 MSymbol category = mchar_get_prop (c, Mcategory);
393 PangoRectangle logical_rect;
398 g->geometry.width = m_glyphs[i].x_advance * PANGO_SCALE;
400 else if (category == MCf)
403 g->geometry.width = 0;
407 g->glyph = (pango_fc_font_get_unknown_glyph
408 (PANGO_FC_FONT (pango_font), c));
409 pango_font_get_glyph_extents (pango_font, g->glyph, NULL,
411 g->geometry.width = logical_rect.width;
413 g->geometry.x_offset = 0;
414 g->geometry.y_offset = 0;
416 g->attr.is_cluster_start
418 || m_glyphs[i - 1].from != m_glyphs[i].from);
419 glyphs->log_clusters[i] = offsets[m_glyphs[i].from];
422 m17n_object_unref (mt);
425 /* Check if PANGO_FONT is suitable for the character WC. */
427 static PangoCoverageLevel
428 m17n_fc_engine_covers (PangoEngineShape *engine,
429 PangoFont *pango_font,
430 PangoLanguage *pango_language,
433 static gunichar last_wc = 0;
434 /* Symbols for character property `script'. */
435 static MSymbol Mlatin, Mcommon, Minherited;
436 PangoCoverage *coverage;
437 PangoCoverageLevel result;
438 MSymbol script, language;
439 MPlist *spec_list, *p;
444 Mlatin = msymbol ("latin");
445 Mcommon = msymbol ("common");
446 Minherited = msymbol ("inherited");
449 if (debug_level > 0 && wc != last_wc)
451 g_debug ("covers for U+%04X", wc);
455 coverage = pango_font_get_coverage (pango_font, pango_language);
456 result = pango_coverage_get (coverage, wc);
457 pango_coverage_unref (coverage);
459 if (result == PANGO_COVERAGE_NONE)
462 g_debug (" %s none", m17n_fc_get_family (pango_font));
466 language = m17n_fc_get_language (pango_language);
471 script = mchar_get_prop ((int) wc, Mscript);
472 if (script == Mcommon || script == Minherited)
473 script = mchar_get_prop ((int) wc, msymbol ("block"));
476 font = m17n_fc_get_font (pango_font);
477 spec_list = m17n_fc_lookup_fontset (script, language);
479 result = PANGO_COVERAGE_NONE;
480 for (p = spec_list; mplist_key (p) != Mnil; p = mplist_next (p))
481 if (mfont_match_p (font, mplist_value (p)))
483 result = PANGO_COVERAGE_EXACT;
486 m17n_object_unref (spec_list);
488 g_debug (" (%s,%s): %s %s",
489 (script ? msymbol_name (script) : ""),
490 (language ? msymbol_name (language) : ""),
491 m17n_fc_get_family (pango_font),
492 result == PANGO_COVERAGE_EXACT ? "exact" : "none");
498 m17n_engine_fc_class_init (PangoEngineShapeClass *class)
500 class->script_shape = m17n_fc_engine_shape;
501 class->covers = m17n_fc_engine_covers;
504 PANGO_ENGINE_SHAPE_DEFINE_TYPE (M17NEngineFc, m17n_engine_fc,
505 m17n_engine_fc_class_init, NULL)
508 PANGO_MODULE_ENTRY(init) (GTypeModule *module)
510 m17n_engine_fc_register_type (module);
517 PANGO_MODULE_ENTRY(exit) (void)
524 PANGO_MODULE_ENTRY(list) (PangoEngineInfo **engines,
527 *engines = script_engines;
528 *n_engines = G_N_ELEMENTS (script_engines);
532 PANGO_MODULE_ENTRY(create) (const char *id)
534 return (!strcmp (id, SCRIPT_ENGINE_NAME)
535 ? g_object_new (m17n_engine_fc_type, NULL)