TableDrawable.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. @implementation TableDrawable
  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* rows = self.drawableTemplate[@"rows"];
  15. int count = [rows[@"count"] intValue];
  16. // 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];
  17. CGRect pdfrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  18. CGRect parentrect =[self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  19. for(int i=0;i<count;i++)
  20. {
  21. NSMutableDictionary * rowjson =rows[ [NSString stringWithFormat:@"row_%d",i ] ];
  22. PDFDrawable* drawable_obj= [[GroupDrawable alloc] init:rowjson];
  23. drawable_obj.delegate = self.delegate;
  24. NSMutableDictionary* rows_data = table_data[rowjson[@"data_source"]];
  25. int row_data_count=[rows_data[@"count"] intValue];
  26. float dy=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(dy+row_rect.size.height> between_header_and_footer.location+between_header_and_footer.length)
  32. {
  33. //越界,新起一页
  34. if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
  35. [self.delegate addPageCount:context];
  36. dy=0;
  37. }
  38. }
  39. // [drawable_obj Draw:context dataSource:row_data ParentRect:(CGRect)pdfrect startX:0 startY:dy flipHeight:flip_height range:between_header_and_footer];
  40. dy+=row_rect.size.height;
  41. // pos = CGPointMake(0, rowpos.y+pos.y);
  42. }
  43. }
  44. return parentrect;
  45. }
  46. -(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
  47. {
  48. NSMutableDictionary* table_data = data[self.drawableTemplate[@"data_source"]];
  49. NSMutableDictionary* rows = self.drawableTemplate[@"rows"];
  50. int count = [rows[@"count"] intValue];
  51. // 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];
  52. CGRect pdfrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  53. CGRect parentrect =[self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  54. for(int i=0;i<count;i++)
  55. {
  56. NSMutableDictionary * rowjson =rows[ [NSString stringWithFormat:@"row_%d",i ] ];
  57. PDFDrawable* drawable_obj= [[GroupDrawable alloc] init:rowjson];
  58. drawable_obj.delegate = self.delegate;
  59. NSMutableDictionary* rows_data = table_data[rowjson[@"data_source"]];
  60. int row_data_count=[rows_data[@"count"] intValue];
  61. float dy=0;
  62. for(int j=0;j<row_data_count;j++)
  63. {
  64. NSMutableDictionary * row_data =rows_data[ [NSString stringWithFormat:@"item_%d",j ] ];
  65. 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];
  66. if(dy+row_rect.size.height> between_header_and_footer.location+between_header_and_footer.length)
  67. {
  68. //越界,新起一页
  69. if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
  70. [self.delegate outofPage:context];
  71. dy=0;
  72. }
  73. }
  74. [drawable_obj Draw:context dataSource:row_data ParentRect:(CGRect)pdfrect startX:0 startY:dy flipHeight:flip_height range:between_header_and_footer];
  75. dy+=row_rect.size.height;
  76. // pos = CGPointMake(0, rowpos.y+pos.y);
  77. }
  78. }
  79. return parentrect;
  80. //return CGPointMake(parentrect.origin.x+parentrect.size.width, parentrect.origin.y+parentrect.size.height);
  81. // if (self.delegate && [self.delegate respondsToSelector:@selector(outofPage:)]) {
  82. // [self.delegate outofPage:context];
  83. // }
  84. }
  85. @end