Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <kkushagra86@gmail.com>"]
license = "Apache-2.0 OR GPL-3.0-or-later"
description = "High-performance native PDF viewing engine and GPUI workspace item for Zed Editor"
Expand Down
3 changes: 3 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[toolchain]
channel = "stable"
components = ["rustfmt", "clippy"]
16 changes: 8 additions & 8 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ 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,
};
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};
83 changes: 40 additions & 43 deletions src/pdfium/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,53 @@ pub fn extract_page_links(page: &PdfPage, page_w: f32, page_h: f32) -> Vec<PdfLi

// 1. Links from page.links()
for link in page.links().iter() {
if let Some(PdfAction::Uri(uri_action)) = link.action() {
if let Ok(url) = uri_action.uri() {
let url_clean = url.trim().to_string();
if !url_clean.is_empty() {
if 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,
});
}
}
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,
});
}
}
}
}

// 2. Links from page.annotations()
for annot in page.annotations().iter() {
if let Some(link_annot) = annot.as_link_annotation() {
if let Ok(link) = link_annot.link() {
if let Some(PdfAction::Uri(uri_action)) = link.action() {
if let Ok(url) = uri_action.uri() {
let url_clean = url.trim().to_string();
if !url_clean.is_empty() {
if 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,
});
}
}
}
}
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,
});
}
}
}
Expand Down
90 changes: 45 additions & 45 deletions src/pdfium/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ pub fn discover_and_bind_pdfium() -> Option<Pdfium> {
// 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));
}
}

Expand All @@ -30,14 +30,14 @@ pub fn discover_and_bind_pdfium() -> Option<Pdfium> {
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));
}
}
}
Expand All @@ -51,36 +51,36 @@ pub fn discover_and_bind_pdfium() -> Option<Pdfium> {

// 4. Secure executable-relative paths via std::env::current_exe()
let mut candidate_paths: Vec<PathBuf> = 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
Expand All @@ -104,11 +104,11 @@ pub fn discover_and_bind_pdfium() -> Option<Pdfium> {

// 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));
}
}

Expand Down
Loading
Loading