Git init
[external/pango1.0.git] / modules / thai / thai-lang.c
1 /* Pango
2  * thai-lang.c:
3  *
4  * Copyright (C) 2003 Theppitak Karoonboonyanan <thep@linux.thai.net>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library 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.
10  *
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  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21
22 #include <string.h>
23 #include <glib.h>
24 #include <pango/pango-engine.h>
25 #include <pango/pango-break.h>
26 #include <thai/thwchar.h>
27 #include <thai/thbrk.h>
28
29 /* No extra fields needed */
30 typedef PangoEngineLang      ThaiEngineLang;
31 typedef PangoEngineLangClass ThaiEngineLangClass;
32
33 #define SCRIPT_ENGINE_NAME "ThaiScriptEngineLang"
34
35 static PangoEngineScriptInfo thai_scripts[] = {
36   { PANGO_SCRIPT_THAI, "*" }
37 };
38
39 static PangoEngineInfo script_engines[] = {
40   {
41     SCRIPT_ENGINE_NAME,
42     PANGO_ENGINE_TYPE_LANG,
43     PANGO_RENDER_TYPE_NONE,
44     thai_scripts, G_N_ELEMENTS(thai_scripts)
45   }
46 };
47
48 /*
49  * tis_text is assumed to be large enough to hold the converted string,
50  * i.e. it must be at least g_utf8_strlen(text, len)+1 bytes.
51  */
52 static thchar_t *
53 utf8_to_tis (const char *text, int len, thchar_t *tis_text, int *tis_cnt)
54 {
55   thchar_t   *tis_p;
56   const char *text_p;
57
58   tis_p = tis_text;
59   for (text_p = text; text_p < text + len; text_p = g_utf8_next_char (text_p))
60     *tis_p++ = th_uni2tis (g_utf8_get_char (text_p));
61   *tis_p++ = '\0';
62
63   *tis_cnt = tis_p - tis_text;
64   return tis_text;
65 }
66
67 static void
68 thai_engine_break (PangoEngineLang *engine G_GNUC_UNUSED,
69                    const char      *text,
70                    int              len,
71                    PangoAnalysis   *analysis G_GNUC_UNUSED,
72                    PangoLogAttr    *attrs,
73                    int              attrs_len G_GNUC_UNUSED)
74 {
75   thchar_t tis_stack[512];
76   int brk_stack[512];
77   thchar_t *tis_text;
78   int *brk_pnts;
79   int cnt;
80
81   if (len < 0)
82     len = strlen (text);
83   cnt = g_utf8_strlen (text, len) + 1;
84
85   tis_text = tis_stack;
86   if (cnt > (int) G_N_ELEMENTS (tis_stack))
87     tis_text = g_new (thchar_t, cnt);
88
89   utf8_to_tis (text, len, tis_text, &cnt);
90
91   brk_pnts = brk_stack;
92   if (cnt > (int) G_N_ELEMENTS (brk_stack))
93     brk_pnts = g_new (int, cnt);
94
95   /* find line break positions */
96   len = th_brk (tis_text, brk_pnts, len);
97   for (cnt = 0; cnt < len; cnt++)
98     {
99       attrs[brk_pnts[cnt]].is_line_break = TRUE;
100       attrs[brk_pnts[cnt]].is_word_start = TRUE;
101       attrs[brk_pnts[cnt]].is_word_end = TRUE;
102     }
103
104   if (brk_pnts != brk_stack)
105     g_free (brk_pnts);
106
107   if (tis_text != tis_stack)
108     g_free (tis_text);
109 }
110
111 static void
112 thai_engine_lang_class_init (PangoEngineLangClass *class)
113 {
114   class->script_break = thai_engine_break;
115 }
116
117 PANGO_ENGINE_LANG_DEFINE_TYPE (ThaiEngineLang, thai_engine_lang,
118                                thai_engine_lang_class_init, NULL);
119
120 void
121 PANGO_MODULE_ENTRY(init) (GTypeModule *module)
122 {
123   thai_engine_lang_register_type (module);
124 }
125
126 void
127 PANGO_MODULE_ENTRY(exit) (void)
128 {
129 }
130
131 void
132 PANGO_MODULE_ENTRY(list) (PangoEngineInfo **engines, gint *n_engines)
133 {
134   *engines = script_engines;
135   *n_engines = G_N_ELEMENTS (script_engines);
136 }
137
138 /* Load a particular engine given the ID for the engine
139  */
140 PangoEngine *
141 PANGO_MODULE_ENTRY(create) (const char *id)
142 {
143   if (!strcmp (id, SCRIPT_ENGINE_NAME))
144     return g_object_new (thai_engine_lang_type, NULL);
145   else
146     return NULL;
147 }
148