From 1b99690990f256a8578dbf367bcb2f4100af6f57 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 19 Sep 2022 06:42:14 -0700 Subject: [PATCH] Add superscript and subscript cell attributes I've expanded the number of bits from 16->32 without impacting the overall struct sizes and reserved 2 bits for super/subscript. I refer to these as vertical alignment properties for conceptual consistency with css. SGR 73, 74, 75 are used to set super, sub and normal vertical alignment. These are compatible with mintty. However, mintty just added support for setting both attributes to render in small caps in https://github.com/mintty/mintty/commit/06ac446049c07ad9566fcc617749120af4bd0fba (https://github.com/mintty/mintty/issues/1171) --- codec/src/lib.rs | 2 +- docs/changelog.md | 4 +++- docs/escape-sequences.md | 3 +++ term/src/terminalstate/mod.rs | 3 +++ termwiz/src/cell.rs | 28 +++++++++++++++++++--------- termwiz/src/escape/csi.rs | 25 +++++++++++++++++++++---- wezterm-gui/src/tabbar.rs | 3 +++ wezterm-gui/src/termwindow/render.rs | 12 +++++++++++- 8 files changed, 64 insertions(+), 16 deletions(-) diff --git a/codec/src/lib.rs b/codec/src/lib.rs index c995d7585..daa3a42a6 100644 --- a/codec/src/lib.rs +++ b/codec/src/lib.rs @@ -416,7 +416,7 @@ macro_rules! pdu { /// The overall version of the codec. /// This must be bumped when backwards incompatible changes /// are made to the types and protocol. -pub const CODEC_VERSION: usize = 29; +pub const CODEC_VERSION: usize = 30; // Defines the Pdu enum. // Each struct has an explicit identifying number. diff --git a/docs/changelog.md b/docs/changelog.md index afceec60b..78231beb7 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -24,7 +24,9 @@ As features stabilize some brief notes about them will accumulate here. [strikethrough_position](config/lua/config/strikethrough_position.md) options to fine tune appearance. [#2505](https://github.com/wez/wezterm/issues/2505) [#2326](https://github.com/wez/wezterm/issues/2326) -* Preliminary support for `modifyOtherKeys` keyboard encoding [#2527](https://github.com/wez/wezterm/issues/2527) +* Preliminary support for `modifyOtherKeys` keyboard encoding + [#2527](https://github.com/wez/wezterm/issues/2527) +* Superscript and subscript text attributes via SGR 73 and SGR 74 #### Fixed * Wayland: key repeat gets stuck after pressing two keys in quick succession. diff --git a/docs/escape-sequences.md b/docs/escape-sequences.md index 96556b276..70cf50ae2 100644 --- a/docs/escape-sequences.md +++ b/docs/escape-sequences.md @@ -198,6 +198,9 @@ It is valid to omit the code number; for example `CSI m` is equivalent to `CSI |53 |OverlineOn|Renders text with a single overline/overbar| |55 |OverlineOff|Cancels OverlineOn| |59 |UnderlineColorDefault|Resets the underline color to default, which is to match the foreground color| +|73 |VerticalAlignSuperScript|Adjusts the baseline of the text so that it renders as superscript (*Since: nightly builds only*)| +|74 |VerticalAlignSubScript|Adjusts the baseline of the text so that it renders as subscript (*Since: nightly builds only*)| +|75 |VerticalAlignBaseLine|Reset the baseline of the text to normal (*Since: nightly builds only*)| |90 |ForegroundBrightBlack|Sets the foreground color to Bright Black, which is palette index 8| |91 |ForegroundBrightRed|Sets the foreground color to Bright Red, which is palette index 9| |92 |ForegroundBrightGreen|Sets the foreground color to Bright Green, which is palette index 10| diff --git a/term/src/terminalstate/mod.rs b/term/src/terminalstate/mod.rs index 0b10e5c27..833e59957 100644 --- a/term/src/terminalstate/mod.rs +++ b/term/src/terminalstate/mod.rs @@ -2523,6 +2523,9 @@ impl TerminalState { Sgr::Overline(overline) => { self.pen.set_overline(overline); } + Sgr::VerticalAlign(align) => { + self.pen.set_vertical_align(align); + } Sgr::Blink(blink) => { self.pen.set_blink(blink); } diff --git a/termwiz/src/cell.rs b/termwiz/src/cell.rs index 3aa657b41..153e96180 100644 --- a/termwiz/src/cell.rs +++ b/termwiz/src/cell.rs @@ -43,7 +43,7 @@ impl Into for SmallColor { #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[derive(Clone, Eq, PartialEq)] pub struct CellAttributes { - attributes: u16, + attributes: u32, /// The foreground color foreground: SmallColor, /// The background color @@ -127,12 +127,12 @@ macro_rules! bitfield { ($getter:ident, $setter:ident, $bitmask:expr, $bitshift:expr) => { #[inline] - pub fn $getter(&self) -> u16 { + pub fn $getter(&self) -> u32 { (self.attributes >> $bitshift) & $bitmask } #[inline] - pub fn $setter(&mut self, value: u16) -> &mut Self { + pub fn $setter(&mut self, value: u32) -> &mut Self { let clear = !($bitmask << $bitshift); let attr_value = (value & $bitmask) << $bitshift; self.attributes = (self.attributes & clear) | attr_value; @@ -143,12 +143,12 @@ macro_rules! bitfield { ($getter:ident, $setter:ident, $enum:ident, $bitmask:expr, $bitshift:expr) => { #[inline] pub fn $getter(&self) -> $enum { - unsafe { mem::transmute(((self.attributes >> $bitshift) & $bitmask) as u16) } + unsafe { mem::transmute(((self.attributes >> $bitshift) & $bitmask) as u8) } } #[inline] pub fn $setter(&mut self, value: $enum) -> &mut Self { - let value = value as u16; + let value = value as u32; let clear = !($bitmask << $bitshift); let attr_value = (value & $bitmask) << $bitshift; self.attributes = (self.attributes & clear) | attr_value; @@ -164,7 +164,7 @@ macro_rules! bitfield { /// by the shell or application that the user is interacting with. #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, FromDynamic, ToDynamic)] -#[repr(u16)] +#[repr(u8)] pub enum SemanticType { Output = 0, Input = 1, @@ -183,7 +183,7 @@ impl Default for SemanticType { /// as a dimmer color variant. #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Copy, PartialEq, Eq, FromDynamic, ToDynamic)] -#[repr(u16)] +#[repr(u8)] pub enum Intensity { Normal = 0, Bold = 1, @@ -199,7 +199,7 @@ impl Default for Intensity { /// Specify just how underlined you want your `Cell` to be #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, FromDynamic, ToDynamic)] -#[repr(u16)] +#[repr(u8)] pub enum Underline { /// The cell is not underlined None = 0, @@ -233,7 +233,7 @@ impl Into for Underline { /// Specify whether you want to slowly or rapidly annoy your users #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Copy, PartialEq, Eq, FromDynamic, ToDynamic)] -#[repr(u16)] +#[repr(u8)] pub enum Blink { None = 0, Slow = 1, @@ -249,6 +249,15 @@ impl Into for Blink { } } +#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] +#[derive(Debug, Clone, Copy, PartialEq, Eq, FromDynamic, ToDynamic)] +#[repr(u8)] +pub enum VerticalAlign { + BaseLine = 0, + SuperScript = 1, + SubScript = 2, +} + impl Default for CellAttributes { fn default() -> Self { Self::blank() @@ -266,6 +275,7 @@ impl CellAttributes { bitfield!(wrapped, set_wrapped, 11); bitfield!(overline, set_overline, 12); bitfield!(semantic_type, set_semantic_type, SemanticType, 0b11, 13); + bitfield!(vertical_align, set_vertical_align, VerticalAlign, 0b11, 15); pub const fn blank() -> Self { Self { diff --git a/termwiz/src/escape/csi.rs b/termwiz/src/escape/csi.rs index 05ec1da37..3cd9fb072 100644 --- a/termwiz/src/escape/csi.rs +++ b/termwiz/src/escape/csi.rs @@ -1,5 +1,5 @@ use super::OneBased; -use crate::cell::{Blink, Intensity, Underline}; +use crate::cell::{Blink, Intensity, Underline, VerticalAlign}; use crate::color::{AnsiColor, ColorSpec, RgbColor, SrgbaTuple}; use crate::input::{Modifiers, MouseButtons}; use num_derive::*; @@ -1374,15 +1374,16 @@ pub enum Sgr { Foreground(ColorSpec), Background(ColorSpec), Overline(bool), + VerticalAlign(VerticalAlign), } #[cfg(all(test, target_pointer_width = "64"))] #[test] fn sgr_size() { - assert_eq!(std::mem::size_of::(), 2); - assert_eq!(std::mem::size_of::(), 2); + assert_eq!(std::mem::size_of::(), 1); + assert_eq!(std::mem::size_of::(), 1); assert_eq!(std::mem::size_of::(), 20); - assert_eq!(std::mem::size_of::(), 2); + assert_eq!(std::mem::size_of::(), 1); assert_eq!(std::mem::size_of::(), 2); } @@ -1430,6 +1431,9 @@ impl Display for Sgr { Sgr::StrikeThrough(false) => code!(StrikeThroughOff), Sgr::Overline(true) => code!(OverlineOn), Sgr::Overline(false) => code!(OverlineOff), + Sgr::VerticalAlign(VerticalAlign::BaseLine) => code!(VerticalAlignBaseLine), + Sgr::VerticalAlign(VerticalAlign::SuperScript) => code!(VerticalAlignSuperScript), + Sgr::VerticalAlign(VerticalAlign::SubScript) => code!(VerticalAlignSubScript), Sgr::Font(Font::Default) => code!(DefaultFont), Sgr::Font(Font::Alternate(1)) => code!(AltFont1), Sgr::Font(Font::Alternate(2)) => code!(AltFont2), @@ -2483,6 +2487,15 @@ impl<'a> CSIParser<'a> { SgrCode::BlinkOff => one!(Sgr::Blink(Blink::None)), SgrCode::ItalicOn => one!(Sgr::Italic(true)), SgrCode::ItalicOff => one!(Sgr::Italic(false)), + SgrCode::VerticalAlignSuperScript => { + one!(Sgr::VerticalAlign(VerticalAlign::SuperScript)) + } + SgrCode::VerticalAlignSubScript => { + one!(Sgr::VerticalAlign(VerticalAlign::SubScript)) + } + SgrCode::VerticalAlignBaseLine => { + one!(Sgr::VerticalAlign(VerticalAlign::BaseLine)) + } SgrCode::ForegroundColor => { self.parse_sgr_color(params).map(Sgr::Foreground) } @@ -2643,6 +2656,10 @@ pub enum SgrCode { UnderlineColor = 58, ResetUnderlineColor = 59, + VerticalAlignSuperScript = 73, + VerticalAlignSubScript = 74, + VerticalAlignBaseLine = 75, + ForegroundBrightBlack = 90, ForegroundBrightRed = 91, ForegroundBrightGreen = 92, diff --git a/wezterm-gui/src/tabbar.rs b/wezterm-gui/src/tabbar.rs index 37f63a4a7..c37cff4b2 100644 --- a/wezterm-gui/src/tabbar.rs +++ b/wezterm-gui/src/tabbar.rs @@ -445,6 +445,9 @@ pub fn parse_status_text(text: &str, default_cell: CellAttributes) -> Line { Sgr::Overline(o) => { pen.set_overline(o); } + Sgr::VerticalAlign(o) => { + pen.set_vertical_align(o); + } Sgr::Blink(b) => { pen.set_blink(b); } diff --git a/wezterm-gui/src/termwindow/render.rs b/wezterm-gui/src/termwindow/render.rs index f9b268f28..d5de92318 100644 --- a/wezterm-gui/src/termwindow/render.rs +++ b/wezterm-gui/src/termwindow/render.rs @@ -2806,6 +2806,15 @@ impl super::TermWindow { let cluster = &item.cluster; let glyph_info = &item.glyph_info; let images = cluster.attrs.images().unwrap_or_else(|| vec![]); + let valign_adjust = match cluster.attrs.vertical_align() { + termwiz::cell::VerticalAlign::BaseLine => 0., + termwiz::cell::VerticalAlign::SuperScript => { + params.render_metrics.cell_size.height as f32 * -0.25 + } + termwiz::cell::VerticalAlign::SubScript => { + params.render_metrics.cell_size.height as f32 * 0.25 + } + }; // TODO: remember logical/visual mapping for selection #[allow(unused_variables)] @@ -2851,8 +2860,9 @@ impl super::TermWindow { { // First, resolve this glyph to a texture let mut texture = glyph.texture.as_ref().cloned(); + let mut top = cell_height - + (params.render_metrics.descender.get() as f32 + + (params.render_metrics.descender.get() as f32 + valign_adjust - (glyph.y_offset + glyph.bearing_y).get() as f32) * height_scale;