Skip to content

Commit

Permalink
chore: improve shuffling in Text (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
shamilsan authored May 5, 2024
1 parent be1c0cc commit 1b767fe
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
8 changes: 6 additions & 2 deletions examples/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn TextExample() -> impl IntoView {
view! {
<Slide node_ref=node_ref>
<Row cols=2 padding=30 border_width=4>
<Column stretch=[5, 1]>
<Column stretch=[5, 1, 5]>
<Text
lattice=true
word_count=word_count1
Expand All @@ -45,6 +45,10 @@ pub fn TextExample() -> impl IntoView {
{"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."}
</Text>
<Label>{words_read1} "w (" {letters_read1} " / " {letters_total1} ")"</Label>
<Text valign=VAlign::Bottom reverse_words=true>
{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."}
{"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."}
</Text>
</Column>

<Column stretch=[5, 1, 5, 1]>
Expand All @@ -53,7 +57,7 @@ pub fn TextExample() -> impl IntoView {
bold=true
font="serif"
valign=VAlign::Middle
reverse_words=true
shuffle_letters=true
words_read=words_read2
letters_read=letters_read2
letters_total=letters_total2
Expand Down
38 changes: 29 additions & 9 deletions src/widgets/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate alloc;

use alloc::rc::Rc;
use leptos::*;
use rand::{prelude::SliceRandom, rngs::OsRng};
use rand::{prelude::SliceRandom, rngs::OsRng, Rng};
use wasm_bindgen::JsValue;
use web_sys::{CanvasRenderingContext2d, HtmlCanvasElement, MouseEvent};

Expand Down Expand Up @@ -208,7 +208,9 @@ fn reverse_word(word: &str) -> String {
let mut result = String::new();
let mut last = 0;
// Reverse only alphabetic chars
for (i, matched) in word.match_indices(|c: char| !(c.is_alphanumeric())) {
for (i, matched) in
word.match_indices(&[',', '.', '!', '?', ':', ';', '…', '(', ')', '«', '»', '"'])
{
if last != i {
result.push_str(&word[last..i].chars().rev().collect::<String>());
}
Expand All @@ -222,14 +224,32 @@ fn reverse_word(word: &str) -> String {
}

fn shuffle_word(word: &str) -> String {
let mut rng = OsRng;
let mut chars: Vec<char> = word.chars().collect();
// Shuffle only internal chars except first and last
let len = chars.len();
if len > 2 {
chars[1..len - 1].shuffle(&mut rng);
let mut result = String::new();
let mut last = 0;

let shuffle = |word: &str| -> String {
let mut rng = OsRng;
let mut word: Vec<_> = word.chars().collect();
let len = word.len();
if len > 3 {
let start = rng.gen_range(1..len / 2);
let end = rng.gen_range((len + 1) / 2..len);
word[start..end].shuffle(&mut rng);
}
word.into_iter().collect()
};
// Shuffle only alphabetic chars except the first and last
for (i, matched) in word.match_indices(|c: char| !(c.is_alphanumeric())) {
if last != i {
result.push_str(shuffle(&word[last..i]).as_str());
}
result.push_str(matched);
last = i + matched.len();
}
chars.into_iter().collect()
if last < word.len() {
result.push_str(shuffle(&word[last..]).as_str());
}
result
}

fn canvas_context(props: &TextProperties) -> CanvasRenderingContext2d {
Expand Down

0 comments on commit 1b767fe

Please sign in to comment.