@@ -32,13 +32,14 @@ def tmap(*args):
3232 """Call map() and convert result to a tuple."""
3333 return tuple (map (* args ))
3434
35- def fill (strings , line = 75 ):
36- """Make one string from sequence of strings, breaking lines
37- to form lines of at most 72 characters, if possible."""
35+ def fill (strings , linelen = 75 ):
36+ """Make one string from sequence of strings, with whitespace
37+ in between. The whitespace is chosen to form lines of at most
38+ linelen characters, if possible."""
3839
3940 s , strings = [strings [0 ]], strings [1 :]
4041 while strings :
41- if len (s [- 1 ]) + len (strings [0 ]) < 72 :
42+ if len (s [- 1 ]) + len (strings [0 ]) < linelen :
4243 s [- 1 ] += ' ' + strings [0 ]
4344 else :
4445 s .append (strings [0 ])
@@ -516,7 +517,55 @@ def check_gc(self, gc):
516517 self .gc .copy_properties (gc )
517518
518519 def draw_arc (self , gcEdge , rgbFace , x , y , width , height , angle1 , angle2 ):
519- print >> sys .stderr , "draw_arc called"
520+ """
521+ Draw an arc using GraphicsContext instance gcEdge, centered at x,y,
522+ with width and height and angles from 0.0 to 360.0
523+ 0 degrees is at 3-o'clock
524+ positive angles are anti-clockwise
525+
526+ If the color rgbFace is not None, fill the arc with it.
527+ """
528+ #print >>sys.stderr, "draw_arc", rgbFace, x, y, width, height, angle1, angle2
529+ # source: agg_bezier_arc.cpp
530+
531+ def arc_to_bezier (cx , cy , rx , ry , angle1 , sweep ):
532+ halfsweep = sweep / 2.0
533+ x0 , y0 = cos (halfsweep ), sin (halfsweep )
534+ tx = (1.0 - x0 ) * 4.0 / 3.0 ;
535+ ty = y0 - tx * x0 / y0 ;
536+ px = x0 , x0 + tx , x0 + tx , x0
537+ py = - y0 , - ty , ty , y0
538+ sn , cs = sin (angle1 + halfsweep ), cos (angle1 + halfsweep )
539+ result = [ (cx + rx * (pxi * cs - pyi * sn ),
540+ cy + ry * (pxi * sn + pyi * cs ))
541+ for pxi , pyi in zip (px , py ) ]
542+ return reduce (lambda x , y : x + y , result )
543+
544+ epsilon = 0.01
545+ angle1 *= pi / 180.0
546+ angle2 *= pi / 180.0
547+ sweep = angle2 - angle1
548+ angle1 = angle1 % (2 * pi )
549+ sweep = min (max (- 2 * pi , sweep ), 2 * pi )
550+
551+ if sweep < 0.0 :
552+ sweep , angle1 , angle2 = - sweep , angle2 , angle1
553+ bp = [ pi / 2.0 * i for i in range (4 ) if pi / 2.0 * i < sweep ]
554+ bp .append (sweep )
555+ subarcs = [ arc_to_bezier (x , y , width / 2.0 , height / 2.0 ,
556+ bp [i ], bp [i + 1 ]- bp [i ])
557+ for i in range (len (bp )- 1 ) ]
558+
559+ self .check_gc (gcEdge )
560+ self .file .write ('%s %s m\n ' % tmap (pdfRepr , subarcs [0 ][0 :2 ]))
561+ for arc in subarcs :
562+ self .file .write (' %s %s %s %s %s %s c\n ' %
563+ tmap (pdfRepr , arc [2 :]))
564+ if rgbFace is not None :
565+ self .file .write (' q %s %s %s rg b Q\n ' %
566+ tmap (pdfRepr , rgbFace ))
567+ else :
568+ self .file .write ('S\n ' )
520569
521570 def draw_image (self , x , y , im , bbox ):
522571 print >> sys .stderr , "draw_image called"
0 commit comments