Skip to content

Commit

Permalink
Fix sphinx-doc#6696: html: scale option of image/figure directive not…
Browse files Browse the repository at this point in the history
… working for SVG

Note: imagesize-1.2.0 or above is required
  • Loading branch information
tk0miya committed Jan 2, 2020
1 parent 577b80c commit 9cc2536
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 28 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -30,6 +30,8 @@ Features added
down the build
* #6837: LaTeX: Support a nested table
* #6966: graphviz: Support ``:class:`` option
* #6696: html: ``:scale:`` option of image/figure directive not working for SVG
images (imagesize-1.2.0 or above is required)

Bugs fixed
----------
Expand Down
35 changes: 21 additions & 14 deletions sphinx/writers/html.py
Expand Up @@ -579,13 +579,34 @@ def visit_image(self, node: Element) -> None:
node['uri'] = posixpath.join(self.builder.imgpath,
self.builder.images[olduri])

if 'scale' in node:
# Try to figure out image height and width. Docutils does that too,
# but it tries the final file name, which does not necessarily exist
# yet at the time the HTML file is written.
if not ('width' in node and 'height' in node):
size = get_image_size(os.path.join(self.builder.srcdir, olduri))
if size is None:
logger.warning(__('Could not obtain image size. :scale: option is ignored.'), # NOQA
location=node)
else:
if 'width' not in node:
node['width'] = str(size[0])
if 'height' not in node:
node['height'] = str(size[1])

uri = node['uri']
if uri.lower().endswith(('svg', 'svgz')):
atts = {'src': uri}
if 'width' in node:
atts['width'] = node['width']
if 'height' in node:
atts['height'] = node['height']
if 'scale' in node:
scale = node['scale'] / 100.0
if 'width' in atts:
atts['width'] = int(atts['width']) * scale
if 'height' in atts:
atts['height'] = int(atts['height']) * scale
atts['alt'] = node.get('alt', uri)
if 'align' in node:
self.body.append('<div align="%s" class="align-%s">' %
Expand All @@ -596,20 +617,6 @@ def visit_image(self, node: Element) -> None:
self.body.append(self.emptytag(node, 'img', '', **atts))
return

if 'scale' in node:
# Try to figure out image height and width. Docutils does that too,
# but it tries the final file name, which does not necessarily exist
# yet at the time the HTML file is written.
if not ('width' in node and 'height' in node):
size = get_image_size(os.path.join(self.builder.srcdir, olduri))
if size is None:
logger.warning(__('Could not obtain image size. :scale: option is ignored.'), # NOQA
location=node)
else:
if 'width' not in node:
node['width'] = str(size[0])
if 'height' not in node:
node['height'] = str(size[1])
super().visit_image(node)

# overwritten
Expand Down
35 changes: 21 additions & 14 deletions sphinx/writers/html5.py
Expand Up @@ -520,13 +520,34 @@ def visit_image(self, node: Element) -> None:
node['uri'] = posixpath.join(self.builder.imgpath,
self.builder.images[olduri])

if 'scale' in node:
# Try to figure out image height and width. Docutils does that too,
# but it tries the final file name, which does not necessarily exist
# yet at the time the HTML file is written.
if not ('width' in node and 'height' in node):
size = get_image_size(os.path.join(self.builder.srcdir, olduri))
if size is None:
logger.warning(__('Could not obtain image size. :scale: option is ignored.'), # NOQA
location=node)
else:
if 'width' not in node:
node['width'] = str(size[0])
if 'height' not in node:
node['height'] = str(size[1])

uri = node['uri']
if uri.lower().endswith(('svg', 'svgz')):
atts = {'src': uri}
if 'width' in node:
atts['width'] = node['width']
if 'height' in node:
atts['height'] = node['height']
if 'scale' in node:
scale = node['scale'] / 100.0
if 'width' in atts:
atts['width'] = int(atts['width']) * scale
if 'height' in atts:
atts['height'] = int(atts['height']) * scale
atts['alt'] = node.get('alt', uri)
if 'align' in node:
self.body.append('<div align="%s" class="align-%s">' %
Expand All @@ -537,20 +558,6 @@ def visit_image(self, node: Element) -> None:
self.body.append(self.emptytag(node, 'img', '', **atts))
return

if 'scale' in node:
# Try to figure out image height and width. Docutils does that too,
# but it tries the final file name, which does not necessarily exist
# yet at the time the HTML file is written.
if not ('width' in node and 'height' in node):
size = get_image_size(os.path.join(self.builder.srcdir, olduri))
if size is None:
logger.warning(__('Could not obtain image size. :scale: option is ignored.'), # NOQA
location=node)
else:
if 'width' not in node:
node['width'] = str(size[0])
if 'height' not in node:
node['height'] = str(size[1])
super().visit_image(node)

# overwritten
Expand Down

0 comments on commit 9cc2536

Please sign in to comment.