use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use laravel::config::{Env, Section};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Format {
#[default]
Png,
Jpg,
Webp,
}
impl Format {
pub fn extension(self) -> &'static str {
match self {
Self::Png => "png",
Self::Jpg => "jpg",
Self::Webp => "webp",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Anchor {
#[default]
Top,
Bottom,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Align {
#[default]
Left,
Center,
Right,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Highlight {
pub padding_x: i32,
pub padding_y: i32,
pub color: String,
}
impl Default for Highlight {
fn default() -> Self {
Self {
padding_x: 10,
padding_y: 6,
color: "#000000".to_owned(),
}
}
}
impl Highlight {
pub fn padded(padding_x: i32, padding_y: i32) -> Self {
Self {
padding_x,
padding_y,
..Self::default()
}
}
#[must_use]
pub fn color(mut self, color: impl Into<String>) -> Self {
self.color = color.into();
self
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct FitRule {
pub font: String,
pub x: i32,
pub max_width: i32,
pub max_lines: usize,
pub sizes: Vec<i32>,
pub line_height: f64,
pub anchor: Anchor,
pub baseline: Option<i32>,
pub color: String,
pub align: Align,
pub highlight: Option<Highlight>,
}
impl Default for FitRule {
fn default() -> Self {
Self {
font: "default".to_owned(),
x: 80,
max_width: 1040,
max_lines: 3,
sizes: vec![72, 64, 56, 48],
line_height: 1.17,
anchor: Anchor::Top,
baseline: None,
color: "#ffffff".to_owned(),
align: Align::Left,
highlight: None,
}
}
}
impl FitRule {
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn font(mut self, font: impl Into<String>) -> Self {
self.font = font.into();
self
}
#[must_use]
pub fn x(mut self, x: i32) -> Self {
self.x = x;
self
}
#[must_use]
pub fn max_width(mut self, max_width: i32) -> Self {
self.max_width = max_width;
self
}
#[must_use]
pub fn max_lines(mut self, max_lines: usize) -> Self {
self.max_lines = max_lines;
self
}
#[must_use]
pub fn sizes(mut self, sizes: impl IntoIterator<Item = i32>) -> Self {
self.sizes = sizes.into_iter().collect();
self
}
#[must_use]
pub fn line_height(mut self, line_height: f64) -> Self {
self.line_height = line_height;
self
}
#[must_use]
pub fn anchor(mut self, anchor: Anchor) -> Self {
self.anchor = anchor;
self
}
#[must_use]
pub fn baseline(mut self, baseline: i32) -> Self {
self.baseline = Some(baseline);
self
}
#[must_use]
pub fn color(mut self, color: impl Into<String>) -> Self {
self.color = color.into();
self
}
#[must_use]
pub fn align(mut self, align: Align) -> Self {
self.align = align;
self
}
#[must_use]
pub fn highlight(mut self, highlight: Highlight) -> Self {
self.highlight = Some(highlight);
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Stack {
pub fields: Vec<String>,
pub center_y: i32,
pub gap: i32,
}
impl Stack {
pub fn new<S: Into<String>>(fields: impl IntoIterator<Item = S>) -> Self {
Self {
fields: fields.into_iter().map(Into::into).collect(),
center_y: 315,
gap: 24,
}
}
#[must_use]
pub fn center_y(mut self, center_y: i32) -> Self {
self.center_y = center_y;
self
}
#[must_use]
pub fn gap(mut self, gap: i32) -> Self {
self.gap = gap;
self
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct TemplateConfig {
pub file: String,
pub width: Option<u32>,
pub height: Option<u32>,
pub format: Option<Format>,
pub fit: Vec<(String, FitRule)>,
pub stack: Option<Stack>,
}
impl TemplateConfig {
pub fn new(file: impl Into<String>) -> Self {
Self {
file: file.into(),
width: None,
height: None,
format: None,
fit: Vec::new(),
stack: None,
}
}
#[must_use]
pub fn width(mut self, width: u32) -> Self {
self.width = Some(width);
self
}
#[must_use]
pub fn height(mut self, height: u32) -> Self {
self.height = Some(height);
self
}
#[must_use]
pub fn format(mut self, format: Format) -> Self {
self.format = Some(format);
self
}
#[must_use]
pub fn fit(mut self, field: impl Into<String>, rule: FitRule) -> Self {
let field = field.into();
match self.fit.iter_mut().find(|(name, _)| *name == field) {
Some(existing) => existing.1 = rule,
None => self.fit.push((field, rule)),
}
self
}
#[must_use]
pub fn stack(mut self, stack: Stack) -> Self {
self.stack = Some(stack);
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Resvg {
pub font_files: Vec<String>,
pub system_fonts: bool,
}
impl Default for Resvg {
fn default() -> Self {
Self {
font_files: Vec::new(),
system_fonts: true,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Renderers {
pub resvg: Resvg,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Paths {
pub templates: String,
pub output: String,
pub manifest: String,
}
impl Default for Paths {
fn default() -> Self {
Self {
templates: "resources/cards".to_owned(),
output: "public/img/cards".to_owned(),
manifest: "storage/app/laracards/manifest.json".to_owned(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Unsplash {
pub access_key: Option<String>,
pub cache: String,
}
impl Default for Unsplash {
fn default() -> Self {
Self {
access_key: None,
cache: "storage/app/laracards/backgrounds".to_owned(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Backgrounds {
pub unsplash: Unsplash,
}
fn default_fonts() -> BTreeMap<String, String> {
BTreeMap::from([("default".to_owned(), "public/fonts/Lato-Bold.ttf".to_owned())])
}
fn default_templates() -> BTreeMap<String, TemplateConfig> {
BTreeMap::from([(
"post".to_owned(),
TemplateConfig::new("post.svg").fit("title", FitRule::new().sizes([82, 72, 64, 56, 48]).baseline(320)),
)])
}
fn backgrounds_from_env(env: &Env) -> Option<Backgrounds> {
let access_key = env.get::<String>("UNSPLASH_ACCESS_KEY")?;
Some(Backgrounds {
unsplash: Unsplash {
access_key: Some(access_key),
..Unsplash::default()
},
})
}
#[derive(Debug, Clone, Section)]
pub struct LaracardsConfig {
pub renderers: Renderers,
#[section(default = 1200)]
pub width: u32,
#[section(default = 630)]
pub height: u32,
pub format: Format,
#[section(default = 85)]
pub quality: u8,
pub paths: Paths,
#[section(default = default_fonts())]
pub fonts: BTreeMap<String, String>,
#[env(with = backgrounds_from_env)]
pub backgrounds: Backgrounds,
#[section(default = default_templates())]
pub templates: BTreeMap<String, TemplateConfig>,
}
impl LaracardsConfig {
pub fn resolve(base: &Path, path: &str) -> PathBuf {
let path = Path::new(path);
if path.is_absolute() {
path.to_path_buf()
} else {
base.join(path)
}
}
pub fn current() -> Self {
laravel::config().section::<Self>().clone()
}
pub fn base() -> PathBuf {
laravel::config().app.base_path.clone()
}
}