xhtml2pdf 0.2.28__tar.gz → 0.2.29__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. {xhtml2pdf-0.2.28/xhtml2pdf.egg-info → xhtml2pdf-0.2.29}/PKG-INFO +1 -1
  2. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_block_box.py +71 -0
  3. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_css_parser.py +2 -2
  4. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_flex.py +14 -0
  5. xhtml2pdf-0.2.29/tests/test_httpconfig.py +65 -0
  6. xhtml2pdf-0.2.29/tests/test_line_box.py +100 -0
  7. xhtml2pdf-0.2.29/tests/test_line_height.py +56 -0
  8. xhtml2pdf-0.2.29/tests/test_list_margins.py +64 -0
  9. xhtml2pdf-0.2.29/tests/test_margin_boxes.py +268 -0
  10. xhtml2pdf-0.2.29/tests/test_opacity.py +109 -0
  11. xhtml2pdf-0.2.29/tests/test_page_break.py +70 -0
  12. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_parser.py +3 -3
  13. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_print.py +12 -0
  14. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_reportlab_api.py +43 -0
  15. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_security_limits.py +44 -8
  16. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_signatures.py +19 -13
  17. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_tables.py +44 -6
  18. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_tags.py +31 -0
  19. xhtml2pdf-0.2.29/tests/test_text_decoration.py +87 -0
  20. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_utils.py +72 -0
  21. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/__init__.py +1 -1
  22. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/builders/block.py +37 -7
  23. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/builders/flex.py +64 -19
  24. xhtml2pdf-0.2.29/xhtml2pdf/builders/margin_boxes.py +303 -0
  25. xhtml2pdf-0.2.29/xhtml2pdf/builders/opacity.py +76 -0
  26. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/builders/printmarks.py +14 -5
  27. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/builders/signs.py +24 -28
  28. xhtml2pdf-0.2.29/xhtml2pdf/builders/strings.py +110 -0
  29. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/config/httpconfig.py +1 -1
  30. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/context.py +87 -3
  31. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/default.py +23 -1
  32. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/document.py +31 -1
  33. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/paragraph.py +6 -2
  34. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/parser.py +49 -11
  35. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/properties.py +19 -1
  36. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/reportlab_paragraph.py +169 -47
  37. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/tables.py +2 -0
  38. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/tags.py +8 -4
  39. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/util.py +174 -9
  40. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/w3c/css.py +4 -0
  41. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/w3c/cssParser.py +48 -2
  42. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/w3c/generated_content.py +12 -2
  43. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/xhtml2pdf_reportlab.py +108 -4
  44. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29/xhtml2pdf.egg-info}/PKG-INFO +1 -1
  45. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf.egg-info/SOURCES.txt +11 -0
  46. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/LICENSE.txt +0 -0
  47. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/MANIFEST.in +0 -0
  48. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/README.rst +0 -0
  49. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/pyproject.toml +0 -0
  50. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/setup.cfg +0 -0
  51. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_asian_font_support.py +0 -0
  52. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_bidi.py +0 -0
  53. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_block_margins.py +0 -0
  54. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_block_width.py +0 -0
  55. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_cli.py +0 -0
  56. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_css_media_rule.py +0 -0
  57. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_css_variables.py +0 -0
  58. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_document.py +0 -0
  59. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_files.py +0 -0
  60. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_flex_flowable.py +0 -0
  61. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_flex_layout.py +0 -0
  62. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_font_isolation.py +0 -0
  63. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_fonts.py +0 -0
  64. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_frame_align.py +0 -0
  65. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_frame_pages.py +0 -0
  66. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_generated_content.py +0 -0
  67. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_http.py +0 -0
  68. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_inline_block.py +0 -0
  69. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_inline_box.py +0 -0
  70. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_insert_pdf.py +0 -0
  71. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_opentype.py +0 -0
  72. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_optional_dependencies.py +0 -0
  73. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_page_numbering.py +0 -0
  74. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_page_padding.py +0 -0
  75. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_paragraph.py +0 -0
  76. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_paragraph_height.py +0 -0
  77. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_pdf.py +0 -0
  78. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_position.py +0 -0
  79. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_reportlab.py +0 -0
  80. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_reportlab_paragraph.py +0 -0
  81. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_resource_policy.py +0 -0
  82. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_right_to_left_font_support.py +0 -0
  83. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_selectors.py +0 -0
  84. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_special_css.py +0 -0
  85. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_table_border_radius.py +0 -0
  86. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_table_css.py +0 -0
  87. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_update_face_name.py +0 -0
  88. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_watermarks.py +0 -0
  89. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_word_breaking.py +0 -0
  90. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/tests/test_wsgi.py +0 -0
  91. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/__main__.py +0 -0
  92. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/_bidi_data.py +0 -0
  93. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/bidi.py +0 -0
  94. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/builders/__init__.py +0 -0
  95. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/builders/flex_layout.py +0 -0
  96. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/builders/inserts.py +0 -0
  97. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/builders/pdfx.py +0 -0
  98. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/builders/position.py +0 -0
  99. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/builders/preflight.py +0 -0
  100. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/builders/watermarks.py +0 -0
  101. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/charts.py +0 -0
  102. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/config/__init__.py +0 -0
  103. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/config/resources.py +0 -0
  104. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/files.py +0 -0
  105. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/opentype.py +0 -0
  106. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/pdf.py +0 -0
  107. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/pisa.py +0 -0
  108. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/w3c/__init__.py +0 -0
  109. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/w3c/cssDOMElementInterface.py +0 -0
  110. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/w3c/cssSpecial.py +0 -0
  111. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/w3c/css_variables.py +0 -0
  112. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf/wsgi.py +0 -0
  113. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf.egg-info/dependency_links.txt +0 -0
  114. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf.egg-info/entry_points.txt +0 -0
  115. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf.egg-info/requires.txt +0 -0
  116. {xhtml2pdf-0.2.28 → xhtml2pdf-0.2.29}/xhtml2pdf.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xhtml2pdf
3
- Version: 0.2.28
3
+ Version: 0.2.29
4
4
  Summary: PDF generator using HTML and CSS
5
5
  Author-email: Dirk Holtwick <dirk.holtwick@gmail.com>
6
6
  Maintainer-email: Luis Zarate <luisza14@gmail.com>, Timo Brembeck <opensource@timo.brembeck.email>
@@ -269,6 +269,77 @@ class PageTestCase(TestCase):
269
269
  reader = PdfReader(io.BytesIO(pdf))
270
270
  self.assertIn("Chapter", reader.pages[0].extract_text())
271
271
 
272
+ def test_a_long_table_in_a_box_runs_on_across_pages(self) -> None:
273
+ """A box's background or padding does not shrink the table to a page."""
274
+ rows = "".join(
275
+ f"<tr><td>row {i}</td><td>value {i}</td></tr>" for i in range(200)
276
+ )
277
+ content = f"<p>Intro.</p><table>{rows}</table>"
278
+
279
+ def pages(html: str) -> list[str]:
280
+ dest = io.BytesIO()
281
+ pisa.CreatePDF(f"<html><body>{html}</body></html>", dest=dest)
282
+ return [
283
+ p.extract_text() for p in PdfReader(io.BytesIO(dest.getvalue())).pages
284
+ ]
285
+
286
+ plain = pages(f"<div>{content}</div>")
287
+ self.assertEqual(3, len(plain))
288
+ for style in ("background: #ffffff", "padding: 0 20px"):
289
+ with self.subTest(style=style):
290
+ self.assertEqual(plain, pages(f'<div style="{style}">{content}</div>'))
291
+ nested = pages(
292
+ '<div style="padding: 0 20px">'
293
+ f'<div style="border: 1px solid black">{content}</div></div>'
294
+ )
295
+ self.assertEqual(3, len(nested))
296
+ self.assertIn("row 199", nested[-1])
297
+
298
+ def test_the_bottom_padding_does_not_go_on_alone(self) -> None:
299
+ """
300
+ Content that fits at the foot, with no room for the padding under it,
301
+ does not leave a slice of empty box on the next page.
302
+ """
303
+
304
+ def pages(html: str) -> list[list[str]]:
305
+ dest = io.BytesIO()
306
+ pisa.CreatePDF(html, dest=dest)
307
+ return [
308
+ p.extract_text().split()
309
+ for p in PdfReader(io.BytesIO(dest.getvalue())).pages
310
+ ]
311
+
312
+ note = '<div style="padding: 7pt 9pt; background: #eee">{}</div><p>after</p>'
313
+ # The frame of an A4 page is 785.2pt; the note's text ends 18.25pt in.
314
+ spacer = '<div style="height: 764pt"></div>'
315
+ boxes, _, _ = render(spacer + note.format("<p style='margin: 0'>note</p>"))
316
+ # One box, on page two, whole: a browser moves it too.
317
+ self.assertEqual([2], [b.page for b in boxes if b.style.backColor])
318
+ self.assertEqual(
319
+ [[], ["note", "after"]],
320
+ pages(spacer + note.format("<p style='margin: 0'>note</p>")),
321
+ )
322
+ # With lines to spare, the last goes on with the padding.
323
+ lines = "<p style='margin: 0'>one<br/>two<br/>three</p>"
324
+ self.assertEqual(
325
+ [["one", "two"], ["three", "after"]],
326
+ pages('<div style="height: 740pt"></div>' + note.format(lines)),
327
+ )
328
+
329
+ def test_a_declared_height_that_does_not_fit_keeps_the_box(self) -> None:
330
+ """
331
+ Content and padding fit, only the rest of a declared height does not:
332
+ the box stays on its page, as a full-page cover's band does.
333
+ """
334
+ spacer = '<div style="height: 700pt"></div>'
335
+ band = (
336
+ '<div style="padding: 7pt; height: 90pt; background: #eee">'
337
+ "<p style='margin: 0'>band</p></div>"
338
+ )
339
+ boxes, _, pdf = render(spacer + band)
340
+ self.assertEqual([1], sorted({b.page for b in boxes if b.style.backColor}))
341
+ self.assertEqual(1, len(PdfReader(io.BytesIO(pdf)).pages))
342
+
272
343
  def assertInside(self, box: Box, table: Drawn) -> None:
273
344
  self.assertEqual(box.page, table.page)
274
345
  self.assertGreaterEqual(table.x, box.x)
@@ -537,7 +537,7 @@ class SourceTest(ParserTestCase):
537
537
  def test_unimplemented_properties_are_named_by_source(self) -> None:
538
538
  output, _ = self._render(
539
539
  '<style>p { box-shadow: 1px 1px red }</style><link rel="stylesheet"'
540
- ' href="ext.css"><p style="opacity: 0.5; width: min(1pt, 2pt)">y</p>',
540
+ ' href="ext.css"><p style="cursor: help; width: min(1pt, 2pt)">y</p>',
541
541
  {"ext.css": "p { float: left }"},
542
542
  )
543
543
  unimplemented = [
@@ -554,7 +554,7 @@ class SourceTest(ParserTestCase):
554
554
  self.assertTrue(ext.endswith(": float"), ext)
555
555
  # A style attribute used to be read without a word about it.
556
556
  self.assertTrue(attribute.startswith("the style attribute of <p>: "))
557
- self.assertTrue(attribute.endswith(": opacity"), attribute)
557
+ self.assertTrue(attribute.endswith(": cursor"), attribute)
558
558
  self.assertIn(
559
559
  "the style attribute of <p>: Ignoring CSS declarations whose value"
560
560
  " xhtml2pdf cannot evaluate: width: min()",
@@ -322,6 +322,20 @@ class FlexRenderTest(TestCase):
322
322
  self.assertIn("SIDE", text)
323
323
  self.assertIn("CELL", text)
324
324
 
325
+ def test_a_long_table_item_keeps_its_rows_on_one_line(self) -> None:
326
+ """Its columns follow their content, so no cell wraps on later pages."""
327
+ rows = "".join(
328
+ f"<tr><td>row {i}</td><td>value {i}</td></tr>" for i in range(200)
329
+ )
330
+ table = f"<table>{rows}</table>"
331
+ plain = [p.extract_text() for p in self._render(f"<div>{table}</div>").pages]
332
+ for direction in ("row", "column"):
333
+ with self.subTest(direction=direction):
334
+ reader = self._render(
335
+ f"<div style='display:flex; flex-direction:{direction}'>{table}</div>"
336
+ )
337
+ self.assertEqual(plain, [p.extract_text() for p in reader.pages])
338
+
325
339
 
326
340
  class FlexWrapTest(TestCase):
327
341
  def test_wrap_makes_lines(self) -> None:
@@ -0,0 +1,65 @@
1
+ from __future__ import annotations
2
+
3
+ import ssl
4
+ from unittest import TestCase
5
+
6
+ from xhtml2pdf.config.httpconfig import HttpConfig
7
+
8
+
9
+ class HttpConfigTest(TestCase):
10
+ def test_default_timeout(self) -> None:
11
+ self.assertEqual({"timeout": 5}, HttpConfig())
12
+
13
+ def test_constructor_arguments_are_kept(self) -> None:
14
+ config = HttpConfig(source_address=("127.0.0.1", 0))
15
+ self.assertEqual(("127.0.0.1", 0), config["source_address"])
16
+
17
+ def test_a_timeout_given_to_the_constructor_is_kept(self) -> None:
18
+ self.assertEqual(30, HttpConfig(timeout=30)["timeout"])
19
+ self.assertEqual(30, HttpConfig({"timeout": 30})["timeout"])
20
+
21
+ def test_http_options_are_stored_without_prefix(self) -> None:
22
+ for option in ("--http_timeout", "-http_timeout", "http_timeout"):
23
+ with self.subTest(option=option):
24
+ config = HttpConfig()
25
+ self.assertTrue(config.is_http_config(option, 10))
26
+ self.assertEqual(10, config["timeout"])
27
+
28
+ def test_http_options_with_other_keys(self) -> None:
29
+ config = HttpConfig()
30
+ self.assertTrue(config.is_http_config("--http_key_file", "client.key"))
31
+ self.assertTrue(config.is_http_config("--http_cert_file", "client.crt"))
32
+ self.assertEqual("client.key", config["key_file"])
33
+ self.assertEqual("client.crt", config["cert_file"])
34
+
35
+ def test_non_http_options_are_ignored(self) -> None:
36
+ for option in ("--encoding", "-q", "debug"):
37
+ with self.subTest(option=option):
38
+ config = HttpConfig()
39
+ self.assertFalse(config.is_http_config(option, "value"))
40
+ self.assertEqual({"timeout": 5}, config)
41
+
42
+ def test_nosslcheck_creates_unverified_context(self) -> None:
43
+ config = HttpConfig()
44
+ self.assertTrue(config.is_http_config("--http_nosslcheck", ""))
45
+
46
+ self.assertNotIn("nosslcheck", config)
47
+ context = config["context"]
48
+ self.assertIsInstance(context, ssl.SSLContext)
49
+ self.assertEqual(ssl.CERT_NONE, context.verify_mode)
50
+ self.assertFalse(context.check_hostname)
51
+
52
+ def test_save_keys_stores_value(self) -> None:
53
+ config = HttpConfig()
54
+ config.save_keys("timeout", 1)
55
+ self.assertEqual(1, config["timeout"])
56
+
57
+ def test_repr_lists_every_item(self) -> None:
58
+ config = HttpConfig()
59
+ config["cert_file"] = "client.crt"
60
+ self.assertEqual("'timeout' = 5, 'cert_file' = 'client.crt', ", repr(config))
61
+
62
+ def test_repr_of_empty_config(self) -> None:
63
+ config = HttpConfig()
64
+ config.clear()
65
+ self.assertEqual("", repr(config))
@@ -0,0 +1,100 @@
1
+ """Where a paragraph's text sits in its lines: CSS half-leading."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import io
6
+ from itertools import pairwise
7
+ from unittest import TestCase
8
+
9
+ from pypdf import PdfReader
10
+ from reportlab.pdfbase.pdfmetrics import getAscentDescent
11
+
12
+ from xhtml2pdf import pisa
13
+ from xhtml2pdf.document import pisaStory
14
+ from xhtml2pdf.xhtml2pdf_reportlab import PmlParagraph
15
+
16
+ FONT_SIZE = 10.0
17
+ #: Top of the default frame: a 1cm page margin.
18
+ FRAME_TOP = 28.3465
19
+
20
+
21
+ def first_paragraph(html: str) -> PmlParagraph:
22
+ story = pisaStory(html).story
23
+ para = next(f for f in story if isinstance(f, PmlParagraph))
24
+ para.wrap(400, 800)
25
+ return para
26
+
27
+
28
+ def baselines(html: str) -> list[float]:
29
+ """Distance from the top of the page to each baseline, in stream order."""
30
+ out = io.BytesIO()
31
+ result = pisa.CreatePDF(html, dest=out)
32
+ assert not result.err
33
+ page = PdfReader(io.BytesIO(out.getvalue())).pages[0]
34
+ height = float(page.mediabox.height)
35
+ found: list[float] = []
36
+
37
+ def visit(text, cm, tm, _font, _size) -> None:
38
+ if text.strip():
39
+ found.append(round(height - (tm[5] * cm[3] + cm[5]), 3))
40
+
41
+ page.extract_text(visitor_text=visit)
42
+ return found
43
+
44
+
45
+ class HalfLeadingTest(TestCase):
46
+ @staticmethod
47
+ def expected_first_baseline(leading: float) -> float:
48
+ ascent, descent = getAscentDescent("Helvetica", FONT_SIZE)
49
+ return ascent + (leading - (ascent - descent)) / 2
50
+
51
+ def test_the_leading_is_shared_above_and_below(self) -> None:
52
+ for line_height, leading in (("150%", 15.0), ("100%", 10.0), ("200%", 20.0)):
53
+ with self.subTest(line_height=line_height):
54
+ para = first_paragraph(
55
+ f'<p style="font-size: {FONT_SIZE}pt; line-height: {line_height}">'
56
+ "one line</p>"
57
+ )
58
+ self.assertAlmostEqual(
59
+ self.expected_first_baseline(leading),
60
+ para._firstBaseline(),
61
+ places=3,
62
+ )
63
+
64
+ def test_plain_lines_keep_their_height(self) -> None:
65
+ para = first_paragraph(
66
+ f'<p style="font-size: {FONT_SIZE}pt; line-height: 150%">'
67
+ + "word " * 300
68
+ + "</p>"
69
+ )
70
+ lines = len(para.blPara.lines)
71
+ self.assertGreater(lines, 1)
72
+ self.assertAlmostEqual(lines * 15.0, para.height, places=3)
73
+
74
+ def test_lines_step_by_the_leading(self) -> None:
75
+ found = baselines(
76
+ f'<p style="font-size: {FONT_SIZE}pt; line-height: 150%">'
77
+ + "word " * 300
78
+ + "</p>"
79
+ )
80
+ steps = {round(b - a, 3) for a, b in pairwise(found)}
81
+ self.assertEqual({15.0}, steps)
82
+
83
+ def test_a_bigger_font_still_grows_its_line(self) -> None:
84
+ plain = first_paragraph(f'<p style="font-size: {FONT_SIZE}pt">x</p>')
85
+ mixed = first_paragraph(
86
+ f'<p style="font-size: {FONT_SIZE}pt">x <span style="font-size: 30pt">'
87
+ "BIG</span> x</p>"
88
+ )
89
+ self.assertGreater(mixed.height, plain.height)
90
+ big_ascent = getAscentDescent("Helvetica", 30)[0]
91
+ self.assertGreaterEqual(mixed._firstBaseline(), big_ascent)
92
+
93
+ def test_the_drawn_text_is_where_the_paragraph_says(self) -> None:
94
+ html = (
95
+ '<div style="font-size: 10pt; line-height: 150%">'
96
+ "<p style='margin: 0'>first</p></div>"
97
+ )
98
+ para = first_paragraph(html)
99
+ top = baselines(html)[0]
100
+ self.assertAlmostEqual(FRAME_TOP + para._firstBaseline(), top, places=2)
@@ -0,0 +1,56 @@
1
+ """line-height, computed and inherited as CSS 2.1 10.8 says."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from unittest import TestCase
6
+
7
+ from xhtml2pdf.document import pisaStory
8
+ from xhtml2pdf.util import leadingFrom, lineHeight
9
+ from xhtml2pdf.xhtml2pdf_reportlab import PmlParagraph
10
+
11
+
12
+ def leadings(html: str) -> list[float]:
13
+ story = pisaStory(html).story
14
+ return [round(f.style.leading, 3) for f in story if isinstance(f, PmlParagraph)]
15
+
16
+
17
+ def nested(line_height: str) -> list[float]:
18
+ """The leading of a 10pt paragraph and of a 20pt one under the same div."""
19
+ return leadings(
20
+ f'<div style="font-size: 10pt; line-height: {line_height}">'
21
+ '<p>small</p><p style="font-size: 20pt">big</p></div>'
22
+ )
23
+
24
+
25
+ class LineHeightTest(TestCase):
26
+ def test_a_number_multiplies_each_elements_own_size(self) -> None:
27
+ self.assertEqual([15.0, 30.0], nested("1.5"))
28
+
29
+ def test_a_whole_number_is_a_number(self) -> None:
30
+ # Not one of HTML's <font size> steps, which read 2 as 0.75em.
31
+ self.assertEqual([20.0, 40.0], nested("2"))
32
+
33
+ def test_a_percentage_is_resolved_where_it_is_declared(self) -> None:
34
+ self.assertEqual([15.0, 15.0], nested("150%"))
35
+
36
+ def test_em_is_resolved_where_it_is_declared(self) -> None:
37
+ self.assertEqual([15.0, 15.0], nested("1.5em"))
38
+
39
+ def test_a_length_is_kept(self) -> None:
40
+ self.assertEqual([12.0, 12.0], nested("12pt"))
41
+
42
+ def test_normal_is_the_font_size(self) -> None:
43
+ self.assertEqual([10.0, 20.0], nested("normal"))
44
+
45
+ def test_the_default_follows_each_size(self) -> None:
46
+ # DEFAULT_CSS gives html line-height: 1.5, a number.
47
+ self.assertEqual(
48
+ [15.0, 30.0],
49
+ leadings(
50
+ '<p style="font-size: 10pt">a</p><p style="font-size: 20pt">b</p>'
51
+ ),
52
+ )
53
+
54
+ def test_inherit_takes_the_parents_source(self) -> None:
55
+ self.assertEqual((30.0, "1.5"), lineHeight("inherit", 20.0, "1.5"))
56
+ self.assertEqual(15.0, leadingFrom("15.0pt", 40.0))
@@ -0,0 +1,64 @@
1
+ """The vertical margins of a list, as a browser gives them."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from unittest import TestCase
6
+
7
+ from reportlab.platypus import Spacer
8
+
9
+ from xhtml2pdf.document import pisaStory
10
+ from xhtml2pdf.xhtml2pdf_reportlab import PmlParagraph
11
+
12
+ FONT = "font-size: 10pt; "
13
+
14
+
15
+ def paragraphs(html: str) -> dict[str, tuple[float, float]]:
16
+ """Text -> (space before, space after) of each paragraph of the story."""
17
+ story = pisaStory(html).story
18
+ found = {}
19
+ for flowable in story:
20
+ if isinstance(flowable, PmlParagraph):
21
+ flowable.wrap(400, 800)
22
+ found[flowable.getPlainText().strip()] = (
23
+ flowable.getSpaceBefore(),
24
+ flowable.getSpaceAfter(),
25
+ )
26
+ return found
27
+
28
+
29
+ class ListMarginTest(TestCase):
30
+ def test_a_list_has_an_em_above_and_below(self) -> None:
31
+ found = paragraphs(
32
+ f'<p style="margin: 0">before</p><ul style="{FONT}"><li>one</li>'
33
+ '<li>two</li></ul><p style="margin: 0">after</p>'
34
+ )
35
+ self.assertEqual(10.0, found["one"][0])
36
+ self.assertEqual(10.0, found["two"][1])
37
+
38
+ def test_a_list_in_a_list_has_none(self) -> None:
39
+ found = paragraphs(
40
+ f'<ul style="{FONT}"><li>outer<ul><li>inner</li></ul></li>'
41
+ "<li>after</li></ul>"
42
+ )
43
+ self.assertEqual((0.0, 0.0), found["inner"])
44
+
45
+ def test_the_last_items_margin_collapses_with_the_lists(self) -> None:
46
+ story = pisaStory(
47
+ '<ul style="margin: 0 0 20pt 0"><li><p style="margin: 0 0 5pt 0">'
48
+ 'a</p></li></ul><p style="margin: 0">after</p>'
49
+ ).story
50
+ # The larger of the two, on the item, and no spacer for the list.
51
+ self.assertFalse(any(isinstance(f, Spacer) for f in story))
52
+ found = paragraphs(
53
+ '<ul style="margin: 0 0 20pt 0"><li><p style="margin: 0 0 5pt 0">'
54
+ 'a</p></li></ul><p style="margin: 0">after</p>'
55
+ )
56
+ self.assertEqual(20.0, found["a"][1])
57
+
58
+ def test_margin_zero_keeps_a_list_tight(self) -> None:
59
+ found = paragraphs(
60
+ '<p style="margin: 0">before</p>'
61
+ '<ul style="margin-top: 0; margin-bottom: 0"><li>one</li></ul>'
62
+ '<p style="margin: 0">after</p>'
63
+ )
64
+ self.assertEqual((0.0, 0.0), found["one"])