<commit_before>// High-level bindings to Cairo.

use core::libc::*;
use cairo::{CAIRO_STATUS_SUCCESS, cairo_format_t, cairo_status_t, cairo_surface_t, cairo_t};
use cairo::bindgen::{cairo_create, cairo_fill, cairo_image_surface_create};
use cairo::bindgen::{cairo_image_surface_get_data, cairo_image_surface_get_format};
use cairo::bindgen::{cairo_image_surface_get_height, cairo_image_surface_get_stride};
use cairo::bindgen::{cairo_image_surface_get_width, cairo_rectangle, cairo_set_line_width};
use cairo::bindgen::{cairo_set_source_rgb, cairo_stroke, cairo_surface_destroy};
use cairo::bindgen::{cairo_surface_reference};
use core::cast::{reinterpret_cast, transmute};

// FIXME: We should have a hierarchy of surfaces, but this needs to wait on case classes.
pub struct ImageSurface {
    cairo_surface: *cairo_surface_t,
}

impl Drop for ImageSurface {
    fn finalize(&self) {
        unsafe {
            cairo_surface_destroy(self.cairo_surface);
        }
    }
}


pub impl ImageSurface {
    fn width(&self)  -> c_int {
        unsafe {
            cairo_image_surface_get_width(self.cairo_surface)
        }
    }
    fn height(&self) -> c_int {
        unsafe {
            cairo_image_surface_get_height(self.cairo_surface)
        }
    }
    fn stride(&self) -> c_int {
        unsafe {
            cairo_image_surface_get_stride(self.cairo_surface)
        }
    }
    fn format(&self) -> c_int    {
        unsafe {
            cairo_image_surface_get_format(self.cairo_surface)
        }
    }

    fn data(&self) -> &'self [u8] {
        unsafe {
            let buffer = cairo_image_surface_get_data(self.cairo_surface);
            let len = (self.stride() * self.height()) as uint;
            return vec::raw::buf_as_slice(buffer, len, |x| transmute(x));
        }
    }
}

// Should be private.
fn image_surface_from_cairo_surface(cairo_surface: *cairo_surface_t) -> ImageSurface {
    fail_unless!(!cairo_surface.is_null());
    ImageSurface { cairo_surface: cairo_surface }
}

pub fn ImageSurface(format: cairo_format_t, width: c_int, height: c_int) -> ImageSurface {
    unsafe {
        let cairo_surface = cairo_image_surface_create(format, width, height);
        if cairo_surface.is_null() {
            fail!(~"couldn't create Cairo image surface");
        }
        return image_surface_from_cairo_surface(cairo_surface);
    }
}

impl ImageSurface {
    /*fn write_to_png_stream(buffer: &BytesWriter) -> Result<(),cairo_status_t> unsafe {
        extern fn write_fn(closure: *c_void, data: *c_uchar, len: c_uint)
                        -> cairo_status_t unsafe {
            let writer: *BytesWriter = reinterpret_cast(&closure);
            do buf_as_slice(data, len as uint) |bytes| {<commit_msg>Change fail_unless! to assert! and remove unused imports.<commit_after>// High-level bindings to Cairo.

use core::libc::*;
use cairo::{cairo_format_t, cairo_surface_t, cairo_t};
use cairo::bindgen::{cairo_create, cairo_fill, cairo_image_surface_create};
use cairo::bindgen::{cairo_image_surface_get_data, cairo_image_surface_get_format};
use cairo::bindgen::{cairo_image_surface_get_height, cairo_image_surface_get_stride};
use cairo::bindgen::{cairo_image_surface_get_width, cairo_rectangle, cairo_set_line_width};
use cairo::bindgen::{cairo_set_source_rgb, cairo_stroke, cairo_surface_destroy};
use cairo::bindgen::{cairo_surface_reference};
use core::cast::transmute;

// FIXME: We should have a hierarchy of surfaces, but this needs to wait on case classes.
pub struct ImageSurface {
    cairo_surface: *cairo_surface_t,
}

impl Drop for ImageSurface {
    fn finalize(&self) {
        unsafe {
            cairo_surface_destroy(self.cairo_surface);
        }
    }
}


pub impl ImageSurface {
    fn width(&self)  -> c_int {
        unsafe {
            cairo_image_surface_get_width(self.cairo_surface)
        }
    }
    fn height(&self) -> c_int {
        unsafe {
            cairo_image_surface_get_height(self.cairo_surface)
        }
    }
    fn stride(&self) -> c_int {
        unsafe {
            cairo_image_surface_get_stride(self.cairo_surface)
        }
    }
    fn format(&self) -> c_int    {
        unsafe {
            cairo_image_surface_get_format(self.cairo_surface)
        }
    }

    fn data(&self) -> &'self [u8] {
        unsafe {
            let buffer = cairo_image_surface_get_data(self.cairo_surface);
            let len = (self.stride() * self.height()) as uint;
            return vec::raw::buf_as_slice(buffer, len, |x| transmute(x));
        }
    }
}

// Should be private.
fn image_surface_from_cairo_surface(cairo_surface: *cairo_surface_t) -> ImageSurface {
    assert!(!cairo_surface.is_null());
    ImageSurface { cairo_surface: cairo_surface }
}

pub fn ImageSurface(format: cairo_format_t, width: c_int, height: c_int) -> ImageSurface {
    unsafe {
        let cairo_surface = cairo_image_surface_create(format, width, height);
        if cairo_surface.is_null() {
            fail!(~"couldn't create Cairo image surface");
        }
        return image_surface_from_cairo_surface(cairo_surface);
    }
}

impl ImageSurface {
    /*fn write_to_png_stream(buffer: &BytesWriter) -> Result<(),cairo_status_t> unsafe {
        extern fn write_fn(closure: *c_void, data: *c_uchar, len: c_uint)
                        -> cairo_status_t unsafe {
            let writer: *BytesWriter = reinterpret_cast(&closure);
            do buf_as_slice(data, len as uint) |bytes| {
{'fraction_non_alphanumeric': 0.10669110907424381, 'fraction_numerical': 0.00036663611365719525, 'mean_word_length': 3.2758620689655173, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '5764394', 'n_tokens_mistral': 1803, 'n_tokens_neox': 1804, 'n_words': 342}