Ejemplo n.º 1
0
def scalePatternMatrix(patternTransform):
    # Scale the pattern by the scaling factor when exporting to bits. This is equivalent to
    # scaling base space by the scaling factor.
    patternScaling = Utilities.getScalingFactor()
    if patternScaling != 1.0:
        patternTransform = Quartz.CGAffineTransformConcat(
            patternTransform,
            Quartz.CGAffineTransformMakeScale(patternScaling, patternScaling))

    return patternTransform
    def testFunctions(self):
        tf = Quartz.CGAffineTransformMake(1.5, 2.5, 3.5, 4.5, 5.5, 6.5)
        self.assertIsInstance(tf, Quartz.CGAffineTransform)
        self.assertEqual(tf.a, 1.5)
        self.assertEqual(tf.b, 2.5)
        self.assertEqual(tf.c, 3.5)
        self.assertEqual(tf.d, 4.5)
        self.assertEqual(tf.tx, 5.5)
        self.assertEqual(tf.ty, 6.5)

        tf = Quartz.CGAffineTransformMakeTranslation(2.5, 3.5)
        self.assertIsInstance(tf, Quartz.CGAffineTransform)
        self.assertEqual(tf.a, 1.0)
        self.assertEqual(tf.b, 0.0)
        self.assertEqual(tf.c, 0.0)
        self.assertEqual(tf.d, 1.0)
        self.assertEqual(tf.tx, 2.5)
        self.assertEqual(tf.ty, 3.5)

        tf = Quartz.CGAffineTransformMakeScale(2.5, 3.5)
        self.assertIsInstance(tf, Quartz.CGAffineTransform)
        self.assertEqual(tf.a, 2.5)
        self.assertEqual(tf.b, 0.0)
        self.assertEqual(tf.c, 0.0)
        self.assertEqual(tf.d, 3.5)
        self.assertEqual(tf.tx, 0.0)
        self.assertEqual(tf.ty, 0.0)

        tf = Quartz.CGAffineTransformMakeRotation(3.4)
        self.assertIsInstance(tf, Quartz.CGAffineTransform)

        self.assertResultHasType(Quartz.CGAffineTransformIsIdentity, objc._C_BOOL)
        self.assertTrue(Quartz.CGAffineTransformIsIdentity(tf) is False)
        self.assertTrue(
            Quartz.CGAffineTransformIsIdentity(Quartz.CGAffineTransformIdentity) is True
        )

        tf = Quartz.CGAffineTransformTranslate(tf, 2.5, 3.5)
        self.assertIsInstance(tf, Quartz.CGAffineTransform)

        tf = Quartz.CGAffineTransformScale(tf, 5.5, 9.5)
        self.assertIsInstance(tf, Quartz.CGAffineTransform)

        tf = Quartz.CGAffineTransformRotate(tf, 0.8)
        self.assertIsInstance(tf, Quartz.CGAffineTransform)

        tf = Quartz.CGAffineTransformInvert(tf)
        self.assertIsInstance(tf, Quartz.CGAffineTransform)

        tf2 = Quartz.CGAffineTransformConcat(
            tf, Quartz.CGAffineTransformMake(1.0, 1.0, 1.0, 1.0, 1.0, 1.0)
        )
        self.assertIsInstance(tf2, Quartz.CGAffineTransform)

        self.assertResultHasType(Quartz.CGAffineTransformEqualToTransform, objc._C_BOOL)
        self.assertTrue(Quartz.CGAffineTransformEqualToTransform(tf, tf2) is False)
        self.assertTrue(Quartz.CGAffineTransformEqualToTransform(tf2, tf2) is True)

        pt = Quartz.CGPointApplyAffineTransform((2.5, 3.5), tf)
        self.assertIsInstance(pt, Quartz.CGPoint)

        sz = Quartz.CGSizeApplyAffineTransform((2.5, 3.5), tf)
        self.assertIsInstance(sz, Quartz.CGSize)

        rct = Quartz.CGRectApplyAffineTransform(((2.5, 3.5), (4.5, 5.5)), tf)
        self.assertIsInstance(rct, Quartz.CGRect)
Ejemplo n.º 3
0
def doPatternMatrix(context):
    basePatternMatrix = Quartz.CGAffineTransformMakeScale(20, 20)
    pattern = createRedBlackCheckerBoardPattern(basePatternMatrix)
    if pattern is None:
        print("Couldn't create pattern!")
        return

    # Create the pattern color space. Since the pattern
    # itself has intrinsic color, the 'baseColorSpace' parameter
    # to Quartz.CGColorSpaceCreatePattern must be None.
    patternColorSpace = Quartz.CGColorSpaceCreatePattern(None)

    Quartz.CGContextSetFillColorSpace(context, patternColorSpace)
    del patternColorSpace

    Quartz.CGContextTranslateCTM(context, 40, 40)
    Quartz.CGContextSetPatternPhase(context,
                                    scalePatternPhase(Quartz.CGSize(40, 40)))

    # The pattern has intrinsic color so the color components array
    # passed to Quartz.CGContextSetFillPattern is the alpha value used
    # to composite the pattern cell.

    # Paint the pattern first with alpha = 1.
    color = [1]
    Quartz.CGContextSetFillPattern(context, pattern, color)

    # Rectangle 1.
    Quartz.CGContextFillRect(context, Quartz.CGRectMake(0, 0, 100, 100))

    Quartz.CGContextSaveGState(context)
    if 1:
        # Rectangle 2.
        # Paint the pattern with 65% alpha.
        color = [0.65]
        Quartz.CGContextSetFillPattern(context, pattern, color)
        # Rotate 45 degrees about the point (150, 50).
        Quartz.CGContextTranslateCTM(context, 150.0, 50.0)
        Quartz.CGContextRotateCTM(context, Utilities.DEGREES_TO_RADIANS(45.0))
        Quartz.CGContextTranslateCTM(context, -50.0, -50.0)
        # Rectangle 2. Patterns do not translate, scale or
        # rotate with the CTM. You can see that the pattern
        # tile of this filled rectangle is that of Rectangle
        # 1.
        Quartz.CGContextFillRect(context, Quartz.CGRectMake(0, 0, 100, 100))
        # Release the pattern.
        del pattern
    Quartz.CGContextRestoreGState(context)

    Quartz.CGContextSaveGState(context)
    if 1:
        # Rectangle 3. The pattern is rotated with the object.
        # Rotate 45 degrees about the point 250, 50.
        t = Quartz.CGAffineTransformMakeTranslation(250.0, 50.0)
        t = Quartz.CGAffineTransformRotate(t,
                                           Utilities.DEGREES_TO_RADIANS(45.0))
        # Translate back to -50, -50.
        t = Quartz.CGAffineTransformTranslate(t, -50.0, -50.0)
        Quartz.CGContextConcatCTM(context, t)
        # Make a new pattern that is equivalent to
        # the old pattern but transformed to current user
        # space. The order of transformations is crucial.
        # This ordering is equivalent to using the same pattern
        # matrix as before but transforming base space by t.
        patTransform = Quartz.CGAffineTransformConcat(basePatternMatrix, t)
        pattern = createRedBlackCheckerBoardPattern(patTransform)
        color = [1]
        Quartz.CGContextSetFillPattern(context, pattern, color)
        # Release the pattern.
        del pattern
        Quartz.CGContextFillRect(context, Quartz.CGRectMake(0, 0, 100, 100))
    Quartz.CGContextRestoreGState(context)

    Quartz.CGContextSaveGState(context)
    if 1:
        # Rectangle 4. The pattern is scaled with the object.
        # Translate and scale.
        t = Quartz.CGAffineTransformMakeTranslation(320, 0)
        t = Quartz.CGAffineTransformScale(t, 2, 2)
        Quartz.CGContextConcatCTM(context, t)
        # Make a new pattern that is equivalent to
        # the old pattern but transformed to current user
        # space. The order of transformations is crucial.
        # This ordering is equivalent to using the same pattern
        # matrix as before but transforming base space by t.
        patTransform = Quartz.CGAffineTransformConcat(basePatternMatrix, t)
        pattern = createRedBlackCheckerBoardPattern(patTransform)
        color = [1]
        Quartz.CGContextSetFillPattern(context, pattern, color)
        # Release the pattern.
        del pattern
        Quartz.CGContextFillRect(context, Quartz.CGRectMake(0, 0, 100, 100))
    Quartz.CGContextRestoreGState(context)