(TextController) Reformatted to reduce LOC
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / xhtml-entities.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // EXTERNAL INCLUDES
19 #include <cstring> // for strlen()
20
21 // FILE HEADER
22 #include "xhtml-entities.h"
23
24 namespace Dali
25 {
26 namespace Toolkit
27 {
28 namespace Text
29 {
30 namespace
31 {
32 /**
33  * Implementation of the XHTML Entity matching
34  */
35 struct XHTMLEntityLookup
36 {
37   const char* const entityName; // XHTML Named Entity string
38   const char* const entityCode; // Corresponding UTF-8
39 };
40
41 /* table of html name entities supported in DALi
42  *
43  * these are stored as pair with Named entity as Key and
44  * its utf 8 as value
45  */
46 // clang-format off
47 const XHTMLEntityLookup XHTMLEntityLookupTable[] =
48 {
49   {"&quot;\0"    ,"\x22\0"         },
50   {"&amp;\0"     ,"\x26\0"         },
51   {"&apos;\0"    ,"\x27\0"         },
52   {"&lt;\0"      ,"\x3c\0"         },
53   {"&gt;\0"      ,"\x3e\0"         },
54   {"&nbsp;\0"    ,"\xc2\xa0\0"     },
55   {"&iexcl;\0"   ,"\xc2\xa1\0"     },
56   {"&cent;\0"    ,"\xc2\xa2\0"     },
57   {"&pound;\0"   ,"\xc2\xa3\0"     },
58   {"&curren;\0"  ,"\xc2\xa4\0"     },
59   {"&yen;\0"     ,"\xc2\xa5\0"     },
60   {"&brvbar;\0"  ,"\xc2\xa6\0"     },
61   {"&sect;\0"    ,"\xc2\xa7\0"     },
62   {"&uml;\0"     ,"\xc2\xa8\0"     },
63   {"&copy;\0"    ,"\xc2\xa9\0"     },
64   {"&ordf;\0"    ,"\xc2\xaa\0"     },
65   {"&laquo;\0"   ,"\xc2\xab\0"     },
66   {"&not;\0"     ,"\xc2\xac\0"     },
67   {"&shy;\0"     ,"\xc2\xad\0"     },
68   {"&reg;\0"     ,"\xc2\xae\0"     },
69   {"&macr;\0"    ,"\xc2\xaf\0"     },
70   {"&deg;\0"     ,"\xc2\xb0\0"     },
71   {"&plusmn;\0"  ,"\xc2\xb1\0"     },
72   {"&sup2;\0"    ,"\xc2\xb2\0"     },
73   {"&sup3;\0"    ,"\xc2\xb3\0"     },
74   {"&acute;\0"   ,"\xc2\xb4\0"     },
75   {"&micro;\0"   ,"\xc2\xb5\0"     },
76   {"&para;\0"    ,"\xc2\xb6\0"     },
77   {"&middot;\0"  ,"\xc2\xb7\0"     },
78   {"&cedil;\0"   ,"\xc2\xb8\0"     },
79   {"&sup1;\0"    ,"\xc2\xb9\0"     },
80   {"&ordm;\0"    ,"\xc2\xba\0"     },
81   {"&raquo;\0"   ,"\xc2\xbb\0"     },
82   {"&frac14;\0"  ,"\xc2\xbc\0"     },
83   {"&frac12;\0"  ,"\xc2\xbd\0"     },
84   {"&frac34;\0"  ,"\xc2\xbe\0"     },
85   {"&iquest;\0"  ,"\xc2\xbf\0"     },
86   {"&Agrave;\0"  ,"\xc3\x80\0"     },
87   {"&Aacute;\0"  ,"\xc3\x81\0"     },
88   {"&Acirc;\0"   ,"\xc3\x82\0"     },
89   {"&Atilde;\0"  ,"\xc3\x83\0"     },
90   {"&Auml;\0"    ,"\xc3\x84\0"     },
91   {"&Aring;\0"   ,"\xc3\x85\0"     },
92   {"&AElig;\0"   ,"\xc3\x86\0"     },
93   {"&Ccedil;\0"  ,"\xc3\x87\0"     },
94   {"&Egrave;\0"  ,"\xc3\x88\0"     },
95   {"&Eacute;\0"  ,"\xc3\x89\0"     },
96   {"&Ecirc;\0"   ,"\xc3\x8a\0"     },
97   {"&Euml;\0"    ,"\xc3\x8b\0"     },
98   {"&Igrave;\0"  ,"\xc3\x8c\0"     },
99   {"&Iacute;\0"  ,"\xc3\x8d\0"     },
100   {"&Icirc;\0"   ,"\xc3\x8e\0"     },
101   {"&Iuml;\0"    ,"\xc3\x8f\0"     },
102   {"&ETH;\0"     ,"\xc3\x90\0"     },
103   {"&Ntilde;\0"  ,"\xc3\x91\0"     },
104   {"&Ograve;\0"  ,"\xc3\x92\0"     },
105   {"&Oacute;\0"  ,"\xc3\x93\0"     },
106   {"&Ocirc;\0"   ,"\xc3\x94\0"     },
107   {"&Otilde;\0"  ,"\xc3\x95\0"     },
108   {"&Ouml;\0"    ,"\xc3\x96\0"     },
109   {"&times;\0"   ,"\xc3\x97\0"     },
110   {"&Oslash;\0"  ,"\xc3\x98\0"     },
111   {"&Ugrave;\0"  ,"\xc3\x99\0"     },
112   {"&Uacute;\0"  ,"\xc3\x9a\0"     },
113   {"&Ucirc;\0"   ,"\xc3\x9b\0"     },
114   {"&Uuml;\0"    ,"\xc3\x9c\0"     },
115   {"&Yacute;\0"  ,"\xc3\x9d\0"     },
116   {"&THORN;\0"   ,"\xc3\x9e\0"     },
117   {"&szlig;\0"   ,"\xc3\x9f\0"     },
118   {"&agrave;\0"  ,"\xc3\xa0\0"     },
119   {"&aacute;\0"  ,"\xc3\xa1\0"     },
120   {"&acirc;\0"   ,"\xc3\xa2\0"     },
121   {"&atilde;\0"  ,"\xc3\xa3\0"     },
122   {"&auml;\0"    ,"\xc3\xa4\0"     },
123   {"&aring;\0"   ,"\xc3\xa5\0"     },
124   {"&aelig;\0"   ,"\xc3\xa6\0"     },
125   {"&ccedil;\0"  ,"\xc3\xa7\0"     },
126   {"&egrave;\0"  ,"\xc3\xa8\0"     },
127   {"&eacute;\0"  ,"\xc3\xa9\0"     },
128   {"&ecirc;\0"   ,"\xc3\xaa\0"     },
129   {"&euml;\0"    ,"\xc3\xab\0"     },
130   {"&igrave;\0"  ,"\xc3\xac\0"     },
131   {"&iacute;\0"  ,"\xc3\xad\0"     },
132   {"&icirc;\0"   ,"\xc3\xae\0"     },
133   {"&iuml;\0"    ,"\xc3\xaf\0"     },
134   {"&eth;\0"     ,"\xc3\xb0\0"     },
135   {"&ntilde;\0"  ,"\xc3\xb1\0"     },
136   {"&ograve;\0"  ,"\xc3\xb2\0"     },
137   {"&oacute;\0"  ,"\xc3\xb3\0"     },
138   {"&ocirc;\0"   ,"\xc3\xb4\0"     },
139   {"&otilde;\0"  ,"\xc3\xb5\0"     },
140   {"&ouml;\0"    ,"\xc3\xb6\0"     },
141   {"&divide;\0"  ,"\xc3\xb7\0"     },
142   {"&oslash;\0"  ,"\xc3\xb8\0"     },
143   {"&ugrave;\0"  ,"\xc3\xb9\0"     },
144   {"&uacute;\0"  ,"\xc3\xba\0"     },
145   {"&ucirc;\0"   ,"\xc3\xbb\0"     },
146   {"&uuml;\0"    ,"\xc3\xbc\0"     },
147   {"&yacute;\0"  ,"\xc3\xbd\0"     },
148   {"&thorn;\0"   ,"\xc3\xbe\0"     },
149   {"&yuml;\0"    ,"\xc3\xbf\0"     },
150   {"&OElig;\0"   ,"\xc5\x92\0"     },
151   {"&oelig;\0"   ,"\xc5\x93\0"     },
152   {"&Scaron;\0"  ,"\xc5\xa0\0"     },
153   {"&scaron;\0"  ,"\xc5\xa1\0"     },
154   {"&Yuml;\0"    ,"\xc5\xb8\0"     },
155   {"&fnof;\0"    ,"\xc6\x92\0"     },
156   {"&circ;\0"    ,"\xcb\x86\0"     },
157   {"&tilde;\0"   ,"\xcb\x9c\0"     },
158   {"&Alpha;\0"   ,"\xce\x91\0"     },
159   {"&Beta;\0"    ,"\xce\x92\0"     },
160   {"&Gamma;\0"   ,"\xce\x93\0"     },
161   {"&Delta;\0"   ,"\xce\x94\0"     },
162   {"&Epsilon;\0" ,"\xce\x95\0"     },
163   {"&Zeta;\0"    ,"\xce\x96\0"     },
164   {"&Eta;\0"     ,"\xce\x97\0"     },
165   {"&Theta;\0"   ,"\xce\x98\0"     },
166   {"&Iota;\0"    ,"\xce\x99\0"     },
167   {"&Kappa;\0"   ,"\xce\x9a\0"     },
168   {"&Lambda;\0"  ,"\xce\x9b\0"     },
169   {"&Mu;\0"      ,"\xce\x9c\0"     },
170   {"&Nu;\0"      ,"\xce\x9d\0"     },
171   {"&Xi;\0"      ,"\xce\x9e\0"     },
172   {"&Omicron;\0" ,"\xce\x9f\0"     },
173   {"&Pi;\0"      ,"\xce\xa0\0"     },
174   {"&Rho;\0"     ,"\xce\xa1\0"     },
175   {"&Sigma;\0"   ,"\xce\xa3\0"     },
176   {"&Tau;\0"     ,"\xce\xa4\0"     },
177   {"&Upsilon;\0" ,"\xce\xa5\0"     },
178   {"&Phi;\0"     ,"\xce\xa6\0"     },
179   {"&Chi;\0"     ,"\xce\xa7\0"     },
180   {"&Psi;\0"     ,"\xce\xa8\0"     },
181   {"&Omega;\0"   ,"\xce\xa9\0"     },
182   {"&alpha;\0"   ,"\xce\xb1\0"     },
183   {"&beta;\0"    ,"\xce\xb2\0"     },
184   {"&gamma;\0"   ,"\xce\xb3\0"     },
185   {"&delta;\0"   ,"\xce\xb4\0"     },
186   {"&epsilon;\0" ,"\xce\xb5\0"     },
187   {"&zeta;\0"    ,"\xce\xb6\0"     },
188   {"&eta;\0"     ,"\xce\xb7\0"     },
189   {"&theta;\0"   ,"\xce\xb8\0"     },
190   {"&iota;\0"    ,"\xce\xb9\0"     },
191   {"&kappa;\0"   ,"\xce\xba\0"     },
192   {"&lambda;\0"  ,"\xce\xbb\0"     },
193   {"&mu;\0"      ,"\xce\xbc\0"     },
194   {"&nu;\0"      ,"\xce\xbd\0"     },
195   {"&xi;\0"      ,"\xce\xbe\0"     },
196   {"&omicron;\0" ,"\xce\xbf\0"     },
197   {"&pi;\0"      ,"\xcf\x80\0"     },
198   {"&rho;\0"     ,"\xcf\x81\0"     },
199   {"&sigmaf;\0"  ,"\xcf\x82\0"     },
200   {"&sigma;\0"   ,"\xcf\x83\0"     },
201   {"&tau;\0"     ,"\xcf\x84\0"     },
202   {"&upsilon;\0" ,"\xcf\x85\0"     },
203   {"&phi;\0"     ,"\xcf\x86\0"     },
204   {"&chi;\0"     ,"\xcf\x87\0"     },
205   {"&psi;\0"     ,"\xcf\x88\0"     },
206   {"&omega;\0"   ,"\xcf\x89\0"     },
207   {"&thetasym;\0","\xcf\x91\0"     },
208   {"&upsih;\0"   ,"\xcf\x92\0"     },
209   {"&piv;\0"     ,"\xcf\x96\0"     },
210   {"&ensp;\0"    ,"\xe2\x80\x82\0" },
211   {"&emsp;\0"    ,"\xe2\x80\x83\0" },
212   {"&thinsp;\0"  ,"\xe2\x80\x89\0" },
213   {"&zwnj;\0"    ,"\xe2\x80\x8c\0" },
214   {"&zwj;\0"     ,"\xe2\x80\x8d\0" },
215   {"&lrm;\0"     ,"\xe2\x80\x8e\0" },
216   {"&rlm;\0"     ,"\xe2\x80\x8f\0" },
217   {"&ndash;\0"   ,"\xe2\x80\x93\0" },
218   {"&mdash;\0"   ,"\xe2\x80\x94\0" },
219   {"&lsquo;\0"   ,"\xe2\x80\x98\0" },
220   {"&rsquo;\0"   ,"\xe2\x80\x99\0" },
221   {"&sbquo;\0"   ,"\xe2\x80\x9a\0" },
222   {"&ldquo;\0"   ,"\xe2\x80\x9c\0" },
223   {"&rdquo;\0"   ,"\xe2\x80\x9d\0" },
224   {"&bdquo;\0"   ,"\xe2\x80\x9e\0" },
225   {"&dagger;\0"  ,"\xe2\x80\xa0\0" },
226   {"&Dagger;\0"  ,"\xe2\x80\xa1\0" },
227   {"&bull;\0"    ,"\xe2\x80\xa2\0" },
228   {"&hellip;\0"  ,"\xe2\x80\xa6\0" },
229   {"&permil;\0"  ,"\xe2\x80\xb0\0" },
230   {"&prime;\0"   ,"\xe2\x80\xb2\0" },
231   {"&Prime;\0"   ,"\xe2\x80\xb3\0" },
232   {"&lsaquo;\0"  ,"\xe2\x80\xb9\0" },
233   {"&rsaquo;\0"  ,"\xe2\x80\xba\0" },
234   {"&oline;\0"   ,"\xe2\x80\xbe\0" },
235   {"&frasl;\0"   ,"\xe2\x81\x84\0" },
236   {"&euro;\0"    ,"\xe2\x82\xac\0" },
237   {"&image;\0"   ,"\xe2\x84\x91\0" },
238   {"&weierp;\0"  ,"\xe2\x84\x98\0" },
239   {"&real;\0"    ,"\xe2\x84\x9c\0" },
240   {"&trade;\0"   ,"\xe2\x84\xa2\0" },
241   {"&alefsym;\0" ,"\xe2\x84\xb5\0" },
242   {"&larr;\0"    ,"\xe2\x86\x90\0" },
243   {"&uarr;\0"    ,"\xe2\x86\x91\0" },
244   {"&rarr;\0"    ,"\xe2\x86\x92\0" },
245   {"&darr;\0"    ,"\xe2\x86\x93\0" },
246   {"&harr;\0"    ,"\xe2\x86\x94\0" },
247   {"&crarr;\0"   ,"\xe2\x86\xb5\0" },
248   {"&lArr;\0"    ,"\xe2\x87\x90\0" },
249   {"&uArr;\0"    ,"\xe2\x87\x91\0" },
250   {"&rArr;\0"    ,"\xe2\x87\x92\0" },
251   {"&dArr;\0"    ,"\xe2\x87\x93\0" },
252   {"&hArr;\0"    ,"\xe2\x87\x94\0" },
253   {"&forall;\0"  ,"\xe2\x88\x80\0" },
254   {"&part;\0"    ,"\xe2\x88\x82\0" },
255   {"&exist;\0"   ,"\xe2\x88\x83\0" },
256   {"&empty;\0"   ,"\xe2\x88\x85\0" },
257   {"&nabla;\0"   ,"\xe2\x88\x87\0" },
258   {"&isin;\0"    ,"\xe2\x88\x88\0" },
259   {"&notin;\0"   ,"\xe2\x88\x89\0" },
260   {"&ni;\0"      ,"\xe2\x88\x8b\0" },
261   {"&prod;\0"    ,"\xe2\x88\x8f\0" },
262   {"&sum;\0"     ,"\xe2\x88\x91\0" },
263   {"&minus;\0"   ,"\xe2\x88\x92\0" },
264   {"&lowast;\0"  ,"\xe2\x88\x97\0" },
265   {"&radic;\0"   ,"\xe2\x88\x9a\0" },
266   {"&prop;\0"    ,"\xe2\x88\x9d\0" },
267   {"&infin;\0"   ,"\xe2\x88\x9e\0" },
268   {"&ang;\0"     ,"\xe2\x88\xa0\0" },
269   {"&and;\0"     ,"\xe2\x88\xa7\0" },
270   {"&or;\0"      ,"\xe2\x88\xa8\0" },
271   {"&cap;\0"     ,"\xe2\x88\xa9\0" },
272   {"&cup;\0"     ,"\xe2\x88\xaa\0" },
273   {"&int;\0"     ,"\xe2\x88\xab\0" },
274   {"&there4;\0"  ,"\xe2\x88\xb4\0" },
275   {"&sim;\0"     ,"\xe2\x88\xbc\0" },
276   {"&cong;\0"    ,"\xe2\x89\x85\0" },
277   {"&asymp;\0"   ,"\xe2\x89\x88\0" },
278   {"&ne;\0"      ,"\xe2\x89\xa0\0" },
279   {"&equiv;\0"   ,"\xe2\x89\xa1\0" },
280   {"&le;\0"      ,"\xe2\x89\xa4\0" },
281   {"&ge;\0"      ,"\xe2\x89\xa5\0" },
282   {"&sub;\0"     ,"\xe2\x8a\x82\0" },
283   {"&sup;\0"     ,"\xe2\x8a\x83\0" },
284   {"&nsub;\0"    ,"\xe2\x8a\x84\0" },
285   {"&sube;\0"    ,"\xe2\x8a\x86\0" },
286   {"&supe;\0"    ,"\xe2\x8a\x87\0" },
287   {"&oplus;\0"   ,"\xe2\x8a\x95\0" },
288   {"&otimes;\0"  ,"\xe2\x8a\x97\0" },
289   {"&perp;\0"    ,"\xe2\x8a\xa5\0" },
290   {"&sdot;\0"    ,"\xe2\x8b\x85\0" },
291   {"&lceil;\0"   ,"\xe2\x8c\x88\0" },
292   {"&rceil;\0"   ,"\xe2\x8c\x89\0" },
293   {"&lfloor;\0"  ,"\xe2\x8c\x8a\0" },
294   {"&rfloor;\0"  ,"\xe2\x8c\x8b\0" },
295   {"&loz;\0"     ,"\xe2\x97\x8a\0" },
296   {"&spades;\0"  ,"\xe2\x99\xa0\0" },
297   {"&clubs;\0"   ,"\xe2\x99\xa3\0" },
298   {"&hearts;\0"  ,"\xe2\x99\xa5\0" },
299   {"&diams;\0"   ,"\xe2\x99\xa6\0" },
300   {"&lang;\0"    ,"\xe2\x9f\xa8\0" },
301   {"&rang;\0"    ,"\xe2\x9f\xa9\0" }
302 };
303 // clang-format on
304
305 const std::size_t XHTMLENTITY_LOOKUP_COUNT = (sizeof(XHTMLEntityLookupTable)) / (sizeof(XHTMLEntityLookup));
306
307 } // unnamed namespace
308
309 const char* const NamedEntityToUtf8(const char* const markupText, unsigned int len)
310 {
311   // finding if given XHTML named entity is supported or not
312   for(size_t i = 0; i < XHTMLENTITY_LOOKUP_COUNT; ++i)
313   {
314     unsigned int entityLen = strlen(XHTMLEntityLookupTable[i].entityName);
315     if(len == entityLen)
316     {
317       if(strncmp(markupText, XHTMLEntityLookupTable[i].entityName, len) == 0) // if named Entity found in table
318       {
319         return XHTMLEntityLookupTable[i].entityCode;
320       }
321     }
322   }
323   return NULL;
324 }
325
326 } // namespace  Text
327
328 } // namespace  Toolkit
329
330 } // namespace  Dali