comparison ios/dw.m @ 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
comparison
equal deleted inserted replaced
2434:404d9e9f1f08 2435:7d6c8b7f41d9
1162 { 1162 {
1163 NSValue *bi = (NSValue *)param; 1163 NSValue *bi = (NSValue *)param;
1164 DWBitBlt *bltinfo = (DWBitBlt *)[bi pointerValue]; 1164 DWBitBlt *bltinfo = (DWBitBlt *)[bi pointerValue];
1165 id bltdest = bltinfo->dest; 1165 id bltdest = bltinfo->dest;
1166 id bltsrc = bltinfo->src; 1166 id bltsrc = bltinfo->src;
1167 /* TODO: CGContextRef context =*/ _dw_draw_context(bltdest, NO); 1167 CGContextRef context = _dw_draw_context(bltdest, NO);
1168
1169 if(context)
1170 UIGraphicsPushContext(context);
1168 1171
1169 if(bltdest && [bltsrc isMemberOfClass:[UIImage class]]) 1172 if(bltdest && [bltsrc isMemberOfClass:[UIImage class]])
1170 { 1173 {
1171 UIImage *rep = bltsrc; 1174 UIImage *rep = bltsrc;
1172 UIImage *image = [[UIImage alloc] initWithCGImage:[rep CGImage]]; 1175 UIImage *image = [[UIImage alloc] initWithCGImage:[rep CGImage]];
1185 blendMode:op alpha:1.0]; 1188 blendMode:op alpha:1.0];
1186 } 1189 }
1187 [bltsrc release]; 1190 [bltsrc release];
1188 [image release]; 1191 [image release];
1189 } 1192 }
1193 if(context)
1194 UIGraphicsPopContext();
1190 free(bltinfo); 1195 free(bltinfo);
1191 } 1196 }
1192 -(void)doFlush:(id)param 1197 -(void)doFlush:(id)param
1193 { 1198 {
1194 NSEnumerator *enumerator = [_DWDirtyDrawables objectEnumerator]; 1199 NSEnumerator *enumerator = [_DWDirtyDrawables objectEnumerator];
1507 { 1512 {
1508 self = [super init]; 1513 self = [super init];
1509 if(self) 1514 if(self)
1510 { 1515 {
1511 CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB(); 1516 CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
1512 CGContextRef cgcontext = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, rgb, kCGImageAlphaLast); 1517 CGContextRef cgcontext = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, rgb, kCGImageAlphaPremultipliedFirst);
1513 CGImageRef cgimage = CGBitmapContextCreateImage(cgcontext); 1518 CGImageRef cgimage = CGBitmapContextCreateImage(cgcontext);
1514 image = [UIImage imageWithCGImage:cgimage]; 1519 image = [UIImage imageWithCGImage:cgimage];
1515 CGContextRelease(cgcontext); 1520 CGContextRelease(cgcontext);
1516 } 1521 }
1517 return self; 1522 return self;
1568 if(!cgcontext) 1573 if(!cgcontext)
1569 { 1574 {
1570 CGSize size = [image size]; 1575 CGSize size = [image size];
1571 CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB(); 1576 CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
1572 1577
1573 cgcontext = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, rgb, kCGImageAlphaLast); 1578 cgcontext = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, rgb, kCGImageAlphaPremultipliedFirst);
1574 CGContextDrawImage(cgcontext, CGRectMake(0,0,size.width,size.height), [image CGImage]); 1579 CGContextDrawImage(cgcontext, CGRectMake(0,0,size.width,size.height), [image CGImage]);
1575 } 1580 }
1576 return cgcontext; 1581 return cgcontext;
1577 } 1582 }
1578 -(void)dealloc { if(cgcontext) CGContextRelease(cgcontext); if(image) [image release]; [super dealloc]; } 1583 -(void)dealloc { if(cgcontext) CGContextRelease(cgcontext); if(image) [image release]; [super dealloc]; }
5257 DW_FUNCTION_NO_RETURN(dw_draw_point) 5262 DW_FUNCTION_NO_RETURN(dw_draw_point)
5258 DW_FUNCTION_RESTORE_PARAM4(handle, HWND, pixmap, HPIXMAP, x, int, y, int) 5263 DW_FUNCTION_RESTORE_PARAM4(handle, HWND, pixmap, HPIXMAP, x, int, y, int)
5259 { 5264 {
5260 DW_FUNCTION_INIT; 5265 DW_FUNCTION_INIT;
5261 DWImage *bi = _dw_dest_image(pixmap, handle); 5266 DWImage *bi = _dw_dest_image(pixmap, handle);
5262 /*CGContextRef context =*/ _dw_draw_context(bi, NO); 5267 CGContextRef context = _dw_draw_context(bi, NO);
5268
5269 if(context)
5270 UIGraphicsPushContext(context);
5263 5271
5264 if(bi) 5272 if(bi)
5265 { 5273 {
5266 UIBezierPath* aPath = [UIBezierPath bezierPath]; 5274 UIBezierPath* aPath = [UIBezierPath bezierPath];
5267 [aPath setLineWidth: 0.5]; 5275 [aPath setLineWidth: 0.5];
5269 [color set]; 5277 [color set];
5270 5278
5271 [aPath moveToPoint:CGPointMake(x, y)]; 5279 [aPath moveToPoint:CGPointMake(x, y)];
5272 [aPath stroke]; 5280 [aPath stroke];
5273 } 5281 }
5282 if(context)
5283 UIGraphicsPopContext();
5274 DW_FUNCTION_RETURN_NOTHING; 5284 DW_FUNCTION_RETURN_NOTHING;
5275 } 5285 }
5276 5286
5277 /* Draw a line on a window (preferably a render window). 5287 /* Draw a line on a window (preferably a render window).
5278 * Parameters: 5288 * Parameters:
5288 DW_FUNCTION_NO_RETURN(dw_draw_line) 5298 DW_FUNCTION_NO_RETURN(dw_draw_line)
5289 DW_FUNCTION_RESTORE_PARAM6(handle, HWND, pixmap, HPIXMAP, x1, int, y1, int, x2, int, y2, int) 5299 DW_FUNCTION_RESTORE_PARAM6(handle, HWND, pixmap, HPIXMAP, x1, int, y1, int, x2, int, y2, int)
5290 { 5300 {
5291 DW_FUNCTION_INIT; 5301 DW_FUNCTION_INIT;
5292 DWImage *bi = _dw_dest_image(pixmap, handle); 5302 DWImage *bi = _dw_dest_image(pixmap, handle);
5293 /*CGContextRef context =*/ _dw_draw_context(bi, NO); 5303 CGContextRef context = _dw_draw_context(bi, NO);
5304
5305 if(context)
5306 UIGraphicsPushContext(context);
5294 5307
5295 if(bi) 5308 if(bi)
5296 { 5309 {
5297 UIBezierPath* aPath = [UIBezierPath bezierPath]; 5310 UIBezierPath* aPath = [UIBezierPath bezierPath];
5298 UIColor *color = pthread_getspecific(_dw_fg_color_key); 5311 UIColor *color = pthread_getspecific(_dw_fg_color_key);
5300 5313
5301 [aPath moveToPoint:CGPointMake(x1 + 0.5, y1 + 0.5)]; 5314 [aPath moveToPoint:CGPointMake(x1 + 0.5, y1 + 0.5)];
5302 [aPath addLineToPoint:CGPointMake(x2 + 0.5, y2 + 0.5)]; 5315 [aPath addLineToPoint:CGPointMake(x2 + 0.5, y2 + 0.5)];
5303 [aPath stroke]; 5316 [aPath stroke];
5304 } 5317 }
5318 if(context)
5319 UIGraphicsPopContext();
5305 DW_FUNCTION_RETURN_NOTHING; 5320 DW_FUNCTION_RETURN_NOTHING;
5306 } 5321 }
5307 5322
5308 /* Draw text on a window (preferably a render window). 5323 /* Draw text on a window (preferably a render window).
5309 * Parameters: 5324 * Parameters:
5341 font = [render font]; 5356 font = [render font];
5342 bi = [render cachedImage]; 5357 bi = [render cachedImage];
5343 } 5358 }
5344 if(bi) 5359 if(bi)
5345 { 5360 {
5346 /*CGContextRef context =*/ _dw_draw_context(bi, NO); 5361 CGContextRef context = _dw_draw_context(bi, NO);
5362
5363 if(context)
5364 UIGraphicsPushContext(context);
5347 5365
5348 UIColor *fgcolor = pthread_getspecific(_dw_fg_color_key); 5366 UIColor *fgcolor = pthread_getspecific(_dw_fg_color_key);
5349 UIColor *bgcolor = pthread_getspecific(_dw_bg_color_key); 5367 UIColor *bgcolor = pthread_getspecific(_dw_bg_color_key);
5350 NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:fgcolor, NSForegroundColorAttributeName, nil]; 5368 NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:fgcolor, NSForegroundColorAttributeName, nil];
5351 if(bgcolor) 5369 if(bgcolor)
5352 [dict setValue:bgcolor forKey:NSBackgroundColorAttributeName]; 5370 [dict setValue:bgcolor forKey:NSBackgroundColorAttributeName];
5353 if(font) 5371 if(font)
5354 [dict setValue:font forKey:NSFontAttributeName]; 5372 [dict setValue:font forKey:NSFontAttributeName];
5355 [nstr drawAtPoint:CGPointMake(x, y) withAttributes:dict]; 5373 [nstr drawAtPoint:CGPointMake(x, y) withAttributes:dict];
5356 [dict release]; 5374 [dict release];
5375 if(context)
5376 UIGraphicsPopContext();
5357 } 5377 }
5358 DW_FUNCTION_RETURN_NOTHING; 5378 DW_FUNCTION_RETURN_NOTHING;
5359 } 5379 }
5360 5380
5361 /* Query the width and height of a text string. 5381 /* Query the width and height of a text string.
5423 DW_FUNCTION_NO_RETURN(dw_draw_polygon) 5443 DW_FUNCTION_NO_RETURN(dw_draw_polygon)
5424 DW_FUNCTION_RESTORE_PARAM6(handle, HWND, pixmap, HPIXMAP, flags, int, npoints, int, x, int *, y, int *) 5444 DW_FUNCTION_RESTORE_PARAM6(handle, HWND, pixmap, HPIXMAP, flags, int, npoints, int, x, int *, y, int *)
5425 { 5445 {
5426 DW_FUNCTION_INIT; 5446 DW_FUNCTION_INIT;
5427 DWImage *bi = _dw_dest_image(pixmap, handle); 5447 DWImage *bi = _dw_dest_image(pixmap, handle);
5428 /*CGContextRef context =*/ _dw_draw_context(bi, flags & DW_DRAW_NOAA ? NO : YES); 5448 CGContextRef context = _dw_draw_context(bi, flags & DW_DRAW_NOAA ? NO : YES);
5449
5450 if(context)
5451 UIGraphicsPushContext(context);
5429 5452
5430 if(bi) 5453 if(bi)
5431 { 5454 {
5432 UIBezierPath* aPath = [UIBezierPath bezierPath]; 5455 UIBezierPath* aPath = [UIBezierPath bezierPath];
5433 UIColor *color = pthread_getspecific(_dw_fg_color_key); 5456 UIColor *color = pthread_getspecific(_dw_fg_color_key);
5444 if(flags & DW_DRAW_FILL) 5467 if(flags & DW_DRAW_FILL)
5445 [aPath fill]; 5468 [aPath fill];
5446 else 5469 else
5447 [aPath stroke]; 5470 [aPath stroke];
5448 } 5471 }
5472 if(context)
5473 UIGraphicsPopContext();
5449 DW_FUNCTION_RETURN_NOTHING; 5474 DW_FUNCTION_RETURN_NOTHING;
5450 } 5475 }
5451 5476
5452 /* Draw a rectangle on a window (preferably a render window). 5477 /* Draw a rectangle on a window (preferably a render window).
5453 * Parameters: 5478 * Parameters:
5464 DW_FUNCTION_NO_RETURN(dw_draw_rect) 5489 DW_FUNCTION_NO_RETURN(dw_draw_rect)
5465 DW_FUNCTION_RESTORE_PARAM7(handle, HWND, pixmap, HPIXMAP, flags, int, x, int, y, int, width, int, height, int) 5490 DW_FUNCTION_RESTORE_PARAM7(handle, HWND, pixmap, HPIXMAP, flags, int, x, int, y, int, width, int, height, int)
5466 { 5491 {
5467 DW_FUNCTION_INIT; 5492 DW_FUNCTION_INIT;
5468 DWImage *bi = _dw_dest_image(pixmap, handle); 5493 DWImage *bi = _dw_dest_image(pixmap, handle);
5469 /*CGContextRef context =*/ _dw_draw_context(bi, flags & DW_DRAW_NOAA ? NO : YES); 5494 CGContextRef context = _dw_draw_context(bi, flags & DW_DRAW_NOAA ? NO : YES);
5495
5496 if(context)
5497 UIGraphicsPushContext(context);
5470 5498
5471 if(bi) 5499 if(bi)
5472 { 5500 {
5473 UIColor *color = pthread_getspecific(_dw_fg_color_key); 5501 UIColor *color = pthread_getspecific(_dw_fg_color_key);
5474 UIBezierPath *bp = [UIBezierPath bezierPathWithRect:CGRectMake(x, y, width, height)];; 5502 UIBezierPath *bp = [UIBezierPath bezierPathWithRect:CGRectMake(x, y, width, height)];;
5478 if(flags & DW_DRAW_FILL) 5506 if(flags & DW_DRAW_FILL)
5479 [bp fill]; 5507 [bp fill];
5480 else 5508 else
5481 [bp stroke]; 5509 [bp stroke];
5482 } 5510 }
5511 if(context)
5512 UIGraphicsPopContext();
5483 DW_FUNCTION_RETURN_NOTHING; 5513 DW_FUNCTION_RETURN_NOTHING;
5484 } 5514 }
5485 5515
5486 /* Draw an arc on a window (preferably a render window). 5516 /* Draw an arc on a window (preferably a render window).
5487 * Parameters: 5517 * Parameters:
5501 DW_FUNCTION_NO_RETURN(dw_draw_arc) 5531 DW_FUNCTION_NO_RETURN(dw_draw_arc)
5502 DW_FUNCTION_RESTORE_PARAM9(handle, HWND, pixmap, HPIXMAP, flags, int, xorigin, int, yorigin, int, x1, int, y1, int, x2, int, y2, int) 5532 DW_FUNCTION_RESTORE_PARAM9(handle, HWND, pixmap, HPIXMAP, flags, int, xorigin, int, yorigin, int, x1, int, y1, int, x2, int, y2, int)
5503 { 5533 {
5504 DW_FUNCTION_INIT; 5534 DW_FUNCTION_INIT;
5505 DWImage *bi = _dw_dest_image(pixmap, handle); 5535 DWImage *bi = _dw_dest_image(pixmap, handle);
5506 /*CGContextRef context =*/ _dw_draw_context(bi, flags & DW_DRAW_NOAA ? NO : YES); 5536 CGContextRef context = _dw_draw_context(bi, flags & DW_DRAW_NOAA ? NO : YES);
5537
5538 if(context)
5539 UIGraphicsPushContext(context);
5507 5540
5508 if(bi) 5541 if(bi)
5509 { 5542 {
5510 UIBezierPath* aPath; 5543 UIBezierPath* aPath;
5511 UIColor *color = pthread_getspecific(_dw_fg_color_key); 5544 UIColor *color = pthread_getspecific(_dw_fg_color_key);
5534 if(flags & DW_DRAW_FILL) 5567 if(flags & DW_DRAW_FILL)
5535 [aPath fill]; 5568 [aPath fill];
5536 else 5569 else
5537 [aPath stroke]; 5570 [aPath stroke];
5538 } 5571 }
5572 if(context)
5573 UIGraphicsPopContext();
5539 DW_FUNCTION_RETURN_NOTHING; 5574 DW_FUNCTION_RETURN_NOTHING;
5540 } 5575 }
5541 5576
5542 /* 5577 /*
5543 * Create a tree object to be packed. 5578 * Create a tree object to be packed.