<s> import sys <EOL> from utils import mult_matrix , translate_matrix <EOL> from utils import enc , bbox2str <EOL> from pdffont import PDFUnicodeNotDefined <EOL> class PDFDevice ( object ) : <EOL> debug = <NUM_LIT:0> <EOL> def __init__ ( self , rsrcmgr ) : <EOL> self . rsrcmgr = rsrcmgr <EOL> self . ctm = None <EOL> return <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> def close ( self ) : <EOL> return <EOL> def set_ctm ( self , ctm ) : <EOL> self . ctm = ctm <EOL> return <EOL> def begin_tag ( self , tag , props = None ) : <EOL> return <EOL> def end_tag ( self ) : <EOL> return <EOL> def do_tag ( self , tag , props = None ) : <EOL> return <EOL> def begin_page ( self , page , ctm ) : <EOL> return <EOL> def end_page ( self , page ) : <EOL> return <EOL> def begin_figure ( self , name , bbox , matrix ) : <EOL> return <EOL> def end_figure ( self , name ) : <EOL> return <EOL> def paint_path ( self , graphicstate , stroke , fill , evenodd , path ) : <EOL> return <EOL> def render_image ( self , name , stream ) : <EOL> return <EOL> def render_string ( self , textstate , seq ) : <EOL> return <EOL> class PDFTextDevice ( PDFDevice ) : <EOL> def render_string ( self , textstate , seq ) : <EOL> matrix = mult_matrix ( textstate . matrix , self . ctm ) <EOL> font = textstate . font <EOL> fontsize = textstate . fontsize <EOL> scaling = textstate . scaling * <NUM_LIT> <EOL> charspace = textstate . charspace * scaling <EOL> wordspace = textstate . wordspace * scaling <EOL> rise = textstate . rise <EOL> if font . is_multibyte ( ) : <EOL> wordspace = <NUM_LIT:0> <EOL> dxscale = <NUM_LIT> * fontsize * scaling <EOL> if font . is_vertical ( ) : <EOL> textstate . linematrix = self . render_string_vertical ( <EOL> seq , matrix , textstate . linematrix , font , fontsize , <EOL> scaling , charspace , wordspace , rise , dxscale ) <EOL> else : <EOL> textstate . linematrix = self . render_string_horizontal ( <EOL> seq , matrix , textstate . linematrix , font , fontsize , <EOL> scaling , charspace , wordspace , rise , dxscale ) <EOL> return <EOL> def render_string_horizontal ( self , seq , matrix , ( x , y ) , <EOL> font , fontsize , scaling , charspace , wordspace , rise , dxscale ) : <EOL> needcharspace = False <EOL> for obj in seq : <EOL> if isinstance ( obj , int ) or isinstance ( obj , float ) : <EOL> x -= obj * dxscale <EOL> needcharspace = True <EOL> else : <EOL> for cid in font . decode ( obj ) : <EOL> if needcharspace : <EOL> x += charspace <EOL> x += self . render_char ( translate_matrix ( matrix , ( x , y ) ) , <EOL> font , fontsize , scaling , rise , cid ) <EOL> if cid == <NUM_LIT:32> and wordspace : <EOL> x += wordspace <EOL> needcharspace = True <EOL> return ( x , y ) <EOL> def render_string_vertical ( self , seq , matrix , ( x , y ) , <EOL> font , fontsize , scaling , charspace , wordspace , rise , dxscale ) : <EOL> needcharspace = False <EOL> for obj in seq : <EOL> if isinstance ( obj , int ) or isinstance ( obj , float ) : <EOL> y -= obj * dxscale <EOL> needcharspace = True <EOL> else : <EOL> for cid in font . decode ( obj ) : <EOL> if needcharspace : <EOL> y += charspace <EOL> y += self . render_char ( translate_matrix ( matrix , ( x , y ) ) , <EOL> font , fontsize , scaling , rise , cid ) <EOL> if cid == <NUM_LIT:32> and wordspace : <EOL> y += wordspace <EOL> needcharspace = True <EOL> return ( x , y ) <EOL> def render_char ( self , matrix , font , fontsize , scaling , rise , cid ) : <EOL> return <NUM_LIT:0> <EOL> class TagExtractor ( PDFDevice ) : <EOL> def __init__ ( self , rsrcmgr , outfp , codec = '<STR_LIT:utf-8>' , debug = <NUM_LIT:0> ) : <EOL> PDFDevice . __init__ ( self , rsrcmgr ) <EOL> self . outfp = outfp <EOL> self . codec = codec <EOL> self . debug = debug <EOL> self . pageno = <NUM_LIT:0> <EOL> self . _stack = [ ] <EOL> return <EOL> def render_string ( self , textstate , seq ) : <EOL> font = textstate . font <EOL> text = '<STR_LIT>' <EOL> for obj in seq : <EOL> if not isinstance ( obj , str ) : continue <EOL> chars = font . decode ( obj ) <EOL> for cid in chars : <EOL> try : <EOL> char = font . to_unichr ( cid ) <EOL> text += char <EOL> except PDFUnicodeNotDefined : <EOL> pass <EOL> self . outfp . write ( enc ( text , self . codec ) ) <EOL> return <EOL> def begin_page ( self , page , ctm ) : <EOL> self . outfp . write ( '<STR_LIT>' % <EOL> ( self . pageno , bbox2str ( page . mediabox ) , page . rotate ) ) <EOL> return <EOL> def end_page ( self , page ) : <EOL> self . outfp . write ( '<STR_LIT>' ) <EOL> self . pageno += <NUM_LIT:1> <EOL> return <EOL> def begin_tag ( self , tag , props = None ) : <EOL> s = '<STR_LIT>' <EOL> if isinstance ( props , dict ) : <EOL> s = '<STR_LIT>' . join ( '<STR_LIT>' % ( enc ( k ) , enc ( str ( v ) ) ) for ( k , v ) <EOL> in sorted ( props . iteritems ( ) ) ) <EOL> self . outfp . write ( '<STR_LIT>' % ( enc ( tag . name ) , s ) ) <EOL> self . _stack . append ( tag ) <EOL> return <EOL> def end_tag ( self ) : <EOL> assert self . _stack <EOL> tag = self . _stack . pop ( - <NUM_LIT:1> ) <EOL> self . outfp . write ( '<STR_LIT>' % enc ( tag . name ) ) <EOL> return <EOL> def do_tag ( self , tag , props = None ) : <EOL> self . begin_tag ( tag , props ) <EOL> self . _stack . pop ( - <NUM_LIT:1> ) <EOL> return </s>
