mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Refactor and move shaping out of the layout module ⛳
This commit is contained in:
parent
54e0da59e3
commit
262a8fa36a
@ -3,7 +3,6 @@
|
|||||||
pub mod elements;
|
pub mod elements;
|
||||||
pub mod line;
|
pub mod line;
|
||||||
pub mod primitive;
|
pub mod primitive;
|
||||||
pub mod shaping;
|
|
||||||
pub mod stack;
|
pub mod stack;
|
||||||
mod tree;
|
mod tree;
|
||||||
|
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
//! Layouting of syntax trees.
|
//! Layouting of syntax trees.
|
||||||
|
|
||||||
use super::line::{LineContext, LineLayouter};
|
use super::line::{LineContext, LineLayouter};
|
||||||
use super::shaping::{shape, ShapeOptions};
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::shaping;
|
||||||
use crate::style::LayoutStyle;
|
use crate::style::LayoutStyle;
|
||||||
use crate::syntax::{
|
use crate::syntax::{
|
||||||
Decoration, Expr, NodeHeading, NodeRaw, Span, SpanWith, Spanned, SynNode, SynTree,
|
Decoration, Expr, NodeHeading, NodeRaw, Span, SpanWith, Spanned, SynNode, SynTree,
|
||||||
@ -103,12 +103,13 @@ impl<'a> TreeLayouter<'a> {
|
|||||||
|
|
||||||
async fn layout_text(&mut self, text: &str) {
|
async fn layout_text(&mut self, text: &str) {
|
||||||
self.layouter.add(
|
self.layouter.add(
|
||||||
shape(text, ShapeOptions {
|
shaping::shape(
|
||||||
loader: &mut self.ctx.loader.borrow_mut(),
|
text,
|
||||||
style: &self.style.text,
|
self.ctx.sys.primary,
|
||||||
dir: self.ctx.sys.primary,
|
self.ctx.align,
|
||||||
align: self.ctx.align,
|
&self.style.text,
|
||||||
})
|
&mut self.ctx.loader.borrow_mut(),
|
||||||
|
)
|
||||||
.await,
|
.await,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,7 @@ pub mod library;
|
|||||||
pub mod paper;
|
pub mod paper;
|
||||||
pub mod parse;
|
pub mod parse;
|
||||||
pub mod prelude;
|
pub mod prelude;
|
||||||
|
pub mod shaping;
|
||||||
pub mod style;
|
pub mod style;
|
||||||
pub mod syntax;
|
pub mod syntax;
|
||||||
|
|
||||||
|
@ -1,73 +1,88 @@
|
|||||||
//! Super-basic text shaping.
|
//! Super-basic text shaping.
|
||||||
//!
|
//!
|
||||||
//! The layouter picks the most suitable font for each individual character. When the
|
//! This is really only suited for simple Latin text. It picks the most suitable
|
||||||
//! direction is right-to-left, the word is spelled backwards. Vertical shaping is not yet
|
//! font for each individual character. When the direction is right-to-left, the
|
||||||
//! supported.
|
//! word is spelled backwards. Vertical shaping is not supported.
|
||||||
|
|
||||||
use fontdock::{FaceId, FaceQuery, FontStyle};
|
use fontdock::{FaceId, FaceQuery, FallbackTree, FontStyle, FontVariant};
|
||||||
use ttf_parser::GlyphId;
|
use ttf_parser::GlyphId;
|
||||||
|
|
||||||
use super::elements::{LayoutElement, Shaped};
|
|
||||||
use super::BoxLayout as Layout;
|
|
||||||
use super::*;
|
|
||||||
use crate::font::FontLoader;
|
use crate::font::FontLoader;
|
||||||
|
use crate::geom::{Point, Size};
|
||||||
|
use crate::layout::elements::{LayoutElement, LayoutElements, Shaped};
|
||||||
|
use crate::layout::{BoxLayout, Dir, LayoutAlign};
|
||||||
use crate::style::TextStyle;
|
use crate::style::TextStyle;
|
||||||
|
|
||||||
/// Shape text into a box.
|
/// Shape text into a box.
|
||||||
pub async fn shape(text: &str, ctx: ShapeOptions<'_>) -> BoxLayout {
|
pub async fn shape(
|
||||||
Shaper::new(text, ctx).layout().await
|
text: &str,
|
||||||
}
|
dir: Dir,
|
||||||
|
align: LayoutAlign,
|
||||||
/// Options for text shaping.
|
style: &TextStyle,
|
||||||
#[derive(Debug)]
|
loader: &mut FontLoader,
|
||||||
pub struct ShapeOptions<'a> {
|
) -> BoxLayout {
|
||||||
/// The font loader to retrieve fonts from.
|
Shaper::new(text, dir, align, style, loader).shape().await
|
||||||
pub loader: &'a mut FontLoader,
|
|
||||||
/// The style for text: Font selection with classes, weights and variants,
|
|
||||||
/// font sizes, spacing and so on.
|
|
||||||
pub style: &'a TextStyle,
|
|
||||||
/// The direction into which the text is laid out. Currently, only horizontal
|
|
||||||
/// directions are supported.
|
|
||||||
pub dir: Dir,
|
|
||||||
/// The alignment of the _resulting_ layout. This does not effect the line
|
|
||||||
/// layouting itself, but rather how the finished layout will be positioned
|
|
||||||
/// in a parent layout.
|
|
||||||
pub align: LayoutAlign,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Performs super-basic text shaping.
|
/// Performs super-basic text shaping.
|
||||||
struct Shaper<'a> {
|
struct Shaper<'a> {
|
||||||
opts: ShapeOptions<'a>,
|
|
||||||
text: &'a str,
|
text: &'a str,
|
||||||
|
dir: Dir,
|
||||||
|
variant: FontVariant,
|
||||||
|
fallback: &'a FallbackTree,
|
||||||
|
loader: &'a mut FontLoader,
|
||||||
shaped: Shaped,
|
shaped: Shaped,
|
||||||
layout: Layout,
|
layout: BoxLayout,
|
||||||
offset: f64,
|
offset: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Shaper<'a> {
|
impl<'a> Shaper<'a> {
|
||||||
fn new(text: &'a str, opts: ShapeOptions<'a>) -> Self {
|
fn new(
|
||||||
|
text: &'a str,
|
||||||
|
dir: Dir,
|
||||||
|
align: LayoutAlign,
|
||||||
|
style: &'a TextStyle,
|
||||||
|
loader: &'a mut FontLoader,
|
||||||
|
) -> Self {
|
||||||
|
let mut variant = style.variant;
|
||||||
|
|
||||||
|
if style.strong {
|
||||||
|
variant.weight = variant.weight.thicken(300);
|
||||||
|
}
|
||||||
|
|
||||||
|
if style.emph {
|
||||||
|
variant.style = match variant.style {
|
||||||
|
FontStyle::Normal => FontStyle::Italic,
|
||||||
|
FontStyle::Italic => FontStyle::Normal,
|
||||||
|
FontStyle::Oblique => FontStyle::Normal,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
text,
|
text,
|
||||||
shaped: Shaped::new(FaceId::MAX, opts.style.font_size()),
|
dir,
|
||||||
|
variant,
|
||||||
|
fallback: &style.fallback,
|
||||||
|
loader,
|
||||||
|
shaped: Shaped::new(FaceId::MAX, style.font_size()),
|
||||||
layout: BoxLayout {
|
layout: BoxLayout {
|
||||||
size: Size::new(0.0, opts.style.font_size()),
|
size: Size::new(0.0, style.font_size()),
|
||||||
align: opts.align,
|
align,
|
||||||
elements: LayoutElements::new(),
|
elements: LayoutElements::new(),
|
||||||
},
|
},
|
||||||
offset: 0.0,
|
offset: 0.0,
|
||||||
opts,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn layout(mut self) -> Layout {
|
async fn shape(mut self) -> BoxLayout {
|
||||||
// If the primary axis is negative, we layout the characters reversed.
|
// If the primary axis is negative, we layout the characters reversed.
|
||||||
if self.opts.dir.is_positive() {
|
if self.dir.is_positive() {
|
||||||
for c in self.text.chars() {
|
for c in self.text.chars() {
|
||||||
self.layout_char(c).await;
|
self.shape_char(c).await;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for c in self.text.chars().rev() {
|
for c in self.text.chars().rev() {
|
||||||
self.layout_char(c).await;
|
self.shape_char(c).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,7 +95,7 @@ impl<'a> Shaper<'a> {
|
|||||||
self.layout
|
self.layout
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn layout_char(&mut self, c: char) {
|
async fn shape_char(&mut self, c: char) {
|
||||||
let (index, glyph, char_width) = match self.select_font(c).await {
|
let (index, glyph, char_width) = match self.select_font(c).await {
|
||||||
Some(selected) => selected,
|
Some(selected) => selected,
|
||||||
// TODO: Issue warning about missing character.
|
// TODO: Issue warning about missing character.
|
||||||
@ -93,7 +108,7 @@ impl<'a> Shaper<'a> {
|
|||||||
if !self.shaped.text.is_empty() {
|
if !self.shaped.text.is_empty() {
|
||||||
let shaped = std::mem::replace(
|
let shaped = std::mem::replace(
|
||||||
&mut self.shaped,
|
&mut self.shaped,
|
||||||
Shaped::new(FaceId::MAX, self.opts.style.font_size()),
|
Shaped::new(FaceId::MAX, self.layout.size.height),
|
||||||
);
|
);
|
||||||
|
|
||||||
let pos = Point::new(self.offset, 0.0);
|
let pos = Point::new(self.offset, 0.0);
|
||||||
@ -112,32 +127,18 @@ 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;
|
|
||||||
|
|
||||||
if self.opts.style.strong {
|
|
||||||
variant.weight = variant.weight.thicken(300);
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.opts.style.emph {
|
|
||||||
variant.style = match variant.style {
|
|
||||||
FontStyle::Normal => FontStyle::Italic,
|
|
||||||
FontStyle::Italic => FontStyle::Normal,
|
|
||||||
FontStyle::Oblique => FontStyle::Normal,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let query = FaceQuery {
|
let query = FaceQuery {
|
||||||
fallback: self.opts.style.fallback.iter(),
|
fallback: self.fallback.iter(),
|
||||||
variant,
|
variant: self.variant,
|
||||||
c,
|
c,
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some((id, owned_face)) = self.opts.loader.query(query).await {
|
if let Some((id, owned_face)) = self.loader.query(query).await {
|
||||||
let face = owned_face.get();
|
let face = owned_face.get();
|
||||||
let font_size = self.opts.style.font_size();
|
|
||||||
|
|
||||||
let units_per_em = face.units_per_em().unwrap_or(1000) as f64;
|
let units_per_em = face.units_per_em().unwrap_or(1000) as f64;
|
||||||
let ratio = 1.0 / units_per_em;
|
let ratio = 1.0 / units_per_em;
|
||||||
|
let font_size = self.layout.size.height;
|
||||||
let to_raw = |x| ratio * x as f64 * font_size;
|
let to_raw = |x| ratio * x as f64 * font_size;
|
||||||
|
|
||||||
// Determine the width of the char.
|
// Determine the width of the char.
|
Loading…
x
Reference in New Issue
Block a user