Skip to content

Instantly share code, notes, and snippets.

@noonat
Created June 4, 2010 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noonat/424870 to your computer and use it in GitHub Desktop.
Save noonat/424870 to your computer and use it in GitHub Desktop.
#import <UIKit/UIKit.h>
namespace PhuceContext {
// state (4.8.11.1.1)
bool save();
bool restore();
// transformations (4.8.11.1.2)
bool scale(double x, double y);
bool rotate(double angle);
bool translate(double x, double y);
bool transform(double m11, double m12, double m21, double m22,
double dx, double dy);
bool setTransform(double m11, double m12, double m21, double m22,
double dx, double dy);
// compositing (4.8.11.1.3)
enum CompositeOp {
kCompositeOpSourceAtop,
kCompositeOpSourceIn,
kCompositeOpSourceOut,
kCompositeOpSourceOver,
kCompositeOpDestinationAtop,
kCompositeOpDestinationIn,
kCompositeOpDestinationOut,
kCompositeOpDestinationOver,
kCompositeOpLighter,
kCompositeOpCopy,
kCompositeOpXor
};
bool setGlobalAlpha(double value);
bool setGlobalCompositeOperation(CompositeOp value);
// colors and styles (4.8.11.1.4)
// line styles (4.8.11.1.5)
enum LineCap {
kLineCapButt,
kLineCapRound,
kLineCapSquare
};
enum LineJoin {
kLineJoinBevel,
kLineJoinRound,
kLineJoinMiter
};
bool setLineWidth(double value);
bool setLineCap(LineCap value);
bool setLineJoin(LineJoin value);
bool setMiterLimit(double value);
// shadows (4.8.11.1.6)
// rectangles (4.8.11.1.7)
bool clearRect(double x, double y, double w, double h);
bool fillRect(double x, double y, double w, double h);
bool strokeRect(double x, double y, double w, double h);
// paths (4.8.11.1.8)
bool beginPath();
bool moveTo(double x, double y);
bool closePath();
bool lineTo(double x, double y);
bool quadraticCurveTo(double cpx, double cpy, double x, double y);
bool bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y,
double x, double y);
bool arcTo(double x1, double y1, double x2, double y2, double radius);
bool arc(double x, double y, double radius, double startAngle,
double endAngle, bool anticlockwise);
bool rect(double x, double y, double w, double h);
bool fill();
bool stroke();
bool clip();
bool isPointInPath(double x, double y, bool *result);
}
#import "PhuceContext.h"
namespace PhuceContext {
// state (4.8.11.1.1)
bool save() {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
return true;
}
bool restore() {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextRestoreGState(context);
return true;
}
// transformations (4.8.11.1.2)
bool scale(double x, double y) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextScaleCTM(context, x, y);
return true;
}
bool rotate(double angle) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextRotateCTM(context, angle);
return true;
}
bool translate(double x, double y) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, x, y);
return true;
}
bool transform(double m11, double m12, double m21, double m22,
double dx, double dy) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform transform = {
m11, m12, m21, m22, dx, dy
};
CGContextConcatCTM(context, transform);
return true;
}
bool setTransform(double m11, double m12, double m21, double m22,
double dx, double dy) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform transform = CGContextGetCTM(context);
transform.a = m11;
transform.b = m12;
transform.c = m21;
transform.d = m22;
transform.tx = dx;
transform.ty = dy;
return true;
}
// compositing (4.8.11.1.3)
bool setGlobalAlpha(double value) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetAlpha(context, value);
return true;
}
bool setGlobalCompositeOperation(CompositeOp value) {
CGBlendMode mode;
switch (value) {
case kCompositeOpSourceAtop:
mode = kCGBlendModeSourceAtop;
break;
case kCompositeOpSourceIn:
mode = kCGBlendModeSourceIn;
break;
case kCompositeOpSourceOut:
mode = kCGBlendModeSourceOut;
break;
case kCompositeOpSourceOver:
mode = kCGBlendModeNormal;
break;
case kCompositeOpDestinationAtop:
mode = kCGBlendModeDestinationAtop;
break;
case kCompositeOpDestinationIn:
mode = kCGBlendModeDestinationIn;
break;
case kCompositeOpDestinationOut:
mode = kCGBlendModeDestinationOut;
break;
case kCompositeOpDestinationOver:
mode = kCGBlendModeDestinationOver;
break;
case kCompositeOpLighter:
mode = kCGBlendModeLighten;
break;
case kCompositeOpCopy:
mode = kCGBlendModeCopy;
break;
case kCompositeOpXor:
mode = kCGBlendModeXOR;
break;
default:
return true;
}
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(context, mode);
return true;
}
// colors and styles (4.8.11.1.4)
// FIXME: todo
// line styles (4.8.11.1.5)
bool setLineWidth(double value) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, value);
return true;
}
bool setLineCap(LineCap value) {
CGLineCap cap;
switch (value) {
case kLineCapButt:
cap = kCGLineCapButt;
break;
case kLineCapRound:
cap = kCGLineCapRound;
break;
case kLineCapSquare:
cap = kCGLineCapSquare;
break;
default:
return true;
}
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineCap(context, cap);
return true;
}
bool setLineJoin(LineJoin value) {
CGLineJoin join;
switch (value) {
case kLineJoinBevel:
join = kCGLineJoinBevel;
break;
case kLineJoinRound:
join = kCGLineJoinRound;
break;
case kLineJoinMiter:
join = kCGLineJoinMiter;
break;
default:
return true;
}
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineJoin(context, join);
return true;
}
// FIXME: this should default to 10, what does cg default to?
bool setMiterLimit(double value) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetMiterLimit(context, value);
return true;
}
// shadows (4.8.11.1.6)
// FIXME: todo
// rectangles (4.8.11.1.7)
bool clearRect(double x, double y, double w, double h) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(x, y, w, h);
CGContextClearRect(context, rect);
return true;
}
bool fillRect(double x, double y, double w, double h) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(x, y, w, h);
CGContextFillRect(context, rect);
return true;
}
bool strokeRect(double x, double y, double w, double h) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(x, y, w, h);
CGContextStrokeRect(context, rect);
return true;
}
// paths (4.8.11.1.8)
bool beginPath() {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
return true;
}
bool moveTo(double x, double y) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, x, y);
return true;
}
bool closePath() {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClosePath(context);
return true;
}
bool lineTo(double x, double y) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddLineToPoint(context, x, y);
return true;
}
bool quadraticCurveTo(double cpx, double cpy, double x, double y) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddQuadCurveToPoint(context, cpx, cpy, x, y);
return true;
}
bool bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y,
double x, double y) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddCurveToPoint(context, cp1x, cp1y, cp2x, cp2y, x, y);
return true;
}
bool arcTo(double x1, double y1, double x2, double y2, double radius) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddArcToPoint(context, x1, y1, x2, y2, radius);
return true;
}
bool arc(double x, double y, double radius, double startAngle,
double endAngle, bool anticlockwise) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddArc(context, x, y, radius, startAngle, endAngle,
(int)anticlockwise);
return true;
}
bool rect(double x, double y, double w, double h) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(x, y, w, h);
CGContextAddRect(context, rect);
return true;
}
bool fill() {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextFillPath(context);
return true;
}
bool stroke() {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextStrokePath(context);
return true;
}
bool clip() {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClip(context);
return true;
}
bool isPointInPath(double x, double y, bool *result) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint point = CGPointMake(x, y);
*result = CGContextPathContainsPoint(context, point, kCGPathFill);
return true;
}
// focus management (4.8.11.1.9)
// FIXME: todo
// text (4.8.11.1.10)
// FIXME: todo
// images (4.8.11.1.11)
// FIXME: todo
// pixel manipulation (4.8.11.1.12)
// FIXME: todo
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment