2 /* =========================== Module _CG =========================== */
9 #include "pywintoolbox.h"
12 #include "pymactoolbox.h"
15 /* Macro to test whether a weak-loaded CFM function exists */
16 #define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL ) {\
17 PyErr_SetString(PyExc_NotImplementedError, \
18 "Not available in this shared library/OS version"); \
23 #ifdef WITHOUT_FRAMEWORKS
24 #include <Quickdraw.h>
25 #include <CGContext.h>
27 #include <ApplicationServices/ApplicationServices.h>
30 #if !TARGET_API_MAC_OSX
31 /* This code is adapted from the CallMachOFramework demo at:
32 http://developer.apple.com/samplecode/Sample_Code/Runtime_Architecture/CallMachOFramework.htm
33 It allows us to call Mach-O functions from CFM apps. */
36 #include "CFMLateImport.h"
38 static OSStatus
LoadFrameworkBundle(CFStringRef framework
, CFBundleRef
*bundlePtr
)
39 // This routine finds a the named framework and creates a CFBundle
40 // object for it. It looks for the framework in the frameworks folder,
41 // as defined by the Folder Manager. Currently this is
42 // "/System/Library/Frameworks", but we recommend that you avoid hard coded
43 // paths to ensure future compatibility.
45 // You might think that you could use CFBundleGetBundleWithIdentifier but
46 // that only finds bundles that are already loaded into your context.
47 // That would work in the case of the System framework but it wouldn't
48 // work if you're using some other, less-obvious, framework.
51 FSRef frameworksFolderRef
;
60 // Find the frameworks folder and create a URL for it.
62 err
= FSFindFolder(kOnAppropriateDisk
, kFrameworksFolderType
, true, &frameworksFolderRef
);
64 baseURL
= CFURLCreateFromFSRef(kCFAllocatorSystemDefault
, &frameworksFolderRef
);
66 err
= coreFoundationUnknownErr
;
70 // Append the name of the framework to the URL.
73 bundleURL
= CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault
, baseURL
, framework
, false);
74 if (bundleURL
== nil
) {
75 err
= coreFoundationUnknownErr
;
79 // Create a bundle based on that URL and load the bundle into memory.
80 // We never unload the bundle, which is reasonable in this case because
81 // the sample assumes that you'll be calling functions from this
82 // framework throughout the life of your application.
85 *bundlePtr
= CFBundleCreate(kCFAllocatorSystemDefault
, bundleURL
);
86 if (*bundlePtr
== nil
) {
87 err
= coreFoundationUnknownErr
;
91 if ( ! CFBundleLoadExecutable( *bundlePtr
) ) {
92 err
= coreFoundationUnknownErr
;
98 if (err
!= noErr
&& *bundlePtr
!= nil
) {
99 CFRelease(*bundlePtr
);
102 if (bundleURL
!= nil
) {
103 CFRelease(bundleURL
);
105 if (baseURL
!= nil
) {
114 // The CFMLateImport approach requires that you define a fragment
115 // initialisation routine that latches the fragment's connection
116 // ID and locator. If your code already has a fragment initialiser
117 // you will have to integrate the following into it.
119 static CFragConnectionID gFragToFixConnID
;
120 static FSSpec gFragToFixFile
;
121 static CFragSystem7DiskFlatLocator gFragToFixLocator
;
123 extern OSErr
FragmentInit(const CFragInitBlock
*initBlock
);
124 extern OSErr
FragmentInit(const CFragInitBlock
*initBlock
)
126 __initialize(initBlock
); /* call the "original" initializer */
127 gFragToFixConnID
= (CFragConnectionID
) initBlock
->closureID
;
128 gFragToFixFile
= *(initBlock
->fragLocator
.u
.onDisk
.fileSpec
);
129 gFragToFixLocator
= initBlock
->fragLocator
.u
.onDisk
;
130 gFragToFixLocator
.fileSpec
= &gFragToFixFile
;
137 extern int GrafObj_Convert(PyObject
*, GrafPtr
*);
143 PyObject
*CGPoint_New(CGPoint
*itself
)
146 return Py_BuildValue("(ff)",
152 CGPoint_Convert(PyObject
*v
, CGPoint
*p_itself
)
154 if( !PyArg_Parse(v
, "(ff)",
161 PyObject
*CGRect_New(CGRect
*itself
)
164 return Py_BuildValue("(ffff)",
168 itself
->size
.height
);
172 CGRect_Convert(PyObject
*v
, CGRect
*p_itself
)
174 if( !PyArg_Parse(v
, "(ffff)",
177 &p_itself
->size
.width
,
178 &p_itself
->size
.height
) )
183 PyObject
*CGAffineTransform_New(CGAffineTransform
*itself
)
186 return Py_BuildValue("(ffffff)",
196 CGAffineTransform_Convert(PyObject
*v
, CGAffineTransform
*p_itself
)
198 if( !PyArg_Parse(v
, "(ffffff)",
209 static PyObject
*CG_Error
;
211 /* -------------------- Object type CGContextRef -------------------- */
213 PyTypeObject CGContextRef_Type
;
215 #define CGContextRefObj_Check(x) ((x)->ob_type == &CGContextRef_Type || PyObject_TypeCheck((x), &CGContextRef_Type))
217 typedef struct CGContextRefObject
{
219 CGContextRef ob_itself
;
220 } CGContextRefObject
;
222 PyObject
*CGContextRefObj_New(CGContextRef itself
)
224 CGContextRefObject
*it
;
225 it
= PyObject_NEW(CGContextRefObject
, &CGContextRef_Type
);
226 if (it
== NULL
) return NULL
;
227 it
->ob_itself
= itself
;
228 return (PyObject
*)it
;
230 int CGContextRefObj_Convert(PyObject
*v
, CGContextRef
*p_itself
)
232 if (!CGContextRefObj_Check(v
))
234 PyErr_SetString(PyExc_TypeError
, "CGContextRef required");
237 *p_itself
= ((CGContextRefObject
*)v
)->ob_itself
;
241 static void CGContextRefObj_dealloc(CGContextRefObject
*self
)
243 CGContextRelease(self
->ob_itself
);
244 self
->ob_type
->tp_free((PyObject
*)self
);
247 static PyObject
*CGContextRefObj_CGContextSaveGState(CGContextRefObject
*_self
, PyObject
*_args
)
249 PyObject
*_res
= NULL
;
250 if (!PyArg_ParseTuple(_args
, ""))
252 CGContextSaveGState(_self
->ob_itself
);
258 static PyObject
*CGContextRefObj_CGContextRestoreGState(CGContextRefObject
*_self
, PyObject
*_args
)
260 PyObject
*_res
= NULL
;
261 if (!PyArg_ParseTuple(_args
, ""))
263 CGContextRestoreGState(_self
->ob_itself
);
269 static PyObject
*CGContextRefObj_CGContextScaleCTM(CGContextRefObject
*_self
, PyObject
*_args
)
271 PyObject
*_res
= NULL
;
274 if (!PyArg_ParseTuple(_args
, "ff",
278 CGContextScaleCTM(_self
->ob_itself
,
286 static PyObject
*CGContextRefObj_CGContextTranslateCTM(CGContextRefObject
*_self
, PyObject
*_args
)
288 PyObject
*_res
= NULL
;
291 if (!PyArg_ParseTuple(_args
, "ff",
295 CGContextTranslateCTM(_self
->ob_itself
,
303 static PyObject
*CGContextRefObj_CGContextRotateCTM(CGContextRefObject
*_self
, PyObject
*_args
)
305 PyObject
*_res
= NULL
;
307 if (!PyArg_ParseTuple(_args
, "f",
310 CGContextRotateCTM(_self
->ob_itself
,
317 static PyObject
*CGContextRefObj_CGContextConcatCTM(CGContextRefObject
*_self
, PyObject
*_args
)
319 PyObject
*_res
= NULL
;
320 CGAffineTransform transform
;
321 if (!PyArg_ParseTuple(_args
, "O&",
322 CGAffineTransform_Convert
, &transform
))
324 CGContextConcatCTM(_self
->ob_itself
,
331 static PyObject
*CGContextRefObj_CGContextGetCTM(CGContextRefObject
*_self
, PyObject
*_args
)
333 PyObject
*_res
= NULL
;
334 CGAffineTransform _rv
;
335 if (!PyArg_ParseTuple(_args
, ""))
337 _rv
= CGContextGetCTM(_self
->ob_itself
);
338 _res
= Py_BuildValue("O&",
339 CGAffineTransform_New
, &_rv
);
343 static PyObject
*CGContextRefObj_CGContextSetLineWidth(CGContextRefObject
*_self
, PyObject
*_args
)
345 PyObject
*_res
= NULL
;
347 if (!PyArg_ParseTuple(_args
, "f",
350 CGContextSetLineWidth(_self
->ob_itself
,
357 static PyObject
*CGContextRefObj_CGContextSetLineCap(CGContextRefObject
*_self
, PyObject
*_args
)
359 PyObject
*_res
= NULL
;
361 if (!PyArg_ParseTuple(_args
, "i",
364 CGContextSetLineCap(_self
->ob_itself
,
371 static PyObject
*CGContextRefObj_CGContextSetLineJoin(CGContextRefObject
*_self
, PyObject
*_args
)
373 PyObject
*_res
= NULL
;
375 if (!PyArg_ParseTuple(_args
, "i",
378 CGContextSetLineJoin(_self
->ob_itself
,
385 static PyObject
*CGContextRefObj_CGContextSetMiterLimit(CGContextRefObject
*_self
, PyObject
*_args
)
387 PyObject
*_res
= NULL
;
389 if (!PyArg_ParseTuple(_args
, "f",
392 CGContextSetMiterLimit(_self
->ob_itself
,
399 static PyObject
*CGContextRefObj_CGContextSetFlatness(CGContextRefObject
*_self
, PyObject
*_args
)
401 PyObject
*_res
= NULL
;
403 if (!PyArg_ParseTuple(_args
, "f",
406 CGContextSetFlatness(_self
->ob_itself
,
413 static PyObject
*CGContextRefObj_CGContextSetAlpha(CGContextRefObject
*_self
, PyObject
*_args
)
415 PyObject
*_res
= NULL
;
417 if (!PyArg_ParseTuple(_args
, "f",
420 CGContextSetAlpha(_self
->ob_itself
,
427 static PyObject
*CGContextRefObj_CGContextBeginPath(CGContextRefObject
*_self
, PyObject
*_args
)
429 PyObject
*_res
= NULL
;
430 if (!PyArg_ParseTuple(_args
, ""))
432 CGContextBeginPath(_self
->ob_itself
);
438 static PyObject
*CGContextRefObj_CGContextMoveToPoint(CGContextRefObject
*_self
, PyObject
*_args
)
440 PyObject
*_res
= NULL
;
443 if (!PyArg_ParseTuple(_args
, "ff",
447 CGContextMoveToPoint(_self
->ob_itself
,
455 static PyObject
*CGContextRefObj_CGContextAddLineToPoint(CGContextRefObject
*_self
, PyObject
*_args
)
457 PyObject
*_res
= NULL
;
460 if (!PyArg_ParseTuple(_args
, "ff",
464 CGContextAddLineToPoint(_self
->ob_itself
,
472 static PyObject
*CGContextRefObj_CGContextAddCurveToPoint(CGContextRefObject
*_self
, PyObject
*_args
)
474 PyObject
*_res
= NULL
;
481 if (!PyArg_ParseTuple(_args
, "ffffff",
489 CGContextAddCurveToPoint(_self
->ob_itself
,
501 static PyObject
*CGContextRefObj_CGContextAddQuadCurveToPoint(CGContextRefObject
*_self
, PyObject
*_args
)
503 PyObject
*_res
= NULL
;
508 if (!PyArg_ParseTuple(_args
, "ffff",
514 CGContextAddQuadCurveToPoint(_self
->ob_itself
,
524 static PyObject
*CGContextRefObj_CGContextClosePath(CGContextRefObject
*_self
, PyObject
*_args
)
526 PyObject
*_res
= NULL
;
527 if (!PyArg_ParseTuple(_args
, ""))
529 CGContextClosePath(_self
->ob_itself
);
535 static PyObject
*CGContextRefObj_CGContextAddRect(CGContextRefObject
*_self
, PyObject
*_args
)
537 PyObject
*_res
= NULL
;
539 if (!PyArg_ParseTuple(_args
, "O&",
540 CGRect_Convert
, &rect
))
542 CGContextAddRect(_self
->ob_itself
,
549 static PyObject
*CGContextRefObj_CGContextAddArc(CGContextRefObject
*_self
, PyObject
*_args
)
551 PyObject
*_res
= NULL
;
558 if (!PyArg_ParseTuple(_args
, "fffffi",
566 CGContextAddArc(_self
->ob_itself
,
578 static PyObject
*CGContextRefObj_CGContextAddArcToPoint(CGContextRefObject
*_self
, PyObject
*_args
)
580 PyObject
*_res
= NULL
;
586 if (!PyArg_ParseTuple(_args
, "fffff",
593 CGContextAddArcToPoint(_self
->ob_itself
,
604 static PyObject
*CGContextRefObj_CGContextIsPathEmpty(CGContextRefObject
*_self
, PyObject
*_args
)
606 PyObject
*_res
= NULL
;
608 if (!PyArg_ParseTuple(_args
, ""))
610 _rv
= CGContextIsPathEmpty(_self
->ob_itself
);
611 _res
= Py_BuildValue("i",
616 static PyObject
*CGContextRefObj_CGContextGetPathCurrentPoint(CGContextRefObject
*_self
, PyObject
*_args
)
618 PyObject
*_res
= NULL
;
620 if (!PyArg_ParseTuple(_args
, ""))
622 _rv
= CGContextGetPathCurrentPoint(_self
->ob_itself
);
623 _res
= Py_BuildValue("O&",
628 static PyObject
*CGContextRefObj_CGContextGetPathBoundingBox(CGContextRefObject
*_self
, PyObject
*_args
)
630 PyObject
*_res
= NULL
;
632 if (!PyArg_ParseTuple(_args
, ""))
634 _rv
= CGContextGetPathBoundingBox(_self
->ob_itself
);
635 _res
= Py_BuildValue("O&",
640 static PyObject
*CGContextRefObj_CGContextDrawPath(CGContextRefObject
*_self
, PyObject
*_args
)
642 PyObject
*_res
= NULL
;
644 if (!PyArg_ParseTuple(_args
, "i",
647 CGContextDrawPath(_self
->ob_itself
,
654 static PyObject
*CGContextRefObj_CGContextFillPath(CGContextRefObject
*_self
, PyObject
*_args
)
656 PyObject
*_res
= NULL
;
657 if (!PyArg_ParseTuple(_args
, ""))
659 CGContextFillPath(_self
->ob_itself
);
665 static PyObject
*CGContextRefObj_CGContextEOFillPath(CGContextRefObject
*_self
, PyObject
*_args
)
667 PyObject
*_res
= NULL
;
668 if (!PyArg_ParseTuple(_args
, ""))
670 CGContextEOFillPath(_self
->ob_itself
);
676 static PyObject
*CGContextRefObj_CGContextStrokePath(CGContextRefObject
*_self
, PyObject
*_args
)
678 PyObject
*_res
= NULL
;
679 if (!PyArg_ParseTuple(_args
, ""))
681 CGContextStrokePath(_self
->ob_itself
);
687 static PyObject
*CGContextRefObj_CGContextFillRect(CGContextRefObject
*_self
, PyObject
*_args
)
689 PyObject
*_res
= NULL
;
691 if (!PyArg_ParseTuple(_args
, "O&",
692 CGRect_Convert
, &rect
))
694 CGContextFillRect(_self
->ob_itself
,
701 static PyObject
*CGContextRefObj_CGContextStrokeRect(CGContextRefObject
*_self
, PyObject
*_args
)
703 PyObject
*_res
= NULL
;
705 if (!PyArg_ParseTuple(_args
, "O&",
706 CGRect_Convert
, &rect
))
708 CGContextStrokeRect(_self
->ob_itself
,
715 static PyObject
*CGContextRefObj_CGContextStrokeRectWithWidth(CGContextRefObject
*_self
, PyObject
*_args
)
717 PyObject
*_res
= NULL
;
720 if (!PyArg_ParseTuple(_args
, "O&f",
721 CGRect_Convert
, &rect
,
724 CGContextStrokeRectWithWidth(_self
->ob_itself
,
732 static PyObject
*CGContextRefObj_CGContextClearRect(CGContextRefObject
*_self
, PyObject
*_args
)
734 PyObject
*_res
= NULL
;
736 if (!PyArg_ParseTuple(_args
, "O&",
737 CGRect_Convert
, &rect
))
739 CGContextClearRect(_self
->ob_itself
,
746 static PyObject
*CGContextRefObj_CGContextClip(CGContextRefObject
*_self
, PyObject
*_args
)
748 PyObject
*_res
= NULL
;
749 if (!PyArg_ParseTuple(_args
, ""))
751 CGContextClip(_self
->ob_itself
);
757 static PyObject
*CGContextRefObj_CGContextEOClip(CGContextRefObject
*_self
, PyObject
*_args
)
759 PyObject
*_res
= NULL
;
760 if (!PyArg_ParseTuple(_args
, ""))
762 CGContextEOClip(_self
->ob_itself
);
768 static PyObject
*CGContextRefObj_CGContextClipToRect(CGContextRefObject
*_self
, PyObject
*_args
)
770 PyObject
*_res
= NULL
;
772 if (!PyArg_ParseTuple(_args
, "O&",
773 CGRect_Convert
, &rect
))
775 CGContextClipToRect(_self
->ob_itself
,
782 static PyObject
*CGContextRefObj_CGContextSetGrayFillColor(CGContextRefObject
*_self
, PyObject
*_args
)
784 PyObject
*_res
= NULL
;
787 if (!PyArg_ParseTuple(_args
, "ff",
791 CGContextSetGrayFillColor(_self
->ob_itself
,
799 static PyObject
*CGContextRefObj_CGContextSetGrayStrokeColor(CGContextRefObject
*_self
, PyObject
*_args
)
801 PyObject
*_res
= NULL
;
804 if (!PyArg_ParseTuple(_args
, "ff",
808 CGContextSetGrayStrokeColor(_self
->ob_itself
,
816 static PyObject
*CGContextRefObj_CGContextSetRGBFillColor(CGContextRefObject
*_self
, PyObject
*_args
)
818 PyObject
*_res
= NULL
;
823 if (!PyArg_ParseTuple(_args
, "ffff",
829 CGContextSetRGBFillColor(_self
->ob_itself
,
839 static PyObject
*CGContextRefObj_CGContextSetRGBStrokeColor(CGContextRefObject
*_self
, PyObject
*_args
)
841 PyObject
*_res
= NULL
;
846 if (!PyArg_ParseTuple(_args
, "ffff",
852 CGContextSetRGBStrokeColor(_self
->ob_itself
,
862 static PyObject
*CGContextRefObj_CGContextSetCMYKFillColor(CGContextRefObject
*_self
, PyObject
*_args
)
864 PyObject
*_res
= NULL
;
870 if (!PyArg_ParseTuple(_args
, "fffff",
877 CGContextSetCMYKFillColor(_self
->ob_itself
,
888 static PyObject
*CGContextRefObj_CGContextSetCMYKStrokeColor(CGContextRefObject
*_self
, PyObject
*_args
)
890 PyObject
*_res
= NULL
;
896 if (!PyArg_ParseTuple(_args
, "fffff",
903 CGContextSetCMYKStrokeColor(_self
->ob_itself
,
914 static PyObject
*CGContextRefObj_CGContextSetCharacterSpacing(CGContextRefObject
*_self
, PyObject
*_args
)
916 PyObject
*_res
= NULL
;
918 if (!PyArg_ParseTuple(_args
, "f",
921 CGContextSetCharacterSpacing(_self
->ob_itself
,
928 static PyObject
*CGContextRefObj_CGContextSetTextPosition(CGContextRefObject
*_self
, PyObject
*_args
)
930 PyObject
*_res
= NULL
;
933 if (!PyArg_ParseTuple(_args
, "ff",
937 CGContextSetTextPosition(_self
->ob_itself
,
945 static PyObject
*CGContextRefObj_CGContextGetTextPosition(CGContextRefObject
*_self
, PyObject
*_args
)
947 PyObject
*_res
= NULL
;
949 if (!PyArg_ParseTuple(_args
, ""))
951 _rv
= CGContextGetTextPosition(_self
->ob_itself
);
952 _res
= Py_BuildValue("O&",
957 static PyObject
*CGContextRefObj_CGContextSetTextMatrix(CGContextRefObject
*_self
, PyObject
*_args
)
959 PyObject
*_res
= NULL
;
960 CGAffineTransform transform
;
961 if (!PyArg_ParseTuple(_args
, "O&",
962 CGAffineTransform_Convert
, &transform
))
964 CGContextSetTextMatrix(_self
->ob_itself
,
971 static PyObject
*CGContextRefObj_CGContextGetTextMatrix(CGContextRefObject
*_self
, PyObject
*_args
)
973 PyObject
*_res
= NULL
;
974 CGAffineTransform _rv
;
975 if (!PyArg_ParseTuple(_args
, ""))
977 _rv
= CGContextGetTextMatrix(_self
->ob_itself
);
978 _res
= Py_BuildValue("O&",
979 CGAffineTransform_New
, &_rv
);
983 static PyObject
*CGContextRefObj_CGContextSetTextDrawingMode(CGContextRefObject
*_self
, PyObject
*_args
)
985 PyObject
*_res
= NULL
;
987 if (!PyArg_ParseTuple(_args
, "i",
990 CGContextSetTextDrawingMode(_self
->ob_itself
,
997 static PyObject
*CGContextRefObj_CGContextSetFontSize(CGContextRefObject
*_self
, PyObject
*_args
)
999 PyObject
*_res
= NULL
;
1001 if (!PyArg_ParseTuple(_args
, "f",
1004 CGContextSetFontSize(_self
->ob_itself
,
1011 static PyObject
*CGContextRefObj_CGContextSelectFont(CGContextRefObject
*_self
, PyObject
*_args
)
1013 PyObject
*_res
= NULL
;
1017 if (!PyArg_ParseTuple(_args
, "sfi",
1022 CGContextSelectFont(_self
->ob_itself
,
1031 static PyObject
*CGContextRefObj_CGContextShowText(CGContextRefObject
*_self
, PyObject
*_args
)
1033 PyObject
*_res
= NULL
;
1034 char *cstring__in__
;
1035 long cstring__len__
;
1036 int cstring__in_len__
;
1037 if (!PyArg_ParseTuple(_args
, "s#",
1038 &cstring__in__
, &cstring__in_len__
))
1040 cstring__len__
= cstring__in_len__
;
1041 CGContextShowText(_self
->ob_itself
,
1042 cstring__in__
, cstring__len__
);
1048 static PyObject
*CGContextRefObj_CGContextShowTextAtPoint(CGContextRefObject
*_self
, PyObject
*_args
)
1050 PyObject
*_res
= NULL
;
1053 char *cstring__in__
;
1054 long cstring__len__
;
1055 int cstring__in_len__
;
1056 if (!PyArg_ParseTuple(_args
, "ffs#",
1059 &cstring__in__
, &cstring__in_len__
))
1061 cstring__len__
= cstring__in_len__
;
1062 CGContextShowTextAtPoint(_self
->ob_itself
,
1065 cstring__in__
, cstring__len__
);
1071 static PyObject
*CGContextRefObj_CGContextEndPage(CGContextRefObject
*_self
, PyObject
*_args
)
1073 PyObject
*_res
= NULL
;
1074 if (!PyArg_ParseTuple(_args
, ""))
1076 CGContextEndPage(_self
->ob_itself
);
1082 static PyObject
*CGContextRefObj_CGContextFlush(CGContextRefObject
*_self
, PyObject
*_args
)
1084 PyObject
*_res
= NULL
;
1085 if (!PyArg_ParseTuple(_args
, ""))
1087 CGContextFlush(_self
->ob_itself
);
1093 static PyObject
*CGContextRefObj_CGContextSynchronize(CGContextRefObject
*_self
, PyObject
*_args
)
1095 PyObject
*_res
= NULL
;
1096 if (!PyArg_ParseTuple(_args
, ""))
1098 CGContextSynchronize(_self
->ob_itself
);
1104 static PyObject
*CGContextRefObj_CGContextSetShouldAntialias(CGContextRefObject
*_self
, PyObject
*_args
)
1106 PyObject
*_res
= NULL
;
1107 int shouldAntialias
;
1108 if (!PyArg_ParseTuple(_args
, "i",
1111 CGContextSetShouldAntialias(_self
->ob_itself
,
1118 static PyObject
*CGContextRefObj_SyncCGContextOriginWithPort(CGContextRefObject
*_self
, PyObject
*_args
)
1120 PyObject
*_res
= NULL
;
1122 if (!PyArg_ParseTuple(_args
, "O&",
1123 GrafObj_Convert
, &port
))
1125 SyncCGContextOriginWithPort(_self
->ob_itself
,
1132 static PyObject
*CGContextRefObj_ClipCGContextToRegion(CGContextRefObject
*_self
, PyObject
*_args
)
1134 PyObject
*_res
= NULL
;
1137 if (!PyArg_ParseTuple(_args
, "O&O&",
1138 PyMac_GetRect
, &portRect
,
1139 ResObj_Convert
, ®ion
))
1141 ClipCGContextToRegion(_self
->ob_itself
,
1149 static PyMethodDef CGContextRefObj_methods
[] = {
1150 {"CGContextSaveGState", (PyCFunction
)CGContextRefObj_CGContextSaveGState
, 1,
1151 PyDoc_STR("() -> None")},
1152 {"CGContextRestoreGState", (PyCFunction
)CGContextRefObj_CGContextRestoreGState
, 1,
1153 PyDoc_STR("() -> None")},
1154 {"CGContextScaleCTM", (PyCFunction
)CGContextRefObj_CGContextScaleCTM
, 1,
1155 PyDoc_STR("(float sx, float sy) -> None")},
1156 {"CGContextTranslateCTM", (PyCFunction
)CGContextRefObj_CGContextTranslateCTM
, 1,
1157 PyDoc_STR("(float tx, float ty) -> None")},
1158 {"CGContextRotateCTM", (PyCFunction
)CGContextRefObj_CGContextRotateCTM
, 1,
1159 PyDoc_STR("(float angle) -> None")},
1160 {"CGContextConcatCTM", (PyCFunction
)CGContextRefObj_CGContextConcatCTM
, 1,
1161 PyDoc_STR("(CGAffineTransform transform) -> None")},
1162 {"CGContextGetCTM", (PyCFunction
)CGContextRefObj_CGContextGetCTM
, 1,
1163 PyDoc_STR("() -> (CGAffineTransform _rv)")},
1164 {"CGContextSetLineWidth", (PyCFunction
)CGContextRefObj_CGContextSetLineWidth
, 1,
1165 PyDoc_STR("(float width) -> None")},
1166 {"CGContextSetLineCap", (PyCFunction
)CGContextRefObj_CGContextSetLineCap
, 1,
1167 PyDoc_STR("(int cap) -> None")},
1168 {"CGContextSetLineJoin", (PyCFunction
)CGContextRefObj_CGContextSetLineJoin
, 1,
1169 PyDoc_STR("(int join) -> None")},
1170 {"CGContextSetMiterLimit", (PyCFunction
)CGContextRefObj_CGContextSetMiterLimit
, 1,
1171 PyDoc_STR("(float limit) -> None")},
1172 {"CGContextSetFlatness", (PyCFunction
)CGContextRefObj_CGContextSetFlatness
, 1,
1173 PyDoc_STR("(float flatness) -> None")},
1174 {"CGContextSetAlpha", (PyCFunction
)CGContextRefObj_CGContextSetAlpha
, 1,
1175 PyDoc_STR("(float alpha) -> None")},
1176 {"CGContextBeginPath", (PyCFunction
)CGContextRefObj_CGContextBeginPath
, 1,
1177 PyDoc_STR("() -> None")},
1178 {"CGContextMoveToPoint", (PyCFunction
)CGContextRefObj_CGContextMoveToPoint
, 1,
1179 PyDoc_STR("(float x, float y) -> None")},
1180 {"CGContextAddLineToPoint", (PyCFunction
)CGContextRefObj_CGContextAddLineToPoint
, 1,
1181 PyDoc_STR("(float x, float y) -> None")},
1182 {"CGContextAddCurveToPoint", (PyCFunction
)CGContextRefObj_CGContextAddCurveToPoint
, 1,
1183 PyDoc_STR("(float cp1x, float cp1y, float cp2x, float cp2y, float x, float y) -> None")},
1184 {"CGContextAddQuadCurveToPoint", (PyCFunction
)CGContextRefObj_CGContextAddQuadCurveToPoint
, 1,
1185 PyDoc_STR("(float cpx, float cpy, float x, float y) -> None")},
1186 {"CGContextClosePath", (PyCFunction
)CGContextRefObj_CGContextClosePath
, 1,
1187 PyDoc_STR("() -> None")},
1188 {"CGContextAddRect", (PyCFunction
)CGContextRefObj_CGContextAddRect
, 1,
1189 PyDoc_STR("(CGRect rect) -> None")},
1190 {"CGContextAddArc", (PyCFunction
)CGContextRefObj_CGContextAddArc
, 1,
1191 PyDoc_STR("(float x, float y, float radius, float startAngle, float endAngle, int clockwise) -> None")},
1192 {"CGContextAddArcToPoint", (PyCFunction
)CGContextRefObj_CGContextAddArcToPoint
, 1,
1193 PyDoc_STR("(float x1, float y1, float x2, float y2, float radius) -> None")},
1194 {"CGContextIsPathEmpty", (PyCFunction
)CGContextRefObj_CGContextIsPathEmpty
, 1,
1195 PyDoc_STR("() -> (int _rv)")},
1196 {"CGContextGetPathCurrentPoint", (PyCFunction
)CGContextRefObj_CGContextGetPathCurrentPoint
, 1,
1197 PyDoc_STR("() -> (CGPoint _rv)")},
1198 {"CGContextGetPathBoundingBox", (PyCFunction
)CGContextRefObj_CGContextGetPathBoundingBox
, 1,
1199 PyDoc_STR("() -> (CGRect _rv)")},
1200 {"CGContextDrawPath", (PyCFunction
)CGContextRefObj_CGContextDrawPath
, 1,
1201 PyDoc_STR("(int mode) -> None")},
1202 {"CGContextFillPath", (PyCFunction
)CGContextRefObj_CGContextFillPath
, 1,
1203 PyDoc_STR("() -> None")},
1204 {"CGContextEOFillPath", (PyCFunction
)CGContextRefObj_CGContextEOFillPath
, 1,
1205 PyDoc_STR("() -> None")},
1206 {"CGContextStrokePath", (PyCFunction
)CGContextRefObj_CGContextStrokePath
, 1,
1207 PyDoc_STR("() -> None")},
1208 {"CGContextFillRect", (PyCFunction
)CGContextRefObj_CGContextFillRect
, 1,
1209 PyDoc_STR("(CGRect rect) -> None")},
1210 {"CGContextStrokeRect", (PyCFunction
)CGContextRefObj_CGContextStrokeRect
, 1,
1211 PyDoc_STR("(CGRect rect) -> None")},
1212 {"CGContextStrokeRectWithWidth", (PyCFunction
)CGContextRefObj_CGContextStrokeRectWithWidth
, 1,
1213 PyDoc_STR("(CGRect rect, float width) -> None")},
1214 {"CGContextClearRect", (PyCFunction
)CGContextRefObj_CGContextClearRect
, 1,
1215 PyDoc_STR("(CGRect rect) -> None")},
1216 {"CGContextClip", (PyCFunction
)CGContextRefObj_CGContextClip
, 1,
1217 PyDoc_STR("() -> None")},
1218 {"CGContextEOClip", (PyCFunction
)CGContextRefObj_CGContextEOClip
, 1,
1219 PyDoc_STR("() -> None")},
1220 {"CGContextClipToRect", (PyCFunction
)CGContextRefObj_CGContextClipToRect
, 1,
1221 PyDoc_STR("(CGRect rect) -> None")},
1222 {"CGContextSetGrayFillColor", (PyCFunction
)CGContextRefObj_CGContextSetGrayFillColor
, 1,
1223 PyDoc_STR("(float gray, float alpha) -> None")},
1224 {"CGContextSetGrayStrokeColor", (PyCFunction
)CGContextRefObj_CGContextSetGrayStrokeColor
, 1,
1225 PyDoc_STR("(float gray, float alpha) -> None")},
1226 {"CGContextSetRGBFillColor", (PyCFunction
)CGContextRefObj_CGContextSetRGBFillColor
, 1,
1227 PyDoc_STR("(float r, float g, float b, float alpha) -> None")},
1228 {"CGContextSetRGBStrokeColor", (PyCFunction
)CGContextRefObj_CGContextSetRGBStrokeColor
, 1,
1229 PyDoc_STR("(float r, float g, float b, float alpha) -> None")},
1230 {"CGContextSetCMYKFillColor", (PyCFunction
)CGContextRefObj_CGContextSetCMYKFillColor
, 1,
1231 PyDoc_STR("(float c, float m, float y, float k, float alpha) -> None")},
1232 {"CGContextSetCMYKStrokeColor", (PyCFunction
)CGContextRefObj_CGContextSetCMYKStrokeColor
, 1,
1233 PyDoc_STR("(float c, float m, float y, float k, float alpha) -> None")},
1234 {"CGContextSetCharacterSpacing", (PyCFunction
)CGContextRefObj_CGContextSetCharacterSpacing
, 1,
1235 PyDoc_STR("(float spacing) -> None")},
1236 {"CGContextSetTextPosition", (PyCFunction
)CGContextRefObj_CGContextSetTextPosition
, 1,
1237 PyDoc_STR("(float x, float y) -> None")},
1238 {"CGContextGetTextPosition", (PyCFunction
)CGContextRefObj_CGContextGetTextPosition
, 1,
1239 PyDoc_STR("() -> (CGPoint _rv)")},
1240 {"CGContextSetTextMatrix", (PyCFunction
)CGContextRefObj_CGContextSetTextMatrix
, 1,
1241 PyDoc_STR("(CGAffineTransform transform) -> None")},
1242 {"CGContextGetTextMatrix", (PyCFunction
)CGContextRefObj_CGContextGetTextMatrix
, 1,
1243 PyDoc_STR("() -> (CGAffineTransform _rv)")},
1244 {"CGContextSetTextDrawingMode", (PyCFunction
)CGContextRefObj_CGContextSetTextDrawingMode
, 1,
1245 PyDoc_STR("(int mode) -> None")},
1246 {"CGContextSetFontSize", (PyCFunction
)CGContextRefObj_CGContextSetFontSize
, 1,
1247 PyDoc_STR("(float size) -> None")},
1248 {"CGContextSelectFont", (PyCFunction
)CGContextRefObj_CGContextSelectFont
, 1,
1249 PyDoc_STR("(char * name, float size, int textEncoding) -> None")},
1250 {"CGContextShowText", (PyCFunction
)CGContextRefObj_CGContextShowText
, 1,
1251 PyDoc_STR("(Buffer cstring) -> None")},
1252 {"CGContextShowTextAtPoint", (PyCFunction
)CGContextRefObj_CGContextShowTextAtPoint
, 1,
1253 PyDoc_STR("(float x, float y, Buffer cstring) -> None")},
1254 {"CGContextEndPage", (PyCFunction
)CGContextRefObj_CGContextEndPage
, 1,
1255 PyDoc_STR("() -> None")},
1256 {"CGContextFlush", (PyCFunction
)CGContextRefObj_CGContextFlush
, 1,
1257 PyDoc_STR("() -> None")},
1258 {"CGContextSynchronize", (PyCFunction
)CGContextRefObj_CGContextSynchronize
, 1,
1259 PyDoc_STR("() -> None")},
1260 {"CGContextSetShouldAntialias", (PyCFunction
)CGContextRefObj_CGContextSetShouldAntialias
, 1,
1261 PyDoc_STR("(int shouldAntialias) -> None")},
1262 {"SyncCGContextOriginWithPort", (PyCFunction
)CGContextRefObj_SyncCGContextOriginWithPort
, 1,
1263 PyDoc_STR("(CGrafPtr port) -> None")},
1264 {"ClipCGContextToRegion", (PyCFunction
)CGContextRefObj_ClipCGContextToRegion
, 1,
1265 PyDoc_STR("(Rect portRect, RgnHandle region) -> None")},
1269 #define CGContextRefObj_getsetlist NULL
1272 #define CGContextRefObj_compare NULL
1274 #define CGContextRefObj_repr NULL
1276 #define CGContextRefObj_hash NULL
1277 #define CGContextRefObj_tp_init 0
1279 #define CGContextRefObj_tp_alloc PyType_GenericAlloc
1281 static PyObject
*CGContextRefObj_tp_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1284 CGContextRef itself
;
1285 char *kw
[] = {"itself", 0};
1287 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "O&", kw
, CGContextRefObj_Convert
, &itself
)) return NULL
;
1288 if ((self
= type
->tp_alloc(type
, 0)) == NULL
) return NULL
;
1289 ((CGContextRefObject
*)self
)->ob_itself
= itself
;
1293 #define CGContextRefObj_tp_free PyObject_Del
1296 PyTypeObject CGContextRef_Type
= {
1297 PyObject_HEAD_INIT(NULL
)
1299 "_CG.CGContextRef", /*tp_name*/
1300 sizeof(CGContextRefObject
), /*tp_basicsize*/
1303 (destructor
) CGContextRefObj_dealloc
, /*tp_dealloc*/
1305 (getattrfunc
)0, /*tp_getattr*/
1306 (setattrfunc
)0, /*tp_setattr*/
1307 (cmpfunc
) CGContextRefObj_compare
, /*tp_compare*/
1308 (reprfunc
) CGContextRefObj_repr
, /*tp_repr*/
1309 (PyNumberMethods
*)0, /* tp_as_number */
1310 (PySequenceMethods
*)0, /* tp_as_sequence */
1311 (PyMappingMethods
*)0, /* tp_as_mapping */
1312 (hashfunc
) CGContextRefObj_hash
, /*tp_hash*/
1315 PyObject_GenericGetAttr
, /*tp_getattro*/
1316 PyObject_GenericSetAttr
, /*tp_setattro */
1318 Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
, /* tp_flags */
1322 0, /*tp_richcompare*/
1323 0, /*tp_weaklistoffset*/
1326 CGContextRefObj_methods
, /* tp_methods */
1328 CGContextRefObj_getsetlist
, /*tp_getset*/
1333 0, /*tp_dictoffset*/
1334 CGContextRefObj_tp_init
, /* tp_init */
1335 CGContextRefObj_tp_alloc
, /* tp_alloc */
1336 CGContextRefObj_tp_new
, /* tp_new */
1337 CGContextRefObj_tp_free
, /* tp_free */
1340 /* ------------------ End object type CGContextRef ------------------ */
1343 static PyObject
*CG_CreateCGContextForPort(PyObject
*_self
, PyObject
*_args
)
1345 PyObject
*_res
= NULL
;
1350 if (!PyArg_ParseTuple(_args
, "O&", GrafObj_Convert
, &port
))
1353 _err
= CreateCGContextForPort(port
, &ctx
);
1355 if (_err
!= noErr
) return PyMac_Error(_err
);
1356 _res
= Py_BuildValue("O&", CGContextRefObj_New
, ctx
);
1361 static PyMethodDef CG_methods
[] = {
1362 {"CreateCGContextForPort", (PyCFunction
)CG_CreateCGContextForPort
, 1,
1363 PyDoc_STR("(CGrafPtr) -> CGContextRef")},
1377 #if !TARGET_API_MAC_OSX
1378 CFBundleRef sysBundle
;
1381 if (&LoadFrameworkBundle
== NULL
) {
1382 PyErr_SetString(PyExc_ImportError
, "CoreCraphics not supported");
1385 err
= LoadFrameworkBundle(CFSTR("ApplicationServices.framework"), &sysBundle
);
1387 err
= CFMLateImportBundle(&gFragToFixLocator
, gFragToFixConnID
, FragmentInit
, "\pCGStubLib", sysBundle
);
1389 PyErr_SetString(PyExc_ImportError
, "CoreCraphics not supported");
1392 #endif /* !TARGET_API_MAC_OSX */
1395 m
= Py_InitModule("_CG", CG_methods
);
1396 d
= PyModule_GetDict(m
);
1397 CG_Error
= PyMac_GetOSErrException();
1398 if (CG_Error
== NULL
||
1399 PyDict_SetItemString(d
, "Error", CG_Error
) != 0)
1401 CGContextRef_Type
.ob_type
= &PyType_Type
;
1402 if (PyType_Ready(&CGContextRef_Type
) < 0) return;
1403 Py_INCREF(&CGContextRef_Type
);
1404 PyModule_AddObject(m
, "CGContextRef", (PyObject
*)&CGContextRef_Type
);
1405 /* Backward-compatible name */
1406 Py_INCREF(&CGContextRef_Type
);
1407 PyModule_AddObject(m
, "CGContextRefType", (PyObject
*)&CGContextRef_Type
);
1410 /* ========================= End module _CG ========================= */