changeset 2435:7d6c8b7f41d9

iOS: Implement graphic context pushing and popping during draw.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Tue, 06 Apr 2021 08:24:28 +0000
parents 404d9e9f1f08
children ab36fbb50fe5
files ios/dw.m
diffstat 1 files changed, 44 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/ios/dw.m	Tue Apr 06 04:48:52 2021 +0000
+++ b/ios/dw.m	Tue Apr 06 08:24:28 2021 +0000
@@ -1164,7 +1164,10 @@
     DWBitBlt *bltinfo = (DWBitBlt *)[bi pointerValue];
     id bltdest = bltinfo->dest;
     id bltsrc = bltinfo->src;
-    /* TODO: CGContextRef context =*/ _dw_draw_context(bltdest, NO);
+    CGContextRef context = _dw_draw_context(bltdest, NO);
+
+    if(context)
+        UIGraphicsPushContext(context);
 
     if(bltdest && [bltsrc isMemberOfClass:[UIImage class]])
     {
@@ -1187,6 +1190,8 @@
         [bltsrc release];
         [image release];
     }
+    if(context)
+        UIGraphicsPopContext();
     free(bltinfo);
 }
 -(void)doFlush:(id)param
@@ -1509,7 +1514,7 @@
     if(self)
     {
         CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
-        CGContextRef cgcontext = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, rgb, kCGImageAlphaLast);
+        CGContextRef cgcontext = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, rgb, kCGImageAlphaPremultipliedFirst);
         CGImageRef cgimage = CGBitmapContextCreateImage(cgcontext);
         image = [UIImage imageWithCGImage:cgimage];
         CGContextRelease(cgcontext);
@@ -1570,7 +1575,7 @@
         CGSize size = [image size];
         CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
 
-        cgcontext = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, rgb, kCGImageAlphaLast);
+        cgcontext = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, rgb, kCGImageAlphaPremultipliedFirst);
         CGContextDrawImage(cgcontext, CGRectMake(0,0,size.width,size.height), [image CGImage]);
     }
     return cgcontext;
@@ -5259,7 +5264,10 @@
 {
     DW_FUNCTION_INIT;
     DWImage *bi = _dw_dest_image(pixmap, handle);
-    /*CGContextRef context =*/ _dw_draw_context(bi, NO);
+    CGContextRef context = _dw_draw_context(bi, NO);
+    
+    if(context)
+        UIGraphicsPushContext(context);
 
     if(bi)
     {
@@ -5271,6 +5279,8 @@
         [aPath moveToPoint:CGPointMake(x, y)];
         [aPath stroke];
     }
+    if(context)
+        UIGraphicsPopContext();
     DW_FUNCTION_RETURN_NOTHING;
 }
 
@@ -5290,7 +5300,10 @@
 {
     DW_FUNCTION_INIT;
     DWImage *bi = _dw_dest_image(pixmap, handle);
-    /*CGContextRef context =*/ _dw_draw_context(bi, NO);
+    CGContextRef context = _dw_draw_context(bi, NO);
+
+    if(context)
+        UIGraphicsPushContext(context);
 
     if(bi)
     {
@@ -5302,6 +5315,8 @@
         [aPath addLineToPoint:CGPointMake(x2 + 0.5, y2 + 0.5)];
         [aPath stroke];
     }
+    if(context)
+        UIGraphicsPopContext();
     DW_FUNCTION_RETURN_NOTHING;
 }
 
@@ -5343,7 +5358,10 @@
     }
     if(bi)
     {
-        /*CGContextRef context =*/ _dw_draw_context(bi, NO);
+        CGContextRef context = _dw_draw_context(bi, NO);
+
+        if(context)
+            UIGraphicsPushContext(context);
 
         UIColor *fgcolor = pthread_getspecific(_dw_fg_color_key);
         UIColor *bgcolor = pthread_getspecific(_dw_bg_color_key);
@@ -5354,6 +5372,8 @@
             [dict setValue:font forKey:NSFontAttributeName];
         [nstr drawAtPoint:CGPointMake(x, y) withAttributes:dict];
         [dict release];
+        if(context)
+            UIGraphicsPopContext();
     }
     DW_FUNCTION_RETURN_NOTHING;
 }
@@ -5425,7 +5445,10 @@
 {
     DW_FUNCTION_INIT;
     DWImage *bi = _dw_dest_image(pixmap, handle);
-    /*CGContextRef context =*/ _dw_draw_context(bi, flags & DW_DRAW_NOAA ? NO : YES);
+    CGContextRef context = _dw_draw_context(bi, flags & DW_DRAW_NOAA ? NO : YES);
+
+    if(context)
+        UIGraphicsPushContext(context);
 
     if(bi)
     {
@@ -5446,6 +5469,8 @@
         else
             [aPath stroke];
     }
+    if(context)
+        UIGraphicsPopContext();
     DW_FUNCTION_RETURN_NOTHING;
 }
 
@@ -5466,7 +5491,10 @@
 {
     DW_FUNCTION_INIT;
     DWImage *bi = _dw_dest_image(pixmap, handle);
-    /*CGContextRef context =*/ _dw_draw_context(bi, flags & DW_DRAW_NOAA ? NO : YES);
+    CGContextRef context = _dw_draw_context(bi, flags & DW_DRAW_NOAA ? NO : YES);
+
+    if(context)
+        UIGraphicsPushContext(context);
 
     if(bi)
     {
@@ -5480,6 +5508,8 @@
         else
             [bp stroke];
     }
+    if(context)
+        UIGraphicsPopContext();
     DW_FUNCTION_RETURN_NOTHING;
 }
 
@@ -5503,7 +5533,10 @@
 {
     DW_FUNCTION_INIT;
     DWImage *bi = _dw_dest_image(pixmap, handle);
-    /*CGContextRef context =*/ _dw_draw_context(bi, flags & DW_DRAW_NOAA ? NO : YES);
+    CGContextRef context = _dw_draw_context(bi, flags & DW_DRAW_NOAA ? NO : YES);
+
+    if(context)
+        UIGraphicsPushContext(context);
 
     if(bi)
     {
@@ -5536,6 +5569,8 @@
         else
             [aPath stroke];
     }
+    if(context)
+        UIGraphicsPopContext();
     DW_FUNCTION_RETURN_NOTHING;
 }