As I promised earlier, I will post today the algorithm used to
perform Auto-Shaping for Arabic Characters.

What is Auto-Shaping?
=====================

Every character in Arabic Alphabet has maximum 4 different shapes.
The shape of the character is determined according to its type and
the type of the previous and next character. Even if the character
may have 4 different shapes, it is still the same character and
should not be altered in the source. It should be changed only for
output (display).

Due to this complexity, there has to be 2 code pages for every
Arabic Font to be used. The Virtual Code Page is the table of
Standard ASCII Codes which has the Arabic Characters in their
isolated form (disconnected). The Actual Code Page is the table that
has the actual codes and bitmap images for all the shapes of Arabic
Character. These Codes are arbitrary and does not have to follow any
standard and it represents the Font used. The Virtual Code Page must
follow a Standard regardless of the font used.

Example:
=========

Let us take the most complex case for the Letter Ain "غ". Based on
MS-Windows Standard, it has the ASCII Code 218, and Unicode 1593.
This is called the Virtual ASCII Code. Using the Character Map Tool
from Windows XP, following are the complete attributes of this
character:

Type: Left/Right Joinable
Left Joinable Bit: 1
Right Joinable Bit: 1
Shape Code Offset: 65225 (based on the MS-Windows Font "Simplified
Arabic")
It has 4 shapes codes, based on the MS-Windows Font "Simplified
Arabic":
1. Isolated: code=65225, shape = غ
2. Right Joinable: code=65226, shape = ﻊ
3. Left Joinable: code=65227, shape = ﻋ
4. Connected: code=65228, shape = ﻌ

(Note: Use Windows Arabic Encoding if you wish to see the character
shapes properly)

Auto-Shaping algorithm will determine the shape code of the Arabic
Character based on the previous and next characters.

Algorithm:
==========
For simplicity, we will follow MS-Windows Standard, and we will
assume that we have one Font Table.

Data Structure:
===============

Table1: ASCII Attribute Table for Arabic Characters. A Flat Table
with 4 columns:
1. ASCII Code (or Unicode, as you wish).
2. Left Joinable Bit (LJB): 0=Not Left Joinable, 1=Left Joinable.
3. Right Joinable Bit (RJB): 0=Not Right Joinable, 1=Right Joinable.
4. Shape Code Offset: the base Index Number of the shape codes in
the Font Table.


Table2: The Shape Codes (Font Table) for all possible shapes for
every character. Every Arabic Character will have exactly 4 entries
in this table, and must be ordered as follows:

1. Isolated Shape Code (Base Offset),
2. Right Joinable Shape Code,
3. Left Joinable Shape Code,
4. Connected Shape Code (Left/Right Connected).

The Table2 will have 2 columns:

1. Code: The code number of the Arabic Letter Shape.
2. Bitmap: the Bit Map Image or the actual shape (stream of bytes).

Table2 does not have to follow any standard in my personal opinion,
because it affects the shape of the character on the platform used.

Logic:
======

Function: Get Shape Code
Objective: Get the Shape Code of a Given Arabic ASCII Code.

Input: Standard ASCII Codes of:
1. Previous Character = PC,
2. Current Character = CC,
3. Next Character = NC.

Output: Shape Code of the Arabic Character as per the Font Table
Table2.

Process:

LJB_PC = Using CC as search key, lookup LJB of PC from Table1

RJB_NC = Using CC as search key, lookup RJB of NC from Table1.

Base Offset of Shape Code (Offset) =
    Using CC as search key, Lookup of Shape Code Offset from Table1.

Index of Shape Code (ISC) =
  Binary Code of RJB_NC & LJB_PC
  (Concatenate the bits and get the decimal index)

Note: ISC must be between 0 and 4 (Binary 00, 01, 10, or 11).

Return Final Shape Code = Offset + ISC.

End of function "Get Shape Code"

------

Function Format Arabic String

Input: Text String to be Formatted (InpStr)

Output: Resultant Formatted String (OutStr).

Note: Input String must NOT be changed.
OutStr = ""
PC = 0 (Nothing)
For i = 1 to len(InpStr)
  CC = asc(mid(InpStr, i, 1))
  if CC is an Arabic Character then
    if i < len(InpStr) then
      NC = asc(mid(InpStr, i+1, 1))
    else
      NC = 0 (nothing)
    end if
  else
    NC = 0
  end if
  Output Character (OC) = GetShapeCode(PC, CC, NC)
  OC_Shape = Lookup Shape of OC from Table2.
  OutStr = OutStr & OC_Shape
  PC = CC
Next

Return OutStr.

End Function Format Arabic String.



--
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to