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
use crate::{
    default_transform, GlyphBrush, RawAndFormat, RawDepthStencilView, RawRenderTargetView,
};
use glyph_brush::ab_glyph::*;
use std::{hash::BuildHasher, marker::PhantomData};

/// Short-lived builder for drawing glyphs, constructed from [`GlyphBrush::use_queue`](struct.GlyphBrush.html#method.use_queue).
///
/// # Example
///
/// ```no_run
/// # fn main() -> Result<(), String> {
/// # let glyph_brush: gfx_glyph::GlyphBrush<gfx_device_gl::Resources, gfx_device_gl::Factory> = unimplemented!();
/// # let gfx_color: gfx_core::handle::RenderTargetView<gfx_device_gl::Resources, gfx::format::Srgba8> = unimplemented!();
/// # let gfx_depth: gfx_core::handle::DepthStencilView<gfx_device_gl::Resources, gfx::format::Depth> = unimplemented!();
/// # let factory: gfx_device_gl::Factory = unimplemented!();
/// # let gfx_encoder: gfx::Encoder<_, _> = factory.create_command_buffer().into();
///
/// glyph_brush
///     .use_queue()
///     .depth_target(&gfx_depth)
///     .draw(&mut gfx_encoder, &gfx_color)?;
/// # Ok(()) }
/// ```
#[must_use]
pub struct DrawBuilder<'a, F, R: gfx::Resources, GF: gfx::Factory<R>, H, DV> {
    pub(crate) brush: &'a mut GlyphBrush<R, GF, F, H>,
    pub(crate) transform: Option<[[f32; 4]; 4]>,
    pub(crate) depth_target: Option<&'a DV>,
}

impl<'a, F, R, GF, H, DV> DrawBuilder<'a, F, R, GF, H, DV>
where
    F: Font,
    R: gfx::Resources,
    GF: gfx::Factory<R>,
    H: BuildHasher,
{
    /// Use a custom position transform (e.g. a projection) replacing the [`default_transform`](fn.default_transform.html).
    ///
    /// # Example
    /// ```no_run
    /// # fn main() -> Result<(), String> {
    /// # let glyph_brush: gfx_glyph::GlyphBrush<gfx_device_gl::Resources, gfx_device_gl::Factory> = unimplemented!();
    /// # let gfx_color: gfx_core::handle::RenderTargetView<gfx_device_gl::Resources, gfx::format::Srgba8> = unimplemented!();
    /// let projection = gfx_glyph::default_transform(&gfx_color);
    ///
    /// glyph_brush.use_queue().transform(projection)
    /// # ;
    /// # Ok(()) }
    /// ```
    #[inline]
    pub fn transform<M: Into<[[f32; 4]; 4]>>(mut self, transform: M) -> Self {
        self.transform = Some(transform.into());
        self
    }

    /// Set a depth buffer target to perform depth testing against.
    ///
    /// # Example
    /// ```no_run
    /// # fn main() -> Result<(), String> {
    /// # let glyph_brush: gfx_glyph::GlyphBrush<gfx_device_gl::Resources, gfx_device_gl::Factory> = unimplemented!();
    /// # let gfx_depth: gfx_core::handle::DepthStencilView<gfx_device_gl::Resources, gfx::format::Depth> = unimplemented!();
    /// glyph_brush.use_queue().depth_target(&gfx_depth)
    /// # ;
    /// # Ok(()) }
    /// ```
    ///
    /// # Raw usage
    /// Can also be used with gfx raw depth views if necessary. The `Format` must also be provided.
    /// ```no_run
    /// # use gfx::format::{self, Formatted};
    /// # use gfx::memory::Typed;
    /// # fn main() -> Result<(), String> {
    /// # let glyph_brush: gfx_glyph::GlyphBrush<gfx_device_gl::Resources, gfx_device_gl::Factory> = unimplemented!();
    /// # let gfx_depth: gfx_core::handle::DepthStencilView<gfx_device_gl::Resources, gfx::format::Depth> = unimplemented!();
    /// # let raw_depth_view = gfx_depth.raw();
    /// glyph_brush
    ///     .use_queue()
    ///     .depth_target(&(raw_depth_view, format::Depth::get_format()))
    /// # ;
    /// # Ok(()) }
    /// ```
    #[inline]
    pub fn depth_target<D>(self, depth: &'a D) -> DrawBuilder<'a, F, R, GF, H, D> {
        let Self {
            brush, transform, ..
        } = self;
        DrawBuilder {
            depth_target: Some(depth),
            brush,
            transform,
        }
    }
}

impl<'a, F, R, GF, H, DV> DrawBuilder<'a, F, R, GF, H, DV>
where
    F: Font + Sync,
    R: gfx::Resources,
    GF: gfx::Factory<R>,
    H: BuildHasher,
    DV: RawAndFormat<Raw = RawDepthStencilView<R>>,
{
    /// Draws all queued sections onto a render target.
    /// See [`queue`](struct.GlyphBrush.html#method.queue).
    ///
    /// Trims the cache, see [caching behaviour](#caching-behaviour).
    ///
    /// # Example
    /// ```no_run
    /// # fn main() -> Result<(), String> {
    /// # let glyph_brush: gfx_glyph::GlyphBrush<gfx_device_gl::Resources, gfx_device_gl::Factory> = unimplemented!();
    /// # let gfx_color: gfx_core::handle::RenderTargetView<gfx_device_gl::Resources, gfx::format::Srgba8> = unimplemented!();
    /// # let factory: gfx_device_gl::Factory = unimplemented!();
    /// # let gfx_encoder: gfx::Encoder<_, _> = factory.create_command_buffer().into();
    /// glyph_brush.use_queue().draw(&mut gfx_encoder, &gfx_color)?;
    /// # Ok(()) }
    /// ```
    ///
    /// # Raw usage
    /// Can also be used with gfx raw render views if necessary. The `Format` must also be provided.
    /// ```no_run
    /// # use gfx::format::{self, Formatted};
    /// # use gfx::memory::Typed;
    /// # fn main() -> Result<(), String> {
    /// # let glyph_brush: gfx_glyph::GlyphBrush<gfx_device_gl::Resources, gfx_device_gl::Factory> = unimplemented!();
    /// # let gfx_color: gfx_core::handle::RenderTargetView<gfx_device_gl::Resources, gfx::format::Srgba8> = unimplemented!();;
    /// # let factory: gfx_device_gl::Factory = unimplemented!();
    /// # let gfx_encoder: gfx::Encoder<_, _> = factory.create_command_buffer().into();
    /// # let raw_render_view = gfx_color.raw();
    /// glyph_brush.use_queue().draw(
    ///     &mut gfx_encoder,
    ///     &(raw_render_view, format::Srgba8::get_format()),
    /// )?;
    /// # Ok(()) }
    /// ```
    #[inline]
    pub fn draw<C, CV>(self, encoder: &mut gfx::Encoder<R, C>, target: &CV) -> Result<(), String>
    where
        C: gfx::CommandBuffer<R>,
        CV: RawAndFormat<Raw = RawRenderTargetView<R>>,
    {
        let Self {
            brush,
            transform,
            depth_target,
        } = self;
        let transform = transform.unwrap_or_else(|| default_transform(target));
        brush.draw(transform, encoder, target, depth_target)
    }
}

struct NoDepth<R: gfx::Resources>(PhantomData<R>);
impl<R: gfx::Resources> RawAndFormat for NoDepth<R> {
    type Raw = RawDepthStencilView<R>;
    fn as_raw(&self) -> &Self::Raw {
        unreachable!()
    }
    fn format(&self) -> gfx::format::Format {
        unreachable!()
    }
}

impl<'a, F, R, GF, H> DrawBuilder<'a, F, R, GF, H, ()>
where
    F: Font + Sync,
    R: gfx::Resources,
    GF: gfx::Factory<R>,
    H: BuildHasher,
{
    #[inline]
    pub fn draw<C, CV>(self, encoder: &mut gfx::Encoder<R, C>, target: &CV) -> Result<(), String>
    where
        C: gfx::CommandBuffer<R>,
        CV: RawAndFormat<Raw = RawRenderTargetView<R>>,
    {
        let Self {
            brush, transform, ..
        } = self;
        let transform = transform.unwrap_or_else(|| default_transform(target));
        brush.draw::<C, CV, NoDepth<R>>(transform, encoder, target, None)
    }
}