use super::paragraph::Paragraph;
use super::*;
#[derive(Debug, Default, Clone, serde::Serialize)]
pub struct Footnote {
pub number: u16,
pub paragraphs: Vec<Paragraph>,
pub before_decoration_letter: u16,
pub after_decoration_letter: u16,
pub number_shape: u32,
pub instance_id: u32,
pub list_header_property: u32,
pub decoration_is_user_char: bool,
}
#[derive(Debug, Default, Clone, serde::Serialize)]
pub struct Endnote {
pub number: u16,
pub paragraphs: Vec<Paragraph>,
pub before_decoration_letter: u16,
pub after_decoration_letter: u16,
pub number_shape: u32,
pub instance_id: u32,
pub list_header_property: u32,
pub decoration_is_user_char: bool,
}
crate::storage::clone_fields!(Endnote, {number, paragraphs, before_decoration_letter, after_decoration_letter, number_shape, instance_id, list_header_property, decoration_is_user_char});
#[derive(Debug, Clone, Default, serde::Serialize)]
pub struct FootnoteShape {
pub attr: u32,
pub number_format: NumberFormat,
pub user_char: char,
pub prefix_char: char,
pub suffix_char: char,
pub start_number: u16,
pub separator_length: i32,
pub separator_margin_top: HwpUnit16,
pub separator_margin_bottom: HwpUnit16,
pub note_spacing: HwpUnit16,
pub separator_line_type: u8,
pub separator_line_width: u8,
pub separator_color: ColorRef,
pub numbering: FootnoteNumbering,
pub placement: FootnotePlacement,
pub number_code_superscript: bool,
pub print_inline_after_text: bool,
pub raw_unknown: u16,
pub deco_chars_from_source: bool,
}
crate::storage::clone_fields!(FootnoteShape, {attr, number_format, user_char, prefix_char, suffix_char, start_number, separator_length, separator_margin_top, separator_margin_bottom, note_spacing, separator_line_type, separator_line_width, separator_color, numbering, placement, number_code_superscript, print_inline_after_text, raw_unknown, deco_chars_from_source});
impl FootnoteShape {
pub fn apply_attr_fields_from_raw(&mut self) {
self.number_format = Self::number_format_from_attr_code(self.attr & 0xff);
self.placement = match (self.attr >> 8) & 0x03 {
1 => FootnotePlacement::BelowText,
2 => FootnotePlacement::RightColumn,
_ => FootnotePlacement::EachColumn,
};
self.numbering = match (self.attr >> 10) & 0x03 {
1 => FootnoteNumbering::RestartSection,
2 => FootnoteNumbering::RestartPage,
_ => FootnoteNumbering::Continue,
};
self.number_code_superscript = (self.attr & (1 << 12)) != 0;
self.print_inline_after_text = (self.attr & (1 << 13)) != 0;
}
pub fn encode_attr(&self) -> u32 {
let number_format = Self::number_format_attr_code(self.number_format);
let placement = match self.placement {
FootnotePlacement::BelowText => 1,
FootnotePlacement::RightColumn => 2,
FootnotePlacement::EachColumn => 0,
};
let numbering = match self.numbering {
FootnoteNumbering::RestartSection => 1,
FootnoteNumbering::RestartPage => 2,
FootnoteNumbering::Continue => 0,
};
let mut attr = self.attr & !0x3fff;
attr |= number_format;
attr |= (placement & 0x03) << 8;
attr |= (numbering & 0x03) << 10;
if self.number_code_superscript {
attr |= 1 << 12;
}
if self.print_inline_after_text {
attr |= 1 << 13;
}
attr
}
pub fn number_format_from_attr_code(code: u32) -> NumberFormat {
match code & 0xff {
0 => NumberFormat::Digit,
1 => NumberFormat::CircledDigit,
2 => NumberFormat::UpperRoman,
3 => NumberFormat::LowerRoman,
4 => NumberFormat::UpperAlpha,
5 => NumberFormat::LowerAlpha,
6 => NumberFormat::CircledUpperAlpha,
7 => NumberFormat::CircledLowerAlpha,
8 => NumberFormat::HangulSyllable,
9 => NumberFormat::CircledHangulSyllable,
10 => NumberFormat::HangulJamo,
11 => NumberFormat::CircledHangulJamo,
12 => NumberFormat::HangulDigit,
13 => NumberFormat::HanjaDigit,
14 => NumberFormat::CircledHanjaDigit,
15 => NumberFormat::HanjaGapEul,
16 => NumberFormat::HanjaGapEulHanja,
0x80 | 17 => NumberFormat::FourSymbol,
0x81 | 18 => NumberFormat::UserChar,
_ => NumberFormat::Digit,
}
}
pub fn number_format_attr_code(format: NumberFormat) -> u32 {
match format {
NumberFormat::Digit => 0,
NumberFormat::CircledDigit => 1,
NumberFormat::UpperRoman => 2,
NumberFormat::LowerRoman => 3,
NumberFormat::UpperAlpha => 4,
NumberFormat::LowerAlpha => 5,
NumberFormat::CircledUpperAlpha => 6,
NumberFormat::CircledLowerAlpha => 7,
NumberFormat::HangulSyllable => 8,
NumberFormat::CircledHangulSyllable => 9,
NumberFormat::HangulJamo => 10,
NumberFormat::CircledHangulJamo => 11,
NumberFormat::HangulDigit => 12,
NumberFormat::HanjaDigit => 13,
NumberFormat::CircledHanjaDigit => 14,
NumberFormat::HanjaGapEul => 15,
NumberFormat::HanjaGapEulHanja => 16,
NumberFormat::FourSymbol => 0x80,
NumberFormat::UserChar => 0x81,
}
}
pub fn number_format_from_name(value: &str, fallback: NumberFormat) -> NumberFormat {
match value {
"digit" | "DIGIT" => NumberFormat::Digit,
"circledDigit" | "CIRCLED_DIGIT" => NumberFormat::CircledDigit,
"upperRoman" | "ROMAN_CAPITAL" => NumberFormat::UpperRoman,
"lowerRoman" | "ROMAN_SMALL" => NumberFormat::LowerRoman,
"upperAlpha" | "LATIN_CAPITAL" => NumberFormat::UpperAlpha,
"lowerAlpha" | "LATIN_SMALL" => NumberFormat::LowerAlpha,
"circledUpperAlpha" | "CIRCLED_LATIN_CAPITAL" => NumberFormat::CircledUpperAlpha,
"circledLowerAlpha" | "CIRCLED_LATIN_SMALL" => NumberFormat::CircledLowerAlpha,
"hangulSyllable" | "HANGUL_SYLLABLE" => NumberFormat::HangulSyllable,
"circledHangulSyllable" | "CIRCLED_HANGUL_SYLLABLE" => {
NumberFormat::CircledHangulSyllable
}
"hangulJamo" | "HANGUL_JAMO" => NumberFormat::HangulJamo,
"circledHangulJamo" | "CIRCLED_HANGUL_JAMO" => NumberFormat::CircledHangulJamo,
"hangulDigit" | "HANGUL_PHONETIC" => NumberFormat::HangulDigit,
"hanjaDigit" | "IDEOGRAPH" => NumberFormat::HanjaDigit,
"circledHanjaDigit" | "CIRCLED_IDEOGRAPH" => NumberFormat::CircledHanjaDigit,
"hanjaGapEul" | "DECAGON_CIRCLE" => NumberFormat::HanjaGapEul,
"hanjaGapEulHanja" | "DECAGON_CIRCLE_HANJA" => NumberFormat::HanjaGapEulHanja,
"fourSymbol" | "SYMBOL" => NumberFormat::FourSymbol,
"userChar" | "USER_CHAR" => NumberFormat::UserChar,
_ => fallback,
}
}
pub fn separator_above_margin_hu(&self) -> HwpUnit16 {
let hwpx_above = self.separator_margin_top.max(0);
if hwpx_above != 0 {
hwpx_above
} else {
self.separator_margin_bottom.max(0)
}
}
pub fn separator_below_margin_hu(&self) -> HwpUnit16 {
self.note_spacing.max(0)
}
pub fn between_notes_margin_hu(&self) -> u16 {
self.raw_unknown
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, serde::Serialize)]
pub enum NumberFormat {
#[default]
Digit, CircledDigit, UpperRoman, LowerRoman, UpperAlpha, LowerAlpha, CircledUpperAlpha, CircledLowerAlpha, HangulSyllable, CircledHangulSyllable, HangulJamo, CircledHangulJamo, HangulDigit, HanjaDigit, CircledHanjaDigit, HanjaGapEul, HanjaGapEulHanja, FourSymbol, UserChar, }
crate::storage::clone_copy!(NumberFormat);
#[derive(Debug, Clone, Copy, Default, PartialEq, serde::Serialize)]
pub enum FootnoteNumbering {
#[default]
Continue,
RestartSection,
RestartPage,
}
crate::storage::clone_copy!(FootnoteNumbering);
#[derive(Debug, Clone, Copy, Default, PartialEq, serde::Serialize)]
pub enum FootnotePlacement {
#[default]
EachColumn,
BelowText,
RightColumn,
}
crate::storage::clone_copy!(FootnotePlacement);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_footnote_default() {
let note = Footnote::default();
assert_eq!(note.number, 0);
assert!(note.paragraphs.is_empty());
}
#[test]
fn test_footnote_shape_default() {
let shape = FootnoteShape::default();
assert_eq!(shape.number_format, NumberFormat::Digit);
assert_eq!(shape.numbering, FootnoteNumbering::Continue);
}
#[test]
fn test_footnote_shape_attr_bits_follow_table_134() {
let mut shape = FootnoteShape {
attr: 0x81 | (1 << 8) | (2 << 10) | (1 << 12) | (1 << 13),
..Default::default()
};
shape.apply_attr_fields_from_raw();
assert_eq!(shape.number_format, NumberFormat::UserChar);
assert_eq!(shape.placement, FootnotePlacement::BelowText);
assert_eq!(shape.numbering, FootnoteNumbering::RestartPage);
assert!(shape.number_code_superscript);
assert!(shape.print_inline_after_text);
assert_eq!(
shape.encode_attr() & 0x3fff,
0x81 | (1 << 8) | (2 << 10) | (1 << 12) | (1 << 13)
);
}
}