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 color;
41pub mod error;
42pub mod font;
43pub mod image;
44pub mod paint;
45pub mod pixmap;
46pub mod point;
47pub mod rect;
48pub mod surface;
49
50// 重新导出主要类型
51pub use border::{Border, BorderRadius, BorderSide};
52pub use canvas::Canvas;
53pub use color::Color;
54pub use error::{DrawError, FontError, ImageError, Result};
55pub use font::{Font, FontStyle, FontWeight, TextStyle};
56pub use image::{Image, ImageFormat};
57pub use paint::{DashStyle, FilterMode, Paint, PaintStyle, StrokeCap, StrokeJoin};
58pub use pixmap::Pixmap;
59pub use point::{IPoint, Point, PointF};
60pub use rect::{IRect, Rect, RectF};
61pub use surface::Surface;
62
63/// 库版本号
64pub const VERSION: &str = env!("CARGO_PKG_VERSION");
65
66/// 库名称
67pub const NAME: &str = env!("CARGO_PKG_NAME");
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn test_create_canvas() {
75        let canvas = Canvas::new(100, 100);
76        assert!(canvas.is_ok());
77        let canvas = canvas.unwrap();
78        assert_eq!(canvas.width(), 100);
79        assert_eq!(canvas.height(), 100);
80    }
81
82    #[test]
83    fn test_color_creation() {
84        let color = Color::rgb(255, 0, 0);
85        assert_eq!(color.r, 255);
86        assert_eq!(color.g, 0);
87        assert_eq!(color.b, 0);
88        assert_eq!(color.a, 255);
89    }
90
91    #[test]
92    fn test_color_from_hex() {
93        let color = Color::from_hex("#FF0000").unwrap();
94        assert_eq!(color.r, 255);
95        assert_eq!(color.g, 0);
96        assert_eq!(color.b, 0);
97
98        let color = Color::from_hex("#00FF00FF").unwrap();
99        assert_eq!(color.r, 0);
100        assert_eq!(color.g, 255);
101        assert_eq!(color.b, 0);
102        assert_eq!(color.a, 255);
103    }
104
105    #[test]
106    fn test_point_creation() {
107        let point = Point::new(10, 20);
108        assert_eq!(point.x, 10);
109        assert_eq!(point.y, 20);
110    }
111
112    #[test]
113    fn test_rect_creation() {
114        let rect = Rect::new(10, 20, 100, 50);
115        assert_eq!(rect.x, 10);
116        assert_eq!(rect.y, 20);
117        assert_eq!(rect.width, 100);
118        assert_eq!(rect.height, 50);
119    }
120
121    #[test]
122    fn test_paint_creation() {
123        let paint = Paint::fill(Color::RED);
124        assert_eq!(paint.color(), Color::RED);
125        assert_eq!(paint.style(), PaintStyle::Fill);
126    }
127
128    #[test]
129    fn test_surface_creation() {
130        let surface = Surface::new(100, 100);
131        assert!(surface.is_ok());
132        let surface = surface.unwrap();
133        assert_eq!(surface.width(), 100);
134        assert_eq!(surface.height(), 100);
135    }
136}