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_UNKNOWN, "*" },
154 { PANGO_SCRIPT_COMMON, "" }
157 static PangoEngineInfo script_engines[] = {
160 PANGO_ENGINE_TYPE_SHAPE,
162 m17n_scripts, G_N_ELEMENTS (m17n_scripts)
167 m17n_fc_get_family (PangoFont *pango_font)
169 PangoFcFont *pango_fc_font = PANGO_FC_FONT (pango_font);
172 if (FcPatternGetString (pango_fc_font->font_pattern, FC_FAMILY, 0, &family)
174 return (char *) family;
178 /* Convert PANGO_LANGUAGE to m17n-lib's language symbol. If m17n-lib
179 doesn't know about PANGO_LANGUAGE, return Mnil. */
182 m17n_fc_get_language (PangoLanguage *pango_language)
188 const char *lang = pango_language_to_string (pango_language);
190 language = msymbol (lang);
191 if (! msymbol_get (language, Mlanguage))
193 if (strlen (lang) <= 2)
197 /* Remove region part (e.g. "zh_CN" -> "zh"). */
200 shortlang[0] = lang[0], shortlang[1] = lang[1], shortlang[2] = 0;
201 language = msymbol (shortlang);
202 if (! msymbol_get (language, Mlanguage))
213 /* Return a list of FONT-SPECs specified in the current fontset for
214 displaying SCRIPT in LANGUAGE. Callers must unref the returned
218 m17n_fc_lookup_fontset (MSymbol script, MSymbol language)
220 MPlist *plist = mfontset_lookup (fontset, script, language, Mnil);
222 if (mplist_key (plist) == Mnil)
224 /* List up languages that have entries for SCRIPT. */
225 MPlist *ll = mfontset_lookup (fontset, script, Mt, Mnil);
229 /* No entry for SCRIPT. Return fallback FONT-SPECs. */
230 m17n_object_unref (plist);
231 plist = mfontset_lookup (fontset, Mnil, Mnil, Mnil);
235 if (mplist_length (ll) == 1)
237 /* If there's just one language, return FONT-SPECs for it. */
238 m17n_object_unref (plist);
239 language = mplist_key (ll);
242 plist = mfontset_lookup (fontset, script, mplist_key (ll), Mnil);
245 /* Sumup FONT-SPECs for all languages. */
246 for (; mplist_key (ll) != Mnil; ll = mplist_next (ll))
250 language = mplist_key (ll);
253 for (pl = mfontset_lookup (fontset, script, language, Mnil);
254 mplist_key (pl) != Mnil; pl = mplist_next (pl))
255 mplist_add (plist, mplist_key (pl), mplist_value (pl));
265 PangoFcFont *pango_fc_font;
270 m17n_fc_free_font_info (gpointer data)
272 M17NFontInfo *font_info = data;
276 mfont_close (font_info->font);
277 pango_fc_font_unlock_face (font_info->pango_fc_font);
283 m17n_fc_get_font (PangoFont *pango_font)
285 GQuark info_id = g_quark_from_string ("m17n-font-info");
286 M17NFontInfo *font_info;
288 font_info = g_object_get_qdata (G_OBJECT (pango_font), info_id);
291 PangoFcFont *pango_fc_font = PANGO_FC_FONT (pango_font);
292 FT_Face ft_face = pango_fc_font_lock_face (pango_fc_font);
294 font_info = g_new (M17NFontInfo, 1);
295 font_info->pango_fc_font = pango_fc_font;
296 font_info->font = mfont_encapsulate (frame, Mfreetype, ft_face);
297 g_object_set_qdata_full (G_OBJECT (pango_font), info_id, font_info,
298 (GDestroyNotify) m17n_fc_free_font_info);
300 return font_info->font;
304 /* Shaper function. */
307 m17n_fc_engine_shape (PangoEngineShape *engine,
308 PangoFont *pango_font,
311 const PangoAnalysis *analysis,
312 PangoGlyphString *glyphs)
314 /* Symbols for character property `category'. */
315 static MSymbol MZs, MCf;
316 static MDrawControl control;
320 MDrawGlyph *m_glyphs;
327 g_return_if_fail (pango_font != NULL);
328 g_return_if_fail (text != NULL);
329 g_return_if_fail (length >= 0);
330 g_return_if_fail (analysis != NULL);
334 char *msg = alloca (length + 1);
338 memcpy (msg, text, length);
343 g_debug ("shape \"%s\"", msg);
348 MZs = msymbol ("Zs");
349 MCf = msymbol ("Cf");
352 language = m17n_fc_get_language (analysis->language);
354 mt = mtext_from_data (text, length, MTEXT_FORMAT_UTF_8);
355 nchars = mtext_len (mt);
356 offsets = alloca (sizeof (int) * nchars);
357 g_return_if_fail (offsets != NULL);
358 for (i = 0, p = text; i < nchars; i++, p = g_utf8_next_char (p))
359 offsets[i] = p - text;
361 font = m17n_fc_get_font (pango_font);
362 g_return_if_fail (font != NULL);
364 mtext_put_prop (mt, 0, nchars, Mfont, font);
365 if (language != Mnil)
366 mtext_put_prop (mt, 0, nchars, Mlanguage, language);
368 control.enable_bidi = 1;
369 m_glyphs = alloca (sizeof (MDrawGlyph) * nchars * 2);
370 g_return_if_fail (m_glyphs != NULL);
371 if (mdraw_glyph_list (frame, mt, 0, nchars, &control, m_glyphs,
372 nchars * 2, &nglyphs) < 0)
374 m_glyphs = alloca (sizeof (MDrawGlyph) * nglyphs);
375 g_return_if_fail (m_glyphs != NULL);
376 mdraw_glyph_list (frame, mt, 0, nchars, &control, m_glyphs,
380 pango_glyph_string_set_size (glyphs, nglyphs);
382 for (i = 0, g = glyphs->glyphs; i < nglyphs; i++, g++)
384 if (m_glyphs[i].glyph_code >= 0)
386 g->glyph = m_glyphs[i].glyph_code;
387 g->geometry.x_offset = m_glyphs[i].x_off * PANGO_SCALE;
388 g->geometry.y_offset = m_glyphs[i].y_off * PANGO_SCALE;
389 g->geometry.width = m_glyphs[i].x_advance * PANGO_SCALE;
393 int c = mtext_ref_char (mt, m_glyphs[i].from);
394 MSymbol category = mchar_get_prop (c, Mcategory);
395 PangoRectangle logical_rect;
400 g->geometry.width = m_glyphs[i].x_advance * PANGO_SCALE;
402 else if (category == MCf)
405 g->geometry.width = 0;
409 g->glyph = (pango_fc_font_get_unknown_glyph
410 (PANGO_FC_FONT (pango_font), c));
411 pango_font_get_glyph_extents (pango_font, g->glyph, NULL,
413 g->geometry.width = logical_rect.width;
415 g->geometry.x_offset = 0;
416 g->geometry.y_offset = 0;
418 g->attr.is_cluster_start
420 || m_glyphs[i - 1].from != m_glyphs[i].from);
421 glyphs->log_clusters[i] = offsets[m_glyphs[i].from];
424 m17n_object_unref (mt);
427 /* Check if PANGO_FONT is suitable for the character WC. */
429 static PangoCoverageLevel
430 m17n_fc_engine_covers (PangoEngineShape *engine,
431 PangoFont *pango_font,
432 PangoLanguage *pango_language,
435 static gunichar last_wc = 0;
436 /* Symbols for character property `script'. */
437 static MSymbol Mlatin, Mcommon, Minherited;
438 PangoCoverage *coverage;
439 PangoCoverageLevel result;
440 MSymbol script, language;
441 MPlist *spec_list, *p;
446 Mlatin = msymbol ("latin");
447 Mcommon = msymbol ("common");
448 Minherited = msymbol ("inherited");
451 if (debug_level > 0 && wc != last_wc)
453 g_debug ("covers for U+%04X", wc);
457 coverage = pango_font_get_coverage (pango_font, pango_language);
458 result = pango_coverage_get (coverage, wc);
459 pango_coverage_unref (coverage);
461 if (result == PANGO_COVERAGE_NONE)
464 g_debug (" %s none", m17n_fc_get_family (pango_font));
468 language = m17n_fc_get_language (pango_language);
473 script = mchar_get_prop ((int) wc, Mscript);
474 if (script == Mcommon || script == Minherited)
475 script = mchar_get_prop ((int) wc, msymbol ("block"));
478 font = m17n_fc_get_font (pango_font);
479 spec_list = m17n_fc_lookup_fontset (script, language);
481 result = PANGO_COVERAGE_NONE;
482 for (p = spec_list; mplist_key (p) != Mnil; p = mplist_next (p))
483 if (mfont_match_p (font, mplist_value (p)))
485 result = PANGO_COVERAGE_EXACT;
488 m17n_object_unref (spec_list);
490 g_debug (" (%s,%s): %s %s",
491 (script ? msymbol_name (script) : ""),
492 (language ? msymbol_name (language) : ""),
493 m17n_fc_get_family (pango_font),
494 result == PANGO_COVERAGE_EXACT ? "exact" : "none");
500 m17n_engine_fc_class_init (PangoEngineShapeClass *class)
502 class->script_shape = m17n_fc_engine_shape;
503 class->covers = m17n_fc_engine_covers;
506 PANGO_ENGINE_SHAPE_DEFINE_TYPE (M17NEngineFc, m17n_engine_fc,
507 m17n_engine_fc_class_init, NULL)
510 PANGO_MODULE_ENTRY(init) (GTypeModule *module)
512 m17n_engine_fc_register_type (module);
519 PANGO_MODULE_ENTRY(exit) (void)
526 PANGO_MODULE_ENTRY(list) (PangoEngineInfo **engines,
529 *engines = script_engines;
530 *n_engines = G_N_ELEMENTS (script_engines);
534 PANGO_MODULE_ENTRY(create) (const char *id)
536 return (!strcmp (id, SCRIPT_ENGINE_NAME)
537 ? g_object_new (m17n_engine_fc_type, NULL)