css.rs 7.62 KB
Newer Older
1 2
use std::collections::hash_map::Entry;
use std::collections::HashMap;
3 4 5
use std::ptr;
use std::slice;
use std::str::{self, FromStr};
6 7 8 9

use libc;

use glib::translate::*;
10
use glib_sys::{self, gboolean, gpointer, GList};
11 12

use attributes::Attribute;
13
use croco::*;
14
use handle::{self, RsvgHandle};
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
use state::State;
use util::utf8_cstr;

pub enum RsvgCssStyles {}

struct Declaration {
    prop_value: String,
    important: bool,
}

// Maps property_name -> Declaration
type DeclarationList = HashMap<String, Declaration>;

pub struct CssStyles {
    selectors_to_declarations: HashMap<String, DeclarationList>,
}

impl CssStyles {
    fn new() -> CssStyles {
        CssStyles {
            selectors_to_declarations: HashMap::new(),
        }
    }

    fn define(&mut self, selector: &str, prop_name: &str, prop_value: &str, important: bool) {
        let decl_list = self
            .selectors_to_declarations
            .entry(selector.to_string())
            .or_insert_with(|| DeclarationList::new());

        match decl_list.entry(prop_name.to_string()) {
            Entry::Occupied(mut e) => {
                let decl = e.get_mut();

                if !decl.important {
                    decl.prop_value = prop_value.to_string();
                    decl.important = important;
                }
            }

            Entry::Vacant(v) => {
                v.insert(Declaration {
                    prop_value: prop_value.to_string(),
                    important,
                });
            }
        }
    }

    pub fn lookup_apply(&self, selector: &str, state: &mut State) -> bool {
        if let Some(decl_list) = self.selectors_to_declarations.get(selector) {
            for (prop_name, declaration) in decl_list.iter() {
                if let Ok(attr) = Attribute::from_str(prop_name) {
                    // FIXME: this is ignoring errors
                    let _ = state.parse_style_pair(
                        attr,
                        &declaration.prop_value,
                        declaration.important,
                    );
                }
            }

            true
        } else {
            false
        }
    }
}

84 85 86 87 88
struct DocHandlerData {
    handle: *mut RsvgHandle,
    selector: *mut CRSelector,
}

89
pub fn parse_into_handle(handle: *mut RsvgHandle, buf: &str) {
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    unsafe {
        let handler_data = DocHandlerData {
            handle,
            selector: ptr::null_mut(),
        };

        let doc_handler = cr_doc_handler_new();
        init_cr_doc_handler(&mut *doc_handler);

        (*doc_handler).app_data = &handler_data as *const _ as gpointer;

        let buf_ptr = buf.as_ptr() as *mut _;
        let buf_len = buf.len() as u64;

        let parser = cr_parser_new_from_buf(buf_ptr, buf_len, CR_UTF_8, false.to_glib());
        if parser.is_null() {
            cr_doc_handler_unref(doc_handler);
            return;
        }

        cr_parser_set_sac_handler(parser, doc_handler);
        cr_doc_handler_unref(doc_handler);

        cr_parser_set_use_core_grammar(parser, false.to_glib());
        cr_parser_parse(parser);

        cr_parser_destroy(parser);
    }
}

120 121 122 123 124 125 126 127 128 129
fn init_cr_doc_handler(handler: &mut CRDocHandler) {
    handler.import_style = Some(css_import_style);
    handler.start_selector = Some(css_start_selector);
    handler.end_selector = Some(css_end_selector);
    handler.property = Some(css_property);
    handler.error = Some(css_error);
    handler.unrecoverable_error = Some(css_unrecoverable_error);
}

unsafe extern "C" fn css_import_style(
130
    a_this: *mut CRDocHandler,
131
    _a_media_list: *mut GList,
132
    a_uri: CRString,
133 134
    _a_uri_default_ns: CRString,
    _a_location: CRParsingLocation,
135
) {
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    let handler_data = get_doc_handler_data(a_this);

    if a_uri.is_null() {
        return;
    }

    let raw_uri = cr_string_peek_raw_str(a_uri);
    let uri = utf8_cstr(raw_uri);

    if let Ok(binary_data) = handle::acquire_data(handler_data.handle, uri) {
        if binary_data.content_type.as_ref().map(String::as_ref) == Some("text/css") {
            parse_into_handle(
                handler_data.handle,
                str::from_utf8_unchecked(&binary_data.data),
            );
        }
    }
153 154
}

155 156 157 158
unsafe fn get_doc_handler_data<'a>(doc_handler: *mut CRDocHandler) -> &'a mut DocHandlerData {
    &mut *((*doc_handler).app_data as *mut DocHandlerData)
}

159 160 161 162
unsafe extern "C" fn css_start_selector(
    a_this: *mut CRDocHandler,
    a_selector_list: *mut CRSelector,
) {
163 164 165 166
    let handler_data = get_doc_handler_data(a_this);

    cr_selector_ref(a_selector_list);
    handler_data.selector = a_selector_list;
167 168
}

169 170 171 172 173 174 175 176
unsafe extern "C" fn css_end_selector(
    a_this: *mut CRDocHandler,
    _a_selector_list: *mut CRSelector,
) {
    let handler_data = get_doc_handler_data(a_this);

    cr_selector_unref(handler_data.selector);
    handler_data.selector = ptr::null_mut();
177 178 179 180 181 182 183 184
}

unsafe extern "C" fn css_property(
    a_this: *mut CRDocHandler,
    a_name: CRString,
    a_expression: CRTerm,
    a_is_important: gboolean,
) {
185 186 187 188 189 190 191 192 193 194 195 196 197 198
    let handler_data = get_doc_handler_data(a_this);

    if a_name.is_null() || a_expression.is_null() || handler_data.selector.is_null() {
        return;
    }

    let mut cur_sel = handler_data.selector;
    while !cur_sel.is_null() {
        let simple_sel = (*cur_sel).simple_sel;

        if !simple_sel.is_null() {
            let raw_selector_name = cr_simple_sel_to_string(simple_sel) as *mut libc::c_char;

            if !raw_selector_name.is_null() {
199 200
                let raw_prop_name = cr_string_peek_raw_str(a_name);
                let prop_name = utf8_cstr(raw_prop_name);
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

                let prop_value =
                    <String as FromGlibPtrFull<_>>::from_glib_full(cr_term_to_string(a_expression));

                let selector_name =
                    <String as FromGlibPtrFull<_>>::from_glib_full(raw_selector_name);

                let important = from_glib(a_is_important);

                let styles = handle::get_css_styles_mut(handler_data.handle);

                styles.define(&selector_name, prop_name, &prop_value, important);
            }
        }

        cur_sel = (*cur_sel).next;
    }
218 219
}

220 221
unsafe extern "C" fn css_error(_a_this: *mut CRDocHandler) {
    println!("CSS parsing error");
222 223
}

224 225
unsafe extern "C" fn css_unrecoverable_error(_a_this: *mut CRDocHandler) {
    println!("CSS unrecoverable error");
226 227
}

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
#[no_mangle]
pub extern "C" fn rsvg_css_styles_new() -> *mut RsvgCssStyles {
    Box::into_raw(Box::new(CssStyles::new())) as *mut RsvgCssStyles
}

#[no_mangle]
pub unsafe extern "C" fn rsvg_css_styles_free(raw_styles: *mut RsvgCssStyles) {
    assert!(!raw_styles.is_null());

    Box::from_raw(raw_styles as *mut CssStyles);
}

#[no_mangle]
pub extern "C" fn rsvg_css_styles_define(
    raw_styles: *mut RsvgCssStyles,
    selector: *const libc::c_char,
    prop_name: *const libc::c_char,
    prop_value: *const libc::c_char,
    important: glib_sys::gboolean,
) {
    assert!(!raw_styles.is_null());
    assert!(!selector.is_null());
    assert!(!prop_name.is_null());
    assert!(!prop_value.is_null());

    let styles = unsafe { &mut *(raw_styles as *mut CssStyles) };
    let selector = unsafe { utf8_cstr(selector) };
    let prop_name = unsafe { utf8_cstr(prop_name) };
    let prop_value = unsafe { utf8_cstr(prop_value) };

    styles.define(selector, prop_name, prop_value, from_glib(important));
}
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

#[no_mangle]
pub unsafe extern "C" fn rsvg_css_parse_into_handle(
    handle: *mut RsvgHandle,
    buf: *const libc::c_char,
    len: usize,
) {
    assert!(!handle.is_null());

    if buf.is_null() || len == 0 {
        return;
    }

    let bytes = slice::from_raw_parts(buf as *const u8, len);
    let utf8 = str::from_utf8_unchecked(bytes);

    parse_into_handle(handle, utf8);
}