summaryrefslogtreecommitdiff
path: root/pango/pango-indic.c
blob: 0bd417cb832e629df82d2b3cb8b6db8e7b70a2b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* Pango
 * pango-indic.c: Library for Indic script rendering
 *
 * Copyright (C) 2000 SuSE Linux Ltd.
 *
 * Author: Robert Brady <rwb197@zepler.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * Licence as published by the Free Software Foundation; either
 * version 2 of the Licence, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public Licence for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * Licence along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "pango-glyph.h"
#include "pango-indic.h"

/**
 * pango_indic_shift_vowels:
 * @script: A #PangoIndicScript
 * @chars: Array of #gunichar
 * @end: Pointer to just after the end of @chars
 * 
 * This causes the any vowels in @chars which are
 * left-joining vowels to move to the start of @chars.
 *
 * It determines whether the vowels are left-joining 
 * by calling is_prefixing_vowel from @script.
 */
void 
pango_indic_shift_vowels (PangoIndicScript *script, 
                          gunichar         *chars, 
                          gunichar         *end) 
{
  int length = end - chars;
  int i, j;
  
  for (i = 1 ; i < length ; i++)
    {
      if (script->is_prefixing_vowel (chars[i]))
        {
           gunichar tmp = chars[i];
           
           for (j = i; j > 0; j--) {
             chars[j] = chars[j - 1];
           }

           chars[0] = tmp;
        }
    }
}

/**
 * pango_indic_compact:
 * @script: A #PangoIndicScript
 * @num: The number of glyphs
 * @chars: An array of glyphs/characters
 * @cluster: The cluster array.
 *
 * This eliminates any blank space in the @chars
 * array, updated @clusters and @num also. 
 * (Blank space is defines as U+0000)
 */
void
pango_indic_compact (PangoIndicScript *script, 
                     int              *num, 
                     gunichar         *chars, 
                     int              *cluster)
{
  gunichar *dest = chars;
  gunichar *end = chars + *num;
  int *cluster_dest = cluster;
  while (chars < end)
    {
      if (*chars)
        {
          *dest = *chars;
          *cluster_dest = *cluster;
          dest++;
          chars++;
          cluster++;
          cluster_dest++;
        }
      else
        {
          chars++;
          cluster++;
        }
    }
  *num -= (chars - dest);
}

static gunichar 
default_vowel_sign_to_matra (gunichar a) 
{
  return (a + 0xc000);
}

/**
 * pango_indic_convert_vowels:
 * @script: A #PangoIndicScript
 * @in_middle: Whether vowels should be converted 
 * @num: The number of elements in @chars.
 * @chars: An array of glyphs/characters
 * @has_standalone_vowels: Whether the font has standalone vowels.
 *
 * This converts the second two vowel signs in a row
 * in a string, to either a vowel letter or spacing forms
 * of the combining vowel.
 */
void
pango_indic_convert_vowels (PangoIndicScript *script,
                            gboolean          in_middle,
                            int              *num, 
                            gunichar         *chars,
                            gboolean          has_standalone_vowels)
{
  /* goes along and converts vowel signs to vowel letters if needed. */
  gunichar *end = chars + *num;
  gunichar *start = chars;
  gunichar (*vowel_sign_to_matra)(gunichar a) = script->vowel_sign_to_matra;
  gboolean last_was_vowel_sign = 0;

  vowel_sign_to_matra = FALSE;
  if (!vowel_sign_to_matra || has_standalone_vowels)
    vowel_sign_to_matra = default_vowel_sign_to_matra;

  while (chars < end)
    {
      gboolean convert = FALSE;
      gboolean is_vowel_sign = script->is_vowel_sign (chars[0]);
      if (is_vowel_sign) 
        {
          if (chars==start) 
            convert = TRUE;

          if (chars > start && in_middle &&
              (last_was_vowel_sign ||
               (script->is_vowel_half && script->is_vowel_half (chars[-1]))))
            convert = TRUE;
        }
      
      if (convert)
        chars[0] = vowel_sign_to_matra (chars[0]);
      
      last_was_vowel_sign = is_vowel_sign;

      chars++;
    }
}

/**
 * pango_indic_split_out_characters
 * @script: A #PangoIndicScript
 * @text: A UTF-8 string
 * @n_chars: The number of UTF-8 sequences in @text
 * @wc: Pointer to array of #gunichar (output param)
 * @n_glyph: Pointer to number of elements in @wc. (output param)
 * @glyphs: A #PangoGlyphString.
 *
 * This splits out the string @text into characters. It will
 * split out two-part vowels using @script->vowel_split if
 * this function is available.
 *
 * *@n_chars is allocated with g_new, you must free it.
 */
void 
pango_indic_split_out_characters (PangoIndicScript *script, 
                                       const char       *text, 
                                       int               n_chars, 
                                       gunichar        **wc, 
                                       int              *n_glyph, 
                                       PangoGlyphString *glyphs)
{
  const char *p = text;
  int i;
  *n_glyph = n_chars;

  if (script->vowel_split) 
    for (i = 0; i < n_chars; i++) 
      {
        gunichar u = g_utf8_get_char (p);
        if (script->vowel_split (u, NULL, NULL))
          (*n_glyph)++;
        p = g_utf8_next_char (p);
      }
  
  p = text;
  *wc = (gunichar *) g_new (gunichar, *n_glyph);
  pango_glyph_string_set_size (glyphs, *n_glyph);
  for (i = 0; i < *n_glyph; i++)
    {
      (*wc)[i] = g_utf8_get_char (p);
      glyphs->log_clusters[i] = p - text;
   
      if (script->vowel_split) 
        if (script->vowel_split ((*wc)[i], &(*wc)[i], &(*wc)[i+1]))
          {
            i++;
            glyphs->log_clusters[i] = p - text;
          }
      
      p = g_utf8_next_char (p);
    }
}