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

Kill Document in pdf.py and text.py

This commit is contained in:
Simon Sapin 2012-07-12 16:10:30 +02:00
parent 30a5708373
commit cca18bdfe9
5 changed files with 11 additions and 11 deletions

View File

@ -168,7 +168,7 @@ class Document(object):
surface.show_page()
surface.finish()
pdf.write_pdf_metadata(self, file_obj)
pdf.write_pdf_metadata(self.pages, file_obj)
if target is None:
return file_obj.getvalue()

View File

@ -165,7 +165,7 @@ def inline_preferred_minimum_width(document, box, outer=True, skip_stack=None,
current_line = inline_preferred_minimum_width(
document, child, skip_stack=skip_stack, first_line=first_line)
elif isinstance(child, boxes.TextBox):
widths = text.line_widths(document, child, width=0, skip=skip)
widths = text.line_widths(child, document.enable_hinting, width=0)
if first_line:
return next(widths)
else:
@ -188,7 +188,8 @@ def inline_preferred_width(document, box, outer=True):
# TODO: handle forced line breaks
current_line += inline_preferred_width(document, child)
elif isinstance(child, boxes.TextBox):
lines = list(text.line_widths(document, child, width=None))
lines = list(text.line_widths(
child, document.enable_hinting, width=None))
assert lines
# The first text line goes on the current line
current_line += lines[0]

View File

@ -348,7 +348,7 @@ def process_bookmarks(raw_bookmarks):
return root, bookmark_list
def gather_metadata(document):
def gather_metadata(pages):
"""Traverse the layout tree (boxes) to find all metadata."""
def walk(box):
# "Border area. That's the area that hit-testing is done on."
@ -381,7 +381,7 @@ def gather_metadata(document):
bookmarks = []
links_by_page = []
anchors = {}
for page_index, page in enumerate(document.pages):
for page_index, page in enumerate(pages):
# cairo coordinates are pixels right and down from the top-left corner
# PDF coordinates are points right and up from the bottom-left corner
matrix = cairo.Matrix(
@ -416,8 +416,8 @@ def gather_metadata(document):
return process_bookmarks(bookmarks), resolved_links_by_page
def write_pdf_metadata(document, fileobj):
bookmarks, links = gather_metadata(document)
def write_pdf_metadata(pages, fileobj):
bookmarks, links = gather_metadata(pages)
pdf = PDFFile(fileobj)
pdf.overwrite_object(pdf.info.object_number, pdf_format(

View File

@ -44,7 +44,7 @@ def get_metadata(html, base_url=resource_filename('<inline HTML>')):
document = TestPDFDocument(html, base_url=base_url,
user_stylesheets=[CSS(
string='@page { size: 500pt 1000pt; margin: 50pt }')])
return pdf.gather_metadata(document)
return pdf.gather_metadata(document.pages)
def get_bookmarks(html, structure_only=False):

View File

@ -114,13 +114,12 @@ def show_first_line(cairo_context, pango_layout, hinting):
PangoCairo.show_layout_line(cairo_context, lines[0])
def line_widths(document, box, width, skip=None):
def line_widths(box, enable_hinting, width, skip=None):
"""Return the width for each line."""
# TODO: without the lstrip, we get an extra empty line at the beginning. Is
# there a better solution to avoid that?
layout = create_layout(
box.text[(skip or 0):].lstrip(), box.style,
document.enable_hinting, width)
box.text[(skip or 0):].lstrip(), box.style, enable_hinting, width)
for line in layout.get_lines_readonly():
_ink_extents, logical_extents = line.get_extents()
yield Pango.units_to_double(logical_extents.width)