1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use std::rc::Rc;
use strict_num::NonZeroPositiveF32;
use crate::{Fill, Group, Paint, PaintOrder, Stroke, TextRendering, Visibility};
use tiny_skia_path::{NonZeroRect, Transform};
/// A font stretch property.
#[allow(missing_docs)]
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
pub enum FontStretch {
UltraCondensed,
ExtraCondensed,
Condensed,
SemiCondensed,
Normal,
SemiExpanded,
Expanded,
ExtraExpanded,
UltraExpanded,
}
impl Default for FontStretch {
#[inline]
fn default() -> Self {
Self::Normal
}
}
/// A font style property.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub enum FontStyle {
/// A face that is neither italic not obliqued.
Normal,
/// A form that is generally cursive in nature.
Italic,
/// A typically-sloped version of the regular face.
Oblique,
}
impl Default for FontStyle {
#[inline]
fn default() -> FontStyle {
Self::Normal
}
}
/// Text font properties.
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub struct Font {
/// A list of family names.
///
/// Never empty. Uses `usvg_parser::Options::font_family` as fallback.
pub families: Vec<String>,
/// A font style.
pub style: FontStyle,
/// A font stretch.
pub stretch: FontStretch,
/// A font width.
pub weight: u16,
}
/// A dominant baseline property.
#[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
}
}
/// An alignment baseline property.
#[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
}
}
/// A baseline shift property.
#[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
}
}
/// A length adjust property.
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum LengthAdjust {
Spacing,
SpacingAndGlyphs,
}
impl Default for LengthAdjust {
fn default() -> Self {
Self::Spacing
}
}
/// A text span decoration style.
///
/// In SVG, text decoration and text it's applied to can have different styles.
/// So you can have black text and green underline.
///
/// Also, in SVG you can specify text decoration stroking.
#[derive(Clone, Debug)]
pub struct TextDecorationStyle {
/// A fill style.
pub fill: Option<Fill>,
/// A stroke style.
pub stroke: Option<Stroke>,
}
/// A text span decoration.
#[derive(Clone, Debug)]
pub struct TextDecoration {
/// An optional underline and its style.
pub underline: Option<TextDecorationStyle>,
/// An optional overline and its style.
pub overline: Option<TextDecorationStyle>,
/// An optional line-through and its style.
pub line_through: Option<TextDecorationStyle>,
}
/// A text style span.
///
/// Spans do not overlap inside a text chunk.
#[derive(Clone, Debug)]
pub struct TextSpan {
/// A span start in bytes.
///
/// Offset is relative to the parent text chunk and not the parent text element.
pub start: usize,
/// A span end in bytes.
///
/// Offset is relative to the parent text chunk and not the parent text element.
pub end: usize,
/// A fill style.
pub fill: Option<Fill>,
/// A stroke style.
pub stroke: Option<Stroke>,
/// A paint order style.
pub paint_order: PaintOrder,
/// A font.
pub font: Font,
/// A font size.
pub font_size: NonZeroPositiveF32,
/// Indicates that small caps should be used.
///
/// Set by `font-variant="small-caps"`
pub small_caps: bool,
/// Indicates that a kerning should be applied.
///
/// Supports both `kerning` and `font-kerning` properties.
pub apply_kerning: bool,
/// A span decorations.
pub decoration: TextDecoration,
/// A span dominant baseline.
pub dominant_baseline: DominantBaseline,
/// A span alignment baseline.
pub alignment_baseline: AlignmentBaseline,
/// A list of all baseline shift that should be applied to this span.
///
/// Ordered from `text` element down to the actual `span` element.
pub baseline_shift: Vec<BaselineShift>,
/// A visibility property.
pub visibility: Visibility,
/// A letter spacing property.
pub letter_spacing: f32,
/// A word spacing property.
pub word_spacing: f32,
/// A text length property.
pub text_length: Option<f32>,
/// A length adjust property.
pub length_adjust: LengthAdjust,
}
/// A text chunk anchor property.
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum TextAnchor {
Start,
Middle,
End,
}
impl Default for TextAnchor {
fn default() -> Self {
Self::Start
}
}
/// A path used by text-on-path.
#[derive(Clone, Debug)]
pub struct TextPath {
/// Element's ID.
///
/// Taken from the SVG itself.
pub id: String,
/// A text offset in SVG coordinates.
///
/// Percentage values already resolved.
pub start_offset: f32,
/// A path.
pub path: Rc<tiny_skia_path::Path>,
}
/// A text chunk flow property.
#[derive(Clone, Debug)]
pub enum TextFlow {
/// A linear layout.
///
/// Includes left-to-right, right-to-left and top-to-bottom.
Linear,
/// A text-on-path layout.
Path(Rc<TextPath>),
}
/// A text chunk.
///
/// Text alignment and BIDI reordering can only be done inside a text chunk.
#[derive(Clone, Debug)]
pub struct TextChunk {
/// An absolute X axis offset.
pub x: Option<f32>,
/// An absolute Y axis offset.
pub y: Option<f32>,
/// A text anchor.
pub anchor: TextAnchor,
/// A list of text chunk style spans.
pub spans: Vec<TextSpan>,
/// A text chunk flow.
pub text_flow: TextFlow,
/// A text chunk actual text.
pub text: String,
}
/// A writing mode.
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum WritingMode {
LeftToRight,
TopToBottom,
}
/// A text element.
///
/// `text` element in SVG.
#[derive(Clone, Debug)]
pub struct Text {
/// Element's ID.
///
/// Taken from the SVG itself.
/// Isn't automatically generated.
/// Can be empty.
pub id: String,
/// Rendering mode.
///
/// `text-rendering` in SVG.
pub rendering_mode: TextRendering,
/// A relative X axis offsets.
///
/// One offset for each Unicode codepoint. Aka `char` in Rust.
pub dx: Vec<f32>,
/// A relative Y axis offsets.
///
/// One offset for each Unicode codepoint. Aka `char` in Rust.
pub dy: Vec<f32>,
/// A list of rotation angles.
///
/// One angle for each Unicode codepoint. Aka `char` in Rust.
pub rotate: Vec<f32>,
/// A writing mode.
pub writing_mode: WritingMode,
/// A list of text chunks.
pub chunks: Vec<TextChunk>,
/// Element's absolute transform.
///
/// Contains all ancestors transforms.
///
/// Will be set after calling `usvg::Tree::postprocess`.
///
/// Note that this is not the relative transform present in SVG.
/// The SVG one would be set only on groups.
pub abs_transform: Transform,
/// Contains a text bounding box.
///
/// Text bounding box is special in SVG and doesn't represent
/// tight bounds of the element's content.
/// You can find more about it
/// [here](https://razrfalcon.github.io/notes-on-svg-parsing/text/bbox.html).
///
/// `objectBoundingBox` in SVG terms. Meaning it doesn't affected by parent transforms.
///
/// Will be set only after calling `usvg::Tree::postprocess` with
/// `usvg::PostProcessingSteps::convert_text_into_paths`.
/// Assuming the `text` build feature of `usvg` was enabled.
/// This is because we have to perform a text layout before calculating a bounding box.
pub bounding_box: Option<NonZeroRect>,
/// Element's object bounding box including stroke.
///
/// Similar to `bounding_box`, but includes stroke.
///
/// Will have the same value as `bounding_box` when path has no stroke.
pub stroke_bounding_box: Option<NonZeroRect>,
/// Text converted into paths, ready to render.
///
/// Will be set only after calling `usvg::Tree::postprocess` with
/// `usvg::PostProcessingSteps::convert_text_into_paths`.
/// Assuming the `text` build feature of `usvg` was enabled.
pub flattened: Option<Box<Group>>,
}
impl Text {
pub(crate) fn subroots(&self, f: &mut dyn FnMut(&Group)) {
if let Some(ref flattened) = self.flattened {
f(flattened);
// Return now, since text chunks would have the same styles
// as the flattened text, which would lead to duplicates.
return;
}
let mut push_patt = |paint: Option<&Paint>| {
if let Some(Paint::Pattern(ref patt)) = paint {
f(&patt.borrow().root);
}
};
for chunk in &self.chunks {
for span in &chunk.spans {
push_patt(span.fill.as_ref().map(|f| &f.paint));
push_patt(span.stroke.as_ref().map(|f| &f.paint));
// Each text decoration can have paint.
if let Some(ref underline) = span.decoration.underline {
push_patt(underline.fill.as_ref().map(|f| &f.paint));
push_patt(underline.stroke.as_ref().map(|f| &f.paint));
}
if let Some(ref overline) = span.decoration.overline {
push_patt(overline.fill.as_ref().map(|f| &f.paint));
push_patt(overline.stroke.as_ref().map(|f| &f.paint));
}
if let Some(ref line_through) = span.decoration.line_through {
push_patt(line_through.fill.as_ref().map(|f| &f.paint));
push_patt(line_through.stroke.as_ref().map(|f| &f.paint));
}
}
}
}
pub(crate) fn subroots_mut(&mut self, f: &mut dyn FnMut(&mut Group)) {
if let Some(ref mut flattened) = self.flattened {
f(flattened);
// Return now, since text chunks would have the same styles
// as the flattened text, which would lead to duplicates.
return;
}
let mut push_patt = |paint: Option<&Paint>| {
if let Some(Paint::Pattern(ref patt)) = paint {
f(&mut patt.borrow_mut().root);
}
};
for chunk in &self.chunks {
for span in &chunk.spans {
push_patt(span.fill.as_ref().map(|f| &f.paint));
push_patt(span.stroke.as_ref().map(|f| &f.paint));
// Each text decoration can have paint.
if let Some(ref underline) = span.decoration.underline {
push_patt(underline.fill.as_ref().map(|f| &f.paint));
push_patt(underline.stroke.as_ref().map(|f| &f.paint));
}
if let Some(ref overline) = span.decoration.overline {
push_patt(overline.fill.as_ref().map(|f| &f.paint));
push_patt(overline.stroke.as_ref().map(|f| &f.paint));
}
if let Some(ref line_through) = span.decoration.line_through {
push_patt(line_through.fill.as_ref().map(|f| &f.paint));
push_patt(line_through.stroke.as_ref().map(|f| &f.paint));
}
}
}
}
}