mirror of
https://github.com/typst/typst
synced 2025-08-17 08:28:33 +08:00
Compare commits
9 Commits
77f26fc702
...
1dc6eec689
Author | SHA1 | Date | |
---|---|---|---|
|
1dc6eec689 | ||
|
9a6268050f | ||
|
d3b7b79275 | ||
|
e29ea242bc | ||
|
c8e838036b | ||
|
a87b426e66 | ||
|
476b79ddfc | ||
|
9256871d62 | ||
|
ddec8feab3 |
@ -76,6 +76,9 @@ pub enum Command {
|
|||||||
/// Processes an input file to extract provided metadata.
|
/// Processes an input file to extract provided metadata.
|
||||||
Query(QueryCommand),
|
Query(QueryCommand),
|
||||||
|
|
||||||
|
/// Create a vendor directory with all used packages.
|
||||||
|
Vendor(VendorCommand),
|
||||||
|
|
||||||
/// Lists all discovered fonts in system and custom font paths.
|
/// Lists all discovered fonts in system and custom font paths.
|
||||||
Fonts(FontsCommand),
|
Fonts(FontsCommand),
|
||||||
|
|
||||||
@ -164,6 +167,22 @@ pub struct QueryCommand {
|
|||||||
pub process: ProcessArgs,
|
pub process: ProcessArgs,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a vendor directory with all used packages in the current directory.
|
||||||
|
#[derive(Debug, Clone, Parser)]
|
||||||
|
pub struct VendorCommand {
|
||||||
|
/// Path to input Typst file. Use `-` to read input from stdin.
|
||||||
|
#[clap(value_parser = input_value_parser(), value_hint = ValueHint::FilePath)]
|
||||||
|
pub input: Input,
|
||||||
|
|
||||||
|
/// World arguments.
|
||||||
|
#[clap(flatten)]
|
||||||
|
pub world: WorldArgs,
|
||||||
|
|
||||||
|
/// Processing arguments.
|
||||||
|
#[clap(flatten)]
|
||||||
|
pub process: ProcessArgs,
|
||||||
|
}
|
||||||
|
|
||||||
/// Lists all discovered fonts in system and custom font paths.
|
/// Lists all discovered fonts in system and custom font paths.
|
||||||
#[derive(Debug, Clone, Parser)]
|
#[derive(Debug, Clone, Parser)]
|
||||||
pub struct FontsCommand {
|
pub struct FontsCommand {
|
||||||
@ -354,6 +373,14 @@ pub struct PackageArgs {
|
|||||||
value_name = "DIR"
|
value_name = "DIR"
|
||||||
)]
|
)]
|
||||||
pub package_cache_path: Option<PathBuf>,
|
pub package_cache_path: Option<PathBuf>,
|
||||||
|
|
||||||
|
/// Custom vendor directory name.
|
||||||
|
#[clap(
|
||||||
|
long = "package-vendor-path",
|
||||||
|
env = "TYPST_PACKAGE_VENDOR_PATH",
|
||||||
|
value_name = "DIR"
|
||||||
|
)]
|
||||||
|
pub vendor_path: Option<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Common arguments to customize available fonts.
|
/// Common arguments to customize available fonts.
|
||||||
|
@ -15,7 +15,7 @@ use crate::package;
|
|||||||
|
|
||||||
/// Execute an initialization command.
|
/// Execute an initialization command.
|
||||||
pub fn init(command: &InitCommand) -> StrResult<()> {
|
pub fn init(command: &InitCommand) -> StrResult<()> {
|
||||||
let package_storage = package::storage(&command.package);
|
let package_storage = package::storage(&command.package, None);
|
||||||
|
|
||||||
// Parse the package specification. If the user didn't specify the version,
|
// Parse the package specification. If the user didn't specify the version,
|
||||||
// we try to figure it out automatically by downloading the package index
|
// we try to figure it out automatically by downloading the package index
|
||||||
|
@ -13,6 +13,7 @@ mod terminal;
|
|||||||
mod timings;
|
mod timings;
|
||||||
#[cfg(feature = "self-update")]
|
#[cfg(feature = "self-update")]
|
||||||
mod update;
|
mod update;
|
||||||
|
mod vendor;
|
||||||
mod watch;
|
mod watch;
|
||||||
mod world;
|
mod world;
|
||||||
|
|
||||||
@ -70,6 +71,7 @@ fn dispatch() -> HintedStrResult<()> {
|
|||||||
Command::Watch(command) => crate::watch::watch(&mut timer, command)?,
|
Command::Watch(command) => crate::watch::watch(&mut timer, command)?,
|
||||||
Command::Init(command) => crate::init::init(command)?,
|
Command::Init(command) => crate::init::init(command)?,
|
||||||
Command::Query(command) => crate::query::query(command)?,
|
Command::Query(command) => crate::query::query(command)?,
|
||||||
|
Command::Vendor(command) => crate::vendor::vendor(command)?,
|
||||||
Command::Fonts(command) => crate::fonts::fonts(command),
|
Command::Fonts(command) => crate::fonts::fonts(command),
|
||||||
Command::Update(command) => crate::update::update(command)?,
|
Command::Update(command) => crate::update::update(command)?,
|
||||||
Command::Completions(command) => crate::completions::completions(command),
|
Command::Completions(command) => crate::completions::completions(command),
|
||||||
|
@ -1,13 +1,17 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use typst_kit::package::PackageStorage;
|
use typst_kit::package::PackageStorage;
|
||||||
|
|
||||||
use crate::args::PackageArgs;
|
use crate::args::PackageArgs;
|
||||||
use crate::download;
|
use crate::download;
|
||||||
|
|
||||||
/// Returns a new package storage for the given args.
|
/// Returns a new package storage for the given args.
|
||||||
pub fn storage(args: &PackageArgs) -> PackageStorage {
|
pub fn storage(args: &PackageArgs, workdir: Option<PathBuf>) -> PackageStorage {
|
||||||
PackageStorage::new(
|
PackageStorage::new(
|
||||||
|
args.vendor_path.clone(),
|
||||||
args.package_cache_path.clone(),
|
args.package_cache_path.clone(),
|
||||||
args.package_path.clone(),
|
args.package_path.clone(),
|
||||||
download::downloader(),
|
download::downloader(),
|
||||||
|
workdir,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
109
crates/typst-cli/src/vendor.rs
Normal file
109
crates/typst-cli/src/vendor.rs
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
use std::{
|
||||||
|
fs::{create_dir, create_dir_all},
|
||||||
|
path::PathBuf,
|
||||||
|
};
|
||||||
|
|
||||||
|
use ecow::eco_format;
|
||||||
|
use typst::{
|
||||||
|
diag::{bail, HintedStrResult, Warned},
|
||||||
|
layout::PagedDocument,
|
||||||
|
};
|
||||||
|
use typst_kit::package::{DEFAULT_PACKAGES_SUBDIR, DEFAULT_VENDOR_SUBDIR};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
args::VendorCommand, compile::print_diagnostics, set_failed, world::SystemWorld,
|
||||||
|
};
|
||||||
|
use typst::World;
|
||||||
|
|
||||||
|
/// Execute a vendor command.
|
||||||
|
pub fn vendor(command: &VendorCommand) -> HintedStrResult<()> {
|
||||||
|
let mut world = SystemWorld::new(&command.input, &command.world, &command.process)?;
|
||||||
|
|
||||||
|
// Reset everything and ensure that the main file is present.
|
||||||
|
world.reset();
|
||||||
|
world.source(world.main()).map_err(|err| err.to_string())?;
|
||||||
|
|
||||||
|
let Warned { output, warnings } = typst::compile::<PagedDocument>(&world);
|
||||||
|
|
||||||
|
match output {
|
||||||
|
Ok(_) => {
|
||||||
|
copy_deps(&mut world, &command.world.package.vendor_path)?;
|
||||||
|
print_diagnostics(&world, &[], &warnings, command.process.diagnostic_format)
|
||||||
|
.map_err(|err| eco_format!("failed to print diagnostics ({err})"))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print diagnostics.
|
||||||
|
Err(errors) => {
|
||||||
|
set_failed();
|
||||||
|
print_diagnostics(
|
||||||
|
&world,
|
||||||
|
&errors,
|
||||||
|
&warnings,
|
||||||
|
command.process.diagnostic_format,
|
||||||
|
)
|
||||||
|
.map_err(|err| eco_format!("failed to print diagnostics ({err})"))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn copy_deps(
|
||||||
|
world: &mut SystemWorld,
|
||||||
|
vendor_path: &Option<PathBuf>,
|
||||||
|
) -> HintedStrResult<()> {
|
||||||
|
let vendor_dir = match vendor_path {
|
||||||
|
Some(path) => match path.canonicalize() {
|
||||||
|
Ok(path) => path,
|
||||||
|
Err(err) => {
|
||||||
|
if err.kind() == std::io::ErrorKind::NotFound {
|
||||||
|
if let Err(err) = create_dir(path) {
|
||||||
|
bail!("failed to create vendor directory: {:?}", err);
|
||||||
|
}
|
||||||
|
path.clone()
|
||||||
|
} else {
|
||||||
|
bail!("failed to canonicalize vendor directory path: {:?}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => world.workdir().join(DEFAULT_VENDOR_SUBDIR),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Must iterate two times in total. As soon as the parent directory is created,
|
||||||
|
// world tries to read the subsequent files from the same package
|
||||||
|
// from the vendor directory since it is higher priority.
|
||||||
|
let all_deps = world
|
||||||
|
.dependencies()
|
||||||
|
.filter_map(|dep_path| {
|
||||||
|
let path = dep_path.to_str().unwrap();
|
||||||
|
path.find(DEFAULT_PACKAGES_SUBDIR).map(|pos| {
|
||||||
|
let dependency_path = &path[pos + DEFAULT_PACKAGES_SUBDIR.len() + 1..];
|
||||||
|
(dep_path.clone(), vendor_dir.join(dependency_path))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
for (from_data_path, to_vendor_path) in all_deps {
|
||||||
|
if let Some(parent) = to_vendor_path.parent() {
|
||||||
|
match parent.try_exists() {
|
||||||
|
Ok(false) => {
|
||||||
|
if let Err(err) = create_dir_all(parent) {
|
||||||
|
bail!(
|
||||||
|
"failed to create package inside the vendor directory: {:?}",
|
||||||
|
err
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
bail!("failed to check existence of a package inside the vendor directory: {:?}", err);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Err(err) = std::fs::copy(from_data_path, to_vendor_path) {
|
||||||
|
bail!("failed to copy dependency to vendor directory: {:?}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
@ -29,7 +29,7 @@ static STDIN_ID: LazyLock<FileId> =
|
|||||||
/// A world that provides access to the operating system.
|
/// A world that provides access to the operating system.
|
||||||
pub struct SystemWorld {
|
pub struct SystemWorld {
|
||||||
/// The working directory.
|
/// The working directory.
|
||||||
workdir: Option<PathBuf>,
|
workdir: PathBuf,
|
||||||
/// The root relative to which absolute paths are resolved.
|
/// The root relative to which absolute paths are resolved.
|
||||||
root: PathBuf,
|
root: PathBuf,
|
||||||
/// The input path.
|
/// The input path.
|
||||||
@ -132,15 +132,18 @@ impl SystemWorld {
|
|||||||
None => Now::System(OnceLock::new()),
|
None => Now::System(OnceLock::new()),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let env_workdir = std::env::current_dir().ok();
|
||||||
|
let workdir = env_workdir.unwrap_or(PathBuf::from("."));
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
workdir: std::env::current_dir().ok(),
|
workdir: workdir.clone(),
|
||||||
root,
|
root,
|
||||||
main,
|
main,
|
||||||
library: LazyHash::new(library),
|
library: LazyHash::new(library),
|
||||||
book: LazyHash::new(fonts.book),
|
book: LazyHash::new(fonts.book),
|
||||||
fonts: fonts.fonts,
|
fonts: fonts.fonts,
|
||||||
slots: Mutex::new(HashMap::new()),
|
slots: Mutex::new(HashMap::new()),
|
||||||
package_storage: package::storage(&world_args.package),
|
package_storage: package::storage(&world_args.package, Some(workdir)),
|
||||||
now,
|
now,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -157,7 +160,7 @@ impl SystemWorld {
|
|||||||
|
|
||||||
/// The current working directory.
|
/// The current working directory.
|
||||||
pub fn workdir(&self) -> &Path {
|
pub fn workdir(&self) -> &Path {
|
||||||
self.workdir.as_deref().unwrap_or(Path::new("."))
|
self.workdir.as_path()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return all paths the last compilation depended on.
|
/// Return all paths the last compilation depended on.
|
||||||
|
@ -109,10 +109,7 @@ fn handle(
|
|||||||
styles.chain(&style),
|
styles.chain(&style),
|
||||||
Region::new(Size::splat(Abs::inf()), Axes::splat(false)),
|
Region::new(Size::splat(Abs::inf()), Axes::splat(false)),
|
||||||
)?;
|
)?;
|
||||||
output.push(HtmlNode::Frame(HtmlFrame {
|
output.push(HtmlNode::Frame(HtmlFrame::new(frame, styles)));
|
||||||
inner: frame,
|
|
||||||
text_size: styles.resolve(TextElem::size),
|
|
||||||
}));
|
|
||||||
} else {
|
} else {
|
||||||
engine.sink.warn(warning!(
|
engine.sink.warn(warning!(
|
||||||
child.span(),
|
child.span(),
|
||||||
|
@ -2,10 +2,11 @@ use std::fmt::{self, Debug, Display, Formatter};
|
|||||||
|
|
||||||
use ecow::{EcoString, EcoVec};
|
use ecow::{EcoString, EcoVec};
|
||||||
use typst_library::diag::{bail, HintedStrResult, StrResult};
|
use typst_library::diag::{bail, HintedStrResult, StrResult};
|
||||||
use typst_library::foundations::{cast, Dict, Repr, Str};
|
use typst_library::foundations::{cast, Dict, Repr, Str, StyleChain};
|
||||||
use typst_library::introspection::{Introspector, Tag};
|
use typst_library::introspection::{Introspector, Tag};
|
||||||
use typst_library::layout::{Abs, Frame};
|
use typst_library::layout::{Abs, Frame};
|
||||||
use typst_library::model::DocumentInfo;
|
use typst_library::model::DocumentInfo;
|
||||||
|
use typst_library::text::TextElem;
|
||||||
use typst_syntax::Span;
|
use typst_syntax::Span;
|
||||||
use typst_utils::{PicoStr, ResolvedPicoStr};
|
use typst_utils::{PicoStr, ResolvedPicoStr};
|
||||||
|
|
||||||
@ -279,3 +280,10 @@ pub struct HtmlFrame {
|
|||||||
/// consistently.
|
/// consistently.
|
||||||
pub text_size: Abs,
|
pub text_size: Abs,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl HtmlFrame {
|
||||||
|
/// Wraps a laid-out frame.
|
||||||
|
pub fn new(inner: Frame, styles: StyleChain) -> Self {
|
||||||
|
Self { inner, text_size: styles.resolve(TextElem::size) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -121,6 +121,7 @@ fn write_children(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> {
|
|||||||
let pretty_inside = allows_pretty_inside(element.tag)
|
let pretty_inside = allows_pretty_inside(element.tag)
|
||||||
&& element.children.iter().any(|node| match node {
|
&& element.children.iter().any(|node| match node {
|
||||||
HtmlNode::Element(child) => wants_pretty_around(child.tag),
|
HtmlNode::Element(child) => wants_pretty_around(child.tag),
|
||||||
|
HtmlNode::Frame(_) => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -305,14 +306,6 @@ fn write_escape(w: &mut Writer, c: char) -> StrResult<()> {
|
|||||||
|
|
||||||
/// Encode a laid out frame into the writer.
|
/// Encode a laid out frame into the writer.
|
||||||
fn write_frame(w: &mut Writer, frame: &HtmlFrame) {
|
fn write_frame(w: &mut Writer, frame: &HtmlFrame) {
|
||||||
// FIXME: This string replacement is obviously a hack.
|
let svg = typst_svg::svg_html_frame(&frame.inner, frame.text_size);
|
||||||
let svg = typst_svg::svg_frame(&frame.inner).replace(
|
|
||||||
"<svg class",
|
|
||||||
&format!(
|
|
||||||
"<svg style=\"overflow: visible; width: {}em; height: {}em;\" class",
|
|
||||||
frame.inner.width() / frame.text_size,
|
|
||||||
frame.inner.height() / frame.text_size,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
w.buf.push_str(&svg);
|
w.buf.push_str(&svg);
|
||||||
}
|
}
|
||||||
|
@ -21,10 +21,15 @@ pub const DEFAULT_NAMESPACE: &str = "preview";
|
|||||||
/// The default packages sub directory within the package and package cache paths.
|
/// The default packages sub directory within the package and package cache paths.
|
||||||
pub const DEFAULT_PACKAGES_SUBDIR: &str = "typst/packages";
|
pub const DEFAULT_PACKAGES_SUBDIR: &str = "typst/packages";
|
||||||
|
|
||||||
|
/// The default vendor sub directory within the project root.
|
||||||
|
pub const DEFAULT_VENDOR_SUBDIR: &str = "vendor";
|
||||||
|
|
||||||
/// Holds information about where packages should be stored and downloads them
|
/// Holds information about where packages should be stored and downloads them
|
||||||
/// on demand, if possible.
|
/// on demand, if possible.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct PackageStorage {
|
pub struct PackageStorage {
|
||||||
|
/// The path at which packages are stored by the vendor command.
|
||||||
|
package_vendor_path: Option<PathBuf>,
|
||||||
/// The path at which non-local packages should be stored when downloaded.
|
/// The path at which non-local packages should be stored when downloaded.
|
||||||
package_cache_path: Option<PathBuf>,
|
package_cache_path: Option<PathBuf>,
|
||||||
/// The path at which local packages are stored.
|
/// The path at which local packages are stored.
|
||||||
@ -39,9 +44,11 @@ impl PackageStorage {
|
|||||||
/// Creates a new package storage for the given package paths. Falls back to
|
/// Creates a new package storage for the given package paths. Falls back to
|
||||||
/// the recommended XDG directories if they are `None`.
|
/// the recommended XDG directories if they are `None`.
|
||||||
pub fn new(
|
pub fn new(
|
||||||
|
package_vendor_path: Option<PathBuf>,
|
||||||
package_cache_path: Option<PathBuf>,
|
package_cache_path: Option<PathBuf>,
|
||||||
package_path: Option<PathBuf>,
|
package_path: Option<PathBuf>,
|
||||||
downloader: Downloader,
|
downloader: Downloader,
|
||||||
|
workdir: Option<PathBuf>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self::with_index(package_cache_path, package_path, downloader, OnceCell::new())
|
Self::with_index(package_cache_path, package_path, downloader, OnceCell::new())
|
||||||
}
|
}
|
||||||
@ -56,6 +63,8 @@ impl PackageStorage {
|
|||||||
index: OnceCell<Vec<serde_json::Value>>,
|
index: OnceCell<Vec<serde_json::Value>>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
package_vendor_path: package_vendor_path
|
||||||
|
.or_else(|| workdir.map(|workdir| workdir.join(DEFAULT_VENDOR_SUBDIR))),
|
||||||
package_cache_path: package_cache_path.or_else(|| {
|
package_cache_path: package_cache_path.or_else(|| {
|
||||||
dirs::cache_dir().map(|cache_dir| cache_dir.join(DEFAULT_PACKAGES_SUBDIR))
|
dirs::cache_dir().map(|cache_dir| cache_dir.join(DEFAULT_PACKAGES_SUBDIR))
|
||||||
}),
|
}),
|
||||||
@ -87,6 +96,16 @@ impl PackageStorage {
|
|||||||
) -> PackageResult<PathBuf> {
|
) -> PackageResult<PathBuf> {
|
||||||
let subdir = format!("{}/{}/{}", spec.namespace, spec.name, spec.version);
|
let subdir = format!("{}/{}/{}", spec.namespace, spec.name, spec.version);
|
||||||
|
|
||||||
|
// Read from vendor dir if it exists.
|
||||||
|
if let Some(vendor_dir) = &self.package_vendor_path {
|
||||||
|
if let Ok(true) = vendor_dir.try_exists() {
|
||||||
|
let dir = vendor_dir.join(&subdir);
|
||||||
|
if dir.exists() {
|
||||||
|
return Ok(dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(packages_dir) = &self.package_path {
|
if let Some(packages_dir) = &self.package_path {
|
||||||
let dir = packages_dir.join(&subdir);
|
let dir = packages_dir.join(&subdir);
|
||||||
if dir.exists() {
|
if dir.exists() {
|
||||||
|
@ -45,6 +45,30 @@ pub fn svg_frame(frame: &Frame) -> String {
|
|||||||
renderer.finalize()
|
renderer.finalize()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Export a frame into an SVG suitable for embedding into HTML.
|
||||||
|
#[typst_macros::time(name = "svg html frame")]
|
||||||
|
pub fn svg_html_frame(frame: &Frame, text_size: Abs) -> String {
|
||||||
|
let mut renderer = SVGRenderer::with_options(xmlwriter::Options {
|
||||||
|
indent: xmlwriter::Indent::None,
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
|
renderer.write_header_with_custom_attrs(frame.size(), |xml| {
|
||||||
|
xml.write_attribute("class", "typst-frame");
|
||||||
|
xml.write_attribute_fmt(
|
||||||
|
"style",
|
||||||
|
format_args!(
|
||||||
|
"overflow: visible; width: {}em; height: {}em;",
|
||||||
|
frame.width() / text_size,
|
||||||
|
frame.height() / text_size,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
let state = State::new(frame.size(), Transform::identity());
|
||||||
|
renderer.render_frame(state, Transform::identity(), frame);
|
||||||
|
renderer.finalize()
|
||||||
|
}
|
||||||
|
|
||||||
/// Export a document with potentially multiple pages into a single SVG file.
|
/// Export a document with potentially multiple pages into a single SVG file.
|
||||||
///
|
///
|
||||||
/// The padding will be added around and between the individual frames.
|
/// The padding will be added around and between the individual frames.
|
||||||
@ -158,8 +182,13 @@ impl State {
|
|||||||
impl SVGRenderer {
|
impl SVGRenderer {
|
||||||
/// Create a new SVG renderer with empty glyph and clip path.
|
/// Create a new SVG renderer with empty glyph and clip path.
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
|
Self::with_options(Default::default())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a new SVG renderer with the given configuration.
|
||||||
|
fn with_options(options: xmlwriter::Options) -> Self {
|
||||||
SVGRenderer {
|
SVGRenderer {
|
||||||
xml: XmlWriter::new(xmlwriter::Options::default()),
|
xml: XmlWriter::new(options),
|
||||||
glyphs: Deduplicator::new('g'),
|
glyphs: Deduplicator::new('g'),
|
||||||
clip_paths: Deduplicator::new('c'),
|
clip_paths: Deduplicator::new('c'),
|
||||||
gradient_refs: Deduplicator::new('g'),
|
gradient_refs: Deduplicator::new('g'),
|
||||||
@ -170,11 +199,22 @@ impl SVGRenderer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write the SVG header, including the `viewBox` and `width` and `height`
|
/// Write the default SVG header, including a `typst-doc` class, the
|
||||||
/// attributes.
|
/// `viewBox` and `width` and `height` attributes.
|
||||||
fn write_header(&mut self, size: Size) {
|
fn write_header(&mut self, size: Size) {
|
||||||
|
self.write_header_with_custom_attrs(size, |xml| {
|
||||||
|
xml.write_attribute("class", "typst-doc");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Write the SVG header with additional attributes and standard attributes.
|
||||||
|
fn write_header_with_custom_attrs(
|
||||||
|
&mut self,
|
||||||
|
size: Size,
|
||||||
|
write_custom_attrs: impl FnOnce(&mut XmlWriter),
|
||||||
|
) {
|
||||||
self.xml.start_element("svg");
|
self.xml.start_element("svg");
|
||||||
self.xml.write_attribute("class", "typst-doc");
|
write_custom_attrs(&mut self.xml);
|
||||||
self.xml.write_attribute_fmt(
|
self.xml.write_attribute_fmt(
|
||||||
"viewBox",
|
"viewBox",
|
||||||
format_args!("0 0 {} {}", size.x.to_pt(), size.y.to_pt()),
|
format_args!("0 0 {} {}", size.x.to_pt(), size.y.to_pt()),
|
||||||
|
11
tests/ref/html/html-frame.html
Normal file
11
tests/ref/html/html-frame.html
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<p>A rectangle:</p>
|
||||||
|
<svg class="typst-frame" style="overflow: visible; width: 4.5em; height: 3em;" viewBox="0 0 45 30" width="45pt" height="30pt" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:h5="http://www.w3.org/1999/xhtml"><g><g transform="translate(-0 -0)"><path class="typst-shape" fill="none" stroke="#000000" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="4" d="M 0 0 L 0 30 L 45 30 L 45 0 Z "/></g></g></svg>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -1,5 +1,6 @@
|
|||||||
// No proper HTML tests here yet because we don't want to test SVG export just
|
--- html-frame html ---
|
||||||
// yet. We'll definitely add tests at some point.
|
A rectangle:
|
||||||
|
#html.frame(rect())
|
||||||
|
|
||||||
--- html-frame-in-layout ---
|
--- html-frame-in-layout ---
|
||||||
// Ensure that HTML frames are transparent in layout. This is less important for
|
// Ensure that HTML frames are transparent in layout. This is less important for
|
||||||
|
Loading…
x
Reference in New Issue
Block a user