1 /* This is a small demo of the high-performance GUIX graphics framework. */
2 
3 #include <stdio.h>
4 #include "tx_api.h"
5 #include "gx_api.h"
6 #include "gx_utility.h"
7 
8 #include "canonical_normalize_32bpp_resources.h"
9 #include "canonical_normalize_32bpp_specifications.h"
10 
11 #if defined(GX_CANONICAL_NORMALIZE_SUPPORT)
12 
13 extern TX_BYTE_POOL       mem_pool;
14 extern VOID memory_free(VOID *mem);
15 
16 GX_CHAR file_name[256] = "..\\NormalizationTest.txt";
17 
18 ULONG source[255];
19 GX_UBYTE utf8_source[2014];
20 ULONG nfc[255];
21 int source_count;
22 int utf8_source_count;
23 int nfc_count;
24 
25 /* Read test data. */
read_line(FILE * file)26 static int read_line(FILE *file)
27 {
28     char ch;
29     int field_index = 0;
30     ULONG code = 0;
31     INT digit;
32     GX_BOOL skip = GX_FALSE;
33 
34     source_count = 0;
35     nfc_count = 0;
36 
37     ch = getc(file);
38 
39     while (ch == '\n' || ch == '\r')
40     {
41         ch = getc(file);
42     }
43 
44     while ((ch != '\n') && (ch != '\r') && (ch != EOF))
45     {
46         if ((!skip) && (field_index < 2))
47         {
48             switch (ch)
49             {
50             case '#':
51             case '@':
52                 skip = GX_TRUE;
53                 break;
54 
55             case ' ':
56             case ';':
57                 if (field_index == 0)
58                 {
59                     source[source_count++] = code;
60                 }
61                 else
62                 {
63                     nfc[nfc_count++] = code;
64                 }
65 
66                 code = 0;
67 
68                 if (ch == ';')
69                 {
70                     field_index++;
71                 }
72                 break;
73             default:
74 
75                 digit = (ch - '0');
76 
77                 if (ch >= 'A' && ch <= 'F')
78                 {
79                     digit = 10 + (ch - 'A');
80                 }
81                 else if (ch >= 'a' && ch <= 'f')
82                 {
83                     digit = 10 + (ch - 'a');
84                 }
85 
86                 code =  (code << 4) + digit;
87                 break;
88             }
89         }
90 
91         ch = getc(file);
92     }
93 
94     if (ch == EOF)
95         return(-1);
96 
97     return(1);
98 
99 }
100 
101 /* Convert unicode format input text to utf8 format text. */
unicode_to_utf8_str()102 void unicode_to_utf8_str()
103 {
104     INT index;
105     UINT glyph_len;
106 
107     utf8_source_count = 0;
108 
109     for (index = 0; index < source_count; index++)
110     {
111         _gx_utility_unicode_to_utf8(source[index], utf8_source + utf8_source_count, &glyph_len);
112         utf8_source_count += glyph_len;
113     }
114 }
115 
116 /* Compare result. */
result_compare(GX_CHAR * utf8_str)117 UINT result_compare(GX_CHAR *utf8_str)
118 {
119     INT  index;
120     UINT glyph_len;
121     GX_CHAR_CODE unicode;
122 
123     for (index = 0; index < nfc_count; index++)
124     {
125         _gx_utility_utf8_string_character_get((GX_CONST CHAR **)&utf8_str, GX_STRLEN(utf8_str), &unicode, &glyph_len);
126 
127         if (unicode != nfc[index])
128         {
129             return GX_FAILURE;
130         }
131     }
132 
133     return GX_SUCCESS;
134 }
135 
normalization_test()136 UINT normalization_test()
137 {
138     FILE *file;
139     GX_UBYTE *normalized_string;
140     UINT normalized_count;
141     UINT   result = GX_SUCCESS;
142     INT  test_count = 0;
143     UINT status;
144     ULONG pool_size;
145 
146     gx_system_text_render_style_set(GX_TEXT_RENDER_CANONICAL_NORMALIZE);
147     file = fopen(file_name, "rb");
148 
149     if (!file)
150     {
151         return GX_FAILURE;
152     }
153 
154     while (read_line(file) > 0)
155     {
156         if (source_count == 0 || nfc_count == 0)
157         {
158             continue;
159         }
160 
161         if (test_count == 23 || test_count == 24)
162         {
163             /* Skip hangul sylable normalization. */
164             test_count++;
165             continue;
166         }
167 
168         unicode_to_utf8_str();
169 
170         pool_size = mem_pool.tx_byte_pool_available;
171 
172         normalized_string = GX_NULL;
173 
174         status = _gx_utility_canonical_normalize(utf8_source, utf8_source_count, &normalized_string, &normalized_count);
175 
176         if (status == GX_NO_CHANGE)
177         {
178             if (result_compare(utf8_source))
179             {
180                 result = GX_FAILURE;
181             }
182         }
183         else
184         {
185             if ((status != GX_SUCCESS || result_compare(normalized_string)))
186             {
187                 result = GX_FAILURE;
188             }
189         }
190 
191         if (normalized_string)
192         {
193             memory_free(normalized_string);
194         }
195 
196         if (pool_size != mem_pool.tx_byte_pool_available)
197         {
198             result = GX_FAILURE;
199         }
200 
201         if (result != GX_SUCCESS)
202         {
203             break;
204         }
205         test_count++;
206     }
207 
208     return result;
209 }
210 
211 #endif