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.
36 #include <arpa/inet.h>
38 // Buffer helper class
40 // This class perform some trival buffer operations while checking for
41 // out-of-bounds errors. As a family they return false if anything is amiss,
42 // updating the current offset otherwise.
45 Buffer(const uint8_t* buffer, size_t length)
50 bool skip(size_t numBytes)
52 if (m_offset + numBytes > m_length)
58 bool readU8(uint8_t* value)
60 if (m_offset + sizeof(uint8_t) > m_length)
62 *value = m_buffer[m_offset];
63 m_offset += sizeof(uint8_t);
67 bool readU16(uint16_t* value)
69 if (m_offset + sizeof(uint16_t) > m_length)
71 memcpy(value, m_buffer + m_offset, sizeof(uint16_t));
72 *value = ntohs(*value);
73 m_offset += sizeof(uint16_t);
77 bool readS16(int16_t* value)
79 return readU16(reinterpret_cast<uint16_t*>(value));
87 void setOffset(size_t newoffset)
93 const uint8_t *const m_buffer;
94 const size_t m_length;
100 // VDMX tables are found in some TrueType/OpenType fonts and contain
101 // ascender/descender overrides for certain (usually small) sizes. This is
102 // needed in order to match font metrics on Windows.
104 // Freetype does not parse these tables so we do so here.
108 // Parse a TrueType VDMX table.
109 // yMax: (output) the ascender value from the table
110 // yMin: (output) the descender value from the table (negative!)
111 // vdmx: the table bytes
112 // vdmxLength: length of @vdmx, in bytes
113 // targetPixelSize: the pixel size of the font (e.g. 16)
115 // Returns true iff a suitable match are found. Otherwise, *yMax and *yMin are
116 // untouched. size_t must be 32-bits to avoid overflow.
118 // See http://www.microsoft.com/opentype/otspec/vdmx.htm
119 bool parseVDMX(int* yMax, int* yMin,
120 const uint8_t* vdmx, size_t vdmxLength,
121 unsigned targetPixelSize)
123 Buffer buf(vdmx, vdmxLength);
125 // We ignore the version. Future tables should be backwards compatible with
128 if (!buf.skip(4) || !buf.readU16(&numRatios))
131 // Now we have two tables. Firstly we have @numRatios Ratio records, then a
132 // matching array of @numRatios offsets. We save the offset of the beginning
133 // of this second table.
135 // Range 6 <= x <= 262146
136 unsigned long offsetTableOffset =
137 buf.offset() + 4 /* sizeof struct ratio */ * numRatios;
139 unsigned desiredRatio = 0xffffffff;
140 // We read 4 bytes per record, so the offset range is
142 for (unsigned i = 0; i < numRatios; ++i) {
143 uint8_t xRatio, yRatio1, yRatio2;
146 || !buf.readU8(&xRatio)
147 || !buf.readU8(&yRatio1)
148 || !buf.readU8(&yRatio2))
151 // This either covers 1:1, or this is the default entry (0, 0, 0)
152 if ((xRatio == 1 && yRatio1 <= 1 && yRatio2 >= 1)
153 || (xRatio == 0 && yRatio1 == 0 && yRatio2 == 0)) {
159 if (desiredRatio == 0xffffffff) // no ratio found
162 // Range 10 <= x <= 393216
163 buf.setOffset(offsetTableOffset + sizeof(uint16_t) * desiredRatio);
165 // Now we read from the offset table to get the offset of another array
166 uint16_t groupOffset;
167 if (!buf.readU16(&groupOffset))
169 // Range 0 <= x <= 65535
170 buf.setOffset(groupOffset);
173 if (!buf.readU16(&numRecords) || !buf.skip(sizeof(uint16_t)))
176 // We read 6 bytes per record, so the offset range is
178 for (unsigned i = 0; i < numRecords; ++i) {
180 if (!buf.readU16(&pixelSize))
182 // the entries are sorted, so we can abort early if need be
183 if (pixelSize > targetPixelSize)
186 if (pixelSize == targetPixelSize) {
187 int16_t tempYMax, tempYMin;
188 if (!buf.readS16(&tempYMax)
189 || !buf.readS16(&tempYMin))
195 if (!buf.skip(2 * sizeof(int16_t)))
202 } // namespace WebCore