2005-08-08 19:23:28 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 by Gadi Cohen
|
|
|
|
*
|
|
|
|
* Largely based on php_hebrev by Zeev Suraski <zeev@php.net>
|
|
|
|
* Heavily modified by Gadi Cohen aka Kinslayer <dragon@wastelands.net>
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2005-08-08 19:23:28 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include "file.h"
|
|
|
|
#include "lcd.h"
|
2005-12-06 13:27:15 +00:00
|
|
|
#include "rbunicode.h"
|
|
|
|
#include "arabjoin.h"
|
2007-07-28 08:12:05 +00:00
|
|
|
#include "scroll_engine.h"
|
2008-05-03 08:35:14 +00:00
|
|
|
#include "bidi.h"
|
2005-08-08 19:23:28 +00:00
|
|
|
|
2007-07-30 22:33:31 +00:00
|
|
|
/* #define _HEB_BUFFER_LENGTH (MAX_PATH + LCD_WIDTH/2 + 3 + 2 + 2) * 2 */
|
2005-08-08 19:23:28 +00:00
|
|
|
#define _HEB_BLOCK_TYPE_ENG 1
|
|
|
|
#define _HEB_BLOCK_TYPE_HEB 0
|
|
|
|
#define _HEB_ORIENTATION_LTR 1
|
|
|
|
#define _HEB_ORIENTATION_RTL 0
|
|
|
|
|
2007-07-30 22:33:31 +00:00
|
|
|
#define ischar(c) ((c > 0x0589 && c < 0x0700) || \
|
|
|
|
(c >= 0xfb50 && c <= 0xfefc) ? 1 : 0)
|
2005-12-06 13:27:15 +00:00
|
|
|
#define _isblank(c) ((c==' ' || c=='\t') ? 1 : 0)
|
|
|
|
#define _isnewline(c) ((c=='\n' || c=='\r') ? 1 : 0)
|
2005-08-08 19:23:28 +00:00
|
|
|
#define XOR(a,b) ((a||b) && !(a&&b))
|
|
|
|
|
2008-09-23 18:46:27 +00:00
|
|
|
#ifndef BOOTLOADER
|
2008-05-03 08:35:14 +00:00
|
|
|
static const arab_t * arab_lookup(unsigned short uchar)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
|
|
|
if (uchar >= 0x621 && uchar <= 0x63a)
|
|
|
|
return &(jointable[uchar - 0x621]);
|
|
|
|
if (uchar >= 0x640 && uchar <= 0x64a)
|
|
|
|
return &(jointable[uchar - 0x621 - 5]);
|
|
|
|
if (uchar >= 0x671 && uchar <= 0x6d5)
|
|
|
|
return &(jointable[uchar - 0x621 - 5 - 38]);
|
|
|
|
if (uchar == 0x200D) /* Support for the zero-width joiner */
|
|
|
|
return ‍
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-03 08:35:14 +00:00
|
|
|
static void arabjoin(unsigned short * stringprt, int length)
|
|
|
|
{
|
2005-12-06 13:27:15 +00:00
|
|
|
bool connected = false;
|
|
|
|
unsigned short * writeprt = stringprt;
|
|
|
|
|
2005-12-06 15:04:48 +00:00
|
|
|
const arab_t * prev = 0;
|
|
|
|
const arab_t * cur;
|
|
|
|
const arab_t * ligature = 0;
|
2005-12-06 13:27:15 +00:00
|
|
|
short uchar;
|
2005-08-08 19:23:28 +00:00
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i <= length; i++) {
|
|
|
|
cur = arab_lookup(uchar = *stringprt++);
|
|
|
|
|
|
|
|
/* Skip non-arabic chars */
|
|
|
|
if (cur == 0) {
|
|
|
|
if (prev) {
|
|
|
|
/* Finish the last char */
|
|
|
|
if (connected) {
|
|
|
|
*writeprt++ = prev->final;
|
|
|
|
connected = false;
|
|
|
|
} else
|
|
|
|
*writeprt++ = prev->isolated;
|
|
|
|
prev = 0;
|
|
|
|
*writeprt++ = uchar;
|
|
|
|
} else {
|
|
|
|
*writeprt++ = uchar;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* nothing to do for arabic char if the previous was non-arabic */
|
|
|
|
if (prev == 0) {
|
|
|
|
prev = cur;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if it's LAM, check for LAM+ALEPH ligatures */
|
|
|
|
if (prev->isolated == 0xfedd) {
|
|
|
|
switch (cur->isolated) {
|
|
|
|
case 0xfe8d:
|
|
|
|
ligature = &(lamaleph[0]);
|
|
|
|
break;
|
|
|
|
case 0xfe87:
|
|
|
|
ligature = &(lamaleph[1]);
|
|
|
|
break;
|
|
|
|
case 0xfe83:
|
|
|
|
ligature = &(lamaleph[2]);
|
|
|
|
break;
|
|
|
|
case 0xfe81:
|
|
|
|
ligature = &(lamaleph[3]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ligature) { /* replace the 2 glyphs by their ligature */
|
|
|
|
prev = ligature;
|
|
|
|
ligature = 0;
|
|
|
|
} else {
|
|
|
|
if (connected) { /* previous char has something connected to it */
|
|
|
|
if (prev->medial && cur->final) /* Can we connect to it? */
|
|
|
|
*writeprt++ = prev->medial;
|
|
|
|
else {
|
|
|
|
*writeprt++ = prev->final;
|
|
|
|
connected = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (prev->initial && cur->final) { /* Can we connect to it? */
|
|
|
|
*writeprt++ = prev->initial;
|
|
|
|
connected = true;
|
|
|
|
} else
|
|
|
|
*writeprt++ = prev->isolated;
|
|
|
|
}
|
|
|
|
prev = cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-09-23 18:46:27 +00:00
|
|
|
#endif /* !BOOTLOADER */
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
unsigned short *bidi_l2v(const unsigned char *str, int orientation)
|
2005-08-08 19:23:28 +00:00
|
|
|
{
|
2006-12-17 15:40:13 +00:00
|
|
|
static unsigned short utf16_buf[SCROLL_LINE_SIZE];
|
2008-09-23 18:46:27 +00:00
|
|
|
unsigned short *target, *tmp;
|
|
|
|
#ifndef BOOTLOADER
|
2006-12-17 15:40:13 +00:00
|
|
|
static unsigned short bidi_buf[SCROLL_LINE_SIZE];
|
2008-09-23 18:46:27 +00:00
|
|
|
unsigned short *heb_str; /* *broken_str */
|
2005-08-08 19:23:28 +00:00
|
|
|
int block_start, block_end, block_type, block_length, i;
|
2008-09-23 18:46:27 +00:00
|
|
|
int length = utf8length(str);
|
2014-01-09 22:20:48 +00:00
|
|
|
length=length>=SCROLL_LINE_SIZE?SCROLL_LINE_SIZE-1:length;
|
2008-09-23 18:46:27 +00:00
|
|
|
#endif
|
2007-07-30 22:33:31 +00:00
|
|
|
/*
|
|
|
|
long max_chars=0;
|
|
|
|
int begin, end, char_count, orig_begin;
|
2005-08-08 19:23:28 +00:00
|
|
|
|
2007-07-30 22:33:31 +00:00
|
|
|
tmp = str;
|
|
|
|
*/
|
2005-12-06 13:27:15 +00:00
|
|
|
target = tmp = utf16_buf;
|
2014-01-09 22:20:48 +00:00
|
|
|
while (*str && target < &utf16_buf[SCROLL_LINE_SIZE-1])
|
2005-12-06 13:27:15 +00:00
|
|
|
str = utf8decode(str, target++);
|
|
|
|
*target = 0;
|
2005-08-08 19:23:28 +00:00
|
|
|
|
2008-09-23 18:46:27 +00:00
|
|
|
#ifdef BOOTLOADER
|
|
|
|
(void)orientation;
|
|
|
|
return utf16_buf;
|
2010-01-03 10:50:34 +00:00
|
|
|
|
2008-09-23 18:46:27 +00:00
|
|
|
#else /* !BOOTLOADER */
|
2006-10-21 00:04:57 +00:00
|
|
|
if (target == utf16_buf) /* empty string */
|
|
|
|
return target;
|
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
/* properly join any arabic chars */
|
|
|
|
arabjoin(utf16_buf, length);
|
2005-08-08 19:23:28 +00:00
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
block_start=block_end=block_length=0;
|
|
|
|
|
|
|
|
heb_str = bidi_buf;
|
2005-08-08 19:23:28 +00:00
|
|
|
if (orientation) {
|
|
|
|
target = heb_str;
|
|
|
|
} else {
|
2005-12-06 13:27:15 +00:00
|
|
|
target = heb_str + length;
|
2005-08-08 19:23:28 +00:00
|
|
|
*target = 0;
|
|
|
|
target--;
|
|
|
|
}
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2005-08-08 19:23:28 +00:00
|
|
|
if (ischar(*tmp))
|
|
|
|
block_type = _HEB_BLOCK_TYPE_HEB;
|
|
|
|
else
|
|
|
|
block_type = _HEB_BLOCK_TYPE_ENG;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2005-08-08 19:23:28 +00:00
|
|
|
do {
|
2005-12-06 13:27:15 +00:00
|
|
|
while((XOR(ischar(*(tmp+1)),block_type)
|
|
|
|
|| _isblank(*(tmp+1)) || ispunct((int)*(tmp+1))
|
|
|
|
|| *(tmp+1)=='\n')
|
|
|
|
&& block_end < length-1) {
|
|
|
|
tmp++;
|
|
|
|
block_end++;
|
|
|
|
block_length++;
|
2005-08-08 19:23:28 +00:00
|
|
|
}
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2005-08-08 19:23:28 +00:00
|
|
|
if (block_type != orientation) {
|
2005-12-06 13:27:15 +00:00
|
|
|
while ((_isblank(*tmp) || ispunct((int)*tmp))
|
2005-08-08 19:23:28 +00:00
|
|
|
&& *tmp!='/' && *tmp!='-' && block_end>block_start) {
|
|
|
|
tmp--;
|
|
|
|
block_end--;
|
|
|
|
}
|
|
|
|
}
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2005-08-08 19:23:28 +00:00
|
|
|
for (i=block_start; i<=block_end; i++) {
|
2007-07-30 22:33:31 +00:00
|
|
|
*target = (block_type == orientation) ?
|
|
|
|
*(utf16_buf+i) : *(utf16_buf+block_end-i+block_start);
|
2005-08-08 19:23:28 +00:00
|
|
|
if (block_type!=orientation) {
|
|
|
|
switch (*target) {
|
|
|
|
case '(':
|
|
|
|
*target = ')';
|
|
|
|
break;
|
|
|
|
case ')':
|
|
|
|
*target = '(';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
target += orientation ? 1 : -1;
|
|
|
|
}
|
|
|
|
block_type = !block_type;
|
|
|
|
block_start=block_end+1;
|
2005-12-06 13:27:15 +00:00
|
|
|
} while(block_end<length-1);
|
|
|
|
|
|
|
|
*target = 0;
|
|
|
|
|
|
|
|
#if 0 /* Is this code really necessary? */
|
|
|
|
broken_str = utf16_buf;
|
|
|
|
begin=end=length-1;
|
2005-08-08 19:23:28 +00:00
|
|
|
target = broken_str;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2005-08-08 19:23:28 +00:00
|
|
|
while (1) {
|
|
|
|
char_count=0;
|
|
|
|
while ((!max_chars || char_count<max_chars) && begin>0) {
|
|
|
|
char_count++;
|
|
|
|
begin--;
|
|
|
|
if (begin<=0 || _isnewline(heb_str[begin])) {
|
|
|
|
while(begin>0 && _isnewline(heb_str[begin-1])) {
|
|
|
|
begin--;
|
|
|
|
char_count++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (char_count==max_chars) { /* try to avoid breaking words */
|
|
|
|
int new_char_count = char_count;
|
|
|
|
int new_begin = begin;
|
|
|
|
|
|
|
|
while (new_char_count>0) {
|
|
|
|
if (_isblank(heb_str[new_begin]) ||
|
|
|
|
_isnewline(heb_str[new_begin])) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
new_begin++;
|
|
|
|
new_char_count--;
|
|
|
|
}
|
|
|
|
if (new_char_count>0) {
|
|
|
|
char_count=new_char_count;
|
|
|
|
begin=new_begin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
orig_begin=begin;
|
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
/* if (_isblank(heb_str[begin])) {
|
2005-08-08 19:23:28 +00:00
|
|
|
heb_str[begin]='\n';
|
2005-12-06 13:27:15 +00:00
|
|
|
} */
|
2005-08-08 19:23:28 +00:00
|
|
|
|
|
|
|
/* skip leading newlines */
|
|
|
|
while (begin<=end && _isnewline(heb_str[begin])) {
|
|
|
|
begin++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy content */
|
|
|
|
for (i=begin; i<=end; i++) {
|
|
|
|
*target = heb_str[i];
|
|
|
|
target++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=orig_begin; i<=end && _isnewline(heb_str[i]); i++) {
|
|
|
|
*target = heb_str[i];
|
|
|
|
target++;
|
|
|
|
}
|
|
|
|
begin=orig_begin;
|
|
|
|
|
|
|
|
if (begin<=0) {
|
|
|
|
*target = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
begin--;
|
|
|
|
end=begin;
|
|
|
|
}
|
|
|
|
return broken_str;
|
2005-12-06 13:27:15 +00:00
|
|
|
#endif
|
|
|
|
return heb_str;
|
2008-09-23 18:46:27 +00:00
|
|
|
#endif /* !BOOTLOADER */
|
2005-08-08 19:23:28 +00:00
|
|
|
}
|
|
|
|
|