2
|
1 |
/*
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
3 |
*
|
|
4 |
* This code is free software; you can redistribute it and/or modify it
|
|
5 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
6 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
7 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
8 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
9 |
*
|
|
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
14 |
* accompanied this code).
|
|
15 |
*
|
|
16 |
* You should have received a copy of the GNU General Public License version
|
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
19 |
*
|
5506
|
20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
21 |
* or visit www.oracle.com if you need additional information or have any
|
|
22 |
* questions.
|
2
|
23 |
*
|
|
24 |
*/
|
|
25 |
|
|
26 |
/*
|
|
27 |
*
|
3935
|
28 |
*
|
7486
|
29 |
* (C) Copyright IBM Corp. 2004-2010 - All Rights Reserved
|
2
|
30 |
*
|
|
31 |
*/
|
|
32 |
|
|
33 |
#include "KernTable.h"
|
|
34 |
#include "LEFontInstance.h"
|
|
35 |
#include "LEGlyphStorage.h"
|
|
36 |
|
|
37 |
#include "LESwaps.h"
|
7486
|
38 |
#include "OpenTypeUtilities.h"
|
2
|
39 |
|
|
40 |
#include <stdio.h>
|
|
41 |
|
|
42 |
#define DEBUG 0
|
|
43 |
|
3935
|
44 |
U_NAMESPACE_BEGIN
|
|
45 |
|
2
|
46 |
struct PairInfo {
|
|
47 |
le_uint32 key; // sigh, MSVC compiler gags on union here
|
|
48 |
le_int16 value; // fword, kern value in funits
|
|
49 |
};
|
|
50 |
#define KERN_PAIRINFO_SIZE 6
|
|
51 |
|
|
52 |
struct Subtable_0 {
|
|
53 |
le_uint16 nPairs;
|
|
54 |
le_uint16 searchRange;
|
|
55 |
le_uint16 entrySelector;
|
|
56 |
le_uint16 rangeShift;
|
|
57 |
};
|
|
58 |
#define KERN_SUBTABLE_0_HEADER_SIZE 8
|
|
59 |
|
|
60 |
// Kern table version 0 only
|
|
61 |
struct SubtableHeader {
|
|
62 |
le_uint16 version;
|
|
63 |
le_uint16 length;
|
|
64 |
le_uint16 coverage;
|
|
65 |
};
|
|
66 |
#define KERN_SUBTABLE_HEADER_SIZE 6
|
|
67 |
|
|
68 |
// Version 0 only, version 1 has different layout
|
|
69 |
struct KernTableHeader {
|
|
70 |
le_uint16 version;
|
|
71 |
le_uint16 nTables;
|
|
72 |
};
|
|
73 |
#define KERN_TABLE_HEADER_SIZE 4
|
|
74 |
|
|
75 |
#define COVERAGE_HORIZONTAL 0x1
|
|
76 |
#define COVERAGE_MINIMUM 0x2
|
|
77 |
#define COVERAGE_CROSS 0x4
|
|
78 |
#define COVERAGE_OVERRIDE 0x8
|
|
79 |
|
|
80 |
/*
|
|
81 |
* This implementation has support for only one subtable, so if the font has
|
|
82 |
* multiple subtables, only the first will be used. If this turns out to
|
|
83 |
* be a problem in practice we should add it.
|
|
84 |
*
|
|
85 |
* This also supports only version 0 of the kern table header, only
|
|
86 |
* Apple supports the latter.
|
|
87 |
*
|
|
88 |
* This implementation isn't careful about the kern table flags, and
|
|
89 |
* might invoke kerning when it is not supposed to. That too I'm
|
|
90 |
* leaving for a bug fix.
|
|
91 |
*
|
|
92 |
* TODO: support multiple subtables
|
|
93 |
* TODO: respect header flags
|
|
94 |
*/
|
7486
|
95 |
KernTable::KernTable(const LEFontInstance* font_, const void* tableData)
|
|
96 |
: pairs(0), font(font_)
|
2
|
97 |
{
|
|
98 |
const KernTableHeader* header = (const KernTableHeader*)tableData;
|
|
99 |
if (header == 0) {
|
|
100 |
#if DEBUG
|
|
101 |
fprintf(stderr, "no kern data\n");
|
|
102 |
#endif
|
|
103 |
return;
|
|
104 |
}
|
|
105 |
|
|
106 |
#if DEBUG
|
|
107 |
// dump first 32 bytes of header
|
|
108 |
for (int i = 0; i < 64; ++i) {
|
|
109 |
fprintf(stderr, "%0.2x ", ((const char*)tableData)[i]&0xff);
|
|
110 |
if (((i+1)&0xf) == 0) {
|
|
111 |
fprintf(stderr, "\n");
|
|
112 |
} else if (((i+1)&0x7) == 0) {
|
|
113 |
fprintf(stderr, " ");
|
|
114 |
}
|
|
115 |
}
|
|
116 |
#endif
|
|
117 |
|
|
118 |
if (header->version == 0 && SWAPW(header->nTables) > 0) {
|
|
119 |
const SubtableHeader* subhead = (const SubtableHeader*)((char*)tableData + KERN_TABLE_HEADER_SIZE);
|
|
120 |
if (subhead->version == 0) {
|
|
121 |
coverage = SWAPW(subhead->coverage);
|
|
122 |
if (coverage & COVERAGE_HORIZONTAL) { // only handle horizontal kerning
|
|
123 |
const Subtable_0* table = (const Subtable_0*)((char*)subhead + KERN_SUBTABLE_HEADER_SIZE);
|
7486
|
124 |
|
|
125 |
nPairs = SWAPW(table->nPairs);
|
|
126 |
|
|
127 |
#if 0 // some old fonts have bad values here...
|
|
128 |
searchRange = SWAPW(table->searchRange);
|
2
|
129 |
entrySelector = SWAPW(table->entrySelector);
|
7486
|
130 |
rangeShift = SWAPW(table->rangeShift);
|
|
131 |
#else
|
|
132 |
entrySelector = OpenTypeUtilities::highBit(nPairs);
|
|
133 |
searchRange = (1 << entrySelector) * KERN_PAIRINFO_SIZE;
|
|
134 |
rangeShift = (nPairs * KERN_PAIRINFO_SIZE) - searchRange;
|
|
135 |
#endif
|
2
|
136 |
|
|
137 |
pairs = (PairInfo*)font->getKernPairs();
|
|
138 |
if (pairs == NULL) {
|
|
139 |
char *pairData = (char*)table + KERN_SUBTABLE_0_HEADER_SIZE;
|
|
140 |
char *pptr = pairData;
|
|
141 |
pairs = (PairInfo*)(malloc(nPairs*sizeof(PairInfo)));
|
|
142 |
PairInfo *p = (PairInfo*)pairs;
|
|
143 |
for (int i = 0; i < nPairs; i++, pptr += KERN_PAIRINFO_SIZE, p++) {
|
|
144 |
memcpy(p, pptr, KERN_PAIRINFO_SIZE);
|
|
145 |
p->key = SWAPL(p->key);
|
|
146 |
}
|
|
147 |
font->setKernPairs((void*)pairs);
|
|
148 |
}
|
|
149 |
|
|
150 |
#if DEBUG
|
|
151 |
fprintf(stderr, "coverage: %0.4x nPairs: %d pairs 0x%x\n", coverage, nPairs, pairs);
|
|
152 |
fprintf(stderr,
|
|
153 |
" searchRange(pairs): %d entrySelector: %d rangeShift(pairs): %d\n",
|
|
154 |
searchRange, entrySelector, rangeShift);
|
|
155 |
|
|
156 |
{
|
|
157 |
// dump part of the pair list
|
|
158 |
char ids[256];
|
|
159 |
for (int i = 256; --i >= 0;) {
|
|
160 |
LEGlyphID id = font->mapCharToGlyph(i);
|
|
161 |
if (id < 256) {
|
|
162 |
ids[id] = (char)i;
|
|
163 |
}
|
|
164 |
}
|
|
165 |
PairInfo *p = pairs;
|
|
166 |
for (int i = 0; i < nPairs; ++i, p++) {
|
|
167 |
le_uint32 k = p->key;
|
|
168 |
le_uint16 left = (k >> 16) & 0xffff;
|
|
169 |
le_uint16 right = k & 0xffff;
|
|
170 |
if (left < 256 && right < 256) {
|
|
171 |
char c = ids[left];
|
|
172 |
if (c > 0x20 && c < 0x7f) {
|
|
173 |
fprintf(stderr, "%c/", c & 0xff);
|
|
174 |
} else {
|
|
175 |
fprintf(stderr, "%0.2x/", c & 0xff);
|
|
176 |
}
|
|
177 |
c = ids[right];
|
|
178 |
if (c > 0x20 && c < 0x7f) {
|
|
179 |
fprintf(stderr, "%c ", c & 0xff);
|
|
180 |
} else {
|
|
181 |
fprintf(stderr, "%0.2x ", c & 0xff);
|
|
182 |
}
|
|
183 |
}
|
|
184 |
}
|
|
185 |
}
|
|
186 |
#endif
|
|
187 |
}
|
|
188 |
}
|
|
189 |
}
|
|
190 |
}
|
|
191 |
|
|
192 |
|
|
193 |
/*
|
|
194 |
* Process the glyph positions. The positions array has two floats for each
|
|
195 |
* glyph, plus a trailing pair to mark the end of the last glyph.
|
|
196 |
*/
|
|
197 |
void KernTable::process(LEGlyphStorage& storage)
|
|
198 |
{
|
|
199 |
if (pairs) {
|
|
200 |
LEErrorCode success = LE_NO_ERROR;
|
|
201 |
|
|
202 |
le_uint32 key = storage[0]; // no need to mask off high bits
|
|
203 |
float adjust = 0;
|
|
204 |
for (int i = 1, e = storage.getGlyphCount(); i < e; ++i) {
|
|
205 |
key = key << 16 | (storage[i] & 0xffff);
|
3935
|
206 |
|
|
207 |
// argh, to do a binary search, we need to have the pair list in sorted order
|
|
208 |
// but it is not in sorted order on win32 platforms because of the endianness difference
|
|
209 |
// so either I have to swap the element each time I examine it, or I have to swap
|
|
210 |
// all the elements ahead of time and store them in the font
|
|
211 |
|
2
|
212 |
const PairInfo* p = pairs;
|
|
213 |
const PairInfo* tp = (const PairInfo*)(p + rangeShift);
|
|
214 |
if (key > tp->key) {
|
|
215 |
p = tp;
|
|
216 |
}
|
|
217 |
|
|
218 |
#if DEBUG
|
|
219 |
fprintf(stderr, "binary search for %0.8x\n", key);
|
|
220 |
#endif
|
|
221 |
|
|
222 |
le_uint32 probe = searchRange;
|
|
223 |
while (probe > 1) {
|
|
224 |
probe >>= 1;
|
|
225 |
tp = (const PairInfo*)(p + probe);
|
|
226 |
le_uint32 tkey = tp->key;
|
|
227 |
#if DEBUG
|
|
228 |
fprintf(stdout, " %.3d (%0.8x)\n", (tp - pairs), tkey);
|
|
229 |
#endif
|
|
230 |
if (tkey <= key) {
|
|
231 |
if (tkey == key) {
|
|
232 |
le_int16 value = SWAPW(tp->value);
|
|
233 |
#if DEBUG
|
|
234 |
fprintf(stdout, "binary found kerning pair %x:%x at %d, value: 0x%x (%g)\n",
|
|
235 |
storage[i-1], storage[i], i, value & 0xffff, font->xUnitsToPoints(value));
|
|
236 |
fflush(stdout);
|
|
237 |
#endif
|
|
238 |
// Have to undo the device transform.
|
|
239 |
// REMIND either find a way to do this only if there is a
|
|
240 |
// device transform, or a faster way, such as moving the
|
|
241 |
// entire kern table up to Java.
|
|
242 |
LEPoint pt;
|
|
243 |
pt.fX = font->xUnitsToPoints(value);
|
|
244 |
pt.fY = 0;
|
|
245 |
|
|
246 |
font->getKerningAdjustment(pt);
|
|
247 |
adjust += pt.fX;
|
|
248 |
break;
|
|
249 |
}
|
|
250 |
p = tp;
|
|
251 |
}
|
|
252 |
}
|
|
253 |
|
|
254 |
storage.adjustPosition(i, adjust, 0, success);
|
|
255 |
}
|
|
256 |
storage.adjustPosition(storage.getGlyphCount(), adjust, 0, success);
|
|
257 |
}
|
|
258 |
}
|
3935
|
259 |
|
|
260 |
U_NAMESPACE_END
|
|
261 |
|