1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-04 16:07:57 +03:00

Add more computed sizes on boxes, make them methods with docstrings.

This commit is contained in:
Simon Sapin 2011-07-22 14:53:51 +02:00
parent bdcbdabf33
commit 5341bb4dbf
3 changed files with 34 additions and 24 deletions

View File

@ -40,8 +40,8 @@ def draw_background(context, box):
context.rectangle(
box.position_x + box.margin_left,
box.position_y + box.margin_top,
box.border_width,
box.border_height)
box.border_width(),
box.border_height())
context.set_source_colorvalue(bg_color)
context.fill()

View File

@ -137,36 +137,54 @@ class Box(object):
new_box.__dict__.update(self.__dict__)
return new_box
@property
def padding_width(self):
"""Width of the padding box."""
return self.width + self.padding_left + self.padding_right
@property
def padding_height(self):
"""Height of the padding box."""
return self.height + self.padding_top + self.padding_bottom
@property
def border_width(self):
return self.padding_width + self.border_left_width + \
"""Width of the border box."""
return self.padding_width() + self.border_left_width + \
self.border_right_width
@property
def border_height(self):
return self.padding_height + self.border_top_width + \
"""Height of the border box."""
return self.padding_height() + self.border_top_width + \
self.border_bottom_width
@property
def margin_width(self):
"""Width of the margin box (aka. outer box)."""
return self.border_width() + self.margin_left + self.margin_right
def margin_height(self):
"""Height of the margin box (aka. outer box)."""
return self.border_height() + self.margin_top + self.margin_bottom
def horizontal_spacing(self):
"""Sum of all horizontal margins, paddings and borders."""
return self.margin_left + self.margin_right + \
self.padding_left + self.padding_right + \
self.border_left_width + self.border_right_width
@property
def vertical_spacing(self):
"""Sum of all vertical margins, paddings and borders."""
return self.margin_top + self.margin_bottom + \
self.padding_top + self.padding_bottom + \
self.border_top_width + self.border_bottom_width
def content_box_x(self):
"""Absolute horizontal position of the content box."""
return self.position_x + self.margin_left + self.padding_left + \
self.border_left_width
def content_box_y(self):
"""Absolute vertical position of the content box."""
return self.position_y + self.margin_top + self.padding_top + \
self.border_top_width
class PageBox(Box):
"""

View File

@ -17,6 +17,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from .css.utils import get_single_keyword
from .formatting_structure import boxes
from .utils import MultiFunction
import text
@ -69,7 +70,7 @@ def resolve_one_percentage(box, property_name, refer_to):
# A percentage
result = percentage * refer_to / 100.
else:
result = ' '.join(value.cssText for value in values)
result = get_single_keyword(values)
# Other than that, only 'auto' is allowed
# TODO: it is only allowed on some properties. Check this here?
assert result == 'auto'
@ -213,10 +214,7 @@ def block_level_width(box):
box.margin_right = margin_sum - margin_l
# Sanity check
total = (box.margin_left + box.margin_right + box.padding_left +
box.padding_right + box.border_left_width +
box.border_right_width + box.width)
assert total == cb_width
assert box.margin_width() == cb_width
def block_level_height(box):
@ -233,10 +231,8 @@ def block_level_height(box):
if box.margin_bottom == 'auto':
box.margin_bottom = 0
position_x = box.position_x + box.margin_left + box.padding_left + \
box.border_left_width
position_y = box.position_y + box.margin_top + box.padding_top + \
box.border_top_width
position_x = box.content_box_x()
position_y = box.content_box_y()
initial_position_y = position_y
for child in box.children:
# TODO: collapse margins:
@ -246,11 +242,7 @@ def block_level_height(box):
compute_dimensions(child)
child_outer_height = (
child.height + child.margin_top + child.margin_bottom +
child.border_top_width + child.border_bottom_width +
child.padding_top + child.padding_bottom)
position_y += child_outer_height
position_y += child.margin_height()
if box.height == 'auto':
box.height = position_y - initial_position_y