summaryrefslogtreecommitdiff
path: root/src/doc.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-05-03 10:33:18 +0200
committerLaurenz <laurmaedje@gmail.com>2023-05-03 10:33:18 +0200
commitad347632ab95e29eb5180b27142f5c264dfc611a (patch)
tree2742a33f4c3d800a86e977de04fa2cec7104c43f /src/doc.rs
parentbcc014c4e177cc4e8cf5ca8c24990908b507c0f8 (diff)
Make ligatures copyable and searchable
Fixes #479 Fixes #1040
Diffstat (limited to 'src/doc.rs')
-rw-r--r--src/doc.rs50
1 files changed, 18 insertions, 32 deletions
diff --git a/src/doc.rs b/src/doc.rs
index 0a744ffc..0a9b9a8d 100644
--- a/src/doc.rs
+++ b/src/doc.rs
@@ -1,7 +1,8 @@
//! Finished documents.
-use std::fmt::{self, Debug, Formatter, Write};
+use std::fmt::{self, Debug, Formatter};
use std::num::NonZeroUsize;
+use std::ops::Range;
use std::str::FromStr;
use std::sync::Arc;
@@ -114,23 +115,6 @@ impl Frame {
pub fn items(&self) -> std::slice::Iter<'_, (Point, FrameItem)> {
self.items.iter()
}
-
- /// Approximately recover the text inside of the frame and its children.
- pub fn text(&self) -> EcoString {
- let mut text = EcoString::new();
- for (_, item) in self.items() {
- match item {
- FrameItem::Text(item) => {
- for glyph in &item.glyphs {
- text.push(glyph.c);
- }
- }
- FrameItem::Group(group) => text.push_str(&group.frame.text()),
- _ => {}
- }
- }
- text
- }
}
/// Insert items and subframes.
@@ -476,6 +460,8 @@ pub struct TextItem {
pub fill: Paint,
/// The natural language of the text.
pub lang: Lang,
+ /// The item's plain text.
+ pub text: EcoString,
/// The glyphs.
pub glyphs: Vec<Glyph>,
}
@@ -489,19 +475,14 @@ impl TextItem {
impl Debug for TextItem {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- // This is only a rough approximation of the source text.
- f.write_str("Text(\"")?;
- for glyph in &self.glyphs {
- for c in glyph.c.escape_debug() {
- f.write_char(c)?;
- }
- }
- f.write_str("\")")
+ f.write_str("Text(")?;
+ self.text.fmt(f)?;
+ f.write_str(")")
}
}
/// A glyph in a run of shaped text.
-#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
+#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Glyph {
/// The glyph's index in the font.
pub id: u16,
@@ -509,12 +490,17 @@ pub struct Glyph {
pub x_advance: Em,
/// The horizontal offset of the glyph.
pub x_offset: Em,
- /// The first character of the glyph's cluster.
- pub c: char,
+ /// The range of the glyph in its item's text.
+ pub range: Range<u16>,
/// The source code location of the text.
- pub span: Span,
- /// The offset within the spanned text.
- pub offset: u16,
+ pub span: (Span, u16),
+}
+
+impl Glyph {
+ /// The range of the glyph in its item's text.
+ pub fn range(&self) -> Range<usize> {
+ usize::from(self.range.start)..usize::from(self.range.end)
+ }
}
/// An identifier for a natural language.