Skip to main content

cdrsdk/text/
characters.rs

1//! `IVGTextCharacters` —— 字符视图
2
3use wincom::{ComObject, Variant};
4use windows::Win32::System::Com::IDispatch;
5
6use crate::text::IvgTextRange;
7
8/// CorelDRAW `IVGTextCharacters` 对象的后期绑定包装(文本字符集)。
9///
10/// 所有属性与方法通过 `IDispatch::Invoke` 按名称调用,调用失败时一般返回 `None` 或 `false`。
11pub struct IvgTextCharacters {
12    disp: ComObject,
13}
14
15impl IvgTextCharacters {
16    /// 由 COM `IDispatch` 构造包装对象。
17    pub fn new(disp: IDispatch) -> Self {
18        Self { disp: ComObject::from_dispatch(disp, crate::types::IID_IDISPATCH) }
19    }
20
21    /// 从 COM `IDispatch` 构造包装对象,等价于 `new`。
22    pub fn from_disp(disp: IDispatch) -> Self {
23        Self::new(disp)
24    }
25
26    fn prop_i64(&self, name: &str) -> Option<i64> {
27        self.disp.get_property(name).ok()?.to_i64().ok()
28    }
29
30    fn prop_dispatch(&self, name: &str) -> Option<IDispatch> {
31        self.disp.get_property(name).ok()?.to_idispatch().ok()
32    }
33
34    /// 读取返回条目数量(`Count`)。
35    pub fn count(&self) -> Option<i64> { self.prop_i64("Count") }
36
37    /// 取从 index 开始的 count 个字符。
38    pub fn item(&self, index: i32, count: i32) -> Option<IvgTextRange> {
39        let args = vec![
40            Variant::from_i64(index as i64),
41            Variant::from_i64(count as i64),
42        ];
43        self.disp
44            .invoke_method("Item", args)
45            .ok()?
46            .to_idispatch()
47            .ok()
48            .map(IvgTextRange::new)
49    }
50
51    /// 读取返回全部条目(`All`)。
52    pub fn all(&self) -> Option<IvgTextRange> {
53        self.prop_dispatch("All").map(IvgTextRange::new)
54    }
55
56    /// 读取返回第一个条目(`First`)。
57    pub fn first(&self) -> Option<IvgTextRange> {
58        self.prop_dispatch("First").map(IvgTextRange::new)
59    }
60
61    /// 读取返回最后一个条目(`Last`)。
62    pub fn last(&self) -> Option<IvgTextRange> {
63        self.prop_dispatch("Last").map(IvgTextRange::new)
64    }
65}