summaryrefslogtreecommitdiff
path: root/crates/typst-library/src/visualize
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2025-01-09 10:34:16 +0100
committerGitHub <noreply@github.com>2025-01-09 09:34:16 +0000
commite2b37fef33a92a7086790e04fb133472413c0c0a (patch)
treea2bdc638482890183414dce18f8f586786154017 /crates/typst-library/src/visualize
parentdacd6acd5e73d35c6e7a7a3b144f16ae70d03daa (diff)
Revamp data loading and deprecate `decode` functions (#5671)
Diffstat (limited to 'crates/typst-library/src/visualize')
-rw-r--r--crates/typst-library/src/visualize/image/mod.rs52
-rw-r--r--crates/typst-library/src/visualize/image/svg.rs1
2 files changed, 33 insertions, 20 deletions
diff --git a/crates/typst-library/src/visualize/image/mod.rs b/crates/typst-library/src/visualize/image/mod.rs
index 452bb65c..0f060201 100644
--- a/crates/typst-library/src/visualize/image/mod.rs
+++ b/crates/typst-library/src/visualize/image/mod.rs
@@ -14,14 +14,14 @@ use ecow::EcoString;
use typst_syntax::{Span, Spanned};
use typst_utils::LazyHash;
-use crate::diag::{At, SourceResult, StrResult};
+use crate::diag::{SourceResult, StrResult};
use crate::engine::Engine;
use crate::foundations::{
- cast, elem, func, scope, Bytes, Cast, Content, NativeElement, Packed, Show, Smart,
- StyleChain,
+ cast, elem, func, scope, Bytes, Cast, Content, Derived, NativeElement, Packed, Show,
+ Smart, StyleChain,
};
use crate::layout::{BlockElem, Length, Rel, Sizing};
-use crate::loading::Readable;
+use crate::loading::{DataSource, Load, Readable};
use crate::model::Figurable;
use crate::text::LocalName;
use crate::World;
@@ -46,25 +46,16 @@ use crate::World;
/// ```
#[elem(scope, Show, LocalName, Figurable)]
pub struct ImageElem {
- /// Path to an image file.
+ /// A path to an image file or raw bytes making up an encoded image.
///
- /// For more details, see the [Paths section]($syntax/#paths).
+ /// For more details about paths, see the [Paths section]($syntax/#paths).
#[required]
#[parse(
- let Spanned { v: path, span } =
- args.expect::<Spanned<EcoString>>("path to image file")?;
- let id = span.resolve_path(&path).at(span)?;
- let data = engine.world.file(id).at(span)?;
- path
+ let source = args.expect::<Spanned<DataSource>>("source")?;
+ let data = source.load(engine.world)?;
+ Derived::new(source.v, data)
)]
- #[borrowed]
- pub path: EcoString,
-
- /// The raw file data.
- #[internal]
- #[required]
- #[parse(Readable::Bytes(data))]
- pub data: Readable,
+ pub source: Derived<DataSource, Bytes>,
/// The image's format. Detected automatically by default.
///
@@ -106,6 +97,9 @@ pub struct ImageElem {
impl ImageElem {
/// Decode a raster or vector graphic from bytes or a string.
///
+ /// This function is deprecated. The [`image`] function now accepts bytes
+ /// directly.
+ ///
/// ```example
/// #let original = read("diagram.svg")
/// #let changed = original.replace(
@@ -138,7 +132,9 @@ impl ImageElem {
#[named]
fit: Option<ImageFit>,
) -> StrResult<Content> {
- let mut elem = ImageElem::new(EcoString::new(), data);
+ let bytes = data.into_bytes();
+ let source = Derived::new(DataSource::Bytes(bytes.clone()), bytes);
+ let mut elem = ImageElem::new(source);
if let Some(format) = format {
elem.push_format(format);
}
@@ -337,6 +333,22 @@ pub enum ImageFormat {
Vector(VectorFormat),
}
+impl ImageFormat {
+ /// Try to detect the format of an image from data.
+ pub fn detect(data: &[u8]) -> Option<Self> {
+ if let Some(format) = RasterFormat::detect(data) {
+ return Some(Self::Raster(format));
+ }
+
+ // SVG or compressed SVG.
+ if data.starts_with(b"<svg") || data.starts_with(&[0x1f, 0x8b]) {
+ return Some(Self::Vector(VectorFormat::Svg));
+ }
+
+ None
+ }
+}
+
/// A vector graphics format.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)]
pub enum VectorFormat {
diff --git a/crates/typst-library/src/visualize/image/svg.rs b/crates/typst-library/src/visualize/image/svg.rs
index 6b6a1b6b..089f0543 100644
--- a/crates/typst-library/src/visualize/image/svg.rs
+++ b/crates/typst-library/src/visualize/image/svg.rs
@@ -110,6 +110,7 @@ impl Hash for Repr {
// all used fonts gives us something similar.
self.data.hash(state);
self.font_hash.hash(state);
+ self.flatten_text.hash(state);
}
}