TableDrawable.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. //
  2. // TableDrawable.m
  3. // pdftest
  4. //
  5. // Created by Ray on 10/18/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "TableDrawable.h"
  9. #import "GroupDrawable.h"
  10. #import "LineDrawable.h"
  11. #import "RAUtils.h"
  12. @implementation TableDrawable
  13. -(void) setDrawableTemplate:(NSMutableDictionary *)drawableTemplate
  14. {
  15. [super setDrawableTemplate:drawableTemplate];
  16. // kCTTextAlignmentJustified CT_ENUM_AVAILABLE(10_8, 6_0) = 3,
  17. // kCTTextAlignmentNatural CT_ENUM_AVAILABLE(10_8, 6_0) = 4,
  18. // _drawableTemplate = drawableTemplate;
  19. // self.margin_left=[self.drawableTemplate[@"margin_left"] doubleValue];
  20. // self.margin_right=[self.drawableTemplate[@"margin_right"] doubleValue];
  21. // self.margin_top=[self.drawableTemplate[@"margin_top"] doubleValue];
  22. // self.margin_bottom=[self.drawableTemplate[@"margin_bottom"] doubleValue];
  23. // self.rect = CGRectMake(0, 0, [self.drawableTemplate[@"width"] doubleValue], [self.drawableTemplate[@"height"] doubleValue]);
  24. }
  25. -(CGRect) Query_Rect:(CGContextRef) context dataSource:(NSMutableDictionary*)data ParentRect:(CGRect)p_rect startX:(double) x startY:(double) y flipHeight:(double)flip_height range:(NSRange)between_header_and_footer
  26. {
  27. /*
  28. bool header_repeat = [self.drawableTemplate[@"header_repeat"] boolValue];
  29. bool drawheader = true;
  30. NSMutableDictionary* table_data = data[self.drawableTemplate[@"data_source"]];
  31. NSMutableDictionary* rows = self.drawableTemplate[@"rows"];
  32. int count = [rows[@"count"] intValue];
  33. */
  34. // CGRect pdfrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  35. CGRect parentrect =[self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  36. /*
  37. float dy=y;
  38. dy= [self QueryHeader:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer dy:dy];
  39. for(int i=0;i<count;i++)
  40. {
  41. NSMutableDictionary * rowjson =rows[ [NSString stringWithFormat:@"row_%d",i ] ];
  42. PDFDrawable* drawable_obj= [[GroupDrawable alloc] init:rowjson];
  43. drawable_obj.delegate = self.delegate;
  44. NSMutableDictionary* rows_data = table_data[rowjson[@"data_source"]];
  45. int row_data_count=[rows_data[@"count"] intValue];
  46. int j=0;
  47. while(true)
  48. // for(int j=0;j<row_data_count;j++)
  49. {
  50. if(i!=count-1&& j==row_data_count)
  51. break;
  52. if(drawheader)
  53. {
  54. drawheader = false;
  55. }
  56. NSMutableDictionary * row_data =rows_data[ [NSString stringWithFormat:@"item_%d",j ] ];
  57. CGRect row_rect= [drawable_obj Query_Rect:context dataSource:row_data ParentRect:(CGRect)pdfrect startX:0 startY:dy flipHeight:flip_height range:between_header_and_footer];
  58. if(row_rect.size.width>parentrect.size.width||row_rect.size.height>parentrect.size.height)
  59. {
  60. // cell 比 table 还要大,模板有错,直接返回。
  61. return parentrect;
  62. }
  63. if(dy+row_rect.size.height> between_header_and_footer.location+between_header_and_footer.length )
  64. {
  65. if(j<row_data_count)
  66. {
  67. //越界,新起一页
  68. if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
  69. [self.delegate outofPage:context];
  70. dy=y;
  71. if(header_repeat)
  72. {
  73. dy= [self QueryHeader:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer dy:dy];
  74. // drawheader=true;
  75. }
  76. // dx=0;
  77. }
  78. }
  79. else
  80. {
  81. break;
  82. // goto finish_draw;
  83. }
  84. }
  85. [drawable_obj Draw:context dataSource:row_data ParentRect:(CGRect)pdfrect startX:0 startY:dy flipHeight:flip_height range:between_header_and_footer];
  86. // if([direction isEqualToString:@"H"])
  87. // {
  88. // dx+=row_rect.size.width;
  89. // if(dx+row_rect.size.width>parentrect.size.width)
  90. {
  91. // dx=0;
  92. dy+=row_rect.size.height;
  93. }
  94. // }
  95. j++;
  96. }
  97. }
  98. //finish_draw:*/
  99. // [self DrawBound:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer];
  100. return parentrect;
  101. }
  102. -(float) QueryHeader:(CGContextRef) context dataSource:(NSMutableDictionary*)data ParentRect:(CGRect)p_rect startX:(double) x startY:(double) y flipHeight:(double)flip_height range:(NSRange)between_header_and_footer dy:(double) dy
  103. {
  104. // draw table header
  105. NSMutableDictionary* table_data = data[self.drawableTemplate[@"data_source"]];
  106. NSMutableDictionary* rows = self.drawableTemplate[@"rows"];
  107. // int count = [rows[@"count"] intValue];
  108. // CGRect pdfrect = /*CGRectMake(self.rect.origin.x+self.margin_left+p_rect.origin.x, self.rect.origin.y+self.margin_top+p_rect.origin.y, self.rect.size.width-self.margin_right, self.rect.size.height-self.margin_bottom);//*/[self to_pdf_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  109. CGRect pdfrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  110. CGRect parentrect =[self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  111. NSMutableDictionary * headerjson =rows[@"header"];
  112. PDFDrawable* header_drawable_obj= [[GroupDrawable alloc] init:headerjson];
  113. header_drawable_obj.delegate = self.delegate;
  114. NSMutableDictionary* header_data = table_data[headerjson[@"data_source"]];
  115. // int row_data_count=[header_data[@"count"] intValue];
  116. NSMutableDictionary * row_data =header_data[ [NSString stringWithFormat:@"item_%d",0 ] ];
  117. CGRect row_rect= [header_drawable_obj Query_Rect:context dataSource:row_data ParentRect:(CGRect)pdfrect startX:0 startY:dy flipHeight:flip_height range:between_header_and_footer];
  118. if(row_rect.size.width>parentrect.size.width||row_rect.size.height>parentrect.size.height)
  119. {
  120. // cell 比 table 还要大,模板有错,直接返回。
  121. return 0;
  122. }
  123. if(dy+row_rect.size.height> between_header_and_footer.location+between_header_and_footer.length )
  124. {
  125. //越界,新起一页
  126. if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
  127. [self.delegate outofPage:context];
  128. dy=y;
  129. // dx=0;
  130. }
  131. }
  132. // [header_drawable_obj Draw:context dataSource:row_data ParentRect:(CGRect)pdfrect startX:0 startY:dy flipHeight:flip_height range:between_header_and_footer];
  133. // if([direction isEqualToString:@"H"])
  134. // {
  135. // dx+=row_rect.size.width;
  136. // if(dx+row_rect.size.width>parentrect.size.width)
  137. {
  138. // dx=0;
  139. dy+=row_rect.size.height;
  140. }
  141. return dy;
  142. }
  143. -(float) DrawHeader:(CGContextRef) context dataSource:(NSMutableDictionary*)data ParentRect:(CGRect)p_rect startX:(double) x startY:(double) y flipHeight:(double)flip_height range:(NSRange)between_header_and_footer dy:(double) dy
  144. {
  145. // draw table header
  146. NSMutableDictionary* table_data = data[self.drawableTemplate[@"data_source"]];
  147. NSMutableDictionary* rows = self.drawableTemplate[@"rows"];
  148. // int count = [rows[@"count"] intValue];
  149. // CGRect pdfrect = /*CGRectMake(self.rect.origin.x+self.margin_left+p_rect.origin.x, self.rect.origin.y+self.margin_top+p_rect.origin.y, self.rect.size.width-self.margin_right, self.rect.size.height-self.margin_bottom);//*/[self to_pdf_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  150. CGRect pdfrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  151. CGRect parentrect =[self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  152. NSMutableDictionary * headerjson =rows[@"header"];
  153. PDFDrawable* header_drawable_obj= [[GroupDrawable alloc] init:headerjson];
  154. header_drawable_obj.delegate = self.delegate;
  155. NSMutableDictionary* header_data = table_data[headerjson[@"data_source"]];
  156. // int row_data_count=[header_data[@"count"] intValue];
  157. NSMutableDictionary * row_data =header_data[ [NSString stringWithFormat:@"item_%d",0 ] ];
  158. CGRect row_rect= [header_drawable_obj Query_Rect:context dataSource:row_data ParentRect:(CGRect)pdfrect startX:0 startY:dy flipHeight:flip_height range:between_header_and_footer];
  159. if(row_rect.size.width>parentrect.size.width||row_rect.size.height>parentrect.size.height)
  160. {
  161. // cell 比 table 还要大,模板有错,直接返回。
  162. return 0;
  163. }
  164. if(dy+row_rect.size.height> between_header_and_footer.location+between_header_and_footer.length )
  165. {
  166. //越界,新起一页
  167. if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
  168. [self.delegate outofPage:context];
  169. dy=y;
  170. // dx=0;
  171. }
  172. }
  173. [header_drawable_obj Draw:context dataSource:row_data ParentRect:(CGRect)pdfrect startX:0 startY:dy flipHeight:flip_height range:between_header_and_footer];
  174. // if([direction isEqualToString:@"H"])
  175. // {
  176. // dx+=row_rect.size.width;
  177. // if(dx+row_rect.size.width>parentrect.size.width)
  178. {
  179. // dx=0;
  180. dy+=row_rect.size.height;
  181. }
  182. return dy;
  183. }
  184. -(CGRect) Draw:(CGContextRef) context dataSource:(NSMutableDictionary*)data ParentRect:(CGRect)p_rect startX:(double) x startY:(double) y flipHeight:(double)flip_height range:(NSRange)between_header_and_footer
  185. {
  186. bool header_repeat = [self.drawableTemplate[@"header_repeat"] boolValue];
  187. bool drawheader = true;
  188. NSMutableDictionary* table_data = data[self.drawableTemplate[@"data_source"]];
  189. NSMutableDictionary* rows = self.drawableTemplate[@"rows"];
  190. int count = [rows[@"count"] intValue];
  191. // CGRect pdfrect = /*CGRectMake(self.rect.origin.x+self.margin_left+p_rect.origin.x, self.rect.origin.y+self.margin_top+p_rect.origin.y, self.rect.size.width-self.margin_right, self.rect.size.height-self.margin_bottom);//*/[self to_pdf_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  192. CGRect pdfrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  193. CGRect parentrect =[self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  194. float dy=y;
  195. dy= [self DrawHeader:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer dy:dy];
  196. // }
  197. // int i=0;
  198. for(int i=0;i<count;i++)
  199. {
  200. NSMutableDictionary * rowjson =rows[ [NSString stringWithFormat:@"row_%d",i ] ];
  201. if(rowjson[@"separator"]!=nil)
  202. {
  203. PDFDrawable* separator_obj= [[LineDrawable alloc] init:rowjson[@"separator"]];
  204. separator_obj.delegate = self.delegate;
  205. [separator_obj Draw:context dataSource:nil ParentRect:(CGRect)pdfrect startX:0 startY:dy flipHeight:flip_height range:between_header_and_footer];
  206. }
  207. NSMutableDictionary* rows_data = table_data[rowjson[@"data_source"]];
  208. int row_data_count=[rows_data[@"count"] intValue];
  209. int j=0;
  210. while(true)
  211. // for(int j=0;j<row_data_count;j++)
  212. {
  213. PDFDrawable* drawable_obj= [[GroupDrawable alloc] init:rowjson];
  214. drawable_obj.delegate = self.delegate;
  215. if(i!=count-1&& j==row_data_count)
  216. break;
  217. if(drawheader)
  218. {
  219. drawheader = false;
  220. }
  221. NSMutableDictionary * row_data =rows_data[ [NSString stringWithFormat:@"item_%d",j ] ];
  222. CGRect row_rect= [drawable_obj Query_Rect:context dataSource:row_data ParentRect:(CGRect)pdfrect startX:0 startY:dy flipHeight:flip_height range:between_header_and_footer];
  223. /*
  224. if(dy+row_rect.size.height> between_header_and_footer.location+between_header_and_footer.length)
  225. {
  226. //越界,新起一页
  227. if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
  228. [self.delegate outofPage:context];
  229. dy=0;
  230. }
  231. }
  232. [drawable_obj Draw:context dataSource:row_data ParentRect:(CGRect)pdfrect startX:0 startY:dy flipHeight:flip_height range:between_header_and_footer];
  233. dy+=row_rect.size.height;
  234. // pos = CGPointMake(0, rowpos.y+pos.y);
  235. */
  236. if(row_rect.size.width>parentrect.size.width||row_rect.size.height>parentrect.size.height)
  237. {
  238. // cell 比 table 还要大,模板有错,直接返回。
  239. return parentrect;
  240. }
  241. if([self.drawableTemplate[@"split"] boolValue])
  242. {
  243. if(/*dy+row_rect.size.height+self.margin_top+self.margin_bottom+self.rect.origin.y*/(row_rect.origin.y+row_rect.size.height> between_header_and_footer.location+between_header_and_footer.length) )
  244. {
  245. if(j<row_data_count)
  246. {
  247. //把表格剩余部分补满
  248. double height = between_header_and_footer.location+between_header_and_footer.length-(row_rect.origin.y);
  249. if(height>0)
  250. {
  251. NSMutableDictionary * fixjson = [[RAUtils copyDict:rowjson] mutableCopy];
  252. // NSMutableDictionary * fixjson =[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:rowjson]];
  253. fixjson[@"height"]=[NSNumber numberWithInt:height];
  254. fixjson[@"expand"]=@"false";
  255. PDFDrawable* fix_obj= [[GroupDrawable alloc] init:fixjson];
  256. fix_obj.delegate = self.delegate;
  257. [fix_obj Draw:context dataSource:nil ParentRect:(CGRect)pdfrect startX:0 startY:dy flipHeight:flip_height range:between_header_and_footer];
  258. }
  259. //越界,新起一页
  260. if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
  261. [self.delegate outofPage:context];
  262. dy=y;
  263. if(header_repeat)
  264. {
  265. dy= [self DrawHeader:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer dy:dy];
  266. // drawheader=true;
  267. }
  268. // dx=0;
  269. }
  270. }
  271. else
  272. {
  273. //把表格剩余部分补满
  274. double height = between_header_and_footer.location+between_header_and_footer.length-(row_rect.origin.y);
  275. if(height>0)
  276. {
  277. NSMutableDictionary * fixjson = [[RAUtils copyDict:rowjson] mutableCopy];
  278. // NSMutableDictionary * fixjson =[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:rowjson]];
  279. fixjson[@"height"]=[NSNumber numberWithInt:height];
  280. fixjson[@"expand"]=@"false";
  281. PDFDrawable* fix_obj= [[GroupDrawable alloc] init:fixjson];
  282. fix_obj.delegate = self.delegate;
  283. [fix_obj Draw:context dataSource:nil ParentRect:(CGRect)pdfrect startX:0 startY:dy flipHeight:flip_height range:between_header_and_footer];
  284. }
  285. break;
  286. // goto finish_draw;
  287. }
  288. }
  289. }
  290. else
  291. {
  292. if(j>row_data_count)
  293. break;
  294. }
  295. [drawable_obj Draw:context dataSource:row_data ParentRect:(CGRect)pdfrect startX:0 startY:dy flipHeight:flip_height range:between_header_and_footer];
  296. // if([direction isEqualToString:@"H"])
  297. // {
  298. // dx+=row_rect.size.width;
  299. // if(dx+row_rect.size.width>parentrect.size.width)
  300. {
  301. // dx=0;
  302. dy+=row_rect.size.height;
  303. }
  304. // }
  305. j++;
  306. }
  307. }
  308. //finish_draw:
  309. [self DrawBound:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer];
  310. return parentrect;
  311. //return CGPointMake(parentrect.origin.x+parentrect.size.width, parentrect.origin.y+parentrect.size.height);
  312. // if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
  313. // [self.delegate outofPage:context];
  314. // }
  315. }
  316. @end