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
// notcurses::visual::visual
//
//!
//
use super::{Blitter, Scale, VisualBuilder, VisualGeometry, VisualOptions};
use crate::{
color::Rgba,
error::{NotcursesError as Error, NotcursesResult as Result},
notcurses::NotcursesInner,
plane::{Align, Plane},
sys::{self, NcRgba, NcVisual},
Notcurses, Position, Size,
};
use std::{cell::RefCell, rc::Rc};
/// A visual bit of multimedia.
pub struct Visual {
pub(super) nc: *mut NcVisual,
pub(super) options: VisualOptions,
// Ensures the notcurses context remains alive as long as this object exists
#[allow(dead_code)]
pub(super) notcurses: Rc<RefCell<NotcursesInner>>,
}
mod core_impls {
use super::Visual;
use core::fmt;
impl Drop for Visual {
#[inline]
fn drop(&mut self) {
if crate::Notcurses::is_initialized() {
self.into_ref_mut().destroy()
}
}
}
impl fmt::Display for Visual {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write![f, "{}", self.options]
}
}
impl fmt::Debug for Visual {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Visual {{ {:?} }}", self.options)
}
}
}
/// # `Visual` constructors and deconstructors.
impl Visual {
/// Returns a new `Visual` builder.
#[inline]
pub fn builder() -> VisualBuilder {
VisualBuilder::new()
}
/// Returns a new `Visual` from a byte buffer with RGBA content.
#[inline]
pub fn from_rgba(nc: &Notcurses, rgba: &[u8], size: impl Into<Size>) -> Result<Visual> {
Visual::builder().build_from_rgba(nc, rgba, size.into())
}
/// Builds a new `Visual` from a byte buffer with RGB content, providing
/// the alpha to assign to all the pixels.
#[inline]
pub fn from_rgb(
nc: &Notcurses,
rgb: &[u8],
size: impl Into<Size>,
alpha: u8,
) -> Result<Visual> {
Visual::builder().build_from_rgb(nc, rgb, size.into(), alpha)
}
/// Builds a new `Visual` from a byte buffer with RGBX content, overriding
/// the alpha byte *X* for all the pixels.
#[inline]
pub fn from_rgbx(
nc: &Notcurses,
rgbx: &[u8],
size: impl Into<Size>,
alpha: u8,
) -> Result<Visual> {
Visual::builder().build_from_rgbx(nc, rgbx, size.into(), alpha)
}
/// Builds a new `Visual` from a byte buffer with BGRA content.
///
/// This is slower than [`build_from_rgba`][VisualBuilder#method.build_fromrgba],
/// since it has to convert the pixels to the rgba format used internally.
#[inline]
pub fn from_bgra(nc: &Notcurses, bgra: &[u8], size: impl Into<Size>) -> Result<Visual> {
Visual::builder().build_from_bgra(nc, bgra, size.into())
}
/// Builds a new `Visual` from a `file`, extracts the codec and parameters
/// and decodes the first image to memory.
///
/// It needs notcurses to be compiled with multimedia capabilities.
#[inline]
pub fn from_file(nc: &Notcurses, file: &str) -> Result<Visual> {
Visual::builder().build_from_file(nc, file)
}
/// Builds a new `Visual` from a [`Plane`].
///
/// The plane may contain only spaces, half blocks, and full blocks.
/// This will be checked, and any other glyph will result in an error.
///
/// This function exists so that planes can be subjected to `Visual` transformations.
/// If possible, it's better to build the visual from memory using
/// [`build_from_rgba`][Visual#method.build_from_rgba].
///
/// Use `None` for either or both of `beg_y` and `beg_x` in order to
/// use the current cursor position along that axis.
///
/// Use `None` for either or both of `len_y` and `len_x` in order to
/// go through the boundary of the plane in that axis (same as `0`).
///
#[inline]
pub fn from_plane(
plane: &Plane,
blitter: Blitter,
beg_x: Option<u32>,
beg_y: Option<u32>,
len_x: Option<u32>,
len_y: Option<u32>,
) -> Result<Visual> {
Visual::builder().build_from_plane(plane, blitter, beg_x, beg_y, len_x, len_y)
}
/// Returns a shared reference to the inner [`NcVisual`].
#[inline]
pub fn into_ref(&self) -> &NcVisual {
unsafe { &*self.nc }
}
/// Returns an exclusive reference to the inner [`NcVisual`].
#[inline]
pub fn into_ref_mut(&mut self) -> &mut NcVisual {
unsafe { &mut *self.nc }
}
/// Returns the visual options.
#[inline]
pub fn options(&self) -> VisualOptions {
self.options
}
/// Sets the visual `options`.
#[inline]
pub fn set_options(&mut self, options: VisualOptions) {
self.options = options;
}
}
/// # `Visual` methods.
impl Visual {
/// Renders the `Visual` to a new [`Plane`], which is returned.
#[inline]
pub fn blit(&mut self, notcurses: &Notcurses) -> Result<Plane> {
let vo: sys::NcVisualOptions = self.options.into();
let ncplane =
notcurses.with_nc_mut(|nc| unsafe { self.into_ref_mut().blit(nc, Some(&vo)) })?;
Ok(Plane::from_ncplane(ncplane, notcurses))
}
/// Renders the `Visual` to an existing `target` [`Plane`].
#[inline]
pub fn blit_plane(&mut self, notcurses: &Notcurses, target: &mut Plane) -> Result<()> {
let mut vo: sys::NcVisualOptions = self.options.into();
vo.n = target.into_ref_mut();
let _ = notcurses.with_nc_mut(|nc| unsafe { self.into_ref_mut().blit(nc, Some(&vo)) })?;
Ok(())
}
/// Renders the `Visual` to a new child [`Plane`] of a `parent` plane, which is returned.
#[inline]
pub fn blit_child(&mut self, notcurses: &Notcurses, parent: &mut Plane) -> Result<Plane> {
let mut vo: sys::NcVisualOptions = self.options.into();
vo.n = parent.into_ref_mut();
vo.flags |= sys::NcVisualFlag::ChildPlane;
let ncplane_child =
notcurses.with_nc_mut(|nc| unsafe { self.into_ref_mut().blit(nc, Some(&vo)) })?;
Ok(Plane::from_ncplane(ncplane_child, notcurses))
}
//
/// Returns the visual geometry.
#[inline]
pub fn geometry(&self, notcurses: &Notcurses) -> Result<VisualGeometry> {
notcurses.with_nc(|nc| {
Ok(self
.into_ref()
.geom(Some(nc), Some(&self.options().into()))?
.into())
})
}
/// Returns the internal size of the visual, in pixels.
#[inline]
pub fn size(&self) -> Result<Size> {
self.into_ref()
.geom(None, Some(&self.options().into()))?
.pix_yx
.map(|s| Size::from(s).swapped())
.ok_or_else(|| Error::Message("visual size error".to_string()))
}
// Â--
/// Resizes the visual to the new `size` using bilinear interpolation.
///
/// This is a lossy transformation, unless the size is unchanged.
#[inline]
pub fn resize(&mut self, size: Size) -> Result<()> {
let (w, h) = size.into();
Ok(self.into_ref_mut().resize(h, w)?)
}
/// Resizes the visual to the new `size` using nearest neighbor interpolation.
///
/// This is a lossy transformation, unless the size is unchanged.
#[inline]
pub fn resize_nearest(&mut self, size: Size) -> Result<()> {
let (w, h) = size.into();
Ok(self.into_ref_mut().resize_noninterpolative(h, w)?)
}
//
/// Rotates the visual a number of `radians`.
///
/// Only M_PI/2 and -M_PI/2 are supported at the moment.
#[inline]
pub fn rotate(&mut self, radians: f64) -> Result<()> {
Ok(self.into_ref_mut().rotate(radians)?)
}
//
/// Sets the horizontal placement, overriding horizontal alignment.
///
/// Default: *`0`*.
#[inline]
pub fn set_x(&mut self, x: i32) {
self.options.set_x(x);
}
/// Sets the vertical placement, overriding vertical alignment.
///
/// Default: *`0`*.
#[inline]
pub fn set_y(&mut self, y: i32) {
self.options.set_y(y);
}
/// Sets both the horizontal & vertical placement,
/// overriding both horizontal & vertical alignment.
///
/// Default: *`(0, 0)`*.
#[inline]
pub fn set_xy(&mut self, x: i32, y: i32) {
self.options.set_x(x);
self.options.set_y(y);
}
/// Convenience wrapper around [`set_yx`][Visual#method.yx].
#[inline]
pub fn set_position(&mut self, position: Position) {
let (x, y) = position.into();
self.set_xy(x, y);
}
/// Sets the horizontal alignment.
///
/// Default: *[`Align::Left`]*.
#[inline]
pub fn set_halign(&mut self, horizontal: Align) {
self.options.set_halign(horizontal);
}
/// Sets the vertical alignment.
///
/// Default: *[`Align::Top`]*.
#[inline]
pub fn set_valign(&mut self, vertical: Align) {
self.options.set_valign(vertical);
}
/// Sets both the vertical & horizontal alignment.
///
/// Default: *`(`[`Align::Top`]*`, `*[`Align::Left`]`)`*.
#[inline]
pub fn set_align(&mut self, vertical: Align, horizontal: Align) {
self.options.set_halign(horizontal);
self.options.set_valign(vertical);
}
/// Sets the [`Scale`].
///
/// Default: `Scale::None`.
#[inline]
pub fn set_scale(&mut self, scale: Scale) {
self.options.set_scale(scale);
}
/// Sets the [`Blitter`].
///
/// Default: `Blitter::Default`.
#[inline]
pub fn set_blitter(&mut self, blitter: Blitter) {
self.options.set_blitter(blitter);
}
/// Sets the [`Pixel`][Blitter::Pixel] blitter.
#[inline]
pub fn set_blitter_pixel(&mut self) {
self.options.set_blitter(Blitter::Pixel);
}
/// Gets the Rgba pixel at the provided coordinates.
///
/// *Corresponds to [`NcVisual::at_yx`].*
#[inline]
pub fn get_pixel(&self, x: u32, y: u32) -> Result<Rgba> {
let ncrgba: NcRgba = self.into_ref().at_yx(y, x)?.into();
Ok(ncrgba.into())
}
/// Sets the Rgba pixel at the provided coordinates.
///
/// *Corresponds to [`NcVisual::set_yx`].*
#[inline]
pub fn set_pixel(&mut self, x: u32, y: u32, rgba: impl Into<Rgba>) -> Result<()> {
let ncrgba: NcRgba = rgba.into().into();
self.into_ref_mut().set_yx(y, x, ncrgba)?;
Ok(())
}
/// (Un)Sets graful degradation.
///
/// Choose between gracefully degrading the blitter, or fail if the choosen
/// `Blitter` is not supported by the terminal.
///
/// Default: true (degrade).
#[inline]
pub fn set_degrade(&mut self, degrade: bool) {
self.options.set_degrade(degrade);
}
/// (Un)Sets this color as transparent.
///
/// Default: `None`.
#[inline]
pub fn set_transparency(&mut self, color: Option<Rgba>) {
self.options.set_transparency(color);
}
/// (Un)Sets alpha blending.
///
/// Choose whether to use [`Alpha::Blend`] with the [`Visual`], so that
/// the foreground or background colors can be a composite between
/// a color and the corresponding colors underneath it.
///
/// Default: *false* (no blend).
///
/// [`Alpha::Blend`]: crate::color::Alpha#associatedconstant.Blend
#[inline]
pub fn set_blend(&mut self, blend: bool) {
self.options.set_blend(blend);
}
/// (Un)Sets scaling interpolation.
///
/// Default: true (interpolate).
#[inline]
pub fn set_interpolate(&mut self, interpolate: bool) {
self.options.set_interpolate(interpolate);
}
/// Sets the region to be rendered.
///
/// - `y`, `x`: origin of the rendered region in pixels.
/// - `len_y`, `len_x`: size of the rendered region in pixels.
#[inline]
pub fn set_region(&mut self, x: u32, y: u32, len_x: u32, len_y: u32) {
self.options.set_region(Some((x, y, len_x, len_y)));
}
/// Sets the pixel offset within the [`Cell`][crate::plane::Cell].
#[inline]
pub fn set_cell_offset(&mut self, x: u32, y: u32) {
self.options.set_cell_offset(Some((x, y)));
}
}