minor changes
| -rw-r--r-- | Cargo.lock | 7 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/cell.rs | 15 | ||||
| -rw-r--r-- | src/lib.rs | 63 |
4 files changed, 60 insertions, 26 deletions
@@ -130,6 +130,7 @@ dependencies = [ "implicit-fn", "lower", "swash", + "swizzle", ] [[package]] @@ -713,6 +714,12 @@ dependencies = [ ] [[package]] +name = "swizzle" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cd32a3a5640a1e8ba59937591c61be77cae746e7c0fa081b24deebb4f7ece4c" + +[[package]] name = "syn" version = "2.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -10,6 +10,7 @@ fimg = { path = "../fimg", features = ["real-show", "save"] } implicit-fn = "0.1.0" lower = "0.2.0" swash = "0.2.5" +swizzle = "0.1.0" [[test]] name = "test" diff --git a/src/cell.rs b/src/cell.rs index c4d81d3..c59af77 100644 --- a/src/cell.rs +++ b/src/cell.rs @@ -1,10 +1,21 @@ -#[derive(Clone, Copy, Debug, Default)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Style { pub bg: [u8; 3], pub color: [u8; 3], // one of [Style::BOLD].. pub flags: u8, } + +impl Default for Style { + fn default() -> Self { + Self { + bg: [0; 3], + color: [255; 3], + flags: 0, + } + } +} + use std::default::Default::default; use std::fmt::Debug; impl Style { @@ -14,7 +25,7 @@ impl Style { pub const UNDERLINE: u8 = 1 << 3; pub const STRIKETHROUGH: u8 = 1 << 4; } -#[derive(Clone, Copy, Default)] +#[derive(Clone, Copy, Default, PartialEq, Eq)] pub struct Cell { pub style: Style, pub letter: Option<char>, @@ -1,4 +1,4 @@ -// |jlfJKlBM// |||||||||||||||||||||||||||||||||||||#![allow(incomplete_features)] +#![allow(incomplete_features)] #![feature( array_chunks, super_let, @@ -15,7 +15,7 @@ #![allow(unsafe_op_in_unsafe_fn)] use std::iter::{successors, zip}; pub mod cell; -use atools::{ArrayTools, Join, splat}; +use atools::{ArrayTools, Join}; use fimg::{Image, OverlayAt}; use swash::FontRef; use swash::scale::{Render, ScaleContext, Source}; @@ -57,6 +57,7 @@ pub unsafe fn render( bgcolor: [u8; 3], fonts: Fonts, line_spacing: f32, + subpixel: bool, ) -> Image<Box<[u8]>, 3> { assert_eq!(c * r, cells.len(), "cells too short."); let met = fonts.regular.metrics(&[]); @@ -182,7 +183,11 @@ pub unsafe fn render( scbd = scbd.size(ppem); let x = Render::new(&[Source::Outline]) - .format(Format::Subpixel) + .format(if subpixel { + Format::Subpixel + } else { + Format::Alpha + }) .render(&mut scbd.build(), id) .unwrap(); @@ -197,26 +202,36 @@ pub unsafe fn render( .round() as u32 - (met.descent * fac).round() as u32 + (k as f32 * line_spacing as f32 * fac) as u32; - - into( - i.as_mut(), - Image::build(x.placement.width, x.placement.height) + if subpixel { + into( + i.as_mut(), + Image::build( + x.placement.width, + x.placement.height, + ) .buf(&x.data), - (x_, y_), - color.map(_ as u32), - cell.style.bg.map(_ as u32), - ); - - // i.as_mut().overlay_blended_at( - // &item.as_ref(), - // // color, - // ((j as f32 * fw + glyph.x) + x.placement.left as f32) - // .round() as u32, - // (((k + 1) as f32 * fh) - (x.placement.top as f32)) - // .round() as u32 - // - (met.descent * fac).round() as u32 - // + (k as f32 * line_spacing as f32 * fac) as u32, - // ); + (x_, y_), + color.map(_ as u32), + cell.style.bg.map(_ as u32), + ); + } else { + i.as_mut().blend_alpha_and_color_at( + &Image::build( + x.placement.width, + x.placement.height, + ) + .buf(&x.data), + color, + ((j as f32 * fw + glyph.x) + + x.placement.left as f32) + .round() as u32, + (((k + 1) as f32 * fh) - (x.placement.top as f32)) + .round() as u32 + - (met.descent * fac).round() as u32 + + (k as f32 * line_spacing as f32 * fac) + as u32, + ); + } } } } @@ -251,7 +266,7 @@ fn blend( ) -> impl Iterator<Item = [u8; 3]> { use atools::pervasive::prelude::*; x.array_chunks::<4>().map(move |&mask| { - let mask = mask.take::<3>().map(_ as u32); + let mask = mask.take::<3>().map(_ as u32).rev(); color .amul(mask) .aadd(256.sub(mask).amul(bgcolor)) @@ -278,7 +293,7 @@ fn into( )) // could opt later .for_each(|((x, y), d)| { - unsafe { i.set_pixel(x + x_, y + y_, d) }; + i.get_pixel_mut(x + x_, y + y_).map(*_ = d); }) } |