use f_office_fonts::FontId;
use f_office_paint::{Opacity, Outline, Rgb};
use f_office_units::Length;
use super::SingleLine;
use crate::FaceMetrics;
use crate::props::{BidiLanguageClass, Strike, Underline};
use f_office_units::div_round;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Face {
pub font: FontId,
pub metrics: FaceMetrics,
pub size: Length,
pub line_size: Length,
pub single_line: SingleLine,
pub color: Rgb,
pub lines: RunLines,
pub background: Option<Rgb>,
pub rise: Length,
pub raised: Length,
pub math: MathFace,
pub outline: Option<Outline>,
pub text_fill: DrawnTextFill,
pub effect: bool,
pub shadow_room: ShadowRoom,
pub shadow: Option<DrawnShadow>,
pub kerned: bool,
pub ligatures: bool,
pub scale: Option<i64>,
pub standing: Standing,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum DrawnTextFill {
#[default]
Solid,
None,
Translucent(Opacity),
Gradient(usize),
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct ShadowRoom {
pub above: Length,
pub below: Length,
}
impl ShadowRoom {
#[must_use]
pub fn is_none(self) -> bool {
self == Self::default()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ShadowForm {
Paths,
Picture {
blur: Length,
opacity: f_office_paint::Opacity,
outline_width: Length,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DrawnShadow {
pub across: Length,
pub down: Length,
pub color: Rgb,
pub form: ShadowForm,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct MathFace {
pub in_fraction: bool,
pub large: bool,
pub script_level: Option<f_office_fonts::ScriptLevel>,
pub room: MathRoom,
pub phantom: bool,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum MathRoom {
#[default]
Lines,
InkBelow,
Ink {
leading: Length,
},
LinesAndInk {
leading: Length,
},
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Standing {
pub synthetic_bold: bool,
pub synthetic_italic: bool,
pub font: bool,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct RunLines {
pub under: Underline,
pub through: Strike,
pub change_bar: bool,
}
impl Face {
#[must_use]
pub fn scale(&self, font_units: i64) -> Length {
self.scale_to(font_units, self.size)
}
fn scale_to(&self, font_units: i64, size: Length) -> Length {
let em = i64::from(self.metrics.units_per_em.max(1));
Length::from_units(div_round(font_units * size.units(), em))
}
#[must_use]
pub fn line_height(&self) -> Length {
self.scale_to(
self.own_line() + 2 * self.east_asian_extra(),
self.line_size,
)
}
#[must_use]
pub fn descent(&self) -> Length {
let descent = i64::from(self.metrics.descent) + self.east_asian_extra();
self.scale_to(descent, self.line_size)
}
fn own_line(&self) -> i64 {
self.single_line.of_metrics(&self.metrics)
}
fn east_asian_extra(&self) -> i64 {
self.single_line.east_asian_room(&self.metrics)
}
#[must_use]
pub fn win_line(&self) -> Length {
self.scale(i64::from(self.metrics.script_top) + i64::from(self.metrics.descent))
}
#[must_use]
pub fn win_top(&self) -> Length {
self.scale(i64::from(self.metrics.script_top))
}
#[must_use]
pub fn whole_line(&self) -> Option<(i64, i64)> {
if self.metrics.units_per_em != crate::params::WHOLE_HEIGHT_UNITS_PER_EM
|| self.raised != Length::ZERO
|| !self.shadow_room.is_none()
{
return None;
}
let per_part = i64::from(self.metrics.units_per_em) * Length::UNITS_PER_POINT;
let parts = |font_units: i64| {
let scaled =
font_units * self.line_size.units() * crate::params::HEIGHT_PARTS_PER_POINT;
(scaled % per_part == 0).then_some(scaled / per_part)
};
let line = parts(self.own_line() + 2 * self.east_asian_extra())?;
let descent = parts(i64::from(self.metrics.descent) + self.east_asian_extra())?;
Some((line - descent, descent))
}
#[must_use]
pub fn ascent(&self) -> Length {
self.line_height() - self.descent()
}
#[must_use]
pub fn raised_ascent(&self) -> Length {
self.shifted_ascent() + self.shadow_room.above
}
#[must_use]
pub fn raised_descent(&self) -> Length {
self.shifted_descent() + self.shadow_room.below
}
#[must_use]
pub fn shifted_ascent(&self) -> Length {
self.ascent() + self.raised
}
#[must_use]
pub fn shifted_descent(&self) -> Length {
self.descent() - self.raised
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum RunDirection {
#[default]
LeftToRight,
RightToLeft(BidiLanguageClass),
}
impl RunDirection {
#[must_use]
pub fn of(rtl: bool, class: Option<BidiLanguageClass>) -> Self {
if rtl {
Self::RightToLeft(class.unwrap_or_default())
} else {
Self::LeftToRight
}
}
#[must_use]
pub fn with_class(self, class: Option<BidiLanguageClass>) -> Self {
match self {
Self::LeftToRight => self,
Self::RightToLeft(_) => Self::RightToLeft(class.unwrap_or_default()),
}
}
#[must_use]
pub fn class(self) -> Option<BidiLanguageClass> {
match self {
Self::LeftToRight => None,
Self::RightToLeft(class) => Some(class),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum FontSide {
EastAsian,
Other,
#[default]
Open,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct FaceIndex(pub(super) usize);
#[cfg(test)]
impl FaceIndex {
pub(crate) const fn nth(n: usize) -> Self {
Self(n)
}
}
#[cfg(test)]
impl Glyph {
pub(crate) fn plain(face: FaceIndex, ch: char) -> Self {
Self {
face,
ch,
id: 1,
advance: Length::from_units(10),
offset: (Length::ZERO, Length::ZERO),
spacing: Length::ZERO,
kerning: Length::ZERO,
counted: Length::ZERO,
written: None,
part: false,
label: false,
space: SpaceKind::Free,
starts_run: false,
left_out_before: LeftOutBefore::Nothing,
direction: RunDirection::LeftToRight,
side: FontSide::Open,
place: TextPlace::default(),
sizing: LineSizing::ByFace,
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct Faces<'s>(pub(super) &'s [Face]);
impl<'s> Faces<'s> {
#[must_use]
pub fn get(self, index: FaceIndex) -> Option<&'s Face> {
self.0.get(index.0)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SpaceKind {
#[default]
Free,
Equation,
Operator,
NoBreak,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum LeftOutBefore {
#[default]
Nothing,
FormatCharacter,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum LineSizing {
#[default]
ByFace,
Nothing,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Glyph {
pub face: FaceIndex,
pub ch: char,
pub id: u32,
pub advance: Length,
pub offset: (Length, Length),
pub spacing: Length,
pub kerning: Length,
pub counted: Length,
pub written: Option<u16>,
pub part: bool,
pub label: bool,
pub space: SpaceKind,
pub starts_run: bool,
pub left_out_before: LeftOutBefore,
pub direction: RunDirection,
pub side: FontSide,
pub place: TextPlace,
pub sizing: LineSizing,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct TextPlace {
pub in_run: u32,
pub in_paragraph: u32,
pub starts_buffer: bool,
pub run: Option<u32>,
}
impl TextPlace {
#[must_use]
pub fn starts_piece(self, from: u32, length: usize) -> bool {
let count = self.in_run.min(self.in_paragraph.saturating_sub(from));
count > 0 && usize::try_from(count).is_ok_and(|c| c % length.max(1) == 0)
}
}