| 1 |
//! Legacy C API for functions that render directly to a `GdkPixbuf`. |
| 2 |
//! |
| 3 |
//! This is the implementation of the `rsvg_pixbuf_*` family of functions. |
| 4 |
|
| 5 |
#![cfg(feature = "pixbuf")] |
| 6 |
|
| 7 |
use std::path::PathBuf; |
| 8 |
use std::ptr; |
| 9 |
|
| 10 |
use gdk_pixbuf::{Colorspace, Pixbuf}; |
| 11 |
use glib::translate::*; |
| 12 |
use rgb::FromSlice; |
| 13 |
|
| 14 |
use super::dpi::Dpi; |
| 15 |
use super::handle::{checked_i32, set_gerror}; |
| 16 |
use super::sizing::LegacySize; |
| 17 |
|
| 18 |
use rsvg::c_api_only::{Pixel, PixelOps, Session, SharedImageSurface, SurfaceType, ToPixel}; |
| 19 |
use rsvg::{CairoRenderer, Loader, RenderingError}; |
| 20 |
|
| 21 |
/// GdkPixbuf's endian-independent RGBA8 pixel layout. |
| 22 |
type GdkPixbufRGBA = rgb::RGBA8; |
| 23 |
|
| 24 |
/// Trait to convert pixels in various formats to RGBA, for GdkPixbuf. |
| 25 |
/// |
| 26 |
/// GdkPixbuf unconditionally uses RGBA ordering regardless of endianness, |
| 27 |
/// but we need to convert to it from Cairo's endian-dependent 0xaarrggbb. |
| 28 |
trait ToGdkPixbufRGBA { |
| 29 |
fn to_pixbuf_rgba(&self) -> GdkPixbufRGBA; |
| 30 |
} |
| 31 |
|
| 32 |
impl ToGdkPixbufRGBA for Pixel { |
| 33 |
#[inline] |
| 34 |
fn to_pixbuf_rgba(&self) -> GdkPixbufRGBA { |
| 35 |
GdkPixbufRGBA { |
| 36 |
r: self.r, |
| 37 |
g: self.g, |
| 38 |
b: self.b, |
| 39 |
a: self.a, |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
pub fn empty_pixbuf() -> Result<Pixbuf, RenderingError> { |
| 45 |
// GdkPixbuf does not allow zero-sized pixbufs, but Cairo allows zero-sized |
| 46 |
// surfaces. In this case, return a 1-pixel transparent pixbuf. |
| 47 |
|
| 48 |
let pixbuf = Pixbuf::new(Colorspace::Rgb, true, 8, 1, 1) |
| 49 |
.ok_or_else(|| RenderingError::OutOfMemory(String::from("creating a Pixbuf")))?; |
| 50 |
pixbuf.put_pixel(0, 0, 0, 0, 0, 0); |
| 51 |
|
| 52 |
Ok(pixbuf) |
| 53 |
} |
| 54 |
|
| 55 |
pub fn pixbuf_from_surface(surface: &SharedImageSurface) -> Result<Pixbuf, RenderingError> { |
| 56 |
let width = surface.width(); |
| 57 |
let height = surface.height(); |
| 58 |
|
| 59 |
let pixbuf = Pixbuf::new(Colorspace::Rgb, true, 8, width, height) |
| 60 |
.ok_or_else(|| RenderingError::OutOfMemory(String::from("creating a Pixbuf")))?; |
| 61 |
|
| 62 |
assert!(pixbuf.colorspace() == Colorspace::Rgb); |
| 63 |
assert!(pixbuf.bits_per_sample() == 8); |
| 64 |
assert!(pixbuf.n_channels() == 4); |
| 65 |
|
| 66 |
let pixbuf_data = unsafe { pixbuf.pixels() }; |
| 67 |
let stride = pixbuf.rowstride() as usize; |
| 68 |
|
| 69 |
// We use chunks_mut(), not chunks_exact_mut(), because gdk-pixbuf tends |
| 70 |
// to make the last row *not* have the full stride (i.e. it is |
| 71 |
// only as wide as the pixels in that row). |
| 72 |
pixbuf_data |
| 73 |
.chunks_mut(stride) |
| 74 |
.take(height as usize) |
| 75 |
.map(|row| row.as_rgba_mut()) |
| 76 |
.zip(surface.rows()) |
| 77 |
.flat_map(|(dest_row, src_row)| src_row.iter().zip(dest_row.iter_mut())) |
| 78 |
.for_each(|(src, dest)| *dest = src.to_pixel().unpremultiply().to_pixbuf_rgba()); |
| 79 |
|
| 80 |
Ok(pixbuf) |
| 81 |
} |
| 82 |
|
| 83 |
enum SizeKind { |
| 84 |
Zoom, |
| 85 |
WidthHeight, |
| 86 |
WidthHeightMax, |
| 87 |
ZoomMax, |
| 88 |
} |
| 89 |
|
| 90 |
struct SizeMode { |
| 91 |
kind: SizeKind, |
| 92 |
x_zoom: f64, |
| 93 |
y_zoom: f64, |
| 94 |
width: i32, |
| 95 |
height: i32, |
| 96 |
} |
| 97 |
|
| 98 |
fn get_final_size(in_width: f64, in_height: f64, size_mode: &SizeMode) -> (f64, f64) { |
| 99 |
if in_width == 0.0 || in_height == 0.0 { |
| 100 |
return (0.0, 0.0); |
| 101 |
} |
| 102 |
|
| 103 |
let mut out_width; |
| 104 |
let mut out_height; |
| 105 |
|
| 106 |
match size_mode.kind { |
| 107 |
SizeKind::Zoom => { |
| 108 |
out_width = size_mode.x_zoom * in_width; |
| 109 |
out_height = size_mode.y_zoom * in_height; |
| 110 |
} |
| 111 |
|
| 112 |
SizeKind::ZoomMax => { |
| 113 |
out_width = size_mode.x_zoom * in_width; |
| 114 |
out_height = size_mode.y_zoom * in_height; |
| 115 |
|
| 116 |
if out_width > f64::from(size_mode.width) || out_height > f64::from(size_mode.height) { |
| 117 |
let zoom_x = f64::from(size_mode.width) / out_width; |
| 118 |
let zoom_y = f64::from(size_mode.height) / out_height; |
| 119 |
let zoom = zoom_x.min(zoom_y); |
| 120 |
|
| 121 |
out_width *= zoom; |
| 122 |
out_height *= zoom; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
SizeKind::WidthHeightMax => { |
| 127 |
let zoom_x = f64::from(size_mode.width) / in_width; |
| 128 |
let zoom_y = f64::from(size_mode.height) / in_height; |
| 129 |
|
| 130 |
let zoom = zoom_x.min(zoom_y); |
| 131 |
|
| 132 |
out_width = zoom * in_width; |
| 133 |
out_height = zoom * in_height; |
| 134 |
} |
| 135 |
|
| 136 |
SizeKind::WidthHeight => { |
| 137 |
if size_mode.width != -1 { |
| 138 |
out_width = f64::from(size_mode.width); |
| 139 |
} else { |
| 140 |
out_width = in_width; |
| 141 |
} |
| 142 |
|
| 143 |
if size_mode.height != -1 { |
| 144 |
out_height = f64::from(size_mode.height); |
| 145 |
} else { |
| 146 |
out_height = in_height; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
(out_width, out_height) |
| 152 |
} |
| 153 |
|
| 154 |
pub fn render_to_pixbuf_at_size( |
| 155 |
renderer: &CairoRenderer<'_>, |
| 156 |
document_width: f64, |
| 157 |
document_height: f64, |
| 158 |
desired_width: f64, |
| 159 |
desired_height: f64, |
| 160 |
) -> Result<Pixbuf, RenderingError> { |
| 161 |
if desired_width == 0.0 |
| 162 |
|| desired_height == 0.0 |
| 163 |
|| document_width == 0.0 |
| 164 |
|| document_height == 0.0 |
| 165 |
{ |
| 166 |
return empty_pixbuf(); |
| 167 |
} |
| 168 |
|
| 169 |
let surface = cairo::ImageSurface::create( |
| 170 |
cairo::Format::ARgb32, |
| 171 |
checked_i32(desired_width.ceil())?, |
| 172 |
checked_i32(desired_height.ceil())?, |
| 173 |
)?; |
| 174 |
|
| 175 |
{ |
| 176 |
let cr = cairo::Context::new(&surface)?; |
| 177 |
cr.scale( |
| 178 |
desired_width / document_width, |
| 179 |
desired_height / document_height, |
| 180 |
); |
| 181 |
|
| 182 |
let viewport = cairo::Rectangle::new(0.0, 0.0, document_width, document_height); |
| 183 |
|
| 184 |
// We do it with a cr transform so we can scale non-proportionally. |
| 185 |
renderer.render_document(&cr, &viewport)?; |
| 186 |
} |
| 187 |
|
| 188 |
let shared_surface = SharedImageSurface::wrap(surface, SurfaceType::SRgb)?; |
| 189 |
|
| 190 |
pixbuf_from_surface(&shared_surface) |
| 191 |
} |
| 192 |
|
| 193 |
unsafe fn pixbuf_from_file_with_size_mode( |
| 194 |
filename: *const libc::c_char, |
| 195 |
size_mode: &SizeMode, |
| 196 |
error: *mut *mut glib::ffi::GError, |
| 197 |
) -> *mut gdk_pixbuf::ffi::GdkPixbuf { |
| 198 |
let path = unsafe { PathBuf::from_glib_none(filename) }; |
| 199 |
|
| 200 |
let session = Session::default(); |
| 201 |
|
| 202 |
let handle = match Loader::new_with_session(session.clone()).read_path(path) { |
| 203 |
Ok(handle) => handle, |
| 204 |
Err(e) => { |
| 205 |
set_gerror(&session, error, 0, &format!("{e}")); |
| 206 |
return ptr::null_mut(); |
| 207 |
} |
| 208 |
}; |
| 209 |
|
| 210 |
let dpi = Dpi::default(); |
| 211 |
let renderer = CairoRenderer::new(&handle).with_dpi(dpi.x(), dpi.y()); |
| 212 |
|
| 213 |
let (document_width, document_height) = match renderer.legacy_document_size() { |
| 214 |
Ok(dim) => dim, |
| 215 |
Err(e) => { |
| 216 |
set_gerror(&session, error, 0, &format!("{e}")); |
| 217 |
return ptr::null_mut(); |
| 218 |
} |
| 219 |
}; |
| 220 |
|
| 221 |
let (desired_width, desired_height) = |
| 222 |
get_final_size(document_width, document_height, size_mode); |
| 223 |
|
| 224 |
render_to_pixbuf_at_size( |
| 225 |
&renderer, |
| 226 |
document_width, |
| 227 |
document_height, |
| 228 |
desired_width, |
| 229 |
desired_height, |
| 230 |
) |
| 231 |
.map(|pixbuf| pixbuf.to_glib_full()) |
| 232 |
.unwrap_or_else(|e| { |
| 233 |
set_gerror(&session, error, 0, &format!("{e}")); |
| 234 |
ptr::null_mut() |
| 235 |
}) |
| 236 |
} |
| 237 |
|
| 238 |
#[unsafe(no_mangle)] |
| 239 |
#[allow(unsafe_op_in_unsafe_fn)] |
| 240 |
pub unsafe extern "C" fn rsvg_pixbuf_from_file( |
| 241 |
filename: *const libc::c_char, |
| 242 |
error: *mut *mut glib::ffi::GError, |
| 243 |
) -> *mut gdk_pixbuf::ffi::GdkPixbuf { |
| 244 |
rsvg_return_val_if_fail! { |
| 245 |
rsvg_pixbuf_from_file => ptr::null_mut(); |
| 246 |
|
| 247 |
!filename.is_null(), |
| 248 |
error.is_null() || (*error).is_null(), |
| 249 |
} |
| 250 |
|
| 251 |
pixbuf_from_file_with_size_mode( |
| 252 |
filename, |
| 253 |
&SizeMode { |
| 254 |
kind: SizeKind::WidthHeight, |
| 255 |
x_zoom: 0.0, |
| 256 |
y_zoom: 0.0, |
| 257 |
width: -1, |
| 258 |
height: -1, |
| 259 |
}, |
| 260 |
error, |
| 261 |
) |
| 262 |
} |
| 263 |
|
| 264 |
#[unsafe(no_mangle)] |
| 265 |
#[allow(unsafe_op_in_unsafe_fn)] |
| 266 |
pub unsafe extern "C" fn rsvg_pixbuf_from_file_at_size( |
| 267 |
filename: *const libc::c_char, |
| 268 |
width: libc::c_int, |
| 269 |
height: libc::c_int, |
| 270 |
error: *mut *mut glib::ffi::GError, |
| 271 |
) -> *mut gdk_pixbuf::ffi::GdkPixbuf { |
| 272 |
rsvg_return_val_if_fail! { |
| 273 |
rsvg_pixbuf_from_file_at_size => ptr::null_mut(); |
| 274 |
|
| 275 |
!filename.is_null(), |
| 276 |
(width >= 1 && height >= 1) || (width == -1 && height == -1), |
| 277 |
error.is_null() || (*error).is_null(), |
| 278 |
} |
| 279 |
|
| 280 |
pixbuf_from_file_with_size_mode( |
| 281 |
filename, |
| 282 |
&SizeMode { |
| 283 |
kind: SizeKind::WidthHeight, |
| 284 |
x_zoom: 0.0, |
| 285 |
y_zoom: 0.0, |
| 286 |
width, |
| 287 |
height, |
| 288 |
}, |
| 289 |
error, |
| 290 |
) |
| 291 |
} |
| 292 |
|
| 293 |
#[unsafe(no_mangle)] |
| 294 |
#[allow(unsafe_op_in_unsafe_fn)] |
| 295 |
pub unsafe extern "C" fn rsvg_pixbuf_from_file_at_zoom( |
| 296 |
filename: *const libc::c_char, |
| 297 |
x_zoom: libc::c_double, |
| 298 |
y_zoom: libc::c_double, |
| 299 |
error: *mut *mut glib::ffi::GError, |
| 300 |
) -> *mut gdk_pixbuf::ffi::GdkPixbuf { |
| 301 |
rsvg_return_val_if_fail! { |
| 302 |
rsvg_pixbuf_from_file_at_zoom => ptr::null_mut(); |
| 303 |
|
| 304 |
!filename.is_null(), |
| 305 |
x_zoom > 0.0 && y_zoom > 0.0, |
| 306 |
error.is_null() || (*error).is_null(), |
| 307 |
} |
| 308 |
|
| 309 |
pixbuf_from_file_with_size_mode( |
| 310 |
filename, |
| 311 |
&SizeMode { |
| 312 |
kind: SizeKind::Zoom, |
| 313 |
x_zoom, |
| 314 |
y_zoom, |
| 315 |
width: 0, |
| 316 |
height: 0, |
| 317 |
}, |
| 318 |
error, |
| 319 |
) |
| 320 |
} |
| 321 |
|
| 322 |
#[unsafe(no_mangle)] |
| 323 |
#[allow(unsafe_op_in_unsafe_fn)] |
| 324 |
pub unsafe extern "C" fn rsvg_pixbuf_from_file_at_zoom_with_max( |
| 325 |
filename: *const libc::c_char, |
| 326 |
x_zoom: libc::c_double, |
| 327 |
y_zoom: libc::c_double, |
| 328 |
max_width: libc::c_int, |
| 329 |
max_height: libc::c_int, |
| 330 |
error: *mut *mut glib::ffi::GError, |
| 331 |
) -> *mut gdk_pixbuf::ffi::GdkPixbuf { |
| 332 |
rsvg_return_val_if_fail! { |
| 333 |
rsvg_pixbuf_from_file_at_zoom_with_max => ptr::null_mut(); |
| 334 |
|
| 335 |
!filename.is_null(), |
| 336 |
x_zoom > 0.0 && y_zoom > 0.0, |
| 337 |
max_width >= 1 && max_height >= 1, |
| 338 |
error.is_null() || (*error).is_null(), |
| 339 |
} |
| 340 |
|
| 341 |
pixbuf_from_file_with_size_mode( |
| 342 |
filename, |
| 343 |
&SizeMode { |
| 344 |
kind: SizeKind::ZoomMax, |
| 345 |
x_zoom, |
| 346 |
y_zoom, |
| 347 |
width: max_width, |
| 348 |
height: max_height, |
| 349 |
}, |
| 350 |
error, |
| 351 |
) |
| 352 |
} |
| 353 |
|
| 354 |
#[unsafe(no_mangle)] |
| 355 |
#[allow(unsafe_op_in_unsafe_fn)] |
| 356 |
pub unsafe extern "C" fn rsvg_pixbuf_from_file_at_max_size( |
| 357 |
filename: *const libc::c_char, |
| 358 |
max_width: libc::c_int, |
| 359 |
max_height: libc::c_int, |
| 360 |
error: *mut *mut glib::ffi::GError, |
| 361 |
) -> *mut gdk_pixbuf::ffi::GdkPixbuf { |
| 362 |
rsvg_return_val_if_fail! { |
| 363 |
rsvg_pixbuf_from_file_at_max_size => ptr::null_mut(); |
| 364 |
|
| 365 |
!filename.is_null(), |
| 366 |
max_width >= 1 && max_height >= 1, |
| 367 |
error.is_null() || (*error).is_null(), |
| 368 |
} |
| 369 |
|
| 370 |
pixbuf_from_file_with_size_mode( |
| 371 |
filename, |
| 372 |
&SizeMode { |
| 373 |
kind: SizeKind::WidthHeightMax, |
| 374 |
x_zoom: 0.0, |
| 375 |
y_zoom: 0.0, |
| 376 |
width: max_width, |
| 377 |
height: max_height, |
| 378 |
}, |
| 379 |
error, |
| 380 |
) |
| 381 |
} |