1 /* Functions for JISX0213 conversion.
2    Copyright (C) 2002-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #ifndef _JISX0213_H
20 #define _JISX0213_H	1
21 
22 #include <stdint.h>
23 
24 extern const uint16_t __jisx0213_to_ucs_combining[][2];
25 extern const uint16_t __jisx0213_to_ucs_main[120 * 94];
26 extern const uint32_t __jisx0213_to_ucs_pagestart[];
27 extern const int16_t __jisx0213_from_ucs_level1[2715];
28 extern const uint16_t __jisx0213_from_ucs_level2[];
29 
30 #define NELEMS(arr) (sizeof (arr) / sizeof (arr[0]))
31 
uint32_t(always_inline)32 static inline uint32_t
33 __attribute ((always_inline))
34 jisx0213_to_ucs4 (unsigned int row, unsigned int col)
35 {
36   uint32_t val;
37 
38   if (row >= 0x121 && row <= 0x17e)
39     row -= 289;
40   else if (row == 0x221)
41     row -= 451;
42   else if (row >= 0x223 && row <= 0x225)
43     row -= 452;
44   else if (row == 0x228)
45     row -= 454;
46   else if (row >= 0x22c && row <= 0x22f)
47     row -= 457;
48   else if (row >= 0x26e && row <= 0x27e)
49     row -= 519;
50   else
51     return 0x0000;
52 
53   if (col >= 0x21 && col <= 0x7e)
54     col -= 0x21;
55   else
56     return 0x0000;
57 
58   val = __jisx0213_to_ucs_main[row * 94 + col];
59   val = __jisx0213_to_ucs_pagestart[val >> 8] + (val & 0xff);
60   if (val == 0xfffd)
61     val = 0x0000;
62   return val;
63 }
64 
uint16_t(always_inline)65 static inline uint16_t
66 __attribute ((always_inline))
67 ucs4_to_jisx0213 (uint32_t ucs)
68 {
69   if (ucs < NELEMS (__jisx0213_from_ucs_level1) << 6)
70     {
71       int index1 = __jisx0213_from_ucs_level1[ucs >> 6];
72       if (index1 >= 0)
73 	return __jisx0213_from_ucs_level2[(index1 << 6) + (ucs & 0x3f)];
74     }
75   return 0x0000;
76 }
77 
78 static inline int
79 __attribute ((always_inline))
jisx0213_added_in_2004_p(uint16_t val)80 jisx0213_added_in_2004_p (uint16_t val)
81 {
82   /* From JISX 0213:2000 to JISX 0213:2004, 10 characters were added to
83      plane 1, and plane 2 was left unchanged.  See ISO-IR-233.  */
84   switch (val >> 8)
85     {
86     case 0x2e:
87       return val == 0x2e21;
88     case 0x2f:
89       return val == 0x2f7e;
90     case 0x4f:
91       return val == 0x4f54 || val == 0x4f7e;
92     case 0x74:
93       return val == 0x7427;
94     case 0x7e:
95       return val >= 0x7e7a && val <= 0x7e7e;
96     default:
97       return 0;
98     }
99 }
100 
101 #endif /* _JISX0213_H */
102