mirror of
https://github.com/typst/typst
synced 2025-08-18 00:48:34 +08:00
Compare commits
13 Commits
ee36e98549
...
552fb46ecb
Author | SHA1 | Date | |
---|---|---|---|
|
552fb46ecb | ||
|
0264534928 | ||
|
4aa6dcf578 | ||
|
34e2e43445 | ||
|
0148a5913d | ||
|
c8d45148f1 | ||
|
a445a3108f | ||
|
69d0d9e26d | ||
|
4fa7e5cfd9 | ||
|
7d6d46c4d8 | ||
|
07ba1c1636 | ||
|
422eb36f16 | ||
|
7f9adfac22 |
@ -130,7 +130,14 @@ fn complete_markup(ctx: &mut CompletionContext) -> bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Start of a reference: "@|" or "@he|".
|
||||
// Start of a reference: "@|".
|
||||
if ctx.leaf.kind() == SyntaxKind::Text && ctx.before.ends_with("@") {
|
||||
ctx.from = ctx.cursor;
|
||||
ctx.label_completions();
|
||||
return true;
|
||||
}
|
||||
|
||||
// An existing reference: "@he|".
|
||||
if ctx.leaf.kind() == SyntaxKind::RefMarker {
|
||||
ctx.from = ctx.leaf.offset() + 1;
|
||||
ctx.label_completions();
|
||||
@ -1644,6 +1651,19 @@ mod tests {
|
||||
test_with_doc(world, pos, doc.as_ref())
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
fn test_with_addition(
|
||||
initial_text: &str,
|
||||
addition: &str,
|
||||
pos: impl FilePos,
|
||||
) -> Response {
|
||||
let mut world = TestWorld::new(initial_text);
|
||||
let doc = typst::compile(&world).output.ok();
|
||||
let end = world.main.text().len();
|
||||
world.main.edit(end..end, addition);
|
||||
test_with_doc(&world, pos, doc.as_ref())
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
fn test_with_doc(
|
||||
world: impl WorldLike,
|
||||
@ -1709,15 +1729,24 @@ mod tests {
|
||||
.must_exclude(["bib"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_autocomplete_ref_function() {
|
||||
test_with_addition("x<test>", " #ref(<)", -2).must_include(["test"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_autocomplete_ref_shorthand() {
|
||||
test_with_addition("x<test>", " @", -1).must_include(["test"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_autocomplete_ref_shorthand_with_partial_identifier() {
|
||||
test_with_addition("x<test>", " @te", -1).must_include(["test"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_autocomplete_ref_identical_labels_returns_single_completion() {
|
||||
let mut world = TestWorld::new("x<test> y<test>");
|
||||
let doc = typst::compile(&world).output.ok();
|
||||
|
||||
let end = world.main.text().len();
|
||||
world.main.edit(end..end, " @t");
|
||||
|
||||
let result = test_with_doc(&world, -1, doc.as_ref());
|
||||
let result = test_with_addition("x<test> y<test>", " @t", -1);
|
||||
let completions = result.completions();
|
||||
let label_count =
|
||||
completions.iter().filter(|c| c.kind == CompletionKind::Label).count();
|
||||
|
@ -9,6 +9,7 @@ use ttf_parser::Tag;
|
||||
use typst_library::engine::Engine;
|
||||
use typst_library::foundations::{Smart, StyleChain};
|
||||
use typst_library::layout::{Abs, Dir, Em, Frame, FrameItem, Point, Size};
|
||||
use typst_library::model::{Microtype, ParElem};
|
||||
use typst_library::text::{
|
||||
families, features, is_default_ignorable, language, variant, Font, FontFamily,
|
||||
FontVariant, Glyph, Lang, Region, ShiftSettings, TextEdgeBounds, TextElem, TextItem,
|
||||
@ -144,7 +145,11 @@ impl ShapedGlyph {
|
||||
|| self.c.is_ascii_digit()
|
||||
}
|
||||
|
||||
pub fn base_adjustability(&self, style: CjkPunctStyle) -> Adjustability {
|
||||
pub fn base_adjustability(
|
||||
&self,
|
||||
style: CjkPunctStyle,
|
||||
microtype: Microtype,
|
||||
) -> Adjustability {
|
||||
let width = self.x_advance;
|
||||
if self.is_space() {
|
||||
Adjustability {
|
||||
@ -168,7 +173,10 @@ impl ShapedGlyph {
|
||||
shrinkability: (width / 4.0, width / 4.0),
|
||||
}
|
||||
} else {
|
||||
Adjustability::default()
|
||||
Adjustability {
|
||||
stretchability: (Em::zero(), microtype.max_expand.em),
|
||||
shrinkability: (Em::zero(), microtype.max_retract.em),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1103,9 +1111,10 @@ fn track_and_space(ctx: &mut ShapingContext) {
|
||||
/// and CJK punctuation adjustments according to Chinese Layout Requirements.
|
||||
fn calculate_adjustability(ctx: &mut ShapingContext, lang: Lang, region: Option<Region>) {
|
||||
let style = cjk_punct_style(lang, region);
|
||||
let microtype = ParElem::microtype_in(ctx.styles);
|
||||
|
||||
for glyph in &mut ctx.glyphs {
|
||||
glyph.adjustability = glyph.base_adjustability(style);
|
||||
glyph.adjustability = glyph.base_adjustability(style, microtype);
|
||||
}
|
||||
|
||||
let mut glyphs = ctx.glyphs.iter_mut().peekable();
|
||||
|
@ -136,6 +136,9 @@ pub struct ParElem {
|
||||
#[default(false)]
|
||||
pub justify: bool,
|
||||
|
||||
/// Microtypographical settings that are used during justification.
|
||||
pub microtype: Microtype,
|
||||
|
||||
/// How to determine line breaks.
|
||||
///
|
||||
/// When this property is set to `{auto}`, its default value, optimized line
|
||||
@ -224,6 +227,36 @@ impl ParElem {
|
||||
type ParLine;
|
||||
}
|
||||
|
||||
/// Configuration for microtypographical settings to be used during
|
||||
/// justification.
|
||||
#[derive(Debug, Default, Copy, Clone, PartialEq, Hash)]
|
||||
pub struct Microtype {
|
||||
/// How much a glyph is allowed to translate into its neighbor.
|
||||
pub max_retract: Length,
|
||||
/// How much a glyph is allowed to translate away from its neighbor.
|
||||
pub max_expand: Length,
|
||||
}
|
||||
|
||||
cast! {
|
||||
Microtype,
|
||||
self => Value::Dict(self.into()),
|
||||
mut dict: Dict => {
|
||||
let max_retract = dict.take("max-retract")?.cast()?;
|
||||
let max_expand = dict.take("max-expand")?.cast()?;
|
||||
dict.finish(&["max-retract", "max-expand"])?;
|
||||
Self { max_retract, max_expand }
|
||||
},
|
||||
}
|
||||
|
||||
impl From<Microtype> for Dict {
|
||||
fn from(microtype: Microtype) -> Self {
|
||||
dict! {
|
||||
"max-retract" => microtype.max_retract,
|
||||
"max-expand" => microtype.max_expand,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// How to determine line breaks in a paragraph.
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)]
|
||||
pub enum Linebreaks {
|
||||
|
Loading…
x
Reference in New Issue
Block a user