Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/build_captchas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::path::Path;

fn main() {
let mut c = Captcha::new();
c.add_chars(5)
c.add_random_chars(5)
.apply_filter(Noise::new(0.2))
.apply_filter(Wave::new(2.0, 20.0))
.view(220, 120)
Expand Down
2 changes: 1 addition & 1 deletion examples/various.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {
println!("{:?}", c.supported_chars());

c.set_chars(&['a', 'b'])
.add_chars(5)
.add_random_chars(5)
.apply_filter(Noise::new(0.2))
.apply_filter(Wave::new(2.0, 20.0))
.view(220, 120)
Expand Down
64 changes: 43 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
//!
//! # fn main() {
//! Captcha::new()
//! .add_chars(5)
//! .add_random_chars(5)
//! .apply_filter(Noise::new(0.4))
//! .apply_filter(Wave::new(2.0, 20.0).horizontal())
//! .apply_filter(Wave::new(2.0, 20.0).vertical())
Expand Down Expand Up @@ -189,28 +189,45 @@ impl<T: rand::Rng + rand::RngCore> RngCaptcha<T> {
fn random_char_as_image(&mut self) -> Option<(char, Image)> {
match self.use_font_chars.choose(&mut self.rng) {
None => None,
Some(c) => match self.font.png(*c) {
None => None,
Some(p) => Image::from_png(p).map(|i| (*c, i)),
},
Some(c) => self.char_as_image(c),
}
}

fn char_as_image(&self, c: &char) -> Option<(char, Image)> {
match self.font.png(*c) {
None => None,
Some(p) => Image::from_png(p).map(|i| (*c, i)),
}
}

fn add_char_to_captcha(&mut self, c: char, i: Image) {
let x = self.text_area.right;
let y = (self.text_area.bottom + self.text_area.top) / 2 - i.height() / 2;
self.img.add_image(x, y, &i);

self.text_area.top = min(self.text_area.top, y);
self.text_area.right = x + i.width() - 1;
self.text_area.bottom = max(self.text_area.bottom, y + i.height() - 1);
self.chars.push(c);
// TODO automatically resize if many characters are added
}

/// Adds a character using the current font.
pub fn add_char(&mut self, c: char) -> &mut Self {
if let Some((c, i)) = self.char_as_image(&c) {
self.add_char_to_captcha(c, i);
}

self
}

/// Adds a random character using the current font.
pub fn add_char(&mut self) -> &mut Self {
pub fn add_random_char(&mut self) -> &mut Self {
if let Some((c, i)) = self.random_char_as_image() {
let x = self.text_area.right;
let y = (self.text_area.bottom + self.text_area.top) / 2 - i.height() / 2;
self.img.add_image(x, y, &i);

self.text_area.top = min(self.text_area.top, y);
self.text_area.right = x + i.width() - 1;
self.text_area.bottom = max(self.text_area.bottom, y + i.height() - 1);
self.chars.push(c);
self.add_char_to_captcha(c, i);
}

self
// TODO automatically resize if many characters are added
}

/// Adds a red box to the CAPTCHA representing the area which contains text.
Expand Down Expand Up @@ -272,9 +289,9 @@ impl<T: rand::Rng + rand::RngCore> RngCaptcha<T> {
}

/// Adds the given number of random characters to the CAPTCHA using the current font.
pub fn add_chars(&mut self, n: u32) -> &mut Self {
pub fn add_random_chars(&mut self, n: u32) -> &mut Self {
for _ in 0..n {
self.add_char();
self.add_random_char();
}
self
}
Expand Down Expand Up @@ -333,17 +350,17 @@ impl<T: rand::Rng + rand::RngCore> RngCaptcha<T> {
mod tests {
use filters::{Grid, Noise};
use fonts::Default;
use Captcha;
use ::{Captcha, Difficulty};

use std::path::Path;

#[test]
fn it_works() {
let mut c = Captcha::new();
c.set_font(Default::new())
.add_char()
.add_char()
.add_char()
.add_random_char()
.add_random_char()
.add_random_char()
.apply_filter(Noise::new(0.1))
.apply_filter(Grid::new(20, 10))
.add_text_area();
Expand All @@ -355,6 +372,11 @@ mod tests {
c.as_png().expect("no png");
}

#[test]
fn gen_difficult_easy() {
assert!(::gen(Difficulty::Easy).as_png().is_some())
}

#[test]
fn image_size() {
let mut c = Captcha::new();
Expand Down
6 changes: 3 additions & 3 deletions src/samples/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn rnd() -> u32 {

fn captcha_amelia(d: Difficulty) -> Captcha {
let mut c = Captcha::new();
c.add_chars(rnd());
c.add_random_chars(rnd());
match d {
Difficulty::Easy => c
.apply_filter(Noise::new(0.2))
Expand Down Expand Up @@ -155,7 +155,7 @@ fn captcha_lucy(d: Difficulty) -> Captcha {
};

let mut c = Captcha::new();
c.add_chars(rnd())
c.add_random_chars(rnd())
.apply_filter(Noise::new(n))
.apply_filter(Grid::new(g, g))
.view(WIDTH, HEIGHT);
Expand All @@ -164,7 +164,7 @@ fn captcha_lucy(d: Difficulty) -> Captcha {

fn captcha_mila(d: Difficulty) -> Captcha {
let mut c = Captcha::new();
c.add_chars(rnd());
c.add_random_chars(rnd());
match d {
Difficulty::Easy => c.apply_filter(Noise::new(0.2)),
Difficulty::Medium => c.apply_filter(Noise::new(0.3)),
Expand Down