image_renderer/
lib.rs

1//! Image Processing Library
2//!
3//! 一个功能完整的图片处理库,参考 skia-safe API 风格设计
4//!
5//! # 主要功能
6//!
7//! - 图片加载、保存和基本操作
8//! - 文本渲染(支持自定义字体、系统字体、大小、颜色、加粗、斜体)
9//! - 图形绘制(矩形、圆形、椭圆、线条等)
10//! - 图片渲染和变换
11//!
12//! # 示例
13//!
14//! ```no_run
15//! use image_renderer::{Canvas, Color, Paint, Point, Rect, Font, TextStyle};
16//!
17//! // 创建画布
18//! let mut canvas = Canvas::new(800, 600).unwrap();
19//!
20//! // 清空为白色背景
21//! canvas.clear(Color::WHITE);
22//!
23//! // 绘制矩形
24//! let rect = Rect::new(100, 100, 200, 150);
25//! let mut paint = Paint::fill(Color::BLUE);
26//! canvas.draw_rect(rect, &paint);
27//!
28//! // 绘制文本
29//! let font = Font::default_system_font().unwrap();
30//! let text_style = TextStyle::new(font, 24.0);
31//! paint.set_color(Color::BLACK);
32//! canvas.draw_text("Hello, World!", Point::new(100, 50), &text_style, &paint);
33//!
34//! // 保存图片
35//! canvas.save("output.png").unwrap();
36//! ```
37
38pub mod border;
39pub mod canvas;
40pub mod clip;
41pub mod color;
42pub mod error;
43pub mod font;
44pub mod image;
45pub mod matrix;
46pub mod paint;
47pub mod path;
48pub mod pixmap;
49pub mod point;
50pub mod rect;
51pub mod surface;
52
53// 重新导出主要类型
54pub use border::{Border, BorderRadius, BorderSide};
55pub use canvas::Canvas;
56pub use clip::{ClipOp, ClipRegion, ClipStack};
57pub use color::Color;
58pub use error::{DrawError, FontError, ImageError, Result};
59pub use font::{Font, FontStyle, FontWeight, TextStyle};
60pub use image::{Image, ImageFormat};
61pub use matrix::{CanvasState, Matrix};
62pub use paint::{DashStyle, FilterMode, Paint, PaintStyle, StrokeCap, StrokeJoin};
63pub use path::{Path, PathCommand, PathDirection, PathFillType};
64pub use pixmap::Pixmap;
65pub use point::{IPoint, Point, PointF};
66pub use rect::{IRect, Rect, RectF};
67pub use surface::Surface;
68
69/// 库版本号
70pub const VERSION: &str = env!("CARGO_PKG_VERSION");
71
72/// 库名称
73pub const NAME: &str = env!("CARGO_PKG_NAME");
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78
79    #[test]
80    fn test_create_canvas() {
81        let canvas = Canvas::new(100, 100);
82        assert!(canvas.is_ok());
83        let canvas = canvas.unwrap();
84        assert_eq!(canvas.width(), 100);
85        assert_eq!(canvas.height(), 100);
86    }
87
88    #[test]
89    fn test_color_creation() {
90        let color = Color::rgb(255, 0, 0);
91        assert_eq!(color.r, 255);
92        assert_eq!(color.g, 0);
93        assert_eq!(color.b, 0);
94        assert_eq!(color.a, 255);
95    }
96
97    #[test]
98    fn test_color_from_hex() {
99        let color = Color::from_hex("#FF0000").unwrap();
100        assert_eq!(color.r, 255);
101        assert_eq!(color.g, 0);
102        assert_eq!(color.b, 0);
103
104        let color = Color::from_hex("#00FF00FF").unwrap();
105        assert_eq!(color.r, 0);
106        assert_eq!(color.g, 255);
107        assert_eq!(color.b, 0);
108        assert_eq!(color.a, 255);
109    }
110
111    #[test]
112    fn test_point_creation() {
113        let point = Point::new(10, 20);
114        assert_eq!(point.x, 10);
115        assert_eq!(point.y, 20);
116    }
117
118    #[test]
119    fn test_rect_creation() {
120        let rect = Rect::new(10, 20, 100, 50);
121        assert_eq!(rect.x, 10);
122        assert_eq!(rect.y, 20);
123        assert_eq!(rect.width, 100);
124        assert_eq!(rect.height, 50);
125    }
126
127    #[test]
128    fn test_paint_creation() {
129        let paint = Paint::fill(Color::RED);
130        assert_eq!(paint.color(), Color::RED);
131        assert_eq!(paint.style(), PaintStyle::Fill);
132    }
133
134    #[test]
135    fn test_surface_creation() {
136        let surface = Surface::new(100, 100);
137        assert!(surface.is_ok());
138        let surface = surface.unwrap();
139        assert_eq!(surface.width(), 100);
140        assert_eq!(surface.height(), 100);
141    }
142}