diff options
author | Linus Nielsen Feltzing <linus@haxx.se> | 2005-08-08 19:23:28 +0000 |
---|---|---|
committer | Linus Nielsen Feltzing <linus@haxx.se> | 2005-08-08 19:23:28 +0000 |
commit | 41a53d2c1c536d20823f357af887c9ff74991efd (patch) | |
tree | 365987aa7fdada4786fc280b50319d0cb1bb2acb /firmware | |
parent | 1874a33298f6b639893e4bc8e556fd71e2c068d3 (diff) | |
download | rockbox-41a53d2c1c536d20823f357af887c9ff74991efd.tar.gz rockbox-41a53d2c1c536d20823f357af887c9ff74991efd.zip |
Patch #783877 by Gadi Cohen updated by Naftali Goldstein - Bidirectional text support for Hebrew and Arabic
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@7292 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware')
-rw-r--r-- | firmware/SOURCES | 1 | ||||
-rw-r--r-- | firmware/bidi.c | 184 | ||||
-rw-r--r-- | firmware/drivers/lcd-h100.c | 4 | ||||
-rw-r--r-- | firmware/drivers/lcd-recorder.c | 4 | ||||
-rw-r--r-- | firmware/export/bidi.h | 23 |
5 files changed, 216 insertions, 0 deletions
diff --git a/firmware/SOURCES b/firmware/SOURCES index be471fb51b..84f54e56a3 100644 --- a/firmware/SOURCES +++ b/firmware/SOURCES | |||
@@ -49,6 +49,7 @@ drivers/lcd-player-charset.c | |||
49 | drivers/lcd-player.c | 49 | drivers/lcd-player.c |
50 | #endif | 50 | #endif |
51 | #ifdef HAVE_LCD_BITMAP | 51 | #ifdef HAVE_LCD_BITMAP |
52 | bidi.c | ||
52 | #if LCD_DEPTH == 2 | 53 | #if LCD_DEPTH == 2 |
53 | drivers/lcd-h100.c | 54 | drivers/lcd-h100.c |
54 | #elif LCD_DEPTH == 1 | 55 | #elif LCD_DEPTH == 1 |
diff --git a/firmware/bidi.c b/firmware/bidi.c new file mode 100644 index 0000000000..765d3dab24 --- /dev/null +++ b/firmware/bidi.c | |||
@@ -0,0 +1,184 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2005 by Gadi Cohen | ||
11 | * | ||
12 | * Largely based on php_hebrev by Zeev Suraski <zeev@php.net> | ||
13 | * Heavily modified by Gadi Cohen aka Kinslayer <dragon@wastelands.net> | ||
14 | * | ||
15 | * All files in this archive are subject to the GNU General Public License. | ||
16 | * See the file COPYING in the source tree root for full license agreement. | ||
17 | * | ||
18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
19 | * KIND, either express or implied. | ||
20 | * | ||
21 | ****************************************************************************/ | ||
22 | #include <stdio.h> | ||
23 | #include <string.h> | ||
24 | #include <ctype.h> | ||
25 | #include "file.h" | ||
26 | #include "lcd.h" | ||
27 | |||
28 | #define _HEB_BUFFER_LENGTH MAX_PATH + LCD_WIDTH/2 + 3 + 2 + 2 | ||
29 | #define _HEB_BLOCK_TYPE_ENG 1 | ||
30 | #define _HEB_BLOCK_TYPE_HEB 0 | ||
31 | #define _HEB_ORIENTATION_LTR 1 | ||
32 | #define _HEB_ORIENTATION_RTL 0 | ||
33 | |||
34 | #define ischar(c) (((((unsigned char) c)>=193) && (((unsigned char) c)<=250)) ? 1 : 0) | ||
35 | #define _isblank(c) (((((unsigned char) c)==' ' || ((unsigned char) c)=='\t')) ? 1 : 0) | ||
36 | #define _isnewline(c) (((((unsigned char) c)=='\n' || ((unsigned char) c)=='\r')) ? 1 : 0) | ||
37 | #define XOR(a,b) ((a||b) && !(a&&b)) | ||
38 | |||
39 | bool bidi_support_enabled = false; | ||
40 | |||
41 | unsigned char *bidi_l2v(const unsigned char *str, int orientation) | ||
42 | { | ||
43 | static unsigned char buf_heb_str[_HEB_BUFFER_LENGTH]; | ||
44 | static unsigned char buf_broken_str[_HEB_BUFFER_LENGTH]; | ||
45 | const unsigned char *tmp; | ||
46 | unsigned char *heb_str, *target, *opposite_target, *broken_str; | ||
47 | int block_start, block_end, block_type, block_length, i; | ||
48 | int block_ended; | ||
49 | long max_chars=0; | ||
50 | int begin, end, char_count, orig_begin; | ||
51 | |||
52 | if (!str || !*str) | ||
53 | return ""; | ||
54 | |||
55 | tmp = str; | ||
56 | block_start=block_end=0; | ||
57 | block_ended=0; | ||
58 | |||
59 | heb_str = buf_heb_str; | ||
60 | if (orientation) { | ||
61 | target = heb_str; | ||
62 | opposite_target = heb_str + strlen(str); | ||
63 | } else { | ||
64 | target = heb_str + strlen(str); | ||
65 | opposite_target = heb_str; | ||
66 | *target = 0; | ||
67 | target--; | ||
68 | } | ||
69 | |||
70 | block_length=0; | ||
71 | if (ischar(*tmp)) | ||
72 | block_type = _HEB_BLOCK_TYPE_HEB; | ||
73 | else | ||
74 | block_type = _HEB_BLOCK_TYPE_ENG; | ||
75 | |||
76 | do { | ||
77 | while((XOR(ischar((int)*(tmp+1)),block_type) | ||
78 | || _isblank((int)*(tmp+1)) || ispunct((int)*(tmp+1)) | ||
79 | || (int)*(tmp+1)=='\n') | ||
80 | && block_end<(int)strlen(str)-1) { | ||
81 | tmp++; | ||
82 | block_end++; | ||
83 | block_length++; | ||
84 | } | ||
85 | |||
86 | if (block_type != orientation) { | ||
87 | while ((_isblank((int)*tmp) || ispunct((int)*tmp)) | ||
88 | && *tmp!='/' && *tmp!='-' && block_end>block_start) { | ||
89 | tmp--; | ||
90 | block_end--; | ||
91 | } | ||
92 | } | ||
93 | |||
94 | for (i=block_start; i<=block_end; i++) { | ||
95 | *target = (block_type == orientation) ? *(str+i) : *(str+block_end-i+block_start); | ||
96 | if (block_type!=orientation) { | ||
97 | switch (*target) { | ||
98 | case '(': | ||
99 | *target = ')'; | ||
100 | break; | ||
101 | case ')': | ||
102 | *target = '('; | ||
103 | break; | ||
104 | default: | ||
105 | break; | ||
106 | } | ||
107 | } | ||
108 | target += orientation ? 1 : -1; | ||
109 | } | ||
110 | block_type = !block_type; | ||
111 | block_start=block_end+1; | ||
112 | } while(block_end<(int)strlen(str)-1); | ||
113 | |||
114 | broken_str = buf_broken_str; | ||
115 | begin=end=strlen(str)-1; | ||
116 | target = broken_str; | ||
117 | |||
118 | while (1) { | ||
119 | char_count=0; | ||
120 | while ((!max_chars || char_count<max_chars) && begin>0) { | ||
121 | char_count++; | ||
122 | begin--; | ||
123 | if (begin<=0 || _isnewline(heb_str[begin])) { | ||
124 | while(begin>0 && _isnewline(heb_str[begin-1])) { | ||
125 | begin--; | ||
126 | char_count++; | ||
127 | } | ||
128 | break; | ||
129 | } | ||
130 | } | ||
131 | if (char_count==max_chars) { /* try to avoid breaking words */ | ||
132 | int new_char_count = char_count; | ||
133 | int new_begin = begin; | ||
134 | |||
135 | while (new_char_count>0) { | ||
136 | if (_isblank(heb_str[new_begin]) || | ||
137 | _isnewline(heb_str[new_begin])) { | ||
138 | break; | ||
139 | } | ||
140 | new_begin++; | ||
141 | new_char_count--; | ||
142 | } | ||
143 | if (new_char_count>0) { | ||
144 | char_count=new_char_count; | ||
145 | begin=new_begin; | ||
146 | } | ||
147 | } | ||
148 | orig_begin=begin; | ||
149 | |||
150 | if (_isblank(heb_str[begin])) { | ||
151 | heb_str[begin]='\n'; | ||
152 | } | ||
153 | |||
154 | /* skip leading newlines */ | ||
155 | while (begin<=end && _isnewline(heb_str[begin])) { | ||
156 | begin++; | ||
157 | } | ||
158 | |||
159 | /* copy content */ | ||
160 | for (i=begin; i<=end; i++) { | ||
161 | *target = heb_str[i]; | ||
162 | target++; | ||
163 | } | ||
164 | |||
165 | for (i=orig_begin; i<=end && _isnewline(heb_str[i]); i++) { | ||
166 | *target = heb_str[i]; | ||
167 | target++; | ||
168 | } | ||
169 | begin=orig_begin; | ||
170 | |||
171 | if (begin<=0) { | ||
172 | *target = 0; | ||
173 | break; | ||
174 | } | ||
175 | begin--; | ||
176 | end=begin; | ||
177 | } | ||
178 | return broken_str; | ||
179 | } | ||
180 | |||
181 | void set_bidi_support(bool setting) | ||
182 | { | ||
183 | bidi_support_enabled = setting; | ||
184 | } | ||
diff --git a/firmware/drivers/lcd-h100.c b/firmware/drivers/lcd-h100.c index 5bdb08abd3..bfdceedc35 100644 --- a/firmware/drivers/lcd-h100.c +++ b/firmware/drivers/lcd-h100.c | |||
@@ -28,6 +28,7 @@ | |||
28 | #include "debug.h" | 28 | #include "debug.h" |
29 | #include "system.h" | 29 | #include "system.h" |
30 | #include "font.h" | 30 | #include "font.h" |
31 | #include "bidi.h" | ||
31 | 32 | ||
32 | /*** definitions ***/ | 33 | /*** definitions ***/ |
33 | 34 | ||
@@ -992,6 +993,9 @@ static void lcd_putsxyofs(int x, int y, int ofs, const unsigned char *str) | |||
992 | int ch; | 993 | int ch; |
993 | struct font* pf = font_get(curfont); | 994 | struct font* pf = font_get(curfont); |
994 | 995 | ||
996 | if (bidi_support_enabled) | ||
997 | str = bidi_l2v(str, 1); | ||
998 | |||
995 | while ((ch = *str++) != '\0' && x < LCD_WIDTH) | 999 | while ((ch = *str++) != '\0' && x < LCD_WIDTH) |
996 | { | 1000 | { |
997 | int width; | 1001 | int width; |
diff --git a/firmware/drivers/lcd-recorder.c b/firmware/drivers/lcd-recorder.c index cdeb4f2959..53640ce649 100644 --- a/firmware/drivers/lcd-recorder.c +++ b/firmware/drivers/lcd-recorder.c | |||
@@ -28,6 +28,7 @@ | |||
28 | #include "system.h" | 28 | #include "system.h" |
29 | #include "font.h" | 29 | #include "font.h" |
30 | #include "hwcompat.h" | 30 | #include "hwcompat.h" |
31 | #include "bidi.h" | ||
31 | 32 | ||
32 | /*** definitions ***/ | 33 | /*** definitions ***/ |
33 | 34 | ||
@@ -846,6 +847,9 @@ static void lcd_putsxyofs(int x, int y, int ofs, const unsigned char *str) | |||
846 | int ch; | 847 | int ch; |
847 | struct font* pf = font_get(curfont); | 848 | struct font* pf = font_get(curfont); |
848 | 849 | ||
850 | if (bidi_support_enabled) | ||
851 | str = bidi_l2v(str, 1); | ||
852 | |||
849 | while ((ch = *str++) != '\0' && x < LCD_WIDTH) | 853 | while ((ch = *str++) != '\0' && x < LCD_WIDTH) |
850 | { | 854 | { |
851 | int width; | 855 | int width; |
diff --git a/firmware/export/bidi.h b/firmware/export/bidi.h new file mode 100644 index 0000000000..88e2eeb07e --- /dev/null +++ b/firmware/export/bidi.h | |||
@@ -0,0 +1,23 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2005 by Gadi Cohen | ||
11 | * | ||
12 | * All files in this archive are subject to the GNU General Public License. | ||
13 | * See the file COPYING in the source tree root for full license agreement. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | #ifndef BIDI_H | ||
20 | extern unsigned char *bidi_l2v(const unsigned char *str, int orientation); | ||
21 | extern bool bidi_support_enabled; | ||
22 | extern void set_bidi_support(bool setting); | ||
23 | #endif | ||