micropdf 0.17.0

A pure Rust PDF library - A pure Rust PDF library with fz_/pdf_ API compatibility
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! GPU Types and Structures
//!
//! Common types used across all GPU backends.

use super::super::Handle;
// Note: Matrix is imported from crate::fitz::geometry when needed

// ============================================================================
// Enums
// ============================================================================

/// GPU backend types
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GpuBackendType {
    /// Automatically select the best available backend
    Auto = 0,
    /// OpenGL (2.1+ / ES 2.0+)
    OpenGL = 1,
    /// Vulkan (1.0+)
    Vulkan = 2,
    /// Metal (macOS/iOS)
    Metal = 3,
    /// DirectX 11
    DirectX11 = 4,
    /// DirectX 12
    DirectX12 = 5,
}

/// Texture/pixel formats
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GpuFormat {
    /// 8-bit RGBA (32 bits per pixel)
    Rgba8 = 0,
    /// 8-bit BGRA (32 bits per pixel)
    Bgra8 = 1,
    /// 8-bit RGB (24 bits per pixel)
    Rgb8 = 2,
    /// 8-bit single channel (8 bits per pixel)
    R8 = 3,
    /// 16-bit float RGBA (64 bits per pixel)
    Rgba16f = 4,
    /// 32-bit float RGBA (128 bits per pixel)
    Rgba32f = 5,
}

impl GpuFormat {
    /// Get bytes per pixel for this format
    pub fn bytes_per_pixel(&self) -> usize {
        match self {
            GpuFormat::Rgba8 | GpuFormat::Bgra8 => 4,
            GpuFormat::Rgb8 => 3,
            GpuFormat::R8 => 1,
            GpuFormat::Rgba16f => 8,
            GpuFormat::Rgba32f => 16,
        }
    }

    /// Get number of components
    pub fn components(&self) -> usize {
        match self {
            GpuFormat::Rgba8 | GpuFormat::Bgra8 | GpuFormat::Rgba16f | GpuFormat::Rgba32f => 4,
            GpuFormat::Rgb8 => 3,
            GpuFormat::R8 => 1,
        }
    }
}

/// Blend modes for compositing
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum GpuBlendMode {
    /// Normal alpha blending
    #[default]
    Normal = 0,
    /// Multiply blend mode
    Multiply = 1,
    /// Screen blend mode
    Screen = 2,
    /// Overlay blend mode
    Overlay = 3,
    /// Darken blend mode
    Darken = 4,
    /// Lighten blend mode
    Lighten = 5,
    /// Color dodge
    ColorDodge = 6,
    /// Color burn
    ColorBurn = 7,
    /// Hard light
    HardLight = 8,
    /// Soft light
    SoftLight = 9,
    /// Difference
    Difference = 10,
    /// Exclusion
    Exclusion = 11,
}

/// GPU buffer usage types
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GpuBufferUsage {
    /// Vertex data
    Vertex = 0,
    /// Index data
    Index = 1,
    /// Uniform/constant data
    Uniform = 2,
    /// General storage (SSBO)
    Storage = 3,
}

/// Shader types
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GpuShaderType {
    /// Vertex shader
    Vertex = 0,
    /// Fragment/pixel shader
    Fragment = 1,
    /// Compute shader
    Compute = 2,
}

/// Texture filtering modes
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum GpuFilter {
    /// Nearest neighbor (pixelated)
    Nearest = 0,
    /// Linear interpolation (smooth)
    #[default]
    Linear = 1,
}

/// Texture wrap modes
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum GpuWrap {
    /// Clamp to edge
    #[default]
    ClampToEdge = 0,
    /// Repeat
    Repeat = 1,
    /// Mirrored repeat
    MirroredRepeat = 2,
}

// ============================================================================
// Structures
// ============================================================================

/// GPU texture
#[derive(Debug)]
pub struct GpuTexture {
    /// Native texture handle (backend-specific)
    pub native_handle: u64,
    /// Texture width in pixels
    pub width: u32,
    /// Texture height in pixels
    pub height: u32,
    /// Pixel format
    pub format: GpuFormat,
    /// Minification filter
    pub min_filter: GpuFilter,
    /// Magnification filter
    pub mag_filter: GpuFilter,
    /// Horizontal wrap mode
    pub wrap_s: GpuWrap,
    /// Vertical wrap mode
    pub wrap_t: GpuWrap,
    /// Backend type that created this texture
    pub backend: GpuBackendType,
}

impl GpuTexture {
    /// Create a new texture descriptor
    pub fn new(width: u32, height: u32, format: GpuFormat, backend: GpuBackendType) -> Self {
        Self {
            native_handle: 0,
            width,
            height,
            format,
            min_filter: GpuFilter::Linear,
            mag_filter: GpuFilter::Linear,
            wrap_s: GpuWrap::ClampToEdge,
            wrap_t: GpuWrap::ClampToEdge,
            backend,
        }
    }

    /// Get size in bytes
    pub fn size_bytes(&self) -> usize {
        (self.width as usize) * (self.height as usize) * self.format.bytes_per_pixel()
    }
}

/// GPU shader program
#[derive(Debug)]
pub struct GpuShader {
    /// Native shader handle (backend-specific)
    pub native_handle: u64,
    /// Shader name/identifier
    pub name: String,
    /// Backend type
    pub backend: GpuBackendType,
}

impl GpuShader {
    /// Create a new shader descriptor
    pub fn new(name: impl Into<String>, backend: GpuBackendType) -> Self {
        Self {
            native_handle: 0,
            name: name.into(),
            backend,
        }
    }
}

/// GPU buffer
#[derive(Debug)]
pub struct GpuBuffer {
    /// Native buffer handle (backend-specific)
    pub native_handle: u64,
    /// Buffer size in bytes
    pub size: usize,
    /// Buffer usage
    pub usage: GpuBufferUsage,
    /// Backend type
    pub backend: GpuBackendType,
}

impl GpuBuffer {
    /// Create a new buffer descriptor
    pub fn new(size: usize, usage: GpuBufferUsage, backend: GpuBackendType) -> Self {
        Self {
            native_handle: 0,
            size,
            usage,
            backend,
        }
    }
}

/// GPU device capabilities
#[derive(Debug, Clone)]
pub struct GpuCapabilities {
    /// Maximum texture dimension (width or height)
    pub max_texture_size: u32,
    /// Maximum number of texture units
    pub max_texture_units: u32,
    /// Supports compute shaders
    pub compute_shaders: bool,
    /// Supports geometry shaders
    pub geometry_shaders: bool,
    /// Supports tessellation
    pub tessellation: bool,
    /// Maximum MSAA samples
    pub max_msaa_samples: u32,
    /// Supports float textures
    pub float_textures: bool,
    /// Supports instanced rendering
    pub instancing: bool,
    /// Available VRAM in MB (0 if unknown)
    pub vram_mb: u32,
    /// Device name
    pub device_name: String,
    /// Vendor name
    pub vendor_name: String,
    /// Driver version
    pub driver_version: String,
}

impl Default for GpuCapabilities {
    fn default() -> Self {
        Self {
            max_texture_size: 4096,
            max_texture_units: 8,
            compute_shaders: false,
            geometry_shaders: false,
            tessellation: false,
            max_msaa_samples: 1,
            float_textures: false,
            instancing: false,
            vram_mb: 0,
            device_name: String::new(),
            vendor_name: String::new(),
            driver_version: String::new(),
        }
    }
}

/// Render pass descriptor
#[derive(Debug, Clone)]
pub struct GpuRenderPass {
    /// Target texture handles
    pub color_attachments: Vec<Handle>,
    /// Depth texture handle (0 if none)
    pub depth_attachment: Handle,
    /// Clear color (RGBA)
    pub clear_color: [f32; 4],
    /// Clear depth value
    pub clear_depth: f32,
    /// Whether to clear color
    pub clear_color_enabled: bool,
    /// Whether to clear depth
    pub clear_depth_enabled: bool,
}

impl Default for GpuRenderPass {
    fn default() -> Self {
        Self {
            color_attachments: Vec::new(),
            depth_attachment: 0,
            clear_color: [1.0, 1.0, 1.0, 1.0],
            clear_depth: 1.0,
            clear_color_enabled: true,
            clear_depth_enabled: true,
        }
    }
}

/// Viewport
#[repr(C)]
#[derive(Debug, Clone, Copy, Default)]
pub struct GpuViewport {
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
    pub min_depth: f32,
    pub max_depth: f32,
}

impl GpuViewport {
    pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
        Self {
            x,
            y,
            width,
            height,
            min_depth: 0.0,
            max_depth: 1.0,
        }
    }
}

/// Scissor rect
#[repr(C)]
#[derive(Debug, Clone, Copy, Default)]
pub struct GpuScissor {
    pub x: i32,
    pub y: i32,
    pub width: u32,
    pub height: u32,
}

// ============================================================================
// Error Types
// ============================================================================

/// GPU error type
#[derive(Debug, Clone)]
pub enum GpuError {
    /// Backend not available on this system
    BackendNotAvailable(GpuBackendType),
    /// Failed to initialize GPU
    InitializationFailed(String),
    /// Shader compilation failed
    ShaderCompilationFailed(String),
    /// Out of GPU memory
    OutOfMemory,
    /// Invalid operation
    InvalidOperation(String),
    /// Feature not supported
    NotSupported(String),
    /// Device lost (GPU reset)
    DeviceLost,
    /// Generic error
    Other(String),
}

impl std::fmt::Display for GpuError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            GpuError::BackendNotAvailable(b) => write!(f, "GPU backend {:?} not available", b),
            GpuError::InitializationFailed(s) => write!(f, "GPU initialization failed: {}", s),
            GpuError::ShaderCompilationFailed(s) => write!(f, "Shader compilation failed: {}", s),
            GpuError::OutOfMemory => write!(f, "Out of GPU memory"),
            GpuError::InvalidOperation(s) => write!(f, "Invalid GPU operation: {}", s),
            GpuError::NotSupported(s) => write!(f, "GPU feature not supported: {}", s),
            GpuError::DeviceLost => write!(f, "GPU device lost"),
            GpuError::Other(s) => write!(f, "GPU error: {}", s),
        }
    }
}

impl std::error::Error for GpuError {}

/// Result type for GPU operations
pub type GpuResult<T> = Result<T, GpuError>;