4 * Copyright (C) 1999 Red Hat Software
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #include <pango/pango.h>
25 #include <pango/pango-utils.h>
26 #include "gdkprivate-fb.h"
29 typedef struct _CharCache CharCache;
35 static PangoEngineRange basic_ranges[] = {
36 /* Language characters */
37 { 0x0000, 0x02af, "*" },
38 { 0x02b0, 0x02ff, "" },
39 { 0x0380, 0x058f, "*" },
40 { 0x0591, 0x05f4, "*" }, /* Hebrew */
41 { 0x060c, 0x06f9, "" }, /* Arabic */
42 { 0x0e01, 0x0e5b, "" }, /* Thai */
43 { 0x10a0, 0x10ff, "*" }, /* Georgian */
44 { 0x1200, 0x16ff, "*" }, /* Ethiopic,Cherokee,Canadian,Ogham,Runic */
45 { 0x1e00, 0x1fff, "*" },
46 { 0x2000, 0x9fff, "*" },
47 { 0xac00, 0xd7a3, "kr" },
48 { 0xf900, 0xfa0b, "kr" },
49 { 0xff00, 0xffe3, "*" }
52 static PangoEngineInfo script_engines[] = {
54 "BasicScriptEngineFB",
55 PANGO_ENGINE_TYPE_SHAPE,
57 basic_ranges, G_N_ELEMENTS(basic_ranges)
61 static gint n_script_engines = G_N_ELEMENTS (script_engines);
64 * FB window system script engine portion
72 result = g_new0 (CharCache, 1);
78 char_cache_free (CharCache *cache)
84 find_char (CharCache *cache, PangoFont *font, gunichar wc, const char *input)
86 return FT_Get_Char_Index(PANGO_FB_FONT(font)->ftf, wc);
90 set_glyph (PangoFont *font, PangoGlyphString *glyphs, int i, int offset, PangoGlyph glyph)
92 PangoRectangle logical_rect;
94 glyphs->glyphs[i].glyph = glyph;
96 glyphs->glyphs[i].geometry.x_offset = 0;
97 glyphs->glyphs[i].geometry.y_offset = 0;
99 glyphs->log_clusters[i] = offset;
101 pango_font_get_glyph_extents (font, glyphs->glyphs[i].glyph, NULL, &logical_rect);
102 glyphs->glyphs[i].geometry.width = logical_rect.width;
107 swap_range (PangoGlyphString *glyphs, int start, int end)
111 for (i = start, j = end - 1; i < j; i++, j--)
113 PangoGlyphInfo glyph_info;
116 glyph_info = glyphs->glyphs[i];
117 glyphs->glyphs[i] = glyphs->glyphs[j];
118 glyphs->glyphs[j] = glyph_info;
120 log_cluster = glyphs->log_clusters[i];
121 glyphs->log_clusters[i] = glyphs->log_clusters[j];
122 glyphs->log_clusters[j] = log_cluster;
127 pango_fb_get_unknown_glyph(PangoFont *font)
129 return FT_Get_Char_Index (PANGO_FB_FONT (font)->ftf, '~');
133 get_char_cache (PangoFont *font)
135 GQuark cache_id = g_quark_from_string ("basic-char-cache");
137 CharCache *cache = g_object_get_qdata (G_OBJECT (font), cache_id);
140 cache = char_cache_new ();
141 g_object_set_qdata_full (G_OBJECT (font), cache_id,
142 cache, (GDestroyNotify)char_cache_free);
149 basic_engine_shape (PangoFont *font,
152 PangoAnalysis *analysis,
153 PangoGlyphString *glyphs)
161 g_return_if_fail (font != NULL);
162 g_return_if_fail (text != NULL);
163 g_return_if_fail (length >= 0);
164 g_return_if_fail (analysis != NULL);
166 cache = get_char_cache (font);
168 n_chars = g_utf8_strlen (text, length);
169 pango_glyph_string_set_size (glyphs, n_chars);
172 for (i=0; i < n_chars; i++)
175 gunichar mirrored_ch;
180 wc = g_utf8_get_char (p);
183 if (analysis->level % 2)
184 if (pango_get_mirror_char (wc, &mirrored_ch))
188 g_unichar_to_utf8 (wc, buf);
192 if (wc == 0x200B || wc == 0x200E || wc == 0x200F) /* Zero-width characters */
194 set_glyph (font, glyphs, i, p - text, 0);
198 index = find_char (cache, font, wc, input);
201 set_glyph (font, glyphs, i, p - text, index);
203 if (g_unichar_type (wc) == G_UNICODE_NON_SPACING_MARK)
207 PangoRectangle logical_rect, ink_rect;
209 glyphs->glyphs[i].geometry.width = MAX (glyphs->glyphs[i-1].geometry.width,
210 glyphs->glyphs[i].geometry.width);
211 glyphs->glyphs[i-1].geometry.width = 0;
212 glyphs->log_clusters[i] = glyphs->log_clusters[i-1];
214 /* Some heuristics to try to guess how overstrike glyphs are
215 * done and compensate
217 pango_font_get_glyph_extents (font, glyphs->glyphs[i].glyph, &ink_rect, &logical_rect);
218 if (logical_rect.width == 0 && ink_rect.x == 0)
219 glyphs->glyphs[i].geometry.x_offset = (glyphs->glyphs[i].geometry.width - ink_rect.width) / 2;
224 set_glyph (font, glyphs, i, p - text, pango_fb_get_unknown_glyph (font));
227 p = g_utf8_next_char (p);
230 /* Simple bidi support... may have separate modules later */
232 if (analysis->level % 2)
236 /* Swap all glyphs */
237 swap_range (glyphs, 0, n_chars);
239 /* Now reorder glyphs within each cluster back to LTR */
240 for (start=0; start<n_chars;)
243 while (end < n_chars &&
244 glyphs->log_clusters[end] == glyphs->log_clusters[start])
247 swap_range (glyphs, start, end);
253 static PangoCoverage *
254 basic_engine_get_coverage (PangoFont *font,
257 CharCache *cache = get_char_cache (font);
258 PangoCoverage *result = pango_coverage_new ();
261 for (wc = 0; wc < 65536; wc++)
265 g_unichar_to_utf8 (wc, buf);
266 if (find_char (cache, font, wc, buf))
267 pango_coverage_set (result, wc, PANGO_COVERAGE_EXACT);
274 basic_engine_fb_new ()
276 PangoEngineShape *result;
278 result = g_new (PangoEngineShape, 1);
280 result->engine.id = "BasicScriptEngineFB";
281 result->engine.type = PANGO_ENGINE_TYPE_LANG;
282 result->engine.length = sizeof (result);
283 result->script_shape = basic_engine_shape;
284 result->get_coverage = basic_engine_get_coverage;
286 return (PangoEngine *)result;
289 /* The following three functions provide the public module API for
293 #define MODULE_ENTRY(func) _pango_basic_##func
295 #define MODULE_ENTRY(func) func
299 MODULE_ENTRY(script_engine_list) (PangoEngineInfo **engines, gint *n_engines)
301 *engines = script_engines;
302 *n_engines = n_script_engines;
306 MODULE_ENTRY(script_engine_load) (const char *id)
308 if (!strcmp (id, "BasicScriptEngineFB"))
309 return basic_engine_fb_new ();
315 MODULE_ENTRY(script_engine_unload) (PangoEngine *engine)