use std::num::NonZeroU32;
use f_office_units::Length;
use f_office_units::scale::LINE;
use f_office_xml::NodeId;
use f_office_xml::enumeration::{Values, another, own};
use super::border::read_borders;
use super::{
Borders, Color, FrameMark, FramePr, NamedSides, RunProps, SetIn, overlay, read_shading,
value_of,
};
use crate::ns::W;
use crate::numbers;
use crate::on_off::{self, off_also_for_none};
use crate::params;
use crate::reader::{PartReader, val};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Alignment {
Start,
Center,
End,
Both,
ThaiDistribute,
}
impl Alignment {
#[must_use]
pub const fn starts_at_indent(self) -> bool {
matches!(self, Self::Start | Self::Both | Self::ThaiDistribute)
}
#[must_use]
pub const fn justified(self) -> bool {
matches!(self, Self::Both | Self::ThaiDistribute)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Kashidas {
None,
Low,
Medium,
High,
}
pub(super) const JC: &Values<Option<(Alignment, Kashidas)>> = &Values::new(&[
own("left", Some((Alignment::Start, Kashidas::None))),
own("start", Some((Alignment::Start, Kashidas::None))),
own("center", Some((Alignment::Center, Kashidas::None))),
own("right", Some((Alignment::End, Kashidas::None))),
own("end", Some((Alignment::End, Kashidas::None))),
own("both", Some((Alignment::Both, Kashidas::None))),
another("distribute", Some((Alignment::Both, Kashidas::None))),
another("mediumKashida", Some((Alignment::Both, Kashidas::Medium))),
another("highKashida", Some((Alignment::Both, Kashidas::High))),
another("lowKashida", Some((Alignment::Both, Kashidas::Low))),
own(
"thaiDistribute",
Some((Alignment::ThaiDistribute, Kashidas::None)),
),
another("numTab", None),
]);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TextAlignment {
Auto,
Baseline,
Top,
Center,
Bottom,
}
impl TextAlignment {
#[must_use]
pub const fn off_baseline(self) -> bool {
matches!(self, Self::Top | Self::Center | Self::Bottom)
}
}
pub(super) const TEXT_ALIGNMENT: &Values<TextAlignment> = &Values::new(&[
own("auto", TextAlignment::Auto),
own("baseline", TextAlignment::Baseline),
own("top", TextAlignment::Top),
own("center", TextAlignment::Center),
own("bottom", TextAlignment::Bottom),
]);
pub(super) const TIGHT_WRAP: &Values<bool> = &Values::new(&[
own("none", false),
own("allLines", true),
own("firstAndLastLine", true),
own("firstLineOnly", true),
own("lastLineOnly", true),
]);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LineSpacing {
Auto(i64),
Exact(Length),
AtLeast(Length),
}
impl LineSpacing {
pub const SINGLE: Self = Self::Auto(LINE);
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TabKind {
Left,
Center,
Right,
Decimal,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum TabSetting {
Stop(TabKind),
Clear,
Bar,
}
pub(super) const TAB_KIND: &Values<TabSetting> = &Values::new(&[
own("left", TabSetting::Stop(TabKind::Left)),
own("start", TabSetting::Stop(TabKind::Left)),
own("center", TabSetting::Stop(TabKind::Center)),
own("right", TabSetting::Stop(TabKind::Right)),
own("end", TabSetting::Stop(TabKind::Right)),
own("decimal", TabSetting::Stop(TabKind::Decimal)),
own("clear", TabSetting::Clear),
own("num", TabSetting::Stop(TabKind::Left)),
another("bar", TabSetting::Bar),
]);
pub(super) const TAB_LEADER: &Values<Option<char>> = &Values::new(&[
own("none", None),
own("dot", Some('.')),
own("hyphen", Some('-')),
own("underscore", Some('_')),
another("heavy", Some('_')),
own("middleDot", Some('\u{b7}')),
]);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct TabStop {
pub position: Length,
pub kind: TabKind,
pub leader: Option<char>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TabMark {
Stop(TabStop),
Clear(Length),
}
impl TabMark {
#[must_use]
pub const fn position(&self) -> Length {
match self {
Self::Stop(stop) => stop.position,
Self::Clear(position) => *position,
}
}
#[must_use]
pub const fn stop(&self) -> Option<TabStop> {
match self {
Self::Stop(stop) => Some(*stop),
Self::Clear(_) => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[expect(
clippy::struct_excessive_bools,
reason = "each field is one on and off element of the markup"
)]
pub(crate) struct ParaFlags {
pub(crate) contextual_spacing: bool,
pub(crate) bidi: bool,
pub(crate) snap_to_grid: bool,
pub(crate) kinsoku: bool,
pub(crate) word_wrap: bool,
pub(crate) overflow_punct: bool,
pub(crate) widow_control: bool,
pub(crate) keep_next: bool,
pub(crate) auto_space_latin: bool,
pub(crate) auto_space_digits: bool,
pub(crate) keep_lines: bool,
pub(crate) suppress_line_numbers: bool,
pub(crate) suppress_auto_hyphens: bool,
pub(crate) page_break_before: bool,
pub(crate) section_end: bool,
pub(crate) continuous_end: bool,
pub(crate) continuous_break_end: bool,
pub(crate) before_auto: bool,
pub(crate) after_auto: bool,
pub(crate) mirror_indents: bool,
pub(crate) adjust_right_indent: bool,
pub(crate) suppress_overlap: bool,
pub(crate) tight_wrap: bool,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ParaProps {
pub style: Option<String>,
pub alignment: Option<Alignment>,
pub kashidas: Option<Kashidas>,
pub text_alignment: Option<SetIn<TextAlignment>>,
pub text_direction: Option<SetIn<bool>>,
pub before: Option<Length>,
pub after: Option<Length>,
pub line: Option<LineSpacing>,
pub indent_left: Option<Length>,
pub indent_right: Option<Length>,
pub indent_first: Option<Length>,
pub contextual_spacing: Option<bool>,
pub bidi: Option<bool>,
pub snap_to_grid: Option<bool>,
pub kinsoku: Option<bool>,
pub word_wrap: Option<bool>,
pub overflow_punct: Option<SetIn<bool>>,
pub widow_control: Option<bool>,
pub keep_next: Option<bool>,
pub auto_space_latin: Option<bool>,
pub auto_space_digits: Option<bool>,
pub keep_lines: Option<bool>,
pub suppress_line_numbers: Option<bool>,
pub suppress_auto_hyphens: Option<bool>,
pub page_break_before: Option<bool>,
pub section_end: Option<bool>,
pub continuous_end: Option<bool>,
pub(crate) continuous_break_end: Option<bool>,
pub before_auto: Option<bool>,
pub after_auto: Option<bool>,
pub html_auto_spacing: Option<HtmlAutoSpacing>,
pub mirror_indents: Option<SetIn<bool>>,
pub adjust_right_indent: Option<SetIn<bool>>,
pub suppress_overlap: Option<SetIn<bool>>,
pub tight_wrap: Option<SetIn<bool>>,
pub div: Option<SetIn<i64>>,
pub outline_level: Option<u8>,
pub tabs: Vec<TabMark>,
pub borders: Option<Box<Borders>>,
pub named_sides: Option<Box<NamedSides>>,
pub shading: Option<Color>,
pub numbering: Option<ListRef>,
pub numbering_level: Option<ListLevel>,
pub frame: Option<Box<FramePr>>,
pub mark: RunProps,
pub mark_style: Option<Box<str>>,
pub mark_change: Option<MarkChange>,
pub original: Option<Box<ParaProps>>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MarkChange {
Inserted,
Deleted,
}
impl ParaProps {
#[must_use]
pub fn borders(&self) -> Option<Borders> {
self.borders.as_deref().copied()
}
pub fn take_border_sides(&mut self) {
self.borders = self
.named_sides
.as_deref()
.map(|sides| Box::new(sides.borders()));
}
pub(crate) fn plain_cell_frame(&mut self) {
if let Some(frame) = self.frame.as_deref_mut().filter(|frame| !frame.drop_cap) {
frame.mark = FrameMark::Plain;
}
}
pub fn drop_none_borders(&mut self) {
if let Some(borders) = self.borders.as_deref_mut() {
*borders = borders.without_none();
}
}
pub fn cut_border_widths(&mut self) {
if let Some(borders) = self.borders.as_deref_mut() {
*borders = borders.in_whole_twips();
}
}
pub fn draw_three_d_borders_as_lines(&mut self) {
if let Some(borders) = self.borders.as_deref_mut() {
*borders = borders.as_one_line();
}
}
pub fn drop_between_border(&mut self) -> bool {
self.borders
.as_deref_mut()
.and_then(|borders| borders.between.take())
.is_some()
}
pub fn drop_borders(&mut self) {
self.borders = None;
}
#[must_use]
pub fn side_indents(&self) -> (Length, Length, bool) {
let start = self.indent_left.unwrap_or(Length::ZERO);
let end = self.indent_right.unwrap_or(Length::ZERO);
if self.flags().bidi {
(end, start, true)
} else {
(start, end, false)
}
}
pub fn apply(&mut self, other: &Self) {
let Self {
style,
alignment,
kashidas,
text_alignment,
text_direction,
before,
after,
line,
indent_left,
indent_right,
indent_first,
contextual_spacing,
bidi,
snap_to_grid,
kinsoku,
word_wrap,
overflow_punct,
widow_control,
keep_next,
auto_space_latin,
auto_space_digits,
keep_lines,
suppress_line_numbers,
suppress_auto_hyphens,
page_break_before,
section_end,
continuous_end,
continuous_break_end,
before_auto,
after_auto,
html_auto_spacing,
mirror_indents,
adjust_right_indent,
suppress_overlap,
tight_wrap,
div,
outline_level,
tabs,
borders,
named_sides,
shading,
numbering,
numbering_level,
frame,
mark,
mark_style,
mark_change,
original: _,
} = other;
overlay(&mut self.style, style.as_ref());
overlay(&mut self.alignment, alignment.as_ref());
overlay(&mut self.kashidas, kashidas.as_ref());
overlay(&mut self.text_alignment, text_alignment.as_ref());
overlay(&mut self.text_direction, text_direction.as_ref());
overlay(&mut self.before, before.as_ref());
overlay(&mut self.after, after.as_ref());
overlay(&mut self.line, line.as_ref());
self.apply_indents(other);
let _ = (indent_left, indent_right, indent_first);
self.apply_flag_fields(other);
let _ = (
contextual_spacing,
snap_to_grid,
kinsoku,
word_wrap,
overflow_punct,
bidi,
widow_control,
keep_next,
auto_space_latin,
auto_space_digits,
keep_lines,
suppress_line_numbers,
suppress_auto_hyphens,
page_break_before,
section_end,
continuous_end,
continuous_break_end,
before_auto,
after_auto,
html_auto_spacing,
mirror_indents,
adjust_right_indent,
suppress_overlap,
tight_wrap,
);
overlay(&mut self.div, div.as_ref());
overlay(&mut self.outline_level, outline_level.as_ref());
overlay(&mut self.borders, borders.as_ref());
NamedSides::overlay(&mut self.named_sides, named_sides.as_deref());
overlay(&mut self.shading, shading.as_ref());
overlay(&mut self.numbering, numbering.as_ref());
overlay(&mut self.numbering_level, numbering_level.as_ref());
FramePr::overlay(&mut self.frame, frame.as_deref());
self.apply_tabs(tabs);
self.mark.apply(mark);
overlay(&mut self.mark_style, mark_style.as_ref());
overlay(&mut self.mark_change, mark_change.as_ref());
}
fn apply_indents(&mut self, other: &Self) {
if other.numbering == Some(ListRef::Removed) {
self.indent_left = None;
self.indent_first = None;
}
overlay(&mut self.indent_left, other.indent_left.as_ref());
overlay(&mut self.indent_right, other.indent_right.as_ref());
overlay(&mut self.indent_first, other.indent_first.as_ref());
}
fn apply_flag_fields(&mut self, other: &Self) {
overlay(
&mut self.contextual_spacing,
other.contextual_spacing.as_ref(),
);
overlay(&mut self.snap_to_grid, other.snap_to_grid.as_ref());
overlay(&mut self.kinsoku, other.kinsoku.as_ref());
overlay(&mut self.word_wrap, other.word_wrap.as_ref());
overlay(&mut self.overflow_punct, other.overflow_punct.as_ref());
overlay(&mut self.bidi, other.bidi.as_ref());
overlay(&mut self.widow_control, other.widow_control.as_ref());
overlay(&mut self.keep_next, other.keep_next.as_ref());
overlay(&mut self.auto_space_latin, other.auto_space_latin.as_ref());
overlay(
&mut self.auto_space_digits,
other.auto_space_digits.as_ref(),
);
overlay(&mut self.keep_lines, other.keep_lines.as_ref());
overlay(
&mut self.suppress_line_numbers,
other.suppress_line_numbers.as_ref(),
);
overlay(
&mut self.suppress_auto_hyphens,
other.suppress_auto_hyphens.as_ref(),
);
overlay(
&mut self.page_break_before,
other.page_break_before.as_ref(),
);
overlay(&mut self.section_end, other.section_end.as_ref());
overlay(&mut self.continuous_end, other.continuous_end.as_ref());
overlay(
&mut self.continuous_break_end,
other.continuous_break_end.as_ref(),
);
overlay(&mut self.before_auto, other.before_auto.as_ref());
overlay(&mut self.after_auto, other.after_auto.as_ref());
overlay(
&mut self.html_auto_spacing,
other.html_auto_spacing.as_ref(),
);
overlay(&mut self.mirror_indents, other.mirror_indents.as_ref());
overlay(
&mut self.adjust_right_indent,
other.adjust_right_indent.as_ref(),
);
overlay(&mut self.suppress_overlap, other.suppress_overlap.as_ref());
overlay(&mut self.tight_wrap, other.tight_wrap.as_ref());
}
fn apply_tabs(&mut self, tabs: &[TabMark]) {
for mark in tabs {
self.tabs.retain(|t| t.position() != mark.position());
if let TabMark::Stop(_) = mark {
self.tabs.push(*mark);
}
}
self.tabs.sort_by_key(TabMark::position);
}
pub fn tab_stops(&self) -> impl Iterator<Item = TabStop> + '_ {
self.tabs.iter().filter_map(TabMark::stop)
}
#[must_use]
pub fn space_before(&self) -> Length {
let automatic = self.html_auto_spacing.unwrap_or_default().automatic();
auto_or(self.flags().before_auto, self.before, automatic)
}
#[must_use]
pub fn space_after(&self) -> Length {
let automatic = self.html_auto_spacing.unwrap_or_default().automatic();
auto_or(self.flags().after_auto, self.after, automatic)
}
#[must_use]
pub(crate) fn flags(&self) -> ParaFlags {
let on = |value: Option<bool>| value.unwrap_or(true);
let off = |value: Option<bool>| value.unwrap_or(false);
ParaFlags {
contextual_spacing: off(self.contextual_spacing),
bidi: off(self.bidi),
snap_to_grid: on(self.snap_to_grid),
kinsoku: on(self.kinsoku),
word_wrap: on(self.word_wrap),
overflow_punct: on(value_of(self.overflow_punct.as_ref())),
widow_control: on(self.widow_control),
keep_next: off(self.keep_next),
auto_space_latin: on(self.auto_space_latin),
auto_space_digits: on(self.auto_space_digits),
keep_lines: off(self.keep_lines),
suppress_line_numbers: off(self.suppress_line_numbers),
suppress_auto_hyphens: off(self.suppress_auto_hyphens),
page_break_before: off(self.page_break_before),
section_end: off(self.section_end),
continuous_end: off(self.continuous_end),
continuous_break_end: off(self.continuous_break_end),
before_auto: off(self.before_auto),
after_auto: off(self.after_auto),
mirror_indents: off(value_of(self.mirror_indents.as_ref())),
adjust_right_indent: on(value_of(self.adjust_right_indent.as_ref())),
suppress_overlap: off(value_of(self.suppress_overlap.as_ref())),
tight_wrap: off(value_of(self.tight_wrap.as_ref())),
}
}
pub(crate) fn read(reader: &mut PartReader, ppr: NodeId) -> Self {
let mut props = Self::default();
props.read_into(reader, ppr);
props
}
pub(crate) fn read_into(&mut self, reader: &mut PartReader, ppr: NodeId) {
for child in reader.doc.child_elements(ppr) {
self.read_child(reader, child);
}
}
fn read_alignment(&mut self, reader: &mut PartReader, child: NodeId) {
let jc = reader.enumeration(child, "val", JC).flatten();
self.alignment = jc.map(|(alignment, _)| alignment);
self.kashidas = jc.map(|(_, kashidas)| kashidas);
}
fn read_child(&mut self, reader: &mut PartReader, child: NodeId) {
let doc = reader.doc;
let Some(element) = doc.element(child) else {
return;
};
match element.name.local {
"pStyle" => self.style = val(doc, child).map(str::to_owned),
"jc" => self.read_alignment(reader, child),
"textAlignment" => {
self.text_alignment = reader
.enumeration(child, "val", TEXT_ALIGNMENT)
.map(|alignment| SetIn::read(alignment, reader));
}
"textDirection" => {
self.text_direction = reader
.or_missing(child, "val", crate::table::turns_text)
.map(|turned| SetIn::read(turned, reader));
}
"spacing" => self.read_spacing(reader, child),
"ind" => self.read_indent(reader, child),
"tabs" => self.tabs = read_tabs(reader, child),
"pBdr" => {
let (borders, sides) = read_borders(reader, child);
self.borders = Some(Box::new(borders));
self.named_sides = Some(Box::new(sides));
}
"shd" => self.shading = read_shading(reader, child),
"numPr" => {
let (list, level) = read_numbering(reader, child);
self.numbering = list.or(self.numbering.take());
self.numbering_level = level.or(self.numbering_level);
}
"framePr" => self.frame = Some(Box::new(FramePr::read(reader, child))),
"pPrChange" => {
self.original = doc
.child(child, W, "pPr")
.map(|ppr| Box::new(Self::read(reader, ppr)));
}
"contextualSpacing" => self.contextual_spacing = Some(off_also_for_none(doc, child)),
"snapToGrid" => self.snap_to_grid = Some(off_also_for_none(doc, child)),
"kinsoku" => self.kinsoku = Some(off_also_for_none(doc, child)),
"wordWrap" => self.word_wrap = Some(off_also_for_none(doc, child)),
"overflowPunct" => {
self.overflow_punct = Some(SetIn::read(off_also_for_none(doc, child), reader));
}
"bidi" => self.bidi = Some(off_also_for_none(doc, child)),
"widowControl" => self.widow_control = Some(off_also_for_none(doc, child)),
"keepNext" => self.keep_next = Some(off_also_for_none(doc, child)),
"autoSpaceDE" => self.auto_space_latin = Some(off_also_for_none(doc, child)),
"autoSpaceDN" => self.auto_space_digits = Some(off_also_for_none(doc, child)),
"keepLines" => self.keep_lines = Some(off_also_for_none(doc, child)),
"suppressLineNumbers" => {
self.suppress_line_numbers = Some(off_also_for_none(doc, child));
}
"suppressAutoHyphens" => {
self.suppress_auto_hyphens = Some(off_also_for_none(doc, child));
}
"pageBreakBefore" => self.page_break_before = Some(off_also_for_none(doc, child)),
"mirrorIndents" => {
self.mirror_indents = Some(SetIn::read(off_also_for_none(doc, child), reader));
}
"adjustRightInd" => {
self.adjust_right_indent = Some(SetIn::read(off_also_for_none(doc, child), reader));
}
"suppressOverlap" => {
self.suppress_overlap = Some(SetIn::read(off_also_for_none(doc, child), reader));
}
"textboxTightWrap" => {
self.tight_wrap = reader
.enumeration(child, "val", TIGHT_WRAP)
.map(|wraps| SetIn::read(wraps, reader));
}
"divId" => {
self.div = reader
.number(child, "val", numbers::ANY)
.map(|div| SetIn::read(div, reader));
}
"outlineLvl" => {
self.outline_level =
reader.number(child, "val", 0..=i64::from(params::BODY_TEXT_OUTLINE_LEVEL));
}
"rPr" => {
self.mark = RunProps::read(reader, child);
self.mark_style = doc
.child(child, W, "rStyle")
.and_then(|s| doc.attribute(s, Some(W), "val"))
.map(Box::from);
let changed =
|names: [&str; 2]| names.iter().any(|n| doc.child(child, W, n).is_some());
self.mark_change = if changed(["del", "moveFrom"]) {
Some(MarkChange::Deleted)
} else if changed(["ins", "moveTo"]) {
Some(MarkChange::Inserted)
} else {
None
};
}
_ => {}
}
}
fn read_spacing(&mut self, reader: &mut PartReader, node: NodeId) {
let doc = reader.doc;
if let Some(before) = reader.twips(node, "before") {
self.before = Some(before);
}
if let Some(after) = reader.twips(node, "after") {
self.after = Some(after);
}
let flag = |name| {
doc.attribute(node, Some(W), name)
.map(|v| on_off::read(v) != Some(false))
};
if let Some(auto) = flag("beforeAutospacing") {
self.before_auto = Some(auto);
}
if let Some(auto) = flag("afterAutospacing") {
self.after_auto = Some(auto);
}
let rule = reader
.enumeration(node, "lineRule", LINE_RULE)
.unwrap_or(LineRule::Auto);
if let Some(line) = reader.line(node, "line") {
self.line = match rule {
LineRule::Exact => Length::from_twips(line.abs()).map(LineSpacing::Exact),
LineRule::AtLeast => Length::from_twips(line).map(LineSpacing::AtLeast),
LineRule::Auto => Some(LineSpacing::Auto(reader.bounded_line_multiple(line))),
};
}
}
fn read_indent(&mut self, reader: &mut PartReader, node: NodeId) {
let left = reader
.twips(node, "left")
.or_else(|| reader.twips(node, "start"));
let right = reader
.twips(node, "right")
.or_else(|| reader.twips(node, "end"));
if left.is_some() {
self.indent_left = left;
}
if right.is_some() {
self.indent_right = right;
}
if let Some(first) = reader.twips(node, "firstLine") {
self.indent_first = Some(first);
}
if let Some(hanging) = reader.twips(node, "hanging") {
self.indent_first = Length::ZERO.checked_sub(hanging);
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ListRef {
Removed,
Id(NonZeroU32),
}
impl ListRef {
#[must_use]
pub fn of(id: u32) -> Self {
NonZeroU32::new(id).map_or(Self::Removed, Self::Id)
}
#[must_use]
pub const fn id(self) -> Option<NonZeroU32> {
match self {
Self::Removed => None,
Self::Id(id) => Some(id),
}
}
}
fn read_numbering(reader: &mut PartReader, node: NodeId) -> (Option<ListRef>, Option<ListLevel>) {
let doc = reader.doc;
let list = doc
.child(node, W, "numId")
.and_then(|n| reader.number(n, "val", numbers::NOT_NEGATIVE))
.map(ListRef::of);
let ilvl = doc
.child(node, W, "ilvl")
.and_then(|n| read_list_level(reader, n));
(list, ilvl)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ListLevel {
At(usize),
Unnumbered,
}
impl ListLevel {
pub const FIRST: Self = Self::At(0);
#[must_use]
pub const fn index(self) -> Option<usize> {
match self {
Self::At(index) => Some(index),
Self::Unnumbered => None,
}
}
}
fn read_list_level(reader: &mut PartReader, node: NodeId) -> Option<ListLevel> {
reader.list_levels().read(reader, node)
}
fn read_tabs(reader: &mut PartReader, node: NodeId) -> Vec<TabMark> {
let doc = reader.doc;
let mut tabs: Vec<TabMark> = doc
.child_elements(node)
.filter(|tab| doc.is(*tab, W, "tab"))
.filter_map(|tab| {
let position = reader.twips(tab, "pos")?;
let setting = reader
.enumeration(tab, "val", TAB_KIND)
.unwrap_or(TabSetting::Stop(TabKind::Left));
let leader = reader.enumeration(tab, "leader", TAB_LEADER).flatten();
match setting {
TabSetting::Stop(kind) => Some(TabMark::Stop(TabStop {
position,
kind,
leader,
})),
TabSetting::Clear => Some(TabMark::Clear(position)),
TabSetting::Bar => None,
}
})
.collect();
tabs.sort_by_key(TabMark::position);
tabs
}
fn auto_or(auto: bool, given: Option<Length>, automatic: Length) -> Length {
if auto {
automatic
} else {
given.unwrap_or(Length::ZERO)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum HtmlAutoSpacing {
#[default]
Html,
Fixed,
}
impl HtmlAutoSpacing {
#[must_use]
pub const fn of(set: bool) -> Self {
if set { Self::Fixed } else { Self::Html }
}
#[must_use]
pub const fn automatic(self) -> Length {
Length::from_units(
match self {
Self::Html => params::AUTOMATIC_SPACING,
Self::Fixed => params::FIXED_AUTOMATIC_SPACING,
} * Length::UNITS_PER_TWIP,
)
}
#[must_use]
pub fn space_between(self, after: Length, before: Length) -> Length {
match self {
Self::Html => after.max(before),
Self::Fixed => after + before,
}
}
#[must_use]
pub const fn drops_automatic_spacing(self) -> bool {
matches!(self, Self::Html)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum LineRule {
Auto,
Exact,
AtLeast,
}
pub(super) const LINE_RULE: &Values<LineRule> = &Values::new(&[
own("auto", LineRule::Auto),
own("exact", LineRule::Exact),
own("atLeast", LineRule::AtLeast),
]);