<s> """<STR_LIT>""" <EOL> from __future__ import division , unicode_literals <EOL> from collections import namedtuple <EOL> from itertools import cycle <EOL> from . . formatting_structure import boxes <EOL> from . import replaced <EOL> from . percentages import resolve_radii_percentages <EOL> Background = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> BackgroundLayer = namedtuple ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def box_rectangle ( box , which_rectangle ) : <EOL> if which_rectangle == '<STR_LIT>' : <EOL> return ( <EOL> box . border_box_x ( ) , <EOL> box . border_box_y ( ) , <EOL> box . border_width ( ) , <EOL> box . border_height ( ) , <EOL> ) <EOL> elif which_rectangle == '<STR_LIT>' : <EOL> return ( <EOL> box . padding_box_x ( ) , <EOL> box . padding_box_y ( ) , <EOL> box . padding_width ( ) , <EOL> box . padding_height ( ) , <EOL> ) <EOL> else : <EOL> assert which_rectangle == '<STR_LIT>' , which_rectangle <EOL> return ( <EOL> box . content_box_x ( ) , <EOL> box . content_box_y ( ) , <EOL> box . width , <EOL> box . height , <EOL> ) <EOL> def layout_box_backgrounds ( page , box , get_image_from_uri ) : <EOL> """<STR_LIT>""" <EOL> resolve_radii_percentages ( box ) <EOL> for child in box . all_children ( ) : <EOL> layout_box_backgrounds ( page , child , get_image_from_uri ) <EOL> style = box . style <EOL> if style . visibility == '<STR_LIT>' : <EOL> box . background = None <EOL> return <EOL> images = [ get_image_from_uri ( value ) if type_ == '<STR_LIT:url>' else value <EOL> for type_ , value in style . background_image ] <EOL> color = style . get_color ( '<STR_LIT>' ) <EOL> if color . alpha == <NUM_LIT:0> and not any ( images ) : <EOL> box . background = None <EOL> return <EOL> box . background = Background ( <EOL> color = color , image_rendering = style . image_rendering , layers = [ <EOL> layout_background_layer ( box , page , style . image_resolution , * layer ) <EOL> for layer in zip ( images , * map ( cycle , [ <EOL> style . background_size , <EOL> style . background_clip , <EOL> style . background_repeat , <EOL> style . background_origin , <EOL> style . background_position , <EOL> style . background_attachment ] ) ) ] ) <EOL> def percentage ( value , refer_to ) : <EOL> """<STR_LIT>""" <EOL> if value == '<STR_LIT>' : <EOL> return value <EOL> elif value . unit == '<STR_LIT>' : <EOL> return value . value <EOL> else : <EOL> assert value . unit == '<STR_LIT:%>' <EOL> return refer_to * value . value / <NUM_LIT:100> <EOL> def layout_background_layer ( box , page , resolution , image , size , clip , repeat , <EOL> origin , position , attachment ) : <EOL> clipped_boxes = [ ] <EOL> painting_area = <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> <EOL> if box is page : <EOL> painting_area = <NUM_LIT:0> , <NUM_LIT:0> , page . margin_width ( ) , page . margin_height ( ) <EOL> clipped_boxes = [ box . rounded_border_box ( ) ] <EOL> elif isinstance ( box , boxes . TableRowGroupBox ) : <EOL> clipped_boxes = [ ] <EOL> total_height = <NUM_LIT:0> <EOL> for row in box . children : <EOL> if row . children : <EOL> clipped_boxes += [ <EOL> cell . rounded_border_box ( ) for cell in row . children ] <EOL> total_height = max ( total_height , max ( <EOL> cell . border_box_y ( ) + cell . border_height ( ) <EOL> for cell in row . children ) ) <EOL> painting_area = [ <EOL> box . border_box_x ( ) , box . border_box_y ( ) , <EOL> box . border_box_x ( ) + box . border_width ( ) , total_height ] <EOL> elif isinstance ( box , boxes . TableRowBox ) : <EOL> if box . children : <EOL> clipped_boxes = [ <EOL> cell . rounded_border_box ( ) for cell in box . children ] <EOL> height = max ( <EOL> cell . border_height ( ) for cell in box . children ) <EOL> painting_area = [ <EOL> box . border_box_x ( ) , box . border_box_y ( ) , <EOL> box . border_box_x ( ) + box . border_width ( ) , <EOL> box . border_box_y ( ) + height ] <EOL> elif isinstance ( box , ( boxes . TableColumnGroupBox , boxes . TableColumnBox ) ) : <EOL> cells = box . get_cells ( ) <EOL> if cells : <EOL> clipped_boxes = [ cell . rounded_border_box ( ) for cell in cells ] <EOL> max_x = max ( <EOL> cell . border_box_x ( ) + cell . border_width ( ) <EOL> for cell in cells ) <EOL> painting_area = [ <EOL> box . border_box_x ( ) , box . border_box_y ( ) , <EOL> max_x - box . border_box_x ( ) , <EOL> box . border_box_y ( ) + box . border_height ( ) ] <EOL> else : <EOL> painting_area = box_rectangle ( box , clip ) <EOL> if clip == '<STR_LIT>' : <EOL> clipped_boxes = [ box . rounded_border_box ( ) ] <EOL> elif clip == '<STR_LIT>' : <EOL> clipped_boxes = [ box . rounded_padding_box ( ) ] <EOL> else : <EOL> assert clip == '<STR_LIT>' , clip <EOL> clipped_boxes = [ box . rounded_content_box ( ) ] <EOL> if image is None or <NUM_LIT:0> in image . get_intrinsic_size ( <NUM_LIT:1> , <NUM_LIT:1> ) : <EOL> return BackgroundLayer ( <EOL> image = None , unbounded = ( box is page ) , painting_area = painting_area , <EOL> size = '<STR_LIT>' , position = '<STR_LIT>' , repeat = '<STR_LIT>' , <EOL> positioning_area = '<STR_LIT>' , clipped_boxes = clipped_boxes ) <EOL> if attachment == '<STR_LIT>' : <EOL> positioning_area = box_rectangle ( page , '<STR_LIT>' ) <EOL> else : <EOL> positioning_area = box_rectangle ( box , origin ) <EOL> positioning_x , positioning_y , positioning_width , positioning_height = ( <EOL> positioning_area ) <EOL> painting_x , painting_y , painting_width , painting_height = ( <EOL> painting_area ) <EOL> if size == '<STR_LIT>' : <EOL> image_width , image_height = replaced . cover_constraint_image_sizing ( <EOL> positioning_width , positioning_height , image . intrinsic_ratio ) <EOL> elif size == '<STR_LIT>' : <EOL> image_width , image_height = replaced . contain_constraint_image_sizing ( <EOL> positioning_width , positioning_height , image . intrinsic_ratio ) <EOL> else : <EOL> size_width , size_height = size <EOL> iwidth , iheight = image . get_intrinsic_size ( <EOL> resolution , box . style . font_size ) <EOL> image_width , image_height = replaced . default_image_sizing ( <EOL> iwidth , iheight , image . intrinsic_ratio , <EOL> percentage ( size_width , positioning_width ) , <EOL> percentage ( size_height , positioning_height ) , <EOL> positioning_width , positioning_height ) <EOL> origin_x , position_x , origin_y , position_y = position <EOL> ref_x = positioning_width - image_width <EOL> ref_y = positioning_height - image_height <EOL> position_x = percentage ( position_x , ref_x ) <EOL> position_y = percentage ( position_y , ref_y ) <EOL> if origin_x == '<STR_LIT:right>' : <EOL> position_x = ref_x - position_x <EOL> if origin_y == '<STR_LIT>' : <EOL> position_y = ref_y - position_y <EOL> repeat_x , repeat_y = repeat <EOL> if repeat_x == '<STR_LIT>' : <EOL> n_repeats = max ( <NUM_LIT:1> , round ( positioning_width / image_width ) ) <EOL> new_width = positioning_width / n_repeats <EOL> position_x = <NUM_LIT:0> <EOL> if repeat_y != '<STR_LIT>' and size [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> image_height *= new_width / image_width <EOL> image_width = new_width <EOL> if repeat_y == '<STR_LIT>' : <EOL> n_repeats = max ( <NUM_LIT:1> , round ( positioning_height / image_height ) ) <EOL> new_height = positioning_height / n_repeats <EOL> position_y = <NUM_LIT:0> <EOL> if repeat_x != '<STR_LIT>' and size [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> image_width *= new_height / image_height <EOL> image_height = new_height <EOL> return BackgroundLayer ( <EOL> image = image , <EOL> size = ( image_width , image_height ) , <EOL> position = ( position_x , position_y ) , <EOL> repeat = repeat , <EOL> unbounded = ( box is page ) , <EOL> painting_area = painting_area , <EOL> positioning_area = positioning_area , <EOL> clipped_boxes = clipped_boxes ) <EOL> def set_canvas_background ( page ) : <EOL> """<STR_LIT>""" <EOL> assert not isinstance ( page . children [ <NUM_LIT:0> ] , boxes . MarginBox ) <EOL> root_box = page . children [ <NUM_LIT:0> ] <EOL> chosen_box = root_box <EOL> if root_box . element_tag . lower ( ) == '<STR_LIT:html>' and root_box . background is None : <EOL> for child in root_box . children : <EOL> if child . element_tag . lower ( ) == '<STR_LIT:body>' : <EOL> chosen_box = child <EOL> break <EOL> if chosen_box . background : <EOL> painting_area = box_rectangle ( page , '<STR_LIT>' ) <EOL> page . canvas_background = chosen_box . background . _replace ( <EOL> layers = [ <EOL> l . _replace ( painting_area = painting_area ) <EOL> for l in chosen_box . background . layers ] ) <EOL> chosen_box . background = None <EOL> else : <EOL> page . canvas_background = None <EOL> def layout_backgrounds ( page , get_image_from_uri ) : <EOL> layout_box_backgrounds ( page , page , get_image_from_uri ) <EOL> set_canvas_background ( page ) </s>
