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
6 * published by the Free Software Foundation.  Oracle designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Oracle 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 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.
23 *
24 */
25
26/*
27 *
28 *
29 * (C) Copyright IBM Corp. 1998 - 2005 - All Rights Reserved
30 *
31 */
32
33#include "LETypes.h"
34#include "OpenTypeTables.h"
35#include "DeviceTables.h"
36#include "LESwaps.h"
37
38U_NAMESPACE_BEGIN
39
40const le_uint16 DeviceTable::fieldMasks[]    = {0x0003, 0x000F, 0x00FF};
41const le_uint16 DeviceTable::fieldSignBits[] = {0x0002, 0x0008, 0x0080};
42const le_uint16 DeviceTable::fieldBits[]     = {     2,      4,      8};
43
44#define FORMAT_COUNT LE_ARRAY_SIZE(fieldBits)
45
46le_int16 DeviceTable::getAdjustment(const LEReferenceTo<DeviceTable>&base, le_uint16 ppem, LEErrorCode &success) const
47{
48    le_int16 result = 0;
49    if (LE_FAILURE(success)) {
50        return result;
51    }
52    le_uint16 start = SWAPW(startSize);
53    le_uint16 format = SWAPW(deltaFormat) - 1;
54
55    if (ppem >= start && ppem <= SWAPW(endSize) && format < FORMAT_COUNT) {
56        le_uint16 sizeIndex = ppem - start;
57        le_uint16 bits = fieldBits[format];
58        le_uint16 count = 16 / bits;
59
60        LEReferenceToArrayOf<le_uint16> deltaValuesRef(base, success, deltaValues, (sizeIndex / count));
61
62        if(LE_FAILURE(success)) {
63          return result;
64        }
65
66        le_uint16 word = SWAPW(deltaValues[sizeIndex / count]);
67        le_uint16 fieldIndex = sizeIndex % count;
68        le_uint16 shift = 16 - (bits * (fieldIndex + 1));
69        le_uint16 field = (word >> shift) & fieldMasks[format];
70
71        result = field;
72
73        if ((field & fieldSignBits[format]) != 0) {
74            result |= ~ fieldMasks[format];
75        }
76    }
77
78    return result;
79}
80
81U_NAMESPACE_END
82