diff --git a/Cargo.lock b/Cargo.lock index 9c61c1a..74c3104 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -67,9 +67,9 @@ checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" [[package]] name = "cc" -version = "1.11.1" +version = "1.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ff2a8126fff0f8c839080d619675a585e61720bb12d2baceae45769fd66f155" +checksum = "33ef9e3e8154a45c7a95cff4c1299702106ad94062d7bb85fdc5d3a5afd91d45" dependencies = [ "find-msvc-tools", "shlex", @@ -137,9 +137,9 @@ checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be" [[package]] name = "errno" -version = "0.3.14" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +checksum = "e35d6e78f4cebb90e51d543db8fe41528851d6deeef6ef21ebd4bfdc1e784776" dependencies = [ "libc", "windows-sys", @@ -299,9 +299,9 @@ checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" [[package]] name = "libloading" -version = "0.9.0" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "754ca22de805bb5744484a5b151a9e1a8e837d5dc232c2d7d8c2e3492edc8b60" +checksum = "a1832accd5a4ca2795ace4d05d15c4039be15b62d3b950eff99746a990e3cc10" dependencies = [ "cfg-if", "windows-link", diff --git a/Cargo.toml b/Cargo.toml index f0b4793..8db907c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "kkpdf-zed" version = "0.1.0" -edition = "2021" -rust-version = "1.80.0" +edition = "2024" +rust-version = "1.88.0" authors = ["Kushagra Kumar "] license = "Apache-2.0 OR GPL-3.0-or-later" description = "High-performance native PDF viewing engine and GPUI workspace item for Zed Editor" diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..73cb934 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,3 @@ +[toolchain] +channel = "stable" +components = ["rustfmt", "clippy"] diff --git a/src/document.rs b/src/document.rs index 2323603..52d20ac 100644 --- a/src/document.rs +++ b/src/document.rs @@ -115,15 +115,15 @@ impl PdfDocument { /// Document title if available in metadata, or falls back to filename. pub fn display_title(&self) -> String { - if let Some(ref title) = self.title { - if !title.trim().is_empty() { - return title.clone(); - } + if let Some(ref title) = self.title + && !title.trim().is_empty() + { + return title.clone(); } - if let Some(ref path) = self.path { - if let Some(file_name) = path.file_name() { - return file_name.to_string_lossy().to_string(); - } + if let Some(ref path) = self.path + && let Some(file_name) = path.file_name() + { + return file_name.to_string_lossy().to_string(); } "Untitled Document.pdf".to_string() } diff --git a/src/lib.rs b/src/lib.rs index e716e1c..bb35b74 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ pub mod ui; pub mod view; pub mod watcher; -pub use cache::{CacheKey, PageLruCache, RenderedPage, DEFAULT_MEMORY_BUDGET_BYTES}; +pub use cache::{CacheKey, DEFAULT_MEMORY_BUDGET_BYTES, PageLruCache, RenderedPage}; pub use document::{PageDimensions, PdfDocument}; pub use pdfium::{ PdfDocumentDetails, PdfLinkAnnotation, PdfPageDetails, PdfTextSegment, PdfiumEngine, @@ -27,5 +27,5 @@ pub use pdfium::{ pub use rasterizer::{LuminosityToneMapper, PageRasterizer, RasterizerOptions}; pub use settings::{DefaultZoomPolicy, PageLayoutMode, PdfViewerSettings}; pub use ui::{PdfToolbarAction, PdfToolbarState}; -pub use view::{PdfView, MAX_ZOOM, MIN_ZOOM, PAGE_SPACING_PX, ZOOM_STEP}; +pub use view::{MAX_ZOOM, MIN_ZOOM, PAGE_SPACING_PX, PdfView, ZOOM_STEP}; pub use watcher::{PdfReloadDebouncer, ViewerStateSnapshot}; diff --git a/src/pdfium/links.rs b/src/pdfium/links.rs index 5fde408..3783640 100644 --- a/src/pdfium/links.rs +++ b/src/pdfium/links.rs @@ -18,22 +18,22 @@ pub fn extract_page_links(page: &PdfPage, page_w: f32, page_h: f32) -> Vec 0.0 && norm_h > 0.0 { - links.push(PdfLinkAnnotation { - url: url_clean, - x: norm_x, - y: norm_y, - width: norm_w, - height: norm_h, - }); - } - } + if let Some(PdfAction::Uri(uri_action)) = link.action() + && let Ok(url) = uri_action.uri() + { + let url_clean = url.trim().to_string(); + if !url_clean.is_empty() + && let Ok(rect) = link.rect() + { + let (norm_x, norm_y, norm_w, norm_h) = normalize_rect(rect, page_w, page_h); + if norm_w > 0.0 && norm_h > 0.0 { + links.push(PdfLinkAnnotation { + url: url_clean, + x: norm_x, + y: norm_y, + width: norm_w, + height: norm_h, + }); } } } @@ -41,33 +41,30 @@ pub fn extract_page_links(page: &PdfPage, page_w: f32, page_h: f32) -> Vec 0.0 && norm_h > 0.0 { - let exists = links.iter().any(|l| { - l.url == url_clean - && (l.x - norm_x).abs() < 0.01 - && (l.y - norm_y).abs() < 0.01 - }); - if !exists { - links.push(PdfLinkAnnotation { - url: url_clean, - x: norm_x, - y: norm_y, - width: norm_w, - height: norm_h, - }); - } - } - } - } + if let Some(link_annot) = annot.as_link_annotation() + && let Ok(link) = link_annot.link() + && let Some(PdfAction::Uri(uri_action)) = link.action() + && let Ok(url) = uri_action.uri() + { + let url_clean = url.trim().to_string(); + if !url_clean.is_empty() + && let Ok(rect) = link.rect() + { + let (norm_x, norm_y, norm_w, norm_h) = normalize_rect(rect, page_w, page_h); + if norm_w > 0.0 && norm_h > 0.0 { + let exists = links.iter().any(|l| { + l.url == url_clean + && (l.x - norm_x).abs() < 0.01 + && (l.y - norm_y).abs() < 0.01 + }); + if !exists { + links.push(PdfLinkAnnotation { + url: url_clean, + x: norm_x, + y: norm_y, + width: norm_w, + height: norm_h, + }); } } } diff --git a/src/pdfium/loader.rs b/src/pdfium/loader.rs index a4092bc..7afa0d6 100644 --- a/src/pdfium/loader.rs +++ b/src/pdfium/loader.rs @@ -13,14 +13,14 @@ pub fn discover_and_bind_pdfium() -> Option { // 1. Explicit environment variable PDFIUM_LIB_PATH (points directly to library file) if let Ok(env_path_str) = std::env::var("PDFIUM_LIB_PATH") { let env_path = Path::new(&env_path_str); - if env_path.is_file() { - if let Ok(bindings) = Pdfium::bind_to_library(env_path) { - log::info!( - "Bound to Pdfium via PDFIUM_LIB_PATH: {}", - env_path.display() - ); - return Some(Pdfium::new(bindings)); - } + if env_path.is_file() + && let Ok(bindings) = Pdfium::bind_to_library(env_path) + { + log::info!( + "Bound to Pdfium via PDFIUM_LIB_PATH: {}", + env_path.display() + ); + return Some(Pdfium::new(bindings)); } } @@ -30,14 +30,14 @@ pub fn discover_and_bind_pdfium() -> Option { if env_dir.is_dir() { for filename in &["libpdfium.so", "libpdfium.dylib", "pdfium.dll"] { let candidate = env_dir.join(filename); - if candidate.is_file() { - if let Ok(bindings) = Pdfium::bind_to_library(&candidate) { - log::info!( - "Bound to Pdfium via PDFIUM_LIB_DIR: {}", - candidate.display() - ); - return Some(Pdfium::new(bindings)); - } + if candidate.is_file() + && let Ok(bindings) = Pdfium::bind_to_library(&candidate) + { + log::info!( + "Bound to Pdfium via PDFIUM_LIB_DIR: {}", + candidate.display() + ); + return Some(Pdfium::new(bindings)); } } } @@ -51,36 +51,36 @@ pub fn discover_and_bind_pdfium() -> Option { // 4. Secure executable-relative paths via std::env::current_exe() let mut candidate_paths: Vec = Vec::new(); - if let Ok(current_exe) = std::env::current_exe() { - if let Some(exe_dir) = current_exe.parent() { - for filename in &["libpdfium.so", "libpdfium.dylib", "pdfium.dll"] { - candidate_paths.push(exe_dir.join(filename)); - candidate_paths.push(exe_dir.join("lib").join(filename)); - } + if let Ok(current_exe) = std::env::current_exe() + && let Some(exe_dir) = current_exe.parent() + { + for filename in &["libpdfium.so", "libpdfium.dylib", "pdfium.dll"] { + candidate_paths.push(exe_dir.join(filename)); + candidate_paths.push(exe_dir.join("lib").join(filename)); } } // 5. User-level standard data directories (~/.local/share/kkpdf-zed/lib, ~/.local/lib, %LOCALAPPDATA%) - if let Ok(home_dir) = std::env::var("HOME").or_else(|_| std::env::var("USERPROFILE")) { - if !home_dir.trim().is_empty() { - let home = Path::new(&home_dir); - for filename in &["libpdfium.so", "libpdfium.dylib", "pdfium.dll"] { - candidate_paths.push(home.join(".local/share/kkpdf-zed/lib").join(filename)); - candidate_paths.push(home.join(".local/lib").join(filename)); - } + if let Ok(home_dir) = std::env::var("HOME").or_else(|_| std::env::var("USERPROFILE")) + && !home_dir.trim().is_empty() + { + let home = Path::new(&home_dir); + for filename in &["libpdfium.so", "libpdfium.dylib", "pdfium.dll"] { + candidate_paths.push(home.join(".local/share/kkpdf-zed/lib").join(filename)); + candidate_paths.push(home.join(".local/lib").join(filename)); } } - if let Ok(appdata) = std::env::var("LOCALAPPDATA") { - if !appdata.trim().is_empty() { - let appdata_path = Path::new(&appdata); - candidate_paths.push( - appdata_path - .join("kkpdf-zed") - .join("lib") - .join("pdfium.dll"), - ); - candidate_paths.push(appdata_path.join("kkpdf-zed").join("pdfium.dll")); - } + if let Ok(appdata) = std::env::var("LOCALAPPDATA") + && !appdata.trim().is_empty() + { + let appdata_path = Path::new(&appdata); + candidate_paths.push( + appdata_path + .join("kkpdf-zed") + .join("lib") + .join("pdfium.dll"), + ); + candidate_paths.push(appdata_path.join("kkpdf-zed").join("pdfium.dll")); } // 6. Well-known operating system shared library locations @@ -104,11 +104,11 @@ pub fn discover_and_bind_pdfium() -> Option { // Test discovered candidate paths for path in &candidate_paths { - if path.is_file() { - if let Ok(bindings) = Pdfium::bind_to_library(path) { - log::info!("Successfully bound to Pdfium at {}", path.display()); - return Some(Pdfium::new(bindings)); - } + if path.is_file() + && let Ok(bindings) = Pdfium::bind_to_library(path) + { + log::info!("Successfully bound to Pdfium at {}", path.display()); + return Some(Pdfium::new(bindings)); } } diff --git a/src/pdfium/mod.rs b/src/pdfium/mod.rs index 9f425b3..1466f58 100644 --- a/src/pdfium/mod.rs +++ b/src/pdfium/mod.rs @@ -21,8 +21,8 @@ use crate::rasterizer::RasterizerOptions; pub use links::PdfLinkAnnotation; pub use render::{ - PdfDocumentRenderResult, PdfPageRenderResult, MAX_COMPOSITE_BUFFER_BYTES, MAX_COMPOSITE_HEIGHT, - MAX_COMPOSITE_PAGES, MAX_PAGE_DIMENSION, + MAX_COMPOSITE_BUFFER_BYTES, MAX_COMPOSITE_HEIGHT, MAX_COMPOSITE_PAGES, MAX_PAGE_DIMENSION, + PdfDocumentRenderResult, PdfPageRenderResult, }; pub use text::PdfTextSegment; @@ -376,14 +376,18 @@ mod tests { assert!(engine.load_document_from_bytes(dummy_pdf, None).is_err()); assert!(engine.render_page_from_bytes(dummy_pdf, 0, opts).is_err()); - assert!(engine - .render_document_range_from_bytes(dummy_pdf, 0, 1, opts, 10) - .is_err()); + assert!( + engine + .render_document_range_from_bytes(dummy_pdf, 0, 1, opts, 10) + .is_err() + ); assert!(engine.extract_text_from_bytes(dummy_pdf, None).is_err()); assert!(engine.extract_document_details(dummy_pdf).is_err()); - assert!(engine - .render_and_extract_document_from_bytes(dummy_pdf, opts) - .is_err()); + assert!( + engine + .render_and_extract_document_from_bytes(dummy_pdf, opts) + .is_err() + ); } #[test] @@ -479,17 +483,23 @@ mod tests { assert!(validate_pdf_bytes(invalid_pdf).is_err()); assert!(engine.load_document_from_bytes(invalid_pdf, None).is_err()); assert!(engine.render_page_from_bytes(invalid_pdf, 0, opts).is_err()); - assert!(engine - .render_document_from_bytes(invalid_pdf, opts, 10) - .is_err()); - assert!(engine - .render_document_range_from_bytes(invalid_pdf, 0, 5, opts, 10) - .is_err()); + assert!( + engine + .render_document_from_bytes(invalid_pdf, opts, 10) + .is_err() + ); + assert!( + engine + .render_document_range_from_bytes(invalid_pdf, 0, 5, opts, 10) + .is_err() + ); assert!(engine.extract_text_from_bytes(invalid_pdf, None).is_err()); assert!(engine.extract_document_details(invalid_pdf).is_err()); - assert!(engine - .render_and_extract_document_from_bytes(invalid_pdf, opts) - .is_err()); + assert!( + engine + .render_and_extract_document_from_bytes(invalid_pdf, opts) + .is_err() + ); } #[test] @@ -500,13 +510,17 @@ mod tests { let opts = RasterizerOptions::default(); // Budget of 0 must fail immediately - assert!(engine - .render_document_range_from_bytes(dummy_pdf, 0, 0, opts, 10) - .is_err()); + assert!( + engine + .render_document_range_from_bytes(dummy_pdf, 0, 0, opts, 10) + .is_err() + ); // Normal bounded render within budget must succeed - assert!(engine - .render_document_range_from_bytes(dummy_pdf, 0, 1, opts, 10) - .is_ok()); + assert!( + engine + .render_document_range_from_bytes(dummy_pdf, 0, 1, opts, 10) + .is_ok() + ); } } diff --git a/src/pdfium/render.rs b/src/pdfium/render.rs index 2e7ebc4..4992a91 100644 --- a/src/pdfium/render.rs +++ b/src/pdfium/render.rs @@ -7,8 +7,8 @@ use crate::cache::RenderedPage; use crate::document::{PageDimensions, PdfDocument}; use crate::rasterizer::{LuminosityToneMapper, PageRasterizer, RasterizerOptions}; -use super::links::{extract_page_links, PdfLinkAnnotation}; -use super::text::{extract_page_text_and_segments, PdfTextSegment}; +use super::links::{PdfLinkAnnotation, extract_page_links}; +use super::text::{PdfTextSegment, extract_page_text_and_segments}; /// Maximum dimension (width or height) allowed when rasterizing a PDF page, /// preventing out-of-memory denial of service attacks from malicious documents. @@ -362,7 +362,7 @@ fn composite_pages( }; let mut composite = vec![0u8; total_bytes]; - for chunk in composite.chunks_exact_mut(4) { + for chunk in composite.as_chunks_mut::<4>().0 { chunk.copy_from_slice(&bg_color); } diff --git a/src/rasterizer.rs b/src/rasterizer.rs index 516c51f..4c815f3 100644 --- a/src/rasterizer.rs +++ b/src/rasterizer.rs @@ -61,7 +61,7 @@ impl LuminosityToneMapper { pub fn apply(rgba_buffer: &mut [u8], saturation_threshold: f32) { let thresh_fixed = (saturation_threshold.clamp(0.0, 1.0) * 65536.0 + 0.5) as u32; - for pixel in rgba_buffer.chunks_exact_mut(4) { + for pixel in rgba_buffer.as_chunks_mut::<4>().0 { let a = pixel[3]; if a == 0 { continue; diff --git a/src/view.rs b/src/view.rs index 1b29d83..8d3aad2 100644 --- a/src/view.rs +++ b/src/view.rs @@ -4,7 +4,7 @@ use anyhow::{Context as _, Result}; use std::path::{Path, PathBuf}; use std::sync::Arc; -use crate::cache::{CacheKey, PageLruCache, RenderedPage, DEFAULT_MEMORY_BUDGET_BYTES}; +use crate::cache::{CacheKey, DEFAULT_MEMORY_BUDGET_BYTES, PageLruCache, RenderedPage}; use crate::document::{PageDimensions, PdfDocument}; use crate::pdfium::PdfiumEngine; use crate::rasterizer::RasterizerOptions; @@ -128,12 +128,12 @@ impl PdfView { /// Restores a captured viewport state. pub fn restore_state_snapshot(&mut self, snapshot: ViewerStateSnapshot) { - if let Some(ref doc) = self.document { - if !doc.is_empty() { - self.current_page = snapshot - .current_page - .min(doc.total_pages().saturating_sub(1)); - } + if let Some(ref doc) = self.document + && !doc.is_empty() + { + self.current_page = snapshot + .current_page + .min(doc.total_pages().saturating_sub(1)); } self.zoom_level = snapshot.zoom_level.clamp(MIN_ZOOM, MAX_ZOOM); self.pan_offset = (snapshot.pan_x, snapshot.pan_y); @@ -215,29 +215,27 @@ impl PdfView { /// Adjusts zoom to fit the current page width. pub fn fit_to_width(&mut self) { - if let Some((container_w, _)) = self.container_size { - if let Some(dim) = self + if let Some((container_w, _)) = self.container_size + && let Some(dim) = self .document .as_ref() .and_then(|d| d.page_size(self.current_page)) - { - self.zoom_level = self.compute_fit_to_width_zoom(container_w, dim.width); - self.pan_offset = (0.0, 0.0); - } + { + self.zoom_level = self.compute_fit_to_width_zoom(container_w, dim.width); + self.pan_offset = (0.0, 0.0); } } /// Adjusts zoom to fit the current entire page in view. pub fn fit_to_page(&mut self) { - if let Some((container_w, container_h)) = self.container_size { - if let Some(dim) = self + if let Some((container_w, container_h)) = self.container_size + && let Some(dim) = self .document .as_ref() .and_then(|d| d.page_size(self.current_page)) - { - self.zoom_level = self.compute_fit_to_page_zoom(container_w, container_h, dim); - self.pan_offset = (0.0, 0.0); - } + { + self.zoom_level = self.compute_fit_to_page_zoom(container_w, container_h, dim); + self.pan_offset = (0.0, 0.0); } } @@ -251,28 +249,28 @@ impl PdfView { self.zoom_level = clamped; - if let Some((center_x, center_y)) = focal_center { - if let Some((cont_w, cont_h)) = self.container_size { - let rel_x = center_x - cont_w / 2.0; - let rel_y = center_y - cont_h / 2.0; + if let Some((center_x, center_y)) = focal_center + && let Some((cont_w, cont_h)) = self.container_size + { + let rel_x = center_x - cont_w / 2.0; + let rel_y = center_y - cont_h / 2.0; - let mouse_offset_x = rel_x - self.pan_offset.0; - let mouse_offset_y = rel_y - self.pan_offset.1; + let mouse_offset_x = rel_x - self.pan_offset.0; + let mouse_offset_y = rel_y - self.pan_offset.1; - let ratio = self.zoom_level / old_zoom; - self.pan_offset.0 += mouse_offset_x * (1.0 - ratio); - self.pan_offset.1 += mouse_offset_y * (1.0 - ratio); - } + let ratio = self.zoom_level / old_zoom; + self.pan_offset.0 += mouse_offset_x * (1.0 - ratio); + self.pan_offset.1 += mouse_offset_y * (1.0 - ratio); } } /// Navigates to the next page. pub fn next_page(&mut self) -> bool { - if let Some(ref doc) = self.document { - if self.current_page + 1 < doc.total_pages() { - self.current_page += 1; - return true; - } + if let Some(ref doc) = self.document + && self.current_page + 1 < doc.total_pages() + { + self.current_page += 1; + return true; } false } @@ -288,11 +286,11 @@ impl PdfView { /// Jumps directly to target page index (0-indexed). pub fn go_to_page(&mut self, page_index: usize) -> bool { - if let Some(ref doc) = self.document { - if page_index < doc.total_pages() { - self.current_page = page_index; - return true; - } + if let Some(ref doc) = self.document + && page_index < doc.total_pages() + { + self.current_page = page_index; + return true; } false } diff --git a/src/watcher.rs b/src/watcher.rs index 1262708..deb3af3 100644 --- a/src/watcher.rs +++ b/src/watcher.rs @@ -9,8 +9,8 @@ //! 3. Active page index and selection states. use std::path::{Path, PathBuf}; -use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; use std::time::{Duration, Instant}; /// Snapshot of viewer position to restore seamlessly across document reloads. @@ -85,12 +85,12 @@ impl PdfReloadDebouncer { return false; } - if let Some(last_mod) = self.last_modified { - if now.duration_since(last_mod) >= self.debounce_duration { - // Debounce timer elapsed, file has settled - self.last_modified = None; - return true; - } + if let Some(last_mod) = self.last_modified + && now.duration_since(last_mod) >= self.debounce_duration + { + // Debounce timer elapsed, file has settled + self.last_modified = None; + return true; } false