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