x_graphics/direct2d/
paint.rs1use windows::Win32::Graphics::{
2 Direct2D::{
3 Common::D2D1_COLOR_F, ID2D1SolidColorBrush, D2D1_CAP_STYLE, D2D1_CAP_STYLE_FLAT, D2D1_CAP_STYLE_ROUND, D2D1_CAP_STYLE_SQUARE, D2D1_LINE_JOIN,
4 D2D1_LINE_JOIN_BEVEL, D2D1_LINE_JOIN_MITER, D2D1_LINE_JOIN_ROUND,
5 },
6 DirectWrite::IDWriteTextFormat,
7};
8
9use crate::{
10 brush::BrushBackend,
11 paint::{Cap, Color, Join, Paint},
12};
13
14#[derive(Clone, Debug, Default)]
15pub(crate) struct PaintCache {
16 text_format: Option<IDWriteTextFormat>,
17 text_brush: Option<ID2D1SolidColorBrush>,
18}
19
20impl PaintCache {
21 pub(crate) fn clear_font(&mut self) {
22 self.text_format = None;
23 }
24}
25
26impl<TBrush: BrushBackend> Paint<TBrush> {
27 pub(super) fn get_text_format(&mut self) -> Option<&IDWriteTextFormat> {
28 self.cache().text_format.as_ref()
29 }
30
31 pub(super) fn set_text_format(&mut self, text_format: IDWriteTextFormat) {
32 self.cache_mut().text_format = Some(text_format);
33 }
34
35 pub(super) fn get_text_brush(&self) -> Option<&ID2D1SolidColorBrush> {
36 self.cache().text_brush.as_ref()
37 }
38
39 pub(super) fn set_text_brush(&mut self, brush: ID2D1SolidColorBrush) {
40 self.cache_mut().text_brush = Some(brush);
41 }
42
43 pub(super) fn clear_text_brush(&mut self) {
44 self.cache_mut().text_brush = None;
45 }
46}
47
48impl From<Cap> for D2D1_CAP_STYLE {
49 fn from(cap: Cap) -> Self {
50 match cap {
51 Cap::Butt => D2D1_CAP_STYLE_FLAT,
52 Cap::Round => D2D1_CAP_STYLE_ROUND,
53 Cap::Square => D2D1_CAP_STYLE_SQUARE,
54 }
55 }
56}
57
58impl From<Join> for D2D1_LINE_JOIN {
59 fn from(join: Join) -> Self {
60 match join {
61 Join::Miter => D2D1_LINE_JOIN_MITER,
62 Join::Round => D2D1_LINE_JOIN_ROUND,
63 Join::Bevel => D2D1_LINE_JOIN_BEVEL,
64 }
65 }
66}
67
68impl From<Color> for D2D1_COLOR_F {
69 fn from(color: Color) -> Self {
70 let (r, g, b, a) = color.get_float_value();
71 D2D1_COLOR_F {
72 r,
73 g,
74 b,
75 a,
76 }
77 }
78}