font: remove half-baked attempt at rendering svg fonts

It's just too complex. Will focus on COLR 1 instead.
This commit is contained in:
Wez Furlong committed 2023-08-20 15:19:30 -07:00
1 parent 6b7ba71f32
commit 44e9e09273
4 files changed
+3 -402

No files matched your search

Generated
+3 -160
View File
@@ -1263,12 +1263,6 @@ version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2e66c9d817f1720209181c316d28635c050fa304f9c79e47a520882661b7308"
[[package]]
name = "data-url"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d7439c3735f405729d52c3fbbe4de140eaf938a1fe47d227c27f8254d4302a5"
[[package]]
name = "deltae"
version = "0.3.2"
@@ -2478,12 +2472,6 @@ dependencies = [
"tiff",
]
[[package]]
name = "imagesize"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "029d73f573d8e8d63e6d5020011d3255b28c3ba85d6cf870a07184ed23de9284"
[[package]]
name = "indexmap"
version = "1.9.3"
@@ -2687,15 +2675,6 @@ dependencies = [
"libc",
]
[[package]]
name = "kurbo"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd85a5776cd9500c2e2059c8c76c3b01528566b7fcbaf8098b55a33fc298849b"
dependencies = [
"arrayvec",
]
[[package]]
name = "lab"
version = "0.11.0"
@@ -3824,12 +3803,6 @@ dependencies = [
"siphasher",
]
[[package]]
name = "pico-args"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315"
[[package]]
name = "pin-project"
version = "1.1.3"
@@ -4222,12 +4195,6 @@ dependencies = [
"yasna",
]
[[package]]
name = "rctree"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b42e27ef78c35d3998403c1d26f3efd9e135d3e5121b0a4845cc5cc27547f4f"
[[package]]
name = "redox_syscall"
version = "0.2.16"
@@ -4350,19 +4317,6 @@ dependencies = [
"rgb",
]
[[package]]
name = "resvg"
version = "0.35.0"
source = "git+https://github.com/wez/resvg.git?rev=d692908d0121c496808eebc83c4519fca4db5331#d692908d0121c496808eebc83c4519fca4db5331"
dependencies = [
"log",
"pico-args",
"rgb",
"svgtypes",
"tiny-skia 0.10.0",
"usvg",
]
[[package]]
name = "rgb"
version = "0.8.36"
@@ -4393,15 +4347,6 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3582f63211428f83597b51b2ddb88e2a91a9d52d12831f9d08f5e624e8977422"
[[package]]
name = "roxmltree"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8f595a457b6b8c6cda66a48503e92ee8d19342f905948f29c383200ec9eb1d8"
dependencies = [
"xmlparser",
]
[[package]]
name = "rstest"
version = "0.18.2"
@@ -4820,15 +4765,6 @@ version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe"
[[package]]
name = "simplecss"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a11be7c62927d9427e9f40f3444d5499d868648e2edbc4e2116de69e7ec0e89d"
dependencies = [
"log",
]
[[package]]
name = "siphasher"
version = "0.3.10"
@@ -5052,9 +4988,6 @@ name = "strict-num"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731"
dependencies = [
"float-cmp",
]
[[package]]
name = "strip-ansi-escapes"
@@ -5076,16 +5009,6 @@ version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fb1df15f412ee2e9dfc1c504260fa695c1c3f10fe9f4a6ee2d2184d7d6450e2"
[[package]]
name = "svgtypes"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed4b0611e7f3277f68c0fa18e385d9e2d26923691379690039548f867cef02a7"
dependencies = [
"kurbo",
"siphasher",
]
[[package]]
name = "syn"
version = "1.0.109"
@@ -5421,21 +5344,6 @@ dependencies = [
"time-core",
]
[[package]]
name = "tiny-skia"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7db11798945fa5c3e5490c794ccca7c6de86d3afdd54b4eb324109939c6f37bc"
dependencies = [
"arrayref",
"arrayvec",
"bytemuck",
"cfg-if",
"log",
"png",
"tiny-skia-path 0.10.0",
]
[[package]]
name = "tiny-skia"
version = "0.11.1"
@@ -5448,18 +5356,7 @@ dependencies = [
"cfg-if",
"log",
"png",
"tiny-skia-path 0.11.1",
]
[[package]]
name = "tiny-skia-path"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f60aa35c89ac2687ace1a2556eaaea68e8c0d47408a2e3e7f5c98a489e7281c"
dependencies = [
"arrayref",
"bytemuck",
"strict-num",
"tiny-skia-path",
]
[[package]]
@@ -5754,47 +5651,6 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "usvg"
version = "0.35.0"
source = "git+https://github.com/wez/resvg.git?rev=d692908d0121c496808eebc83c4519fca4db5331#d692908d0121c496808eebc83c4519fca4db5331"
dependencies = [
"base64 0.21.2",
"log",
"pico-args",
"usvg-parser",
"usvg-tree",
"xmlwriter",
]
[[package]]
name = "usvg-parser"
version = "0.35.0"
source = "git+https://github.com/wez/resvg.git?rev=d692908d0121c496808eebc83c4519fca4db5331#d692908d0121c496808eebc83c4519fca4db5331"
dependencies = [
"data-url",
"flate2",
"imagesize",
"kurbo",
"log",
"roxmltree",
"simplecss",
"siphasher",
"svgtypes",
"usvg-tree",
]
[[package]]
name = "usvg-tree"
version = "0.35.0"
source = "git+https://github.com/wez/resvg.git?rev=d692908d0121c496808eebc83c4519fca4db5331#d692908d0121c496808eebc83c4519fca4db5331"
dependencies = [
"rctree",
"strict-num",
"svgtypes",
"tiny-skia-path 0.10.0",
]
[[package]]
name = "utf8parse"
version = "0.2.1"
@@ -6228,7 +6084,6 @@ dependencies = [
"objc",
"ordered-float",
"rangeset",
"resvg",
"termwiz",
"thiserror",
"walkdir",
@@ -6302,7 +6157,7 @@ dependencies = [
"termwiz-funcs",
"textwrap 0.16.0",
"thiserror",
"tiny-skia 0.11.1",
"tiny-skia",
"uds_windows",
"umask",
"unicode-normalization",
@@ -6697,7 +6552,7 @@ dependencies = [
"shlex",
"smithay-client-toolkit",
"thiserror",
"tiny-skia 0.11.1",
"tiny-skia",
"url",
"wayland-client",
"wayland-egl",
@@ -7027,18 +6882,6 @@ version = "0.8.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47430998a7b5d499ccee752b41567bc3afc57e1327dc855b1a2aa44ce29b5fa1"
[[package]]
name = "xmlparser"
version = "0.13.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d25c75bf9ea12c4040a97f829154768bbbce366287e2dc044af160cd79a13fd"
[[package]]
name = "xmlwriter"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec7a2a501ed189703dba8b08142f057e887dfc4b2cc4db2d343ac6376ba3e0b9"
[[package]]
name = "yaml-rust"
version = "0.4.5"
-1
View File
@@ -30,7 +30,6 @@ memmap2 = "0.2"
metrics = { version="0.17", features=["std"]}
ordered-float = "3.0"
rangeset = { path = "../rangeset" }
resvg = {version="0.35", default-features=false, git="https://github.com/wez/resvg.git", rev="d692908d0121c496808eebc83c4519fca4db5331"}
termwiz = { path = "../termwiz" }
thiserror = "1.0"
walkdir = "2"
-12
View File
@@ -876,18 +876,6 @@ impl Library {
}
}
// Hook up our svg rendering
if config::configuration().experimental_svg_fonts {
unsafe {
FT_Property_Set(
lib.lib,
b"ot-svg\0" as *const u8 as *const FT_String,
b"svg-hooks\0" as *const u8 as *const FT_String,
&crate::rasterizer::freetype::SVG_HOOKS as *const SVG_RendererHooks as *const _,
);
}
}
// Due to patent concerns, the freetype library disables the LCD
// filtering feature by default, and since we always build our
// own copy of freetype, it is likewise disabled by default for
-229
View File
@@ -9,18 +9,10 @@ use ::freetype::{
use anyhow::{bail, Context};
use config::{DisplayPixelGeometry, FreeTypeLoadFlags, FreeTypeLoadTarget};
use lfucache::LfuCache;
use resvg::tiny_skia::{Pixmap, PixmapMut, PixmapPaint, Transform};
use std::cell::RefCell;
use std::{mem, slice};
use wezterm_color_types::linear_u8_to_srgb8;
pub static SVG_HOOKS: SVG_RendererHooks = SVG_RendererHooks {
init_svg: Some(init_svg_library),
free_svg: Some(free_svg_library),
render_svg: Some(svg_render),
preset_slot: Some(svg_preset_slot),
};
pub struct FreeTypeRasterizer {
has_color: bool,
face: RefCell<ftwrap::Face>,
@@ -283,224 +275,3 @@ impl FreeTypeRasterizer {
})
}
}
struct SvgState {
pixmap: Pixmap,
doc_cache: LfuCache<DocCacheKey, resvg::usvg::Tree>,
}
impl SvgState {
fn new() -> Self {
Self {
pixmap: Pixmap::new(1, 1).expect("to allocate empty pixmap"),
doc_cache: LfuCache::new(
"font.svg.parser.hit",
"font.svg.parser.miss",
|_| 64,
&config::configuration(),
),
}
}
}
// The SVG rendering implementation was produced by roughly following
// <https://gitlab.freedesktop.org/freetype/freetype-demos/-/blob/master/src/rsvg-port.c>
// and adapting for resvg
unsafe extern "C" fn init_svg_library(data_pointer: *mut FT_Pointer) -> FT_Error {
let state = Box::new(SvgState::new());
*data_pointer = Box::into_raw(state) as *mut std::os::raw::c_void;
FT_Err_Ok as FT_Error
}
unsafe extern "C" fn free_svg_library(data_pointer: *mut FT_Pointer) {
let state: Box<SvgState> = Box::from_raw((*data_pointer) as *mut SvgState);
drop(state);
*data_pointer = std::ptr::null_mut();
}
/*
* This hook is called at two different locations. Firstly, it is called
* when presetting the glyphslot when `FT_Load_Glyph` is called.
* Secondly, it is called right before the render hook is called. When
* `cache` is false, it is the former, when `cache` is true, it is the
* latter.
*
* The job of this function is to preset the slot setting the width,
* height, pitch, `bitmap.left`, and `bitmap.top`. These are all
* necessary for appropriate memory allocation, as well as ultimately
* compositing the glyph later on by client applications.
*/
unsafe extern "C" fn svg_preset_slot(
slot: FT_GlyphSlot,
cache: FT_Bool,
state: *mut FT_Pointer,
) -> FT_Error {
let state: &mut SvgState = &mut *((*state) as *mut SvgState);
let slot: &mut FT_GlyphSlotRec_ = &mut *slot;
let document: &mut FT_SVG_DocumentRec_ = &mut *(slot.other as FT_SVG_Document);
let cache = cache != 0;
match svg_preset_slot_impl(slot, document, cache, state) {
Ok(_) => FT_Err_Ok as FT_Error,
Err(err) => {
log::error!("svg_preset_slot: {err:#}");
FT_Err_Invalid_SVG_Document as FT_Error
}
}
}
#[derive(Hash, Eq, PartialEq, Clone, Copy, Debug)]
struct DocCacheKey {
doc_ptr: usize,
start_glyph_id: u16,
end_glyph_id: u16,
}
fn svg_preset_slot_impl(
slot: &mut FT_GlyphSlotRec_,
document: &mut FT_SVG_DocumentRec_,
cache: bool,
state: &mut SvgState,
) -> anyhow::Result<()> {
use resvg::usvg::TreeParsing;
let svg_data = unsafe {
std::slice::from_raw_parts(document.svg_document, document.svg_document_length as usize)
};
let document_hash = DocCacheKey {
doc_ptr: svg_data.as_ptr() as usize,
start_glyph_id: document.start_glyph_id,
end_glyph_id: document.end_glyph_id,
};
let utree = match state.doc_cache.get(&document_hash) {
Some(t) => t,
None => {
let options = resvg::usvg::Options {
image_href_resolver: resvg::usvg::ImageHrefResolver {
resolve_string: Box::new(|_href, _opts| None),
..Default::default()
},
..Default::default()
};
let utree =
resvg::usvg::Tree::from_data(svg_data, &options).context("Tree::from_data")?;
state.doc_cache.put(document_hash, utree);
state
.doc_cache
.get(&document_hash)
.expect("just inserted it")
}
};
let tree_node;
let rtree = if document.start_glyph_id < document.end_glyph_id {
tree_node = utree
.node_by_id(&format!("glyph{}", slot.glyph_index))
.context("missing glyph")?;
resvg::Tree::from_usvg_node(&tree_node).context("from_usvg_node")?
} else {
resvg::Tree::from_usvg(&utree)
};
let pixmap_size = rtree.size.to_int_size();
let x_svg_to_out = document.metrics.x_ppem as f64 / pixmap_size.width() as f64;
let y_svg_to_out = document.metrics.y_ppem as f64 / pixmap_size.height() as f64;
let xx = (document.transform.xx as f64) / (1 << 16) as f64;
let xy = -(document.transform.xy as f64) / (1 << 16) as f64;
let yx = -(document.transform.yx as f64) / (1 << 16) as f64;
let yy = (document.transform.yy as f64) / (1 << 16) as f64;
let x0 =
document.delta.x as f64 / 64. * pixmap_size.width() as f64 / document.metrics.x_ppem as f64;
let y0 = -document.delta.y as f64 / 64. * pixmap_size.height() as f64
/ document.metrics.y_ppem as f64;
let transform = resvg::tiny_skia::Transform::from_row(
xx as f32, xy as f32, yx as f32, yy as f32, x0 as f32, y0 as f32,
)
.post_scale(x_svg_to_out as f32, y_svg_to_out as f32);
let dimension_x = pixmap_size.width() as f64 * x_svg_to_out;
let dimension_y = pixmap_size.height() as f64 * y_svg_to_out;
slot.bitmap_left = 0;
slot.bitmap_top = dimension_y as i32; // This sets the y-bearing. It is incorrect,
// but better than using 0
slot.bitmap.rows = dimension_y as _;
slot.bitmap.width = dimension_x as _;
slot.bitmap.pitch = (dimension_x as i32) * 4;
slot.bitmap.pixel_mode = ftwrap::FT_Pixel_Mode::FT_PIXEL_MODE_BGRA as _;
slot.metrics.width = (dimension_x * 64.0) as FT_Pos;
slot.metrics.height = (dimension_y * 64.0) as FT_Pos;
slot.metrics.horiBearingX = 0;
slot.metrics.horiBearingY = 0;
slot.metrics.vertBearingX = 0;
slot.metrics.vertBearingY = 0;
if slot.metrics.vertAdvance == 0 {
slot.metrics.vertAdvance = (dimension_y * 1.2 * 64.) as FT_Pos;
}
if cache {
let mut pixmap =
Pixmap::new(dimension_x as u32, dimension_y as u32).context("Pixmap::new")?;
rtree.render(transform, &mut pixmap.as_mut());
state.pixmap = pixmap;
}
Ok(())
}
/// The render hook. The job of this hook is to simply render the glyph in
/// the buffer that has been allocated on the FreeType side.
unsafe extern "C" fn svg_render(slot: FT_GlyphSlot, data_pointer: *mut FT_Pointer) -> FT_Error {
let state: &mut SvgState = &mut *((*data_pointer) as *mut SvgState);
let slot: &mut FT_GlyphSlotRec_ = &mut *slot;
match svg_render_impl(slot, state) {
Ok(_) => FT_Err_Ok as FT_Error,
Err(err) => {
log::error!("svg_render: {err:#}");
FT_Err_Invalid_SVG_Document as FT_Error
}
}
}
fn svg_render_impl(slot: &mut FT_GlyphSlotRec_, state: &mut SvgState) -> anyhow::Result<()> {
let bitmap = unsafe {
std::slice::from_raw_parts_mut(
slot.bitmap.buffer,
slot.bitmap.width as usize * slot.bitmap.rows as usize * 4,
)
};
let mut pixmap =
PixmapMut::from_bytes(bitmap, slot.bitmap.width, slot.bitmap.rows).context("PixmapMut")?;
pixmap.draw_pixmap(
0,
0,
state.pixmap.as_ref(),
&PixmapPaint::default(),
Transform::default(),
None,
);
// Post-process: freetype wants BGRA but tiny-skia is RGBA
for pixel in pixmap.pixels_mut() {
let (r, g, b, a) = (pixel.red(), pixel.green(), pixel.blue(), pixel.alpha());
*pixel =
resvg::tiny_skia::PremultipliedColorU8::from_rgba(b, g, r, a).expect("swap to succeed");
}
slot.bitmap.pixel_mode = ftwrap::FT_Pixel_Mode::FT_PIXEL_MODE_BGRA as _;
slot.bitmap.num_grays = 256;
slot.format = crate::ftwrap::FT_Glyph_Format_::FT_GLYPH_FORMAT_BITMAP;
Ok(())
}