Skip to main content

easyofd_reader/keyword/
keyword_resource.rs

1//! 文本资源(关键字搜索上下文)。
2//!
3//! 对应 Java: org.ofdrw.reader.keyword.KeywordResource
4
5/// 文本资源,描述关键字搜索过程中的字体和文本上下文。
6///
7/// 对应 Java: `org.ofdrw.reader.keyword.KeywordResource`
8///
9/// 在关键字搜索时,需要字体信息来计算字符宽度,从而确定关键字的
10/// 精确矩形区域。
11#[derive(Debug, Clone)]
12pub struct KeywordResource {
13    /// 页码(从 1 开始)。
14    pub page: usize,
15    /// 字体引用 ID。
16    pub font_id: Option<String>,
17    /// 字体大小(毫米)。
18    pub font_size: Option<f64>,
19}
20
21impl KeywordResource {
22    /// 创建新的文本资源。
23    #[must_use]
24    pub fn new(page: usize) -> Self {
25        Self {
26            page,
27            font_id: None,
28            font_size: None,
29        }
30    }
31
32    /// 设置字体引用 ID。
33    #[must_use]
34    pub fn with_font_id(mut self, font_id: impl Into<String>) -> Self {
35        self.font_id = Some(font_id.into());
36        self
37    }
38
39    /// 设置字体大小。
40    #[must_use]
41    pub fn with_font_size(mut self, size: f64) -> Self {
42        self.font_size = Some(size);
43        self
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_keyword_resource_new() {
53        let res = KeywordResource::new(1);
54        assert_eq!(res.page, 1);
55        assert!(res.font_id.is_none());
56        assert!(res.font_size.is_none());
57    }
58
59    #[test]
60    fn test_keyword_resource_with_font() {
61        let res = KeywordResource::new(2)
62            .with_font_id("font_0")
63            .with_font_size(12.0);
64        assert_eq!(res.font_id.as_deref(), Some("font_0"));
65        assert!((res.font_size.unwrap() - 12.0).abs() < f64::EPSILON);
66    }
67}