|
|
|
|
| 8 |
#include "hhea.h" |
8 |
#include "hhea.h" |
| 9 |
#include "maxp.h" |
9 |
#include "maxp.h" |
| 10 |
|
10 |
|
| 11 |
// hmtx - Horizontal Metrics |
11 |
// hmtx - Horizontal Metrics |
| 12 |
// http://www.microsoft.com/opentype/otspec/hmtx.htm |
12 |
// http://www.microsoft.com/opentype/otspec/hmtx.htm |
| 13 |
|
13 |
|
| 14 |
namespace ots { |
14 |
namespace ots { |
| 15 |
|
15 |
|
| 16 |
bool ots_hmtx_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { |
16 |
bool ots_Xmtx_parse(OpenTypeFile *file, const uint8_t *data, size_t length, |
|
|
17 |
const OpenTypeHHEA *hhea, OpenTypeHMTX **out_hmtx) { |
| 17 |
Buffer table(data, length); |
18 |
Buffer table(data, length); |
| 18 |
OpenTypeHMTX *hmtx = new OpenTypeHMTX; |
19 |
OpenTypeHMTX *hmtx = new OpenTypeHMTX; |
| 19 |
file->hmtx = hmtx; |
20 |
*out_hmtx = hmtx; |
| 20 |
|
21 |
|
| 21 |
if (!file->hhea || !file->maxp) { |
22 |
if (!hhea || !file->maxp) { |
| 22 |
return OTS_FAILURE(); |
23 |
return OTS_FAILURE(); |
| 23 |
} |
24 |
} |
| 24 |
|
25 |
|
| 25 |
// |num_hmetrics| is a uint16_t, so it's bounded < 65536. This limits that |
26 |
// |num_hmetrics| is a uint16_t, so it's bounded < 65536. This limits that |
| 26 |
// amount of memory that we'll allocate for this to a sane amount. |
27 |
// amount of memory that we'll allocate for this to a sane amount. |
| 27 |
const unsigned num_hmetrics = file->hhea->num_hmetrics; |
28 |
const unsigned num_hmetrics = hhea->num_hmetrics; |
| 28 |
|
29 |
|
| 29 |
if (num_hmetrics > file->maxp->num_glyphs) { |
30 |
if (num_hmetrics > file->maxp->num_glyphs) { |
| 30 |
return OTS_FAILURE(); |
31 |
return OTS_FAILURE(); |
| 31 |
} |
32 |
} |
| 32 |
if (!num_hmetrics) { |
33 |
if (!num_hmetrics) { |
| 33 |
return OTS_FAILURE(); |
34 |
return OTS_FAILURE(); |
| 34 |
} |
35 |
} |
| 35 |
const unsigned num_lsbs = file->maxp->num_glyphs - num_hmetrics; |
36 |
const unsigned num_lsbs = file->maxp->num_glyphs - num_hmetrics; |
|
Lines 40-108
bool ots_hmtx_parse(OpenTypeFile *file,
|
Link Here
|
|---|
|
| 40 |
int16_t lsb = 0; |
41 |
int16_t lsb = 0; |
| 41 |
if (!table.ReadU16(&adv) || !table.ReadS16(&lsb)) { |
42 |
if (!table.ReadU16(&adv) || !table.ReadS16(&lsb)) { |
| 42 |
return OTS_FAILURE(); |
43 |
return OTS_FAILURE(); |
| 43 |
} |
44 |
} |
| 44 |
|
45 |
|
| 45 |
// Since so many fonts don't have proper value on |adv| and |lsb|, |
46 |
// Since so many fonts don't have proper value on |adv| and |lsb|, |
| 46 |
// we should not call ots_failure() here. For example, about 20% of fonts |
47 |
// we should not call ots_failure() here. For example, about 20% of fonts |
| 47 |
// in http://www.princexml.com/fonts/ (200+ fonts) fails these tests. |
48 |
// in http://www.princexml.com/fonts/ (200+ fonts) fails these tests. |
| 48 |
if (adv > file->hhea->adv_width_max) { |
49 |
if (adv > hhea->adv_width_max) { |
| 49 |
OTS_WARNING("bad adv: %u > %u", adv, file->hhea->adv_width_max); |
50 |
OTS_WARNING("bad adv: %u > %u", adv, hhea->adv_width_max); |
| 50 |
adv = file->hhea->adv_width_max; |
51 |
adv = hhea->adv_width_max; |
| 51 |
} |
52 |
} |
| 52 |
if (lsb < file->hhea->min_lsb) { |
53 |
if (lsb < hhea->min_lsb) { |
| 53 |
OTS_WARNING("bad lsb: %d < %d", lsb, file->hhea->min_lsb); |
54 |
OTS_WARNING("bad lsb: %d < %d", lsb, hhea->min_lsb); |
| 54 |
lsb = file->hhea->min_lsb; |
55 |
lsb = hhea->min_lsb; |
| 55 |
} |
56 |
} |
| 56 |
|
57 |
|
| 57 |
hmtx->metrics.push_back(std::make_pair(adv, lsb)); |
58 |
hmtx->metrics.push_back(std::make_pair(adv, lsb)); |
| 58 |
} |
59 |
} |
| 59 |
|
60 |
|
| 60 |
hmtx->lsbs.reserve(num_lsbs); |
61 |
hmtx->lsbs.reserve(num_lsbs); |
| 61 |
for (unsigned i = 0; i < num_lsbs; ++i) { |
62 |
for (unsigned i = 0; i < num_lsbs; ++i) { |
| 62 |
int16_t lsb; |
63 |
int16_t lsb; |
| 63 |
if (!table.ReadS16(&lsb)) { |
64 |
if (!table.ReadS16(&lsb)) { |
| 64 |
// Some Japanese fonts (e.g., mona.ttf) fail this test. |
65 |
// Some Japanese fonts (e.g., mona.ttf) fail this test. |
| 65 |
return OTS_FAILURE(); |
66 |
return OTS_FAILURE(); |
| 66 |
} |
67 |
} |
| 67 |
|
68 |
|
| 68 |
if (lsb < file->hhea->min_lsb) { |
69 |
if (lsb < hhea->min_lsb) { |
| 69 |
// The same as above. Three fonts in http://www.fontsquirrel.com/fontface |
70 |
// The same as above. Three fonts in http://www.fontsquirrel.com/fontface |
| 70 |
// (e.g., Notice2Std.otf) have weird lsb values. |
71 |
// (e.g., Notice2Std.otf) have weird lsb values. |
| 71 |
OTS_WARNING("bad lsb: %d < %d", lsb, file->hhea->min_lsb); |
72 |
OTS_WARNING("bad lsb: %d < %d", lsb, hhea->min_lsb); |
| 72 |
lsb = file->hhea->min_lsb; |
73 |
lsb = hhea->min_lsb; |
| 73 |
} |
74 |
} |
| 74 |
|
75 |
|
| 75 |
hmtx->lsbs.push_back(lsb); |
76 |
hmtx->lsbs.push_back(lsb); |
| 76 |
} |
77 |
} |
| 77 |
|
78 |
|
| 78 |
return true; |
79 |
return true; |
| 79 |
} |
80 |
} |
| 80 |
|
81 |
|
|
|
82 |
bool ots_hmtx_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { |
| 83 |
return ots_Xmtx_parse(file, data, length, file->hhea, &file->hmtx); |
| 84 |
} |
| 85 |
|
| 86 |
bool ots_vmtx_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { |
| 87 |
return ots_Xmtx_parse(file, data, length, reinterpret_cast<const OpenTypeHHEA*>(file->vhea), reinterpret_cast<OpenTypeHMTX**>(&file->vmtx)); |
| 88 |
} |
| 89 |
|
| 81 |
bool ots_hmtx_should_serialise(OpenTypeFile *file) { |
90 |
bool ots_hmtx_should_serialise(OpenTypeFile *file) { |
| 82 |
return file->hmtx; |
91 |
return file->hmtx; |
| 83 |
} |
92 |
} |
| 84 |
|
93 |
|
| 85 |
bool ots_hmtx_serialise(OTSStream *out, OpenTypeFile *file) { |
94 |
bool ots_vmtx_should_serialise(OpenTypeFile *file) { |
| 86 |
const OpenTypeHMTX *hmtx = file->hmtx; |
95 |
return file->preserve_otl && file->vmtx; |
|
|
96 |
} |
| 87 |
|
97 |
|
|
|
98 |
bool ots_Xmtx_serialise(OTSStream *out, OpenTypeFile *file, const OpenTypeHMTX *hmtx) { |
| 88 |
for (unsigned i = 0; i < hmtx->metrics.size(); ++i) { |
99 |
for (unsigned i = 0; i < hmtx->metrics.size(); ++i) { |
| 89 |
if (!out->WriteU16(hmtx->metrics[i].first) || |
100 |
if (!out->WriteU16(hmtx->metrics[i].first) || |
| 90 |
!out->WriteS16(hmtx->metrics[i].second)) { |
101 |
!out->WriteS16(hmtx->metrics[i].second)) { |
| 91 |
return OTS_FAILURE(); |
102 |
return OTS_FAILURE(); |
| 92 |
} |
103 |
} |
| 93 |
} |
104 |
} |
| 94 |
|
105 |
|
| 95 |
for (unsigned i = 0; i < hmtx->lsbs.size(); ++i) { |
106 |
for (unsigned i = 0; i < hmtx->lsbs.size(); ++i) { |
| 96 |
if (!out->WriteS16(hmtx->lsbs[i])) { |
107 |
if (!out->WriteS16(hmtx->lsbs[i])) { |
| 97 |
return OTS_FAILURE(); |
108 |
return OTS_FAILURE(); |
| 98 |
} |
109 |
} |
| 99 |
} |
110 |
} |
| 100 |
|
111 |
|
| 101 |
return true; |
112 |
return true; |
| 102 |
} |
113 |
} |
| 103 |
|
114 |
|
|
|
115 |
bool ots_hmtx_serialise(OTSStream *out, OpenTypeFile *file) { |
| 116 |
return ots_Xmtx_serialise(out, file, file->hmtx); |
| 117 |
} |
| 118 |
|
| 119 |
bool ots_vmtx_serialise(OTSStream *out, OpenTypeFile *file) { |
| 120 |
return ots_Xmtx_serialise(out, file, reinterpret_cast<const OpenTypeHMTX*>(file->vmtx)); |
| 121 |
} |
| 122 |
|
| 104 |
void ots_hmtx_free(OpenTypeFile *file) { |
123 |
void ots_hmtx_free(OpenTypeFile *file) { |
| 105 |
delete file->hmtx; |
124 |
delete file->hmtx; |
| 106 |
} |
125 |
} |
| 107 |
|
126 |
|
|
|
127 |
void ots_vmtx_free(OpenTypeFile *file) { |
| 128 |
delete file->vmtx; |
| 129 |
} |
| 130 |
|
| 108 |
} // namespace ots |
131 |
} // namespace ots |