Просмотр исходного кода

Labels: print Polish, Cyrillic and Greek letters (#3553)

- Label PDFs used the PDF base font Helvetica, which has only
  Western European characters; letters like ą, ł or ż and Cyrillic
  or Greek text came out as empty boxes
- Embed Liberation Sans (SIL OFL 1.1, same letter widths as
  Helvetica) from backend/app/data/fonts, so the layout is unchanged
- Tests: extracted text on all templates, only the embedded font in
  the PDF
maziggy 6 дней назад
Родитель
Сommit
87d2f42ec6

+ 101 - 0
backend/app/data/fonts/LICENSE_LIBERATION

@@ -0,0 +1,101 @@
+Digitized data copyright (c) 2010 Google Corporation
+	with Reserved Font Arimo, Tinos and Cousine.
+Copyright (c) 2012 Red Hat, Inc.
+	with Reserved Font Name Liberation.
+
+This Font Software is licensed under the SIL Open Font License,
+Version 1.1.
+
+This license is copied below, and is also available with a FAQ at:
+http://scripts.sil.org/OFL
+
+SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
+
+PREAMBLE The goals of the Open Font License (OFL) are to stimulate
+worldwide development of collaborative font projects, to support the font
+creation efforts of academic and linguistic communities, and to provide
+a free and open framework in which fonts may be shared and improved in
+partnership with others.
+
+The OFL allows the licensed fonts to be used, studied, modified and
+redistributed freely as long as they are not sold by themselves.
+The fonts, including any derivative works, can be bundled, embedded,
+redistributed and/or sold with any software provided that any reserved
+names are not used by derivative works.  The fonts and derivatives,
+however, cannot be released under any other type of license.  The
+requirement for fonts to remain under this license does not apply to
+any document created using the fonts or their derivatives.
+
+
+
+DEFINITIONS
+"Font Software" refers to the set of files released by the Copyright
+Holder(s) under this license and clearly marked as such.
+This may include source files, build scripts and documentation.
+
+"Reserved Font Name" refers to any names specified as such after the
+copyright statement(s).
+
+"Original Version" refers to the collection of Font Software components
+as distributed by the Copyright Holder(s).
+
+"Modified Version" refers to any derivative made by adding to, deleting,
+or substituting ? in part or in whole ?
+any of the components of the Original Version, by changing formats or
+by porting the Font Software to a new environment.
+
+"Author" refers to any designer, engineer, programmer, technical writer
+or other person who contributed to the Font Software.
+
+
+PERMISSION & CONDITIONS
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of the Font Software, to use, study, copy, merge, embed, modify,
+redistribute, and sell modified and unmodified copies of the Font
+Software, subject to the following conditions:
+
+1) Neither the Font Software nor any of its individual components,in
+   Original or Modified Versions, may be sold by itself.
+
+2) Original or Modified Versions of the Font Software may be bundled,
+   redistributed and/or sold with any software, provided that each copy
+   contains the above copyright notice and this license. These can be
+   included either as stand-alone text files, human-readable headers or
+   in the appropriate machine-readable metadata fields within text or
+   binary files as long as those fields can be easily viewed by the user.
+
+3) No Modified Version of the Font Software may use the Reserved Font
+   Name(s) unless explicit written permission is granted by the
+   corresponding Copyright Holder. This restriction only applies to the
+   primary font name as presented to the users.
+
+4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
+   Software shall not be used to promote, endorse or advertise any
+   Modified Version, except to acknowledge the contribution(s) of the
+   Copyright Holder(s) and the Author(s) or with their explicit written
+   permission.
+
+5) The Font Software, modified or unmodified, in part or in whole, must
+   be distributed entirely under this license, and must not be distributed
+   under any other license. The requirement for fonts to remain under
+   this license does not apply to any document created using the Font
+   Software.
+
+
+
+TERMINATION
+This license becomes null and void if any of the above conditions are not met.
+
+
+
+DISCLAIMER
+THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
+OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT.  IN NO EVENT SHALL THE
+COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
+DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER
+DEALINGS IN THE FONT SOFTWARE.

BIN
backend/app/data/fonts/LiberationSans-Bold.ttf


BIN
backend/app/data/fonts/LiberationSans-Italic.ttf


BIN
backend/app/data/fonts/LiberationSans-Regular.ttf


+ 60 - 22
backend/app/services/label_renderer.py

@@ -46,14 +46,52 @@ from __future__ import annotations
 import io
 import io
 from dataclasses import dataclass
 from dataclasses import dataclass
 from datetime import date
 from datetime import date
+from pathlib import Path
 from typing import Literal
 from typing import Literal
 
 
 import qrcode
 import qrcode
 from reportlab.lib.colors import Color, HexColor, black, white
 from reportlab.lib.colors import Color, HexColor, black, white
 from reportlab.lib.pagesizes import A4, letter
 from reportlab.lib.pagesizes import A4, letter
 from reportlab.lib.units import mm
 from reportlab.lib.units import mm
+from reportlab.pdfbase import pdfmetrics
+from reportlab.pdfbase.ttfonts import TTFont
 from reportlab.pdfgen import canvas as rl_canvas
 from reportlab.pdfgen import canvas as rl_canvas
 
 
+# Label text is printed in Liberation Sans, embedded in the PDF (#3553). The
+# PDF base fonts (Helvetica) only cover Western European characters, so names
+# in Polish, Czech, Cyrillic or Greek lost letters. Liberation Sans has the
+# same letter widths as Helvetica, so the layout and truncation are unchanged.
+_FONT_DIR = Path(__file__).resolve().parent.parent / "data" / "fonts"
+FONT_REGULAR = "LabelSans"
+FONT_BOLD = "LabelSans-Bold"
+FONT_ITALIC = "LabelSans-Italic"
+
+
+def _register_fonts() -> None:
+    registered = set(pdfmetrics.getRegisteredFontNames())
+    for name, filename in (
+        (FONT_REGULAR, "LiberationSans-Regular.ttf"),
+        (FONT_BOLD, "LiberationSans-Bold.ttf"),
+        (FONT_ITALIC, "LiberationSans-Italic.ttf"),
+    ):
+        if name not in registered:
+            pdfmetrics.registerFont(TTFont(name, str(_FONT_DIR / filename)))
+
+
+_register_fonts()
+
+
+def _new_canvas(buf: io.BytesIO, pagesize: tuple[float, float]) -> rl_canvas.Canvas:
+    """A canvas whose default font is the label font, so the PDF does not
+    carry an unused Helvetica resource."""
+    # types-reportlab declares initialFontName as a float; reportlab takes a font name.
+    return rl_canvas.Canvas(
+        buf,
+        pagesize=pagesize,
+        initialFontName=FONT_REGULAR,  # pyright: ignore[reportArgumentType]
+    )
+
+
 TemplateName = Literal[
 TemplateName = Literal[
     "ams_holder_74x33",
     "ams_holder_74x33",
     "ams_holder_75x55",
     "ams_holder_75x55",
@@ -361,8 +399,8 @@ def _draw_label_tight(
     # easiest thing to read on a small AMS holder at arm's length.
     # easiest thing to read on a small AMS holder at arm's length.
     brand_size = 6.5
     brand_size = 6.5
     if data.brand and "brand" in fields:
     if data.brand and "brand" in fields:
-        c.setFont("Helvetica-Bold", brand_size)
-        brand = _truncate_to_width(c, data.brand, "Helvetica-Bold", brand_size, text_w)
+        c.setFont(FONT_BOLD, brand_size)
+        brand = _truncate_to_width(c, data.brand, FONT_BOLD, brand_size, text_w)
         c.drawString(text_x, y + h - pad - brand_size, brand)
         c.drawString(text_x, y + h - pad - brand_size, brand)
 
 
     # Second line: material + subtype, small
     # Second line: material + subtype, small
@@ -370,8 +408,8 @@ def _draw_label_tight(
     sub_line = " ".join(filter(None, [data.material, data.subtype])) if "material" in fields else ""
     sub_line = " ".join(filter(None, [data.material, data.subtype])) if "material" in fields else ""
     sub_y_baseline = y + h - pad - brand_size - 0.6 - sub_size
     sub_y_baseline = y + h - pad - brand_size - 0.6 - sub_size
     if sub_line:
     if sub_line:
-        c.setFont("Helvetica", sub_size)
-        sub_line = _truncate_to_width(c, sub_line, "Helvetica", sub_size, text_w)
+        c.setFont(FONT_REGULAR, sub_size)
+        sub_line = _truncate_to_width(c, sub_line, FONT_REGULAR, sub_size, text_w)
         c.drawString(text_x, sub_y_baseline, sub_line)
         c.drawString(text_x, sub_y_baseline, sub_line)
 
 
     # Third line (when there's room): hex code, tiny — useful when the user
     # Third line (when there's room): hex code, tiny — useful when the user
@@ -382,15 +420,15 @@ def _draw_label_tight(
         hex_y = sub_y_baseline - 0.4 - hex_size
         hex_y = sub_y_baseline - 0.4 - hex_size
         # Don't render if it'd collide with the spool ID at the bottom.
         # Don't render if it'd collide with the spool ID at the bottom.
         if hex_y > inner_y + 13:
         if hex_y > inner_y + 13:
-            c.setFont("Helvetica", hex_size)
+            c.setFont(FONT_REGULAR, hex_size)
             c.drawString(text_x, hex_y, hex_code)
             c.drawString(text_x, hex_y, hex_code)
 
 
     # Bottom: BIG spool ID — the killer field at-a-glance.
     # Bottom: BIG spool ID — the killer field at-a-glance.
     if "spool_id" not in fields:
     if "spool_id" not in fields:
         return
         return
     id_size = 13
     id_size = 13
-    c.setFont("Helvetica-Bold", id_size)
-    id_text = _truncate_to_width(c, f"#{data.spool_id}", "Helvetica-Bold", id_size, text_w)
+    c.setFont(FONT_BOLD, id_size)
+    id_text = _truncate_to_width(c, f"#{data.spool_id}", FONT_BOLD, id_size, text_w)
     c.drawString(text_x, inner_y + 0.5, id_text)
     c.drawString(text_x, inner_y + 0.5, id_text)
 
 
 
 
@@ -456,18 +494,18 @@ def _draw_label_roomy(
     # are what the label has always used; the rest share the location's.
     # are what the label has always used; the rest share the location's.
     rows: list[tuple[LabelField, str, str, float, float]] = [
     rows: list[tuple[LabelField, str, str, float, float]] = [
         # Brand — bumped to bold + larger per the #809 follow-up.
         # Brand — bumped to bold + larger per the #809 follow-up.
-        ("brand", data.brand or "", "Helvetica-Bold", 8, 1.2),
-        ("material", " · ".join(filter(None, [data.material, data.subtype])), "Helvetica", 7, 1.5),
+        ("brand", data.brand or "", FONT_BOLD, 8, 1.2),
+        ("material", " · ".join(filter(None, [data.material, data.subtype])), FONT_REGULAR, 7, 1.5),
         # Hex colour code — useful for telling near-identical material+colour
         # Hex colour code — useful for telling near-identical material+colour
         # spools apart when the swatch is small or the user is colour-blind.
         # spools apart when the swatch is small or the user is colour-blind.
-        ("hex", _hex_code_label(data.rgba), "Helvetica", 6.5, 1.2),
-        ("name", name if name not in repeats else "", "Helvetica-Bold", 9, 1.2),
-        ("location", data.storage_location or "", "Helvetica-Oblique", 6.5, 1.2),
-        ("material_number", data.material_number or "", "Helvetica", 6.5, 1.2),
-        ("temps", _temps_text(data), "Helvetica", 6.5, 1.2),
-        ("weight", _weight_text(data), "Helvetica", 6.5, 1.2),
-        ("note", data.note or "", "Helvetica-Oblique", 6.5, 1.2),
-        ("added", data.added.isoformat() if data.added else "", "Helvetica", 6.5, 1.2),
+        ("hex", _hex_code_label(data.rgba), FONT_REGULAR, 6.5, 1.2),
+        ("name", name if name not in repeats else "", FONT_BOLD, 9, 1.2),
+        ("location", data.storage_location or "", FONT_ITALIC, 6.5, 1.2),
+        ("material_number", data.material_number or "", FONT_REGULAR, 6.5, 1.2),
+        ("temps", _temps_text(data), FONT_REGULAR, 6.5, 1.2),
+        ("weight", _weight_text(data), FONT_REGULAR, 6.5, 1.2),
+        ("note", data.note or "", FONT_ITALIC, 6.5, 1.2),
+        ("added", data.added.isoformat() if data.added else "", FONT_REGULAR, 6.5, 1.2),
     ]
     ]
 
 
     cursor_y = y + h - pad
     cursor_y = y + h - pad
@@ -482,8 +520,8 @@ def _draw_label_roomy(
         cursor_y -= gap
         cursor_y -= gap
 
 
     if show_id:
     if show_id:
-        c.setFont("Helvetica-Bold", id_size)
-        id_text = _truncate_to_width(c, f"#{data.spool_id}", "Helvetica-Bold", id_size, text_w)
+        c.setFont(FONT_BOLD, id_size)
+        id_text = _truncate_to_width(c, f"#{data.spool_id}", FONT_BOLD, id_size, text_w)
         c.drawString(text_x, inner_y + 0.5, id_text)
         c.drawString(text_x, inner_y + 0.5, id_text)
 
 
 
 
@@ -599,7 +637,7 @@ def _render_single_label_pdf(
     page_w, page_h = w_mm * mm, h_mm * mm
     page_w, page_h = w_mm * mm, h_mm * mm
 
 
     buf = io.BytesIO()
     buf = io.BytesIO()
-    c = rl_canvas.Canvas(buf, pagesize=(page_w, page_h))
+    c = _new_canvas(buf, (page_w, page_h))
     c.setTitle(f"Bambuddy spool labels ({template})")
     c.setTitle(f"Bambuddy spool labels ({template})")
 
 
     for data in data_list:
     for data in data_list:
@@ -660,7 +698,7 @@ def _render_sheet_pdf(
     row_gap = layout.row_gap_mm * mm
     row_gap = layout.row_gap_mm * mm
 
 
     buf = io.BytesIO()
     buf = io.BytesIO()
-    c = rl_canvas.Canvas(buf, pagesize=page_size)
+    c = _new_canvas(buf, page_size)
     c.setTitle(f"Bambuddy spool labels ({title})")
     c.setTitle(f"Bambuddy spool labels ({title})")
 
 
     per_page = layout.capacity
     per_page = layout.capacity
@@ -737,7 +775,7 @@ def render_label_preview_pdf(
     w_mm, h_mm = label_size_mm(template)
     w_mm, h_mm = label_size_mm(template)
     page_w, page_h = w_mm * mm, h_mm * mm
     page_w, page_h = w_mm * mm, h_mm * mm
     buf = io.BytesIO()
     buf = io.BytesIO()
-    c = rl_canvas.Canvas(buf, pagesize=(page_w, page_h))
+    c = _new_canvas(buf, (page_w, page_h))
     _draw_label(c, 0, 0, page_w, page_h, data, monochrome, fields)
     _draw_label(c, 0, 0, page_w, page_h, data, monochrome, fields)
     c.showPage()
     c.showPage()
     c.save()
     c.save()

+ 46 - 6
backend/tests/unit/services/test_label_renderer.py

@@ -9,6 +9,7 @@ from reportlab.lib.pagesizes import letter
 from reportlab.lib.units import mm
 from reportlab.lib.units import mm
 
 
 from backend.app.services.label_renderer import (
 from backend.app.services.label_renderer import (
+    FONT_BOLD,
     LabelData,
     LabelData,
     SheetLayout,
     SheetLayout,
     get_sheet_capacity,
     get_sheet_capacity,
@@ -300,7 +301,7 @@ def test_hex_color_code_skipped_when_rgba_invalid():
 
 
 
 
 def test_brand_rendered_in_bold_per_809_followup():
 def test_brand_rendered_in_bold_per_809_followup():
-    """#809 follow-up: brand should render in Helvetica-Bold (not regular).
+    """#809 follow-up: brand should render in the bold font (not regular).
     Uncompressed PDFs include font-name tokens like '/F2' tied to a font
     Uncompressed PDFs include font-name tokens like '/F2' tied to a font
     resource; we can grep for the bold font's basename in the resource block.
     resource; we can grep for the bold font's basename in the resource block.
     """
     """
@@ -315,10 +316,9 @@ def test_brand_rendered_in_bold_per_809_followup():
         )
         )
     ]
     ]
     pdf = _render_uncompressed("box_62x29", data)
     pdf = _render_uncompressed("box_62x29", data)
-    # ReportLab references the bold variant of Helvetica via /Helvetica-Bold
-    # in the font dictionary — both the spool ID (always bold) and the brand
-    # (now bold per #809 follow-up) cause the resource to be embedded.
-    assert b"Helvetica-Bold" in pdf, "label PDF must reference Helvetica-Bold for the brand line"
+    # Both the spool ID (always bold) and the brand (bold per #809 follow-up)
+    # embed the bold font, which the font dictionary names.
+    assert b"LiberationSans-Bold" in pdf, "label PDF must embed the bold font for the brand line"
 
 
 
 
 def test_box_template_does_not_truncate_normal_brand_or_name():
 def test_box_template_does_not_truncate_normal_brand_or_name():
@@ -499,7 +499,7 @@ def test_the_smallest_label_has_room_for_a_full_size_spool_id(label_h_mm):
     swatch_w = min(inner_w * 0.18, inner_h, 16 * mm)
     swatch_w = min(inner_w * 0.18, inner_h, 16 * mm)
     qr = _roomy_qr_size(inner_w, inner_h)
     qr = _roomy_qr_size(inner_w, inner_h)
     text_w = inner_w - swatch_w - qr - 2 * 1.5 * mm
     text_w = inner_w - swatch_w - qr - 2 * 1.5 * mm
-    assert text_w >= stringWidth("#9999", "Helvetica-Bold", 16)
+    assert text_w >= stringWidth("#9999", FONT_BOLD, 16)
     assert MIN_LABEL_H_MM >= 20.0  # the tight layout starts below 20 mm
     assert MIN_LABEL_H_MM >= 20.0  # the tight layout starts below 20 mm
 
 
 
 
@@ -581,3 +581,43 @@ def test_custom_layout_size_and_capacity():
     assert label_size_mm(layout) == (70.0, 36.0)
     assert label_size_mm(layout) == (70.0, 36.0)
     assert get_sheet_capacity(layout) == 24
     assert get_sheet_capacity(layout) == 24
     assert render_labels(layout, [_sample()]).startswith(b"%PDF")
     assert render_labels(layout, [_sample()]).startswith(b"%PDF")
+
+
+# ── Non-Latin-1 text (#3553) ──
+
+
+def _page_texts(pdf: bytes) -> list[str]:
+    import pypdfium2 as pdfium  # noqa: PLC0415
+
+    doc = pdfium.PdfDocument(pdf)
+    try:
+        return [doc[i].get_textpage().get_text_range() for i in range(len(doc))]
+    finally:
+        doc.close()
+
+
+@pytest.mark.parametrize("template", ALL_TEMPLATES)
+def test_polish_and_cyrillic_text_prints_as_written(template):
+    """The PDF base fonts cover only Western European characters: Polish
+    ą ę ł ń ś ź ż and Cyrillic came out as empty boxes."""
+    data = [
+        _sample(
+            7,
+            brand="Żółć",
+            name="Brązowy",
+            material="PLA",
+            subtype="Мат",
+        )
+    ]
+    text = _page_texts(render_labels(template, data))[0]
+    for word in ("Żółć", "Brązowy", "Мат"):
+        assert word in text, f"{word!r} missing from {template}: {text!r}"
+
+
+@pytest.mark.parametrize("template", ALL_TEMPLATES)
+def test_labels_use_no_base_font(template):
+    import re  # noqa: PLC0415
+
+    pdf = render_labels(template, [_sample(3)])
+    fonts = set(re.findall(rb"/BaseFont /(?:[A-Z]{6}\+)?([^\s/>]+)", pdf))
+    assert fonts == {b"LiberationSans", b"LiberationSans-Bold"}, fonts

+ 7 - 8
backend/tests/unit/services/test_label_renderer_fields.py

@@ -66,14 +66,13 @@ def _text(template: str, data: LabelData, fields=DEFAULT_LABEL_FIELDS) -> bytes:
     c.showPage()
     c.showPage()
     c.save()
     c.save()
     pdf = buf.getvalue()
     pdf = buf.getvalue()
-    streams = []
-    for m in re.finditer(rb"stream\r?\n(.*?)endstream", pdf, re.S):
-        # The object's own dictionary sits between its "N 0 obj" and "stream".
-        head = pdf[pdf.rfind(b" obj", 0, m.start()) : m.start()]
-        if b"/Subtype /Image" not in head:
-            streams.append(m.group(1))
-    assert len(streams) == 1, f"expected one page content stream, got {len(streams)}"
-    return streams[0]
+    # The page names its content stream; the embedded fonts and the QR image
+    # are streams of their own.
+    refs = re.findall(rb"/Contents (\d+) 0 R", pdf)
+    assert len(refs) == 1, f"expected one page content stream, got {len(refs)}"
+    m = re.search(rb"(?<!\d)" + refs[0] + rb" 0 obj.*?stream\r?\n(.*?)endstream", pdf, re.S)
+    assert m is not None
+    return m.group(1)
 
 
 
 
 def test_default_fields_are_what_labels_always_carried():
 def test_default_fields_are_what_labels_always_carried():