~vpzom/savg

5a28ee163e00f5ca984c0a1938d070005caa4262 — Colin Reeder 5 years ago 561051b
Initial work on presentation attributes
4 files changed, 77 insertions(+), 7 deletions(-)

A src/color.rs
M src/document.rs
M src/main.rs
M src/save.rs
A src/color.rs => src/color.rs +20 -0
@@ 0,0 1,20 @@
#[derive(Clone)]
pub enum Color {
    RGBA(RGBA),
    RGB(RGB),
}

#[derive(Clone)]
pub struct RGBA {
    pub r: u8,
    pub g: u8,
    pub b: u8,
    pub a: f32,
}

#[derive(Clone)]
pub struct RGB {
    pub r: u8,
    pub g: u8,
    pub b: u8,
}

M src/document.rs => src/document.rs +21 -2
@@ 1,5 1,6 @@
use cairo;

use color;
use types::*;

pub struct Document {


@@ 32,9 33,16 @@ impl Element {
    pub fn draw(&self, ctx: &cairo::Context) {
        match self {
            Element::Rect(rect) => {
                ctx.set_source_rgb(1.0, 0.0, 0.0); // TODO not this
                ctx.rectangle(rect.position.x, rect.position.y, rect.size.width, rect.size.height);
                ctx.fill();
                if let Some(ref fill) = rect.presentation.fill {
                    match fill {
                        Paint::Color(ref color) => match color {
                            color::Color::RGB(rgb) => ctx.set_source_rgb(rgb.r as f64 / 255.0, rgb.g as f64 / 255.0, rgb.b as f64 / 255.0),
                            color::Color::RGBA(rgba) => ctx.set_source_rgba(rgba.r as f64 / 255.0, rgba.g as f64 / 255.0, rgba.b as f64 / 255.0, rgba.a as f64),
                        }
                    }
                    ctx.fill();
                }
            }
        }
    }


@@ 44,4 52,15 @@ impl Element {
pub struct Rect {
    pub position: Point2,
    pub size: Size2,
    pub presentation: Presentation,
}

#[derive(Clone)]
pub struct Presentation {
    pub fill: Option<Paint>,
}

#[derive(Clone)]
pub enum Paint {
    Color(color::Color),
}

M src/main.rs => src/main.rs +8 -0
@@ 4,6 4,7 @@ extern crate gdk;
extern crate gtk;
extern crate quick_xml;

mod color;
mod document;
mod save;
mod types;


@@ 85,6 86,13 @@ fn main() {
                        x: pos_x,
                        y: pos_y,
                    },
                    presentation: Presentation {
                        fill: Some(Paint::Color(color::Color::RGB(color::RGB {
                            r: 0,
                            g: 0,
                            b: 255,
                        }))),
                    },
                    size: Size2 {
                        width: 100.0,
                        height: 50.0,

M src/save.rs => src/save.rs +28 -5
@@ 1,8 1,11 @@
use quick_xml;
use std;

use color;
use document;

use std::borrow::Cow;

pub enum Error {
    XML(quick_xml::Error),
    IO(std::io::Error),


@@ 47,12 50,19 @@ fn write_element_svg(elem: &document::Element, writer: &mut quick_xml::Writer<im
            let y_str = rect.position.y.to_string();
            let width_str = rect.size.width.to_string();
            let height_str = rect.size.height.to_string();
            let attrs = vec![
                ("x", &x_str[..]),
                ("y", &y_str[..]),
                ("width", &width_str[..]),
                ("height", &height_str[..]),
            let mut attrs = vec![
                ("x", &x_str[..]).into(),
                ("y", &y_str[..]).into(),
                ("width", &width_str[..]).into(),
                ("height", &height_str[..]).into(),
            ];
            if let Some(ref fill) = rect.presentation.fill {
                let fill_str = paint_to_str(&fill);
                attrs.push(quick_xml::events::attributes::Attribute {
                    key: b"fill",
                    value: Cow::Owned(fill_str.as_bytes().to_owned()),
                });
            }
            writer.write_event(quick_xml::events::Event::Start(quick_xml::events::BytesStart::borrowed_name(b"rect")
                                                               .with_attributes(attrs.into_iter())))?;
            writer.write_event(quick_xml::events::Event::End(quick_xml::events::BytesEnd::borrowed(b"rect")))?;


@@ 60,3 70,16 @@ fn write_element_svg(elem: &document::Element, writer: &mut quick_xml::Writer<im
        }
    }
}

fn paint_to_str(paint: &document::Paint) -> String {
    match paint {
        document::Paint::Color(ref color) => color_to_str(color),
    }
}

fn color_to_str(color: &color::Color) -> String {
    match color {
        color::Color::RGB(rgb) => format!("#{:02x}{:02x}{:02x}", rgb.r, rgb.g, rgb.b),
        color::Color::RGBA(rgba) => format!("rgba({},{},{},{})", rgba.r, rgba.g, rgba.b, rgba.a),
    }
}