2 * Copyright (c) 2008, 2009, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "VDMXParser.h"
37 #include "wtf/ByteOrder.h"
39 // Buffer helper class
41 // This class perform some trival buffer operations while checking for
42 // out-of-bounds errors. As a family they return false if anything is amiss,
43 // updating the current offset otherwise.
46 Buffer(const uint8_t* buffer, size_t length)
51 bool skip(size_t numBytes)
53 if (m_offset + numBytes > m_length)
59 bool readU8(uint8_t* value)
61 if (m_offset + sizeof(uint8_t) > m_length)
63 *value = m_buffer[m_offset];
64 m_offset += sizeof(uint8_t);
68 bool readU16(uint16_t* value)
70 if (m_offset + sizeof(uint16_t) > m_length)
72 memcpy(value, m_buffer + m_offset, sizeof(uint16_t));
73 *value = ntohs(*value);
74 m_offset += sizeof(uint16_t);
78 bool readS16(int16_t* value)
80 return readU16(reinterpret_cast<uint16_t*>(value));
88 void setOffset(size_t newoffset)
94 const uint8_t *const m_buffer;
95 const size_t m_length;
101 // VDMX tables are found in some TrueType/OpenType fonts and contain
102 // ascender/descender overrides for certain (usually small) sizes. This is
103 // needed in order to match font metrics on Windows.
105 // Freetype does not parse these tables so we do so here.
109 // Parse a TrueType VDMX table.
110 // yMax: (output) the ascender value from the table
111 // yMin: (output) the descender value from the table (negative!)
112 // vdmx: the table bytes
113 // vdmxLength: length of @vdmx, in bytes
114 // targetPixelSize: the pixel size of the font (e.g. 16)
116 // Returns true iff a suitable match are found. Otherwise, *yMax and *yMin are
117 // untouched. size_t must be 32-bits to avoid overflow.
119 // See http://www.microsoft.com/opentype/otspec/vdmx.htm
120 bool parseVDMX(int* yMax, int* yMin,
121 const uint8_t* vdmx, size_t vdmxLength,
122 unsigned targetPixelSize)
124 Buffer buf(vdmx, vdmxLength);
126 // We ignore the version. Future tables should be backwards compatible with
129 if (!buf.skip(4) || !buf.readU16(&numRatios))
132 // Now we have two tables. Firstly we have @numRatios Ratio records, then a
133 // matching array of @numRatios offsets. We save the offset of the beginning
134 // of this second table.
136 // Range 6 <= x <= 262146
137 unsigned long offsetTableOffset =
138 buf.offset() + 4 /* sizeof struct ratio */ * numRatios;
140 unsigned desiredRatio = 0xffffffff;
141 // We read 4 bytes per record, so the offset range is
143 for (unsigned i = 0; i < numRatios; ++i) {
144 uint8_t xRatio, yRatio1, yRatio2;
147 || !buf.readU8(&xRatio)
148 || !buf.readU8(&yRatio1)
149 || !buf.readU8(&yRatio2))
152 // This either covers 1:1, or this is the default entry (0, 0, 0)
153 if ((xRatio == 1 && yRatio1 <= 1 && yRatio2 >= 1)
154 || (xRatio == 0 && yRatio1 == 0 && yRatio2 == 0)) {
160 if (desiredRatio == 0xffffffff) // no ratio found
163 // Range 10 <= x <= 393216
164 buf.setOffset(offsetTableOffset + sizeof(uint16_t) * desiredRatio);
166 // Now we read from the offset table to get the offset of another array
167 uint16_t groupOffset;
168 if (!buf.readU16(&groupOffset))
170 // Range 0 <= x <= 65535
171 buf.setOffset(groupOffset);
174 if (!buf.readU16(&numRecords) || !buf.skip(sizeof(uint16_t)))
177 // We read 6 bytes per record, so the offset range is
179 for (unsigned i = 0; i < numRecords; ++i) {
181 if (!buf.readU16(&pixelSize))
183 // the entries are sorted, so we can abort early if need be
184 if (pixelSize > targetPixelSize)
187 if (pixelSize == targetPixelSize) {
188 int16_t tempYMax, tempYMin;
189 if (!buf.readS16(&tempYMax)
190 || !buf.readS16(&tempYMin))
196 if (!buf.skip(2 * sizeof(int16_t)))
203 } // namespace WebCore