-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from archer884/feature/multi-size
Add in-band size option to annotations
- Loading branch information
Showing
8 changed files
with
488 additions
and
366 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "annatar" | ||
version = "0.3.4" | ||
version = "0.4.0" | ||
authors = ["J/A <[email protected]>"] | ||
license = "MIT/Apache-2.0" | ||
keywords = ["image", "image-manipulation", "meme", "pepe", "kek"] | ||
|
@@ -10,9 +10,12 @@ homepage = "https://github.com/archer884/annatar" | |
categories = ["multimedia::images"] | ||
description = """ | ||
A command line tool for making memes. | ||
Full readme available at: https://github.com/archer884/annatar | ||
""" | ||
|
||
[dependencies] | ||
artano = "0.2.6" | ||
clap = "2.31" | ||
clap = "2.32" | ||
regex = "1.0" | ||
reqwest = "0.8" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod options; | ||
mod scaled_annotation; | ||
|
||
pub mod resource; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use artano::Annotation; | ||
use regex::Regex; | ||
|
||
#[derive(Debug)] | ||
pub struct ScaledAnnotation { | ||
pub scale_multiplier: f32, | ||
pub annotation: Annotation, | ||
} | ||
|
||
impl ScaledAnnotation { | ||
fn new(scale_multiplier: f32, annotation: Annotation) -> Self { | ||
Self { | ||
scale_multiplier, | ||
annotation, | ||
} | ||
} | ||
} | ||
|
||
pub struct ScaledAnnotationParser { | ||
pattern: Regex, | ||
} | ||
|
||
impl ScaledAnnotationParser { | ||
pub fn new() -> Self { | ||
Self { | ||
pattern: Regex::new(r#"\\(?P<scale>\d+(\.\d+)?)\s+(?P<caption>.+)"#).unwrap(), | ||
} | ||
} | ||
|
||
pub fn bottom(&self, scale: f32, s: &str) -> ScaledAnnotation { | ||
let (annotation_scale, annotation) = parse_scaled_annotation(&self.pattern, s); | ||
ScaledAnnotation::new( | ||
annotation_scale.unwrap_or(scale), | ||
Annotation::bottom(annotation), | ||
) | ||
} | ||
|
||
pub fn middle(&self, scale: f32, s: &str) -> ScaledAnnotation { | ||
let (annotation_scale, annotation) = parse_scaled_annotation(&self.pattern, s); | ||
ScaledAnnotation::new( | ||
annotation_scale.unwrap_or(scale), | ||
Annotation::middle(annotation), | ||
) | ||
} | ||
|
||
pub fn top(&self, scale: f32, s: &str) -> ScaledAnnotation { | ||
let (annotation_scale, annotation) = parse_scaled_annotation(&self.pattern, s); | ||
ScaledAnnotation::new( | ||
annotation_scale.unwrap_or(scale), | ||
Annotation::top(annotation), | ||
) | ||
} | ||
} | ||
|
||
fn parse_scaled_annotation(pattern: &Regex, s: &str) -> (Option<f32>, String) { | ||
// The plan here is to provide in-band scaling per-annotation via the following format: we | ||
// check the beginning of any given annotation for \<float>. If we find that, we treat it | ||
// as entirely separate from the annotation, up to and including all trailing white space. | ||
// | ||
// For instance, an annotation of the form `\1.2 frenchmen can't spell` would equate to | ||
// the message "frenchmen can't spell" at a size multiplier of 1.2. | ||
// | ||
// It's valid to escape the leading \ with \\. | ||
|
||
// Annotation does not contain in-band scaling information. | ||
if !(s.starts_with('\\') && !s.starts_with("\\\\")) { | ||
return (None, s.into()); | ||
} | ||
|
||
match pattern.captures(s) { | ||
None => (None, s.into()), | ||
|
||
// These unwrap assumptions may look horrendously unsafe, but the design of the regular | ||
// expression itself makes failure here very unlikely. | ||
Some(captures) => ( | ||
Some( | ||
captures | ||
.name("scale") | ||
.and_then(|s| s.as_str().parse().ok()) | ||
.unwrap(), | ||
), | ||
captures.name("caption").map(|s| s.as_str().into()).unwrap(), | ||
), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{ScaledAnnotation, ScaledAnnotationParser}; | ||
|
||
#[test] | ||
fn scaled_annotation_parser_works() { | ||
let parser = ScaledAnnotationParser::new(); | ||
let caption = "\\2.0 Hello, world!"; | ||
let ScaledAnnotation { | ||
scale_multiplier, | ||
annotation, | ||
} = parser.top(1.0, caption); | ||
|
||
assert_eq!(2.0, scale_multiplier); | ||
assert_eq!("Hello, world!", annotation.text); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
extern crate clap; | ||
|
||
extern crate artano; | ||
extern crate regex; | ||
extern crate reqwest; | ||
|
||
mod application; | ||
|