Skip to content

Commit

Permalink
Merge pull request #7 from keiya01/feat/impl-flex-direction-row
Browse files Browse the repository at this point in the history
Feat/support `flex-direction: row;`
  • Loading branch information
keiya01 authored Oct 24, 2021
2 parents 5d1851c + 2313a77 commit cb41ea7
Show file tree
Hide file tree
Showing 16 changed files with 338 additions and 103 deletions.
4 changes: 4 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ path = "into_vec.rs"
[[example]]
name = "textarea"
path = "textarea.rs"

[[example]]
name = "row_container"
path = "row_container.rs"
Binary file modified examples/assets/output_absolute.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/assets/output_background_color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/assets/output_background_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/assets/output_container.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/assets/output_ellipsis.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/assets/output_into_vec.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/assets/output_row_container.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/assets/output_textarea.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
78 changes: 78 additions & 0 deletions examples/row_container.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use og_image_writer::{style, writer::OGImageWriter};
use std::path::Path;

fn main() -> anyhow::Result<()> {
let text = "This is Open Graphic Image Writer for Web Developer.";

let mut container = OGImageWriter::new(style::WindowStyle {
width: 500,
height: 250,
background_color: Some(style::Rgba([70, 40, 90, 255])),
align_items: style::AlignItems::Center,
justify_content: style::JustifyContent::Center,
..style::WindowStyle::default()
})?;

let font = Vec::from(include_bytes!("../fonts/Mplus1-Black.ttf") as &[u8]);

container.set_text(
text,
style::Style {
margin: style::Margin(0, 10, 0, 10),
line_height: 1.5,
font_size: 80.,
word_break: style::WordBreak::Normal,
color: style::Rgba([255, 255, 255, 255]),
text_align: style::TextAlign::Start,
text_overflow: style::TextOverflow::Ellipsis,
max_height: Some(200),
..style::Style::default()
},
font,
)?;

let mut writer = OGImageWriter::new(style::WindowStyle {
width: 1024,
height: 512,
background_color: Some(style::Rgba([255, 255, 255, 255])),
align_items: style::AlignItems::Center,
justify_content: style::JustifyContent::Center,
flex_direction: style::FlexDirection::Row,
..style::WindowStyle::default()
})?;

writer.set_container(
&mut container,
style::Style {
margin: style::Margin(0, 10, 0, 10),
text_align: style::TextAlign::Center,
border_radius: style::BorderRadius(10, 10, 10, 10),
..style::Style::default()
},
)?;

let font = Vec::from(include_bytes!("../fonts/Mplus1-Black.ttf") as &[u8]);

writer.set_text(
"This is Open Graphic Image Writer for Web Developer.",
style::Style {
margin: style::Margin(0, 20, 0, 20),
line_height: 1.8,
font_size: 100.,
word_break: style::WordBreak::Normal,
color: style::Rgba([0, 0, 0, 255]),
text_align: style::TextAlign::Center,
max_width: Some(500),
max_height: Some(400),
..style::Style::default()
},
font,
)?;

let out_dir = "./examples/assets";
let out_filename = "output_row_container.png";

writer.generate(Path::new(&format!("{}/{}", out_dir, out_filename)))?;

Ok(())
}
42 changes: 23 additions & 19 deletions og_image_writer/src/element.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::layout::TextArea;
use super::style::{Position, Style};
use super::style::{Margin, Position, Style};
use image::{ImageBuffer, Rgba};
use rusttype::Font;
use std::ops::Range;
Expand All @@ -18,44 +18,45 @@ impl<'a> Element<'a> {
_ => false,
}
}

pub(super) fn margin(&self) -> Margin {
match self {
Element::Img(Some(img)) => img.style.margin.clone(),
Element::Text(Some(text)) => text.style.margin.clone(),
_ => Margin::default(),
}
}
}

#[derive(Debug, Default)]
pub struct Rect {
pub(super) x: u32,
pub(super) y: u32,
pub(super) width: u32,
pub(super) height: u32,
}

impl Rect {
pub fn new(x: u32, y: u32) -> Self {
Rect { x, y }
pub fn new(x: u32, y: u32, width: u32, height: u32) -> Self {
Rect {
x,
y,
width,
height,
}
}
}

#[derive(Debug)]
pub struct Img<'a> {
pub(super) buf: ImageBuffer<Rgba<u8>, Vec<u8>>,
pub(super) width: u32,
pub(super) height: u32,
pub(super) rect: Rect,
pub(super) style: Style<'a>,
}

impl<'a> Img<'a> {
pub fn new(
buf: ImageBuffer<Rgba<u8>, Vec<u8>>,
width: u32,
height: u32,
rect: Rect,
style: Style<'a>,
) -> Self {
Img {
buf,
width,
height,
rect,
style,
}
pub fn new(buf: ImageBuffer<Rgba<u8>, Vec<u8>>, rect: Rect, style: Style<'a>) -> Self {
Img { buf, rect, style }
}
}

Expand All @@ -79,6 +80,7 @@ pub struct Text<'a> {
pub(super) style: Style<'a>,
pub(super) font: Font<'a>,
pub(super) max_line_height: f32,
pub(super) max_line_width: f32,
pub(super) textarea: TextArea<'a>,
}

Expand All @@ -90,6 +92,7 @@ impl<'a> Text<'a> {
style: Style<'a>,
font: Font<'a>,
max_line_height: f32,
max_line_width: f32,
textarea: TextArea<'a>,
) -> Self {
Text {
Expand All @@ -99,6 +102,7 @@ impl<'a> Text<'a> {
style,
font,
max_line_height,
max_line_width,
textarea,
}
}
Expand Down
Loading

0 comments on commit cb41ea7

Please sign in to comment.