chore: initial commit
This commit is contained in:
commit
ab93f649bd
31 changed files with 9918 additions and 0 deletions
39
src/app/document/file.rs
Normal file
39
src/app/document/file.rs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
// SPDX-License-Identifier: MPL-2.0 OR Apache-2.0
|
||||
// src/app/document/file.rs
|
||||
//
|
||||
// Opening files and dispatching to the correct concrete document type.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use anyhow::anyhow;
|
||||
|
||||
use super::portable::PortableDocument;
|
||||
use super::raster::RasterDocument;
|
||||
use super::vector::VectorDocument;
|
||||
use super::{DocumentContent, DocumentKind};
|
||||
|
||||
/// Open a document from a file path and dispatch to the correct type.
|
||||
///
|
||||
/// Raster formats are delegated to the `image` crate, which decides
|
||||
/// based on enabled codecs (e.g. default-formats).
|
||||
pub fn open_document(path: PathBuf) -> anyhow::Result<DocumentContent> {
|
||||
let kind = DocumentKind::from_path(&path)
|
||||
.ok_or_else(|| anyhow!("Unsupported document type: {:?}", path))?;
|
||||
|
||||
let content = match kind {
|
||||
DocumentKind::Raster => {
|
||||
let raster = RasterDocument::open(path)?;
|
||||
DocumentContent::Raster(raster)
|
||||
}
|
||||
DocumentKind::Vector => {
|
||||
let vector = VectorDocument::open(path)?;
|
||||
DocumentContent::Vector(vector)
|
||||
}
|
||||
DocumentKind::Portable => {
|
||||
let portable = PortableDocument::open(path)?;
|
||||
DocumentContent::Portable(portable)
|
||||
}
|
||||
};
|
||||
|
||||
Ok(content)
|
||||
}
|
||||
2
src/app/document/meta.rs
Normal file
2
src/app/document/meta.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
// SPDX-License-Identifier: MPL-2.0
|
||||
// src/app/document/meta.rs
|
||||
112
src/app/document/mod.rs
Normal file
112
src/app/document/mod.rs
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
// SPDX-License-Identifier: MPL-2.0 OR Apache-2.0
|
||||
// src/app/document/mod.rs
|
||||
//
|
||||
// Document module root: common enums and type erasure for document kinds.
|
||||
|
||||
pub mod file;
|
||||
pub mod meta;
|
||||
pub mod portable;
|
||||
pub mod raster;
|
||||
pub mod transform;
|
||||
pub mod utils;
|
||||
pub mod vector;
|
||||
|
||||
use cosmic::iced::widget::image as iced_image;
|
||||
use cosmic::iced_renderer::graphics::image::image_rs::ImageFormat as CosmicImageFormat;
|
||||
use std::fmt;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use self::portable::PortableDocument;
|
||||
use self::raster::RasterDocument;
|
||||
use self::vector::VectorDocument;
|
||||
|
||||
/// High-level classification of documents.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum DocumentKind {
|
||||
Raster,
|
||||
Vector,
|
||||
Portable,
|
||||
}
|
||||
|
||||
/// Unified document type used by the application.
|
||||
pub enum DocumentContent {
|
||||
Raster(RasterDocument),
|
||||
Vector(VectorDocument),
|
||||
Portable(PortableDocument),
|
||||
}
|
||||
|
||||
impl fmt::Debug for DocumentContent {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
DocumentContent::Raster(_) => f.write_str("DocumentContent::Raster(..)"),
|
||||
DocumentContent::Vector(_) => f.write_str("DocumentContent::Vector(..)"),
|
||||
DocumentContent::Portable(_) => f.write_str("DocumentContent::Portable(..)"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DocumentKind {
|
||||
/// Derive document kind from file extension.
|
||||
///
|
||||
/// - `pdf` => Portable
|
||||
/// - `svg` => Vector
|
||||
/// - supported image extensions (via libcosmic/image_rs ImageFormat)
|
||||
/// => Raster
|
||||
///
|
||||
/// Returns `None` if the extension is not recognized as any supported kind.
|
||||
pub fn from_path(path: &Path) -> Option<Self> {
|
||||
let ext_os = path.extension()?;
|
||||
let ext_str = ext_os.to_str()?;
|
||||
let ext_lower = ext_str.to_ascii_lowercase();
|
||||
|
||||
match ext_lower.as_str() {
|
||||
"pdf" => return Some(DocumentKind::Portable),
|
||||
"svg" => return Some(DocumentKind::Vector),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// Ask libcosmic/image_rs if this extension corresponds to a known image
|
||||
// format. If yes, we treat it as a raster document.
|
||||
if CosmicImageFormat::from_extension(ext_os).is_some() {
|
||||
return Some(DocumentKind::Raster);
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl DocumentContent {
|
||||
/// Returns a cloneable image handle for rendering.
|
||||
///
|
||||
/// This is intentionally linear: every concrete document type
|
||||
/// owns some kind of `iced_image::Handle`, and the canvas can
|
||||
/// just call `doc.handle()` without additional branching.
|
||||
pub fn handle(&self) -> iced_image::Handle {
|
||||
match self {
|
||||
DocumentContent::Raster(doc) => doc.handle.clone(),
|
||||
DocumentContent::Vector(doc) => doc.handle.clone(),
|
||||
DocumentContent::Portable(doc) => doc.handle.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the underlying filesystem path of this document, if any.
|
||||
pub fn path(&self) -> Option<&PathBuf> {
|
||||
match self {
|
||||
DocumentContent::Raster(doc) => doc.path.as_ref(),
|
||||
DocumentContent::Vector(doc) => Some(&doc.path),
|
||||
DocumentContent::Portable(doc) => Some(&doc.path),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the native dimensions (width, height) of the document in pixels.
|
||||
///
|
||||
/// For raster images this is the actual pixel size.
|
||||
/// For vector/portable documents this is the rasterized size at default DPI.
|
||||
pub fn dimensions(&self) -> (u32, u32) {
|
||||
match self {
|
||||
DocumentContent::Raster(doc) => doc.dimensions(),
|
||||
DocumentContent::Vector(doc) => doc.dimensions(),
|
||||
DocumentContent::Portable(doc) => doc.dimensions(),
|
||||
}
|
||||
}
|
||||
}
|
||||
65
src/app/document/portable.rs
Normal file
65
src/app/document/portable.rs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
// SPDX-License-Identifier: MPL-2.0 OR Apache-2.0
|
||||
// src/app/document/portable.rs
|
||||
//
|
||||
// Portable documents (e.g. PDF) – basic model and rendering stub.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use cosmic::iced::widget::image as iced_image;
|
||||
use image::{GenericImageView, DynamicImage};
|
||||
|
||||
/// Represents a portable document (PDF).
|
||||
pub struct PortableDocument {
|
||||
pub path: PathBuf,
|
||||
pub page_count: u32,
|
||||
pub current_page: u32,
|
||||
pub rotation: i32, // 0, 90, 180, 270; kept for future backend integration
|
||||
pub rendered: DynamicImage,
|
||||
pub handle: iced_image::Handle,
|
||||
// TODO: internal PDF handle from chosen backend
|
||||
}
|
||||
|
||||
impl PortableDocument {
|
||||
/// Open a portable document and render the first page.
|
||||
///
|
||||
/// Currently this uses a dummy 1x1 transparent image as placeholder.
|
||||
pub fn open(path: PathBuf) -> anyhow::Result<Self> {
|
||||
// TODO: open PDF and render first page using a proper backend.
|
||||
let dummy = DynamicImage::new_rgba8(1, 1);
|
||||
let handle = Self::build_handle(&dummy);
|
||||
|
||||
Ok(Self {
|
||||
path,
|
||||
page_count: 1, // TODO: query real page count from backend
|
||||
current_page: 0,
|
||||
rotation: 0,
|
||||
rendered: dummy,
|
||||
handle,
|
||||
})
|
||||
}
|
||||
|
||||
/// Construct an iced image handle from a DynamicImage.
|
||||
fn build_handle(img: &DynamicImage) -> iced_image::Handle {
|
||||
let (w, h) = img.dimensions();
|
||||
let rgba = img.to_rgba8();
|
||||
let pixels = rgba.into_raw();
|
||||
iced_image::Handle::from_rgba(w, h, pixels)
|
||||
}
|
||||
|
||||
/// Rebuild the handle after mutating `rendered`.
|
||||
pub fn refresh_handle(&mut self) {
|
||||
self.handle = Self::build_handle(&self.rendered);
|
||||
}
|
||||
|
||||
/// Returns the dimensions of the currently rendered page.
|
||||
pub fn dimensions(&self) -> (u32, u32) {
|
||||
self.rendered.dimensions()
|
||||
}
|
||||
|
||||
/// Re-render the current page with the current rotation.
|
||||
pub fn rerender_page(&mut self) {
|
||||
// TODO: use PDF backend and self.rotation / self.current_page
|
||||
// self.rendered = render_page_to_dynamic(...);
|
||||
// self.refresh_handle();
|
||||
}
|
||||
}
|
||||
65
src/app/document/raster.rs
Normal file
65
src/app/document/raster.rs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
// SPDX-License-Identifier: MPL-2.0
|
||||
// src/app/document/raster.rs
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use cosmic::iced::widget::image as iced_image;
|
||||
use image::{GenericImageView, DynamicImage, ImageReader};
|
||||
|
||||
/// Represents a raster image document (PNG, JPEG, WebP, ...).
|
||||
pub struct RasterDocument {
|
||||
pub path: Option<PathBuf>,
|
||||
pub image: DynamicImage,
|
||||
pub handle: iced_image::Handle,
|
||||
}
|
||||
|
||||
impl RasterDocument {
|
||||
/// Load a raster document from disk.
|
||||
pub fn open(path: PathBuf) -> image::ImageResult<Self> {
|
||||
let img = ImageReader::open(&path)?.decode()?;
|
||||
let handle = Self::build_handle(&img);
|
||||
|
||||
Ok(Self {
|
||||
path: Some(path),
|
||||
image: img,
|
||||
handle,
|
||||
})
|
||||
}
|
||||
|
||||
/// Construct a handle from a DynamicImage.
|
||||
fn build_handle(img: &DynamicImage) -> iced_image::Handle {
|
||||
// Get image dimensions.
|
||||
let (w, h) = img.dimensions();
|
||||
|
||||
// Convert to RGBA8 buffer and extract raw bytes.
|
||||
let rgba = img.to_rgba8();
|
||||
let pixels = rgba.into_raw(); // Vec<u8>
|
||||
|
||||
// Build an iced image handle from raw RGBA pixels.
|
||||
iced_image::Handle::from_rgba(w, h, pixels)
|
||||
}
|
||||
|
||||
/// Rebuild the handle after mutating `image`.
|
||||
pub fn refresh_handle(&mut self) {
|
||||
self.handle = Self::build_handle(&self.image);
|
||||
}
|
||||
|
||||
/// Returns the native pixel dimensions (width, height).
|
||||
pub fn dimensions(&self) -> (u32, u32) {
|
||||
self.image.dimensions()
|
||||
}
|
||||
|
||||
/// Save the current image back to disk (overwrite).
|
||||
pub fn save(&self) -> image::ImageResult<()> {
|
||||
if let Some(path) = &self.path {
|
||||
self.image.save(path)
|
||||
} else {
|
||||
// Cant imagine that it happen but caller should handle missing path case.
|
||||
Err(image::ImageError::Parameter(
|
||||
image::error::ParameterError::from_kind(image::error::ParameterErrorKind::Generic(
|
||||
"RasterDocument does not have a path".into(),
|
||||
)),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
112
src/app/document/transform.rs
Normal file
112
src/app/document/transform.rs
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
// SPDX-License-Identifier: MPL-2.0 OR Apache-2.0
|
||||
// src/app/document/transform.rs
|
||||
//
|
||||
// High-level document transformations (rotate, flip, etc.).
|
||||
|
||||
use image::{imageops, DynamicImage};
|
||||
|
||||
use super::portable::PortableDocument;
|
||||
use super::raster::RasterDocument;
|
||||
use super::vector::VectorDocument;
|
||||
use super::DocumentContent;
|
||||
|
||||
/// Rotate current document 90 degrees clockwise.
|
||||
pub fn rotate_cw(doc: &mut DocumentContent) {
|
||||
match doc {
|
||||
DocumentContent::Raster(raster) => rotate_cw_raster(raster),
|
||||
DocumentContent::Vector(vector) => rotate_cw_vector(vector),
|
||||
DocumentContent::Portable(portable) => rotate_cw_portable(portable),
|
||||
}
|
||||
}
|
||||
|
||||
/// Rotate current document 90 degrees counter-clockwise.
|
||||
pub fn rotate_ccw(doc: &mut DocumentContent) {
|
||||
match doc {
|
||||
DocumentContent::Raster(raster) => rotate_ccw_raster(raster),
|
||||
DocumentContent::Vector(vector) => rotate_ccw_vector(vector),
|
||||
DocumentContent::Portable(portable) => rotate_ccw_portable(portable),
|
||||
}
|
||||
}
|
||||
|
||||
/// Flip current document horizontally.
|
||||
pub fn flip_horizontal(doc: &mut DocumentContent) {
|
||||
match doc {
|
||||
DocumentContent::Raster(raster) => flip_horizontal_raster(raster),
|
||||
DocumentContent::Vector(vector) => flip_horizontal_vector(vector),
|
||||
DocumentContent::Portable(portable) => flip_horizontal_portable(portable),
|
||||
}
|
||||
}
|
||||
|
||||
/// Flip current document vertically.
|
||||
pub fn flip_vertical(doc: &mut DocumentContent) {
|
||||
match doc {
|
||||
DocumentContent::Raster(raster) => flip_vertical_raster(raster),
|
||||
DocumentContent::Vector(vector) => flip_vertical_vector(vector),
|
||||
DocumentContent::Portable(portable) => flip_vertical_portable(portable),
|
||||
}
|
||||
}
|
||||
|
||||
// --- Raster implementations ---------------------------------------------------
|
||||
|
||||
fn rotate_cw_raster(doc: &mut RasterDocument) {
|
||||
doc.image = DynamicImage::ImageRgba8(imageops::rotate90(&doc.image));
|
||||
doc.refresh_handle();
|
||||
}
|
||||
|
||||
fn rotate_ccw_raster(doc: &mut RasterDocument) {
|
||||
doc.image = DynamicImage::ImageRgba8(imageops::rotate270(&doc.image));
|
||||
doc.refresh_handle();
|
||||
}
|
||||
|
||||
fn flip_horizontal_raster(doc: &mut RasterDocument) {
|
||||
doc.image = DynamicImage::ImageRgba8(imageops::flip_horizontal(&doc.image));
|
||||
doc.refresh_handle();
|
||||
}
|
||||
|
||||
fn flip_vertical_raster(doc: &mut RasterDocument) {
|
||||
doc.image = DynamicImage::ImageRgba8(imageops::flip_vertical(&doc.image));
|
||||
doc.refresh_handle();
|
||||
}
|
||||
|
||||
// --- Portable implementations (operate on rendered image) ---------------------
|
||||
|
||||
fn rotate_cw_portable(doc: &mut PortableDocument) {
|
||||
// Keep rotation in sync for a future real PDF backend.
|
||||
doc.rotation = (doc.rotation + 90).rem_euclid(360);
|
||||
doc.rendered = DynamicImage::ImageRgba8(imageops::rotate90(&doc.rendered));
|
||||
doc.refresh_handle();
|
||||
}
|
||||
|
||||
fn rotate_ccw_portable(doc: &mut PortableDocument) {
|
||||
doc.rotation = (doc.rotation - 90).rem_euclid(360);
|
||||
doc.rendered = DynamicImage::ImageRgba8(imageops::rotate270(&doc.rendered));
|
||||
doc.refresh_handle();
|
||||
}
|
||||
|
||||
fn flip_horizontal_portable(doc: &mut PortableDocument) {
|
||||
doc.rendered = DynamicImage::ImageRgba8(imageops::flip_horizontal(&doc.rendered));
|
||||
doc.refresh_handle();
|
||||
}
|
||||
|
||||
fn flip_vertical_portable(doc: &mut PortableDocument) {
|
||||
doc.rendered = DynamicImage::ImageRgba8(imageops::flip_vertical(&doc.rendered));
|
||||
doc.refresh_handle();
|
||||
}
|
||||
|
||||
// --- Vector implementations (view-transform only, for now) --------------------
|
||||
|
||||
fn rotate_cw_vector(_doc: &mut VectorDocument) {
|
||||
// TODO: either update a rotation property or re-rasterize with rotation.
|
||||
}
|
||||
|
||||
fn rotate_ccw_vector(_doc: &mut VectorDocument) {
|
||||
// TODO: either update a rotation property or re-rasterize with rotation.
|
||||
}
|
||||
|
||||
fn flip_horizontal_vector(_doc: &mut VectorDocument) {
|
||||
// TODO: apply horizontal flip to SVG or adjust view transform.
|
||||
}
|
||||
|
||||
fn flip_vertical_vector(_doc: &mut VectorDocument) {
|
||||
// TODO: apply vertical flip to SVG or adjust view transform.
|
||||
}
|
||||
2
src/app/document/utils.rs
Normal file
2
src/app/document/utils.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
// SPDX-License-Identifier: MPL-2.0
|
||||
// src/app/document/utils.rs
|
||||
48
src/app/document/vector.rs
Normal file
48
src/app/document/vector.rs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
// SPDX-License-Identifier: MPL-2.0
|
||||
// src/app/document/vector.rs
|
||||
//
|
||||
// Vector documents (SVG, etc.).
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use cosmic::iced::widget::image as iced_image;
|
||||
|
||||
/// Represents a vector document such as SVG.
|
||||
/// For now this only stores the raw data and a rasterized handle.
|
||||
pub struct VectorDocument {
|
||||
pub path: PathBuf,
|
||||
pub raw_data: String,
|
||||
pub handle: iced_image::Handle,
|
||||
/// Cached dimensions of the rasterized representation.
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
}
|
||||
|
||||
impl VectorDocument {
|
||||
pub fn open(path: PathBuf) -> anyhow::Result<Self> {
|
||||
let raw_data = std::fs::read_to_string(&path)?;
|
||||
|
||||
// TODO: proper SVG parsing and rendering.
|
||||
// For now, use a placeholder size based on a typical default.
|
||||
let (width, height) = (800, 600);
|
||||
let handle = iced_image::Handle::from_rgba(1, 1, vec![0, 0, 0, 0]);
|
||||
|
||||
Ok(Self {
|
||||
path,
|
||||
raw_data,
|
||||
handle,
|
||||
width,
|
||||
height,
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns the dimensions of the rasterized representation.
|
||||
pub fn dimensions(&self) -> (u32, u32) {
|
||||
(self.width, self.height)
|
||||
}
|
||||
|
||||
pub fn refresh_handle(&mut self) {
|
||||
// TODO: re-render SVG to DynamicImage and rebuild handle.
|
||||
// Update self.width and self.height accordingly.
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue