#include "config.h"
#include "OpenTypeCG.h"
#include "OpenTypeTypes.h"
namespace WebCore {
namespace OpenType {
#if PLATFORM(WIN)
static const unsigned long kCTFontTableOS2 = 'OS/2';
#endif
bool fontHasMathTable(CTFontRef ctFont)
{
RetainPtr<CFArrayRef> tableTags = adoptCF(CTFontCopyAvailableTables(ctFont, kCTFontTableOptionNoOptions));
if (!tableTags)
return false;
CFIndex numTables = CFArrayGetCount(tableTags.get());
for (CFIndex index = 0; index < numTables; ++index) {
CTFontTableTag tag = (CTFontTableTag)(uintptr_t)CFArrayGetValueAtIndex(tableTags.get(), index);
if (tag == 'MATH')
return true;
}
return false;
}
static inline short readShortFromTable(const UInt8* os2Data, CFIndex offset)
{
return *(reinterpret_cast<const OpenType::Int16*>(os2Data + offset));
}
bool tryGetTypoMetrics(CTFontRef font, short& ascent, short& descent, short& lineGap)
{
bool result = false;
if (auto os2Table = adoptCF(CTFontCopyTable(font, kCTFontTableOS2, kCTFontTableOptionNoOptions))) {
const CFIndex fsSelectionOffset = 16 * 2 + 10 + 4 * 4 + 4 * 1;
const CFIndex sTypoAscenderOffset = fsSelectionOffset + 3 * 2;
const CFIndex sTypoDescenderOffset = sTypoAscenderOffset + 2;
const CFIndex sTypoLineGapOffset = sTypoDescenderOffset + 2;
if (CFDataGetLength(os2Table.get()) >= sTypoLineGapOffset + 2) {
const UInt8* os2Data = CFDataGetBytePtr(os2Table.get());
const UInt8 useTypoMetricsMask = 1 << 7;
if (*(os2Data + fsSelectionOffset + 1) & useTypoMetricsMask) {
ascent = readShortFromTable(os2Data, sTypoAscenderOffset);
descent = readShortFromTable(os2Data, sTypoDescenderOffset);
lineGap = readShortFromTable(os2Data, sTypoLineGapOffset);
result = true;
}
}
}
return result;
}
} }