<commit_before>use std::cell::RefCell;
use std::ptr;
use std::cell::Cell;
extern crate libc;

use drawing_ctx;
use drawing_ctx::*;
use handle::RsvgHandle;
use length::*;
use marker;
use node::*;
use parsers;
use path_builder::*;
use path_parser;
use property_bag;
use property_bag::*;
use state::RsvgState;

fn render_path_builder (builder:  &RsvgPathBuilder,
                        draw_ctx: *const RsvgDrawingCtx,
                        state:    *mut RsvgState,
                        dominate: i32,
                        render_markers: bool) {
    drawing_ctx::state_reinherit_top (draw_ctx, state, dominate);
    drawing_ctx::render_path_builder (draw_ctx, builder);

    if render_markers {
        marker::render_markers_for_path_builder (builder, draw_ctx);
    }
}

fn render_ellipse (cx: f64,
                   cy: f64,
                   rx: f64,
                   ry: f64,
                   node: &RsvgNode,
                   draw_ctx: *const RsvgDrawingCtx,
                   dominate: i32) {
    // Per the spec, rx and ry must be nonnegative

    if rx <= 0.0 || ry <= 0.0 {
        return;
    }

    // 4/3 * (1-cos 45°)/sin 45° = 4/3 * sqrt(2) - 1
    let arc_magic: f64 = 0.5522847498;

    // approximate an ellipse using 4 Bézier curves

    let mut builder = RsvgPathBuilder::new ();

    builder.move_to (cx + rx, cy);

    builder.curve_to (cx + rx, cy - arc_magic * ry,
                      cx + arc_magic * rx, cy - ry,
                      cx, cy - ry);

    builder.curve_to (cx - arc_magic * rx, cy - ry,
                      cx - rx, cy - arc_magic * ry,
                      cx - rx, cy);

    builder.curve_to (cx - rx, cy + arc_magic * ry,
                      cx - arc_magic * rx, cy + ry,
                      cx, cy + ry);

    builder.curve_to (cx + arc_magic * rx, cy + ry,
                      cx + rx, cy + arc_magic * ry,
                      cx + rx, cy);

    builder.close_path ();

    render_path_builder (&builder, draw_ctx, node.get_state (), dominate, false);
}

/***** NodePath *****/

struct NodePath {
    builder: RefCell<RsvgPathBuilder>
}

impl NodePath {
    fn new () -> NodePath {
        NodePath {
            builder: RefCell::new (RsvgPathBuilder::new ())
        }
    }
}

impl NodeTrait for NodePath {
    fn set_atts (&self, _: &RsvgNode, _: *const RsvgHandle, pbag: *const RsvgPropertyBag) {
        if let Some (value) = property_bag::lookup (pbag, "d") {
            let mut builder = self.builder.borrow_mut ();

            if let Err (_) = path_parser::parse_path_into_builder (&value, &mut *builder) {
                // FIXME: we don't propagate errors upstream, but creating a partial
                // path is OK per the spec
            }
        }
    }

    fn draw (&self, node: &RsvgNode, draw_ctx: *const RsvgDrawingCtx, dominate: i32) {
        render_path_builder (&*self.builder.borrow (), draw_ctx, node.get_state (), dominate, true);
    }

    fn get_c_impl (&self) -> *const RsvgCNodeImpl {
        ptr::null ()
    }
}

/***** NodePoly *****/

#[derive(Debug, PartialEq)]
enum PolyKind {
    Open,
    Closed
}

struct NodePoly {
    points: RefCell <Option<Vec<(f64, f64)>>>,
    kind: PolyKind
}

impl NodePoly {
    fn new (kind: PolyKind) -> NodePoly {
        NodePoly {
            points: RefCell::new (None),
            kind:   kind
        }
    }
}

impl NodeTrait for NodePoly {
    fn set_atts (&self, _: &RsvgNode, _: *const RsvgHandle, pbag: *const RsvgPropertyBag) {
        // support for svg < 1.0 which used verts
        if let Some (value) = property_bag::lookup (pbag, "verts").or (property_bag::lookup (pbag, "points")) {
            let result = parsers::list_of_points (value.as_bytes ()).to_full_result ();

            match result {
                Ok (v) => {
                    *self.points.borrow_mut () = Some (v);
                },

                Err (_) => {
                    // FIXME: propagate errors upstream
                    *self.points.borrow_mut () = None;
                }
            }
        }
    }

    fn draw (&self, node: &RsvgNode, draw_ctx: *const RsvgDrawingCtx, dominate: i32) {
        if let Some (ref points) = *self.points.borrow () {
            let mut builder = RsvgPathBuilder::new ();

            for (i, &(x, y)) in points.iter ().enumerate () {<commit_msg>shapes.rs: Trim whitespace from the list-of-points string before parsing it<commit_after>use std::cell::RefCell;
use std::ptr;
use std::cell::Cell;
extern crate libc;

use drawing_ctx;
use drawing_ctx::*;
use handle::RsvgHandle;
use length::*;
use marker;
use node::*;
use parsers;
use path_builder::*;
use path_parser;
use property_bag;
use property_bag::*;
use state::RsvgState;

fn render_path_builder (builder:  &RsvgPathBuilder,
                        draw_ctx: *const RsvgDrawingCtx,
                        state:    *mut RsvgState,
                        dominate: i32,
                        render_markers: bool) {
    drawing_ctx::state_reinherit_top (draw_ctx, state, dominate);
    drawing_ctx::render_path_builder (draw_ctx, builder);

    if render_markers {
        marker::render_markers_for_path_builder (builder, draw_ctx);
    }
}

fn render_ellipse (cx: f64,
                   cy: f64,
                   rx: f64,
                   ry: f64,
                   node: &RsvgNode,
                   draw_ctx: *const RsvgDrawingCtx,
                   dominate: i32) {
    // Per the spec, rx and ry must be nonnegative

    if rx <= 0.0 || ry <= 0.0 {
        return;
    }

    // 4/3 * (1-cos 45°)/sin 45° = 4/3 * sqrt(2) - 1
    let arc_magic: f64 = 0.5522847498;

    // approximate an ellipse using 4 Bézier curves

    let mut builder = RsvgPathBuilder::new ();

    builder.move_to (cx + rx, cy);

    builder.curve_to (cx + rx, cy - arc_magic * ry,
                      cx + arc_magic * rx, cy - ry,
                      cx, cy - ry);

    builder.curve_to (cx - arc_magic * rx, cy - ry,
                      cx - rx, cy - arc_magic * ry,
                      cx - rx, cy);

    builder.curve_to (cx - rx, cy + arc_magic * ry,
                      cx - arc_magic * rx, cy + ry,
                      cx, cy + ry);

    builder.curve_to (cx + arc_magic * rx, cy + ry,
                      cx + rx, cy + arc_magic * ry,
                      cx + rx, cy);

    builder.close_path ();

    render_path_builder (&builder, draw_ctx, node.get_state (), dominate, false);
}

/***** NodePath *****/

struct NodePath {
    builder: RefCell<RsvgPathBuilder>
}

impl NodePath {
    fn new () -> NodePath {
        NodePath {
            builder: RefCell::new (RsvgPathBuilder::new ())
        }
    }
}

impl NodeTrait for NodePath {
    fn set_atts (&self, _: &RsvgNode, _: *const RsvgHandle, pbag: *const RsvgPropertyBag) {
        if let Some (value) = property_bag::lookup (pbag, "d") {
            let mut builder = self.builder.borrow_mut ();

            if let Err (_) = path_parser::parse_path_into_builder (&value, &mut *builder) {
                // FIXME: we don't propagate errors upstream, but creating a partial
                // path is OK per the spec
            }
        }
    }

    fn draw (&self, node: &RsvgNode, draw_ctx: *const RsvgDrawingCtx, dominate: i32) {
        render_path_builder (&*self.builder.borrow (), draw_ctx, node.get_state (), dominate, true);
    }

    fn get_c_impl (&self) -> *const RsvgCNodeImpl {
        ptr::null ()
    }
}

/***** NodePoly *****/

#[derive(Debug, PartialEq)]
enum PolyKind {
    Open,
    Closed
}

struct NodePoly {
    points: RefCell <Option<Vec<(f64, f64)>>>,
    kind: PolyKind
}

impl NodePoly {
    fn new (kind: PolyKind) -> NodePoly {
        NodePoly {
            points: RefCell::new (None),
            kind:   kind
        }
    }
}

impl NodeTrait for NodePoly {
    fn set_atts (&self, _: &RsvgNode, _: *const RsvgHandle, pbag: *const RsvgPropertyBag) {
        // support for svg < 1.0 which used verts
        if let Some (value) = property_bag::lookup (pbag, "verts").or (property_bag::lookup (pbag, "points")) {
            let result = parsers::list_of_points (value.trim ().as_bytes ()).to_full_result ();

            match result {
                Ok (v) => {
                    *self.points.borrow_mut () = Some (v);
                },

                Err (_) => {
                    // FIXME: propagate errors upstream
                    *self.points.borrow_mut () = None;
                }
            }
        }
    }

    fn draw (&self, node: &RsvgNode, draw_ctx: *const RsvgDrawingCtx, dominate: i32) {
        if let Some (ref points) = *self.points.borrow () {
            let mut builder = RsvgPathBuilder::new ();

            for (i, &(x, y)) in points.iter ().enumerate () {
{'fraction_non_alphanumeric': 0.12482868889904065, 'fraction_numerical': 0.011649154865235267, 'mean_word_length': 1.8422590068159688, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6568278', 'n_tokens_mistral': 2932, 'n_tokens_neox': 2723, 'n_words': 744}