cdrsdk 0.2.0

Rust SDK for CorelDRAW automation via COM IDispatch late binding
Documentation
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//! `IVGShapeRange` 鈥斺€?鍥惧舰鑼冨洿

use wincom::{ComObject, Variant};
use windows::Win32::System::Com::IDispatch;

use crate::color::IvgColor;
use crate::geometry::IvgRect;
use crate::layer::IvgLayer;
use crate::outline::IvgArrowHead;
use crate::shape::{IvgShape, IvgShapes};

pub struct IvgShapeRange {
    disp: ComObject,
}

impl IvgShapeRange {
    pub fn new(disp: IDispatch) -> Self {
        Self { disp: ComObject::from_dispatch(disp, crate::types::IID_IDISPATCH) }
    }

    pub fn from_disp(disp: IDispatch) -> Self {
        Self::new(disp)
    }

    pub fn as_variant(&self) -> Variant {
        self.disp.as_variant()
    }

    pub fn raw(&self) -> &ComObject {
        &self.disp
    }

    fn prop_i64(&self, name: &str) -> Option<i64> {
        self.disp.get_property(name).ok()?.to_i64().ok()
    }

    fn prop_f64(&self, name: &str) -> Option<f64> {
        self.disp.get_property(name).ok()?.to_f64().ok()
    }

    fn prop_dispatch(&self, name: &str) -> Option<IDispatch> {
        self.disp.get_property(name).ok()?.to_idispatch().ok()
    }

    fn put_f64(&self, name: &str, v: f64) -> bool {
        let arg = Variant::from_f64(v);
        self.disp.set_property(name, vec![arg]).is_ok()
    }

    // fn put_i64(&self, name: &str, v: i64) -> bool {
    //     let arg = Variant::from_i64(v);
    //     self.disp.set_property(name, vec![arg]).is_ok()
    // }

    // ---- 鍩烘湰 ----

    pub fn count(&self) -> Option<i64> { self.prop_i64("Count") }
    pub fn shapes(&self) -> Option<IvgShapes> {
        self.prop_dispatch("Shapes").map(IvgShapes::new)
    }
    pub fn first_shape(&self) -> Option<IvgShape> {
        self.prop_dispatch("FirstShape").map(IvgShape::new)
    }
    pub fn last_shape(&self) -> Option<IvgShape> {
        self.prop_dispatch("LastShape").map(IvgShape::new)
    }

    pub fn item(&self, index_or_name: Variant) -> Option<IvgShape> {
        let args = vec![index_or_name];
        self.disp
            .invoke_method("Item", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgShape::new)
    }

    pub fn item_by_index(&self, i: i32) -> Option<IvgShape> {
        self.item(Variant::from_i64(i as i64))
    }

    // ---- 鍑犱綍 ----

    pub fn position_x(&self) -> Option<f64> { self.prop_f64("PositionX") }
    pub fn set_position_x(&self, v: f64) -> bool { self.put_f64("PositionX", v) }

    pub fn position_y(&self) -> Option<f64> { self.prop_f64("PositionY") }
    pub fn set_position_y(&self, v: f64) -> bool { self.put_f64("PositionY", v) }

    pub fn size_width(&self) -> Option<f64> { self.prop_f64("SizeWidth") }
    pub fn set_size_width(&self, v: f64) -> bool { self.put_f64("SizeWidth", v) }

    pub fn size_height(&self) -> Option<f64> { self.prop_f64("SizeHeight") }
    pub fn set_size_height(&self, v: f64) -> bool { self.put_f64("SizeHeight", v) }

    pub fn left_x(&self) -> Option<f64> { self.prop_f64("LeftX") }
    pub fn right_x(&self) -> Option<f64> { self.prop_f64("RightX") }
    pub fn top_y(&self) -> Option<f64> { self.prop_f64("TopY") }
    pub fn bottom_y(&self) -> Option<f64> { self.prop_f64("BottomY") }

    pub fn center_x(&self) -> Option<f64> { self.prop_f64("CenterX") }
    pub fn set_center_x(&self, v: f64) -> bool { self.put_f64("CenterX", v) }

    pub fn center_y(&self) -> Option<f64> { self.prop_f64("CenterY") }
    pub fn set_center_y(&self, v: f64) -> bool { self.put_f64("CenterY", v) }

    pub fn bounding_box(&self) -> Option<IvgRect> {
        self.prop_dispatch("BoundingBox").map(IvgRect::new)
    }

    // ---- 鍙樻崲 ----

    pub fn move_by(&self, dx: f64, dy: f64) -> bool {
        let args = vec![
            Variant::from_f64(dx),
            Variant::from_f64(dy),
        ];
        self.disp.invoke_method("Move", args).is_ok()
    }

    pub fn rotate(&self, angle: f64) -> bool {
        let args = vec![Variant::from_f64(angle)];
        self.disp.invoke_method("Rotate", args).is_ok()
    }

    pub fn rotate_ex(&self, angle: f64, cx: f64, cy: f64) -> bool {
        let args = vec![
            Variant::from_f64(angle),
            Variant::from_f64(cx),
            Variant::from_f64(cy),
        ];
        self.disp.invoke_method("RotateEx", args).is_ok()
    }

    pub fn skew(&self, ax: f64, ay: f64) -> bool {
        let args = vec![
            Variant::from_f64(ax),
            Variant::from_f64(ay),
        ];
        self.disp.invoke_method("Skew", args).is_ok()
    }

    pub fn set_position(&self, x: f64, y: f64) -> bool {
        let args = vec![
            Variant::from_f64(x),
            Variant::from_f64(y),
        ];
        self.disp.invoke_method("SetPosition", args).is_ok()
    }

    pub fn set_size(&self, w: f64, h: f64) -> bool {
        let args = vec![
            Variant::from_f64(w),
            Variant::from_f64(h),
        ];
        self.disp.invoke_method("SetSize", args).is_ok()
    }

    pub fn flip(&self, axes: i32) -> bool {
        let args = vec![Variant::from_i64(axes as i64)];
        self.disp.invoke_method("Flip", args).is_ok()
    }

    pub fn stretch(&self, sx: f64, sy: f64, stretch_chars: bool) -> bool {
        let args = vec![
            Variant::from_f64(sx),
            Variant::from_f64(sy),
            Variant::from_bool(stretch_chars),
        ];
        self.disp.invoke_method("Stretch", args).is_ok()
    }

    // ---- 澶嶅埗 / 鍒犻櫎 ----

    pub fn duplicate(&self, dx: f64, dy: f64) -> Option<IvgShapeRange> {
        let args = vec![
            Variant::from_f64(dx),
            Variant::from_f64(dy),
        ];
        self.disp
            .invoke_method("Duplicate", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgShapeRange::new)
    }

    pub fn clone(&self, dx: f64, dy: f64) -> Option<IvgShapeRange> {
        let args = vec![
            Variant::from_f64(dx),
            Variant::from_f64(dy),
        ];
        self.disp
            .invoke_method("Clone", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgShapeRange::new)
    }

    pub fn delete(&self) -> bool {
        self.disp.invoke_method("Delete", vec![]).is_ok()
    }

    pub fn copy(&self) -> bool {
        self.disp.invoke_method("Copy", vec![]).is_ok()
    }

    pub fn cut(&self) -> bool {
        self.disp.invoke_method("Cut", vec![]).is_ok()
    }

    // ---- 缇ょ粍 ----

    pub fn group(&self) -> Option<IvgShape> {
        self.disp
            .invoke_method("Group", vec![])
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgShape::new)
    }

    pub fn ungroup(&self) -> bool {
        self.disp.invoke_method("Ungroup", vec![]).is_ok()
    }

    pub fn ungroup_all(&self) -> bool {
        self.disp.invoke_method("UngroupAll", vec![]).is_ok()
    }

    pub fn ungroup_ex(&self) -> Option<IvgShapeRange> {
        self.disp
            .invoke_method("UngroupEx", vec![])
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgShapeRange::new)
    }

    pub fn ungroup_all_ex(&self) -> Option<IvgShapeRange> {
        self.disp
            .invoke_method("UngroupAllEx", vec![])
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgShapeRange::new)
    }

    pub fn combine(&self) -> Option<IvgShape> {
        self.disp
            .invoke_method("Combine", vec![])
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgShape::new)
    }

    pub fn break_apart(&self) -> bool {
        self.disp.invoke_method("BreakApart", vec![]).is_ok()
    }

    // ---- Z 椤哄簭 ----

    pub fn order_to_front(&self) -> bool {
        self.disp.invoke_method("OrderToFront", vec![]).is_ok()
    }
    pub fn order_to_back(&self) -> bool {
        self.disp.invoke_method("OrderToBack", vec![]).is_ok()
    }
    pub fn order_reverse(&self) -> bool {
        self.disp.invoke_method("OrderReverse", vec![]).is_ok()
    }

    // ---- 闆嗗悎鎿嶄綔 ----

    pub fn add(&self, shape: &IvgShape) -> bool {
        let args = vec![shape.as_variant()];
        self.disp.invoke_method("Add", args).is_ok()
    }
    pub fn add_range(&self, other: &IvgShapeRange) -> bool {
        let args = vec![other.as_variant()];
        self.disp.invoke_method("AddRange", args).is_ok()
    }
    pub fn remove(&self, index: i32) -> bool {
        let args = vec![Variant::from_i64(index as i64)];
        self.disp.invoke_method("Remove", args).is_ok()
    }
    pub fn remove_range(&self, other: &IvgShapeRange) -> bool {
        let args = vec![other.as_variant()];
        self.disp.invoke_method("RemoveRange", args).is_ok()
    }
    pub fn remove_all(&self) -> bool {
        self.disp.invoke_method("RemoveAll", vec![]).is_ok()
    }

    pub fn index_of(&self, shape: &IvgShape) -> Option<i64> {
        let args = vec![shape.as_variant()];
        self.disp.invoke_method("IndexOf", args).ok()?.to_i64().ok()
    }

    pub fn exists(&self, shape: &IvgShape) -> Option<bool> {
        let args = vec![shape.as_variant()];
        self.disp.invoke_method("Exists", args).ok()?.to_bool().ok()
    }

    // ---- 閫夋嫨 ----

    pub fn create_selection(&self) -> bool {
        self.disp.invoke_method("CreateSelection", vec![]).is_ok()
    }
    pub fn add_to_selection(&self) -> bool {
        self.disp.invoke_method("AddToSelection", vec![]).is_ok()
    }
    pub fn remove_from_selection(&self) -> bool {
        self.disp.invoke_method("RemoveFromSelection", vec![]).is_ok()
    }

    // ---- 濉厖 / 杞粨 ----

    pub fn apply_no_fill(&self) -> bool {
        self.disp.invoke_method("ApplyNoFill", vec![]).is_ok()
    }
    pub fn apply_uniform_fill(&self, color: &IvgColor) -> bool {
        let args = vec![color.as_variant()];
        self.disp.invoke_method("ApplyUniformFill", args).is_ok()
    }

    pub fn apply_outline(&self, outline: &crate::outline::IvgOutline) -> bool {
        let args = vec![outline.as_variant()];
        self.disp.invoke_method("ApplyOutline", args).is_ok()
    }
    pub fn apply_fill(&self, fill: &crate::fill::IvgFill) -> bool {
        let args = vec![fill.as_variant()];
        self.disp.invoke_method("ApplyFill", args).is_ok()
    }

    #[allow(clippy::too_many_arguments)]
    pub fn set_outline_properties(
        &self,
        width: f64,
        style: &crate::outline::IvgOutlineStyle,
        color: &IvgColor,
        start_arrow: &IvgArrowHead,
        end_arrow: &IvgArrowHead,
        behind_fill: i32,
        scale_with_shape: i32,
        line_caps: i32,
        line_join: i32,
        nib_angle: f64,
        nib_stretch: i32,
        dash_dot_length: f64,
        pen_width: f64,
        miter_limit: f64,
    ) -> bool {
        let args = vec![
            Variant::from_f64(width),
            style.as_variant(),
            color.as_variant(),
            start_arrow.as_variant(),
            end_arrow.as_variant(),
            Variant::from_i64(behind_fill as i64),
            Variant::from_i64(scale_with_shape as i64),
            Variant::from_i64(line_caps as i64),
            Variant::from_i64(line_join as i64),
            Variant::from_f64(nib_angle),
            Variant::from_i64(nib_stretch as i64),
            Variant::from_f64(dash_dot_length),
            Variant::from_f64(pen_width),
            Variant::from_f64(miter_limit),
        ];
        self.disp.invoke_method("SetOutlineProperties", args).is_ok()
    }

    // ---- 鍙樻崲 / 杞崲 ----

    pub fn convert_to_curves(&self) -> bool {
        self.disp.invoke_method("ConvertToCurves", vec![]).is_ok()
    }

    pub fn convert_to_bitmap(
        &self,
        bit_depth: i32, grayscale: bool, dithered: bool, transparent_bg: bool,
        resolution: i32, anti_aliasing: i32, use_color_profile: bool,
        multi_channel: bool, always_overprint_black: bool, overprint_black_limit: i32,
    ) -> Option<IvgShape> {
        let args = vec![
            Variant::from_i64(bit_depth as i64),
            Variant::from_bool(grayscale),
            Variant::from_bool(dithered),
            Variant::from_bool(transparent_bg),
            Variant::from_i64(resolution as i64),
            Variant::from_i64(anti_aliasing as i64),
            Variant::from_bool(use_color_profile),
            Variant::from_bool(multi_channel),
            Variant::from_bool(always_overprint_black),
            Variant::from_i64(overprint_black_limit as i64),
        ];
        self.disp
            .invoke_method("ConvertToBitmap", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgShape::new)
    }

    // ---- 鏍峰紡 ----

    pub fn apply_style(&self, style_name: impl Into<String>) -> bool {
        let args = vec![Variant::from_str(style_name.into())];
        self.disp.invoke_method("ApplyStyle", args).is_ok()
    }

    // ---- 灞?----

    pub fn move_to_layer(&self, l: &IvgLayer) -> bool {
        let args = vec![l.as_variant()];
        self.disp.invoke_method("MoveToLayer", args).is_ok()
    }

    pub fn copy_to_layer(&self, l: &IvgLayer) -> Option<IvgShapeRange> {
        let args = vec![l.as_variant()];
        self.disp
            .invoke_method("CopyToLayer", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgShapeRange::new)
    }

    // ---- 瀵归綈 / 鍒嗗竷 ----

    pub fn align_to_shape(&self, align_type: i32, shape: &IvgShape, text_origin: i32) -> bool {
        let args = vec![
            Variant::from_i64(align_type as i64),
            shape.as_variant(),
            Variant::from_i64(text_origin as i64),
        ];
        self.disp.invoke_method("AlignToShape", args).is_ok()
    }

    pub fn align_to_page(&self, align_type: i32, text_origin: i32) -> bool {
        let args = vec![
            Variant::from_i64(align_type as i64),
            Variant::from_i64(text_origin as i64),
        ];
        self.disp.invoke_method("AlignToPage", args).is_ok()
    }

    pub fn align_to_page_center(&self, align_type: i32, text_origin: i32) -> bool {
        let args = vec![
            Variant::from_i64(align_type as i64),
            Variant::from_i64(text_origin as i64),
        ];
        self.disp.invoke_method("AlignToPageCenter", args).is_ok()
    }

    pub fn align_to_grid(&self, align_type: i32, text_origin: i32) -> bool {
        let args = vec![
            Variant::from_i64(align_type as i64),
            Variant::from_i64(text_origin as i64),
        ];
        self.disp.invoke_method("AlignToGrid", args).is_ok()
    }

    pub fn align_to_point(&self, align_type: i32, x: f64, y: f64, text_origin: i32) -> bool {
        let args = vec![
            Variant::from_i64(align_type as i64),
            Variant::from_f64(x),
            Variant::from_f64(y),
            Variant::from_i64(text_origin as i64),
        ];
        self.disp.invoke_method("AlignToPoint", args).is_ok()
    }

    pub fn distribute(&self, dist_type: i32, page_extent: bool) -> bool {
        let args = vec![
            Variant::from_i64(dist_type as i64),
            Variant::from_bool(page_extent),
        ];
        self.disp.invoke_method("Distribute", args).is_ok()
    }

    // ---- 鍏跺畠 ----

    pub fn set_bounding_box(
        &self,
        x: f64, y: f64, w: f64, h: f64,
        keep_aspect: bool, ref_point: i32,
    ) -> bool {
        let args = vec![
            Variant::from_f64(x),
            Variant::from_f64(y),
            Variant::from_f64(w),
            Variant::from_f64(h),
            Variant::from_bool(keep_aspect),
            Variant::from_i64(ref_point as i64),
        ];
        self.disp.invoke_method("SetBoundingBox", args).is_ok()
    }

    pub fn lock(&self) -> bool { self.disp.invoke_method("Lock", vec![]).is_ok() }
    pub fn unlock(&self) -> bool { self.disp.invoke_method("Unlock", vec![]).is_ok() }

    pub fn clear_transformations(&self) -> bool {
        self.disp.invoke_method("ClearTransformations", vec![]).is_ok()
    }

    pub fn flatten_effects(&self) -> bool {
        self.disp.invoke_method("FlattenEffects", vec![]).is_ok()
    }
}