Fix #7357: html: Resizing SVG image fails with ValueError

This commit is contained in:
Takeshi KOMIYA 2020-03-22 21:30:47 +09:00
parent eb00870b6f
commit 2659a37c1f
3 changed files with 33 additions and 6 deletions

View File

@ -135,6 +135,7 @@ Bugs fixed
* #5637: inheritance_diagram: Incorrect handling of nested class names
* #7139: ``code-block:: guess`` does not work
* #7325: html: source_suffix containing dot leads to wrong source link
* #7357: html: Resizing SVG image fails with ValueError
* #7278: html search: Fix use of ``html_file_suffix`` instead of
``html_link_suffix`` in search results
* #7297: html theme: ``bizstyle`` does not support ``sidebarwidth``

View File

@ -11,6 +11,7 @@
import copy
import os
import posixpath
import re
import warnings
from typing import Any, Iterable, Tuple
from typing import cast
@ -38,6 +39,19 @@ logger = logging.getLogger(__name__)
# http://www.arnebrodowski.de/blog/write-your-own-restructuredtext-writer.html
def multiply_length(length: str, scale: int) -> str:
"""Multiply *length* (width or height) by *scale*."""
matched = re.match(r'^(\d*\.?\d*)\s*(\S*)$', length)
if not matched:
return length
elif scale == 100:
return length
else:
amount, unit = matched.groups()
result = float(amount) * scale / 100
return "%s%s" % (int(result), unit)
class HTMLWriter(Writer):
# override embed-stylesheet default value to 0.
@ -597,11 +611,10 @@ class HTMLTranslator(SphinxTranslator, BaseTranslator):
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
atts['width'] = multiply_length(atts['width'], node['scale'])
if 'height' in atts:
atts['height'] = int(atts['height']) * scale
atts['height'] = multiply_length(atts['height'], node['scale'])
atts['alt'] = node.get('alt', uri)
if 'align' in node:
atts['class'] = 'align-%s' % node['align']

View File

@ -10,6 +10,7 @@
import os
import posixpath
import re
import warnings
from typing import Any, Iterable, Tuple
from typing import cast
@ -37,6 +38,19 @@ logger = logging.getLogger(__name__)
# http://www.arnebrodowski.de/blog/write-your-own-restructuredtext-writer.html
def multiply_length(length: str, scale: int) -> str:
"""Multiply *length* (width or height) by *scale*."""
matched = re.match(r'^(\d*\.?\d*)\s*(\S*)$', length)
if not matched:
return length
elif scale == 100:
return length
else:
amount, unit = matched.groups()
result = float(amount) * scale / 100
return "%s%s" % (int(result), unit)
class HTML5Translator(SphinxTranslator, BaseTranslator):
"""
Our custom HTML translator.
@ -538,11 +552,10 @@ class HTML5Translator(SphinxTranslator, BaseTranslator):
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
atts['width'] = multiply_length(atts['width'], node['scale'])
if 'height' in atts:
atts['height'] = int(atts['height']) * scale
atts['height'] = multiply_length(atts['height'], node['scale'])
atts['alt'] = node.get('alt', uri)
if 'align' in node:
atts['class'] = 'align-%s' % node['align']