rust: +22 cell functions

pull/922/head
joseLuís 4 years ago
parent d3cac57d03
commit 18f90e394c

@ -9,59 +9,59 @@
// //
// static inline functions to reimplement: 45 // static inline functions to reimplement: 45
// ------------------------------------------ (done / (x) wont / remaining) // ------------------------------------------ (done / (x) wont / remaining)
// (+) implement : 2 / 0 / 43 // (+) implement : 24 / 0 / 21
// (#) unit tests: 0 / 0 / 45 // (#) unit tests: 0 / 0 / 45
// ------------------------------------------ // ------------------------------------------
// cell_bchannel //+cell_bchannel
// cell_bg // cell_bg
// cell_bg_alpha // cell_bg_alpha
// cell_bg_default_p // cell_bg_default_p
// cell_bg_palindex // cell_bg_palindex
// cell_bg_palindex_p // cell_bg_palindex_p
// cell_bg_rgb //+cell_bg_rgb
// cell_blend_bchannel // cell_blend_bchannel
// cell_blend_fchannel // cell_blend_fchannel
// cellcmp //+cellcmp
// cell_double_wide_p //+cell_double_wide_p
// cell_extract //+cell_extract
// cell_fchannel //+cell_fchannel
// cell_fg // cell_fg
// cell_fg_alpha // cell_fg_alpha
// cell_fg_default_p // cell_fg_default_p
// cell_fg_palindex // cell_fg_palindex
// cell_fg_palindex_p // cell_fg_palindex_p
// cell_fg_rgb //+cell_fg_rgb
// cell_init //+cell_init
// cell_load_simple //+cell_load_simple
//+cell_prime //+cell_prime
// cell_set_bchannel //+cell_set_bchannel
// cell_set_bg // cell_set_bg
// cell_set_bg_alpha //+cell_set_bg_alpha
// cell_set_bg_default // cell_set_bg_default
// cell_set_bg_palindex // cell_set_bg_palindex
// cell_set_bg_rgb // cell_set_bg_rgb
// cell_set_bg_rgb_clipped // cell_set_bg_rgb_clipped
// cell_set_fchannel //+cell_set_fchannel
// cell_set_fg // cell_set_fg
// cell_set_fg_alpha //+cell_set_fg_alpha
// cell_set_fg_default //+cell_set_fg_default
// cell_set_fg_palindex //+cell_set_fg_palindex
// cell_set_fg_rgb // cell_set_fg_rgb
// cell_set_fg_rgb_clipped // cell_set_fg_rgb_clipped
// cell_simple_p // cell_simple_p
//+cells_load_box // FIXME //+cells_load_box // FIXME
// cell_strdup //+cell_strdup
// cell_styles //+cell_styles
// cell_styles_off //+cell_styles_off
// cell_styles_on //+cell_styles_on
// cell_styles_set //+cell_styles_set
// cell_wide_left_p //+cell_wide_left_p
// cell_wide_right_p //+cell_wide_right_p
use cstr_core::CString; use cstr_core::CString;
use crate as ffi; use crate as ffi;
use ffi::types::{ChannelPair, IntResult}; use ffi::types::{AlphaBits, Channel, ChannelPair, Color, GraphemeCluster, IntResult, StyleMask};
use ffi::{cell, ncplane}; use ffi::{cell, ncplane};
/// cell_load(), plus blast the styling with 'attr' and 'channels'. /// cell_load(), plus blast the styling with 'attr' and 'channels'.
@ -108,6 +108,7 @@ pub fn cells_load_box(
) -> IntResult { ) -> IntResult {
cell_prime(plane, _ul, gcluster, style, channels) cell_prime(plane, _ul, gcluster, style, channels)
} }
// static inline int // static inline int
// cells_load_box(struct ncplane* n, uint32_t style, uint64_t channels, // cells_load_box(struct ncplane* n, uint32_t style, uint64_t channels,
// cell* ul, cell* ur, cell* ll, cell* lr, // cell* ul, cell* ur, cell* ll, cell* lr,
@ -134,192 +135,227 @@ pub fn cells_load_box(
// return -1; // return -1;
// } // }
// static inline void
// cell_init(cell* c){ // TODO: TEST
// memset(c, 0, sizeof(*c)); #[inline]
// } pub fn cell_init(cell: &mut cell) {
// *cell = unsafe { core::mem::zeroed() }
// Set the specified style bits for the cell 'c', whether they're actively }
// supported or not. Only the lower 16 bits are meaningful.
// static inline void /// Set the specified style bits for the cell 'c', whether they're actively
// cell_styles_set(cell* c, unsigned stylebits){ /// supported or not. Only the lower 16 bits are meaningful.
// c->stylemask = stylebits & NCSTYLE_MASK; /// static inline void
// } // TODO: TEST
// #[inline]
// // Extract the style bits from the cell. pub fn cell_styles_set(cell: &mut cell, stylebits: StyleMask) {
// static inline unsigned cell.stylemask = stylebits & ffi::NCSTYLE_MASK as u16;
// cell_styles(const cell* c){ }
// return c->stylemask;
// } /// Extract the style bits from the cell.
// // TODO: TEST
// // Add the specified styles (in the LSBs) to the cell's existing spec, whether #[inline]
// // they're actively supported or not. pub fn cell_styles(cell: &cell) -> StyleMask {
// static inline void cell.stylemask
// cell_styles_on(cell* c, unsigned stylebits){ }
// c->stylemask |= (stylebits & NCSTYLE_MASK);
// } /// Add the specified styles (in the LSBs) to the cell's existing spec, whether
// /// they're actively supported or not.
// // Remove the specified styles (in the LSBs) from the cell's existing spec. // TODO: TEST
// static inline void #[inline]
// cell_styles_off(cell* c, unsigned stylebits){ pub fn cell_styles_on(cell: &mut cell, stylebits: StyleMask) {
// c->stylemask &= ~(stylebits & NCSTYLE_MASK); cell.stylemask |= stylebits & ffi::NCSTYLE_MASK as u16;
// } }
//
// // Use the default color for the foreground. /// Remove the specified styles (in the LSBs) from the cell's existing spec.
// static inline void // TODO: TEST
// cell_set_fg_default(cell* c){ #[inline]
// channels_set_fg_default(&c->channels); pub fn cell_styles_off(cell: &mut cell, stylebits: StyleMask) {
// } cell.stylemask &= !(stylebits & ffi::NCSTYLE_MASK as u16);
// }
// // Use the default color for the background.
// static inline void /// Use the default color for the foreground.
// cell_set_bg_default(cell* c){ // TODO: TEST
// channels_set_bg_default(&c->channels); #[inline]
// } pub fn cell_set_fg_default(cell: &mut cell) {
// ffi::channels_set_fg_default(&mut cell.channels);
// static inline int }
// cell_set_fg_alpha(cell* c, int alpha){
// return channels_set_fg_alpha(&c->channels, alpha); /// Use the default color for the background.
// } // TODO: TEST
// #[inline]
// static inline int pub fn cell_set_bg_default(cell: &mut cell) {
// cell_set_bg_alpha(cell* c, int alpha){ ffi::channels_set_bg_default(&mut cell.channels);
// return channels_set_bg_alpha(&c->channels, alpha); }
// }
// /// Set the foreground alpha.
// // Does the cell contain an East Asian Wide codepoint? // TODO: TEST
// static inline bool #[inline]
// cell_double_wide_p(const cell* c){ pub fn cell_set_fg_alpha(cell: &mut cell, alpha: AlphaBits) {
// return (c->channels & CELL_WIDEASIAN_MASK); ffi::channels_set_fg_alpha(&mut cell.channels, alpha);
// } }
//
// // Is this the right half of a wide character? /// Set the background alpha.
// static inline bool // TODO: TEST
// cell_wide_right_p(const cell* c){ #[inline]
// return cell_double_wide_p(c) && c->gcluster == 0; pub fn cell_set_bg_alpha(cell: &mut cell, alpha: AlphaBits) {
// } ffi::channels_set_bg_alpha(&mut cell.channels, alpha);
// }
// // Is this the left half of a wide character?
// static inline bool /// Does the cell contain an East Asian Wide codepoint?
// cell_wide_left_p(const cell* c){ // TODO: TEST
// return cell_double_wide_p(c) && c->gcluster; // NOTE: remove casting when fixed: https://github.com/rust-lang/rust-bindgen/issues/1875
// } #[inline]
// pub fn cell_double_wide_p(cell: &cell) -> bool {
// // copy the UTF8-encoded EGC out of the cell, whether simple or complex. the (cell.channels & ffi::CELL_WIDEASIAN_MASK as u64) != 0
// // result is not tied to the ncplane, and persists across erases / destruction. }
// static inline char*
// cell_strdup(const struct ncplane* n, const cell* c){ /// Is this the right half of a wide character?
// return strdup(cell_extended_gcluster(n, c)); // TODO: TEST
// } #[inline]
// pub fn cell_wide_right_p(cell: &cell) -> bool {
// // Extract the three elements of a cell. cell_double_wide_p(cell) && cell.gcluster == 0
// static inline char* }
// cell_extract(const struct ncplane* n, const cell* c,
// uint16_t* stylemask, uint64_t* channels){ /// Is this the left half of a wide character?
// if(stylemask){ // TODO: TEST
// *stylemask = c->stylemask; #[inline]
// } pub fn cell_wide_left_p(cell: &cell) -> bool {
// if(channels){ cell_double_wide_p(cell) && cell.gcluster != 0
// *channels = c->channels; }
// }
// return cell_strdup(n, c); /// copy the UTF8-encoded EGC out of the cell, whether simple or complex. the
// } /// result is not tied to the ncplane, and persists across erases / destruction.
// // TODO: TEST
// // Returns true if the two cells are distinct EGCs, attributes, or channels. #[inline]
// // The actual egcpool index needn't be the same--indeed, the planes needn't even pub fn cell_strdup(plane: &ncplane, cell: &cell) -> *mut i8 {
// // be the same. Only the expanded EGC must be equal. The EGC must be bit-equal; unsafe{
// // it would probably be better to test whether they're Unicode-equal FIXME. libc::strdup(ffi::cell_extended_gcluster(plane, cell))
// static inline bool }
// cellcmp(const struct ncplane* n1, const cell* RESTRICT c1, }
// const struct ncplane* n2, const cell* RESTRICT c2){
// if(c1->stylemask != c2->stylemask){ /// Extract the three elements of a cell.
// return true; // TODO: TEST
// } #[inline]
// if(c1->channels != c2->channels){ pub fn cell_extract(plane: &ncplane, cell: &cell, stylemask: &mut StyleMask, channels: &mut ChannelPair) -> *mut i8 {
// return true; if *stylemask != 0 {
// } *stylemask = cell.stylemask;
// return strcmp(cell_extended_gcluster(n1, c1), cell_extended_gcluster(n2, c2)); }
// } if *channels != 0 {
// *channels = cell.channels;
// static inline int }
// cell_load_simple(struct ncplane* n, cell* c, char ch){ cell_strdup(plane, cell)
// cell_release(n, c); }
// c->channels &= ~(CELL_WIDEASIAN_MASK | CELL_NOBACKGROUND_MASK);
// c->gcluster = ch; /// Returns true if the two cells are distinct EGCs, attributes, or channels.
// return 1; /// The actual egcpool index needn't be the same--indeed, the planes needn't even
// } /// be the same. Only the expanded EGC must be equal. The EGC must be bit-equal;
// /// it would probably be better to test whether they're Unicode-equal FIXME.
// // Extract the 32-bit background channel from a cell. // TODO: TEST
// static inline unsigned #[inline]
// cell_bchannel(const cell* cl){ pub fn cellcmp(plane1: &ncplane, cell1: &cell, plane2: &ncplane, cell2: &cell) -> bool {
// return channels_bchannel(cl->channels); if cell1.stylemask != cell2.stylemask {
// } return true;
// }
// // Extract the 32-bit foreground channel from a cell. if cell1.channels != cell2.channels {
// static inline unsigned return true;
// cell_fchannel(const cell* cl){ }
// return channels_fchannel(cl->channels); unsafe {
// } libc::strcmp(ffi::cell_extended_gcluster(plane1, cell1), ffi::cell_extended_gcluster(plane2, cell2)) != 0
// }
// // Set the 32-bit background channel of a cell. }
// static inline uint64_t
// cell_set_bchannel(cell* cl, uint32_t channel){ // TODO: TEST
// return channels_set_bchannel(&cl->channels, channel); // NOTE: remove casting when fixed: https://github.com/rust-lang/rust-bindgen/issues/1875
// } #[inline]
// pub fn cell_load_simple(plane: &mut ncplane, cell: &mut cell, ch: char) -> i32 {
// // Set the 32-bit foreground channel of a cell. unsafe { ffi::cell_release(plane, cell); }
// static inline uint64_t cell.channels &= !(ffi::CELL_WIDEASIAN_MASK as u64 | ffi::CELL_NOBACKGROUND_MASK);
// cell_set_fchannel(cell* cl, uint32_t channel){ cell.gcluster = ch as GraphemeCluster;
// return channels_set_fchannel(&cl->channels, channel); 1
// } }
//
// // do not pass palette-indexed channels! /// Extract the 32-bit background channel from a cell.
// TODO: TEST
#[inline]
pub fn cell_bchannel(cell: &cell) -> Channel {
ffi::channels_bchannel(cell.channels)
}
/// Extract the 32-bit foreground channel from a cell.
// TODO: TEST
#[inline]
pub fn cell_fchannel(cell: &cell) -> Channel {
ffi::channels_fchannel(cell.channels)
}
/// Set the 32-bit background channel of a cell.
// TODO: TEST
#[inline]
pub fn cell_set_bchannel(cell: &mut cell, channel: Channel) -> ChannelPair {
ffi::channels_set_bchannel(&mut cell.channels, channel)
}
/// Set the 32-bit foreground channel of a cell.
// TODO: TEST
#[inline]
pub fn cell_set_fchannel(cell: &mut cell, channel: Channel) -> ChannelPair {
ffi::channels_set_fchannel(&mut cell.channels, channel)
}
// NOTE: do not pass palette-indexed channels!
// static inline uint64_t // static inline uint64_t
// cell_blend_fchannel(cell* cl, unsigned channel, unsigned* blends){ // cell_blend_fchannel(cell* cl, unsigned channel, unsigned* blends){
// return cell_set_fchannel(cl, channels_blend(cell_fchannel(cl), channel, blends)); // return cell_set_fchannel(cl, channels_blend(cell_fchannel(cl), channel, blends));
// } // }
//
// NOTE: do not pass palette-indexed channels!
// static inline uint64_t // static inline uint64_t
// cell_blend_bchannel(cell* cl, unsigned channel, unsigned* blends){ // cell_blend_bchannel(cell* cl, unsigned channel, unsigned* blends){
// return cell_set_bchannel(cl, channels_blend(cell_bchannel(cl), channel, blends)); // return cell_set_bchannel(cl, channels_blend(cell_bchannel(cl), channel, blends));
// } // }
//
// // Extract 24 bits of foreground RGB from 'cell', shifted to LSBs. /// Extract 24 bits of foreground RGB from 'cell', shifted to LSBs.
// static inline unsigned // TODO: TEST
// cell_fg(const cell* cl){ #[inline]
// return channels_fg(cl->channels); pub fn cell_fg(cell: &cell) -> Channel {
// } ffi::channels_fg(cell.channels)
// }
// // Extract 24 bits of background RGB from 'cell', shifted to LSBs.
// static inline unsigned /// Extract 24 bits of background RGB from 'cell', shifted to LSBs.
// cell_bg(const cell* cl){ // TODO: TEST
// return channels_bg(cl->channels); #[inline]
// } pub fn cell_bg(cell: &cell) -> Channel {
// ffi::channels_bg(cell.channels)
// // Extract 2 bits of foreground alpha from 'cell', shifted to LSBs. }
// static inline unsigned
// cell_fg_alpha(const cell* cl){ /// Extract 2 bits of foreground alpha from 'cell', shifted to LSBs.
// return channels_fg_alpha(cl->channels); // TODO: TEST
// } #[inline]
// pub fn cell_fg_alpha(cell: &cell) -> AlphaBits {
// // Extract 2 bits of background alpha from 'cell', shifted to LSBs. ffi::channels_fg_alpha(cell.channels)
// static inline unsigned }
// cell_bg_alpha(const cell* cl){
// return channels_bg_alpha(cl->channels); /// Extract 2 bits of background alpha from 'cell', shifted to LSBs.
// } // TODO: TEST
// #[inline]
// // Extract 24 bits of foreground RGB from 'cell', split into components. pub fn cell_bg_alpha(cell: &cell) -> AlphaBits {
// static inline unsigned ffi::channels_bg_alpha(cell.channels)
// cell_fg_rgb(const cell* cl, unsigned* r, unsigned* g, unsigned* b){ }
// return channels_fg_rgb(cl->channels, r, g, b);
// } /// Extract 24 bits of foreground RGB from 'cell', split into components.
// // TODO: TEST
// // Extract 24 bits of background RGB from 'cell', split into components. #[inline]
// static inline unsigned pub fn cell_fg_rgb(cell: &cell, red: &mut Color, green: &mut Color, blue: &mut Color) -> Channel {
// cell_bg_rgb(const cell* cl, unsigned* r, unsigned* g, unsigned* b){ ffi::channels_fg_rgb(cell.channels, red, green, blue)
// return channels_bg_rgb(cl->channels, r, g, b); }
// }
// /// Extract 24 bits of background RGB from 'cell', split into components.
// TODO: TEST
#[inline]
pub fn cell_bg_rgb(cell: &cell, red: &mut Color, green: &mut Color, blue: &mut Color) -> Channel {
ffi::channels_bg_rgb(cell.channels, red, green, blue)
}
// // Set the r, g, and b cell for the foreground component of this 64-bit // // Set the r, g, and b cell for the foreground component of this 64-bit
// // 'cell' variable, and mark it as not using the default color. // // 'cell' variable, and mark it as not using the default color.
// static inline int // static inline int

Loading…
Cancel
Save