1import math, sys 2 3from Quartz import * 4import Quartz 5 6 7def doSimpleRect(context): 8 # Set the fill color to opaque red. 9 CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0) 10 # Set up the rectangle for drawing. 11 ourRect = CGRectMake(20.0, 20.0, 130.0, 100.0) 12 # Draw the filled rectangle. 13 CGContextFillRect(context, ourRect) 14 15def doStrokedRect(context): 16 # Set the stroke color to a light opaque blue. 17 CGContextSetRGBStrokeColor(context, 0.482, 0.62, 0.871, 1.0) 18 # Set up the rectangle for drawing. 19 ourRect = CGRectMake(20.0, 20.0, 130.0, 100.0) 20 # Draw the stroked rectangle with a line width of 3. 21 CGContextStrokeRectWithWidth(context, ourRect, 3.0) 22 23def doStrokedAndFilledRect(context): 24 # Define a rectangle to use for drawing. 25 ourRect = CGRectMake(20.0, 220.0, 130.0, 100.0) 26 27 # ***** Rectangle 1 ***** 28 # Set the fill color to a light opaque blue. 29 CGContextSetRGBFillColor(context, 0.482, 0.62, 0.871, 1.0) 30 # Set the stroke color to an opaque green. 31 CGContextSetRGBStrokeColor(context, 0.404, 0.808, 0.239, 1.0) 32 # Fill the rect. 33 CGContextFillRect(context, ourRect) 34 # ***** Rectangle 2 ***** 35 # Move the rectangle's origin to the right by 200 units. 36 ourRect.origin.x += 200.0 37 # Stroke the rectangle with a line width of 10. 38 CGContextStrokeRectWithWidth(context, ourRect, 10.0) 39 # ***** Rectangle 3 ***** 40 # Move the rectangle's origin to the left by 200 units 41 # and down by 200 units. 42 ourRect.origin.x -= 200.0 43 ourRect.origin.y -= 200.0 44 # Fill then stroke the rect with a line width of 10. 45 CGContextFillRect(context, ourRect) 46 CGContextStrokeRectWithWidth(context, ourRect, 10.0) 47 # ***** Rectangle 4 ***** 48 # Move the rectangle's origin to the right by 200 units. 49 ourRect.origin.x += 200.0 50 # Stroke then fill the rect. 51 CGContextStrokeRectWithWidth(context, ourRect, 10.0) 52 CGContextFillRect(context, ourRect) 53 54def createRectPath(context, rect): 55 # Create a path using the coordinates of the rect passed in. 56 CGContextBeginPath(context) 57 CGContextMoveToPoint(context, rect.origin.x, rect.origin.y) 58 # ***** Segment 1 ***** 59 CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, 60 rect.origin.y) 61 # ***** Segment 2 ***** 62 CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, 63 rect.origin.y + rect.size.height) 64 # ***** Segment 3 ***** 65 CGContextAddLineToPoint(context, rect.origin.x, 66 rect.origin.y + rect.size.height) 67 # ***** Segment 4 is created by closing the path ***** 68 CGContextClosePath(context) 69 70def doPathRects(context): 71 # Define a rectangle to use for drawing. 72 ourRect = CGRectMake(20.0, 20.0, 130.0, 100.0) 73 74 # ***** Rectangle 1 ***** 75 # Create the rect path. 76 createRectPath (context, ourRect) 77 # Set the fill color to a light opaque blue. 78 CGContextSetRGBFillColor(context, 0.482, 0.62, 0.871, 1.0) 79 # Fill the path. 80 CGContextDrawPath (context, kCGPathFill) # Clears the path. 81 # ***** Rectangle 2 ***** 82 # Translate the coordinate system 200 units to the right. 83 CGContextTranslateCTM(context, 200.0, 0.0) 84 # Set the stroke color to an opaque green. 85 CGContextSetRGBStrokeColor(context, 0.404, 0.808, 0.239, 1.0) 86 createRectPath (context, ourRect) 87 # Set the line width to 10 units. 88 CGContextSetLineWidth (context, 10.0) 89 # Stroke the path. 90 CGContextDrawPath (context, kCGPathStroke) # Clears the path. 91 # ***** Rectangle 3 ***** 92 # Translate the coordinate system 93 # 200 units to the left and 200 units down. 94 CGContextTranslateCTM(context, -200.0, -200.0) 95 createRectPath (context, ourRect) 96 #CGContextSetLineWidth(context, 10.0) # This is redundant. 97 # Fill, then stroke the path. 98 CGContextDrawPath (context, kCGPathFillStroke) # Clears the path. 99 # ***** Rectangle 4 ***** 100 # Translate the coordinate system 200 units to the right. 101 CGContextTranslateCTM(context, 200.0, 0.0) 102 createRectPath (context, ourRect) 103 # Stroke the path. 104 CGContextDrawPath (context, kCGPathStroke) # Clears the path. 105 # Create the path again. 106 createRectPath (context, ourRect) 107 # Fill the path. 108 CGContextDrawPath (context, kCGPathFill) # Clears the path. 109 110def doAlphaRects(context): 111 # ***** Part 1 ***** 112 ourRect = CGRectMake(0.0, 0.0, 130.0, 100.0) 113 numRects = 6 114 rotateAngle = 2*math.pi/numRects 115 tintAdjust = 1.0/numRects 116 117 # ***** Part 2 ***** 118 CGContextTranslateCTM(context, 2*ourRect.size.width, 119 2*ourRect.size.height) 120 121 # ***** Part 3 ***** 122 tint = 1.0 123 for i in range(numRects): 124 CGContextSetRGBFillColor (context, tint, 0.0, 0.0, tint) 125 CGContextFillRect(context, ourRect) 126 # These transformations are cummulative. 127 CGContextRotateCTM(context, rotateAngle) 128 tint -= tintAdjust 129 130def drawStrokedLine(context, start, end): 131 CGContextBeginPath(context) 132 CGContextMoveToPoint(context, start.x, start.y) 133 CGContextAddLineToPoint(context, end.x, end.y) 134 CGContextDrawPath(context, kCGPathStroke) 135 136def doDashedLines(context): 137 lengths = ( 12.0, 6.0, 5.0, 6.0, 5.0, 6.0 ) 138 139 start = CGPoint(20.0, 270.0) 140 end = CGPoint(300.0, 270.0) 141 # ***** Line 1 solid line ***** 142 CGContextSetLineWidth(context, 5.0) 143 drawStrokedLine(context, start, end) 144 # ***** Line 2 long dashes ***** 145 CGContextTranslateCTM(context, 0.0, -50.0) 146 CGContextSetLineDash(context, 0.0, lengths, 2) 147 drawStrokedLine(context, start, end) 148 # ***** Line 3 long short pattern ***** 149 CGContextTranslateCTM(context, 0.0, -50.0) 150 CGContextSetLineDash(context, 0.0, lengths, 4) 151 drawStrokedLine(context, start, end) 152 # ***** Line 4 long short short pattern ***** 153 CGContextTranslateCTM(context, 0.0, -50.0) 154 CGContextSetLineDash(context, 0.0, lengths, 6) 155 drawStrokedLine(context, start, end) 156 # ***** Line 5 short short long pattern ***** 157 CGContextTranslateCTM(context, 0.0, -50.0) 158 CGContextSetLineDash(context, lengths[0]+lengths[1], lengths, 6) 159 drawStrokedLine(context, start, end) 160 # ***** Line 6 solid line ***** 161 CGContextTranslateCTM(context, 0.0, -50.0) 162 # Reset dash to solid line. 163 CGContextSetLineDash(context, 0, None, 0) 164 drawStrokedLine(context, start, end) 165 166def doClippedCircle(context): 167 circleCenter = CGPoint(150.0, 150.0) 168 circleRadius = 100.0 169 startingAngle = 0.0 170 endingAngle = 2*math.pi 171 ourRect = CGRectMake(65.0, 65.0, 170.0, 170.0) 172 173 # ***** Filled Circle ***** 174 CGContextSetRGBFillColor(context, 0.663, 0., 0.031, 1.0) 175 CGContextBeginPath(context) 176 # Construct the circle path counterclockwise. 177 CGContextAddArc(context, circleCenter.x, 178 circleCenter.y, circleRadius, 179 startingAngle, endingAngle, 0) 180 CGContextDrawPath(context, kCGPathFill) 181 182 # ***** Stroked Square ***** 183 CGContextStrokeRect(context, ourRect) 184 185 # Translate so that the next drawing doesn't overlap what 186 # has already been drawn. 187 CGContextTranslateCTM(context, ourRect.size.width + circleRadius + 5.0, 0) 188 # Create a rectangular path and clip to that path. 189 CGContextBeginPath(context) 190 CGContextAddRect(context, ourRect) 191 CGContextClip(context) 192 193 # ***** Clipped Circle ***** 194 CGContextBeginPath(context) 195 # Construct the circle path counterclockwise. 196 CGContextAddArc (context, circleCenter.x, 197 circleCenter.y, circleRadius, 198 startingAngle, endingAngle, 0) 199 CGContextDrawPath(context, kCGPathFill) 200 201 202def doPDFDocument(context, url): 203 204 pdfDoc = CGPDFDocumentCreateWithURL(url) 205 if pdfDoc is not None: 206 CGContextScaleCTM(context, .5, .5) 207 # The media box is the bounding box of the PDF document. 208 pdfRect = CGPDFDocumentGetMediaBox(pdfDoc, 1) # page 1 209 # Set the destination rect origin to the Quartz origin. 210 pdfRect.origin.x = pdfRect.origin.y = 0. 211 # Draw page 1 of the PDF document. 212 CGContextDrawPDFDocument(context, pdfRect, pdfDoc, 1) 213 214 CGContextTranslateCTM(context, pdfRect.size.width*1.2, 0) 215 # Scale non-uniformly making the y coordinate scale 1.5 times 216 # the x coordinate scale. 217 CGContextScaleCTM(context, 1, 1.5) 218 CGContextDrawPDFDocument(context, pdfRect, pdfDoc, 1) 219 220 CGContextTranslateCTM(context, pdfRect.size.width*1.2, pdfRect.size.height) 221 # Flip the y coordinate axis horizontally about the x axis. 222 CGContextScaleCTM(context, 1, -1) 223 CGContextDrawPDFDocument(context, pdfRect, pdfDoc, 1) 224 225 else: 226 print >>sys.stderr, "Can't create PDF document for URL!" 227