GridDrawable.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // GridDrawable.m
  3. // pdftest
  4. //
  5. // Created by Ray on 10/21/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "GridDrawable.h"
  9. #import "GroupDrawable.h"
  10. @implementation GridDrawable
  11. -(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
  12. {
  13. NSMutableDictionary* table_data = data[self.drawableTemplate[@"data_source"]];
  14. NSMutableDictionary* celljson = self.drawableTemplate[@"cell"];
  15. NSString* direction = celljson[@"direction"];
  16. if(direction==nil)
  17. direction=@"H";
  18. // 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];
  19. CGRect pdfrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  20. CGRect parentrect =[self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  21. PDFDrawable* drawable_obj= [[GroupDrawable alloc] init:celljson];
  22. drawable_obj.delegate = self.delegate;
  23. NSMutableDictionary* rows_data = table_data[celljson[@"data_source"]];
  24. int row_data_count=[rows_data[@"count"] intValue];
  25. float dy=y;
  26. float dx=0;
  27. for(int j=0;j<row_data_count;j++)
  28. {
  29. NSMutableDictionary * row_data =rows_data[ [NSString stringWithFormat:@"item_%d",j ] ];
  30. 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];
  31. if(row_rect.size.width>parentrect.size.width||row_rect.size.height>parentrect.size.height)
  32. {
  33. // cell 比 grid 还要大,模板有错,直接返回。
  34. return parentrect;
  35. }
  36. if([direction isEqualToString:@"H"])
  37. {
  38. if(dy+row_rect.size.height> between_header_and_footer.location+between_header_and_footer.length )
  39. {
  40. //越界,新起一页
  41. if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
  42. [self.delegate addPageCount:context];
  43. dy=y;
  44. dx=0;
  45. }
  46. }
  47. }
  48. else
  49. {
  50. if(/*dy+row_rect.size.height> between_header_and_footer.location+between_header_and_footer.length && */dx+row_rect.size.width>parentrect.size.width )
  51. {
  52. //越界,新起一页
  53. if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
  54. [self.delegate addPageCount:context];
  55. dy=y;
  56. dx=0;
  57. }
  58. }
  59. }
  60. // [drawable_obj Draw:context dataSource:row_data ParentRect:(CGRect)pdfrect startX:dx startY:dy flipHeight:flip_height range:between_header_and_footer];
  61. if([direction isEqualToString:@"H"])
  62. {
  63. dx+=row_rect.size.width;
  64. if(dx+row_rect.size.width>parentrect.size.width)
  65. {
  66. dx=0;
  67. dy+=row_rect.size.height;
  68. }
  69. }
  70. else
  71. {
  72. dy+=row_rect.size.height;
  73. if(dy-y+row_rect.size.height>parentrect.size.height)
  74. {
  75. dy=y;
  76. dx+=row_rect.size.width;
  77. }
  78. }
  79. }
  80. return parentrect;
  81. //return CGPointMake(parentrect.origin.x+parentrect.size.width, parentrect.origin.y+parentrect.size.height);
  82. // if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
  83. // [self.delegate outofPage:context];
  84. // }
  85. }
  86. -(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
  87. {
  88. NSMutableDictionary* table_data = data[self.drawableTemplate[@"data_source"]];
  89. NSMutableDictionary* celljson = self.drawableTemplate[@"cell"];
  90. NSString* direction = celljson[@"direction"];
  91. if(direction==nil)
  92. direction=@"H";
  93. // 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];
  94. CGRect pdfrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  95. CGRect parentrect =[self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  96. PDFDrawable* drawable_obj= [[GroupDrawable alloc] init:celljson];
  97. drawable_obj.delegate = self.delegate;
  98. NSMutableDictionary* rows_data = table_data[celljson[@"data_source"]];
  99. int row_data_count=[rows_data[@"count"] intValue];
  100. float dy=y;
  101. float dx=0;
  102. for(int j=0;j<row_data_count;j++)
  103. {
  104. NSMutableDictionary * row_data =rows_data[ [NSString stringWithFormat:@"item_%d",j ] ];
  105. 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];
  106. if(row_rect.size.width>parentrect.size.width||row_rect.size.height>parentrect.size.height)
  107. {
  108. // cell 比 grid 还要大,模板有错,直接返回。
  109. return parentrect;
  110. }
  111. if([direction isEqualToString:@"H"])
  112. {
  113. if(dy+row_rect.size.height> between_header_and_footer.location+between_header_and_footer.length )
  114. {
  115. //越界,新起一页
  116. if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
  117. [self.delegate outofPage:context];
  118. dy=y;
  119. dx=0;
  120. }
  121. }
  122. }
  123. else
  124. {
  125. if(/*dy+row_rect.size.height> between_header_and_footer.location+between_header_and_footer.length && */dx+row_rect.size.width>parentrect.size.width )
  126. {
  127. //越界,新起一页
  128. if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
  129. [self.delegate outofPage:context];
  130. dy=y;
  131. dx=0;
  132. }
  133. }
  134. }
  135. [drawable_obj Draw:context dataSource:row_data ParentRect:(CGRect)pdfrect startX:dx startY:dy flipHeight:flip_height range:between_header_and_footer];
  136. if([direction isEqualToString:@"H"])
  137. {
  138. dx+=row_rect.size.width;
  139. if(dx+row_rect.size.width>parentrect.size.width)
  140. {
  141. dx=0;
  142. dy+=row_rect.size.height;
  143. }
  144. }
  145. else
  146. {
  147. dy+=row_rect.size.height;
  148. if(dy-y+row_rect.size.height>parentrect.size.height)
  149. {
  150. dy=y;
  151. dx+=row_rect.size.width;
  152. }
  153. }
  154. }
  155. return parentrect;
  156. //return CGPointMake(parentrect.origin.x+parentrect.size.width, parentrect.origin.y+parentrect.size.height);
  157. // if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
  158. // [self.delegate outofPage:context];
  159. // }
  160. }
  161. @end