*** empty log message ***
[m17n/m17n-lib.git] / example / linebreak.c
1 #include <m17n-gui.h>
2
3 #include <config.h>
4
5 #ifdef HAVE_WORDCUT
6
7 #define THAI_BEG 0x0E00
8 #define THAI_END 0x0E6F
9
10 static int wordcut_initiazlied = 0;
11
12 #include <wordcut/wordcut.h>
13 #include <wordcut/xmalloc.h>
14 #include <wordcut/path.h>
15
16 static Wordcut wordcut;
17
18 static MSymbol Mwordcut_wordbeg, Miso_8859_11;
19
20 static void
21 init_th_wordcut ()
22 {
23   Mwordcut_wordbeg = msymbol (" wordcut-wordseg");
24   Miso_8859_11 = msymbol ("iso-8859-11");
25 }
26
27 int
28 thai_line_break (MText *mt, int pos, int from, int to)
29 {
30   WordcutResult result;
31   MTextProperty *prop;
32   int pos1, pos2, c, i;
33   unsigned char *str;
34
35   if (wordcut_initiazlied < 0)
36     return pos;
37   if (! wordcut_initiazlied)
38     {
39       if (wordcut_init (&wordcut, WORDCUT_TDICT) != 0)
40         {
41           wordcut_initiazlied = -1;
42           return pos;
43         }
44       init_th_wordcut ();
45       wordcut_initiazlied = 1;
46     }
47   prop = mtext_get_property (mt, pos, Mwordcut_wordbeg);
48   if (prop)
49     {
50       pos1 = mtext_property_start (prop);
51       if (pos1 == from)
52         pos1 = pos;
53     return pos1;
54     }
55   str = alloca (to - from);
56   pos1 = pos - 1;
57   while (pos1 >= from
58          && (c = mtext_ref_char (mt, pos1)) >= THAI_BEG && c <= THAI_END)
59     str[pos1-- - from] = mchar_encode (Miso_8859_11, c);
60   pos1++;
61   pos2 = pos;
62   while (pos2 < to
63          && (c = mtext_ref_char (mt, pos2)) >= 0x0E00 && c <= 0x0E6F)
64     str[pos2++ - from] = mchar_encode (Miso_8859_11, c);
65   str[pos2 - from] = 0;
66   wordcut_cut (&wordcut, (char *) (str + pos1 - from), &result);
67   for (i = 0; i < result.count; i++)
68     {
69       int start = pos1 + result.start[i];
70
71       prop = mtext_property (Mwordcut_wordbeg, Mt,
72                              MTEXTPROP_VOLATILE_WEAK | MTEXTPROP_NO_MERGE);
73       mtext_attach_property (mt, start, start + result.offset[i], prop);
74       m17n_object_unref (prop);
75     }
76   prop = mtext_get_property (mt, pos, Mwordcut_wordbeg);
77   pos1 = mtext_property_start (prop);
78   if (pos1 == from)
79     pos1 = pos;
80   return pos1;
81 }
82
83 #define CHECK_THAI_LINE_BREAK(c, mt, pos, from, to)             \
84   do {                                                          \
85     if ((c) >= THAI_BEG && (c) <= THAI_END)                     \
86       return thai_line_break ((mt), (pos), (from), (to));       \
87   } while (0)
88
89 #else  /* not HAVE_WORDCUT */
90
91 #define CHECK_THAI_LINE_BREAK(c, mt, pos, from, to) (void) 0
92
93 #endif  /* not HAVE_WORDCUT */
94
95 int
96 line_break (MText *mt, int pos, int from, int to, int line, int y)
97 {
98   int c = mtext_ref_char (mt, pos);
99   int orig_pos = pos;
100
101   if (c == ' ' || c == '\t' || c == '\n')
102     {
103       for (pos++; pos < to; pos++)
104         if ((c = mtext_ref_char (mt, pos)) != ' ' && c != '\t' && c != '\n')
105           break;
106     }
107   else
108     {
109       while (pos > from)
110         {
111           if (c == ' ' || c == '\t')
112             break;
113           CHECK_THAI_LINE_BREAK (c, mt, pos, from, to);
114           pos--;
115           c = mtext_ref_char (mt, pos);
116         }
117       if (pos == from)
118         pos = orig_pos;
119       else
120         pos++;
121     }
122   return pos;
123 }