| 1 |
// -*- related-file-name: "../../libefont/otfdata.cc" -*- |
| 2 |
#ifndef EFONT_OTFDATA_HH |
| 3 |
#define EFONT_OTFDATA_HH |
| 4 |
#include <lcdf/string.hh> |
| 5 |
#include <lcdf/vector.hh> |
| 6 |
#include <lcdf/inttypes.h> |
| 7 |
#ifdef HAVE_SYS_TYPES_H |
| 8 |
# include <sys/types.h> |
| 9 |
#endif |
| 10 |
#if NEED_ARPA_INET_H |
| 11 |
# include <arpa/inet.h> |
| 12 |
#endif |
| 13 |
#if HAVE_BYTEORDER_H |
| 14 |
# include <byteorder.h> |
| 15 |
#elif HAVE_NETINET_IN_H |
| 16 |
# include <netinet/in.h> |
| 17 |
#elif HAVE_SYS_PARAM_H |
| 18 |
# include <sys/param.h> |
| 19 |
#elif !defined(WIN32) |
| 20 |
# error "configury disaster! Report this error to [email protected]" |
| 21 |
#endif |
| 22 |
#ifdef WIN32 |
| 23 |
# ifdef __MSC_VER |
| 24 |
# pragma warning (disable: 4290) |
| 25 |
# endif |
| 26 |
# include <winsock2.h> |
| 27 |
#endif |
| 28 |
class ErrorHandler; |
| 29 |
namespace Efont { namespace OpenType { |
| 30 |
|
| 31 |
#if HAVE_PRECONDITION_CHECKING |
| 32 |
#define efont_precondition(x) assert((x)) |
| 33 |
#else |
| 34 |
#define efont_precondition(x) |
| 35 |
#endif |
| 36 |
|
| 37 |
class Error { |
| 38 |
public: |
| 39 |
String description; |
| 40 |
Error() : description(String::make_stable("unspecified error")) { } |
| 41 |
Error(const String &d) : description(d) { } |
| 42 |
}; |
| 43 |
|
| 44 |
class Bounds : public Error { |
| 45 |
public: |
| 46 |
Bounds() : Error(String::make_stable("bounds error")) { } |
| 47 |
}; |
| 48 |
|
| 49 |
class Format : public Error { |
| 50 |
public: |
| 51 |
Format(const String& name) : Error(name + " format error") { } |
| 52 |
Format(const String& name, const String& type) |
| 53 |
: Error(name + " " + type + " format error") { } |
| 54 |
}; |
| 55 |
|
| 56 |
class BlankTable : public Format { |
| 57 |
public: |
| 58 |
BlankTable(const String& name) : Format(name, "blank table") { } |
| 59 |
}; |
| 60 |
|
| 61 |
class Data { |
| 62 |
public: |
| 63 |
Data() { } |
| 64 |
Data(const String& str) : _str(str) { _str.align(2); } |
| 65 |
// default copy constructor |
| 66 |
// default destructor |
| 67 |
|
| 68 |
const String& string() const { return _str; } |
| 69 |
operator const String&() const { return _str; } |
| 70 |
|
| 71 |
operator String::unspecified_bool_type() const { return _str; } |
| 72 |
const uint8_t *udata() const { return _str.udata(); } |
| 73 |
int length() const { return _str.length(); } |
| 74 |
|
| 75 |
static inline uint16_t u16(const unsigned char* s); |
| 76 |
static inline uint16_t u16_aligned(const unsigned char* s); |
| 77 |
static inline int16_t s16(const unsigned char* s); |
| 78 |
static inline int16_t s16_aligned(const unsigned char* s); |
| 79 |
static inline uint32_t u32(const unsigned char* s); |
| 80 |
static inline uint32_t u32_aligned16(const unsigned char* s); |
| 81 |
static inline uint32_t u32_aligned(const unsigned char* s); |
| 82 |
static inline int32_t s32(const unsigned char* s); |
| 83 |
static inline int32_t s32_aligned16(const unsigned char* s); |
| 84 |
static inline int32_t s32_aligned(const unsigned char* s); |
| 85 |
static inline double fixed_aligned16(const unsigned char* s); |
| 86 |
static inline double fixed_aligned(const unsigned char* s); |
| 87 |
|
| 88 |
inline uint8_t operator[](unsigned offset) const; |
| 89 |
inline uint16_t u16(unsigned offset) const; |
| 90 |
inline int16_t s16(unsigned offset) const; |
| 91 |
inline uint32_t u32(unsigned offset) const; |
| 92 |
inline int32_t s32(unsigned offset) const; |
| 93 |
inline uint8_t operator[](int offset) const; |
| 94 |
inline uint16_t u16(int offset) const; |
| 95 |
inline int16_t s16(int offset) const; |
| 96 |
inline uint32_t u32(int offset) const; |
| 97 |
inline int32_t s32(int offset) const; |
| 98 |
|
| 99 |
Data subtable(unsigned offset) const; |
| 100 |
Data offset_subtable(unsigned offset_offset) const; |
| 101 |
inline Data substring(int left, int len = -1) const noexcept; |
| 102 |
|
| 103 |
void align_long() { _str.align(4); } |
| 104 |
|
| 105 |
private: |
| 106 |
|
| 107 |
String _str; |
| 108 |
|
| 109 |
}; |
| 110 |
|
| 111 |
inline uint8_t Data::operator[](unsigned offset) const { |
| 112 |
if (offset >= static_cast<unsigned>(_str.length())) |
| 113 |
throw Bounds(); |
| 114 |
else |
| 115 |
return _str[offset]; |
| 116 |
} |
| 117 |
|
| 118 |
inline uint16_t Data::u16(const unsigned char* s) { |
| 119 |
return (s[0] << 8) + s[1]; |
| 120 |
} |
| 121 |
|
| 122 |
inline uint16_t Data::u16_aligned(const unsigned char* s) { |
| 123 |
efont_precondition((reinterpret_cast<uintptr_t>(s) & 1) == 0); |
| 124 |
return ntohs(*reinterpret_cast<const uint16_t*>(s)); |
| 125 |
} |
| 126 |
|
| 127 |
inline int16_t Data::s16(const unsigned char* s) { |
| 128 |
return (int16_t) ((s[0] << 8) + s[1]); |
| 129 |
} |
| 130 |
|
| 131 |
inline int16_t Data::s16_aligned(const unsigned char* s) { |
| 132 |
efont_precondition((reinterpret_cast<uintptr_t>(s) & 1) == 0); |
| 133 |
return ntohs(*reinterpret_cast<const int16_t*>(s)); |
| 134 |
} |
| 135 |
|
| 136 |
inline uint32_t Data::u32(const unsigned char* s) { |
| 137 |
return ((unsigned) s[0] << 24) + (s[1] << 16) + (s[2] << 8) + s[3]; |
| 138 |
} |
| 139 |
|
| 140 |
inline uint32_t Data::u32_aligned16(const unsigned char* s) { |
| 141 |
efont_precondition((reinterpret_cast<uintptr_t>(s) & 1) == 0); |
| 142 |
return ((unsigned) u16_aligned(s) << 16) + u16_aligned(s + 2); |
| 143 |
} |
| 144 |
|
| 145 |
inline uint32_t Data::u32_aligned(const unsigned char* s) { |
| 146 |
efont_precondition((reinterpret_cast<uintptr_t>(s) & 3) == 0); |
| 147 |
return ntohl(*reinterpret_cast<const uint32_t*>(s)); |
| 148 |
} |
| 149 |
|
| 150 |
inline int32_t Data::s32(const unsigned char* s) { |
| 151 |
return (int32_t) (((unsigned) s[0] << 24) + (s[1] << 16) + (s[2] << 8) + s[3]); |
| 152 |
} |
| 153 |
|
| 154 |
inline int32_t Data::s32_aligned16(const unsigned char* s) { |
| 155 |
efont_precondition((reinterpret_cast<uintptr_t>(s) & 1) == 0); |
| 156 |
return (int32_t) (((unsigned) u16_aligned(s) << 16) + u16_aligned(s + 2)); |
| 157 |
} |
| 158 |
|
| 159 |
inline int32_t Data::s32_aligned(const unsigned char* s) { |
| 160 |
efont_precondition((reinterpret_cast<uintptr_t>(s) & 3) == 0); |
| 161 |
return ntohl(*reinterpret_cast<const int32_t*>(s)); |
| 162 |
} |
| 163 |
|
| 164 |
inline double Data::fixed_aligned16(const unsigned char* s) { |
| 165 |
return s32_aligned16(s) / 65536.; |
| 166 |
} |
| 167 |
|
| 168 |
inline double Data::fixed_aligned(const unsigned char* s) { |
| 169 |
return s32_aligned(s) / 65536.; |
| 170 |
} |
| 171 |
|
| 172 |
inline uint16_t Data::u16(unsigned offset) const { |
| 173 |
if (offset + 1 >= static_cast<unsigned>(_str.length()) || offset + 1 == 0) |
| 174 |
throw Bounds(); |
| 175 |
else |
| 176 |
return u16_aligned(_str.udata() + offset); |
| 177 |
} |
| 178 |
|
| 179 |
inline int16_t Data::s16(unsigned offset) const { |
| 180 |
if (offset + 1 >= static_cast<unsigned>(_str.length()) || offset + 1 == 0) |
| 181 |
throw Bounds(); |
| 182 |
else |
| 183 |
return s16_aligned(_str.udata() + offset); |
| 184 |
} |
| 185 |
|
| 186 |
inline uint32_t Data::u32(unsigned offset) const { |
| 187 |
if (offset + 3 >= static_cast<unsigned>(_str.length()) || offset + 3 < 3) |
| 188 |
throw Bounds(); |
| 189 |
else |
| 190 |
return u32_aligned16(_str.udata() + offset); |
| 191 |
} |
| 192 |
|
| 193 |
inline int32_t Data::s32(unsigned offset) const { |
| 194 |
if (offset + 3 >= static_cast<unsigned>(_str.length()) || offset + 3 < 3) |
| 195 |
throw Bounds(); |
| 196 |
else |
| 197 |
return s32_aligned16(_str.udata() + offset); |
| 198 |
} |
| 199 |
|
| 200 |
inline uint8_t Data::operator[](int offset) const { |
| 201 |
return (*this)[unsigned(offset)]; |
| 202 |
} |
| 203 |
|
| 204 |
inline uint16_t Data::u16(int offset) const { |
| 205 |
return u16(unsigned(offset)); |
| 206 |
} |
| 207 |
|
| 208 |
inline int16_t Data::s16(int offset) const { |
| 209 |
return s16(unsigned(offset)); |
| 210 |
} |
| 211 |
|
| 212 |
inline uint32_t Data::u32(int offset) const { |
| 213 |
return u32(unsigned(offset)); |
| 214 |
} |
| 215 |
|
| 216 |
inline int32_t Data::s32(int offset) const { |
| 217 |
return s32(unsigned(offset)); |
| 218 |
} |
| 219 |
|
| 220 |
inline Data Data::substring(int left, int len) const noexcept { |
| 221 |
return Data(_str.substring(left, len)); |
| 222 |
} |
| 223 |
|
| 224 |
} // namespace Efont::OpenType |
| 225 |
} // namespace Efont |
| 226 |
#endif |