diff options
| author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2020-01-04 22:36:42 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-04 22:36:42 +0900 |
| commit | 2f7823e1a63eb14c7a30cb08cdacbfb2f85e3a2d (patch) | |
| tree | 1e2e4607fe3d33de9cb62e8d74b68d8dde8343ab /sphinx | |
| parent | 55415614904522651da2cca3321ac6ee08f282a9 (diff) | |
| parent | 9cc25367b832fc743f071572645fa2c56d207c91 (diff) | |
| download | sphinx-git-2f7823e1a63eb14c7a30cb08cdacbfb2f85e3a2d.tar.gz | |
Merge pull request #6985 from tk0miya/6696_scale_not_working_for_SVG
Fix #6696: html: scale option of image/figure directive not working for SVG
Diffstat (limited to 'sphinx')
| -rw-r--r-- | sphinx/writers/html.py | 35 | ||||
| -rw-r--r-- | sphinx/writers/html5.py | 35 |
2 files changed, 42 insertions, 28 deletions
diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py index 47c3fdb4a..84cdcc15b 100644 --- a/sphinx/writers/html.py +++ b/sphinx/writers/html.py @@ -579,6 +579,21 @@ class HTMLTranslator(SphinxTranslator, BaseTranslator): 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} @@ -586,6 +601,12 @@ class HTMLTranslator(SphinxTranslator, BaseTranslator): 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">' % @@ -596,20 +617,6 @@ class HTMLTranslator(SphinxTranslator, BaseTranslator): 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 diff --git a/sphinx/writers/html5.py b/sphinx/writers/html5.py index aebe1e428..087c92842 100644 --- a/sphinx/writers/html5.py +++ b/sphinx/writers/html5.py @@ -520,6 +520,21 @@ class HTML5Translator(SphinxTranslator, BaseTranslator): 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} @@ -527,6 +542,12 @@ class HTML5Translator(SphinxTranslator, BaseTranslator): 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">' % @@ -537,20 +558,6 @@ class HTML5Translator(SphinxTranslator, BaseTranslator): 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 |
