use std::ops::{Add, Sub};
use std::rc::Rc;
use std::sync::Arc;
pub type GlyphCode = u32;
#[derive(Debug, Default, PartialEq, Eq, Clone, Copy, Hash)]
pub struct Vector<T> {
pub x: T,
pub y: T,
}
impl Add for Vector<i32> {
type Output = Vector<i32>;
fn add(self, rhs: Vector<i32>) -> Vector<i32> {
Vector {
x: self.x + rhs.x,
y: self.y + rhs.y,
}
}
}
impl Sub for Vector<i32> {
type Output = Vector<i32>;
fn sub(self, rhs: Vector<i32>) -> Vector<i32> {
Vector {
x: self.x - rhs.x,
y: self.y - rhs.y,
}
}
}
#[derive(Debug, Default, PartialEq, Eq, Clone, Copy, Hash)]
pub struct Extents<T> {
pub left_side_bearing: T,
pub width: T,
pub ascent: T,
pub descent: T,
}
impl Extents<i32> {
pub fn new(left_side_bearing: i32, width: i32, ascent: i32, descent: i32) -> Self {
Extents {
left_side_bearing,
width,
ascent,
descent,
}
}
pub fn height(&self) -> i32 {
self.ascent + self.descent
}
}
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[repr(C)]
pub enum MathConstant {
ScriptPercentScaleDown = 0,
ScriptScriptPercentScaleDown,
DelimitedSubFormulaMinHeight,
DisplayOperatorMinHeight,
MathLeading,
AxisHeight,
AccentBaseHeight,
FlattenedAccentBaseHeight,
SubscriptShiftDown,
SubscriptTopMax,
SubscriptBaselineDropMin,
SuperscriptShiftUp,
SuperscriptShiftUpCramped,
SuperscriptBottomMin,
SuperscriptBaselineDropMax,
SubSuperscriptGapMin,
SuperscriptBottomMaxWithSubscript,
SpaceAfterScript,
UpperLimitGapMin,
UpperLimitBaselineRiseMin,
LowerLimitGapMin,
LowerLimitBaselineDropMin,
StackTopShiftUp,
StackTopDisplayStyleShiftUp,
StackBottomShiftDown,
StackBottomDisplayStyleShiftDown,
StackGapMin,
StackDisplayStyleGapMin,
StretchStackTopShiftUp,
StretchStackBottomShiftDown,
StretchStackGapAboveMin,
StretchStackGapBelowMin,
FractionNumeratorShiftUp,
FractionNumeratorDisplayStyleShiftUp,
FractionDenominatorShiftDown,
FractionDenominatorDisplayStyleShiftDown,
FractionNumeratorGapMin,
FractionNumDisplayStyleGapMin,
FractionRuleThickness,
FractionDenominatorGapMin,
FractionDenomDisplayStyleGapMin,
SkewedFractionHorizontalGap,
SkewedFractionVerticalGap,
OverbarVerticalGap,
OverbarRuleThickness,
OverbarExtraAscender,
UnderbarVerticalGap,
UnderbarRuleThickness,
UnderbarExtraDescender,
RadicalVerticalGap,
RadicalDisplayStyleVerticalGap,
RadicalRuleThickness,
RadicalExtraAscender,
RadicalKernBeforeDegree,
RadicalKernAfterDegree,
RadicalDegreeBottomRaisePercent,
}
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct MathGlyph {
pub glyph_code: u32,
pub cluster: u32,
pub offset: Vector<i32>,
pub advance_width: i32,
pub extents: Extents<i32>,
pub italic_correction: i32,
pub top_accent_attachment: i32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum CornerPosition {
TopLeft = 1,
TopRight = 0,
BottomLeft = 3,
BottomRight = 2,
}
impl CornerPosition {
pub fn is_left(self) -> bool {
use CornerPosition::*;
match self {
TopLeft | BottomLeft => true,
_ => false,
}
}
pub fn is_top(self) -> bool {
use CornerPosition::*;
match self {
TopLeft | TopRight => true,
_ => false,
}
}
pub fn horizontal_mirror(self) -> Self {
use CornerPosition::*;
match self {
TopLeft => TopRight,
TopRight => TopLeft,
BottomLeft => BottomRight,
BottomRight => BottomLeft,
}
}
pub fn vertical_mirror(self) -> Self {
use CornerPosition::*;
match self {
TopLeft => BottomLeft,
TopRight => BottomRight,
BottomLeft => TopLeft,
BottomRight => TopRight,
}
}
pub fn diagonal_mirror(self) -> Self {
use CornerPosition::*;
match self {
TopLeft => BottomRight,
TopRight => BottomLeft,
BottomLeft => TopRight,
BottomRight => TopLeft,
}
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ShapeOptions {
pub script_level: u8,
pub flat_accent: bool,
}
pub trait MathFont {
fn units_per_em(&self) -> u32;
fn constant(&self, c: MathConstant) -> i32;
fn shape(&self, text: &str, options: ShapeOptions) -> Vec<MathGlyph>;
fn glyph(&self, glyph: GlyphCode, options: ShapeOptions) -> MathGlyph;
fn is_stretchable(&self, glyph: GlyphCode, horizontal: bool) -> bool;
fn stretch(
&self,
glyph: GlyphCode,
horizontal: bool,
target_size: i32,
options: ShapeOptions,
) -> Vec<(Vector<i32>, MathGlyph)>;
fn math_kerning(&self, glyph: GlyphCode, corner: CornerPosition, correction_height: i32)
-> i32;
}
macro_rules! forward_math_font {
($($ptr:ty),*) => {$(
impl<F: MathFont + ?Sized> MathFont for $ptr {
fn units_per_em(&self) -> u32 {
(**self).units_per_em()
}
fn constant(&self, c: MathConstant) -> i32 {
(**self).constant(c)
}
fn shape(&self, text: &str, options: ShapeOptions) -> Vec<MathGlyph> {
(**self).shape(text, options)
}
fn glyph(&self, glyph: GlyphCode, options: ShapeOptions) -> MathGlyph {
(**self).glyph(glyph, options)
}
fn is_stretchable(&self, glyph: GlyphCode, horizontal: bool) -> bool {
(**self).is_stretchable(glyph, horizontal)
}
fn stretch(
&self,
glyph: GlyphCode,
horizontal: bool,
target_size: i32,
options: ShapeOptions,
) -> Vec<(Vector<i32>, MathGlyph)> {
(**self).stretch(glyph, horizontal, target_size, options)
}
fn math_kerning(
&self,
glyph: GlyphCode,
corner: CornerPosition,
correction_height: i32,
) -> i32 {
(**self).math_kerning(glyph, corner, correction_height)
}
}
)*};
}
forward_math_font!(&F, Rc<F>, Arc<F>, Box<F>);