use wincom::{ComObject, Variant};
use windows::Win32::System::Com::IDispatch;
use crate::color::IvgColor;
use crate::fill::{
IvgFountainFill, IvgHatchFill, IvgPatternFill, IvgPostScriptFill,
IvgTextureFill, IvgPSScreenOptions
};
pub struct IvgFill {
disp: ComObject,
}
impl IvgFill {
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 raw(&self) -> &ComObject {
&self.disp
}
pub fn as_variant(&self) -> Variant {
self.disp.as_variant()
}
fn prop_i64(&self, name: &str) -> Option<i64> {
self.disp.get_property(name).ok()?.to_i64().ok()
}
fn prop_dispatch(&self, name: &str) -> Option<IDispatch> {
self.disp.get_property(name).ok()?.to_idispatch().ok()
}
fn put_dispatch(&self, name: &str, d: Variant) -> bool {
self.disp.set_property(name, vec![d]).is_ok()
}
pub fn fill_type(&self) -> Option<i64> {
self.prop_i64("Type")
}
pub fn uniform_color(&self) -> Option<IvgColor> {
self.prop_dispatch("UniformColor").map(IvgColor::new)
}
pub fn set_uniform_color(&self, c: &IvgColor) -> bool {
self.put_dispatch("UniformColor", c.as_variant())
}
pub fn fountain(&self) -> Option<IvgFountainFill> {
self.prop_dispatch("Fountain").map(IvgFountainFill::new)
}
pub fn pattern(&self) -> Option<IvgPatternFill> {
self.prop_dispatch("Pattern").map(IvgPatternFill::new)
}
pub fn texture(&self) -> Option<IvgTextureFill> {
self.prop_dispatch("Texture").map(IvgTextureFill::new)
}
pub fn postscript(&self) -> Option<IvgPostScriptFill> {
self.prop_dispatch("PostScript").map(IvgPostScriptFill::new)
}
pub fn hatch(&self) -> Option<IvgHatchFill> {
self.prop_dispatch("Hatch").map(IvgHatchFill::new)
}
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()
}
#[allow(clippy::too_many_arguments)]
pub fn apply_fountain_fill(
&self,
start_color: &IvgColor,
end_color: &IvgColor,
fill_type: i32,
angle: f64,
steps: i32,
edge_pad: i32,
mid_point: i32,
blend_type: i32,
center_offset_x: f64,
center_offset_y: f64,
) -> Option<IvgFountainFill> {
let args = vec![
start_color.as_variant(),
end_color.as_variant(),
Variant::from_i64(fill_type as i64),
Variant::from_f64(angle),
Variant::from_i64(steps as i64),
Variant::from_i64(edge_pad as i64),
Variant::from_i64(mid_point as i64),
Variant::from_i64(blend_type as i64),
Variant::from_f64(center_offset_x),
Variant::from_f64(center_offset_y),
];
self.disp
.invoke_method("ApplyFountainFill", args)
.ok()?
.to_idispatch()
.ok()
.map(IvgFountainFill::new)
}
pub fn apply_pattern_fill(
&self,
pattern_type: i32,
file_name: impl Into<String>,
canvas_index: i32,
front_color: &IvgColor,
back_color: &IvgColor,
transform_with_shape: bool,
) -> Option<IvgPatternFill> {
let args = vec![
Variant::from_i64(pattern_type as i64),
Variant::from_str(file_name.into()),
Variant::from_i64(canvas_index as i64),
front_color.as_variant(),
back_color.as_variant(),
Variant::from_bool(transform_with_shape),
];
self.disp
.invoke_method("ApplyPatternFill", args)
.ok()?
.to_idispatch()
.ok()
.map(IvgPatternFill::new)
}
pub fn apply_texture_fill(
&self,
texture_name: impl Into<String>,
library_name: impl Into<String>,
) -> Option<IvgTextureFill> {
let args = vec![
Variant::from_str(texture_name.into()),
Variant::from_str(library_name.into()),
];
self.disp
.invoke_method("ApplyTextureFill", args)
.ok()?
.to_idispatch()
.ok()
.map(IvgTextureFill::new)
}
pub fn apply_postscript_fill(&self, index_or_name: Variant) -> Option<IvgPostScriptFill> {
let args = vec![index_or_name];
self.disp
.invoke_method("ApplyPostscriptFill", args)
.ok()?
.to_idispatch()
.ok()
.map(IvgPostScriptFill::new)
}
#[allow(clippy::too_many_arguments)]
pub fn apply_custom_hatch_fill(
&self,
angle: f64,
spacing: f64,
shift: f64,
origin_x: f64,
origin_y: f64,
width: f64,
color: &IvgColor,
style: &crate::outline::IvgOutlineStyle,
dash_dot_length: f64,
pen_width: f64,
back_color: &IvgColor,
transform_with_shape: bool,
scale_lines_with_shape: bool,
use_world_coordinates: bool,
fill_scale: f64,
line_scale: f64,
fill_angle: f64,
fill_skew: f64,
) -> Option<IvgHatchFill> {
let args = vec![
Variant::from_f64(angle),
Variant::from_f64(spacing),
Variant::from_f64(shift),
Variant::from_f64(origin_x),
Variant::from_f64(origin_y),
Variant::from_f64(width),
color.as_variant(),
style.as_variant(),
Variant::from_f64(dash_dot_length),
Variant::from_f64(pen_width),
back_color.as_variant(),
Variant::from_bool(transform_with_shape),
Variant::from_bool(scale_lines_with_shape),
Variant::from_bool(use_world_coordinates),
Variant::from_f64(fill_scale),
Variant::from_f64(line_scale),
Variant::from_f64(fill_angle),
Variant::from_f64(fill_skew),
];
self.disp
.invoke_method("ApplyCustomHatchFill", args)
.ok()?
.to_idispatch()
.ok()
.map(IvgHatchFill::new)
}
#[allow(clippy::too_many_arguments)]
pub fn apply_hatch_fill(
&self,
library_name: impl Into<String>,
hatch_name_or_index: Variant,
back_color: &IvgColor,
transform_with_shape: bool,
scale_lines_with_shape: bool,
use_world_coordinates: bool,
fill_scale: f64,
line_scale: f64,
fill_angle: f64,
fill_skew: f64,
) -> Option<IvgHatchFill> {
let args = vec![
Variant::from_str(library_name.into()),
hatch_name_or_index,
back_color.as_variant(),
Variant::from_bool(transform_with_shape),
Variant::from_bool(scale_lines_with_shape),
Variant::from_bool(use_world_coordinates),
Variant::from_f64(fill_scale),
Variant::from_f64(line_scale),
Variant::from_f64(fill_angle),
Variant::from_f64(fill_skew),
];
self.disp
.invoke_method("ApplyHatchFill", args)
.ok()?
.to_idispatch()
.ok()
.map(IvgHatchFill::new)
}
pub fn get_copy(&self) -> Option<IvgFill> {
self.disp
.invoke_method("GetCopy", vec![])
.ok()?
.to_idispatch()
.ok()
.map(IvgFill::new)
}
pub fn copy_assign(&self, other: &IvgFill) -> bool {
let args = vec![other.as_variant()];
self.disp.invoke_method("CopyAssign", args).is_ok()
}
pub fn compare_with(&self, other: &IvgFill) -> Option<bool> {
let args = vec![other.as_variant()];
self.disp
.invoke_method("CompareWith", args)
.ok()?
.to_bool()
.ok()
}
pub fn to_fill_string(&self) -> Option<String> {
self.disp.invoke_method("ToString", vec![]).ok()?.to_string().ok()
}
pub fn string_assign(&self, s: impl Into<String>) -> Option<bool> {
let args = vec![Variant::from_str(s.into())];
self.disp
.invoke_method("StringAssign", args)
.ok()?
.to_bool()
.ok()
}
pub fn ps_screen(&self) -> Option<IvgPSScreenOptions> {
self.prop_dispatch("PSScreen").map(IvgPSScreenOptions::new)
}
}