Skip to content

Commit c093f6a

Browse files
committed
tweak graphics conversion stuff
1 parent a72bea0 commit c093f6a

2 files changed

Lines changed: 46 additions & 21 deletions

File tree

‎doc/selfdocenv.rst‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
Self-Documenting Environments
22
=============================
33

4-
The ``flm.docgen`` module provides a self-documenting environment system
5-
that lets feature authors write structured documentation for macros,
6-
environments, and specials directly in FLM markup.
4+
The ``flm.docgen`` module provides a self-documenting environment system that
5+
lets feature authors write structured documentation for macros, environments,
6+
and specials directly in FLM markup.
77

88
The system defines special-purpose constructs (``\flmDocArg``,
9-
``{flmDocArguments}``, etc.) that produce formatted argument documentation
10-
when rendered. This is used internally to generate the
11-
:doc:`standard-syntax` reference from the ``get_flm_doc()`` methods on
12-
each spec info class.
9+
``{flmDocArguments}``, etc.) that produce formatted argument documentation when
10+
rendered. I'm planning to use this internally to generate the
11+
:doc:`standard-syntax` reference from the ``get_flm_doc()`` methods on each spec
12+
info class.
1313

1414
.. automodule:: flm.docgen.docgen
1515
:members:

‎flm/main/feature_graphics_collection.py‎

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ class GraphicsFormatConversionRule:
150150
'via': [..list of converter names..],
151151
'options': <dict or None> }
152152
153-
Built-in converter names are: 'cairosvg', 'gs', 'pdftocairo', 'magick'.
153+
Built-in converter names are: 'cairosvg', 'gs', 'pdftocairo', 'magick'. If
154+
you specify a list of converter names, these are meant to be acceptable
155+
converters to use to perform the transformation. The rule will pick the
156+
first one that can perform the conversion. (There is currently no way of
157+
specifying a conversion chain through multiple converters with intermediate
158+
formats.)
154159
155160
Example rule in YAML::
156161
@@ -159,7 +164,6 @@ class GraphicsFormatConversionRule:
159164
to: '.png'
160165
options:
161166
dpi: 192
162-
163167
"""
164168

165169
def __init__(self, rule):
@@ -220,10 +224,17 @@ def get_converter_or_none(self, ext):
220224
if len(self.via_list) and converter.name not in self.via_list:
221225
continue
222226

223-
try:
224-
can_convert = converter.can_convert(ext, to_ext)
225-
except ConverterNotAvailable as e:
226-
converter_errors.append(e)
227+
can_convert = None
228+
if len(self.via_list) == 1 and self.via_list[0] == converter.name:
229+
# user requested this converter specifically, don't call
230+
# can_convert().
231+
can_convert = True
232+
233+
if can_convert is None:
234+
try:
235+
can_convert = converter.can_convert(ext, to_ext)
236+
except ConverterNotAvailable as e:
237+
converter_errors.append(e)
227238

228239
if not can_convert:
229240
continue
@@ -353,7 +364,7 @@ def convert(self, source_type, src_url, target_path, converter_info, options=Non
353364
elif target_ext in ('.ps', '.eps'):
354365
svgconvert = cairosvg.svg2ps
355366
else:
356-
raise ValueError(f"Invalid target ext {target_ext}, shouldn't be here")
367+
raise ValueError(f"Invalid target ext {target_ext}")
357368

358369
svgconvert(url=src_url, write_to=target_path, **svgkwargs)
359370

@@ -504,6 +515,11 @@ def convert(self, source_type, src_url, target_path, converter_info, options=Non
504515
xtracmdargs += [ '-transp' ]
505516

506517
input_data = self.read_input(source_type, src_url, binary=True)
518+
519+
if target_ext not in self._fmts_to_opts:
520+
raise ValueError(
521+
f"Invalid target extension for PdfToCairoCmdlConverter: ‘{target_ext}’"
522+
)
507523

508524
cmdargs = [
509525
self.pdftocairo_exe,
@@ -545,7 +561,8 @@ def can_convert(cls, ext, to_ext):
545561
)
546562
except ExecutableNotFoundError as e:
547563
raise ConverterNotAvailable(cls.name, e.msg, exc=e)
548-
564+
# by default, any conversion is possible - magick supports pretty much
565+
# everything
549566
return True
550567

551568
def __init__(self):
@@ -558,7 +575,7 @@ def convert(self, source_type, src_url, target_path, converter_info, options=Non
558575
else:
559576
options = dict(options)
560577

561-
transparent_bg = options.pop('transparent_bg', False)
578+
transparent_bg = options.pop('transparent_bg', None)
562579
dpi = options.pop('dpi', None)
563580

564581
if len(options):
@@ -570,19 +587,27 @@ def convert(self, source_type, src_url, target_path, converter_info, options=Non
570587
if src_url.endswith( ('.gif', '.mng') ):
571588
ins = '-[0]'
572589

573-
extra_args = []
590+
extra_settings = []
591+
extra_operators = []
592+
574593
if transparent_bg:
575-
extra_args = extra_args + [ '-background', 'none', ]
594+
extra_settings = extra_settings + [ '-background', 'none', ]
595+
elif transparent_bg is False:
596+
extra_operators = extra_operators + [
597+
'-background', 'white', '-alpha', 'remove',
598+
'-alpha', 'off',
599+
]
600+
576601
if dpi is not None:
577-
extra_args = extra_args + [
602+
extra_settings = extra_settings + [
578603
'-density', str(dpi),
579604
]
580605

581606
cmdargs = [
582607
self.magick_exe,
583-
'convert',
584-
*extra_args,
608+
*extra_settings,
585609
ins,
610+
*extra_operators,
586611
target_path
587612
]
588613

0 commit comments

Comments
 (0)