From 2ed25024f89a09665230f4fa25707bcf9f658e8a Mon Sep 17 00:00:00 2001 From: "jack.burridge" Date: Fri, 25 Feb 2022 11:06:04 +0000 Subject: [PATCH] Add adjustbox to change width, height and scale --- README.rst | 1 + sphinxcontrib/plantuml.py | 44 +++++++++++++++++++++++++++++++++++---- tests/test_functional.py | 41 +++++++++++++++++++++++++++++++++++- 3 files changed, 81 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 461509e..2fdb69c 100644 --- a/README.rst +++ b/README.rst @@ -92,6 +92,7 @@ plantuml_latex_output_format :eps: generate .eps (not supported by `pdflatex`) :pdf: generate .eps and convert it to .pdf (requires `epstopdf`) :png: generate .png + :tikz: generate .latex in the TikZ format :none: do not generate any images (ignore uml directive) Because embedded png looks pretty bad, it is recommended to choose `pdf` diff --git a/sphinxcontrib/plantuml.py b/sphinxcontrib/plantuml.py index a20481a..d2a9d72 100644 --- a/sphinxcontrib/plantuml.py +++ b/sphinxcontrib/plantuml.py @@ -555,6 +555,36 @@ def _lookup_latex_format(fmt): % (', '.join(map(repr, _KNOWN_LATEX_FORMATS)), fmt)) +def _latex_adjust_box_options(self, node): + adjust_box_options = [] + if 'width' in node: + if 'scale' in node: + w = self.latex_image_length(node['width'], node['scale']) + else: + w = self.latex_image_length(node['width']) + if w: + adjust_box_options.append('width=%s' % w) + if 'height' in node: + if 'scale' in node: + h = self.latex_image_length(node['height'], node['scale']) + else: + h = self.latex_image_length(node['height']) + if h: + adjust_box_options.append('height=%s' % h) + if 'scale' in node: + if not adjust_box_options: + adjust_box_options.append('scale=%s' + % (float(node['scale']) / 100.0)) + return adjust_box_options + + +def _latex_add_package(self, package): + # TODO: Currently modifying the preamble to add a package, there may be a cleaner solution + package = '\\usepackage{%s}' % (package,) + if package not in self.elements['preamble']: + self.elements['preamble'] += package + '\n' + + def latex_visit_plantuml(self, node): _render_batches_on_vist(self) if 'latex_format' in node: @@ -572,12 +602,18 @@ def latex_visit_plantuml(self, node): raise nodes.SkipNode if fmt == 'tikz': - package = '\\usepackage{tikz}' - if package not in self.elements['preamble']: - self.elements['preamble'] += package + '\n' + adjust_box_options = _latex_adjust_box_options(self, node) + _latex_add_package(self, 'tikz') + base, ext = os.path.splitext(refname) - self.body.append('\\input{{%s}%s}' % (base, ext)) + input_macro = '\\input{{%s}%s}' % (base, ext) + if adjust_box_options: + _latex_add_package(self, 'adjustbox') + options = ','.join(adjust_box_options) + self.body.append('\\adjustbox{%s}{%s}' % (options, input_macro)) + else: + self.body.append(input_macro) else: # put node representing rendered image img_node = nodes.image(uri=refname, **node.attributes) diff --git a/tests/test_functional.py b/tests/test_functional.py index 587091e..77cce9b 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -262,7 +262,7 @@ def test_buildlatex_simple_with_tikz(): """ files = glob.glob(os.path.join(_outdir, 'plantuml-*.latex')) assert len(files) == 1 - assert re.search(br'\\(sphinx)?input\{+plantuml-', + assert re.search(br'\\input\{+plantuml-', readfile('plantuml_fixture.tex')) content = readfile(files[0]).splitlines() @@ -270,6 +270,45 @@ def test_buildlatex_simple_with_tikz(): assert content[1][2:] == b'Hello' +@with_runsphinx('latex', plantuml_latex_output_format='tikz') +def test_buildlatex_simple_scale_with_tikz(): + """Generate simple LaTeX with TikZ + + .. uml:: + :scale: 20% + + Hello + """ + assert re.search(br'\\adjustbox\{scale=0.2\}\{\\input\{+plantuml-', + readfile('plantuml_fixture.tex')) + + +@with_runsphinx('latex', plantuml_latex_output_format='tikz') +def test_buildlatex_simple_width_with_tikz(): + """Generate simple LaTeX with TikZ + + .. uml:: + :width: 50mm + + Hello + """ + assert re.search(br'\\adjustbox\{width=50mm\}\{\\input\{+plantuml-', + readfile('plantuml_fixture.tex')) + + +@with_runsphinx('latex', plantuml_latex_output_format='tikz') +def test_buildlatex_simple_height_with_tikz(): + """Generate simple LaTeX with TikZ + + .. uml:: + :height: 50mm + + Hello + """ + assert re.search(br'\\adjustbox\{height=50mm\}\{\\input\{+plantuml-', + readfile('plantuml_fixture.tex')) + + @with_runsphinx('latex', plantuml_latex_output_format='pdf') def test_buildlatex_simple_with_pdf(): """Generate simple LaTeX with PDF