Merge pull request #7360 from tk0miya/7357_ValueError_on_resizing_SVG

Fix #7357: html: Resizing SVG image fails with ValueError
This commit is contained in:
Takeshi KOMIYA 2020-03-22 23:39:17 +09:00 committed by GitHub
commit 9ae34e1757
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 6 deletions

View File

@ -136,6 +136,7 @@ Bugs fixed
* #5637: inheritance_diagram: Incorrect handling of nested class names * #5637: inheritance_diagram: Incorrect handling of nested class names
* #7139: ``code-block:: guess`` does not work * #7139: ``code-block:: guess`` does not work
* #7325: html: source_suffix containing dot leads to wrong source link * #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 * #7278: html search: Fix use of ``html_file_suffix`` instead of
``html_link_suffix`` in search results ``html_link_suffix`` in search results
* #7297: html theme: ``bizstyle`` does not support ``sidebarwidth`` * #7297: html theme: ``bizstyle`` does not support ``sidebarwidth``

View File

@ -11,6 +11,7 @@
import copy import copy
import os import os
import posixpath import posixpath
import re
import warnings import warnings
from typing import Any, Iterable, Tuple from typing import Any, Iterable, Tuple
from typing import cast from typing import cast
@ -38,6 +39,19 @@ logger = logging.getLogger(__name__)
# http://www.arnebrodowski.de/blog/write-your-own-restructuredtext-writer.html # 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): class HTMLWriter(Writer):
# override embed-stylesheet default value to 0. # override embed-stylesheet default value to 0.
@ -597,11 +611,10 @@ class HTMLTranslator(SphinxTranslator, BaseTranslator):
if 'height' in node: if 'height' in node:
atts['height'] = node['height'] atts['height'] = node['height']
if 'scale' in node: if 'scale' in node:
scale = node['scale'] / 100.0
if 'width' in atts: if 'width' in atts:
atts['width'] = int(atts['width']) * scale atts['width'] = multiply_length(atts['width'], node['scale'])
if 'height' in atts: if 'height' in atts:
atts['height'] = int(atts['height']) * scale atts['height'] = multiply_length(atts['height'], node['scale'])
atts['alt'] = node.get('alt', uri) atts['alt'] = node.get('alt', uri)
if 'align' in node: if 'align' in node:
atts['class'] = 'align-%s' % node['align'] atts['class'] = 'align-%s' % node['align']

View File

@ -10,6 +10,7 @@
import os import os
import posixpath import posixpath
import re
import warnings import warnings
from typing import Any, Iterable, Tuple from typing import Any, Iterable, Tuple
from typing import cast from typing import cast
@ -37,6 +38,19 @@ logger = logging.getLogger(__name__)
# http://www.arnebrodowski.de/blog/write-your-own-restructuredtext-writer.html # 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): class HTML5Translator(SphinxTranslator, BaseTranslator):
""" """
Our custom HTML translator. Our custom HTML translator.
@ -538,11 +552,10 @@ class HTML5Translator(SphinxTranslator, BaseTranslator):
if 'height' in node: if 'height' in node:
atts['height'] = node['height'] atts['height'] = node['height']
if 'scale' in node: if 'scale' in node:
scale = node['scale'] / 100.0
if 'width' in atts: if 'width' in atts:
atts['width'] = int(atts['width']) * scale atts['width'] = multiply_length(atts['width'], node['scale'])
if 'height' in atts: if 'height' in atts:
atts['height'] = int(atts['height']) * scale atts['height'] = multiply_length(atts['height'], node['scale'])
atts['alt'] = node.get('alt', uri) atts['alt'] = node.get('alt', uri)
if 'align' in node: if 'align' in node:
atts['class'] = 'align-%s' % node['align'] atts['class'] = 'align-%s' % node['align']