blob: 54de9a646af465f29a8511f05a2051a59cc76ed8 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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
6 * published by the Free Software Foundation. Sun designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Sun in the LICENSE file that accompanied this code.
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 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 *
24 */
25
26/*
27 *
28 * (C) Copyright IBM Corp. 1998-2004 - All Rights Reserved
29 *
30 */
31
32#include "LETypes.h"
33#include "LayoutTables.h"
34#include "LookupTables.h"
35#include "LESwaps.h"
36
37/*
38 These are the rolled-up versions of the uniform binary search.
39 Someday, if we need more performance, we can un-roll them.
40
41 Note: I put these in the base class, so they only have to
42 be written once. Since the base class doesn't define the
43 segment table, these routines assume that it's right after
44 the binary search header.
45
46 Another way to do this is to put each of these routines in one
47 of the derived classes, and implement it in the others by casting
48 the "this" pointer to the type that has the implementation.
49*/
50const LookupSegment *BinarySearchLookupTable::lookupSegment(const LookupSegment *segments, LEGlyphID glyph) const
51{
52 le_int16 unity = SWAPW(unitSize);
53 le_int16 probe = SWAPW(searchRange);
54 le_int16 extra = SWAPW(rangeShift);
55 TTGlyphID ttGlyph = (TTGlyphID) LE_GET_GLYPH(glyph);
56 const LookupSegment *entry = segments;
57 const LookupSegment *trial = (const LookupSegment *) ((char *) entry + extra);
58
59 if (SWAPW(trial->lastGlyph) <= ttGlyph) {
60 entry = trial;
61 }
62
63 while (probe > unity) {
64 probe >>= 1;
65 trial = (const LookupSegment *) ((char *) entry + probe);
66
67 if (SWAPW(trial->lastGlyph) <= ttGlyph) {
68 entry = trial;
69 }
70 }
71
72 if (SWAPW(entry->firstGlyph) <= ttGlyph) {
73 return entry;
74 }
75
76 return NULL;
77}
78
79const LookupSingle *BinarySearchLookupTable::lookupSingle(const LookupSingle *entries, LEGlyphID glyph) const
80{
81 le_int16 unity = SWAPW(unitSize);
82 le_int16 probe = SWAPW(searchRange);
83 le_int16 extra = SWAPW(rangeShift);
84 TTGlyphID ttGlyph = (TTGlyphID) LE_GET_GLYPH(glyph);
85 const LookupSingle *entry = entries;
86 const LookupSingle *trial = (const LookupSingle *) ((char *) entry + extra);
87
88 if (SWAPW(trial->glyph) <= ttGlyph) {
89 entry = trial;
90 }
91
92 while (probe > unity) {
93 probe >>= 1;
94 trial = (const LookupSingle *) ((char *) entry + probe);
95
96 if (SWAPW(trial->glyph) <= ttGlyph) {
97 entry = trial;
98 }
99 }
100
101 if (SWAPW(entry->glyph) == ttGlyph) {
102 return entry;
103 }
104
105 return NULL;
106}