1 /* Pango
2 * break-thai.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 #include "config.h"
22
23 #include "pango-break.h"
24 #include "pango-impl-utils.h"
25
26 #ifdef HAVE_LIBTHAI
27 #include <thai/thwchar.h>
28 #include <thai/thbrk.h>
29
30 G_LOCK_DEFINE_STATIC (thai_brk);
31
32 #ifdef HAVE_TH_BRK_FIND_BREAKS
33 static ThBrk *thai_brk = NULL;
34 #endif
35
36 /*
37 * tis_text is assumed to be large enough to hold the converted string,
38 * i.e. it must be at least pango_utf8_strlen(text, len)+1 bytes.
39 */
40 static thchar_t *
utf8_to_tis(const char * text,int len,thchar_t * tis_text,int * tis_cnt)41 utf8_to_tis (const char *text, int len, thchar_t *tis_text, int *tis_cnt)
42 {
43 thchar_t *tis_p;
44 const char *text_p;
45
46 tis_p = tis_text;
47 for (text_p = text; text_p < text + len; text_p = g_utf8_next_char (text_p))
48 *tis_p++ = th_uni2tis (g_utf8_get_char (text_p));
49 *tis_p++ = '\0';
50
51 *tis_cnt = tis_p - tis_text;
52 return tis_text;
53 }
54
55 #endif
56 static void
break_thai(const char * text,int len,const PangoAnalysis * analysis G_GNUC_UNUSED,PangoLogAttr * attrs,int attrs_len G_GNUC_UNUSED)57 break_thai (const char *text,
58 int len,
59 const PangoAnalysis *analysis G_GNUC_UNUSED,
60 PangoLogAttr *attrs,
61 int attrs_len G_GNUC_UNUSED)
62 {
63 #ifdef HAVE_LIBTHAI
64 thchar_t tis_stack[512];
65 int brk_stack[512];
66 thchar_t *tis_text;
67 int *brk_pnts;
68 int cnt;
69
70 cnt = pango_utf8_strlen (text, len) + 1;
71
72 tis_text = tis_stack;
73 if (cnt > (int) G_N_ELEMENTS (tis_stack))
74 tis_text = g_new (thchar_t, cnt);
75
76 utf8_to_tis (text, len, tis_text, &cnt);
77
78 brk_pnts = brk_stack;
79 if (cnt > (int) G_N_ELEMENTS (brk_stack))
80 brk_pnts = g_new (int, cnt);
81
82 /* find line break positions */
83
84 G_LOCK (thai_brk);
85 #ifdef HAVE_TH_BRK_FIND_BREAKS
86 if (thai_brk == NULL)
87 thai_brk = th_brk_new(NULL);
88 len = th_brk_find_breaks(thai_brk, tis_text, brk_pnts, cnt);
89 #else
90 len = th_brk (tis_text, brk_pnts, cnt);
91 #endif
92 G_UNLOCK (thai_brk);
93
94 for (cnt = 0; cnt < len; cnt++)
95 if (attrs[brk_pnts[cnt]].is_char_break)
96 {
97 /* Only allow additional line breaks if line-breaking is NOT
98 * prohibited. (The alternative would be to set is_char_break to
99 * TRUE as well. NOT setting it will break invariants that any
100 * line break opportunity is also a char break opportunity. */
101 attrs[brk_pnts[cnt]].is_line_break = TRUE;
102 attrs[brk_pnts[cnt]].is_word_start = TRUE;
103 attrs[brk_pnts[cnt]].is_word_end = TRUE;
104 }
105
106 if (brk_pnts != brk_stack)
107 g_free (brk_pnts);
108
109 if (tis_text != tis_stack)
110 g_free (tis_text);
111 #endif
112 }
113