sl@0
|
1 |
/*
|
sl@0
|
2 |
* @(#)KernTable.cpp 1.1 04/10/13
|
sl@0
|
3 |
*
|
sl@0
|
4 |
* (C) Copyright IBM Corp. 2004-2005 - All Rights Reserved
|
sl@0
|
5 |
*
|
sl@0
|
6 |
*/
|
sl@0
|
7 |
|
sl@0
|
8 |
#include "KernTable.h"
|
sl@0
|
9 |
|
sl@0
|
10 |
#include "LESwaps.h"
|
sl@0
|
11 |
|
sl@0
|
12 |
#include <stdio.h>
|
sl@0
|
13 |
|
sl@0
|
14 |
#define DEBUG 0
|
sl@0
|
15 |
|
sl@0
|
16 |
U_NAMESPACE_BEGIN
|
sl@0
|
17 |
|
sl@0
|
18 |
struct PairInfo {
|
sl@0
|
19 |
le_uint32 key; // sigh, MSVC compiler gags on union here
|
sl@0
|
20 |
le_int16 value; // fword, kern value in funits
|
sl@0
|
21 |
};
|
sl@0
|
22 |
#define KERN_PAIRINFO_SIZE 6
|
sl@0
|
23 |
|
sl@0
|
24 |
struct Subtable_0 {
|
sl@0
|
25 |
le_uint16 nPairs;
|
sl@0
|
26 |
le_uint16 searchRange;
|
sl@0
|
27 |
le_uint16 entrySelector;
|
sl@0
|
28 |
le_uint16 rangeShift;
|
sl@0
|
29 |
};
|
sl@0
|
30 |
#define KERN_SUBTABLE_0_HEADER_SIZE 8
|
sl@0
|
31 |
|
sl@0
|
32 |
// Kern table version 0 only
|
sl@0
|
33 |
struct SubtableHeader {
|
sl@0
|
34 |
le_uint16 version;
|
sl@0
|
35 |
le_uint16 length;
|
sl@0
|
36 |
le_uint16 coverage;
|
sl@0
|
37 |
};
|
sl@0
|
38 |
#define KERN_SUBTABLE_HEADER_SIZE 6
|
sl@0
|
39 |
|
sl@0
|
40 |
// Version 0 only, version 1 has different layout
|
sl@0
|
41 |
struct KernTableHeader {
|
sl@0
|
42 |
le_uint16 version;
|
sl@0
|
43 |
le_uint16 nTables;
|
sl@0
|
44 |
};
|
sl@0
|
45 |
#define KERN_TABLE_HEADER_SIZE 4
|
sl@0
|
46 |
|
sl@0
|
47 |
#define COVERAGE_HORIZONTAL 0x1
|
sl@0
|
48 |
#define COVERAGE_MINIMUM 0x2
|
sl@0
|
49 |
#define COVERAGE_CROSS 0x4
|
sl@0
|
50 |
#define COVERAGE_OVERRIDE 0x8
|
sl@0
|
51 |
|
sl@0
|
52 |
KernTable::KernTable(const LEFontInstance* font, const void* tableData)
|
sl@0
|
53 |
: pairs(0), font(font)
|
sl@0
|
54 |
{
|
sl@0
|
55 |
const KernTableHeader* header = (const KernTableHeader*)tableData;
|
sl@0
|
56 |
if (header == 0) {
|
sl@0
|
57 |
#if DEBUG
|
sl@0
|
58 |
fprintf(stderr, "no kern data\n");
|
sl@0
|
59 |
fflush(stderr);
|
sl@0
|
60 |
#endif
|
sl@0
|
61 |
return;
|
sl@0
|
62 |
}
|
sl@0
|
63 |
|
sl@0
|
64 |
#if DEBUG
|
sl@0
|
65 |
// dump first 32 bytes of header
|
sl@0
|
66 |
for (int i = 0; i < 64; ++i) {
|
sl@0
|
67 |
fprintf(stderr, "%0.2x ", ((const char*)tableData)[i]&0xff);
|
sl@0
|
68 |
if (((i+1)&0xf) == 0) {
|
sl@0
|
69 |
fprintf(stderr, "\n");
|
sl@0
|
70 |
} else if (((i+1)&0x7) == 0) {
|
sl@0
|
71 |
fprintf(stderr, " ");
|
sl@0
|
72 |
}
|
sl@0
|
73 |
}
|
sl@0
|
74 |
fflush(stderr);
|
sl@0
|
75 |
#endif
|
sl@0
|
76 |
|
sl@0
|
77 |
if (header->version == 0 && SWAPW(header->nTables) > 0) {
|
sl@0
|
78 |
const SubtableHeader* subhead = (const SubtableHeader*)((char*)tableData + KERN_TABLE_HEADER_SIZE);
|
sl@0
|
79 |
if (subhead->version == 0) {
|
sl@0
|
80 |
coverage = SWAPW(subhead->coverage);
|
sl@0
|
81 |
if (coverage & COVERAGE_HORIZONTAL) { // only handle horizontal kerning
|
sl@0
|
82 |
const Subtable_0* table = (const Subtable_0*)((char*)subhead + KERN_SUBTABLE_HEADER_SIZE);
|
sl@0
|
83 |
nPairs = SWAPW(table->nPairs);
|
sl@0
|
84 |
searchRange = SWAPW(table->searchRange);
|
sl@0
|
85 |
entrySelector = SWAPW(table->entrySelector);
|
sl@0
|
86 |
rangeShift = SWAPW(table->rangeShift);
|
sl@0
|
87 |
pairs = (const PairInfo*)((char*)table + KERN_SUBTABLE_0_HEADER_SIZE);
|
sl@0
|
88 |
|
sl@0
|
89 |
#if DEBUG
|
sl@0
|
90 |
fprintf(stderr, "coverage: %0.4x nPairs: %d pairs 0x%x\n", coverage, nPairs, pairs);
|
sl@0
|
91 |
fprintf(stderr, " searchRange: %d entrySelector: %d rangeShift: %d\n", searchRange, entrySelector, rangeShift);
|
sl@0
|
92 |
fflush(stderr);
|
sl@0
|
93 |
|
sl@0
|
94 |
{
|
sl@0
|
95 |
// dump part of the pair list
|
sl@0
|
96 |
char ids[256];
|
sl@0
|
97 |
for (int i = 256; --i >= 0;) {
|
sl@0
|
98 |
LEGlyphID id = font->mapCharToGlyph(i);
|
sl@0
|
99 |
if (id < 256) {
|
sl@0
|
100 |
ids[id] = (char)i;
|
sl@0
|
101 |
}
|
sl@0
|
102 |
}
|
sl@0
|
103 |
|
sl@0
|
104 |
const PairInfo* p = pairs;
|
sl@0
|
105 |
for (i = 0; i < nPairs; ++i, p = (const PairInfo*)((char*)p+KERN_PAIRINFO_SIZE)) {
|
sl@0
|
106 |
le_uint32 k = SWAPL(p->key);
|
sl@0
|
107 |
le_uint16 left = (k >> 16) & 0xffff;
|
sl@0
|
108 |
le_uint16 right = k & 0xffff;
|
sl@0
|
109 |
if (left < 256 && right < 256) {
|
sl@0
|
110 |
char c = ids[left];
|
sl@0
|
111 |
if (c > 0x20 && c < 0x7f) {
|
sl@0
|
112 |
fprintf(stderr, "%c/", c & 0xff);
|
sl@0
|
113 |
} else {
|
sl@0
|
114 |
fprintf(stderr, "%0.2x/", c & 0xff);
|
sl@0
|
115 |
}
|
sl@0
|
116 |
c = ids[right];
|
sl@0
|
117 |
if (c > 0x20 && c < 0x7f) {
|
sl@0
|
118 |
fprintf(stderr, "%c ", c & 0xff);
|
sl@0
|
119 |
} else {
|
sl@0
|
120 |
fprintf(stderr, "%0.2x ", c & 0xff);
|
sl@0
|
121 |
}
|
sl@0
|
122 |
}
|
sl@0
|
123 |
}
|
sl@0
|
124 |
fflush(stderr);
|
sl@0
|
125 |
}
|
sl@0
|
126 |
#endif
|
sl@0
|
127 |
}
|
sl@0
|
128 |
}
|
sl@0
|
129 |
}
|
sl@0
|
130 |
}
|
sl@0
|
131 |
|
sl@0
|
132 |
|
sl@0
|
133 |
/*
|
sl@0
|
134 |
* Process the glyph positions. The positions array has two floats for each
|
sl@0
|
135 |
* glyph, plus a trailing pair to mark the end of the last glyph.
|
sl@0
|
136 |
*/
|
sl@0
|
137 |
//void KernTable::process(const LEGlyphID glyphs[], float* positions, le_int32 glyphCount)
|
sl@0
|
138 |
void KernTable::process(LEGlyphStorage& storage)
|
sl@0
|
139 |
{
|
sl@0
|
140 |
if (pairs) {
|
sl@0
|
141 |
LEErrorCode success = LE_NO_ERROR;
|
sl@0
|
142 |
|
sl@0
|
143 |
le_uint32 key = storage[0]; // no need to mask off high bits
|
sl@0
|
144 |
float adjust = 0;
|
sl@0
|
145 |
for (int i = 1, e = storage.getGlyphCount(); i < e; ++i) {
|
sl@0
|
146 |
key = key << 16 | (storage[i] & 0xffff);
|
sl@0
|
147 |
|
sl@0
|
148 |
// argh, to do a binary search, we need to have the pair list in sorted order
|
sl@0
|
149 |
// but it is not in sorted order on win32 platforms because of the endianness difference
|
sl@0
|
150 |
// so either I have to swap the element each time I examine it, or I have to swap
|
sl@0
|
151 |
// all the elements ahead of time and store them in the font
|
sl@0
|
152 |
|
sl@0
|
153 |
const PairInfo* p = pairs;
|
sl@0
|
154 |
const PairInfo* tp = (const PairInfo*)((char*)p + rangeShift);
|
sl@0
|
155 |
if (key > SWAPL(tp->key)) {
|
sl@0
|
156 |
p = tp;
|
sl@0
|
157 |
}
|
sl@0
|
158 |
|
sl@0
|
159 |
#if DEBUG
|
sl@0
|
160 |
fprintf(stderr, "binary search for %0.8x\n", key);
|
sl@0
|
161 |
fflush(stderr);
|
sl@0
|
162 |
#endif
|
sl@0
|
163 |
|
sl@0
|
164 |
le_uint32 probe = searchRange;
|
sl@0
|
165 |
while (probe > KERN_PAIRINFO_SIZE) {
|
sl@0
|
166 |
probe >>= 1;
|
sl@0
|
167 |
tp = (const PairInfo*)((char*)p + probe);
|
sl@0
|
168 |
le_uint32 tkey = SWAPL(tp->key);
|
sl@0
|
169 |
#if DEBUG
|
sl@0
|
170 |
fprintf(stdout, " %.3d (%0.8x)\n", ((char*)tp - (char*)pairs)/KERN_PAIRINFO_SIZE, tkey);
|
sl@0
|
171 |
fflush(stdout);
|
sl@0
|
172 |
#endif
|
sl@0
|
173 |
if (tkey <= key) {
|
sl@0
|
174 |
if (tkey == key) {
|
sl@0
|
175 |
le_int16 value = SWAPW(tp->value);
|
sl@0
|
176 |
#if DEBUG
|
sl@0
|
177 |
fprintf(stdout, "binary found kerning pair %x:%x at %d, value: 0x%x (%g)\n",
|
sl@0
|
178 |
storage[i-1], storage[i], i, value & 0xffff, font->xUnitsToPoints(value));
|
sl@0
|
179 |
fflush(stdout);
|
sl@0
|
180 |
#endif
|
sl@0
|
181 |
adjust += font->xUnitsToPoints(value);
|
sl@0
|
182 |
break;
|
sl@0
|
183 |
}
|
sl@0
|
184 |
p = tp;
|
sl@0
|
185 |
}
|
sl@0
|
186 |
}
|
sl@0
|
187 |
|
sl@0
|
188 |
storage.adjustPosition(i, adjust, 0, success);
|
sl@0
|
189 |
}
|
sl@0
|
190 |
storage.adjustPosition(storage.getGlyphCount(), adjust, 0, success);
|
sl@0
|
191 |
}
|
sl@0
|
192 |
}
|
sl@0
|
193 |
|
sl@0
|
194 |
U_NAMESPACE_END
|
sl@0
|
195 |
|