Second version. Complete redesign, based on C++ classes to ensure endian

correctness.
This commit is contained in:
Behdad Esfahbod 2006-12-21 22:31:10 -05:00
parent f78e70c301
commit 6b4ce01da1
1 changed files with 202 additions and 92 deletions

View File

@ -1,4 +1,5 @@
#include <stdint.h>
#include <glib.h>
/*
*
@ -6,111 +7,220 @@
*
*/
/*
* Data Types:
* Data Types
*/
/* "All OpenType fonts use Motorola-style byte ordering (Big Endian)"
* So we define the following hb_be_* types as structs/unions, to
* disallow accidental access to them as an integer. */
typedef union {
uint8_t u8;
uint8_t v;
} hb_be_uint8_t;
typedef union {
int8_t i8;
int8_t v;
} hb_be_int8_t;
typedef union {
uint8_t u8x2[2];
uint16_t u16;
uint16_t v;
} hb_be_uint16_t;
typedef union {
uint8_t u8x2[2];
int16_t i16;
int16_t v;
} hb_be_int16_t;
typedef union {
uint8_t u8x4[4];
uint16_t u16x2[2];
uint32_t u32;
uint32_t v;
inline operator int () { return (u8x4[0]<<24)+(u8x4[1]<<16)+(u8x4[2]<<8)+u8x4[3]; }
} hb_be_uint32_t;
typedef union {
uint8_t u8x4[4];
uint16_t u16x2[2];
int32_t i32;
int32_t v;
} hb_be_int32_t;
typedef union {
uint8_t u8x8[8];
uint16_t u16x4[4];
uint32_t u32x2[2];
uint64_t u64;
uint64_t v;
} hb_be_uint64_t;
typedef union {
uint8_t u8x8[8];
uint16_t u16x4[4];
uint32_t u32x2[2];
int64_t i64;
int64_t v;
} hb_be_int64_t;
/* "The following data types are used in the OpenType font file.
* All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
/* Macros to convert to/from BigEndian */
#define hb_be_uint8_t
#define hb_be_int8_t
#define hb_be_uint16_t GUINT16_TO_BE
#define hb_be_int16_t GINT16_TO_BE
#define hb_be_uint32_t GUINT32_TO_BE
#define hb_be_int32_t GINT32_TO_BE
#define hb_be_uint64_t GUINT64_TO_BE
#define hb_be_int64_t GINT64_TO_BE
#define DEFINE_INT_TYPE1(NAME, TYPE, BIG_ENDIAN) \
struct NAME { \
inline NAME (TYPE i) { v = BIG_ENDIAN(i); } \
inline TYPE operator = (TYPE i) { v = BIG_ENDIAN(i); return i; } \
inline operator TYPE(void) { return BIG_ENDIAN(v); } \
private: TYPE v; \
}
#define DEFINE_INT_TYPE0(NAME, type) DEFINE_INT_TYPE1 (NAME, type, hb_be_##type)
#define DEFINE_INT_TYPE(NAME, u, w) DEFINE_INT_TYPE0 (NAME, u##int##w##_t)
DEFINE_INT_TYPE (HB_BYTE, u, 8); /* 8-bit unsigned integer. */
DEFINE_INT_TYPE (HB_CHAR, , 8); /* 8-bit signed integer. */
DEFINE_INT_TYPE (HB_USHORT, u, 16); /* 16-bit unsigned integer. */
DEFINE_INT_TYPE (HB_SHORT, , 16); /* 16-bit signed integer. */
DEFINE_INT_TYPE (HB_ULONG, u, 32); /* 32-bit unsigned integer. */
DEFINE_INT_TYPE (HB_LONG, , 32); /* 32-bit signed integer. */
/* Date represented in number of seconds since 12:00 midnight, January 1,
* 1904. The value is represented as a signed 64-bit integer. */
DEFINE_INT_TYPE (HB_LONGDATETIME, , 64);
/* 32-bit signed fixed-point number (16.16) */
struct HB_Fixed : HB_LONG {
inline operator double(void) { return (uint32_t) this / 65536.; }
inline int16_t int_part (void) { return (uint32_t) this >> 16; }
inline int16_t frac_part (void) { return (uint32_t) this & 0xffff; }
};
/* Smallest measurable distance in the em space. */
struct HB_FUNIT;
/* 16-bit signed integer (SHORT) that describes a quantity in FUnits. */
struct HB_FWORD : HB_SHORT {
};
/* 16-bit unsigned integer (USHORT) that describes a quantity in FUnits. */
struct HB_UFWORD : HB_USHORT {
};
/* 16-bit signed fixed number with the low 14 bits of fraction (2.14). */
struct HB_F2DOT14 : HB_SHORT {
inline operator double() { return (uint32_t) this / 16384.; }
};
/* Array of four uint8s (length = 32 bits) used to identify a script, language
* system, feature, or baseline */
struct HB_Tag : public HB_ULONG {
inline HB_Tag (char *c) : HB_ULONG(c ? *(uint32_t*)c : 0) {}
};
/* Glyph index number, same as uint16 (length = 16 bits) */
struct HB_GlyphID : HB_USHORT {
};
/* Offset to a table, same as uint16 (length = 16 bits), NULL offset = 0x0000 */
struct HB_Offset : HB_USHORT {
};
/* CheckSum */
struct HB_CheckSum : HB_ULONG {
static uint32_t CalcTableChecksum (HB_ULONG *Table, uint32_t Length) {
uint32_t Sum = 0L;
HB_ULONG *EndPtr = Table+((Length+3) & ~3) / sizeof(HB_ULONG);
while (Table < EndPtr)
Sum += *Table++;
return Sum;
}
};
/*
* Version Numbers
*/
struct HB_USHORT_Version : HB_USHORT {
};
struct HB_Fixed_Version : HB_Fixed {
inline int16_t major (void) { return this->int_part(); }
inline int16_t minor (void) { return this->frac_part(); }
};
/*
* Organization of an OpenType Font
*/
/* Offset Table */
struct HB_OffsetTable {
HB_Fixed_Version sfnt_version; /* 0x00010000 for version 1.0. */
HB_USHORT numTables; /* Number of tables. */
HB_USHORT searchRange; /* (Maximum power of 2 <= numTables)
* x 16 */
HB_USHORT entrySelector; /* Log2(maximum power of 2 <=
* numTables). */
HB_USHORT rangeShift; /* NumTables x 16-searchRange. */
};
/* Table Directory */
struct HB_TableDirectory {
HB_Tag tag; /* 4-byte identifier. */
HB_CheckSum checkSum; /* CheckSum for this table. */
HB_ULONG offset; /* Offset from beginning of TrueType font
* file. */
HB_ULONG length; /* Length of this table. */
};
/*
* TrueType Collections
*/
/* TTC Header Version 1.0 */
struct HB_TTCHeader {
HB_Tag TTCTag; /* TrueType Collection ID string: 'ttcf' */
HB_ULONG version; /* Version of the TTC Header (1.0 or 2.0),
* 0x00010000 or 0x00020000 */
HB_ULONG numFonts; /* Number of fonts in TTC */
HB_ULONG offsetTable[0]; /* Array of offsets to the OffsetTable for
* each font from the beginning of the file.
* numFonts entries long. */
inline int len(void) { return sizeof (HB_TTCHeader)
+ sizeof (HB_ULONG) * numFonts; }
};
/* TTC Header Version 2.0 tail
* Follows after HB_TTCHeader with appropriate size for the offsetTable. */
struct HB_TTCHeaderVersion2Tail {
HB_ULONG ulDsigTag; /* Tag indicating that a DSIG table exists,
* 0x44534947 ('DSIG') (null if no signature) */
HB_ULONG ulDsigLength; /* The length (in bytes) of the DSIG table
* (null if no signature) */
HB_ULONG ulDsigOffset; /* The offset (in bytes) of the DSIG table
* from the beginning of the TTC file (null if
* no signature) */
};
/*
*
* OpenType Layout Common Table Formats
*
*/
/*
* Script List Table and Script Record
*/
struct ScriptRecord {
Tag scriptTag; /* 4-byte ScriptTag identifier */
Offset scriptOffset; /* Offset to Script table--from beginning of
* ScriptList */
};
struct ScriptList {
USHORT scriptCount; /* Number of ScriptRecords */
ScriptRecord scriptRecord[]; /* Array of ScriptRecords--listed alphabetically
* by ScriptTag. scriptCount entries long */
inline int len(void) { return sizeof (ScriptList)
+ sizeof (ScriptRecord) * scriptCount; }
};
/* "The following data types are used in the OpenType font file." */
typedef hb_be_uint8_t HB_BYTE; /* 8-bit unsigned integer. */
typedef hb_be_int8_t HB_CHAR; /* 8-bit signed integer. */
typedef hb_be_uint16_t HB_USHORT; /* 16-bit unsigned integer. */
typedef hb_be_int16_t HB_SHORT; /* 16-bit signed integer. */
typedef hb_be_uint32_t HB_ULONG; /* 32-bit unsigned integer. */
typedef hb_be_int32_t HB_LONG; /* 32-bit signed integer. */
typedef hb_be_int32_t HB_Fixed; /* 32-bit signed fixed-point number
* (16.16) */
typedef struct _FUNIT HB_FUNIT; /* Smallest measurable distance
* in the em space. */
typedef HB_SHORT HB_FWORD; /* 16-bit signed integer (SHORT) that
* describes a quantity in FUnits. */
typedef HB_USHORT HB_UFWORD; /* 16-bit unsigned integer (USHORT)
* that describes a quantity in
* FUnits. */
typedef hb_be_int16_t HB_F2DOT14; /* 16-bit signed fixed number with the
* low 14 bits of fraction (2.14). */
typedef hb_be_int64_t HB_LONGDATETIME;/* Date represented in number of
* seconds since 12:00 midnight,
* January 1, 1904. The value is
* represented as a signed 64-bit
* integer. */
typedef hb_be_uint32_t HB_Tag; /* Array of four uint8s (length = 32
* bits) used to identify a script,
* language system, feature, or
* baseline */
typedef hb_be_uint16_t HB_GlyphID; /* Glyph index number, same as
* uint16(length = 16 bits) */
typedef hb_be_uint16_t HB_Offset; /* Offset to a table, same as uint16
* (length = 16 bits), NULL offset =
* 0x0000 */
#include <stdio.h>
int
main (void)
{
HB_Tag x = {{'a', 'b', 'c', 'd'}};
HB_ULONG y = x;
HB_Tag y("abcd");
HB_Tag &x = y;
HB_BYTE b(0);
printf ("%d\n", x+0);
printf ("%d %04x %04x\n", sizeof (x), x+0, y+0);
return 0;
}