Updated screenshot for H10-5GB recording screen, the previous one was showing an...
[kugel-rb.git] / firmware / bidi.c
bloba7a3211c39db94131d115fafaf0e082467eb5683
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Gadi Cohen
12 * Largely based on php_hebrev by Zeev Suraski <zeev@php.net>
13 * Heavily modified by Gadi Cohen aka Kinslayer <dragon@wastelands.net>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
24 #include <stdio.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include "file.h"
28 #include "lcd.h"
29 #include "rbunicode.h"
30 #include "arabjoin.h"
31 #include "scroll_engine.h"
32 #include "bidi.h"
34 /* #define _HEB_BUFFER_LENGTH (MAX_PATH + LCD_WIDTH/2 + 3 + 2 + 2) * 2 */
35 #define _HEB_BLOCK_TYPE_ENG 1
36 #define _HEB_BLOCK_TYPE_HEB 0
37 #define _HEB_ORIENTATION_LTR 1
38 #define _HEB_ORIENTATION_RTL 0
40 #define ischar(c) ((c > 0x0589 && c < 0x0700) || \
41 (c >= 0xfb50 && c <= 0xfefc) ? 1 : 0)
42 #define _isblank(c) ((c==' ' || c=='\t') ? 1 : 0)
43 #define _isnewline(c) ((c=='\n' || c=='\r') ? 1 : 0)
44 #define XOR(a,b) ((a||b) && !(a&&b))
46 static const arab_t * arab_lookup(unsigned short uchar)
48 if (uchar >= 0x621 && uchar <= 0x63a)
49 return &(jointable[uchar - 0x621]);
50 if (uchar >= 0x640 && uchar <= 0x64a)
51 return &(jointable[uchar - 0x621 - 5]);
52 if (uchar >= 0x671 && uchar <= 0x6d5)
53 return &(jointable[uchar - 0x621 - 5 - 38]);
54 if (uchar == 0x200D) /* Support for the zero-width joiner */
55 return &zwj;
56 return 0;
59 static void arabjoin(unsigned short * stringprt, int length)
61 bool connected = false;
62 unsigned short * writeprt = stringprt;
64 const arab_t * prev = 0;
65 const arab_t * cur;
66 const arab_t * ligature = 0;
67 short uchar;
69 int i;
70 for (i = 0; i <= length; i++) {
71 cur = arab_lookup(uchar = *stringprt++);
73 /* Skip non-arabic chars */
74 if (cur == 0) {
75 if (prev) {
76 /* Finish the last char */
77 if (connected) {
78 *writeprt++ = prev->final;
79 connected = false;
80 } else
81 *writeprt++ = prev->isolated;
82 prev = 0;
83 *writeprt++ = uchar;
84 } else {
85 *writeprt++ = uchar;
87 continue;
90 /* nothing to do for arabic char if the previous was non-arabic */
91 if (prev == 0) {
92 prev = cur;
93 continue;
96 /* if it's LAM, check for LAM+ALEPH ligatures */
97 if (prev->isolated == 0xfedd) {
98 switch (cur->isolated) {
99 case 0xfe8d:
100 ligature = &(lamaleph[0]);
101 break;
102 case 0xfe87:
103 ligature = &(lamaleph[1]);
104 break;
105 case 0xfe83:
106 ligature = &(lamaleph[2]);
107 break;
108 case 0xfe81:
109 ligature = &(lamaleph[3]);
113 if (ligature) { /* replace the 2 glyphs by their ligature */
114 prev = ligature;
115 ligature = 0;
116 } else {
117 if (connected) { /* previous char has something connected to it */
118 if (prev->medial && cur->final) /* Can we connect to it? */
119 *writeprt++ = prev->medial;
120 else {
121 *writeprt++ = prev->final;
122 connected = false;
124 } else {
125 if (prev->initial && cur->final) { /* Can we connect to it? */
126 *writeprt++ = prev->initial;
127 connected = true;
128 } else
129 *writeprt++ = prev->isolated;
131 prev = cur;
136 unsigned short *bidi_l2v(const unsigned char *str, int orientation)
138 int length = utf8length(str);
139 static unsigned short utf16_buf[SCROLL_LINE_SIZE];
140 static unsigned short bidi_buf[SCROLL_LINE_SIZE];
141 unsigned short *heb_str, *target, *tmp; /* *broken_str */
142 int block_start, block_end, block_type, block_length, i;
144 long max_chars=0;
145 int begin, end, char_count, orig_begin;
147 tmp = str;
149 target = tmp = utf16_buf;
150 while (*str)
151 str = utf8decode(str, target++);
152 *target = 0;
154 if (target == utf16_buf) /* empty string */
155 return target;
157 /* properly join any arabic chars */
158 arabjoin(utf16_buf, length);
160 block_start=block_end=block_length=0;
162 heb_str = bidi_buf;
163 if (orientation) {
164 target = heb_str;
165 } else {
166 target = heb_str + length;
167 *target = 0;
168 target--;
171 if (ischar(*tmp))
172 block_type = _HEB_BLOCK_TYPE_HEB;
173 else
174 block_type = _HEB_BLOCK_TYPE_ENG;
176 do {
177 while((XOR(ischar(*(tmp+1)),block_type)
178 || _isblank(*(tmp+1)) || ispunct((int)*(tmp+1))
179 || *(tmp+1)=='\n')
180 && block_end < length-1) {
181 tmp++;
182 block_end++;
183 block_length++;
186 if (block_type != orientation) {
187 while ((_isblank(*tmp) || ispunct((int)*tmp))
188 && *tmp!='/' && *tmp!='-' && block_end>block_start) {
189 tmp--;
190 block_end--;
194 for (i=block_start; i<=block_end; i++) {
195 *target = (block_type == orientation) ?
196 *(utf16_buf+i) : *(utf16_buf+block_end-i+block_start);
197 if (block_type!=orientation) {
198 switch (*target) {
199 case '(':
200 *target = ')';
201 break;
202 case ')':
203 *target = '(';
204 break;
205 default:
206 break;
209 target += orientation ? 1 : -1;
211 block_type = !block_type;
212 block_start=block_end+1;
213 } while(block_end<length-1);
215 *target = 0;
217 #if 0 /* Is this code really necessary? */
218 broken_str = utf16_buf;
219 begin=end=length-1;
220 target = broken_str;
222 while (1) {
223 char_count=0;
224 while ((!max_chars || char_count<max_chars) && begin>0) {
225 char_count++;
226 begin--;
227 if (begin<=0 || _isnewline(heb_str[begin])) {
228 while(begin>0 && _isnewline(heb_str[begin-1])) {
229 begin--;
230 char_count++;
232 break;
235 if (char_count==max_chars) { /* try to avoid breaking words */
236 int new_char_count = char_count;
237 int new_begin = begin;
239 while (new_char_count>0) {
240 if (_isblank(heb_str[new_begin]) ||
241 _isnewline(heb_str[new_begin])) {
242 break;
244 new_begin++;
245 new_char_count--;
247 if (new_char_count>0) {
248 char_count=new_char_count;
249 begin=new_begin;
252 orig_begin=begin;
254 /* if (_isblank(heb_str[begin])) {
255 heb_str[begin]='\n';
256 } */
258 /* skip leading newlines */
259 while (begin<=end && _isnewline(heb_str[begin])) {
260 begin++;
263 /* copy content */
264 for (i=begin; i<=end; i++) {
265 *target = heb_str[i];
266 target++;
269 for (i=orig_begin; i<=end && _isnewline(heb_str[i]); i++) {
270 *target = heb_str[i];
271 target++;
273 begin=orig_begin;
275 if (begin<=0) {
276 *target = 0;
277 break;
279 begin--;
280 end=begin;
282 return broken_str;
283 #endif
284 return heb_str;