mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Fix generated strong and emphasized text
This commit is contained in:
parent
5a7c901f21
commit
08554380f8
@ -1,6 +1,6 @@
|
||||
use crate::library::layout::BlockSpacing;
|
||||
use crate::library::prelude::*;
|
||||
use crate::library::text::{FontFamily, TextNode, TextSize, Toggle};
|
||||
use crate::library::text::{FontFamily, TextNode, TextSize};
|
||||
|
||||
/// A section heading.
|
||||
#[derive(Debug, Hash)]
|
||||
@ -103,11 +103,11 @@ impl Show for HeadingNode {
|
||||
}
|
||||
|
||||
if resolve!(Self::STRONG) {
|
||||
map.set(TextNode::STRONG, Toggle);
|
||||
realized = realized.strong();
|
||||
}
|
||||
|
||||
if resolve!(Self::EMPH) {
|
||||
map.set(TextNode::EMPH, Toggle);
|
||||
realized = realized.emph();
|
||||
}
|
||||
|
||||
if resolve!(Self::UNDERLINE) {
|
||||
|
@ -110,10 +110,10 @@ impl TextNode {
|
||||
|
||||
/// Whether the font weight should be increased by 300.
|
||||
#[property(skip, fold)]
|
||||
pub const STRONG: Toggle = false;
|
||||
pub const BOLD: Toggle = false;
|
||||
/// Whether the the font style should be inverted.
|
||||
#[property(skip, fold)]
|
||||
pub const EMPH: Toggle = false;
|
||||
pub const ITALIC: Toggle = false;
|
||||
/// A case transformation that should be applied to the text.
|
||||
#[property(skip)]
|
||||
pub const CASE: Option<Case> = None;
|
||||
@ -508,7 +508,7 @@ impl Fold for Decoration {
|
||||
}
|
||||
}
|
||||
|
||||
/// Strong text, rendered in boldface.
|
||||
/// Strong text, rendered in boldface by default.
|
||||
#[derive(Debug, Hash)]
|
||||
pub struct StrongNode(pub Content);
|
||||
|
||||
@ -529,11 +529,11 @@ impl Show for StrongNode {
|
||||
}
|
||||
|
||||
fn realize(&self, _: &mut Context, _: StyleChain) -> TypResult<Content> {
|
||||
Ok(self.0.clone().styled(TextNode::STRONG, Toggle))
|
||||
Ok(self.0.clone().styled(TextNode::BOLD, Toggle))
|
||||
}
|
||||
}
|
||||
|
||||
/// Emphasized text, rendered with an italic face.
|
||||
/// Emphasized text, rendered with an italic face by default.
|
||||
#[derive(Debug, Hash)]
|
||||
pub struct EmphNode(pub Content);
|
||||
|
||||
@ -554,6 +554,6 @@ impl Show for EmphNode {
|
||||
}
|
||||
|
||||
fn realize(&self, _: &mut Context, _: StyleChain) -> TypResult<Content> {
|
||||
Ok(self.0.clone().styled(TextNode::EMPH, Toggle))
|
||||
Ok(self.0.clone().styled(TextNode::ITALIC, Toggle))
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ use syntect::highlighting::{
|
||||
};
|
||||
use syntect::parsing::SyntaxSet;
|
||||
|
||||
use super::{FontFamily, Hyphenate, TextNode, Toggle};
|
||||
use super::{FontFamily, Hyphenate, TextNode};
|
||||
use crate::library::layout::BlockSpacing;
|
||||
use crate::library::prelude::*;
|
||||
use crate::source::SourceId;
|
||||
@ -137,27 +137,26 @@ impl Show for RawNode {
|
||||
|
||||
/// Style a piece of text with a syntect style.
|
||||
fn styled(piece: &str, foreground: Paint, style: Style) -> Content {
|
||||
let mut styles = StyleMap::new();
|
||||
let mut body = Content::Text(piece.into());
|
||||
|
||||
let paint = style.foreground.into();
|
||||
if paint != foreground {
|
||||
styles.set(TextNode::FILL, paint);
|
||||
body = body.styled(TextNode::FILL, paint);
|
||||
}
|
||||
|
||||
if style.font_style.contains(FontStyle::BOLD) {
|
||||
styles.set(TextNode::STRONG, Toggle);
|
||||
body = body.strong();
|
||||
}
|
||||
|
||||
if style.font_style.contains(FontStyle::ITALIC) {
|
||||
styles.set(TextNode::EMPH, Toggle);
|
||||
body = body.emph();
|
||||
}
|
||||
|
||||
if style.font_style.contains(FontStyle::UNDERLINE) {
|
||||
body = body.underlined();
|
||||
}
|
||||
|
||||
body.styled_with_map(styles)
|
||||
body
|
||||
}
|
||||
|
||||
/// The lazily-loaded syntect syntax definitions.
|
||||
|
@ -517,11 +517,11 @@ pub fn variant(styles: StyleChain) -> FontVariant {
|
||||
styles.get(TextNode::STRETCH),
|
||||
);
|
||||
|
||||
if styles.get(TextNode::STRONG) {
|
||||
if styles.get(TextNode::BOLD) {
|
||||
variant.weight = variant.weight.thicken(300);
|
||||
}
|
||||
|
||||
if styles.get(TextNode::EMPH) {
|
||||
if styles.get(TextNode::ITALIC) {
|
||||
variant.style = match variant.style {
|
||||
FontStyle::Normal => FontStyle::Italic,
|
||||
FontStyle::Italic => FontStyle::Normal,
|
||||
|
@ -14,7 +14,9 @@ use crate::diag::StrResult;
|
||||
use crate::library::layout::{FlowChild, FlowNode, PageNode, PlaceNode, Spacing};
|
||||
use crate::library::prelude::*;
|
||||
use crate::library::structure::{DocNode, ListItem, ListNode, ORDERED, UNORDERED};
|
||||
use crate::library::text::{DecoNode, ParChild, ParNode, UNDERLINE};
|
||||
use crate::library::text::{
|
||||
DecoNode, EmphNode, ParChild, ParNode, StrongNode, UNDERLINE,
|
||||
};
|
||||
use crate::util::EcoString;
|
||||
|
||||
/// Composable representation of styled content.
|
||||
@ -164,6 +166,16 @@ impl Content {
|
||||
self.clone().styled_with_entry(StyleEntry::Unguard(sel))
|
||||
}
|
||||
|
||||
/// Make this content strong.
|
||||
pub fn strong(self) -> Self {
|
||||
Self::show(StrongNode(self))
|
||||
}
|
||||
|
||||
/// Make this content emphasized.
|
||||
pub fn emph(self) -> Self {
|
||||
Self::show(EmphNode(self))
|
||||
}
|
||||
|
||||
/// Underline this content.
|
||||
pub fn underlined(self) -> Self {
|
||||
Self::show(DecoNode::<UNDERLINE>(self))
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 24 KiB |
@ -46,6 +46,8 @@ multiline.
|
||||
= Heading
|
||||
|
||||
#set heading(family: "Roboto", fill: eastern)
|
||||
#show it: heading as it.body
|
||||
#show it: strong as it.body + [!]
|
||||
|
||||
===== Heading 🌍
|
||||
#heading(level: 5)[Heading]
|
||||
|
Loading…
x
Reference in New Issue
Block a user