mirror of
https://github.com/typst/typst
synced 2025-08-12 22:27:56 +08:00
Plain image constructors
This commit is contained in:
parent
b68971d01e
commit
776c6d1923
@ -7,7 +7,7 @@ use typst_syntax::Span;
|
|||||||
use usvg::tiny_skia_path;
|
use usvg::tiny_skia_path;
|
||||||
use xmlwriter::XmlWriter;
|
use xmlwriter::XmlWriter;
|
||||||
|
|
||||||
use crate::foundations::{Bytes, Smart};
|
use crate::foundations::Bytes;
|
||||||
use crate::layout::{Abs, Frame, FrameItem, Point, Size};
|
use crate::layout::{Abs, Frame, FrameItem, Point, Size};
|
||||||
use crate::text::{Font, Glyph};
|
use crate::text::{Font, Glyph};
|
||||||
use crate::visualize::{
|
use crate::visualize::{
|
||||||
@ -105,11 +105,7 @@ fn draw_raster_glyph(
|
|||||||
raster_image: ttf_parser::RasterGlyphImage,
|
raster_image: ttf_parser::RasterGlyphImage,
|
||||||
) -> Option<()> {
|
) -> Option<()> {
|
||||||
let data = Bytes::new(raster_image.data.to_vec());
|
let data = Bytes::new(raster_image.data.to_vec());
|
||||||
let image = Image::new(
|
let image = Image::plain(RasterImage::plain(data, ExchangeFormat::Png).ok()?);
|
||||||
RasterImage::new(data, ExchangeFormat::Png, Smart::Auto).ok()?,
|
|
||||||
None,
|
|
||||||
Smart::Auto,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Apple Color emoji doesn't provide offset information (or at least
|
// Apple Color emoji doesn't provide offset information (or at least
|
||||||
// not in a way ttf-parser understands), so we artificially shift their
|
// not in a way ttf-parser understands), so we artificially shift their
|
||||||
@ -181,7 +177,7 @@ fn draw_colr_glyph(
|
|||||||
svg.end_element();
|
svg.end_element();
|
||||||
|
|
||||||
let data = Bytes::from_string(svg.end_document());
|
let data = Bytes::from_string(svg.end_document());
|
||||||
let image = Image::new(SvgImage::new(data).ok()?, None, Smart::Auto);
|
let image = Image::plain(SvgImage::new(data).ok()?);
|
||||||
|
|
||||||
let y_shift = Abs::pt(upem.to_pt() - y_max);
|
let y_shift = Abs::pt(upem.to_pt() - y_max);
|
||||||
let position = Point::new(Abs::pt(x_min), y_shift);
|
let position = Point::new(Abs::pt(x_min), y_shift);
|
||||||
@ -257,7 +253,7 @@ fn draw_svg_glyph(
|
|||||||
);
|
);
|
||||||
|
|
||||||
let data = Bytes::from_string(wrapper_svg);
|
let data = Bytes::from_string(wrapper_svg);
|
||||||
let image = Image::new(SvgImage::new(data).ok()?, None, Smart::Auto);
|
let image = Image::plain(SvgImage::new(data).ok()?);
|
||||||
|
|
||||||
let position = Point::new(Abs::pt(left), Abs::pt(top) + upem);
|
let position = Point::new(Abs::pt(left), Abs::pt(top) + upem);
|
||||||
let size = Size::new(Abs::pt(width), Abs::pt(height));
|
let size = Size::new(Abs::pt(width), Abs::pt(height));
|
||||||
|
@ -290,6 +290,11 @@ impl Image {
|
|||||||
Self::new_impl(kind.into(), alt, scaling)
|
Self::new_impl(kind.into(), alt, scaling)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create an image with optional properties set to the default.
|
||||||
|
pub fn plain(kind: impl Into<ImageKind>) -> Self {
|
||||||
|
Self::new(kind, None, Smart::Auto)
|
||||||
|
}
|
||||||
|
|
||||||
/// The internal, non-generic implementation. This is memoized to reuse
|
/// The internal, non-generic implementation. This is memoized to reuse
|
||||||
/// the `Arc` and `LazyHash`.
|
/// the `Arc` and `LazyHash`.
|
||||||
#[comemo::memoize]
|
#[comemo::memoize]
|
||||||
|
@ -33,10 +33,15 @@ impl RasterImage {
|
|||||||
data: Bytes,
|
data: Bytes,
|
||||||
format: impl Into<RasterFormat>,
|
format: impl Into<RasterFormat>,
|
||||||
icc: Smart<Bytes>,
|
icc: Smart<Bytes>,
|
||||||
) -> StrResult<RasterImage> {
|
) -> StrResult<Self> {
|
||||||
Self::new_impl(data, format.into(), icc)
|
Self::new_impl(data, format.into(), icc)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a raster image with optional properties set to the default.
|
||||||
|
pub fn plain(data: Bytes, format: impl Into<RasterFormat>) -> StrResult<Self> {
|
||||||
|
Self::new(data, format, Smart::Auto)
|
||||||
|
}
|
||||||
|
|
||||||
/// The internal, non-generic implementation.
|
/// The internal, non-generic implementation.
|
||||||
#[comemo::memoize]
|
#[comemo::memoize]
|
||||||
#[typst_macros::time(name = "load raster image")]
|
#[typst_macros::time(name = "load raster image")]
|
||||||
@ -425,7 +430,7 @@ mod tests {
|
|||||||
fn test(path: &str, format: ExchangeFormat, dpi: f64) {
|
fn test(path: &str, format: ExchangeFormat, dpi: f64) {
|
||||||
let data = typst_dev_assets::get(path).unwrap();
|
let data = typst_dev_assets::get(path).unwrap();
|
||||||
let bytes = Bytes::new(data);
|
let bytes = Bytes::new(data);
|
||||||
let image = RasterImage::new(bytes, format, Smart::Auto).unwrap();
|
let image = RasterImage::plain(bytes, format).unwrap();
|
||||||
assert_eq!(image.dpi().map(f64::round), Some(dpi));
|
assert_eq!(image.dpi().map(f64::round), Some(dpi));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ use std::io::Read;
|
|||||||
use base64::Engine;
|
use base64::Engine;
|
||||||
use ecow::EcoString;
|
use ecow::EcoString;
|
||||||
use ttf_parser::GlyphId;
|
use ttf_parser::GlyphId;
|
||||||
use typst_library::foundations::{Bytes, Smart};
|
use typst_library::foundations::Bytes;
|
||||||
use typst_library::layout::{Abs, Point, Ratio, Size, Transform};
|
use typst_library::layout::{Abs, Point, Ratio, Size, Transform};
|
||||||
use typst_library::text::{Font, TextItem};
|
use typst_library::text::{Font, TextItem};
|
||||||
use typst_library::visualize::{
|
use typst_library::visualize::{
|
||||||
@ -246,15 +246,8 @@ fn convert_bitmap_glyph_to_image(font: &Font, id: GlyphId) -> Option<(Image, f64
|
|||||||
if raster.format != ttf_parser::RasterImageFormat::PNG {
|
if raster.format != ttf_parser::RasterImageFormat::PNG {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let image = Image::new(
|
let image = Image::plain(
|
||||||
RasterImage::new(
|
RasterImage::plain(Bytes::new(raster.data.to_vec()), ExchangeFormat::Png).ok()?,
|
||||||
Bytes::new(raster.data.to_vec()),
|
|
||||||
ExchangeFormat::Png,
|
|
||||||
Smart::Auto,
|
|
||||||
)
|
|
||||||
.ok()?,
|
|
||||||
None,
|
|
||||||
Smart::Auto,
|
|
||||||
);
|
);
|
||||||
Some((image, raster.x as f64, raster.y as f64))
|
Some((image, raster.x as f64, raster.y as f64))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user