#ifndef HB_OT_MATH_TABLE_HH
#define HB_OT_MATH_TABLE_HH
#include "hb-open-type-private.hh"
#include "hb-ot-layout-common-private.hh"
#include "hb-ot-math.h"
namespace OT {
struct MathValueRecord
{
inline hb_position_t get_x_value (hb_font_t *font, const void *base) const
{ return font->em_scale_x (value) + (base+deviceTable).get_x_delta (font); }
inline hb_position_t get_y_value (hb_font_t *font, const void *base) const
{ return font->em_scale_y (value) + (base+deviceTable).get_y_delta (font); }
inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
{
TRACE_SANITIZE (this);
return_trace (c->check_struct (this) && deviceTable.sanitize (c, base));
}
protected:
SHORT value;
OffsetTo<Device> deviceTable;
public:
DEFINE_SIZE_STATIC (4);
};
struct MathConstants
{
inline bool sanitize_math_value_records (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
unsigned int count = ARRAY_LENGTH (mathValueRecords);
for (unsigned int i = 0; i < count; i++)
if (!mathValueRecords[i].sanitize (c, this))
return_trace (false);
return_trace (true);
}
inline bool sanitize (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
return_trace (c->check_struct (this) && sanitize_math_value_records(c));
}
inline hb_position_t get_value (hb_ot_math_constant_t constant,
hb_font_t *font) const
{
switch (constant) {
case HB_OT_MATH_CONSTANT_SCRIPT_PERCENT_SCALE_DOWN:
case HB_OT_MATH_CONSTANT_SCRIPT_SCRIPT_PERCENT_SCALE_DOWN:
return percentScaleDown[constant - HB_OT_MATH_CONSTANT_SCRIPT_PERCENT_SCALE_DOWN];
case HB_OT_MATH_CONSTANT_DELIMITED_SUB_FORMULA_MIN_HEIGHT:
case HB_OT_MATH_CONSTANT_DISPLAY_OPERATOR_MIN_HEIGHT:
return font->em_scale_y (minHeight[constant - HB_OT_MATH_CONSTANT_DELIMITED_SUB_FORMULA_MIN_HEIGHT]);
case HB_OT_MATH_CONSTANT_RADICAL_KERN_AFTER_DEGREE:
case HB_OT_MATH_CONSTANT_RADICAL_KERN_BEFORE_DEGREE:
case HB_OT_MATH_CONSTANT_SKEWED_FRACTION_HORIZONTAL_GAP:
case HB_OT_MATH_CONSTANT_SPACE_AFTER_SCRIPT:
return mathValueRecords[constant - HB_OT_MATH_CONSTANT_MATH_LEADING].get_x_value(font, this);
case HB_OT_MATH_CONSTANT_ACCENT_BASE_HEIGHT:
case HB_OT_MATH_CONSTANT_AXIS_HEIGHT:
case HB_OT_MATH_CONSTANT_FLATTENED_ACCENT_BASE_HEIGHT:
case HB_OT_MATH_CONSTANT_FRACTION_DENOMINATOR_DISPLAY_STYLE_SHIFT_DOWN:
case HB_OT_MATH_CONSTANT_FRACTION_DENOMINATOR_GAP_MIN:
case HB_OT_MATH_CONSTANT_FRACTION_DENOMINATOR_SHIFT_DOWN:
case HB_OT_MATH_CONSTANT_FRACTION_DENOM_DISPLAY_STYLE_GAP_MIN:
case HB_OT_MATH_CONSTANT_FRACTION_NUMERATOR_DISPLAY_STYLE_SHIFT_UP:
case HB_OT_MATH_CONSTANT_FRACTION_NUMERATOR_GAP_MIN:
case HB_OT_MATH_CONSTANT_FRACTION_NUMERATOR_SHIFT_UP:
case HB_OT_MATH_CONSTANT_FRACTION_NUM_DISPLAY_STYLE_GAP_MIN:
case HB_OT_MATH_CONSTANT_FRACTION_RULE_THICKNESS:
case HB_OT_MATH_CONSTANT_LOWER_LIMIT_BASELINE_DROP_MIN:
case HB_OT_MATH_CONSTANT_LOWER_LIMIT_GAP_MIN:
case HB_OT_MATH_CONSTANT_MATH_LEADING:
case HB_OT_MATH_CONSTANT_OVERBAR_EXTRA_ASCENDER:
case HB_OT_MATH_CONSTANT_OVERBAR_RULE_THICKNESS:
case HB_OT_MATH_CONSTANT_OVERBAR_VERTICAL_GAP:
case HB_OT_MATH_CONSTANT_RADICAL_DISPLAY_STYLE_VERTICAL_GAP:
case HB_OT_MATH_CONSTANT_RADICAL_EXTRA_ASCENDER:
case HB_OT_MATH_CONSTANT_RADICAL_RULE_THICKNESS:
case HB_OT_MATH_CONSTANT_RADICAL_VERTICAL_GAP:
case HB_OT_MATH_CONSTANT_SKEWED_FRACTION_VERTICAL_GAP:
case HB_OT_MATH_CONSTANT_STACK_BOTTOM_DISPLAY_STYLE_SHIFT_DOWN:
case HB_OT_MATH_CONSTANT_STACK_BOTTOM_SHIFT_DOWN:
case HB_OT_MATH_CONSTANT_STACK_DISPLAY_STYLE_GAP_MIN:
case HB_OT_MATH_CONSTANT_STACK_GAP_MIN:
case HB_OT_MATH_CONSTANT_STACK_TOP_DISPLAY_STYLE_SHIFT_UP:
case HB_OT_MATH_CONSTANT_STACK_TOP_SHIFT_UP:
case HB_OT_MATH_CONSTANT_STRETCH_STACK_BOTTOM_SHIFT_DOWN:
case HB_OT_MATH_CONSTANT_STRETCH_STACK_GAP_ABOVE_MIN:
case HB_OT_MATH_CONSTANT_STRETCH_STACK_GAP_BELOW_MIN:
case HB_OT_MATH_CONSTANT_STRETCH_STACK_TOP_SHIFT_UP:
case HB_OT_MATH_CONSTANT_SUBSCRIPT_BASELINE_DROP_MIN:
case HB_OT_MATH_CONSTANT_SUBSCRIPT_SHIFT_DOWN:
case HB_OT_MATH_CONSTANT_SUBSCRIPT_TOP_MAX:
case HB_OT_MATH_CONSTANT_SUB_SUPERSCRIPT_GAP_MIN:
case HB_OT_MATH_CONSTANT_SUPERSCRIPT_BASELINE_DROP_MAX:
case HB_OT_MATH_CONSTANT_SUPERSCRIPT_BOTTOM_MAX_WITH_SUBSCRIPT:
case HB_OT_MATH_CONSTANT_SUPERSCRIPT_BOTTOM_MIN:
case HB_OT_MATH_CONSTANT_SUPERSCRIPT_SHIFT_UP:
case HB_OT_MATH_CONSTANT_SUPERSCRIPT_SHIFT_UP_CRAMPED:
case HB_OT_MATH_CONSTANT_UNDERBAR_EXTRA_DESCENDER:
case HB_OT_MATH_CONSTANT_UNDERBAR_RULE_THICKNESS:
case HB_OT_MATH_CONSTANT_UNDERBAR_VERTICAL_GAP:
case HB_OT_MATH_CONSTANT_UPPER_LIMIT_BASELINE_RISE_MIN:
case HB_OT_MATH_CONSTANT_UPPER_LIMIT_GAP_MIN:
return mathValueRecords[constant - HB_OT_MATH_CONSTANT_MATH_LEADING].get_y_value(font, this);
case HB_OT_MATH_CONSTANT_RADICAL_DEGREE_BOTTOM_RAISE_PERCENT:
return radicalDegreeBottomRaisePercent;
default:
return 0;
}
}
protected:
SHORT percentScaleDown[2];
USHORT minHeight[2];
MathValueRecord mathValueRecords[51];
SHORT radicalDegreeBottomRaisePercent;
public:
DEFINE_SIZE_STATIC (214);
};
struct MathItalicsCorrectionInfo
{
inline bool sanitize (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
return_trace (c->check_struct (this) &&
coverage.sanitize (c, this) &&
italicsCorrection.sanitize (c, this));
}
inline hb_position_t get_value (hb_codepoint_t glyph,
hb_font_t *font) const
{
unsigned int index = (this+coverage).get_coverage (glyph);
return italicsCorrection[index].get_x_value (font, this);
}
protected:
OffsetTo<Coverage> coverage;
ArrayOf<MathValueRecord> italicsCorrection;
public:
DEFINE_SIZE_ARRAY (4, italicsCorrection);
};
struct MathTopAccentAttachment
{
inline bool sanitize (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
return_trace (c->check_struct (this) &&
topAccentCoverage.sanitize (c, this) &&
topAccentAttachment.sanitize (c, this));
}
inline hb_position_t get_value (hb_codepoint_t glyph,
hb_font_t *font) const
{
unsigned int index = (this+topAccentCoverage).get_coverage (glyph);
if (index == NOT_COVERED)
return font->get_glyph_h_advance (glyph) / 2;
return topAccentAttachment[index].get_x_value(font, this);
}
protected:
OffsetTo<Coverage> topAccentCoverage;
ArrayOf<MathValueRecord> topAccentAttachment;
public:
DEFINE_SIZE_ARRAY (2 + 2, topAccentAttachment);
};
struct MathKern
{
inline bool sanitize_math_value_records (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
unsigned int count = 2 * heightCount + 1;
for (unsigned int i = 0; i < count; i++)
if (!mathValueRecords[i].sanitize (c, this)) return_trace (false);
return_trace (true);
}
inline bool sanitize (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
return_trace (c->check_struct (this) &&
c->check_array (mathValueRecords,
mathValueRecords[0].static_size,
2 * heightCount + 1) &&
sanitize_math_value_records (c));
}
inline hb_position_t get_value (hb_position_t correction_height, hb_font_t *font) const
{
const MathValueRecord* correctionHeight = mathValueRecords;
const MathValueRecord* kernValue = mathValueRecords + heightCount;
int sign = font->y_scale < 0 ? -1 : +1;
unsigned int i = 0;
unsigned int count = heightCount;
while (count > 0)
{
unsigned int half = count / 2;
hb_position_t height = correctionHeight[i + half].get_y_value(font, this);
if (sign * height < sign * correction_height)
{
i += half + 1;
count -= half + 1;
} else
count = half;
}
return kernValue[i].get_x_value(font, this);
}
protected:
USHORT heightCount;
MathValueRecord mathValueRecords[VAR];
public:
DEFINE_SIZE_ARRAY (2, mathValueRecords);
};
struct MathKernInfoRecord
{
inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
{
TRACE_SANITIZE (this);
unsigned int count = ARRAY_LENGTH (mathKern);
for (unsigned int i = 0; i < count; i++)
if (unlikely (!mathKern[i].sanitize (c, base)))
return_trace (false);
return_trace (true);
}
inline hb_position_t get_kerning (hb_ot_math_kern_t kern,
hb_position_t correction_height,
hb_font_t *font,
const void *base) const
{
unsigned int idx = kern;
if (unlikely (idx >= ARRAY_LENGTH (mathKern))) return 0;
return (base+mathKern[idx]).get_value (correction_height, font);
}
protected:
OffsetTo<MathKern> mathKern[4];
public:
DEFINE_SIZE_STATIC (8);
};
struct MathKernInfo
{
inline bool sanitize (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
return_trace (c->check_struct (this) &&
mathKernCoverage.sanitize (c, this) &&
mathKernInfoRecords.sanitize (c, this));
}
inline hb_position_t get_kerning (hb_codepoint_t glyph,
hb_ot_math_kern_t kern,
hb_position_t correction_height,
hb_font_t *font) const
{
unsigned int index = (this+mathKernCoverage).get_coverage (glyph);
return mathKernInfoRecords[index].get_kerning (kern, correction_height, font, this);
}
protected:
OffsetTo<Coverage> mathKernCoverage;
ArrayOf<MathKernInfoRecord> mathKernInfoRecords;
public:
DEFINE_SIZE_ARRAY (4, mathKernInfoRecords);
};
struct MathGlyphInfo
{
inline bool sanitize (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
return_trace (c->check_struct (this) &&
mathItalicsCorrectionInfo.sanitize (c, this) &&
mathTopAccentAttachment.sanitize (c, this) &&
extendedShapeCoverage.sanitize (c, this) &&
mathKernInfo.sanitize(c, this));
}
inline hb_position_t
get_italics_correction (hb_codepoint_t glyph, hb_font_t *font) const
{ return (this+mathItalicsCorrectionInfo).get_value (glyph, font); }
inline hb_position_t
get_top_accent_attachment (hb_codepoint_t glyph, hb_font_t *font) const
{ return (this+mathTopAccentAttachment).get_value (glyph, font); }
inline bool is_extended_shape (hb_codepoint_t glyph) const
{ return (this+extendedShapeCoverage).get_coverage (glyph) != NOT_COVERED; }
inline hb_position_t get_kerning (hb_codepoint_t glyph,
hb_ot_math_kern_t kern,
hb_position_t correction_height,
hb_font_t *font) const
{ return (this+mathKernInfo).get_kerning (glyph, kern, correction_height, font); }
protected:
OffsetTo<MathItalicsCorrectionInfo> mathItalicsCorrectionInfo;
OffsetTo<MathTopAccentAttachment> mathTopAccentAttachment;
OffsetTo<Coverage> extendedShapeCoverage;
OffsetTo<MathKernInfo> mathKernInfo;
public:
DEFINE_SIZE_STATIC (8);
};
struct MathGlyphVariantRecord
{
friend struct MathGlyphConstruction;
inline bool sanitize (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
return_trace (c->check_struct (this));
}
protected:
GlyphID variantGlyph;
USHORT advanceMeasurement;
public:
DEFINE_SIZE_STATIC (4);
};
struct PartFlags : USHORT
{
enum Flags {
Extender = 0x0001u,
Defined = 0x0001u,
};
public:
DEFINE_SIZE_STATIC (2);
};
struct MathGlyphPartRecord
{
inline bool sanitize (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
return_trace (c->check_struct (this));
}
inline void extract (hb_ot_math_glyph_part_t &out,
int scale,
hb_font_t *font) const
{
out.glyph = glyph;
out.start_connector_length = font->em_scale (startConnectorLength, scale);
out.end_connector_length = font->em_scale (endConnectorLength, scale);
out.full_advance = font->em_scale (fullAdvance, scale);
ASSERT_STATIC ((unsigned int) HB_MATH_GLYPH_PART_FLAG_EXTENDER ==
(unsigned int) PartFlags::Extender);
out.flags = (hb_ot_math_glyph_part_flags_t)
(unsigned int)
(partFlags & PartFlags::Defined);
}
protected:
GlyphID glyph;
USHORT startConnectorLength;
USHORT endConnectorLength;
USHORT fullAdvance;
PartFlags partFlags;
public:
DEFINE_SIZE_STATIC (10);
};
struct MathGlyphAssembly
{
inline bool sanitize (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
return_trace (c->check_struct (this) &&
italicsCorrection.sanitize(c, this) &&
partRecords.sanitize(c));
}
inline unsigned int get_parts (hb_direction_t direction,
hb_font_t *font,
unsigned int start_offset,
unsigned int *parts_count,
hb_ot_math_glyph_part_t *parts ,
hb_position_t *italics_correction ) const
{
if (parts_count)
{
int scale = font->dir_scale (direction);
const MathGlyphPartRecord *arr =
partRecords.sub_array (start_offset, parts_count);
unsigned int count = *parts_count;
for (unsigned int i = 0; i < count; i++)
arr[i].extract (parts[i], scale, font);
}
if (italics_correction)
*italics_correction = italicsCorrection.get_x_value (font, this);
return partRecords.len;
}
protected:
MathValueRecord italicsCorrection;
ArrayOf<MathGlyphPartRecord> partRecords;
public:
DEFINE_SIZE_ARRAY (6, partRecords);
};
struct MathGlyphConstruction
{
inline bool sanitize (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
return_trace (c->check_struct (this) &&
glyphAssembly.sanitize(c, this) &&
mathGlyphVariantRecord.sanitize(c));
}
inline const MathGlyphAssembly &get_assembly (void) const
{ return this+glyphAssembly; }
inline unsigned int get_variants (hb_direction_t direction,
hb_font_t *font,
unsigned int start_offset,
unsigned int *variants_count,
hb_ot_math_glyph_variant_t *variants ) const
{
if (variants_count)
{
int scale = font->dir_scale (direction);
const MathGlyphVariantRecord *arr =
mathGlyphVariantRecord.sub_array (start_offset, variants_count);
unsigned int count = *variants_count;
for (unsigned int i = 0; i < count; i++)
{
variants[i].glyph = arr[i].variantGlyph;
variants[i].advance = font->em_scale (arr[i].advanceMeasurement, scale);
}
}
return mathGlyphVariantRecord.len;
}
protected:
OffsetTo<MathGlyphAssembly> glyphAssembly;
ArrayOf<MathGlyphVariantRecord> mathGlyphVariantRecord;
public:
DEFINE_SIZE_ARRAY (4, mathGlyphVariantRecord);
};
struct MathVariants
{
inline bool sanitize_offsets (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
unsigned int count = vertGlyphCount + horizGlyphCount;
for (unsigned int i = 0; i < count; i++)
if (!glyphConstruction[i].sanitize (c, this)) return_trace (false);
return_trace (true);
}
inline bool sanitize (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
return_trace (c->check_struct (this) &&
vertGlyphCoverage.sanitize (c, this) &&
horizGlyphCoverage.sanitize (c, this) &&
c->check_array (glyphConstruction,
glyphConstruction[0].static_size,
vertGlyphCount + horizGlyphCount) &&
sanitize_offsets (c));
}
inline hb_position_t get_min_connector_overlap (hb_direction_t direction,
hb_font_t *font) const
{ return font->em_scale_dir (minConnectorOverlap, direction); }
inline unsigned int get_glyph_variants (hb_codepoint_t glyph,
hb_direction_t direction,
hb_font_t *font,
unsigned int start_offset,
unsigned int *variants_count,
hb_ot_math_glyph_variant_t *variants ) const
{ return get_glyph_construction (glyph, direction, font)
.get_variants (direction, font, start_offset, variants_count, variants); }
inline unsigned int get_glyph_parts (hb_codepoint_t glyph,
hb_direction_t direction,
hb_font_t *font,
unsigned int start_offset,
unsigned int *parts_count,
hb_ot_math_glyph_part_t *parts ,
hb_position_t *italics_correction ) const
{ return get_glyph_construction (glyph, direction, font)
.get_assembly ()
.get_parts (direction, font,
start_offset, parts_count, parts,
italics_correction); }
private:
inline const MathGlyphConstruction &
get_glyph_construction (hb_codepoint_t glyph,
hb_direction_t direction,
hb_font_t *font) const
{
bool vertical = HB_DIRECTION_IS_VERTICAL (direction);
unsigned int count = vertical ? vertGlyphCount : horizGlyphCount;
const OffsetTo<Coverage> &coverage = vertical ? vertGlyphCoverage
: horizGlyphCoverage;
unsigned int index = (this+coverage).get_coverage (glyph);
if (unlikely (index >= count)) return Null(MathGlyphConstruction);
if (!vertical)
index += vertGlyphCount;
return this+glyphConstruction[index];
}
protected:
USHORT minConnectorOverlap;
OffsetTo<Coverage> vertGlyphCoverage;
OffsetTo<Coverage> horizGlyphCoverage;
USHORT vertGlyphCount;
USHORT horizGlyphCount;
OffsetTo<MathGlyphConstruction> glyphConstruction[VAR];
public:
DEFINE_SIZE_ARRAY (10, glyphConstruction);
};
struct MATH
{
static const hb_tag_t tableTag = HB_OT_TAG_MATH;
inline bool sanitize (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
return_trace (version.sanitize (c) &&
likely (version.major == 1) &&
mathConstants.sanitize (c, this) &&
mathGlyphInfo.sanitize (c, this) &&
mathVariants.sanitize (c, this));
}
inline hb_position_t get_constant (hb_ot_math_constant_t constant,
hb_font_t *font) const
{ return (this+mathConstants).get_value (constant, font); }
inline const MathGlyphInfo &get_math_glyph_info (void) const
{ return this+mathGlyphInfo; }
inline const MathVariants &get_math_variants (void) const
{ return this+mathVariants; }
protected:
FixedVersion<>version;
OffsetTo<MathConstants> mathConstants;
OffsetTo<MathGlyphInfo> mathGlyphInfo;
OffsetTo<MathVariants> mathVariants;
public:
DEFINE_SIZE_STATIC (10);
};
}
#endif