| 1 |
// Copyright 2015 The Servo Project Developers. See the COPYRIGHT |
| 2 |
// file at the top-level directory of this distribution. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 5 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 6 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 7 |
// option. This file may not be copied, modified, or distributed |
| 8 |
// except according to those terms. |
| 9 |
|
| 10 |
use base::CGFloat; |
| 11 |
use color_space::CGColorSpace; |
| 12 |
use core_foundation::base::{CFTypeID, TCFType}; |
| 13 |
use font::{CGFont, CGGlyph}; |
| 14 |
use geometry::{CGPoint, CGSize}; |
| 15 |
use gradient::{CGGradient, CGGradientDrawingOptions}; |
| 16 |
use color::CGColor; |
| 17 |
use path::CGPathRef; |
| 18 |
use libc::{c_int, size_t}; |
| 19 |
use std::os::raw::c_void; |
| 20 |
|
| 21 |
use std::cmp; |
| 22 |
use std::ptr; |
| 23 |
use std::slice; |
| 24 |
use geometry::{CGAffineTransform, CGRect}; |
| 25 |
use image::CGImage; |
| 26 |
use foreign_types::{ForeignType, ForeignTypeRef}; |
| 27 |
|
| 28 |
#[repr(C)] |
| 29 |
#[derive(Clone, Copy, Debug)] |
| 30 |
pub enum CGBlendMode { |
| 31 |
Normal = 0, |
| 32 |
Multiply, |
| 33 |
Screen, |
| 34 |
Overlay, |
| 35 |
Darken, |
| 36 |
Lighten, |
| 37 |
ColorDodge, |
| 38 |
ColorBurn, |
| 39 |
SoftLight, |
| 40 |
HardLight, |
| 41 |
Difference, |
| 42 |
Exclusion, |
| 43 |
Hue, |
| 44 |
Saturation, |
| 45 |
Color, |
| 46 |
Luminosity, |
| 47 |
// 10.5 and up: |
| 48 |
Clear, |
| 49 |
Copy, |
| 50 |
SourceIn, |
| 51 |
SourceOut, |
| 52 |
SourceAtop, |
| 53 |
DestinationOver, |
| 54 |
DestinationIn, |
| 55 |
DestinationOut, |
| 56 |
DestinationAtop, |
| 57 |
Xor, |
| 58 |
PlusDarker, |
| 59 |
PlusLighter, |
| 60 |
} |
| 61 |
|
| 62 |
#[repr(C)] |
| 63 |
pub enum CGTextDrawingMode { |
| 64 |
CGTextFill, |
| 65 |
CGTextStroke, |
| 66 |
CGTextFillStroke, |
| 67 |
CGTextInvisible, |
| 68 |
CGTextFillClip, |
| 69 |
CGTextStrokeClip, |
| 70 |
CGTextFillStrokeClip, |
| 71 |
CGTextClip |
| 72 |
} |
| 73 |
|
| 74 |
#[repr(C)] |
| 75 |
#[derive(Clone, Copy, Debug)] |
| 76 |
pub enum CGLineCap { |
| 77 |
CGLineCapButt, |
| 78 |
CGLineCapRound, |
| 79 |
CGLineCapSquare, |
| 80 |
} |
| 81 |
|
| 82 |
#[repr(C)] |
| 83 |
#[derive(Clone, Copy, Debug)] |
| 84 |
pub enum CGLineJoin { |
| 85 |
CGLineJoinMiter, |
| 86 |
CGLineJoinRound, |
| 87 |
CGLineJoinBevel, |
| 88 |
} |
| 89 |
|
| 90 |
#[repr(C)] |
| 91 |
#[derive(Clone, Copy, Debug)] |
| 92 |
pub enum CGPathDrawingMode { |
| 93 |
CGPathFill, |
| 94 |
CGPathEOFill, |
| 95 |
CGPathStroke, |
| 96 |
CGPathFillStroke, |
| 97 |
CGPathEOFillStroke, |
| 98 |
} |
| 99 |
|
| 100 |
#[repr(C)] |
| 101 |
#[derive(Clone, Copy, Debug)] |
| 102 |
pub enum CGInterpolationQuality { |
| 103 |
CGInterpolationQualityDefault, |
| 104 |
CGInterpolationQualityNone, |
| 105 |
CGInterpolationQualityLow, |
| 106 |
CGInterpolationQualityMedium, |
| 107 |
CGInterpolationQualityHigh, |
| 108 |
} |
| 109 |
|
| 110 |
foreign_type! { |
| 111 |
#[doc(hidden)] |
| 112 |
pub unsafe type CGContext { |
| 113 |
type CType = ::sys::CGContext; |
| 114 |
fn drop = |cs| CGContextRelease(cs); |
| 115 |
fn clone = |p| CGContextRetain(p); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
impl CGContext { |
| 120 |
pub fn type_id() -> CFTypeID { |
| 121 |
unsafe { |
| 122 |
CGContextGetTypeID() |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/// Creates a `CGContext` instance from an existing [`CGContextRef`] pointer. |
| 127 |
/// |
| 128 |
/// This funtion will internally call [`CGRetain`] and hence there is no need to call it explicitly. |
| 129 |
/// |
| 130 |
/// This function is particularly useful for cases when the context is not instantiated/managed |
| 131 |
/// by the caller, but it's retrieve via other means (e.g., by calling the method [`NSGraphicsContext::CGContext`] |
| 132 |
/// in a cocoa application). |
| 133 |
/// |
| 134 |
/// [`CGContextRef`]: https://developer.apple.com/documentation/coregraphics/cgcontextref |
| 135 |
/// [`CGRetain`]: https://developer.apple.com/documentation/coregraphics/1586506-cgcontextretain |
| 136 |
/// [`NSGraphicsContext::CGContext`]: https://developer.apple.com/documentation/appkit/nsgraphicscontext/1535352-currentcontext |
| 137 |
pub unsafe fn from_existing_context_ptr(ctx: *mut ::sys::CGContext) -> CGContext { |
| 138 |
CGContextRetain(ctx); |
| 139 |
Self::from_ptr(ctx) |
| 140 |
} |
| 141 |
|
| 142 |
pub fn create_bitmap_context(data: Option<*mut c_void>, |
| 143 |
width: size_t, |
| 144 |
height: size_t, |
| 145 |
bits_per_component: size_t, |
| 146 |
bytes_per_row: size_t, |
| 147 |
space: &CGColorSpace, |
| 148 |
bitmap_info: u32) |
| 149 |
-> CGContext { |
| 150 |
unsafe { |
| 151 |
let result = CGBitmapContextCreate(data.unwrap_or(ptr::null_mut()), |
| 152 |
width, |
| 153 |
height, |
| 154 |
bits_per_component, |
| 155 |
bytes_per_row, |
| 156 |
space.as_ptr(), |
| 157 |
bitmap_info); |
| 158 |
assert!(!result.is_null()); |
| 159 |
Self::from_ptr(result) |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
pub fn data(&mut self) -> &mut [u8] { |
| 164 |
unsafe { |
| 165 |
slice::from_raw_parts_mut( |
| 166 |
CGBitmapContextGetData(self.as_ptr()) as *mut u8, |
| 167 |
(self.height() * self.bytes_per_row()) as usize) |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
impl CGContextRef { |
| 173 |
pub fn flush(&self) { |
| 174 |
unsafe { |
| 175 |
CGContextFlush(self.as_ptr()) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
pub fn width(&self) -> size_t { |
| 180 |
unsafe { |
| 181 |
CGBitmapContextGetWidth(self.as_ptr()) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
pub fn height(&self) -> size_t { |
| 186 |
unsafe { |
| 187 |
CGBitmapContextGetHeight(self.as_ptr()) |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
pub fn bytes_per_row(&self) -> size_t { |
| 192 |
unsafe { |
| 193 |
CGBitmapContextGetBytesPerRow(self.as_ptr()) |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
pub fn clip_bounding_box(&self) -> CGRect { |
| 198 |
unsafe { |
| 199 |
CGContextGetClipBoundingBox(self.as_ptr()) |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
pub fn set_fill_color(&self, color: &CGColor) { |
| 204 |
unsafe { |
| 205 |
CGContextSetFillColorWithColor(self.as_ptr(), color.as_concrete_TypeRef()); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
pub fn set_rgb_fill_color(&self, red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) { |
| 210 |
unsafe { |
| 211 |
CGContextSetRGBFillColor(self.as_ptr(), red, green, blue, alpha) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
pub fn set_rgb_stroke_color(&self, red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) { |
| 216 |
unsafe { |
| 217 |
CGContextSetRGBStrokeColor(self.as_ptr(), red, green, blue, alpha) |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
pub fn set_gray_fill_color(&self, gray: CGFloat, alpha: CGFloat) { |
| 222 |
unsafe { |
| 223 |
CGContextSetGrayFillColor(self.as_ptr(), gray, alpha) |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
pub fn set_blend_mode(&self, blend_mode: CGBlendMode) { |
| 228 |
unsafe { |
| 229 |
CGContextSetBlendMode(self.as_ptr(), blend_mode) |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
pub fn set_allows_font_smoothing(&self, allows_font_smoothing: bool) { |
| 234 |
unsafe { |
| 235 |
CGContextSetAllowsFontSmoothing(self.as_ptr(), allows_font_smoothing) |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
pub fn set_font_smoothing_style(&self, style: i32) { |
| 240 |
unsafe { |
| 241 |
CGContextSetFontSmoothingStyle(self.as_ptr(), style as _); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
pub fn set_should_smooth_fonts(&self, should_smooth_fonts: bool) { |
| 246 |
unsafe { |
| 247 |
CGContextSetShouldSmoothFonts(self.as_ptr(), should_smooth_fonts) |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
pub fn set_allows_antialiasing(&self, allows_antialiasing: bool) { |
| 252 |
unsafe { |
| 253 |
CGContextSetAllowsAntialiasing(self.as_ptr(), allows_antialiasing) |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
pub fn set_should_antialias(&self, should_antialias: bool) { |
| 258 |
unsafe { |
| 259 |
CGContextSetShouldAntialias(self.as_ptr(), should_antialias) |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
pub fn set_allows_font_subpixel_quantization(&self, allows_font_subpixel_quantization: bool) { |
| 264 |
unsafe { |
| 265 |
CGContextSetAllowsFontSubpixelQuantization(self.as_ptr(), allows_font_subpixel_quantization) |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
pub fn set_should_subpixel_quantize_fonts(&self, should_subpixel_quantize_fonts: bool) { |
| 270 |
unsafe { |
| 271 |
CGContextSetShouldSubpixelQuantizeFonts(self.as_ptr(), should_subpixel_quantize_fonts) |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
pub fn set_allows_font_subpixel_positioning(&self, allows_font_subpixel_positioning: bool) { |
| 276 |
unsafe { |
| 277 |
CGContextSetAllowsFontSubpixelPositioning(self.as_ptr(), allows_font_subpixel_positioning) |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
pub fn set_should_subpixel_position_fonts(&self, should_subpixel_position_fonts: bool) { |
| 282 |
unsafe { |
| 283 |
CGContextSetShouldSubpixelPositionFonts(self.as_ptr(), should_subpixel_position_fonts) |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
pub fn set_text_drawing_mode(&self, mode: CGTextDrawingMode) { |
| 288 |
unsafe { |
| 289 |
CGContextSetTextDrawingMode(self.as_ptr(), mode) |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
pub fn set_line_cap(&self, cap: CGLineCap) { |
| 294 |
unsafe { |
| 295 |
CGContextSetLineCap(self.as_ptr(), cap) |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
pub fn set_line_dash(&self, phase: CGFloat, lengths: &[CGFloat]) { |
| 300 |
unsafe { |
| 301 |
CGContextSetLineDash(self.as_ptr(), phase, lengths.as_ptr(), lengths.len()) |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
pub fn set_line_join(&self, join: CGLineJoin) { |
| 306 |
unsafe { |
| 307 |
CGContextSetLineJoin(self.as_ptr(), join) |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
pub fn set_line_width(&self, width: CGFloat) { |
| 312 |
unsafe { |
| 313 |
CGContextSetLineWidth(self.as_ptr(), width) |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
pub fn set_miter_limit(&self, limit: CGFloat) { |
| 318 |
unsafe { |
| 319 |
CGContextSetMiterLimit(self.as_ptr(), limit) |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
pub fn add_path(&self, path: &CGPathRef) { |
| 324 |
unsafe { |
| 325 |
CGContextAddPath(self.as_ptr(), path.as_ptr()); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
pub fn add_curve_to_point(&self, |
| 330 |
cp1x: CGFloat, |
| 331 |
cp1y: CGFloat, |
| 332 |
cp2x: CGFloat, |
| 333 |
cp2y: CGFloat, |
| 334 |
x: CGFloat, |
| 335 |
y: CGFloat) { |
| 336 |
unsafe { |
| 337 |
CGContextAddCurveToPoint(self.as_ptr(), |
| 338 |
cp1x, cp1y, |
| 339 |
cp2x, cp2y, |
| 340 |
x, y); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
pub fn add_quad_curve_to_point(&self, |
| 345 |
cpx: CGFloat, |
| 346 |
cpy: CGFloat, |
| 347 |
x: CGFloat, |
| 348 |
y: CGFloat) { |
| 349 |
unsafe { |
| 350 |
CGContextAddQuadCurveToPoint(self.as_ptr(), |
| 351 |
cpx, cpy, |
| 352 |
x, y); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
pub fn add_line_to_point(&self, x: CGFloat, y: CGFloat) { |
| 357 |
unsafe { |
| 358 |
CGContextAddLineToPoint(self.as_ptr(), x, y); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
pub fn begin_path(&self) { |
| 363 |
unsafe { |
| 364 |
CGContextBeginPath(self.as_ptr()); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
pub fn close_path(&self) { |
| 369 |
unsafe { |
| 370 |
CGContextClosePath(self.as_ptr()); |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
pub fn move_to_point(&self, x: CGFloat, y: CGFloat) { |
| 375 |
unsafe { |
| 376 |
CGContextMoveToPoint(self.as_ptr(), x, y); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
pub fn clip(&self) { |
| 381 |
unsafe { |
| 382 |
CGContextClip(self.as_ptr()); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
pub fn eo_clip(&self) { |
| 387 |
unsafe { |
| 388 |
CGContextEOClip(self.as_ptr()); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
pub fn reset_clip(&self) { |
| 393 |
unsafe { |
| 394 |
CGContextResetClip(self.as_ptr()); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
pub fn draw_path(&self, mode: CGPathDrawingMode) { |
| 399 |
unsafe { |
| 400 |
CGContextDrawPath(self.as_ptr(), mode); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
pub fn fill_path(&self) { |
| 405 |
unsafe { |
| 406 |
CGContextFillPath(self.as_ptr()); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
pub fn eo_fill_path(&self) { |
| 411 |
unsafe { |
| 412 |
CGContextEOFillPath(self.as_ptr()); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
pub fn stroke_path(&self) { |
| 417 |
unsafe { |
| 418 |
CGContextStrokePath(self.as_ptr()); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
pub fn fill_rect(&self, rect: CGRect) { |
| 423 |
unsafe { |
| 424 |
CGContextFillRect(self.as_ptr(), rect) |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
pub fn fill_rects(&self, rects: &[CGRect]) { |
| 429 |
unsafe { |
| 430 |
CGContextFillRects(self.as_ptr(), rects.as_ptr(), rects.len()) |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
pub fn clear_rect(&self, rect: CGRect) { |
| 435 |
unsafe { |
| 436 |
CGContextClearRect(self.as_ptr(), rect) |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
pub fn stroke_rect(&self, rect: CGRect) { |
| 441 |
unsafe { |
| 442 |
CGContextStrokeRect(self.as_ptr(), rect) |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
pub fn stroke_rect_with_width(&self, rect: CGRect, width: CGFloat) { |
| 447 |
unsafe { |
| 448 |
CGContextStrokeRectWithWidth(self.as_ptr(), rect, width) |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
pub fn clip_to_rect(&self, rect: CGRect) { |
| 453 |
unsafe { |
| 454 |
CGContextClipToRect(self.as_ptr(), rect) |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
pub fn clip_to_rects(&self, rects: &[CGRect]) { |
| 459 |
unsafe { |
| 460 |
CGContextClipToRects(self.as_ptr(), rects.as_ptr(), rects.len()) |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
pub fn clip_to_mask(&self, rect: CGRect, image: &CGImage) { |
| 465 |
unsafe { |
| 466 |
CGContextClipToMask(self.as_ptr(), rect, image.as_ptr()) |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
pub fn replace_path_with_stroked_path(&self) { |
| 471 |
unsafe { |
| 472 |
CGContextReplacePathWithStrokedPath(self.as_ptr()) |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
pub fn fill_ellipse_in_rect(&self, rect: CGRect) { |
| 477 |
unsafe { |
| 478 |
CGContextFillEllipseInRect(self.as_ptr(), rect) |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
pub fn stroke_ellipse_in_rect(&self, rect: CGRect) { |
| 483 |
unsafe { |
| 484 |
CGContextStrokeEllipseInRect(self.as_ptr(), rect) |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
pub fn stroke_line_segments(&self, points: &[CGPoint]) { |
| 489 |
unsafe { |
| 490 |
CGContextStrokeLineSegments(self.as_ptr(), points.as_ptr(), points.len()) |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
pub fn set_interpolation_quality(&self, quality: CGInterpolationQuality) { |
| 495 |
unsafe { |
| 496 |
CGContextSetInterpolationQuality(self.as_ptr(), quality); |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
pub fn get_interpolation_quality(&self) -> CGInterpolationQuality { |
| 501 |
unsafe { |
| 502 |
CGContextGetInterpolationQuality(self.as_ptr()) |
| 503 |
|
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
pub fn draw_image(&self, rect: CGRect, image: &CGImage) { |
| 508 |
unsafe { |
| 509 |
CGContextDrawImage(self.as_ptr(), rect, image.as_ptr()); |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
pub fn create_image(&self) -> Option<CGImage> { |
| 514 |
let image = unsafe { CGBitmapContextCreateImage(self.as_ptr()) }; |
| 515 |
if !image.is_null() { |
| 516 |
Some(unsafe { CGImage::from_ptr(image) }) |
| 517 |
} else { |
| 518 |
None |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
pub fn set_font(&self, font: &CGFont) { |
| 523 |
unsafe { |
| 524 |
CGContextSetFont(self.as_ptr(), font.as_ptr()) |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
pub fn set_font_size(&self, size: CGFloat) { |
| 529 |
unsafe { |
| 530 |
CGContextSetFontSize(self.as_ptr(), size) |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
pub fn set_text_matrix(&self, t: &CGAffineTransform) { |
| 535 |
unsafe { |
| 536 |
CGContextSetTextMatrix(self.as_ptr(), *t) |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
pub fn set_text_position(&self, x: CGFloat, y: CGFloat) { |
| 541 |
unsafe { |
| 542 |
CGContextSetTextPosition(self.as_ptr(), x, y) |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
pub fn show_glyphs_at_positions(&self, glyphs: &[CGGlyph], positions: &[CGPoint]) { |
| 547 |
unsafe { |
| 548 |
let count = cmp::min(glyphs.len(), positions.len()); |
| 549 |
CGContextShowGlyphsAtPositions(self.as_ptr(), |
| 550 |
glyphs.as_ptr(), |
| 551 |
positions.as_ptr(), |
| 552 |
count) |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
pub fn save(&self) { |
| 557 |
unsafe { |
| 558 |
CGContextSaveGState(self.as_ptr()); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
pub fn restore(&self) { |
| 563 |
unsafe { |
| 564 |
CGContextRestoreGState(self.as_ptr()); |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
pub fn translate(&self, tx: CGFloat, ty: CGFloat) { |
| 569 |
unsafe { |
| 570 |
CGContextTranslateCTM(self.as_ptr(), tx, ty); |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
pub fn scale(&self, sx: CGFloat, sy: CGFloat) { |
| 575 |
unsafe { |
| 576 |
CGContextScaleCTM(self.as_ptr(), sx, sy); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
pub fn rotate(&self, angle: CGFloat) { |
| 581 |
unsafe { |
| 582 |
CGContextRotateCTM(self.as_ptr(), angle); |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
pub fn get_ctm(&self) -> CGAffineTransform { |
| 587 |
unsafe { |
| 588 |
CGContextGetCTM(self.as_ptr()) |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
pub fn concat_ctm(&self, transform: CGAffineTransform) { |
| 593 |
unsafe { |
| 594 |
CGContextConcatCTM(self.as_ptr(), transform) |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
pub fn draw_linear_gradient(&self, gradient: &CGGradient, start_point: CGPoint, end_point: CGPoint, options: CGGradientDrawingOptions) { |
| 599 |
unsafe { |
| 600 |
CGContextDrawLinearGradient(self.as_ptr(), gradient.as_ptr(), start_point, end_point, options); |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
pub fn draw_radial_gradient(&self, gradient: &CGGradient, start_center: CGPoint, start_radius: CGFloat, end_center: CGPoint, end_radius: CGFloat, options: CGGradientDrawingOptions) { |
| 605 |
unsafe { |
| 606 |
CGContextDrawRadialGradient(self.as_ptr(), gradient.as_ptr(), start_center, start_radius, end_center, end_radius, options); |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
pub fn set_shadow(&self, offset: CGSize, blur: CGFloat) { |
| 611 |
unsafe { |
| 612 |
CGContextSetShadow(self.as_ptr(), offset, blur); |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
pub fn set_shadow_with_color(&self, offset: CGSize, blur: CGFloat, color: &CGColor) { |
| 617 |
unsafe { |
| 618 |
CGContextSetShadowWithColor(self.as_ptr(), offset, blur, color.as_concrete_TypeRef()); |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
pub fn set_alpha(&self, alpha: CGFloat) { |
| 623 |
unsafe { |
| 624 |
CGContextSetAlpha(self.as_ptr(), alpha); |
| 625 |
} |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
#[test] |
| 630 |
fn create_bitmap_context_test() { |
| 631 |
use geometry::*; |
| 632 |
|
| 633 |
let cs = CGColorSpace::create_device_rgb(); |
| 634 |
let ctx = CGContext::create_bitmap_context(None, |
| 635 |
16, 8, |
| 636 |
8, 0, |
| 637 |
&cs, |
| 638 |
::base::kCGImageAlphaPremultipliedLast); |
| 639 |
ctx.set_rgb_fill_color(1.,0.,1.,1.); |
| 640 |
ctx.set_miter_limit(4.); |
| 641 |
ctx.fill_rect(CGRect::new(&CGPoint::new(0.,0.), &CGSize::new(8.,8.))); |
| 642 |
let img = ctx.create_image().unwrap(); |
| 643 |
assert_eq!(16, img.width()); |
| 644 |
assert_eq!(8, img.height()); |
| 645 |
assert_eq!(8, img.bits_per_component()); |
| 646 |
assert_eq!(32, img.bits_per_pixel()); |
| 647 |
let data = img.data(); |
| 648 |
assert_eq!(255, data.bytes()[0]); |
| 649 |
assert_eq!(0, data.bytes()[1]); |
| 650 |
assert_eq!(255, data.bytes()[2]); |
| 651 |
assert_eq!(255, data.bytes()[3]); |
| 652 |
} |
| 653 |
|
| 654 |
#[link(name = "CoreGraphics", kind = "framework")] |
| 655 |
extern { |
| 656 |
fn CGContextRetain(c: ::sys::CGContextRef) -> ::sys::CGContextRef; |
| 657 |
fn CGContextRelease(c: ::sys::CGContextRef); |
| 658 |
|
| 659 |
fn CGBitmapContextCreate(data: *mut c_void, |
| 660 |
width: size_t, |
| 661 |
height: size_t, |
| 662 |
bitsPerComponent: size_t, |
| 663 |
bytesPerRow: size_t, |
| 664 |
space: ::sys::CGColorSpaceRef, |
| 665 |
bitmapInfo: u32) |
| 666 |
-> ::sys::CGContextRef; |
| 667 |
fn CGBitmapContextGetData(context: ::sys::CGContextRef) -> *mut c_void; |
| 668 |
fn CGBitmapContextGetWidth(context: ::sys::CGContextRef) -> size_t; |
| 669 |
fn CGBitmapContextGetHeight(context: ::sys::CGContextRef) -> size_t; |
| 670 |
fn CGBitmapContextGetBytesPerRow(context: ::sys::CGContextRef) -> size_t; |
| 671 |
fn CGBitmapContextCreateImage(context: ::sys::CGContextRef) -> ::sys::CGImageRef; |
| 672 |
fn CGContextGetTypeID() -> CFTypeID; |
| 673 |
fn CGContextGetClipBoundingBox(c: ::sys::CGContextRef) -> CGRect; |
| 674 |
fn CGContextFlush(c: ::sys::CGContextRef); |
| 675 |
fn CGContextSetBlendMode(c: ::sys::CGContextRef, blendMode: CGBlendMode); |
| 676 |
fn CGContextSetAllowsFontSmoothing(c: ::sys::CGContextRef, allowsFontSmoothing: bool); |
| 677 |
fn CGContextSetShouldSmoothFonts(c: ::sys::CGContextRef, shouldSmoothFonts: bool); |
| 678 |
fn CGContextSetFontSmoothingStyle(c: ::sys::CGContextRef, style: c_int); |
| 679 |
fn CGContextSetAllowsAntialiasing(c: ::sys::CGContextRef, allowsAntialiasing: bool); |
| 680 |
fn CGContextSetShouldAntialias(c: ::sys::CGContextRef, shouldAntialias: bool); |
| 681 |
fn CGContextSetAllowsFontSubpixelQuantization(c: ::sys::CGContextRef, |
| 682 |
allowsFontSubpixelQuantization: bool); |
| 683 |
fn CGContextSetShouldSubpixelQuantizeFonts(c: ::sys::CGContextRef, |
| 684 |
shouldSubpixelQuantizeFonts: bool); |
| 685 |
fn CGContextSetAllowsFontSubpixelPositioning(c: ::sys::CGContextRef, |
| 686 |
allowsFontSubpixelPositioning: bool); |
| 687 |
fn CGContextSetShouldSubpixelPositionFonts(c: ::sys::CGContextRef, |
| 688 |
shouldSubpixelPositionFonts: bool); |
| 689 |
fn CGContextSetTextDrawingMode(c: ::sys::CGContextRef, mode: CGTextDrawingMode); |
| 690 |
fn CGContextSetFillColorWithColor(c: ::sys::CGContextRef, color: ::sys::CGColorRef); |
| 691 |
fn CGContextSetLineCap(c: ::sys::CGContextRef, cap: CGLineCap); |
| 692 |
fn CGContextSetLineDash(c: ::sys::CGContextRef, phase: CGFloat, lengths: *const CGFloat, count: size_t); |
| 693 |
fn CGContextSetLineJoin(c: ::sys::CGContextRef, join: CGLineJoin); |
| 694 |
fn CGContextSetLineWidth(c: ::sys::CGContextRef, width: CGFloat); |
| 695 |
fn CGContextSetMiterLimit(c: ::sys::CGContextRef, limit: CGFloat); |
| 696 |
|
| 697 |
fn CGContextAddPath(c: ::sys::CGContextRef, path: ::sys::CGPathRef); |
| 698 |
fn CGContextAddCurveToPoint(c: ::sys::CGContextRef, |
| 699 |
cp1x: CGFloat, |
| 700 |
cp1y: CGFloat, |
| 701 |
cp2x: CGFloat, |
| 702 |
cp2y: CGFloat, |
| 703 |
x: CGFloat, |
| 704 |
y: CGFloat); |
| 705 |
fn CGContextAddQuadCurveToPoint(c: ::sys::CGContextRef, |
| 706 |
cpx: CGFloat, |
| 707 |
cpy: CGFloat, |
| 708 |
x: CGFloat, |
| 709 |
y: CGFloat); |
| 710 |
fn CGContextAddLineToPoint(c: ::sys::CGContextRef, |
| 711 |
x: CGFloat, |
| 712 |
y: CGFloat); |
| 713 |
fn CGContextBeginPath(c: ::sys::CGContextRef); |
| 714 |
fn CGContextClosePath(c: ::sys::CGContextRef); |
| 715 |
fn CGContextMoveToPoint(c: ::sys::CGContextRef, |
| 716 |
x: CGFloat, |
| 717 |
y: CGFloat); |
| 718 |
fn CGContextDrawPath(c: ::sys::CGContextRef, mode: CGPathDrawingMode); |
| 719 |
fn CGContextFillPath(c: ::sys::CGContextRef); |
| 720 |
fn CGContextEOFillPath(c: ::sys::CGContextRef); |
| 721 |
fn CGContextClip(c: ::sys::CGContextRef); |
| 722 |
fn CGContextEOClip(c: ::sys::CGContextRef); |
| 723 |
fn CGContextResetClip(c: ::sys::CGContextRef); |
| 724 |
fn CGContextStrokePath(c: ::sys::CGContextRef); |
| 725 |
fn CGContextSetRGBFillColor(context: ::sys::CGContextRef, |
| 726 |
red: CGFloat, |
| 727 |
green: CGFloat, |
| 728 |
blue: CGFloat, |
| 729 |
alpha: CGFloat); |
| 730 |
fn CGContextSetRGBStrokeColor(context: ::sys::CGContextRef, |
| 731 |
red: CGFloat, |
| 732 |
green: CGFloat, |
| 733 |
blue: CGFloat, |
| 734 |
alpha: CGFloat); |
| 735 |
fn CGContextSetGrayFillColor(context: ::sys::CGContextRef, gray: CGFloat, alpha: CGFloat); |
| 736 |
fn CGContextClearRect(context: ::sys::CGContextRef, |
| 737 |
rect: CGRect); |
| 738 |
fn CGContextFillRect(context: ::sys::CGContextRef, |
| 739 |
rect: CGRect); |
| 740 |
fn CGContextFillRects(context: ::sys::CGContextRef, |
| 741 |
rects: *const CGRect, |
| 742 |
count: size_t); |
| 743 |
fn CGContextStrokeRect(context: ::sys::CGContextRef, |
| 744 |
rect: CGRect); |
| 745 |
fn CGContextStrokeRectWithWidth(context: ::sys::CGContextRef, |
| 746 |
rect: CGRect, |
| 747 |
width: CGFloat); |
| 748 |
fn CGContextClipToRect(context: ::sys::CGContextRef, |
| 749 |
rect: CGRect); |
| 750 |
fn CGContextClipToRects(context: ::sys::CGContextRef, |
| 751 |
rects: *const CGRect, |
| 752 |
count: size_t); |
| 753 |
fn CGContextClipToMask(ctx: ::sys::CGContextRef, rect: CGRect, mask: ::sys::CGImageRef); |
| 754 |
fn CGContextReplacePathWithStrokedPath(context: ::sys::CGContextRef); |
| 755 |
fn CGContextFillEllipseInRect(context: ::sys::CGContextRef, |
| 756 |
rect: CGRect); |
| 757 |
fn CGContextStrokeEllipseInRect(context: ::sys::CGContextRef, |
| 758 |
rect: CGRect); |
| 759 |
fn CGContextStrokeLineSegments(context: ::sys::CGContextRef, |
| 760 |
points: *const CGPoint, |
| 761 |
count: size_t); |
| 762 |
fn CGContextDrawImage(c: ::sys::CGContextRef, rect: CGRect, image: ::sys::CGImageRef); |
| 763 |
fn CGContextSetInterpolationQuality(c: ::sys::CGContextRef, quality: CGInterpolationQuality); |
| 764 |
fn CGContextGetInterpolationQuality(c: ::sys::CGContextRef) -> CGInterpolationQuality; |
| 765 |
fn CGContextSetFont(c: ::sys::CGContextRef, font: ::sys::CGFontRef); |
| 766 |
fn CGContextSetFontSize(c: ::sys::CGContextRef, size: CGFloat); |
| 767 |
fn CGContextSetTextMatrix(c: ::sys::CGContextRef, t: CGAffineTransform); |
| 768 |
fn CGContextSetTextPosition(c: ::sys::CGContextRef, x: CGFloat, y: CGFloat); |
| 769 |
fn CGContextShowGlyphsAtPositions(c: ::sys::CGContextRef, |
| 770 |
glyphs: *const CGGlyph, |
| 771 |
positions: *const CGPoint, |
| 772 |
count: size_t); |
| 773 |
|
| 774 |
fn CGContextSaveGState(c: ::sys::CGContextRef); |
| 775 |
fn CGContextRestoreGState(c: ::sys::CGContextRef); |
| 776 |
fn CGContextTranslateCTM(c: ::sys::CGContextRef, tx: CGFloat, ty: CGFloat); |
| 777 |
fn CGContextScaleCTM(c: ::sys::CGContextRef, sx: CGFloat, sy: CGFloat); |
| 778 |
fn CGContextRotateCTM(c: ::sys::CGContextRef, angle: CGFloat); |
| 779 |
fn CGContextGetCTM(c: ::sys::CGContextRef) -> CGAffineTransform; |
| 780 |
fn CGContextConcatCTM(c: ::sys::CGContextRef, transform: CGAffineTransform); |
| 781 |
|
| 782 |
fn CGContextDrawLinearGradient(c: ::sys::CGContextRef, gradient: ::sys::CGGradientRef, startPoint: CGPoint, endPoint: CGPoint, options: CGGradientDrawingOptions); |
| 783 |
fn CGContextDrawRadialGradient(c: ::sys::CGContextRef, gradient: ::sys::CGGradientRef, startCenter: CGPoint, startRadius: CGFloat, endCenter:CGPoint, endRadius:CGFloat, options: CGGradientDrawingOptions); |
| 784 |
|
| 785 |
fn CGContextSetShadow(c: ::sys::CGContextRef, offset: CGSize, blur: CGFloat); |
| 786 |
fn CGContextSetShadowWithColor(c: ::sys::CGContextRef, offset: CGSize, blur: CGFloat, color: ::sys::CGColorRef); |
| 787 |
|
| 788 |
fn CGContextSetAlpha(c: ::sys::CGContextRef, alpha: CGFloat); |
| 789 |
} |
| 790 |
|