TableDrawable.m 17 KB

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