sl@0: /* sl@0: * @(#)KernTable.cpp 1.1 04/10/13 sl@0: * sl@0: * (C) Copyright IBM Corp. 2004-2005 - All Rights Reserved sl@0: * sl@0: */ sl@0: sl@0: #include "KernTable.h" sl@0: sl@0: #include "LESwaps.h" sl@0: sl@0: #include sl@0: sl@0: #define DEBUG 0 sl@0: sl@0: U_NAMESPACE_BEGIN sl@0: sl@0: struct PairInfo { sl@0: le_uint32 key; // sigh, MSVC compiler gags on union here sl@0: le_int16 value; // fword, kern value in funits sl@0: }; sl@0: #define KERN_PAIRINFO_SIZE 6 sl@0: sl@0: struct Subtable_0 { sl@0: le_uint16 nPairs; sl@0: le_uint16 searchRange; sl@0: le_uint16 entrySelector; sl@0: le_uint16 rangeShift; sl@0: }; sl@0: #define KERN_SUBTABLE_0_HEADER_SIZE 8 sl@0: sl@0: // Kern table version 0 only sl@0: struct SubtableHeader { sl@0: le_uint16 version; sl@0: le_uint16 length; sl@0: le_uint16 coverage; sl@0: }; sl@0: #define KERN_SUBTABLE_HEADER_SIZE 6 sl@0: sl@0: // Version 0 only, version 1 has different layout sl@0: struct KernTableHeader { sl@0: le_uint16 version; sl@0: le_uint16 nTables; sl@0: }; sl@0: #define KERN_TABLE_HEADER_SIZE 4 sl@0: sl@0: #define COVERAGE_HORIZONTAL 0x1 sl@0: #define COVERAGE_MINIMUM 0x2 sl@0: #define COVERAGE_CROSS 0x4 sl@0: #define COVERAGE_OVERRIDE 0x8 sl@0: sl@0: KernTable::KernTable(const LEFontInstance* font, const void* tableData) sl@0: : pairs(0), font(font) sl@0: { sl@0: const KernTableHeader* header = (const KernTableHeader*)tableData; sl@0: if (header == 0) { sl@0: #if DEBUG sl@0: fprintf(stderr, "no kern data\n"); sl@0: fflush(stderr); sl@0: #endif sl@0: return; sl@0: } sl@0: sl@0: #if DEBUG sl@0: // dump first 32 bytes of header sl@0: for (int i = 0; i < 64; ++i) { sl@0: fprintf(stderr, "%0.2x ", ((const char*)tableData)[i]&0xff); sl@0: if (((i+1)&0xf) == 0) { sl@0: fprintf(stderr, "\n"); sl@0: } else if (((i+1)&0x7) == 0) { sl@0: fprintf(stderr, " "); sl@0: } sl@0: } sl@0: fflush(stderr); sl@0: #endif sl@0: sl@0: if (header->version == 0 && SWAPW(header->nTables) > 0) { sl@0: const SubtableHeader* subhead = (const SubtableHeader*)((char*)tableData + KERN_TABLE_HEADER_SIZE); sl@0: if (subhead->version == 0) { sl@0: coverage = SWAPW(subhead->coverage); sl@0: if (coverage & COVERAGE_HORIZONTAL) { // only handle horizontal kerning sl@0: const Subtable_0* table = (const Subtable_0*)((char*)subhead + KERN_SUBTABLE_HEADER_SIZE); sl@0: nPairs = SWAPW(table->nPairs); sl@0: searchRange = SWAPW(table->searchRange); sl@0: entrySelector = SWAPW(table->entrySelector); sl@0: rangeShift = SWAPW(table->rangeShift); sl@0: pairs = (const PairInfo*)((char*)table + KERN_SUBTABLE_0_HEADER_SIZE); sl@0: sl@0: #if DEBUG sl@0: fprintf(stderr, "coverage: %0.4x nPairs: %d pairs 0x%x\n", coverage, nPairs, pairs); sl@0: fprintf(stderr, " searchRange: %d entrySelector: %d rangeShift: %d\n", searchRange, entrySelector, rangeShift); sl@0: fflush(stderr); sl@0: sl@0: { sl@0: // dump part of the pair list sl@0: char ids[256]; sl@0: for (int i = 256; --i >= 0;) { sl@0: LEGlyphID id = font->mapCharToGlyph(i); sl@0: if (id < 256) { sl@0: ids[id] = (char)i; sl@0: } sl@0: } sl@0: sl@0: const PairInfo* p = pairs; sl@0: for (i = 0; i < nPairs; ++i, p = (const PairInfo*)((char*)p+KERN_PAIRINFO_SIZE)) { sl@0: le_uint32 k = SWAPL(p->key); sl@0: le_uint16 left = (k >> 16) & 0xffff; sl@0: le_uint16 right = k & 0xffff; sl@0: if (left < 256 && right < 256) { sl@0: char c = ids[left]; sl@0: if (c > 0x20 && c < 0x7f) { sl@0: fprintf(stderr, "%c/", c & 0xff); sl@0: } else { sl@0: fprintf(stderr, "%0.2x/", c & 0xff); sl@0: } sl@0: c = ids[right]; sl@0: if (c > 0x20 && c < 0x7f) { sl@0: fprintf(stderr, "%c ", c & 0xff); sl@0: } else { sl@0: fprintf(stderr, "%0.2x ", c & 0xff); sl@0: } sl@0: } sl@0: } sl@0: fflush(stderr); sl@0: } sl@0: #endif sl@0: } sl@0: } sl@0: } sl@0: } sl@0: sl@0: sl@0: /* sl@0: * Process the glyph positions. The positions array has two floats for each sl@0: * glyph, plus a trailing pair to mark the end of the last glyph. sl@0: */ sl@0: //void KernTable::process(const LEGlyphID glyphs[], float* positions, le_int32 glyphCount) sl@0: void KernTable::process(LEGlyphStorage& storage) sl@0: { sl@0: if (pairs) { sl@0: LEErrorCode success = LE_NO_ERROR; sl@0: sl@0: le_uint32 key = storage[0]; // no need to mask off high bits sl@0: float adjust = 0; sl@0: for (int i = 1, e = storage.getGlyphCount(); i < e; ++i) { sl@0: key = key << 16 | (storage[i] & 0xffff); sl@0: sl@0: // argh, to do a binary search, we need to have the pair list in sorted order sl@0: // but it is not in sorted order on win32 platforms because of the endianness difference sl@0: // so either I have to swap the element each time I examine it, or I have to swap sl@0: // all the elements ahead of time and store them in the font sl@0: sl@0: const PairInfo* p = pairs; sl@0: const PairInfo* tp = (const PairInfo*)((char*)p + rangeShift); sl@0: if (key > SWAPL(tp->key)) { sl@0: p = tp; sl@0: } sl@0: sl@0: #if DEBUG sl@0: fprintf(stderr, "binary search for %0.8x\n", key); sl@0: fflush(stderr); sl@0: #endif sl@0: sl@0: le_uint32 probe = searchRange; sl@0: while (probe > KERN_PAIRINFO_SIZE) { sl@0: probe >>= 1; sl@0: tp = (const PairInfo*)((char*)p + probe); sl@0: le_uint32 tkey = SWAPL(tp->key); sl@0: #if DEBUG sl@0: fprintf(stdout, " %.3d (%0.8x)\n", ((char*)tp - (char*)pairs)/KERN_PAIRINFO_SIZE, tkey); sl@0: fflush(stdout); sl@0: #endif sl@0: if (tkey <= key) { sl@0: if (tkey == key) { sl@0: le_int16 value = SWAPW(tp->value); sl@0: #if DEBUG sl@0: fprintf(stdout, "binary found kerning pair %x:%x at %d, value: 0x%x (%g)\n", sl@0: storage[i-1], storage[i], i, value & 0xffff, font->xUnitsToPoints(value)); sl@0: fflush(stdout); sl@0: #endif sl@0: adjust += font->xUnitsToPoints(value); sl@0: break; sl@0: } sl@0: p = tp; sl@0: } sl@0: } sl@0: sl@0: storage.adjustPosition(i, adjust, 0, success); sl@0: } sl@0: storage.adjustPosition(storage.getGlyphCount(), adjust, 0, success); sl@0: } sl@0: } sl@0: sl@0: U_NAMESPACE_END sl@0: