Skip to content

Commit

Permalink
background support
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Feb 1, 2024
1 parent 62216dc commit 8177b11
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 44 deletions.
4 changes: 4 additions & 0 deletions frontends/rioterm/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,10 @@ impl EventHandler for Router {
.sugarloaf
.resize(w.try_into().unwrap(), h.try_into().unwrap());
}
println!(
"columns {:?} lines {:?}",
current.sugarloaf.layout.columns, current.sugarloaf.layout.lines
);
current.resize_all_contexts();
}
}
Expand Down
32 changes: 31 additions & 1 deletion frontends/rioterm/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use rio_backend::config::colors::{
};
use rio_backend::config::Config;
use rio_backend::sugarloaf::core::{
Sugar, SugarCustomDecoration, SugarDecoration, SugarStack, SugarStyle,
Sugar, SugarCursor, SugarCursorStyle, SugarCustomDecoration, SugarDecoration,
SugarStack, SugarStyle,
};
// use rio_backend::sugarloaf::layout::SpanStyle;
use rio_backend::sugarloaf::{SugarGraphic, Sugarloaf};
Expand Down Expand Up @@ -227,6 +228,7 @@ impl State {
decoration,
custom_decoration,
media: None,
cursor: None,
}
}

Expand Down Expand Up @@ -329,6 +331,7 @@ impl State {
decoration: None,
custom_decoration: None,
media: None,
cursor: None,
};
stack.push(selected_sugar);
} else {
Expand Down Expand Up @@ -544,6 +547,32 @@ impl State {
width: media.width,
height: media.height,
}),
cursor: None,
}
}

#[inline]
fn create_sugar_cursor(&self) -> Option<SugarCursor> {
let color = if !self.is_vi_mode_enabled {
self.named_colors.cursor
} else {
self.named_colors.vi_cursor
};

match self.cursor.state.content {
CursorShape::Block => Some(SugarCursor {
style: SugarCursorStyle::Block,
color,
}),
CursorShape::Underline => Some(SugarCursor {
style: SugarCursorStyle::Underline,
color,
}),
CursorShape::Beam => Some(SugarCursor {
style: SugarCursorStyle::Caret,
color,
}),
CursorShape::Hidden => None,
}
}

Expand All @@ -557,6 +586,7 @@ impl State {
decoration: None,
custom_decoration: None,
media: None,
cursor: self.create_sugar_cursor(),
};

let is_italic = square.flags.contains(Flags::ITALIC);
Expand Down
1 change: 0 additions & 1 deletion sugarloaf/src/components/rich_text/batch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

use crate::components::rich_text::image_cache::TextureId;
use bytemuck::{Pod, Zeroable};

Expand Down
14 changes: 14 additions & 0 deletions sugarloaf/src/components/rich_text/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ impl Compositor {
true,
);
}

if let Some(bg_color) = style.background_color {
self.batches.add_rect(
&Rect::new(
rect.x,
style.topline,
rect.width,
style.line_height,
),
depth,
&bg_color,
);
}

if underline {
if entry.top - underline_offset < entry.height as i32 {
if let Some(mut desc_ink) = entry.desc.range() {
Expand Down
49 changes: 25 additions & 24 deletions sugarloaf/src/components/rich_text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ impl RichTextBrush {
ctx: &mut Context,
encoder: &mut wgpu::CommandEncoder,
view: &wgpu::TextureView,
layout: &crate::layout::SugarloafLayout
layout: &crate::layout::SugarloafLayout,
) {
// Used for quick testings
// let content = build_simple_content();
Expand All @@ -341,8 +341,8 @@ impl RichTextBrush {
}

self.needs_update = true;
let w = ctx.size.width * 2;
let _h = ctx.size.height;
// let w = ctx.size.width;
// let _h = ctx.size.height;
if self.needs_update {
let mut lb = self.rich_text_layout_context.builder(
Direction::LeftToRight,
Expand All @@ -366,13 +366,11 @@ impl RichTextBrush {
let transform_has_changed = transform != self.current_transform;

// if transform_has_changed {
let lw = w as f32 - margin_x;
println!("lw {:?}", lw);
self.rich_text_layout
.break_lines()
.break_remaining(lw, self.align);
self.size_changed = false;
// self.selection_changed = true;
self.rich_text_layout
.break_lines()
.break_remaining(ctx.size.width as f32 - margin_x, self.align);
self.size_changed = false;
// self.selection_changed = true;
// }

// let inserted = None;
Expand All @@ -391,12 +389,7 @@ impl RichTextBrush {

// Render
self.comp.begin();
draw_layout(
&mut self.comp,
&self.rich_text_layout,
margin_x,
margin_y,
);
draw_layout(&mut self.comp, &self.rich_text_layout, margin_x, margin_y);

// for r in &self.selection_rects {
// let rect = [r[0] + margin, r[1] + margin, r[2], r[3]];
Expand Down Expand Up @@ -759,12 +752,7 @@ impl RichTextBrush {
}

#[inline]
fn draw_layout(
comp: &mut compositor::Compositor,
layout: &Paragraph,
x: f32,
y: f32,
) {
fn draw_layout(comp: &mut compositor::Compositor, layout: &Paragraph, x: f32, y: f32) {
let depth = 0.0;
let mut glyphs = Vec::new();
for line in layout.lines() {
Expand All @@ -783,12 +771,16 @@ fn draw_layout(
}
}
let color = run.color();

let style = TextRunStyle {
font: font.as_ref(),
font_coords: run.normalized_coords(),
font_size: run.font_size(),
color,
background_color: run.background_color(),
baseline: py,
topline: py - line.ascent(),
line_height: line.ascent() + line.descent(),
advance: px - run_x,
underline: if run.underline() {
Some(UnderlineStyle {
Expand Down Expand Up @@ -916,11 +908,18 @@ fn build_terminal_content() -> crate::content::Content {
S::Size(24.),
// S::features(&[("dlig", 1).into(), ("hlig", 1).into()][..]),
]);
db.enter_span(&[S::Weight(Weight::BOLD), S::Color([1.0, 0.5, 0.5, 1.0])]);
db.enter_span(&[
S::Weight(Weight::BOLD),
S::BackgroundColor([0.0, 1.0, 1.0, 1.0]),
S::Color([1.0, 0.5, 0.5, 1.0]),
]);
db.add_char('R');
db.leave_span();
// should return to span
db.enter_span(&[S::Color([0.0, 1.0, 0.0, 1.0])]);
db.enter_span(&[
S::Color([0.0, 1.0, 0.0, 1.0]),
S::BackgroundColor([1.0, 1.0, 0.0, 1.0]),
]);
db.add_char('i');
db.leave_span();
db.enter_span(&[
Expand All @@ -930,6 +929,8 @@ fn build_terminal_content() -> crate::content::Content {
// S::Size(20.),
]);
db.add_char('o');
db.leave_span();
db.add_char('+');
db.add_char(' ');
for x in 0..5 {
db.add_char('🌊');
Expand Down
7 changes: 6 additions & 1 deletion sugarloaf/src/components/rich_text/text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

use swash::{FontRef, GlyphId, NormalizedCoord};

/// Properties for a text run.
Expand All @@ -12,8 +11,14 @@ pub struct TextRunStyle<'a> {
pub font_size: f32,
/// Color of the text.
pub color: [f32; 4],
/// Background of the text.
pub background_color: Option<[f32; 4]>,
/// Baseline of the run.
pub baseline: f32,
/// Topline of the run (basically y axis).
pub topline: f32,
/// Absolute line height of the run.
pub line_height: f32,
/// Total advance of the run.
pub advance: f32,
/// Underline style.
Expand Down
2 changes: 1 addition & 1 deletion sugarloaf/src/components/rich_text/util/fxhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
use core::default::Default;
use core::hash::{BuildHasherDefault, Hasher};
use core::ops::BitXor;
use std::collections::{HashMap};
use std::collections::HashMap;

/// A builder for default Fx hashers.
pub type FxBuildHasher = BuildHasherDefault<FxHasher>;
Expand Down
14 changes: 14 additions & 0 deletions sugarloaf/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,24 @@ pub struct Sugar {
pub background_color: [f32; 4],
pub style: Option<SugarStyle>,
pub decoration: Option<SugarDecoration>,
pub cursor: Option<SugarCursor>,
pub custom_decoration: Option<SugarCustomDecoration>,
pub media: Option<SugarGraphic>,
}

#[derive(Debug)]
pub enum SugarCursorStyle {
Block,
Caret,
Underline,
}

#[derive(Debug)]
pub struct SugarCursor {
pub color: [f32; 4],
pub style: SugarCursorStyle,
}

#[derive(Debug)]
pub enum SugarDecoration {
Underline,
Expand Down
8 changes: 7 additions & 1 deletion sugarloaf/src/layout/builder_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ pub struct SpanData {
pub font_attrs: (Stretch, Weight, Style),
/// Font size in ppem.
pub font_size: f32,
/// Font size in ppem.
/// Font color.
pub color: [f32; 4],
/// Background color.
pub background_color: Option<[f32; 4]>,
/// Font features.
pub font_features: FontSettingKey,
/// Font variations.
Expand Down Expand Up @@ -162,6 +164,7 @@ impl BuilderState {
word_spacing: 0.,
line_spacing: 1.,
color: [1.0, 1.0, 1.0, 1.0],
background_color: None,
underline: false,
underline_offset: None,
underline_size: None,
Expand Down Expand Up @@ -251,6 +254,9 @@ impl BuilderState {
S::Color(color) => {
span.color = *color;
}
S::BackgroundColor(color) => {
span.background_color = Some(*color);
}
S::Features(features) => {
span.font_features = self.features.add(features.iter().copied());
}
Expand Down
7 changes: 7 additions & 0 deletions sugarloaf/src/layout/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ impl Paragraph {
font: font.clone(),
coords: (coords_start, coords_end),
color: span_data.color,
background_color: span_data.background_color,
size,
level,
whitespace: false,
Expand Down Expand Up @@ -186,6 +187,7 @@ impl Paragraph {
descent: metrics.descent * span_data.line_spacing,
leading: metrics.leading * span_data.line_spacing,
color: span_data.color,
background_color: span_data.background_color,
underline: span_data.underline,
underline_offset: span_data
.underline_offset
Expand Down Expand Up @@ -332,6 +334,11 @@ impl<'a> Run<'a> {
self.run.advance
}

/// Returns true if the run has an background color
pub fn background_color(&self) -> Option<[f32; 4]> {
self.run.background_color
}

/// Returns true if the run has an underline decoration.
pub fn underline(&self) -> bool {
self.run.underline
Expand Down
1 change: 1 addition & 0 deletions sugarloaf/src/layout/layout_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ pub struct RunData {
pub ascent: f32,
pub descent: f32,
pub leading: f32,
pub background_color: Option<[f32; 4]>,
pub underline: bool,
pub underline_offset: f32,
pub underline_size: f32,
Expand Down
9 changes: 2 additions & 7 deletions sugarloaf/src/layout/line_breaker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@ use super::layout::*;
use super::layout_data::*;

/// Alignment of a paragraph.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[derive(Copy, Default, Clone, PartialEq, Eq, Debug)]
#[repr(u8)]
pub enum Alignment {
#[default]
Start,
Middle,
End,
}

impl Default for Alignment {
fn default() -> Self {
Self::Start
}
}

/// Line breaking support for a paragraph.
pub struct BreakLines<'a> {
layout: &'a mut LayoutData,
Expand Down
4 changes: 2 additions & 2 deletions sugarloaf/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ impl SugarloafLayout {
font_size,
sugarwidth: font_size,
sugarheight: font_size,
scaled_sugarwidth: font_size,
scaled_sugarheight: font_size,
scaled_sugarwidth: font_size * scale_factor,
scaled_sugarheight: font_size * scale_factor,
background_image: None,
line_height,
style,
Expand Down
4 changes: 4 additions & 0 deletions sugarloaf/src/layout/span_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub enum SpanStyle<'a> {
Weight(Weight),
/// Font color.
Color([f32; 4]),
/// Background color.
BackgroundColor([f32; 4]),
/// Font style.
Style(Style),
/// Font feature settings.
Expand Down Expand Up @@ -64,6 +66,7 @@ impl<'a> SpanStyle<'a> {
Self::Stretch(v) => S::Stretch(*v),
Self::Weight(v) => S::Weight(*v),
Self::Color(v) => S::Color(*v),
Self::BackgroundColor(v) => S::BackgroundColor(*v),
Self::Style(v) => S::Style(*v),
Self::Features(v) => S::Features(Cow::Owned(v.clone().into_owned())),
Self::Variations(v) => S::Variations(Cow::Owned(v.clone().into_owned())),
Expand All @@ -87,6 +90,7 @@ impl<'a> SpanStyle<'a> {
Self::Stretch(v) => S::Stretch(v),
Self::Weight(v) => S::Weight(v),
Self::Color(v) => S::Color(v),
Self::BackgroundColor(v) => S::BackgroundColor(v),
Self::Style(v) => S::Style(v),
Self::Features(v) => S::Features(Cow::Owned(v.into_owned())),
Self::Variations(v) => S::Variations(Cow::Owned(v.into_owned())),
Expand Down
Loading

0 comments on commit 8177b11

Please sign in to comment.