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
//! 棰滆壊绠$悊鍣?& Profile & 绠$悊绛栫暐

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

use crate::color::color::IvgColor;
use crate::color::context::IvgColorContext;

// =============================================================
// IvgColorProfile
// =============================================================

pub struct IvgColorProfile {
    disp: ComObject,
}

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

    fn prop_string(&self, name: &str) -> Option<String> {
        self.disp.get_property(name).ok()?.to_string().ok()
    }

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

    fn prop_bool(&self, name: &str) -> Option<bool> {
        self.disp.get_property(name).ok()?.to_bool().ok()
    }

    pub fn name(&self) -> Option<String> { self.prop_string("Name") }
    pub fn file_name(&self) -> Option<String> { self.prop_string("FileName") }
    pub fn manufacturer(&self) -> Option<String> { self.prop_string("Manufacturer") }
    pub fn device_model(&self) -> Option<String> { self.prop_string("DeviceModel") }
    pub fn device_type(&self) -> Option<i64> { self.prop_i64("DeviceType") }
    pub fn selected(&self) -> Option<bool> { self.prop_bool("Selected") }
    pub fn generic(&self) -> Option<bool> { self.prop_bool("Generic") }
    pub fn installed(&self) -> Option<bool> { self.prop_bool("Installed") }
    pub fn id(&self) -> Option<String> { self.prop_string("ID") }
    pub fn color_model(&self) -> Option<i64> { self.prop_i64("ColorModel") }

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

    /// 鐢ㄥ綋鍓?profile + 娓叉煋鎰忓浘鍒涘缓 ColorContext
    pub fn create_color_context(&self, rendering_intent: i32) -> Option<IvgColorContext> {
        let args = vec![Variant::from_i64(rendering_intent as i64)];
        self.disp
            .invoke_method("CreateColorContext", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgColorContext::new)
    }

    pub fn is_same(&self, other: &IvgColorProfile) -> Option<bool> {
        let args = vec![other.as_variant()];
        self.disp
            .invoke_method("IsSame", args)
            .ok()?
            .to_bool()
            .ok()
    }

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

    pub fn dispatch(&self) -> &IDispatch {
        unimplemented!("鎸?wincom 瀹為檯 API 璋冩暣")
    }
}

// =============================================================
// IvgColorProfiles
// =============================================================

pub struct IvgColorProfiles {
    disp: ComObject,
}

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

    pub fn count(&self) -> Option<i64> {
        self.disp.get_property("Count").ok()?.to_i64().ok()
    }

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

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

    pub fn item_by_name(&self, s: impl Into<String>) -> Option<IvgColorProfile> {
        self.item(Variant::from_str(s.into()))
    }

    pub fn device_type(&self) -> Option<i64> {
        self.disp.get_property("DeviceType").ok()?.to_i64().ok()
    }

    pub fn generic_profile(&self) -> Option<IvgColorProfile> {
        self.disp
            .get_property("GenericProfile")
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgColorProfile::new)
    }

    pub fn select_by_name(&self, name: impl Into<String>) -> bool {
        let args = vec![Variant::from_str(name.into())];
        self.disp
            .invoke_method("SelectByName", args)
            .ok()
            .and_then(|v| v.to_bool().ok())
            .unwrap_or(false)
    }

    pub fn install(&self, file_name: impl Into<String>) -> bool {
        let args = vec![Variant::from_str(file_name.into())];
        self.disp
            .invoke_method("Install", args)
            .ok()
            .and_then(|v| v.to_bool().ok())
            .unwrap_or(false)
    }
}

// =============================================================
// IvgColorManagementPolicy
// =============================================================

pub struct IvgColorManagementPolicy {
    disp: ComObject,
}

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

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

    fn prop_bool(&self, name: &str) -> Option<bool> {
        self.disp.get_property(name).ok()?.to_bool().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()
    }

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

    pub fn action_for_rgb(&self) -> Option<i64> { self.prop_i64("ActionForRGB") }
    pub fn set_action_for_rgb(&self, v: i32) -> bool {
        self.put_i64("ActionForRGB", v as i64)
    }

    pub fn action_for_cmyk(&self) -> Option<i64> { self.prop_i64("ActionForCMYK") }
    pub fn set_action_for_cmyk(&self, v: i32) -> bool {
        self.put_i64("ActionForCMYK", v as i64)
    }

    pub fn action_for_grayscale(&self) -> Option<i64> { self.prop_i64("ActionForGrayscale") }
    pub fn set_action_for_grayscale(&self, v: i32) -> bool {
        self.put_i64("ActionForGrayscale", v as i64)
    }

    pub fn warn_on_mismatched_profiles(&self) -> Option<bool> {
        self.prop_bool("WarnOnMismatchedProfiles")
    }
    pub fn set_warn_on_mismatched_profiles(&self, v: bool) -> bool {
        self.put_bool("WarnOnMismatchedProfiles", v)
    }

    pub fn warn_on_missing_profiles(&self) -> Option<bool> {
        self.prop_bool("WarnOnMissingProfiles")
    }
    pub fn set_warn_on_missing_profiles(&self, v: bool) -> bool {
        self.put_bool("WarnOnMissingProfiles", v)
    }
}

// =============================================================
// IvgColorManager
// =============================================================

pub struct IvgColorManager {
    disp: ComObject,
}

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

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

    fn prop_bool(&self, name: &str) -> Option<bool> {
        self.disp.get_property(name).ok()?.to_bool().ok()
    }

    fn prop_string(&self, name: &str) -> Option<String> {
        self.disp.get_property(name).ok()?.to_string().ok()
    }

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

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

    // ---------------------------------------------------------
    // 鏍″噯
    // ---------------------------------------------------------

    pub fn scanner_calibrated(&self) -> Option<bool> { self.prop_bool("ScannerCalibrated") }
    pub fn set_scanner_calibrated(&self, v: bool) -> bool { self.put_bool("ScannerCalibrated", v) }

    pub fn separation_printer_calibrated(&self) -> Option<bool> {
        self.prop_bool("SeparationPrinterCalibrated")
    }
    pub fn set_separation_printer_calibrated(&self, v: bool) -> bool {
        self.put_bool("SeparationPrinterCalibrated", v)
    }

    pub fn composite_printer_calibrated(&self) -> Option<bool> {
        self.prop_bool("CompositePrinterCalibrated")
    }

    pub fn composite_printer_calibration(&self) -> Option<i64> {
        self.prop_i64("CompositePrinterCalibration")
    }
    pub fn set_composite_printer_calibration(&self, v: i32) -> bool {
        self.put_i64("CompositePrinterCalibration", v as i64)
    }

    pub fn monitor_calibration(&self) -> Option<i64> { self.prop_i64("MonitorCalibration") }
    pub fn set_monitor_calibration(&self, v: i32) -> bool {
        self.put_i64("MonitorCalibration", v as i64)
    }

    // ---------------------------------------------------------
    // 鍑虹晫璀﹀憡
    // ---------------------------------------------------------

    pub fn show_out_of_gamut(&self) -> Option<bool> { self.prop_bool("ShowOutOfGamut") }
    pub fn set_show_out_of_gamut(&self, v: bool) -> bool { self.put_bool("ShowOutOfGamut", v) }

    pub fn out_of_gamut_color(&self) -> Option<IvgColor> {
        self.prop_dispatch("OutOfGamutColor").map(IvgColor::new)
    }

    pub fn out_of_gamut_transparency(&self) -> Option<i64> {
        self.prop_i64("OutOfGamutTransparency")
    }
    pub fn set_out_of_gamut_transparency(&self, v: i32) -> bool {
        self.put_i64("OutOfGamutTransparency", v as i64)
    }

    // ---------------------------------------------------------
    // CMYK 琛ㄧ幇
    // ---------------------------------------------------------

    pub fn cmyk_in_percents(&self) -> Option<bool> { self.prop_bool("CMYKInPercents") }
    pub fn set_cmyk_in_percents(&self, v: bool) -> bool { self.put_bool("CMYKInPercents", v) }

    pub fn cmyk_gamut_for_spot_colors(&self) -> Option<bool> {
        self.prop_bool("CMYKGamutForSpotColors")
    }
    pub fn set_cmyk_gamut_for_spot_colors(&self, v: bool) -> bool {
        self.put_bool("CMYKGamutForSpotColors", v)
    }

    pub fn rendering_intent(&self) -> Option<i64> { self.prop_i64("RenderingIntent") }
    pub fn set_rendering_intent(&self, v: i32) -> bool {
        self.put_i64("RenderingIntent", v as i64)
    }

    pub fn color_engine(&self) -> Option<i64> { self.prop_i64("ColorEngine") }
    pub fn set_color_engine(&self, v: i32) -> bool {
        self.put_i64("ColorEngine", v as i64)
    }

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

    pub fn style_count(&self) -> Option<i64> { self.prop_i64("StyleCount") }

    pub fn style_by_index(&self, index: i32) -> Option<String> {
        let args = vec![Variant::from_i64(index as i64)];
        self.disp
            .invoke_method("StyleByIndex", args)
            .ok()?
            .to_string()
            .ok()
    }

    pub fn load_style(&self, name: impl Into<String>) -> bool {
        let args = vec![Variant::from_str(name.into())];
        self.disp
            .invoke_method("LoadStyle", args)
            .ok()
            .and_then(|v| v.to_bool().ok())
            .unwrap_or(false)
    }

    pub fn delete_style(&self, name: impl Into<String>) -> bool {
        let args = vec![Variant::from_str(name.into())];
        self.disp
            .invoke_method("DeleteStyle", args)
            .ok()
            .and_then(|v| v.to_bool().ok())
            .unwrap_or(false)
    }

    pub fn save_style(&self, name: impl Into<String>) -> bool {
        let args = vec![Variant::from_str(name.into())];
        self.disp
            .invoke_method("SaveStyle", args)
            .ok()
            .and_then(|v| v.to_bool().ok())
            .unwrap_or(false)
    }

    pub fn can_delete_style(&self, name: impl Into<String>) -> Option<bool> {
        let args = vec![Variant::from_str(name.into())];
        self.disp
            .invoke_method("CanDeleteStyle", args)
            .ok()?
            .to_bool()
            .ok()
    }

    pub fn unsaved_style_name(&self) -> Option<String> {
        self.prop_string("UnsavedStyleName")
    }

    // ---------------------------------------------------------
    // Profile 鏌ヨ
    // ---------------------------------------------------------

    pub fn current_profile(&self, device_type: i32) -> Option<IvgColorProfile> {
        let args = vec![Variant::from_i64(device_type as i64)];
        self.disp
            .invoke_method("CurrentProfile", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgColorProfile::new)
    }

    pub fn installed_profiles(&self, device_type: i32) -> Option<IvgColorProfiles> {
        let args = vec![Variant::from_i64(device_type as i64)];
        self.disp
            .invoke_method("InstalledProfiles", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgColorProfiles::new)
    }

    pub fn profiles_by_color_model(&self, color_model: i32) -> Option<IvgColorProfiles> {
        let args = vec![Variant::from_i64(color_model as i64)];
        self.disp
            .invoke_method("GetProfilesByColorModel", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgColorProfiles::new)
    }

    pub fn profiles_for_device(
        &self,
        device_type: i32,
        device_name: impl Into<String>,
    ) -> Option<IvgColorProfiles> {
        let args = vec![
            Variant::from_i64(device_type as i64),
            Variant::from_str(device_name.into()),
        ];
        self.disp
            .invoke_method("GetProfilesForDevice", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgColorProfiles::new)
    }

    pub fn default_color_context(&self) -> Option<IvgColorContext> {
        self.prop_dispatch("DefaultColorContext").map(IvgColorContext::new)
    }

    pub fn color_profiles(&self) -> Option<IvgColorProfiles> {
        self.prop_dispatch("ColorProfiles").map(IvgColorProfiles::new)
    }

    pub fn monitor_color_profiles(&self) -> Option<IvgColorProfiles> {
        self.prop_dispatch("MonitorColorProfiles").map(IvgColorProfiles::new)
    }

    // ---------------------------------------------------------
    // 绛栫暐
    // ---------------------------------------------------------

    pub fn policy_for_open(&self) -> Option<IvgColorManagementPolicy> {
        self.prop_dispatch("PolicyForOpen").map(IvgColorManagementPolicy::new)
    }

    pub fn policy_for_import(&self) -> Option<IvgColorManagementPolicy> {
        self.prop_dispatch("PolicyForImport").map(IvgColorManagementPolicy::new)
    }

    // ---------------------------------------------------------
    // 瀵煎叆 / 瀵煎嚭鏍℃
    // ---------------------------------------------------------

    pub fn color_correction_on_import(&self) -> Option<i64> {
        self.prop_i64("ColorCorrectionOnImport")
    }
    pub fn set_color_correction_on_import(&self, v: i32) -> bool {
        self.put_i64("ColorCorrectionOnImport", v as i64)
    }

    pub fn color_correction_on_export(&self) -> Option<i64> {
        self.prop_i64("ColorCorrectionOnExport")
    }
    pub fn set_color_correction_on_export(&self, v: i32) -> bool {
        self.put_i64("ColorCorrectionOnExport", v as i64)
    }

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

    pub fn is_icm2_available(&self) -> Option<bool> { self.prop_bool("IsICM2Available") }
    pub fn is_composite_printer_cmyk(&self) -> Option<bool> {
        self.prop_bool("IsCompositePrinterCMYK")
    }

    pub fn map_gray_to_cmyk_black(&self) -> Option<bool> { self.prop_bool("MapGrayToCMYKBlack") }
    pub fn set_map_gray_to_cmyk_black(&self, v: bool) -> bool {
        self.put_bool("MapGrayToCMYKBlack", v)
    }

    pub fn preserve_pure_black(&self) -> Option<bool> { self.prop_bool("PreservePureBlack") }
    pub fn set_preserve_pure_black(&self, v: bool) -> bool {
        self.put_bool("PreservePureBlack", v)
    }

    pub fn spot_color_definition(&self) -> Option<i64> { self.prop_i64("SpotColorDefinition") }
    pub fn set_spot_color_definition(&self, v: i32) -> bool {
        self.put_i64("SpotColorDefinition", v as i64)
    }

    pub fn color_engine_present(&self, color_engine: i32) -> Option<bool> {
        let args = vec![Variant::from_i64(color_engine as i64)];
        self.disp
            .invoke_method("ColorEnginePresent", args)
            .ok()?
            .to_bool()
            .ok()
    }
}