use std::{
cell::{Cell, Ref, RefCell},
collections::HashMap,
};
use ranim_core::{
Extract,
color::{AlphaColor, Srgb},
core_item::CoreItem,
glam::{DAffine3, DMat3, DVec3},
traits::{
Aabb, Discard, FillColor, Locate, PointsFunc, RotateTransform, ScaleTransform,
ShiftTransform, StrokeColor, StrokeWidth, With,
},
};
use typst::foundations::Repr;
use crate::vitem::{
VItem,
geometry::{Parallelogram, anchor::Origin},
svg::SvgItem,
typst::typst_svg,
};
pub use typst::text::{FontStretch, FontStyle, FontVariant, FontWeight};
#[derive(Clone, Debug)]
pub struct TextFont {
families: Vec<String>,
variant: FontVariant,
features: HashMap<String, u32>,
}
impl TextFont {
pub fn new(families: impl IntoIterator<Item = impl Into<String>>) -> Self {
Self {
families: families.into_iter().map(|v| v.into()).collect(),
variant: Default::default(),
features: Default::default(),
}
}
pub fn with_weight(mut self, weight: FontWeight) -> Self {
self.variant.weight = weight;
self
}
pub fn with_style(mut self, style: FontStyle) -> Self {
self.variant.style = style;
self
}
pub fn with_stretch(mut self, stretch: FontStretch) -> Self {
self.variant.stretch = stretch;
self
}
pub fn with_features(
mut self,
features: impl IntoIterator<Item = (impl Into<String>, u32)>,
) -> Self {
self.features
.extend(features.into_iter().map(|(k, v)| (k.into(), v)));
self
}
}
impl Default for TextFont {
fn default() -> Self {
Self::new(["New Computer Modern", "Libertinus Serif"])
}
}
#[derive(Clone, Debug)]
pub struct TextItem {
origin: DVec3,
basis: (DVec3, DVec3),
text: String,
font: TextFont,
fill_rgbas: AlphaColor<Srgb>,
stroke_rgbas: AlphaColor<Srgb>,
stroke_width: f32,
items: RefCell<Option<Vec<VItem>>>,
inline_length_em: Cell<Option<f64>>,
}
impl Locate<TextItem> for Origin {
fn locate(&self, target: &TextItem) -> DVec3 {
target.origin
}
}
impl TextItem {
pub fn new(text: impl Into<String>, em_size: f64) -> Self {
Self {
origin: DVec3::ZERO,
basis: (DVec3::X * em_size, DVec3::Y * em_size),
text: text.into(),
font: TextFont::default(),
fill_rgbas: AlphaColor::WHITE,
stroke_rgbas: AlphaColor::WHITE,
stroke_width: 0.0,
items: RefCell::default(),
inline_length_em: Cell::default(),
}
}
pub fn with_font(mut self, font: TextFont) -> Self {
self.font = font;
self.items.take();
self
}
pub fn font(&self) -> &TextFont {
&self.font
}
pub fn basis(&self) -> (DVec3, DVec3) {
self.basis
}
pub fn text(&self) -> &str {
&self.text
}
pub fn inline_length_em(&self) -> f64 {
let _ = self.items(); self.inline_length_em.get().unwrap()
}
pub fn text_box(&self) -> Parallelogram {
let (u, v) = self.basis;
Parallelogram::new(self.origin, (u * self.inline_length_em(), v))
}
fn generate_items(&self) -> Vec<VItem> {
let font = &self.font;
let text = self.text.as_str();
let mut families = String::new();
for family in font.families.iter() {
families.push('"');
families.push_str(family);
families.push_str("\", ");
}
let weight = font.variant.weight.to_number();
let style = {
use FontStyle::*;
match font.variant.style {
Normal => "normal",
Italic => "italic",
Oblique => "oblique",
}
};
let stretch = font.variant.stretch.to_ratio().repr();
let features = if font.features.is_empty() {
":".to_string()
} else {
let mut features = String::new();
for (tag, value) in font.features.iter() {
features.push('"');
features.push_str(tag);
features.push_str("\": ");
features.push_str(value.to_string().as_str());
features.push_str(", ");
}
features
};
let svg_src = typst_svg(
format!(
r#"#set text(
top-edge: 1em,
font: ({families}),
weight: {weight},
style: "{style}",
stretch: {stretch},
features: ({features}),
)
#set page(
width: auto,
height: auto,
margin: 0pt,
background: rect(width: 100%, height: 100%),
)
{text}
"#
)
.as_str(),
);
let mut items = Vec::<VItem>::from(SvgItem::new(svg_src));
let baseline_em_box = items[0].aabb();
let texts = items.split_off(1);
let &Self {
basis: (u, v),
origin,
fill_rgbas,
stroke_rgbas,
stroke_width,
..
} = self;
let [min, max] = baseline_em_box;
let h = max.y - min.y;
self.inline_length_em.set(Some((max.x - min.x) / h));
let mat = DAffine3::from_mat3_translation(DMat3::from_cols(u, v, DVec3::ZERO), origin);
texts.with(|x| {
x.shift(-min)
.scale(DVec3::splat(1. / h)) .apply_point_func(|p| *p = mat.transform_point3(*p))
.set_fill_color(fill_rgbas)
.set_stroke_color(stroke_rgbas)
.set_stroke_width(stroke_width)
.discard()
})
}
fn items(&self) -> Ref<'_, Vec<VItem>> {
if self.items.borrow().is_none() {
let items = self.generate_items();
self.items.replace(Some(items));
}
Ref::map(self.items.borrow(), |v| v.as_ref().unwrap())
}
fn transform_items(&self, transformation: impl FnOnce(&mut Vec<VItem>)) {
if let Some(v) = self.items.borrow_mut().as_mut() {
transformation(v);
}
}
}
impl Aabb for TextItem {
fn aabb(&self) -> [DVec3; 2] {
self.items().aabb()
}
}
impl ShiftTransform for TextItem {
fn shift(&mut self, offset: DVec3) -> &mut Self {
self.origin += offset;
self.transform_items(|item| item.shift(offset).discard());
self
}
}
impl RotateTransform for TextItem {
fn rotate_on_axis(&mut self, axis: DVec3, angle: f64) -> &mut Self {
self.origin.rotate_on_axis(axis, angle);
self.basis.0.rotate_on_axis(axis, angle);
self.basis.1.rotate_on_axis(axis, angle);
self.transform_items(|item| item.rotate_on_axis(axis, angle).discard());
self
}
}
impl ScaleTransform for TextItem {
fn scale(&mut self, scale: DVec3) -> &mut Self {
self.origin.scale(scale).discard();
self.basis.0 *= scale;
self.basis.1 *= scale;
self.transform_items(|item| item.scale(scale).discard());
self
}
}
impl FillColor for TextItem {
fn fill_color(&self) -> AlphaColor<Srgb> {
self.fill_rgbas
}
fn set_fill_color(&mut self, color: AlphaColor<Srgb>) -> &mut Self {
self.fill_rgbas = color;
self.transform_items(|item| item.set_fill_color(color).discard());
self
}
fn set_fill_opacity(&mut self, opacity: f32) -> &mut Self {
self.fill_rgbas = self.fill_rgbas.with_alpha(opacity);
self.transform_items(|item| item.set_fill_opacity(opacity).discard());
self
}
}
impl StrokeColor for TextItem {
fn stroke_color(&self) -> AlphaColor<Srgb> {
self.stroke_rgbas
}
fn set_stroke_color(&mut self, color: AlphaColor<Srgb>) -> &mut Self {
self.stroke_rgbas = color;
self.transform_items(|item| item.set_stroke_color(color).discard());
self
}
fn set_stroke_opacity(&mut self, opacity: f32) -> &mut Self {
self.stroke_rgbas = self.stroke_rgbas.with_alpha(opacity);
self.transform_items(|item| item.set_stroke_opacity(opacity).discard());
self
}
}
impl From<TextItem> for Vec<VItem> {
fn from(item: TextItem) -> Self {
item.items().clone()
}
}
impl Extract for TextItem {
type Target = CoreItem;
fn extract_into(&self, buf: &mut Vec<Self::Target>) {
self.items().extract_into(buf);
}
}
#[cfg(test)]
mod tests {
use assert_float_eq::assert_float_absolute_eq;
use super::*;
#[test]
fn test_text_item() {
let item = TextItem::new("Hello, world!", 0.25);
assert_float_absolute_eq!(item.basis.0.length(), 0.25, 1e-10);
assert_float_absolute_eq!(item.origin.distance(DVec3::ZERO), 0.0, 1e-10);
}
#[test]
fn test_font() {
let font = TextFont::new(["Arial", "Helvetica"])
.with_weight(FontWeight::BOLD)
.with_style(FontStyle::Italic)
.with_stretch(FontStretch::CONDENSED)
.with_features([("liga", 1), ("dlig", 1)]);
dbg!(&font);
}
}