MSW:Symbol Encoding

6. Symbol Encoding
Every symbol of the ISWA 2010 has a normative name as the symbol ID, which is 18 characters long. These names are overly verbose and are not used directly for encoding.

6.A. 16-bit Symbol Code
A 16-bit code is an integer between 0 and 65,535. This type of value is perfect for a primary key for database lookup or other integer index. Through a simple formula, each symbol has a unique 16-bit codepoint in the x-ISWA-2010 coded character set.

There are 652 BaseSymbols, numbered from 1 to 652. Each BaseSymbol can be visualized on a grid of 6 columns and 16 rows: for the 6 fills and 16 rotations. Each symbol can be identified by 3 values of BaseSymbol, column and row.

If we start on the first BaseSymbol grid, give the first symbol a code value of 1, incremented down the first column, continue to the next column, and continue through the remaining BaseSymbols; we end up with numbers from 1 to 62952.

Given any symbol with:
 * BaseSymbol number = n
 * Fill = f
 * Rotation = r

code = (n-1)*96 + (f-1)*16 + r

6.B. ASCII Symbol Key
The symbol key is a formatted string, 6 ASCII characters long, that identifies a symbol. The symbol key makes explicit features of the symbol otherwise only implicit in the symbol code.

The first character of the symbol key is always "S". This aids in parsing and human readability. The last 5 characters represent the symbol encoding in hexadecimal: base 16.

Base 10 is called decimal and is the most widely used numbering system. This uses 10 digits from 0 through 9. Hexadecimal numbers use base 16. This uses 16 digits, from 0 through 9 and from “a” through “f”.

Given any symbol with:
 * BaseSymbol number = n
 * Fill = f
 * Rotation = r
 * dechex = function that converts a decimal number to a hexadecimal number.

'''key = S. dechex(n+255). dechex(f-1). dechex(r-1)'''

6.C. Encoding Data
Most operations of layout and searching can be completed without requiring additional information outside of the sign text strings. There are 4 special cases that require access to outside information.

6.C.1. ID to Key
The symbol ID does not have an automatic conversion to symbol key without additional information. Every one of the 652 BaseSymbols has a unique mapping from BaseSymbol ID to BaseSymbol number. The data file includes 652 entries of the format “xx-xx-xxx-xx equals BS #”

6.C.2. Valid Fills and Rotations
Each BaseSymbol has 2 integer values that define validity for fills and rotations. The Binary Fills value is a 6-bit number from 1 to 63. This value defines which fills are valid for the BaseSymbol. The Binary Rotations value is a 16-bit number from 1 to 65,535. This value defines which rotations are valid for the BaseSymbol. The data file includes 652 entries that map each BaseSymbol to 2 different values: one for the Binary Fills value and one for the Binary Rotations value.

Given any symbol defined as a Symbol Key:
 * Symbol Key = “S” . H1 . H2 . H3 . H4 . H5
 * Fill Power = 2 ^ hexdec(H4)
 * Rotation Power = 2 ^ hexdec(H5)
 * Binary Fills = look up value for BaseSymbol
 * Binary Rotations = look up value for BaseSymbol

The symbol key represents a valid symbol if both:
 * (Fill Power BITWISE AND Binary Fills) > 0
 * (Rotation Power BITWISE AND Binary Rotations) > 0

6.C.3. BaseSymbol Variations
Most symbol IDs use a variation number of 1. This is the fourth number of the symbol ID. A few symbols utilize the variation number to create a relationship between different BaseSymbols: such as arrows of different length. When BaseSymbols are related through the variation number, the first 3 numbers of the symbol ID will be the same, and the variation number will start at 1 for the first BaseSymbol and increase by 1 for each subsequent related BaseSymbol. The data file includes 652 entries that list the number of variations for each BaseSymbol. BaseSymbols that are related by variation will have the same number of variations. The symbol ID must be referenced to determine the variation number for any symbol.

6.C.4. Symbol Sizes
Every symbol of the ISWA 2010 has a specific width and height. These values are needed for string preprocessing that includes centering or sizing. These values can be determined several different ways.

Image Analysis The PNG or SVG image can be analyzed for each symbol to determine the width and height.

ID or Key Lookup Using the 16-bit symbol code or symbol key, the width and height can be accessed from a database or other data storage.

6.D. Transformations
Many symbols can be transformed by rotation, mirror, flop, or variation.

Given any symbol defined as a Symbol Key:
 * Symbol Key = “S” . H1 . H2 . H3 . H4 . H5
 * Fill = hexdec(H4) + 1
 * Rotation = hexdec(H5) + 1;

New symbol keys should always be checked for validity. If the symbol key is not valid, repeat the transformation.

6.D.1. Reflection Transformation
If Binary Rotations > 255 there are more than 8 rotations:
 * If Rotation > 8
 * Rotation = Rotation - 8
 * Else if Rotation <= 8
 * Rotation = Rotation + 8

If Binary Rotations < 256 there are 8 or less rotations
 * Rotation 1 mirrors 1
 * Rotation 2 mirrors 8
 * Rotation 3 mirrors 7
 * Rotation 4 mirrors 6
 * Rotation 5 mirrors 5

'''Symbol Key = “S”. H1. H2. H3. dechex(Rotation - 1). H5'''

6.D.2. Rotation Transformation
Rotations 1 thru 8 loop Rotations 9 thru 16 loop

If Rotation < 9
 * Clockwise
 * Rotation = Rotation - 1
 * Counter Clockwise
 * Rotation = Rotation + 1

If Rotation > 8
 * Clockwise
 * Rotation = Rotation + 1
 * Counter Clockwise
 * Rotation = Rotation - 1

'''Symbol Key = “S”. H1. H2. H3. dechex(Rotation - 1). H5'''

6.D.3. Flop Transformation
Fills 1 thru 6 loop Fill = Fill + 1

'''Symbol Key = “S”. H1. H2. H3. H4. dechex(Fill - 1)'''

6.D.4. Variation Transformation
Each BaseSymbol has 2 numbers that deal with variations: the first is the specific variation number in the Symbol ID and the second is the general number of variations found in a data file.

Given any symbol:
 * Variation number = symbol specific value from the symbol ID
 * Number of variations = BaseSymbol value from a data file

The variation transformation is available if the number of variations is greater than 1:
 * If (variation number < number of variations)
 * Variation number = variation number + 1
 * Else if (variation number = number of variations)
 * Variation number = 1