LineDrawable.m 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // LineDrawable.m
  3. // pdftest
  4. //
  5. // Created by Ray on 10/15/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "LineDrawable.h"
  9. #import "RAUtils.h"
  10. @implementation LineDrawable
  11. +(NSMutableDictionary *) createlineTemplate:(double) width from:(CGPoint) from to:(CGPoint) to
  12. {
  13. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  14. // "type": "line",
  15. // "x0": 10,
  16. // "y0": 10,
  17. // "x1": 100,
  18. // "y1": 100,
  19. // "line_width": 0.02
  20. ret[@"type"]=@"line" ;
  21. ret[@"x0"]=[NSNumber numberWithDouble:from.x] ;
  22. ret[@"y0"]=[NSNumber numberWithDouble:from.y] ;
  23. ret[@"x1"]=[NSNumber numberWithDouble:to.x] ;
  24. ret[@"y1"]=[NSNumber numberWithDouble:to.y] ;
  25. ret[@"line_width"]=[NSNumber numberWithDouble:width] ;
  26. return ret;
  27. }
  28. -(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
  29. {
  30. return CGRectMake(0, 0, 0, 0);
  31. }
  32. -(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
  33. {
  34. CGContextSaveGState(context);
  35. // CGContextSetRGBStrokeColor(context,1,1,1,1.0);//画笔线的颜色
  36. CGContextSetLineWidth(context, [self.drawableTemplate[@"line_width"] doubleValue]);
  37. // CGContextMoveToPoint(context, [self.drawableTemplate[@"x0"] doubleValue], [self.drawableTemplate[@"y0"] doubleValue]);
  38. //
  39. // CGContextAddLineToPoint(context, [self.drawableTemplate[@"x1"] doubleValue], [self.drawableTemplate[@"y1"] doubleValue]);
  40. //
  41. // CGContextStrokePath(context);
  42. CGPoint aPoints[2];//坐标点
  43. aPoints[0] =[self to_pdf_point:p_rect point:CGPointMake([self.drawableTemplate[@"x0"] doubleValue]+x, [self.drawableTemplate[@"y0"] doubleValue]+y) contextHeight:flip_height];//坐标1
  44. aPoints[1] =[self to_pdf_point:p_rect point:CGPointMake([self.drawableTemplate[@"x1"] doubleValue]+x, [self.drawableTemplate[@"y1"] doubleValue]+y) contextHeight:flip_height];//坐标2
  45. DebugLog(@"line from %@ to %@",NSStringFromCGPoint(aPoints[0]),NSStringFromCGPoint(aPoints[1]));
  46. //CGContextAddLines(CGContextRef c, const CGPoint points[],size_t count)
  47. //points[]坐标数组,和count大小
  48. CGContextAddLines(context, aPoints, 2);//添加线
  49. CGContextDrawPath(context, kCGPathStroke); //根据坐标绘制路径
  50. CGContextRestoreGState(context);
  51. return CGRectMake(0, 0, 0, 0);
  52. }
  53. @end