2022-03-25 13:47:27 +03:00
|
|
|
"""Test various unicode texts and filenames."""
|
2018-03-20 01:35:56 +03:00
|
|
|
|
|
|
|
import os.path
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
|
2020-12-06 22:19:59 +03:00
|
|
|
from weasyprint.urls import ensure_url
|
|
|
|
|
2021-04-19 18:15:53 +03:00
|
|
|
from .draw import assert_pixels_equal, document_to_pixels, html_to_pixels
|
2018-03-20 01:35:56 +03:00
|
|
|
from .testing_utils import FakeHTML, assert_no_logs, resource_filename
|
|
|
|
|
|
|
|
|
|
|
|
@assert_no_logs
|
|
|
|
def test_unicode():
|
|
|
|
text = 'I løvë Unicode'
|
|
|
|
style = '''
|
|
|
|
@page {
|
|
|
|
background: #fff;
|
|
|
|
size: 200px 50px;
|
|
|
|
}
|
|
|
|
p { color: blue }
|
|
|
|
'''
|
2020-05-18 00:54:25 +03:00
|
|
|
expected_lines = html_to_pixels('unicode_reference', 200, 50, '''
|
2018-03-20 01:35:56 +03:00
|
|
|
<style>{0}</style>
|
|
|
|
<p><img src="pattern.png"> {1}</p>
|
|
|
|
'''.format(style, text))
|
|
|
|
|
|
|
|
temp = tempfile.mkdtemp(prefix=text + '-')
|
|
|
|
try:
|
|
|
|
stylesheet = os.path.join(temp, 'style.css')
|
|
|
|
image = os.path.join(temp, 'pattern.png')
|
|
|
|
html = os.path.join(temp, 'doc.html')
|
|
|
|
with open(stylesheet, 'wb') as fd:
|
2022-02-14 12:55:47 +03:00
|
|
|
fd.write(style.encode())
|
2018-03-20 01:35:56 +03:00
|
|
|
with open(resource_filename('pattern.png'), 'rb') as fd:
|
|
|
|
image_content = fd.read()
|
|
|
|
with open(image, 'wb') as fd:
|
|
|
|
fd.write(image_content)
|
|
|
|
with open(html, 'wb') as fd:
|
|
|
|
html_content = '''
|
|
|
|
<link rel=stylesheet href="{0}">
|
|
|
|
<p><img src="{1}"> {2}</p>
|
|
|
|
'''.format(
|
|
|
|
ensure_url(stylesheet), ensure_url(image), text
|
|
|
|
)
|
2022-02-14 12:55:47 +03:00
|
|
|
fd.write(html_content.encode())
|
2018-03-20 01:35:56 +03:00
|
|
|
|
2022-02-09 00:41:03 +03:00
|
|
|
document = FakeHTML(html, encoding='utf-8')
|
2018-03-20 01:35:56 +03:00
|
|
|
lines = document_to_pixels(document, 'unicode', 200, 50)
|
|
|
|
assert_pixels_equal('unicode', 200, 50, lines, expected_lines)
|
|
|
|
finally:
|
|
|
|
shutil.rmtree(temp)
|