Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/editor-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ fn redraw(
cursor_color,
selection_color,
selected_text_color,
|x, y, w, h, color| {
|x, y, w, h, color, subpixel_mask| {
assert!(subpixel_mask.is_none());
window.rect(x, y, w, h, orbclient::Color { data: color.0 });
},
);
Expand Down
79 changes: 68 additions & 11 deletions examples/editor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,74 @@ fn main() {
let mut paint = Paint::default();
paint.anti_alias = false;
editor.shape_as_needed(true);
editor.draw(&mut swash_cache, |x, y, w, h, color| {
// Note: due to softbuffer and tiny_skia having incompatible internal color representations we swap
// the red and blue channels here
paint.set_color_rgba8(color.b(), color.g(), color.r(), color.a());
pixmap.fill_rect(
Rect::from_xywh(x as f32, y as f32, w as f32, h as f32)
.unwrap(),
&paint,
Transform::identity(),
None,
);
editor.draw(&mut swash_cache, |x, y, w, h, color, subpixel_mask| {
if let Some(mask) = subpixel_mask {
// Subpixel mask must be manually applied
//TODO: just clamp x, y, w, and h?
if x < 0 || y < 0 {
return;
}
let width = pixmap.width();
if x as u32 + w > width {
return;
}
let height = pixmap.height();
if y as u32 + h > height {
return;
}
//TODO: simd?
let color_r = color.r() as u32;
let color_g = color.g() as u32;
let color_b = color.b() as u32;
let color_a = color.a() as u32;
let mask_r = ((mask.r() as u32) * color_a) >> 8;
let mask_g = ((mask.g() as u32) * color_a) >> 8;
let mask_b = ((mask.b() as u32) * color_a) >> 8;
let pixels = pixmap.pixels_mut();
for row in 0..h {
let row_i = (y as u32 + row) * width;
for col in 0..w {
let i = (row_i + x as u32 + col) as usize;
let pixel = &mut pixels[i];
// Note: due to softbuffer and tiny_skia having incompatible internal color representations we swap
// the red and blue channels here
let pixel_r = pixel.blue() as u32;
let pixel_g = pixel.green() as u32;
let pixel_b = pixel.red() as u32;
let r = ((pixel_r * (255 - mask_r))
+ (color_r * mask_r))
>> 8;
let g = ((pixel_g * (255 - mask_g))
+ (color_g * mask_g))
>> 8;
let b = ((pixel_b * (255 - mask_b))
+ (color_b * mask_b))
>> 8;
// Note: due to softbuffer and tiny_skia having incompatible internal color representations we swap
// the red and blue channels here
*pixel = tiny_skia::PremultipliedColorU8::from_rgba(
b as u8, g as u8, r as u8, 0xFF,
)
.unwrap();
}
}
} else {
// Note: due to softbuffer and tiny_skia having incompatible internal color representations we swap
// the red and blue channels here
paint.set_color_rgba8(
color.b(),
color.g(),
color.r(),
color.a(),
);
pixmap.fill_rect(
Rect::from_xywh(x as f32, y as f32, w as f32, h as f32)
.unwrap(),
&paint,
Transform::identity(),
None,
);
}
});
if let Some((x, y)) = editor.cursor_position() {
window.set_ime_cursor_area(
Expand Down
77 changes: 68 additions & 9 deletions examples/multiview/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,74 @@ fn main() {
buffer.draw(
&mut swash_cache,
cosmic_text::Color::rgb(0xFF, 0xFF, 0xFF),
|x, y, w, h, color| {
paint.set_color_rgba8(color.r(), color.g(), color.b(), color.a());
pixmap.fill_rect(
Rect::from_xywh(x as f32, y as f32, w as f32, h as f32)
.unwrap(),
&paint,
transform,
None,
);
|x, y, w, h, color, subpixel_mask| {
if let Some(mask) = subpixel_mask {
// Subpixel mask must be manually applied
//TODO: just clamp x, y, w, and h?
if x < 0 || y < 0 {
return;
}
let width = pixmap.width();
if x as u32 + w > width {
return;
}
let height = pixmap.height();
if y as u32 + h > height {
return;
}
//TODO: simd?
let color_r = color.r() as u32;
let color_g = color.g() as u32;
let color_b = color.b() as u32;
let color_a = color.a() as u32;
let mask_r = ((mask.r() as u32) * color_a) >> 8;
let mask_g = ((mask.g() as u32) * color_a) >> 8;
let mask_b = ((mask.b() as u32) * color_a) >> 8;
let pixels = pixmap.pixels_mut();
for row in 0..h {
let row_i = (y as u32 + row) * width;
for col in 0..w {
let i = (row_i + x as u32 + col) as usize;
let pixel = &mut pixels[i];
// Note: due to softbuffer and tiny_skia having incompatible internal color representations we swap
// the red and blue channels here
let pixel_r = pixel.blue() as u32;
let pixel_g = pixel.green() as u32;
let pixel_b = pixel.red() as u32;
let r = ((pixel_r * (255 - mask_r))
+ (color_r * mask_r))
>> 8;
let g = ((pixel_g * (255 - mask_g))
+ (color_g * mask_g))
>> 8;
let b = ((pixel_b * (255 - mask_b))
+ (color_b * mask_b))
>> 8;
// Note: due to softbuffer and tiny_skia having incompatible internal color representations we swap
// the red and blue channels here
*pixel = tiny_skia::PremultipliedColorU8::from_rgba(
b as u8, g as u8, r as u8, 0xFF,
)
.unwrap();
}
}
} else {
// Note: due to softbuffer and tiny_skia having incompatible internal color representations we swap
// the red and blue channels here
paint.set_color_rgba8(
color.b(),
color.g(),
color.r(),
color.a(),
);
pixmap.fill_rect(
Rect::from_xywh(x as f32, y as f32, w as f32, h as f32)
.unwrap(),
&paint,
transform,
None,
);
}
},
);

Expand Down
85 changes: 69 additions & 16 deletions examples/rich-text/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,22 +195,75 @@ fn main() {
cursor_color,
selection_color,
selected_text_color,
|x, y, w, h, color| {
// Note: due to softbuffer and tiny_skia having incompatible internal color representations we swap
// the red and blue channels here
paint.set_color_rgba8(
color.b(),
color.g(),
color.r(),
color.a(),
);
pixmap.fill_rect(
Rect::from_xywh(x as f32, y as f32, w as f32, h as f32)
.unwrap(),
&paint,
Transform::identity(),
None,
);
|x, y, w, h, color, subpixel_mask| {
if let Some(mask) = subpixel_mask {
// Subpixel mask must be manually applied
//TODO: just clamp x, y, w, and h?
if x < 0 || y < 0 {
return;
}
let width = pixmap.width();
if x as u32 + w > width {
return;
}
let height = pixmap.height();
if y as u32 + h > height {
return;
}
//TODO: simd?
let color_r = color.r() as u32;
let color_g = color.g() as u32;
let color_b = color.b() as u32;
let color_a = color.a() as u32;
let mask_r = ((mask.r() as u32) * color_a) >> 8;
let mask_g = ((mask.g() as u32) * color_a) >> 8;
let mask_b = ((mask.b() as u32) * color_a) >> 8;
let pixels = pixmap.pixels_mut();
for row in 0..h {
let row_i = (y as u32 + row) * width;
for col in 0..w {
let i = (row_i + x as u32 + col) as usize;
let pixel = &mut pixels[i];
// Note: due to softbuffer and tiny_skia having incompatible internal color representations we swap
// the red and blue channels here
let pixel_r = pixel.blue() as u32;
let pixel_g = pixel.green() as u32;
let pixel_b = pixel.red() as u32;
let r = ((pixel_r * (255 - mask_r))
+ (color_r * mask_r))
>> 8;
let g = ((pixel_g * (255 - mask_g))
+ (color_g * mask_g))
>> 8;
let b = ((pixel_b * (255 - mask_b))
+ (color_b * mask_b))
>> 8;
// Note: due to softbuffer and tiny_skia having incompatible internal color representations we swap
// the red and blue channels here
*pixel =
tiny_skia::PremultipliedColorU8::from_rgba(
b as u8, g as u8, r as u8, 0xFF,
)
.unwrap();
}
}
} else {
// Note: due to softbuffer and tiny_skia having incompatible internal color representations we swap
// the red and blue channels here
paint.set_color_rgba8(
color.b(),
color.g(),
color.r(),
color.a(),
);
pixmap.fill_rect(
Rect::from_xywh(x as f32, y as f32, w as f32, h as f32)
.unwrap(),
&paint,
Transform::identity(),
None,
);
}
},
);

Expand Down
43 changes: 28 additions & 15 deletions examples/terminal/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,34 @@ fn main() {
let mut canvas = vec![vec![None; width as usize]; height as usize];

// Draw to the canvas
buffer.draw(&mut swash_cache, TEXT_COLOR, |x, y, w, h, color| {
let a = color.a();
if a == 0 || x < 0 || x >= width as i32 || y < 0 || y >= height as i32 || w != 1 || h != 1 {
// Ignore alphas of 0, or invalid x, y coordinates, or unimplemented sizes
return;
}

// Scale by alpha (mimics blending with black)
let scale = |c: u8| (c as i32 * a as i32 / 255).clamp(0, 255) as u8;

let r = scale(color.r());
let g = scale(color.g());
let b = scale(color.b());
canvas[y as usize][x as usize] = Some((r, g, b));
});
buffer.draw(
&mut swash_cache,
TEXT_COLOR,
|x, y, w, h, color, subpixel_mask| {
assert!(subpixel_mask.is_none());

let a = color.a();
if a == 0
|| x < 0
|| x >= width as i32
|| y < 0
|| y >= height as i32
|| w != 1
|| h != 1
{
// Ignore alphas of 0, or invalid x, y coordinates, or unimplemented sizes
return;
}

// Scale by alpha (mimics blending with black)
let scale = |c: u8| (c as i32 * a as i32 / 255).clamp(0, 255) as u8;

let r = scale(color.r());
let g = scale(color.g());
let b = scale(color.b());
canvas[y as usize][x as usize] = Some((r, g, b));
},
);

// Render the canvas
let mut output = String::new();
Expand Down
Loading