Skip to content

Commit

Permalink
Remove redundant usvg_tree::Text::positions.
Browse files Browse the repository at this point in the history
  • Loading branch information
RazrFalcon committed Nov 18, 2023
1 parent 9101f30 commit b8938f4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 53 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
This changelog also contains important changes in dependencies.

## [Unreleased]
### Changed
- `usvg_tree::Text::positions` was replaced with `usvg_tree::Text::dx` and `usvg_tree::Text::dy`.<br>
`usvg_tree::CharacterPosition::x` and `usvg_tree::CharacterPosition::y` are gone.
They were redundant and you should use `usvg_tree::TextChunk::x`
and `usvg_tree::TextChunk::y` instead.

### Removed
- `usvg_tree::CharacterPosition`

## [0.36.0] - 2023-10-01
### Added
Expand Down
18 changes: 17 additions & 1 deletion crates/usvg-parser/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ impl<'a, 'input: 'a> FromValue<'a, 'input> for usvg_tree::FontStyle {
}
}

/// A text character position.
///
/// _Character_ is a Unicode codepoint.
#[derive(Clone, Copy, Debug)]
struct CharacterPosition {
/// An absolute X axis position.
x: Option<f32>,
/// An absolute Y axis position.
y: Option<f32>,
/// A relative X axis offset.
dx: Option<f32>,
/// A relative Y axis offset.
dy: Option<f32>,
}

pub(crate) fn convert(
text_node: SvgNode,
state: &converter::State,
Expand All @@ -109,7 +124,8 @@ pub(crate) fn convert(
let text = Text {
id,
rendering_mode,
positions: pos_list,
dx: pos_list.iter().map(|v| v.dx.unwrap_or(0.0)).collect(),
dy: pos_list.iter().map(|v| v.dy.unwrap_or(0.0)).collect(),
rotate: rotate_list,
writing_mode,
chunks,
Expand Down
54 changes: 20 additions & 34 deletions crates/usvg-text-layout/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,9 @@ fn text_to_paths(
apply_word_spacing(chunk, &mut clusters);
apply_length_adjust(chunk, &mut clusters);
let mut curr_pos = resolve_clusters_positions(
text_node,
chunk,
char_offset,
&text_node.positions,
&text_node.rotate,
text_node.writing_mode,
abs_ts,
&fonts_cache,
Expand Down Expand Up @@ -1370,30 +1369,23 @@ fn find_font_for_char(
///
/// Returns the last text position. The next text chunk should start from that position.
fn resolve_clusters_positions(
text: &Text,
chunk: &TextChunk,
char_offset: usize,
pos_list: &[CharacterPosition],
rotate_list: &[f32],
writing_mode: WritingMode,
ts: Transform,
fonts_cache: &FontsCache,
clusters: &mut [OutlinedCluster],
) -> (f32, f32) {
match chunk.text_flow {
TextFlow::Linear => resolve_clusters_positions_horizontal(
chunk,
char_offset,
pos_list,
rotate_list,
writing_mode,
clusters,
),
TextFlow::Linear => {
resolve_clusters_positions_horizontal(text, chunk, char_offset, writing_mode, clusters)
}
TextFlow::Path(ref path) => resolve_clusters_positions_path(
text,
chunk,
char_offset,
path,
pos_list,
rotate_list,
writing_mode,
ts,
fonts_cache,
Expand All @@ -1403,10 +1395,9 @@ fn resolve_clusters_positions(
}

fn resolve_clusters_positions_horizontal(
text: &Text,
chunk: &TextChunk,
offset: usize,
pos_list: &[CharacterPosition],
rotate_list: &[f32],
writing_mode: WritingMode,
clusters: &mut [OutlinedCluster],
) -> (f32, f32) {
Expand All @@ -1415,20 +1406,20 @@ fn resolve_clusters_positions_horizontal(

for cluster in clusters {
let cp = offset + cluster.byte_idx.code_point_at(&chunk.text);
if let Some(pos) = pos_list.get(cp) {
if let (Some(dx), Some(dy)) = (text.dx.get(cp), text.dy.get(cp)) {
if writing_mode == WritingMode::LeftToRight {
x += pos.dx.unwrap_or(0.0);
y += pos.dy.unwrap_or(0.0);
x += dx;
y += dy;
} else {
y -= pos.dx.unwrap_or(0.0);
x += pos.dy.unwrap_or(0.0);
y -= dx;
x += dy;
}
cluster.has_relative_shift = pos.dx.is_some() || pos.dy.is_some();
cluster.has_relative_shift = !dx.approx_zero_ulps(4) || !dy.approx_zero_ulps(4);
}

cluster.transform = cluster.transform.pre_translate(x, y);

if let Some(angle) = rotate_list.get(cp).cloned() {
if let Some(angle) = text.rotate.get(cp).cloned() {
if !angle.approx_zero_ulps(4) {
cluster.transform = cluster.transform.pre_rotate(angle);
cluster.has_relative_shift = true;
Expand All @@ -1442,11 +1433,10 @@ fn resolve_clusters_positions_horizontal(
}

fn resolve_clusters_positions_path(
text: &Text,
chunk: &TextChunk,
char_offset: usize,
path: &TextPath,
pos_list: &[CharacterPosition],
rotate_list: &[f32],
writing_mode: WritingMode,
ts: Transform,
fonts_cache: &FontsCache,
Expand All @@ -1468,10 +1458,10 @@ fn resolve_clusters_positions_path(
chunk_offset + path.start_offset + process_anchor(chunk.anchor, clusters_length(clusters));

let normals = collect_normals(
text,
chunk,
clusters,
&path.path,
pos_list,
char_offset,
start_offset,
ts,
Expand All @@ -1498,9 +1488,7 @@ fn resolve_clusters_positions_path(
cluster.transform = cluster.transform.pre_rotate_at(angle, half_width, 0.0);

let cp = char_offset + cluster.byte_idx.code_point_at(&chunk.text);
if let Some(pos) = pos_list.get(cp) {
dy += pos.dy.unwrap_or(0.0);
}
dy += text.dy.get(cp).cloned().unwrap_or(0.0);

let baseline_shift = chunk_span_at(chunk, cluster.byte_idx)
.map(|span| {
Expand All @@ -1521,7 +1509,7 @@ fn resolve_clusters_positions_path(
.pre_translate(shift.x as f32, shift.y as f32);
}

if let Some(angle) = rotate_list.get(cp).cloned() {
if let Some(angle) = text.rotate.get(cp).cloned() {
if !angle.approx_zero_ulps(4) {
cluster.transform = cluster.transform.pre_rotate(angle);
}
Expand Down Expand Up @@ -1556,10 +1544,10 @@ struct PathNormal {
}

fn collect_normals(
text: &Text,
chunk: &TextChunk,
clusters: &[OutlinedCluster],
path: &tiny_skia_path::Path,
pos_list: &[CharacterPosition],
char_offset: usize,
offset: f32,
ts: Transform,
Expand All @@ -1574,9 +1562,7 @@ fn collect_normals(

// Include relative position.
let cp = char_offset + cluster.byte_idx.code_point_at(&chunk.text);
if let Some(pos) = pos_list.get(cp) {
advance += pos.dx.unwrap_or(0.0);
}
advance += text.dx.get(cp).cloned().unwrap_or(0.0);

let offset = advance + half_width;

Expand Down
26 changes: 8 additions & 18 deletions crates/usvg-tree/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,21 +277,6 @@ pub struct TextChunk {
pub text: String,
}

/// A text character position.
///
/// _Character_ is a Unicode codepoint.
#[derive(Clone, Copy, Debug)]
pub struct CharacterPosition {
/// An absolute X axis position.
pub x: Option<f32>,
/// An absolute Y axis position.
pub y: Option<f32>,
/// A relative X axis offset.
pub dx: Option<f32>,
/// A relative Y axis offset.
pub dy: Option<f32>,
}

/// A writing mode.
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
Expand All @@ -317,10 +302,15 @@ pub struct Text {
/// `text-rendering` in SVG.
pub rendering_mode: TextRendering,

/// A list of character positions.
/// A relative X axis offsets.
///
/// One offset for each Unicode codepoint. Aka `char` in Rust.
pub dx: Vec<f32>,

/// A relative Y axis offsets.
///
/// One position for each Unicode codepoint. Aka `char` in Rust.
pub positions: Vec<CharacterPosition>,
/// One offset for each Unicode codepoint. Aka `char` in Rust.
pub dy: Vec<f32>,

/// A list of rotation angles.
///
Expand Down

0 comments on commit b8938f4

Please sign in to comment.