From 6df225826a4282d88a74c7c10722107571ccc997 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Tue, 7 Jan 2025 08:52:23 -0300 Subject: [PATCH] [PCB Print][Added] Workaround for GS error on %d - When using %dH where H is an hex digit GS does some crazy things. The %d is expanded to the page number, but then it says it can't open the file, which isn't created. - Now we use a temporal name and then rename the generated file. Closes #763 --- CHANGELOG.md | 5 ++++- docs/source/Changelog.rst | 8 ++++++-- kibot/out_pcb_print.py | 19 ++++++++++++++++--- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce2549f8..bc0a5119 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,7 +49,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Include table: - A mechanism to set the font, using a text box (#748) - `force_font_width` to force a font width (#752) -- PCB Print: a mechanism to filter components for a particular layer (#706) +- PCB Print: + - A mechanism to filter components for a particular layer (#706) + - Workaround for Ghostscript handling %d wrong when followed by an hex digit + (#763) - Report: - `top_total`, `bot_total`, `total_smd`, `total_tht` and `total_all` component counts (See #730) diff --git a/docs/source/Changelog.rst b/docs/source/Changelog.rst index e6810e1e..b6f11662 100644 --- a/docs/source/Changelog.rst +++ b/docs/source/Changelog.rst @@ -82,8 +82,12 @@ Added - A mechanism to set the font, using a text box (#748) - ``force_font_width`` to force a font width (#752) -- PCB Print: a mechanism to filter components for a particular layer - (#706) +- PCB Print: + + - A mechanism to filter components for a particular layer (#706) + - Workaround for Ghostscript handling %d wrong when followed by an + hex digit (#763) + - Report: - ``top_total``, ``bot_total``, ``total_smd``, ``total_tht`` and diff --git a/kibot/out_pcb_print.py b/kibot/out_pcb_print.py index a8f737f8..e817adce 100644 --- a/kibot/out_pcb_print.py +++ b/kibot/out_pcb_print.py @@ -66,6 +66,7 @@ "stroke-linejoin:round;fill-rule:evenodd;") DRAWING_LAYERS = ['Dwgs.User', 'Cmts.User', 'Eco1.User', 'Eco2.User'] EXTRA_LAYERS = ['F.Fab', 'B.Fab', 'F.CrtYd', 'B.CrtYd'] +GSPNERROR = re.compile(r'%\d*d[0-9a-fA-F]') # The following modules will be downloaded after we solve the dependencies # They are just helpers and we solve their dependencies svgutils = None # Will be loaded during dependency check @@ -1284,14 +1285,18 @@ def check_tools(self): # if self.format == 'EPS': # self.rsvg_command_eps = self.ensure_tool('rsvg2') - def rename_pages(self, output_dir): + def rename_pages(self, output_dir, real_output=None): for n, p in enumerate(self._pages): id, ext = self.get_id_and_ext(n) cur_name = self.expand_filename(output_dir, self.output, id, ext) id, ext = self.get_id_and_ext(n, p.page_id) - user_name = self.expand_filename(output_dir, self.output, id, ext) + user_name = self.expand_filename(output_dir, real_output or self.output, id, ext) if cur_name != user_name and os.path.isfile(cur_name): + logger.debug('- Renaming {cur_name} -> {user_name}') os.replace(cur_name, user_name) + if real_output: + # Revert the output, GS workaround + self.output = real_output def check_ki7_scale_issue(self): """ Check if all visible layers has scaling problems """ @@ -1543,13 +1548,21 @@ def generate_output(self, output): else: # EPS and PNG id, ext = self.get_id_and_ext() out_file = self.expand_filename(output_dir, self.output, id, ext, make_safe=False) + real_output = None + # Ghostscript issue workaround + # Patterns like %02d followed by an hex digit makes GS fail (i.e. GS 10.00.0) + # So here we use a pattern that works and then we rename the files + if GSPNERROR.search(out_file): + real_output = self.output + self.output = '_kibot_tmp_%i.'+ext + out_file = self.expand_filename(output_dir, self.output, id, ext, make_safe=False) if self.format == 'EPS': # Use GS to create one EPS per page self.pdf_to_eps(pdf_file, out_file) else: # Use GS to create one PNG per page and then scale to the wanted width self.pdf_to_png(pdf_file, out_file) - self.rename_pages(output_dir) + self.rename_pages(output_dir, real_output) # Restore KiBot image groups away self.restore_kibot_image_groups() # Remove the temporal files