mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Rename bold/italic to strong/emph ✏
This commit is contained in:
parent
730715c064
commit
75e6dbfbe6
@ -14,7 +14,7 @@ use crate::font::FontLoader;
|
|||||||
use crate::geom::Size;
|
use crate::geom::Size;
|
||||||
use crate::style::TextStyle;
|
use crate::style::TextStyle;
|
||||||
|
|
||||||
/// Layouts text into a box.
|
/// Shape text into a box.
|
||||||
pub async fn shape(text: &str, ctx: ShapeOptions<'_>) -> BoxLayout {
|
pub async fn shape(text: &str, ctx: ShapeOptions<'_>) -> BoxLayout {
|
||||||
Shaper::new(text, ctx).layout().await
|
Shaper::new(text, ctx).layout().await
|
||||||
}
|
}
|
||||||
@ -115,11 +115,11 @@ impl<'a> Shaper<'a> {
|
|||||||
async fn select_font(&mut self, c: char) -> Option<(FaceId, GlyphId, f64)> {
|
async fn select_font(&mut self, c: char) -> Option<(FaceId, GlyphId, f64)> {
|
||||||
let mut variant = self.opts.style.variant;
|
let mut variant = self.opts.style.variant;
|
||||||
|
|
||||||
if self.opts.style.bolder {
|
if self.opts.style.strong {
|
||||||
variant.weight = variant.weight.thicken(300);
|
variant.weight = variant.weight.thicken(300);
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.opts.style.italic {
|
if self.opts.style.emph {
|
||||||
variant.style = match variant.style {
|
variant.style = match variant.style {
|
||||||
FontStyle::Normal => FontStyle::Italic,
|
FontStyle::Normal => FontStyle::Italic,
|
||||||
FontStyle::Italic => FontStyle::Normal,
|
FontStyle::Italic => FontStyle::Normal,
|
||||||
|
@ -60,24 +60,24 @@ impl<'a> TreeLayouter<'a> {
|
|||||||
match &node.v {
|
match &node.v {
|
||||||
SynNode::Space => self.layout_space(),
|
SynNode::Space => self.layout_space(),
|
||||||
SynNode::Text(text) => {
|
SynNode::Text(text) => {
|
||||||
if self.style.text.italic {
|
if self.style.text.emph {
|
||||||
decorate(self, Decoration::Italic);
|
decorate(self, Decoration::Emph);
|
||||||
}
|
}
|
||||||
if self.style.text.bolder {
|
if self.style.text.strong {
|
||||||
decorate(self, Decoration::Bold);
|
decorate(self, Decoration::Strong);
|
||||||
}
|
}
|
||||||
self.layout_text(text).await;
|
self.layout_text(text).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
SynNode::Linebreak => self.layouter.finish_line(),
|
SynNode::Linebreak => self.layouter.finish_line(),
|
||||||
SynNode::Parbreak => self.layout_parbreak(),
|
SynNode::Parbreak => self.layout_parbreak(),
|
||||||
SynNode::ToggleItalic => {
|
SynNode::Emph => {
|
||||||
self.style.text.italic = !self.style.text.italic;
|
self.style.text.emph = !self.style.text.emph;
|
||||||
decorate(self, Decoration::Italic);
|
decorate(self, Decoration::Emph);
|
||||||
}
|
}
|
||||||
SynNode::ToggleBolder => {
|
SynNode::Strong => {
|
||||||
self.style.text.bolder = !self.style.text.bolder;
|
self.style.text.strong = !self.style.text.strong;
|
||||||
decorate(self, Decoration::Bold);
|
decorate(self, Decoration::Strong);
|
||||||
}
|
}
|
||||||
|
|
||||||
SynNode::Heading(heading) => self.layout_heading(heading).await,
|
SynNode::Heading(heading) => self.layout_heading(heading).await,
|
||||||
@ -116,7 +116,7 @@ impl<'a> TreeLayouter<'a> {
|
|||||||
async fn layout_heading(&mut self, heading: &NodeHeading) {
|
async fn layout_heading(&mut self, heading: &NodeHeading) {
|
||||||
let style = self.style.text.clone();
|
let style = self.style.text.clone();
|
||||||
self.style.text.font_scale *= 1.5 - 0.1 * heading.level.v as f64;
|
self.style.text.font_scale *= 1.5 - 0.1 * heading.level.v as f64;
|
||||||
self.style.text.bolder = true;
|
self.style.text.strong = true;
|
||||||
|
|
||||||
self.layout_parbreak();
|
self.layout_parbreak();
|
||||||
self.layout_tree(&heading.contents).await;
|
self.layout_tree(&heading.contents).await;
|
||||||
|
@ -62,8 +62,8 @@ fn node(p: &mut Parser, at_start: bool) -> Option<Spanned<SynNode>> {
|
|||||||
Token::LineComment(_) | Token::BlockComment(_) => return None,
|
Token::LineComment(_) | Token::BlockComment(_) => return None,
|
||||||
|
|
||||||
// Markup.
|
// Markup.
|
||||||
Token::Star => SynNode::ToggleBolder,
|
Token::Star => SynNode::Strong,
|
||||||
Token::Underscore => SynNode::ToggleItalic,
|
Token::Underscore => SynNode::Emph,
|
||||||
Token::Backslash => SynNode::Linebreak,
|
Token::Backslash => SynNode::Linebreak,
|
||||||
Token::Hashtag => {
|
Token::Hashtag => {
|
||||||
if at_start {
|
if at_start {
|
||||||
|
@ -13,9 +13,7 @@ use crate::syntax::*;
|
|||||||
// ------------------------------ Construct Syntax Nodes ------------------------------ //
|
// ------------------------------ Construct Syntax Nodes ------------------------------ //
|
||||||
|
|
||||||
use Decoration::*;
|
use Decoration::*;
|
||||||
use SynNode::{
|
use SynNode::{Emph as E, Linebreak as L, Parbreak as P, Space as S, Strong as B};
|
||||||
Linebreak as L, Parbreak as P, Space as S, ToggleBolder as B, ToggleItalic as I,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn T(text: &str) -> SynNode {
|
fn T(text: &str) -> SynNode {
|
||||||
SynNode::Text(text.to_string())
|
SynNode::Text(text.to_string())
|
||||||
@ -230,7 +228,7 @@ fn test_parse_simple_nodes() {
|
|||||||
t!("" => );
|
t!("" => );
|
||||||
t!("hi" => T("hi"));
|
t!("hi" => T("hi"));
|
||||||
t!("*hi" => B, T("hi"));
|
t!("*hi" => B, T("hi"));
|
||||||
t!("hi_" => T("hi"), I);
|
t!("hi_" => T("hi"), E);
|
||||||
t!("hi you" => T("hi"), S, T("you"));
|
t!("hi you" => T("hi"), S, T("you"));
|
||||||
t!("special~name" => T("special"), T("\u{00A0}"), T("name"));
|
t!("special~name" => T("special"), T("\u{00A0}"), T("name"));
|
||||||
t!("special\\~name" => T("special"), T("~"), T("name"));
|
t!("special\\~name" => T("special"), T("~"), T("name"));
|
||||||
@ -415,7 +413,7 @@ fn test_parse_values() {
|
|||||||
v!("\"a\n[]\\\"string\"" => Str("a\n[]\"string"));
|
v!("\"a\n[]\\\"string\"" => Str("a\n[]\"string"));
|
||||||
|
|
||||||
// Content.
|
// Content.
|
||||||
v!("{_hi_}" => Tree![I, T("hi"), I]);
|
v!("{_hi_}" => Tree![E, T("hi"), E]);
|
||||||
e!("[val: {_hi_}]" => );
|
e!("[val: {_hi_}]" => );
|
||||||
v!("[hi]" => Tree![F!("hi")]);
|
v!("[hi]" => Tree![F!("hi")]);
|
||||||
e!("[val: [hi]]" => );
|
e!("[val: [hi]]" => );
|
||||||
|
12
src/style.rs
12
src/style.rs
@ -22,12 +22,12 @@ pub struct TextStyle {
|
|||||||
pub fallback: FallbackTree,
|
pub fallback: FallbackTree,
|
||||||
/// The selected font variant.
|
/// The selected font variant.
|
||||||
pub variant: FontVariant,
|
pub variant: FontVariant,
|
||||||
/// Whether the bolder toggle is active or inactive. This determines
|
/// Whether the strong toggle is active or inactive. This determines
|
||||||
/// whether the next `*` adds or removes font weight.
|
/// whether the next `*` adds or removes font weight.
|
||||||
pub bolder: bool,
|
pub strong: bool,
|
||||||
/// Whether the italic toggle is active or inactive. This determines
|
/// Whether the emphasis toggle is active or inactive. This determines
|
||||||
/// whether the next `_` makes italic or non-italic.
|
/// whether the next `_` makes italic or non-italic.
|
||||||
pub italic: bool,
|
pub emph: bool,
|
||||||
/// The base font size.
|
/// The base font size.
|
||||||
pub base_font_size: f64,
|
pub base_font_size: f64,
|
||||||
/// The font scale to apply on the base font size.
|
/// The font scale to apply on the base font size.
|
||||||
@ -83,8 +83,8 @@ impl Default for TextStyle {
|
|||||||
weight: FontWeight::REGULAR,
|
weight: FontWeight::REGULAR,
|
||||||
stretch: FontStretch::Normal,
|
stretch: FontStretch::Normal,
|
||||||
},
|
},
|
||||||
bolder: false,
|
strong: false,
|
||||||
italic: false,
|
emph: false,
|
||||||
base_font_size: Length::pt(11.0).as_raw(),
|
base_font_size: Length::pt(11.0).as_raw(),
|
||||||
font_scale: 1.0,
|
font_scale: 1.0,
|
||||||
word_spacing_scale: 0.25,
|
word_spacing_scale: 0.25,
|
||||||
|
@ -18,10 +18,10 @@ pub enum SynNode {
|
|||||||
Linebreak,
|
Linebreak,
|
||||||
/// A paragraph break.
|
/// A paragraph break.
|
||||||
Parbreak,
|
Parbreak,
|
||||||
/// Italics were enabled / disabled.
|
/// Emphasized text was enabled / disabled.
|
||||||
ToggleItalic,
|
Emph,
|
||||||
/// Bolder was enabled / disabled.
|
/// Strong text was enabled / disabled.
|
||||||
ToggleBolder,
|
Strong,
|
||||||
|
|
||||||
/// A section heading.
|
/// A section heading.
|
||||||
Heading(NodeHeading),
|
Heading(NodeHeading),
|
||||||
|
@ -16,10 +16,10 @@ pub use token::*;
|
|||||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
|
||||||
#[cfg_attr(feature = "serialize", serde(rename_all = "camelCase"))]
|
#[cfg_attr(feature = "serialize", serde(rename_all = "camelCase"))]
|
||||||
pub enum Decoration {
|
pub enum Decoration {
|
||||||
/// Text in italics.
|
/// Emphasized text.
|
||||||
Italic,
|
Emph,
|
||||||
/// Text in bold.
|
/// Strong text.
|
||||||
Bold,
|
Strong,
|
||||||
/// A valid, successfully resolved name.
|
/// A valid, successfully resolved name.
|
||||||
Resolved,
|
Resolved,
|
||||||
/// An invalid, unresolved name.
|
/// An invalid, unresolved name.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user