diff --git a/flake.nix b/flake.nix index 543f8c3..d0d3b65 100644 --- a/flake.nix +++ b/flake.nix @@ -3,16 +3,21 @@ nixpkgs.url = "github:nixos/nixpkgs"; }; - outputs = { self, nixpkgs }: let + outputs = { + self, + nixpkgs, + }: let inherit (nixpkgs) lib; systems = ["x86_64-linux"]; - eachSystem = f: lib.genAttrs systems (system: f { - inherit system; - pkgs = nixpkgs.legacyPackages.${system}; - ownPkgs = self.packages.${system}; - }); + eachSystem = f: + lib.genAttrs systems (system: + f { + inherit system; + pkgs = nixpkgs.legacyPackages.${system}; + ownPkgs = self.packages.${system}; + }); in { - packages = eachSystem ({ pkgs, ... }: { + packages = eachSystem ({pkgs, ...}: { default = pkgs.rustPlatform.buildRustPackage (final: { pname = "zathura-typst"; version = "0.1.0"; @@ -48,10 +53,22 @@ }); }); - devShells = eachSystem ({ pkgs, ownPkgs, ... }: { + devShells = eachSystem ({ + pkgs, + ownPkgs, + ... + }: { default = pkgs.mkShell { inputsFrom = [ownPkgs.default]; - packages = [pkgs.rust-analyzer pkgs.clippy pkgs.rustfmt pkgs.gdb]; + packages = [ + pkgs.rust-analyzer + pkgs.clippy + pkgs.rustfmt + pkgs.gdb + (pkgs.zathuraPkgs.zathura_core.overrideAttrs { + mesonBuildType = "debug"; + }) + ]; LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib"; shellHook = '' # From: https://github.com/NixOS/nixpkgs/blob/1fab95f5190d087e66a3502481e34e15d62090aa/pkgs/applications/networking/browsers/firefox/common.nix#L247-L253 diff --git a/src/lib.rs b/src/lib.rs index c5067bc..0c508c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ use std::{ cmp::{max, min}, - ffi::{CStr, CString, OsStr, c_void}, + ffi::{CStr, OsStr, c_void}, fs, os::unix::ffi::OsStrExt, path::Path, @@ -41,6 +41,17 @@ mod typst; mod zathura; use typst::TypstDocument; +unsafe extern "C" { + safe fn calloc(n: usize, s: usize) -> *mut c_void; +} + +fn alloc_c_string(bytes: &[u8]) -> *mut i8 { + let cstr = calloc(bytes.len() + 1, 1); + unsafe { slice::from_raw_parts_mut(cstr as *mut u8, bytes.len()) } + .copy_from_slice(bytes); + cstr as *mut i8 +} + #[allow(non_upper_case_globals)] #[unsafe(no_mangle)] pub static zathura_plugin_6_7: zathura::zathura_plugin_definition_s = @@ -151,15 +162,7 @@ unsafe extern "C" fn document_get_information( ] .into_iter() .filter(|(_, st)| !st.is_empty()) - .map(|(ty, st)| { - unsafe extern "C" { - safe fn calloc(n: usize, s: usize) -> *mut c_void; - } - let st_m = calloc(st.len() + 1, 1); - unsafe { slice::from_raw_parts_mut(st_m as *mut u8, st.len()) } - .copy_from_slice(st.as_bytes()); - (ty, st_m as *const i8) - }) + .map(|(ty, st)| (ty, alloc_c_string(st.as_bytes()))) .for_each(|(ty, st)| { unsafe { lst.append_allocated(zathura_document_information_entry_new(ty, st)) }; }); @@ -447,13 +450,7 @@ unsafe extern "C" fn page_links_get( zathura_link_target_s { destination_type: zathura_link_destination_type_e_ZATHURA_LINK_DESTINATION_UNKNOWN, - value: doc - .cstring_cache - .entry(url.to_string()) - .or_insert_with(|| { - CString::new(url.as_bytes()).expect("URL shouldn't contain NUL") - }) - .as_ptr() as *mut i8, + value: alloc_c_string(url.as_bytes()), page_number: 0, left: -1., right: -1., @@ -506,13 +503,10 @@ unsafe extern "C" fn page_links_get( } unsafe extern "C" fn page_get_label( - zpage: *mut zathura_page_t, + _zpage: *mut zathura_page_t, page: *mut c_void, out: *mut *mut i8, ) -> ZathuraResult { - let doc = unsafe { - &mut *(zathura_document_get_data(zathura_page_get_document(zpage)) as *mut TypstDocument) - }; let page = unsafe { &*(page as *mut Page) }; let numbering = match page.numbering.clone() { Some(Numbering::Pattern(numbering)) => numbering, @@ -520,11 +514,7 @@ unsafe extern "C" fn page_get_label( }; let label = numbering.apply(&[page.number]); - let label = doc - .cstring_cache - .entry(label.to_string()) - .or_insert_with_key(|k| CString::new(k.to_owned()).expect("It's a string")) - .as_ptr(); + let label = alloc_c_string(label.as_bytes()); unsafe { out.write(label as _) }; ZathuraResult::OK diff --git a/src/typst.rs b/src/typst.rs index c89a98f..5104871 100644 --- a/src/typst.rs +++ b/src/typst.rs @@ -1,5 +1,5 @@ use std::{ - collections::{hash_map::Entry, HashMap}, ffi::CString, hash::Hash, io::{self, ErrorKind}, iter, path::{Path, PathBuf}, slice, sync::{LazyLock, Mutex} + collections::{hash_map::Entry, HashMap}, hash::Hash, io::{self, ErrorKind}, iter, path::{Path, PathBuf}, slice, sync::{LazyLock, Mutex} }; use codespan_reporting::{ @@ -131,7 +131,6 @@ impl World { pub struct TypstDocument { pub doc: typst::layout::PagedDocument, pub warnings: Vec, - pub cstring_cache: HashMap, } #[allow(clippy::result_large_err)] @@ -147,7 +146,6 @@ fn compile(path: &Path) -> Result, Vec Ok(TypstDocument { doc, warnings: warnings.to_vec(), - cstring_cache: Default::default(), }), Err(mut errors) => { errors.extend(warnings); @@ -279,7 +277,6 @@ fn diagnostics(world: &World, errors: impl IntoIterator TypstDocument { doc: output.expect("Should be valid syntax"), warnings: vec![], - cstring_cache: Default::default() } }