mirror of
https://github.com/Kozea/WeasyPrint.git
synced 2024-10-04 16:07:57 +03:00
Move percentage function into percentages module
This commit is contained in:
parent
846515b109
commit
9f442be640
@ -1,26 +0,0 @@
|
||||
"""
|
||||
weasyprint.calc
|
||||
-----------------------------
|
||||
|
||||
Resolve percentages into fixed values.
|
||||
|
||||
:copyright: Copyright 2011-2019 Simon Sapin and contributors, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
|
||||
"""
|
||||
|
||||
|
||||
def percentage(value, refer_to):
|
||||
"""Return the percentage of the reference value, or the value unchanged.
|
||||
|
||||
``refer_to`` is the length for 100%. If ``refer_to`` is not a number, it
|
||||
just replaces percentages.
|
||||
|
||||
"""
|
||||
if value is None or value == 'auto':
|
||||
return value
|
||||
elif value.unit == 'px':
|
||||
return value.value
|
||||
else:
|
||||
assert value.unit == '%'
|
||||
return refer_to * value.value / 100.
|
@ -17,7 +17,6 @@ from urllib.parse import unquote, urljoin
|
||||
from tinycss2.color3 import parse_color
|
||||
|
||||
from ..formatting_structure import counters
|
||||
from ..images import LinearGradient, RadialGradient
|
||||
from ..urls import iri_to_uri, url_is_absolute
|
||||
from .properties import Dimension
|
||||
|
||||
@ -562,6 +561,8 @@ def get_resolution(token):
|
||||
|
||||
def get_image(token, base_url):
|
||||
"""Parse an <image> token."""
|
||||
from ..images import LinearGradient, RadialGradient
|
||||
|
||||
if token.type != 'function':
|
||||
parsed_url = get_url(token, base_url)
|
||||
if parsed_url:
|
||||
|
@ -16,7 +16,7 @@ import warnings
|
||||
|
||||
import cairocffi as cairo
|
||||
|
||||
from . import CSS, calc
|
||||
from . import CSS
|
||||
from .css import get_all_computed_styles
|
||||
from .css.targets import TargetCollector
|
||||
from .draw import draw_page, stacked
|
||||
@ -26,6 +26,7 @@ from .formatting_structure.build import build_formatting_structure
|
||||
from .html import W3C_DATE_RE
|
||||
from .images import get_image_from_uri as original_get_image_from_uri
|
||||
from .layout import layout_document
|
||||
from .layout.percentages import percentage
|
||||
from .logger import LOGGER, PROGRESS_LOGGER
|
||||
from .pdf import write_pdf_metadata
|
||||
|
||||
@ -52,8 +53,8 @@ def _get_matrix(box):
|
||||
border_width = box.border_width()
|
||||
border_height = box.border_height()
|
||||
origin_x, origin_y = box.style['transform_origin']
|
||||
offset_x = calc.percentage(origin_x, border_width)
|
||||
offset_y = calc.percentage(origin_y, border_height)
|
||||
offset_x = percentage(origin_x, border_width)
|
||||
offset_y = percentage(origin_y, border_height)
|
||||
origin_x = box.border_box_x() + offset_x
|
||||
origin_y = box.border_box_y() + offset_y
|
||||
|
||||
@ -67,8 +68,8 @@ def _get_matrix(box):
|
||||
elif name == 'translate':
|
||||
translate_x, translate_y = args
|
||||
matrix.translate(
|
||||
calc.percentage(translate_x, border_width),
|
||||
calc.percentage(translate_y, border_height),
|
||||
percentage(translate_x, border_width),
|
||||
percentage(translate_y, border_height),
|
||||
)
|
||||
else:
|
||||
if name == 'skewx':
|
||||
|
@ -17,7 +17,7 @@ import cairocffi
|
||||
import cairosvg.parser
|
||||
import cairosvg.surface
|
||||
|
||||
from . import calc
|
||||
from .layout.percentages import percentage
|
||||
from .logger import LOGGER
|
||||
from .urls import URLFetchingError, fetch
|
||||
|
||||
@ -245,8 +245,8 @@ def process_color_stops(gradient_line_size, positions):
|
||||
Return processed color stops, as a list of floats in px.
|
||||
|
||||
"""
|
||||
positions = [calc.percentage(position, gradient_line_size)
|
||||
for position in positions]
|
||||
positions = [
|
||||
percentage(position, gradient_line_size) for position in positions]
|
||||
# First and last default to 100%
|
||||
if positions[0] is None:
|
||||
positions[0] = 0
|
||||
@ -434,8 +434,8 @@ class RadialGradient(Gradient):
|
||||
if len(self.colors) == 1:
|
||||
return 1, 'solid', self.colors[0], [], []
|
||||
origin_x, center_x, origin_y, center_y = self.center
|
||||
center_x = calc.percentage(center_x, width)
|
||||
center_y = calc.percentage(center_y, height)
|
||||
center_x = percentage(center_x, width)
|
||||
center_y = percentage(center_y, height)
|
||||
if origin_x == 'right':
|
||||
center_x = width - center_x
|
||||
if origin_y == 'bottom':
|
||||
@ -502,8 +502,8 @@ class RadialGradient(Gradient):
|
||||
def _resolve_size(self, width, height, center_x, center_y):
|
||||
if self.size_type == 'explicit':
|
||||
size_x, size_y = self.size
|
||||
size_x = calc.percentage(size_x, width)
|
||||
size_y = calc.percentage(size_y, height)
|
||||
size_x = percentage(size_x, width)
|
||||
size_y = percentage(size_y, height)
|
||||
return size_x, size_y
|
||||
left = abs(center_x)
|
||||
right = abs(width - center_x)
|
||||
|
@ -10,10 +10,9 @@
|
||||
from collections import namedtuple
|
||||
from itertools import cycle
|
||||
|
||||
from .. import calc
|
||||
from ..formatting_structure import boxes
|
||||
from . import replaced
|
||||
from .percentages import resolve_radii_percentages
|
||||
from .percentages import percentage, resolve_radii_percentages
|
||||
|
||||
Background = namedtuple('Background', 'color, layers, image_rendering')
|
||||
BackgroundLayer = namedtuple(
|
||||
@ -167,15 +166,15 @@ def layout_background_layer(box, page, resolution, image, size, clip, repeat,
|
||||
resolution, box.style['font_size'])
|
||||
image_width, image_height = replaced.default_image_sizing(
|
||||
iwidth, iheight, image.intrinsic_ratio,
|
||||
calc.percentage(size_width, positioning_width),
|
||||
calc.percentage(size_height, positioning_height),
|
||||
percentage(size_width, positioning_width),
|
||||
percentage(size_height, positioning_height),
|
||||
positioning_width, positioning_height)
|
||||
|
||||
origin_x, position_x, origin_y, position_y = position
|
||||
ref_x = positioning_width - image_width
|
||||
ref_y = positioning_height - image_height
|
||||
position_x = calc.percentage(position_x, ref_x)
|
||||
position_y = calc.percentage(position_y, ref_y)
|
||||
position_x = percentage(position_x, ref_x)
|
||||
position_y = percentage(position_y, ref_y)
|
||||
if origin_x == 'right':
|
||||
position_x = ref_x - position_x
|
||||
if origin_y == 'bottom':
|
||||
|
@ -9,10 +9,25 @@
|
||||
|
||||
"""
|
||||
|
||||
from .. import calc
|
||||
from ..formatting_structure import boxes
|
||||
|
||||
|
||||
def percentage(value, refer_to):
|
||||
"""Return the percentage of the reference value, or the value unchanged.
|
||||
|
||||
``refer_to`` is the length for 100%. If ``refer_to`` is not a number, it
|
||||
just replaces percentages.
|
||||
|
||||
"""
|
||||
if value is None or value == 'auto':
|
||||
return value
|
||||
elif value.unit == 'px':
|
||||
return value.value
|
||||
else:
|
||||
assert value.unit == '%'
|
||||
return refer_to * value.value / 100.
|
||||
|
||||
|
||||
def resolve_one_percentage(box, property_name, refer_to,
|
||||
main_flex_direction=None):
|
||||
"""Set a used length value from a computed length value.
|
||||
@ -24,9 +39,9 @@ def resolve_one_percentage(box, property_name, refer_to,
|
||||
# box.style has computed values
|
||||
value = box.style[property_name]
|
||||
# box attributes are used values
|
||||
percentage = calc.percentage(value, refer_to)
|
||||
setattr(box, property_name, percentage)
|
||||
if property_name in ('min_width', 'min_height') and percentage == 'auto':
|
||||
percent = percentage(value, refer_to)
|
||||
setattr(box, property_name, percent)
|
||||
if property_name in ('min_width', 'min_height') and percent == 'auto':
|
||||
if (main_flex_direction is None or
|
||||
property_name != ('min_%s' % main_flex_direction)):
|
||||
setattr(box, property_name, 0)
|
||||
@ -130,6 +145,6 @@ def resolve_radii_percentages(box):
|
||||
for corner in corners:
|
||||
property_name = 'border_%s_radius' % corner
|
||||
rx, ry = box.style[property_name]
|
||||
rx = calc.percentage(rx, box.border_width())
|
||||
ry = calc.percentage(ry, box.border_height())
|
||||
rx = percentage(rx, box.border_width())
|
||||
ry = percentage(ry, box.border_height())
|
||||
setattr(box, property_name, (rx, ry))
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
"""
|
||||
|
||||
from .. import calc
|
||||
from .percentages import percentage
|
||||
|
||||
|
||||
def image_marker_layout(box):
|
||||
@ -131,8 +131,8 @@ def replacedbox_layout(box):
|
||||
ref_x = box.width - draw_width
|
||||
ref_y = box.height - draw_height
|
||||
|
||||
position_x = calc.percentage(position_x, ref_x)
|
||||
position_y = calc.percentage(position_y, ref_y)
|
||||
position_x = percentage(position_x, ref_x)
|
||||
position_y = percentage(position_y, ref_y)
|
||||
if origin_x == 'right':
|
||||
position_x = ref_x - position_x
|
||||
if origin_y == 'bottom':
|
||||
|
Loading…
Reference in New Issue
Block a user