use serde_derive::{Deserialize, Serialize};
use crate::graphics::Rect;
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PageAnnotation {
pub name: String,
pub page: usize,
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LinkAnnotation {
pub rect: Rect,
pub actions: Actions,
#[serde(default)]
pub border: BorderArray,
#[serde(default)]
pub color: ColorArray,
#[serde(default)]
pub highlighting: HighlightingMode,
}
impl LinkAnnotation {
pub fn new(
rect: Rect,
actions: Actions,
border: Option<BorderArray>,
color: Option<ColorArray>,
highlighting: Option<HighlightingMode>,
) -> Self {
Self {
rect,
border: border.unwrap_or_default(),
color: color.unwrap_or_default(),
actions,
highlighting: highlighting.unwrap_or_default(),
}
}
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", tag = "type", content = "data")]
pub enum BorderArray {
Solid([f32; 3]),
Dashed([f32; 3], DashPhase),
}
impl BorderArray {
pub fn to_array(&self) -> Vec<f32> {
match self {
BorderArray::Solid(s) => s.to_vec(),
BorderArray::Dashed(s, dash_phase) => {
let mut s = s.to_vec();
s.push(dash_phase.phase);
s
}
}
}
}
impl Default for BorderArray {
fn default() -> Self {
BorderArray::Solid([0.0, 0.0, 1.0])
}
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DashPhase {
pub dash_array: Vec<f32>,
pub phase: f32,
}
#[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", tag = "type", content = "data")]
pub enum ColorArray {
Transparent,
Gray([f32; 1]),
Rgb([f32; 3]),
Cmyk([f32; 4]),
}
impl Default for ColorArray {
fn default() -> Self {
ColorArray::Rgb([0.0, 1.0, 1.0])
}
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[non_exhaustive]
#[serde(rename_all = "kebab-case", tag = "type", content = "data")]
pub enum Destination {
Xyz {
page: usize,
left: Option<f32>,
top: Option<f32>,
zoom: Option<f32>,
},
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", tag = "type", content = "data")]
pub enum Actions {
Goto(Destination),
Uri(String),
}
impl Actions {
pub fn get_action_type_id(&self) -> &'static str {
match self {
Actions::Goto(_) => "GoTo",
Actions::Uri(_) => "URI",
}
}
pub fn go_to(destination: Destination) -> Self {
Self::Goto(destination)
}
pub fn uri(uri: String) -> Self {
Self::Uri(uri)
}
}
#[derive(Debug, PartialEq, Clone, Copy, Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum HighlightingMode {
None,
#[default]
Invert,
Outline,
Push,
}
impl HighlightingMode {
pub fn get_id(&self) -> &'static str {
use self::HighlightingMode::*;
match self {
None => "N",
Invert => "I",
Outline => "O",
Push => "P",
}
}
}