Skip to content

Commit

Permalink
[PCB Print][Added] Workaround for GS error on %d
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
set-soft committed Jan 7, 2025
1 parent 487c9c9 commit 6df2258
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions docs/source/Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 16 additions & 3 deletions kibot/out_pcb_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 """
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6df2258

Please sign in to comment.