use std::sync::Arc;
use strict_num::NonZeroPositiveF32;
pub(crate) use svgtypes::FontFamily;
use crate::svg::usvg::layout::Span;
use crate::svg::usvg::{
Fill, Group, NonEmptyString, NonZeroRect, PaintOrder, Rect, Stroke, TextRendering, Transform,
};
const STRETCH_KEYWORDS: [(crate::fonts::FontWidth, fontdb::Stretch); 9] = [
(
crate::fonts::FontWidth::ULTRA_CONDENSED,
fontdb::Stretch::UltraCondensed,
),
(
crate::fonts::FontWidth::EXTRA_CONDENSED,
fontdb::Stretch::ExtraCondensed,
),
(
crate::fonts::FontWidth::CONDENSED,
fontdb::Stretch::Condensed,
),
(
crate::fonts::FontWidth::SEMI_CONDENSED,
fontdb::Stretch::SemiCondensed,
),
(crate::fonts::FontWidth::NORMAL, fontdb::Stretch::Normal),
(
crate::fonts::FontWidth::SEMI_EXPANDED,
fontdb::Stretch::SemiExpanded,
),
(crate::fonts::FontWidth::EXPANDED, fontdb::Stretch::Expanded),
(
crate::fonts::FontWidth::EXTRA_EXPANDED,
fontdb::Stretch::ExtraExpanded,
),
(
crate::fonts::FontWidth::ULTRA_EXPANDED,
fontdb::Stretch::UltraExpanded,
),
];
pub(crate) fn nearest_stretch(width: crate::fonts::FontWidth) -> fontdb::Stretch {
STRETCH_KEYWORDS
.iter()
.min_by(|a, b| {
(a.0.value() - width.value())
.abs()
.total_cmp(&(b.0.value() - width.value()).abs())
})
.map_or(fontdb::Stretch::Normal, |(_, stretch)| *stretch)
}
#[derive(Clone, Copy, Debug)]
pub struct FontVariation {
pub tag: [u8; 4],
pub value: f32,
}
impl FontVariation {
pub fn new(tag: [u8; 4], value: f32) -> Self {
Self { tag, value }
}
}
impl PartialEq for FontVariation {
fn eq(&self, other: &Self) -> bool {
self.tag == other.tag && self.value.to_bits() == other.value.to_bits()
}
}
impl Eq for FontVariation {}
impl std::hash::Hash for FontVariation {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.tag.hash(state);
self.value.to_bits().hash(state);
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub enum FontStyle {
Normal,
Italic,
Left,
Right,
Oblique,
}
impl Default for FontStyle {
#[inline]
fn default() -> FontStyle {
Self::Normal
}
}
impl From<fontdb::Style> for FontStyle {
fn from(style: fontdb::Style) -> Self {
match style {
fontdb::Style::Normal => FontStyle::Normal,
fontdb::Style::Italic => FontStyle::Italic,
fontdb::Style::Oblique => FontStyle::Oblique,
}
}
}
impl From<FontStyle> for fontdb::Style {
fn from(style: FontStyle) -> Self {
match style {
FontStyle::Normal => fontdb::Style::Normal,
FontStyle::Italic | FontStyle::Left | FontStyle::Right => fontdb::Style::Italic,
FontStyle::Oblique => fontdb::Style::Oblique,
}
}
}
#[derive(Clone, Debug)]
pub struct Font {
pub(crate) families: Vec<FontFamily>,
pub(crate) default_family: bool,
pub(crate) style: FontStyle,
pub(crate) slant: crate::fonts::FontSlant,
pub(crate) width: crate::fonts::FontWidth,
pub(crate) weight: crate::fonts::FontWeight,
pub(crate) variations: Vec<FontVariation>,
pub(crate) explicit_variations: Vec<FontVariation>,
pub(crate) synthesis: crate::fonts::FontSynthesis,
}
impl PartialEq for Font {
fn eq(&self, other: &Self) -> bool {
self.families == other.families
&& self.default_family == other.default_family
&& self.style == other.style
&& self.numbers() == other.numbers()
&& self.variations == other.variations
&& self.explicit_variations == other.explicit_variations
&& self.synthesis == other.synthesis
}
}
impl Eq for Font {}
impl std::hash::Hash for Font {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.families.hash(state);
self.default_family.hash(state);
self.style.hash(state);
self.numbers().hash(state);
self.variations.hash(state);
self.explicit_variations.hash(state);
self.synthesis.hash(state);
}
}
impl Font {
pub fn families(&self) -> &[FontFamily] {
&self.families
}
pub fn uses_default_family(&self) -> bool {
self.default_family
}
pub fn style(&self) -> FontStyle {
self.style
}
pub fn slant(&self) -> crate::fonts::FontSlant {
self.slant
}
pub fn width(&self) -> crate::fonts::FontWidth {
self.width
}
pub fn weight(&self) -> crate::fonts::FontWeight {
self.weight
}
fn numbers(&self) -> [u32; 3] {
[self.slant.value(), self.width.value(), self.weight.value()].map(f32::to_bits)
}
pub fn variations(&self) -> &[FontVariation] {
&self.variations
}
pub fn explicit_variations(&self) -> &[FontVariation] {
&self.explicit_variations
}
pub(crate) fn synthesis(&self) -> crate::fonts::FontSynthesis {
self.synthesis
}
}
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum DominantBaseline {
Auto,
UseScript,
NoChange,
ResetSize,
Ideographic,
Alphabetic,
Hanging,
Mathematical,
Central,
Middle,
TextAfterEdge,
TextBeforeEdge,
}
impl Default for DominantBaseline {
fn default() -> Self {
Self::Auto
}
}
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum AlignmentBaseline {
Auto,
Baseline,
BeforeEdge,
TextBeforeEdge,
Middle,
Central,
AfterEdge,
TextAfterEdge,
Ideographic,
Alphabetic,
Hanging,
Mathematical,
}
impl Default for AlignmentBaseline {
fn default() -> Self {
Self::Auto
}
}
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum BaselineShift {
Baseline,
Subscript,
Superscript,
Number(f32),
}
impl Default for BaselineShift {
#[inline]
fn default() -> BaselineShift {
BaselineShift::Baseline
}
}
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum LengthAdjust {
Spacing,
SpacingAndGlyphs,
}
impl Default for LengthAdjust {
fn default() -> Self {
Self::Spacing
}
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum FontOpticalSizing {
Auto,
None,
}
impl Default for FontOpticalSizing {
fn default() -> Self {
Self::Auto
}
}
#[derive(Clone, Debug)]
pub struct TextDecorationStyle {
pub(crate) fill: Option<Fill>,
pub(crate) stroke: Option<Stroke>,
pub(crate) declared: crate::style::TextDecoration,
pub(crate) style: Arc<crate::style::Style>,
pub(crate) element: usize,
}
impl TextDecorationStyle {
pub fn fill(&self) -> Option<&Fill> {
self.fill.as_ref()
}
pub fn stroke(&self) -> Option<&Stroke> {
self.stroke.as_ref()
}
}
#[derive(Clone, Debug)]
pub struct TextDecoration {
pub(crate) underline: Option<TextDecorationStyle>,
pub(crate) overline: Option<TextDecorationStyle>,
pub(crate) line_through: Option<TextDecorationStyle>,
}
impl TextDecoration {
pub fn underline(&self) -> Option<&TextDecorationStyle> {
self.underline.as_ref()
}
pub fn overline(&self) -> Option<&TextDecorationStyle> {
self.overline.as_ref()
}
pub fn line_through(&self) -> Option<&TextDecorationStyle> {
self.line_through.as_ref()
}
}
#[derive(Clone, Debug)]
pub(crate) struct Ancestor {
pub(crate) element: usize,
pub(crate) style: Arc<crate::style::Style>,
pub(crate) font: Font,
pub(crate) font_size: f32,
}
#[derive(Clone, Debug)]
pub struct TextSpan {
pub(crate) start: usize,
pub(crate) end: usize,
pub(crate) fill: Option<Fill>,
pub(crate) stroke: Option<Stroke>,
pub(crate) paint_order: PaintOrder,
pub(crate) font: Font,
pub(crate) font_size: NonZeroPositiveF32,
pub(crate) style: Arc<crate::style::Style>,
pub(crate) ancestry: Arc<[Arc<Ancestor>]>,
pub(crate) single_character: bool,
pub(crate) linear_light: bool,
pub(crate) text_orientation: crate::style::logical::TextOrientation,
pub(crate) font_optical_sizing: FontOpticalSizing,
pub(crate) decoration: TextDecoration,
pub(crate) dominant_baseline: DominantBaseline,
pub(crate) alignment_baseline: AlignmentBaseline,
pub(crate) parent_alignment: f32,
pub(crate) baseline_shift: Vec<BaselineShift>,
pub(crate) line_edge: Option<LineEdge>,
pub(crate) visible: bool,
pub(crate) letter_spacing: f32,
pub(crate) word_spacing: f32,
pub(crate) text_length: Option<f32>,
pub(crate) length_adjust: LengthAdjust,
}
impl TextSpan {
pub fn start(&self) -> usize {
self.start
}
pub fn end(&self) -> usize {
self.end
}
pub fn fill(&self) -> Option<&Fill> {
self.fill.as_ref()
}
pub fn stroke(&self) -> Option<&Stroke> {
self.stroke.as_ref()
}
pub fn paint_order(&self) -> PaintOrder {
self.paint_order
}
pub fn font(&self) -> &Font {
&self.font
}
pub fn font_size(&self) -> NonZeroPositiveF32 {
self.font_size
}
pub fn font_optical_sizing(&self) -> FontOpticalSizing {
self.font_optical_sizing
}
pub fn decoration(&self) -> &TextDecoration {
&self.decoration
}
pub fn dominant_baseline(&self) -> DominantBaseline {
self.dominant_baseline
}
pub fn alignment_baseline(&self) -> AlignmentBaseline {
self.alignment_baseline
}
pub fn baseline_shift(&self) -> &[BaselineShift] {
&self.baseline_shift
}
pub fn is_visible(&self) -> bool {
self.visible
}
pub fn letter_spacing(&self) -> f32 {
self.letter_spacing
}
pub fn word_spacing(&self) -> f32 {
self.word_spacing
}
pub fn text_length(&self) -> Option<f32> {
self.text_length
}
pub fn length_adjust(&self) -> LengthAdjust {
self.length_adjust
}
}
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum TextAnchor {
Start,
Middle,
End,
}
impl Default for TextAnchor {
fn default() -> Self {
Self::Start
}
}
#[derive(Debug)]
pub struct TextPath {
pub(crate) id: NonEmptyString,
pub(crate) start_offset: f32,
pub(crate) path: Arc<tiny_skia_path::Path>,
}
impl TextPath {
pub fn id(&self) -> &str {
self.id.get()
}
pub fn start_offset(&self) -> f32 {
self.start_offset
}
pub fn path(&self) -> &tiny_skia_path::Path {
&self.path
}
}
#[derive(Clone, Debug)]
pub enum TextFlow {
Linear,
Path(Arc<TextPath>),
}
#[derive(Clone, Debug)]
pub struct TextChunk {
pub(crate) x: Option<f32>,
pub(crate) y: Option<f32>,
pub(crate) anchor: TextAnchor,
pub(crate) rtl: bool,
pub(crate) spans: Vec<TextSpan>,
pub(crate) text_flow: TextFlow,
pub(crate) text: String,
pub(crate) line_breaks: usize,
pub(crate) inline_offset: f32,
pub(crate) hanging: [f32; 2],
pub(crate) soft_wrapped: bool,
pub(crate) overflowing: bool,
}
impl TextChunk {
pub fn x(&self) -> Option<f32> {
self.x
}
pub fn y(&self) -> Option<f32> {
self.y
}
pub fn anchor(&self) -> TextAnchor {
self.anchor
}
pub fn spans(&self) -> &[TextSpan] {
&self.spans
}
pub fn text_flow(&self) -> TextFlow {
self.text_flow.clone()
}
pub fn text(&self) -> &str {
&self.text
}
pub(crate) fn holding(&self, text: String, spans: Vec<TextSpan>) -> Self {
crate::work::count(crate::work::Work::HeldSpan, spans.len());
Self {
x: self.x,
y: self.y,
anchor: self.anchor,
rtl: self.rtl,
spans,
text_flow: self.text_flow.clone(),
text,
line_breaks: self.line_breaks,
inline_offset: self.inline_offset,
hanging: self.hanging,
soft_wrapped: self.soft_wrapped,
overflowing: self.overflowing,
}
}
}
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum WritingMode {
LeftToRight,
TopToBottom,
}
#[derive(Clone, Debug)]
pub struct Text {
pub(crate) id: String,
pub(crate) rendering_mode: TextRendering,
pub(crate) dx: Vec<f32>,
pub(crate) dy: Vec<f32>,
pub(crate) rotate: Vec<f32>,
pub(crate) writing_mode: WritingMode,
pub(crate) chunks: Vec<TextChunk>,
pub(crate) abs_transform: Transform,
pub(crate) bounding_box: Rect,
pub(crate) abs_bounding_box: Rect,
pub(crate) stroke_bounding_box: Rect,
pub(crate) abs_stroke_bounding_box: Rect,
pub(crate) flattened: Box<Group>,
pub(crate) layouted: Vec<Span>,
pub(crate) non_scaling_stroke: bool,
pub(crate) linear_light: bool,
pub(crate) strut: Strut,
pub(crate) first_line: Option<FirstLine>,
pub(crate) wrapping: Option<Wrapping>,
pub(crate) clip: Option<NonZeroRect>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum WrapWidth {
Length(f32),
MinContent,
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct Wrapping {
pub(crate) width: WrapWidth,
pub(crate) overflow: [crate::style::TextOverflow; 2],
}
#[derive(Clone, Debug)]
pub(crate) struct LineEdge {
pub(crate) bottom: bool,
pub(crate) root: usize,
pub(crate) strut: Strut,
}
#[derive(Clone, Debug)]
pub(crate) struct FirstLine {
pub(crate) chunks: Vec<TextChunk>,
pub(crate) strut: Strut,
}
#[derive(Clone, Debug)]
pub(crate) struct Strut {
pub(crate) font: Font,
pub(crate) font_size: f32,
pub(crate) style: Arc<crate::style::Style>,
}
impl Text {
pub(crate) fn line_strut(&self, line: usize) -> &Strut {
match &self.first_line {
Some(first) if line == 0 => &first.strut,
_ => &self.strut,
}
}
pub fn id(&self) -> &str {
&self.id
}
pub fn rendering_mode(&self) -> TextRendering {
self.rendering_mode
}
pub fn dx(&self) -> &[f32] {
&self.dx
}
pub fn dy(&self) -> &[f32] {
&self.dy
}
pub fn rotate(&self) -> &[f32] {
&self.rotate
}
pub fn writing_mode(&self) -> WritingMode {
self.writing_mode
}
pub fn chunks(&self) -> &[TextChunk] {
&self.chunks
}
pub fn abs_transform(&self) -> Transform {
self.abs_transform
}
pub fn bounding_box(&self) -> Rect {
self.bounding_box
}
pub fn abs_bounding_box(&self) -> Rect {
self.abs_bounding_box
}
pub fn stroke_bounding_box(&self) -> Rect {
self.stroke_bounding_box
}
pub fn abs_stroke_bounding_box(&self) -> Rect {
self.abs_stroke_bounding_box
}
pub fn flattened(&self) -> &Group {
&self.flattened
}
pub fn non_scaling_stroke(&self) -> bool {
self.non_scaling_stroke
}
pub fn layouted(&self) -> &[Span] {
&self.layouted
}
pub(crate) fn subroots(&self, f: &mut dyn FnMut(&Group)) {
f(&self.flattened);
}
}