comparison ios/dw.m @ 2434:404d9e9f1f08

iOS: Second attempt at a DWImage class that will actually work.... Doesn't work yet, might need to switch to the Core Graphics functions.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Tue, 06 Apr 2021 04:48:52 +0000
parents 87669cfe3c92
children 7d6c8b7f41d9
comparison
equal deleted inserted replaced
2433:87669cfe3c92 2434:404d9e9f1f08
263 /* Some internal prototypes */ 263 /* Some internal prototypes */
264 static void _dw_do_resize(Box *thisbox, int x, int y); 264 static void _dw_do_resize(Box *thisbox, int x, int y);
265 void _dw_handle_resize_events(Box *thisbox); 265 void _dw_handle_resize_events(Box *thisbox);
266 int _dw_remove_userdata(UserData **root, const char *varname, int all); 266 int _dw_remove_userdata(UserData **root, const char *varname, int all);
267 int _dw_main_iteration(NSDate *date); 267 int _dw_main_iteration(NSDate *date);
268 CGContextRef _dw_draw_context(UIImage *image, bool antialias); 268 CGContextRef _dw_draw_context(id image, bool antialias);
269 typedef id (*DWIMP)(id, SEL, ...); 269 typedef id (*DWIMP)(id, SEL, ...);
270 270
271 /* Internal function to queue a window redraw */ 271 /* Internal function to queue a window redraw */
272 void _dw_redraw(id window, int skip) 272 void _dw_redraw(id window, int skip)
273 { 273 {
791 -(void)setShown:(int)val { shown = val; } 791 -(void)setShown:(int)val { shown = val; }
792 -(void)dealloc { dw_signal_disconnect_by_window(self); [super dealloc]; } 792 -(void)dealloc { dw_signal_disconnect_by_window(self); [super dealloc]; }
793 -(void)layoutSubviews { } 793 -(void)layoutSubviews { }
794 @end 794 @end
795 795
796 @interface DWImage : NSObject
797 {
798 UIImage *image;
799 CGContextRef cgcontext;
800 }
801 -(id)initWithSize:(CGSize)size;
802 -(id)initWithCGImage:(CGImageRef)cgimage;
803 -(id)initWithUIImage:(UIImage *)newimage;
804 -(void)setImage:(UIImage *)input;
805 -(UIImage *)image;
806 -(CGContextRef)cgcontext;
807 -(void)dealloc;
808 @end
809
796 /* Subclass for a render area type */ 810 /* Subclass for a render area type */
797 @interface DWRender : UIControl 811 @interface DWRender : UIControl
798 { 812 {
799 void *userdata; 813 void *userdata;
800 UIFont *font; 814 UIFont *font;
801 CGSize size; 815 CGSize size;
802 UIImage *cachedDrawingRep; 816 DWImage *cachedImage;
803 } 817 }
804 -(void *)userdata; 818 -(void *)userdata;
805 -(void)setUserdata:(void *)input; 819 -(void)setUserdata:(void *)input;
806 -(void)setFont:(UIFont *)input; 820 -(void)setFont:(UIFont *)input;
807 -(UIFont *)font; 821 -(UIFont *)font;
808 -(void)setSize:(CGSize)input; 822 -(void)setSize:(CGSize)input;
809 -(CGSize)size; 823 -(CGSize)size;
810 -(UIImage *)cachedDrawingRep; 824 -(DWImage *)cachedImage;
811 -(void)mouseDown:(UIEvent *)theEvent; 825 -(void)mouseDown:(UIEvent *)theEvent;
812 -(void)mouseUp:(UIEvent *)theEvent; 826 -(void)mouseUp:(UIEvent *)theEvent;
813 -(DWMenu *)menuForEvent:(UIEvent *)theEvent; 827 -(DWMenu *)menuForEvent:(UIEvent *)theEvent;
814 -(void)rightMouseUp:(UIEvent *)theEvent; 828 -(void)rightMouseUp:(UIEvent *)theEvent;
815 -(void)otherMouseDown:(UIEvent *)theEvent; 829 -(void)otherMouseDown:(UIEvent *)theEvent;
824 -(void)setUserdata:(void *)input { userdata = input; } 838 -(void)setUserdata:(void *)input { userdata = input; }
825 -(void)setFont:(UIFont *)input { [font release]; font = input; [font retain]; } 839 -(void)setFont:(UIFont *)input { [font release]; font = input; [font retain]; }
826 -(UIFont *)font { return font; } 840 -(UIFont *)font { return font; }
827 -(void)setSize:(CGSize)input { 841 -(void)setSize:(CGSize)input {
828 size = input; 842 size = input;
829 if(cachedDrawingRep) 843 if(cachedImage)
830 { 844 {
831 UIImage *oldrep = cachedDrawingRep; 845 DWImage *oldimage = cachedImage;
846 UIImage *newimage;
832 UIGraphicsBeginImageContext(self.frame.size); 847 UIGraphicsBeginImageContext(self.frame.size);
833 [[self layer] renderInContext:UIGraphicsGetCurrentContext()]; 848 [[self layer] renderInContext:UIGraphicsGetCurrentContext()];
834 cachedDrawingRep = UIGraphicsGetImageFromCurrentImageContext(); 849 newimage = UIGraphicsGetImageFromCurrentImageContext();
835 UIGraphicsEndImageContext(); 850 UIGraphicsEndImageContext();
836 [cachedDrawingRep retain]; 851 cachedImage = [[DWImage alloc] initWithUIImage:newimage];
837 [oldrep release]; 852 [cachedImage retain];
853 [oldimage release];
838 } 854 }
839 } 855 }
840 -(CGSize)size { return size; } 856 -(CGSize)size { return size; }
841 -(UIImage *)cachedDrawingRep { 857 -(DWImage *)cachedImage {
842 if(!cachedDrawingRep) 858 if(!cachedImage)
843 { 859 {
860 UIImage *newimage;
844 UIGraphicsBeginImageContext(self.frame.size); 861 UIGraphicsBeginImageContext(self.frame.size);
845 [[self layer] renderInContext:UIGraphicsGetCurrentContext()]; 862 [[self layer] renderInContext:UIGraphicsGetCurrentContext()];
846 cachedDrawingRep = UIGraphicsGetImageFromCurrentImageContext(); 863 newimage = UIGraphicsGetImageFromCurrentImageContext();
847 UIGraphicsEndImageContext(); 864 UIGraphicsEndImageContext();
848 [cachedDrawingRep retain]; 865 cachedImage = [[DWImage alloc] initWithUIImage:newimage];
866 [cachedImage retain];
849 } 867 }
850 /* Mark this render dirty if something is requesting it to draw */ 868 /* Mark this render dirty if something is requesting it to draw */
851 if(![_DWDirtyDrawables containsObject:self]) 869 if(![_DWDirtyDrawables containsObject:self])
852 [_DWDirtyDrawables addObject:self]; 870 [_DWDirtyDrawables addObject:self];
853 return cachedDrawingRep; 871 return cachedImage;
854 } 872 }
855 -(void)mouseDown:(UIEvent *)theEvent 873 -(void)mouseDown:(UIEvent *)theEvent
856 { 874 {
857 if(![theEvent isMemberOfClass:[UIEvent class]]) 875 if(![theEvent isMemberOfClass:[UIEvent class]])
858 _dw_event_handler(self, theEvent, 3); 876 _dw_event_handler(self, theEvent, 3);
863 -(void)otherMouseDown:(UIEvent *)theEvent { _dw_event_handler(self, theEvent, 3); } 881 -(void)otherMouseDown:(UIEvent *)theEvent { _dw_event_handler(self, theEvent, 3); }
864 -(void)otherMouseUp:(UIEvent *)theEvent { _dw_event_handler(self, theEvent, 4); } 882 -(void)otherMouseUp:(UIEvent *)theEvent { _dw_event_handler(self, theEvent, 4); }
865 -(void)mouseDragged:(UIEvent *)theEvent { _dw_event_handler(self, theEvent, 5); } 883 -(void)mouseDragged:(UIEvent *)theEvent { _dw_event_handler(self, theEvent, 5); }
866 -(void)drawRect:(CGRect)rect { 884 -(void)drawRect:(CGRect)rect {
867 _dw_event_handler(self, nil, 7); 885 _dw_event_handler(self, nil, 7);
868 if (cachedDrawingRep) 886 if(cachedImage)
869 { 887 {
870 [cachedDrawingRep drawInRect:self.bounds]; 888 [[cachedImage image] drawInRect:self.bounds];
871 [_DWDirtyDrawables removeObject:self]; 889 [_DWDirtyDrawables removeObject:self];
872 [self setNeedsDisplay]; 890 [self setNeedsDisplay];
873 } 891 }
874 } 892 }
875 -(void)keyDown:(UIEvent *)theEvent { _dw_event_handler(self, theEvent, 2); } 893 -(void)keyDown:(UIEvent *)theEvent { _dw_event_handler(self, theEvent, 2); }
877 -(void)dealloc { 895 -(void)dealloc {
878 UserData *root = userdata; 896 UserData *root = userdata;
879 _dw_remove_userdata(&root, NULL, TRUE); 897 _dw_remove_userdata(&root, NULL, TRUE);
880 [font release]; 898 [font release];
881 dw_signal_disconnect_by_window(self); 899 dw_signal_disconnect_by_window(self);
882 [cachedDrawingRep release]; 900 [cachedImage release];
883 [_DWDirtyDrawables removeObject:self]; 901 [_DWDirtyDrawables removeObject:self];
884 [super dealloc]; 902 [super dealloc];
885 } 903 }
886 -(BOOL)acceptsFirstResponder { return YES; } 904 -(BOOL)acceptsFirstResponder { return YES; }
887 @end 905 @end
1144 { 1162 {
1145 NSValue *bi = (NSValue *)param; 1163 NSValue *bi = (NSValue *)param;
1146 DWBitBlt *bltinfo = (DWBitBlt *)[bi pointerValue]; 1164 DWBitBlt *bltinfo = (DWBitBlt *)[bi pointerValue];
1147 id bltdest = bltinfo->dest; 1165 id bltdest = bltinfo->dest;
1148 id bltsrc = bltinfo->src; 1166 id bltsrc = bltinfo->src;
1149 1167 /* TODO: CGContextRef context =*/ _dw_draw_context(bltdest, NO);
1150 if([bltdest isMemberOfClass:[DWRender class]]) 1168
1151 {
1152 DWRender *render = bltdest;
1153
1154 bltdest = [render cachedDrawingRep];
1155 }
1156 if([bltdest isMemberOfClass:[UIImage class]])
1157 {
1158 _dw_draw_context(bltdest, NO);
1159 }
1160 if(bltdest && [bltsrc isMemberOfClass:[UIImage class]]) 1169 if(bltdest && [bltsrc isMemberOfClass:[UIImage class]])
1161 { 1170 {
1162 UIImage *rep = bltsrc; 1171 UIImage *rep = bltsrc;
1163 UIImage *image = [[UIImage alloc] initWithCGImage:[rep CGImage]]; 1172 UIImage *image = [[UIImage alloc] initWithCGImage:[rep CGImage]];
1164 CGBlendMode op = kCGBlendModeNormal; 1173 CGBlendMode op = kCGBlendModeNormal;
1176 blendMode:op alpha:1.0]; 1185 blendMode:op alpha:1.0];
1177 } 1186 }
1178 [bltsrc release]; 1187 [bltsrc release];
1179 [image release]; 1188 [image release];
1180 } 1189 }
1181 if([bltdest isMemberOfClass:[UIImage class]])
1182 {
1183 UIGraphicsEndImageContext();
1184 }
1185 free(bltinfo); 1190 free(bltinfo);
1186 } 1191 }
1187 -(void)doFlush:(id)param 1192 -(void)doFlush:(id)param
1188 { 1193 {
1189 NSEnumerator *enumerator = [_DWDirtyDrawables objectEnumerator]; 1194 NSEnumerator *enumerator = [_DWDirtyDrawables objectEnumerator];
1190 DWRender *rend; 1195 DWRender *rend;
1191 1196
1192 while (rend = [enumerator nextObject]) 1197 while(rend = [enumerator nextObject])
1193 [rend setNeedsDisplay]; 1198 [rend setNeedsDisplay];
1194 [_DWDirtyDrawables removeAllObjects]; 1199 [_DWDirtyDrawables removeAllObjects];
1200 UIGraphicsEndImageContext();
1195 } 1201 }
1196 -(void)doWindowFunc:(id)param 1202 -(void)doWindowFunc:(id)param
1197 { 1203 {
1198 NSValue *v = (NSValue *)param; 1204 NSValue *v = (NSValue *)param;
1199 void **params = (void **)[v pointerValue]; 1205 void **params = (void **)[v pointerValue];
1494 return nil; 1500 return nil;
1495 } 1501 }
1496 -(void)dealloc { [super dealloc]; } 1502 -(void)dealloc { [super dealloc]; }
1497 @end 1503 @end
1498 1504
1499 @interface DWImage : NSObject
1500 {
1501 UIImage *image;
1502 CGImageRef cgimage;
1503 }
1504 -(void)setImage:(UIImage *)input;
1505 -(void)setCGImage:(CGImageRef)input;
1506 -(UIImage *)image;
1507 -(CGImageRef)cgimage;
1508 -(void)dealloc;
1509 @end
1510
1511 @implementation DWImage 1505 @implementation DWImage
1512 -(void)setImage:(UIImage *)input { image = input; } 1506 -(id)initWithSize:(CGSize)size
1513 -(void)setCGImage:(CGImageRef)input { cgimage = input; } 1507 {
1514 -(UIImage *)image { return image; } 1508 self = [super init];
1515 -(CGImageRef)cgimage { return cgimage; } 1509 if(self)
1516 -(void)dealloc { if(cgimage) CGImageRelease(cgimage); if(image) [image release]; [super dealloc]; } 1510 {
1511 CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
1512 CGContextRef cgcontext = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, rgb, kCGImageAlphaLast);
1513 CGImageRef cgimage = CGBitmapContextCreateImage(cgcontext);
1514 image = [UIImage imageWithCGImage:cgimage];
1515 CGContextRelease(cgcontext);
1516 }
1517 return self;
1518 }
1519 -(id)initWithCGImage:(CGImageRef)cgimage
1520 {
1521 self = [super init];
1522 if(self)
1523 {
1524 image = [UIImage imageWithCGImage:cgimage];
1525 [image retain];
1526 }
1527 return self;
1528 }
1529 -(id)initWithUIImage:(UIImage *)newimage
1530 {
1531 self = [super init];
1532 if(self)
1533 {
1534 image = newimage;
1535 [image retain];
1536 }
1537 return self;
1538 }
1539 -(void)setImage:(UIImage *)input
1540 {
1541 UIImage *oldimage = image;
1542 image = input;
1543 [image retain];
1544 [oldimage release];
1545 }
1546 -(UIImage *)image
1547 {
1548 /* If our CGContext has been modified... */
1549 if(cgcontext)
1550 {
1551 UIImage *oldimage = image;
1552 CGImageRef cgimage;
1553
1554 /* Create a new UIImage from the CGContext via CGImage */
1555 cgimage = CGBitmapContextCreateImage(cgcontext);
1556 image = [UIImage imageWithCGImage:cgimage];
1557 CGContextRelease(cgcontext);
1558 cgcontext = nil;
1559 [oldimage release];
1560 }
1561 return image;
1562 }
1563 -(CGContextRef)cgcontext
1564 {
1565 /* If we don't have an active context, create a bitmap
1566 * context and copy the image from our UIImage.
1567 */
1568 if(!cgcontext)
1569 {
1570 CGSize size = [image size];
1571 CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
1572
1573 cgcontext = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, rgb, kCGImageAlphaLast);
1574 CGContextDrawImage(cgcontext, CGRectMake(0,0,size.width,size.height), [image CGImage]);
1575 }
1576 return cgcontext;
1577 }
1578 -(void)dealloc { if(cgcontext) CGContextRelease(cgcontext); if(image) [image release]; [super dealloc]; }
1517 @end 1579 @end
1518 1580
1519 /* Subclass for a scrollbox type */ 1581 /* Subclass for a scrollbox type */
1520 @interface DWScrollBox : UIScrollView 1582 @interface DWScrollBox : UIScrollView
1521 { 1583 {
5150 newcolor = [[params lastObject] unsignedLongValue]; 5212 newcolor = [[params lastObject] unsignedLongValue];
5151 5213
5152 return newcolor; 5214 return newcolor;
5153 } 5215 }
5154 5216
5155 CGContextRef _dw_draw_context(UIImage *image, bool antialiased) 5217 CGContextRef _dw_draw_context(id source, bool antialiased)
5156 { 5218 {
5157 CGContextRef context; 5219 CGContextRef context = nil;
5158 5220
5159 UIGraphicsBeginImageContext(image.size); 5221 if([source isMemberOfClass:[DWImage class]])
5160 context = UIGraphicsGetCurrentContext(); 5222 {
5161 CGContextSetAllowsAntialiasing(context, antialiased); 5223 DWImage *image = source;
5224
5225 context = [image cgcontext];
5226 }
5227 else if([source isMemberOfClass:[DWRender class]])
5228 {
5229 DWRender *render = source;
5230
5231 UIGraphicsBeginImageContext([render frame].size);
5232 context = UIGraphicsGetCurrentContext();
5233 }
5234 if(context)
5235 CGContextSetAllowsAntialiasing(context, antialiased);
5162 return context; 5236 return context;
5237 }
5238
5239 DWImage *_dw_dest_image(HPIXMAP pixmap, id object)
5240 {
5241 if(pixmap && pixmap->image)
5242 return pixmap->image;
5243 if([object isMemberOfClass:[DWRender class]])
5244 return [object cachedImage];
5245 return nil;
5163 } 5246 }
5164 5247
5165 /* Draw a point on a window (preferably a render window). 5248 /* Draw a point on a window (preferably a render window).
5166 * Parameters: 5249 * Parameters:
5167 * handle: Handle to the window. 5250 * handle: Handle to the window.
5173 DW_FUNCTION_ADD_PARAM4(handle, pixmap, x, y) 5256 DW_FUNCTION_ADD_PARAM4(handle, pixmap, x, y)
5174 DW_FUNCTION_NO_RETURN(dw_draw_point) 5257 DW_FUNCTION_NO_RETURN(dw_draw_point)
5175 DW_FUNCTION_RESTORE_PARAM4(handle, HWND, pixmap, HPIXMAP, x, int, y, int) 5258 DW_FUNCTION_RESTORE_PARAM4(handle, HWND, pixmap, HPIXMAP, x, int, y, int)
5176 { 5259 {
5177 DW_FUNCTION_INIT; 5260 DW_FUNCTION_INIT;
5178 DW_LOCAL_POOL_IN; 5261 DWImage *bi = _dw_dest_image(pixmap, handle);
5179 id image = handle; 5262 /*CGContextRef context =*/ _dw_draw_context(bi, NO);
5180 UIImage *bi = nil; 5263
5181 bool bCanDraw = YES;
5182
5183 if(pixmap)
5184 bi = (id)pixmap->image;
5185 else
5186 {
5187 if([image isMemberOfClass:[DWRender class]])
5188 {
5189 DWRender *render = image;
5190
5191 bi = [render cachedDrawingRep];
5192 }
5193 }
5194 if(bi) 5264 if(bi)
5195 {
5196 _dw_draw_context(bi, NO);
5197 }
5198 if(bCanDraw == YES)
5199 { 5265 {
5200 UIBezierPath* aPath = [UIBezierPath bezierPath]; 5266 UIBezierPath* aPath = [UIBezierPath bezierPath];
5201 [aPath setLineWidth: 0.5]; 5267 [aPath setLineWidth: 0.5];
5202 UIColor *color = pthread_getspecific(_dw_fg_color_key); 5268 UIColor *color = pthread_getspecific(_dw_fg_color_key);
5203 [color set]; 5269 [color set];
5204 5270
5205 [aPath moveToPoint:CGPointMake(x, y)]; 5271 [aPath moveToPoint:CGPointMake(x, y)];
5206 [aPath stroke]; 5272 [aPath stroke];
5207 } 5273 }
5208 if(bi)
5209 UIGraphicsEndImageContext();
5210 DW_LOCAL_POOL_OUT;
5211 DW_FUNCTION_RETURN_NOTHING; 5274 DW_FUNCTION_RETURN_NOTHING;
5212 } 5275 }
5213 5276
5214 /* Draw a line on a window (preferably a render window). 5277 /* Draw a line on a window (preferably a render window).
5215 * Parameters: 5278 * Parameters:
5224 DW_FUNCTION_ADD_PARAM6(handle, pixmap, x1, y1, x2, y2) 5287 DW_FUNCTION_ADD_PARAM6(handle, pixmap, x1, y1, x2, y2)
5225 DW_FUNCTION_NO_RETURN(dw_draw_line) 5288 DW_FUNCTION_NO_RETURN(dw_draw_line)
5226 DW_FUNCTION_RESTORE_PARAM6(handle, HWND, pixmap, HPIXMAP, x1, int, y1, int, x2, int, y2, int) 5289 DW_FUNCTION_RESTORE_PARAM6(handle, HWND, pixmap, HPIXMAP, x1, int, y1, int, x2, int, y2, int)
5227 { 5290 {
5228 DW_FUNCTION_INIT; 5291 DW_FUNCTION_INIT;
5229 DW_LOCAL_POOL_IN; 5292 DWImage *bi = _dw_dest_image(pixmap, handle);
5230 id image = handle; 5293 /*CGContextRef context =*/ _dw_draw_context(bi, NO);
5231 UIImage *bi = nil; 5294
5232 bool bCanDraw = YES;
5233
5234 if(pixmap)
5235 bi = (id)pixmap->image;
5236 else
5237 {
5238 if([image isMemberOfClass:[DWRender class]])
5239 {
5240 DWRender *render = image;
5241
5242 bi = [render cachedDrawingRep];
5243 }
5244 }
5245 if(bi) 5295 if(bi)
5246 {
5247 _dw_draw_context(bi, NO);
5248 }
5249 if(bCanDraw == YES)
5250 { 5296 {
5251 UIBezierPath* aPath = [UIBezierPath bezierPath]; 5297 UIBezierPath* aPath = [UIBezierPath bezierPath];
5252 UIColor *color = pthread_getspecific(_dw_fg_color_key); 5298 UIColor *color = pthread_getspecific(_dw_fg_color_key);
5253 [color set]; 5299 [color set];
5254 5300
5255 [aPath moveToPoint:CGPointMake(x1 + 0.5, y1 + 0.5)]; 5301 [aPath moveToPoint:CGPointMake(x1 + 0.5, y1 + 0.5)];
5256 [aPath addLineToPoint:CGPointMake(x2 + 0.5, y2 + 0.5)]; 5302 [aPath addLineToPoint:CGPointMake(x2 + 0.5, y2 + 0.5)];
5257 [aPath stroke]; 5303 [aPath stroke];
5258 } 5304 }
5259
5260 if(bi)
5261 UIGraphicsEndImageContext();
5262 DW_LOCAL_POOL_OUT;
5263 DW_FUNCTION_RETURN_NOTHING; 5305 DW_FUNCTION_RETURN_NOTHING;
5264 } 5306 }
5265 5307
5266 /* Draw text on a window (preferably a render window). 5308 /* Draw text on a window (preferably a render window).
5267 * Parameters: 5309 * Parameters:
5275 DW_FUNCTION_ADD_PARAM5(handle, pixmap, x, y, text) 5317 DW_FUNCTION_ADD_PARAM5(handle, pixmap, x, y, text)
5276 DW_FUNCTION_NO_RETURN(dw_draw_text) 5318 DW_FUNCTION_NO_RETURN(dw_draw_text)
5277 DW_FUNCTION_RESTORE_PARAM5(handle, HWND, pixmap, HPIXMAP, x, int, y, int, text, const char *) 5319 DW_FUNCTION_RESTORE_PARAM5(handle, HWND, pixmap, HPIXMAP, x, int, y, int, text, const char *)
5278 { 5320 {
5279 DW_FUNCTION_INIT; 5321 DW_FUNCTION_INIT;
5280 DW_LOCAL_POOL_IN;
5281 id image = handle;
5282 NSString *nstr = [ NSString stringWithUTF8String:text ]; 5322 NSString *nstr = [ NSString stringWithUTF8String:text ];
5283 UIImage *bi = nil; 5323 DWImage *bi = nil;
5284 UIFont *font = nil; 5324 UIFont *font = nil;
5285 DWRender *render; 5325 DWRender *render;
5286 bool bCanDraw = YES; 5326 id image = handle;
5287 5327
5288 if(pixmap) 5328 if(pixmap)
5289 { 5329 {
5290 bi = (id)pixmap->image; 5330 bi = (id)pixmap->image;
5291 font = pixmap->font; 5331 font = pixmap->font;
5297 } 5337 }
5298 else if(image && [image isMemberOfClass:[DWRender class]]) 5338 else if(image && [image isMemberOfClass:[DWRender class]])
5299 { 5339 {
5300 render = image; 5340 render = image;
5301 font = [render font]; 5341 font = [render font];
5302 bi = [render cachedDrawingRep]; 5342 bi = [render cachedImage];
5303 } 5343 }
5304 if(bi) 5344 if(bi)
5305 { 5345 {
5306 _dw_draw_context(bi, NO); 5346 /*CGContextRef context =*/ _dw_draw_context(bi, NO);
5307 } 5347
5308
5309 if(bCanDraw == YES)
5310 {
5311 UIColor *fgcolor = pthread_getspecific(_dw_fg_color_key); 5348 UIColor *fgcolor = pthread_getspecific(_dw_fg_color_key);
5312 UIColor *bgcolor = pthread_getspecific(_dw_bg_color_key); 5349 UIColor *bgcolor = pthread_getspecific(_dw_bg_color_key);
5313 NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:fgcolor, NSForegroundColorAttributeName, nil]; 5350 NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:fgcolor, NSForegroundColorAttributeName, nil];
5314 if(bgcolor) 5351 if(bgcolor)
5315 [dict setValue:bgcolor forKey:NSBackgroundColorAttributeName]; 5352 [dict setValue:bgcolor forKey:NSBackgroundColorAttributeName];
5316 if(font) 5353 if(font)
5317 [dict setValue:font forKey:NSFontAttributeName]; 5354 [dict setValue:font forKey:NSFontAttributeName];
5318 [nstr drawAtPoint:CGPointMake(x, y) withAttributes:dict]; 5355 [nstr drawAtPoint:CGPointMake(x, y) withAttributes:dict];
5319 [dict release]; 5356 [dict release];
5320 } 5357 }
5321
5322 if(bi)
5323 UIGraphicsEndImageContext();
5324 DW_LOCAL_POOL_OUT;
5325 DW_FUNCTION_RETURN_NOTHING; 5358 DW_FUNCTION_RETURN_NOTHING;
5326 } 5359 }
5327 5360
5328 /* Query the width and height of a text string. 5361 /* Query the width and height of a text string.
5329 * Parameters: 5362 * Parameters:
5389 DW_FUNCTION_ADD_PARAM6(handle, pixmap, flags, npoints, x, y) 5422 DW_FUNCTION_ADD_PARAM6(handle, pixmap, flags, npoints, x, y)
5390 DW_FUNCTION_NO_RETURN(dw_draw_polygon) 5423 DW_FUNCTION_NO_RETURN(dw_draw_polygon)
5391 DW_FUNCTION_RESTORE_PARAM6(handle, HWND, pixmap, HPIXMAP, flags, int, npoints, int, x, int *, y, int *) 5424 DW_FUNCTION_RESTORE_PARAM6(handle, HWND, pixmap, HPIXMAP, flags, int, npoints, int, x, int *, y, int *)
5392 { 5425 {
5393 DW_FUNCTION_INIT; 5426 DW_FUNCTION_INIT;
5394 DW_LOCAL_POOL_IN; 5427 DWImage *bi = _dw_dest_image(pixmap, handle);
5395 id image = handle; 5428 /*CGContextRef context =*/ _dw_draw_context(bi, flags & DW_DRAW_NOAA ? NO : YES);
5396 UIImage *bi = nil; 5429
5397 bool bCanDraw = YES;
5398 int z;
5399
5400 if(pixmap)
5401 bi = (id)pixmap->image;
5402 else
5403 {
5404 if([image isMemberOfClass:[DWRender class]])
5405 {
5406 DWRender *render = image;
5407
5408 bi = [render cachedDrawingRep];
5409 }
5410 }
5411 if(bi) 5430 if(bi)
5412 {
5413 _dw_draw_context(bi, flags & DW_DRAW_NOAA ? NO : YES);
5414 }
5415
5416 if(bCanDraw == YES)
5417 { 5431 {
5418 UIBezierPath* aPath = [UIBezierPath bezierPath]; 5432 UIBezierPath* aPath = [UIBezierPath bezierPath];
5419 UIColor *color = pthread_getspecific(_dw_fg_color_key); 5433 UIColor *color = pthread_getspecific(_dw_fg_color_key);
5434 int z;
5435
5420 [color set]; 5436 [color set];
5421 5437
5422 [aPath moveToPoint:CGPointMake(*x + 0.5, *y + 0.5)]; 5438 [aPath moveToPoint:CGPointMake(*x + 0.5, *y + 0.5)];
5423 for(z=1;z<npoints;z++) 5439 for(z=1;z<npoints;z++)
5424 { 5440 {
5428 if(flags & DW_DRAW_FILL) 5444 if(flags & DW_DRAW_FILL)
5429 [aPath fill]; 5445 [aPath fill];
5430 else 5446 else
5431 [aPath stroke]; 5447 [aPath stroke];
5432 } 5448 }
5433
5434 if(bi)
5435 UIGraphicsEndImageContext();
5436 DW_LOCAL_POOL_OUT;
5437 DW_FUNCTION_RETURN_NOTHING; 5449 DW_FUNCTION_RETURN_NOTHING;
5438 } 5450 }
5439 5451
5440 /* Draw a rectangle on a window (preferably a render window). 5452 /* Draw a rectangle on a window (preferably a render window).
5441 * Parameters: 5453 * Parameters:
5451 DW_FUNCTION_ADD_PARAM7(handle, pixmap, flags, x, y, width, height) 5463 DW_FUNCTION_ADD_PARAM7(handle, pixmap, flags, x, y, width, height)
5452 DW_FUNCTION_NO_RETURN(dw_draw_rect) 5464 DW_FUNCTION_NO_RETURN(dw_draw_rect)
5453 DW_FUNCTION_RESTORE_PARAM7(handle, HWND, pixmap, HPIXMAP, flags, int, x, int, y, int, width, int, height, int) 5465 DW_FUNCTION_RESTORE_PARAM7(handle, HWND, pixmap, HPIXMAP, flags, int, x, int, y, int, width, int, height, int)
5454 { 5466 {
5455 DW_FUNCTION_INIT; 5467 DW_FUNCTION_INIT;
5456 DW_LOCAL_POOL_IN; 5468 DWImage *bi = _dw_dest_image(pixmap, handle);
5457 id image = handle; 5469 /*CGContextRef context =*/ _dw_draw_context(bi, flags & DW_DRAW_NOAA ? NO : YES);
5458 UIImage *bi = nil; 5470
5459 bool bCanDraw = YES;
5460
5461 if(pixmap)
5462 bi = (id)pixmap->image;
5463 else
5464 {
5465 if([image isMemberOfClass:[DWRender class]])
5466 {
5467 DWRender *render = image;
5468
5469 bi = [render cachedDrawingRep];
5470 }
5471 }
5472 if(bi) 5471 if(bi)
5473 {
5474 _dw_draw_context(bi, flags & DW_DRAW_NOAA ? NO : YES);
5475 }
5476
5477 if(bCanDraw == YES)
5478 { 5472 {
5479 UIColor *color = pthread_getspecific(_dw_fg_color_key); 5473 UIColor *color = pthread_getspecific(_dw_fg_color_key);
5480 UIBezierPath *bp = [UIBezierPath bezierPathWithRect:CGRectMake(x, y, width, height)];; 5474 UIBezierPath *bp = [UIBezierPath bezierPathWithRect:CGRectMake(x, y, width, height)];;
5481 5475
5482 [color set]; 5476 [color set];
5484 if(flags & DW_DRAW_FILL) 5478 if(flags & DW_DRAW_FILL)
5485 [bp fill]; 5479 [bp fill];
5486 else 5480 else
5487 [bp stroke]; 5481 [bp stroke];
5488 } 5482 }
5489
5490 if(bi)
5491 UIGraphicsEndImageContext();
5492 DW_LOCAL_POOL_OUT;
5493 DW_FUNCTION_RETURN_NOTHING; 5483 DW_FUNCTION_RETURN_NOTHING;
5494 } 5484 }
5495 5485
5496 /* Draw an arc on a window (preferably a render window). 5486 /* Draw an arc on a window (preferably a render window).
5497 * Parameters: 5487 * Parameters:
5510 DW_FUNCTION_ADD_PARAM9(handle, pixmap, flags, xorigin, yorigin, x1, y1, x2, y2) 5500 DW_FUNCTION_ADD_PARAM9(handle, pixmap, flags, xorigin, yorigin, x1, y1, x2, y2)
5511 DW_FUNCTION_NO_RETURN(dw_draw_arc) 5501 DW_FUNCTION_NO_RETURN(dw_draw_arc)
5512 DW_FUNCTION_RESTORE_PARAM9(handle, HWND, pixmap, HPIXMAP, flags, int, xorigin, int, yorigin, int, x1, int, y1, int, x2, int, y2, int) 5502 DW_FUNCTION_RESTORE_PARAM9(handle, HWND, pixmap, HPIXMAP, flags, int, xorigin, int, yorigin, int, x1, int, y1, int, x2, int, y2, int)
5513 { 5503 {
5514 DW_FUNCTION_INIT; 5504 DW_FUNCTION_INIT;
5515 DW_LOCAL_POOL_IN; 5505 DWImage *bi = _dw_dest_image(pixmap, handle);
5516 id image = handle; 5506 /*CGContextRef context =*/ _dw_draw_context(bi, flags & DW_DRAW_NOAA ? NO : YES);
5517 UIImage *bi = nil; 5507
5518 bool bCanDraw = YES;
5519
5520 if(pixmap)
5521 bi = (id)pixmap->image;
5522 else
5523 {
5524 if([image isMemberOfClass:[DWRender class]])
5525 {
5526 DWRender *render = image;
5527
5528 bi = [render cachedDrawingRep];
5529 }
5530 }
5531 if(bi) 5508 if(bi)
5532 {
5533 _dw_draw_context(bi, flags & DW_DRAW_NOAA ? NO : YES);
5534 }
5535
5536 if(bCanDraw)
5537 { 5509 {
5538 UIBezierPath* aPath; 5510 UIBezierPath* aPath;
5539 UIColor *color = pthread_getspecific(_dw_fg_color_key); 5511 UIColor *color = pthread_getspecific(_dw_fg_color_key);
5540 [color set]; 5512 [color set];
5541 5513
5562 if(flags & DW_DRAW_FILL) 5534 if(flags & DW_DRAW_FILL)
5563 [aPath fill]; 5535 [aPath fill];
5564 else 5536 else
5565 [aPath stroke]; 5537 [aPath stroke];
5566 } 5538 }
5567
5568 if(bi)
5569 UIGraphicsEndImageContext();
5570 DW_LOCAL_POOL_OUT;
5571 DW_FUNCTION_RETURN_NOTHING; 5539 DW_FUNCTION_RETURN_NOTHING;
5572 } 5540 }
5573 5541
5574 /* 5542 /*
5575 * Create a tree object to be packed. 5543 * Create a tree object to be packed.
6829 { 6797 {
6830 HPIXMAP pixmap = NULL; 6798 HPIXMAP pixmap = NULL;
6831 6799
6832 if((pixmap = calloc(1,sizeof(struct _hpixmap)))) 6800 if((pixmap = calloc(1,sizeof(struct _hpixmap))))
6833 { 6801 {
6834 CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
6835 CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, calloc(width*height, 4), width*height*4, NULL);
6836 CGImageRef image = CGImageCreate(width, height, 8, 32, 4 * width,
6837 rgb, kCGBitmapByteOrderDefault | kCGImageAlphaLast,
6838 provider, NULL, false, kCGRenderingIntentDefault);
6839 pixmap->width = width; 6802 pixmap->width = width;
6840 pixmap->height = height; 6803 pixmap->height = height;
6841 pixmap->handle = handle; 6804 pixmap->handle = handle;
6842 pixmap->image = [[[UIImage alloc] initWithCGImage:image] retain]; 6805 pixmap->image = [[[DWImage alloc] initWithSize:CGSizeMake(width,height)] retain];
6843 } 6806 }
6844 return pixmap; 6807 return pixmap;
6845 } 6808 }
6846 6809
6847 /* 6810 /*
6878 return NULL; 6841 return NULL;
6879 } 6842 }
6880 pixmap->width = [tmpimage size].width; 6843 pixmap->width = [tmpimage size].width;
6881 pixmap->height = [tmpimage size].height; 6844 pixmap->height = [tmpimage size].height;
6882 pixmap->image = tmpimage; 6845 pixmap->image = tmpimage;
6883 pixmap->handle = handle; 6846 pixmap->handle = [[DWImage alloc] initWithUIImage:tmpimage];
6884 DW_LOCAL_POOL_OUT; 6847 DW_LOCAL_POOL_OUT;
6885 return pixmap; 6848 return pixmap;
6886 } 6849 }
6887 6850
6888 /* 6851 /*
6912 DW_LOCAL_POOL_OUT; 6875 DW_LOCAL_POOL_OUT;
6913 return NULL; 6876 return NULL;
6914 } 6877 }
6915 pixmap->width = [tmpimage size].width; 6878 pixmap->width = [tmpimage size].width;
6916 pixmap->height = [tmpimage size].height; 6879 pixmap->height = [tmpimage size].height;
6917 pixmap->image = tmpimage; 6880 pixmap->image = [[DWImage alloc] initWithUIImage:tmpimage];
6918 pixmap->handle = handle; 6881 pixmap->handle = handle;
6919 DW_LOCAL_POOL_OUT; 6882 DW_LOCAL_POOL_OUT;
6920 return pixmap; 6883 return pixmap;
6921 } 6884 }
6922 6885
6960 6923
6961 if(tmpimage) 6924 if(tmpimage)
6962 { 6925 {
6963 pixmap->width = [tmpimage size].width; 6926 pixmap->width = [tmpimage size].width;
6964 pixmap->height = [tmpimage size].height; 6927 pixmap->height = [tmpimage size].height;
6965 pixmap->image = tmpimage; 6928 pixmap->image = [[DWImage alloc] initWithUIImage:tmpimage];
6966 pixmap->handle = handle; 6929 pixmap->handle = handle;
6967 DW_LOCAL_POOL_OUT; 6930 DW_LOCAL_POOL_OUT;
6968 return pixmap; 6931 return pixmap;
6969 } 6932 }
6970 free(pixmap); 6933 free(pixmap);
7012 */ 6975 */
7013 void API dw_pixmap_destroy(HPIXMAP pixmap) 6976 void API dw_pixmap_destroy(HPIXMAP pixmap)
7014 { 6977 {
7015 if(pixmap) 6978 if(pixmap)
7016 { 6979 {
7017 UIImage *image = (UIImage *)pixmap->image; 6980 DWImage *image = (DWImage *)pixmap->image;
7018 UIFont *font = pixmap->font; 6981 UIFont *font = pixmap->font;
7019 DW_LOCAL_POOL_IN; 6982 DW_LOCAL_POOL_IN;
7020 [image release]; 6983 [image release];
7021 [font release]; 6984 [font release];
7022 free(pixmap); 6985 free(pixmap);