Merge branch '1.8'

Resolved Conflicts:
	sphinx/writers/latex.py
This commit is contained in:
jfbu 2019-01-17 17:26:51 +01:00
commit ff5031c96e
3 changed files with 34 additions and 21 deletions

View File

@ -218,6 +218,7 @@ Bugs fixed
* #5936: LaTeX: PDF build broken by inclusion of image taller than page height * #5936: LaTeX: PDF build broken by inclusion of image taller than page height
in an admonition in an admonition
* #5231: "make html" does not read and build "po" files in "locale" dir * #5231: "make html" does not read and build "po" files in "locale" dir
* #5954: ``:scale:`` image option may break PDF build if image in an admonition
Testing Testing
-------- --------

View File

@ -421,21 +421,32 @@ def escape_abbr(text):
return re.sub(r'\.(?=\s|$)', r'.\@', text) return re.sub(r'\.(?=\s|$)', r'.\@', text)
def rstdim_to_latexdim(width_str): def rstdim_to_latexdim(width_str, scale = 100):
# type: (str) -> str # type: (str, int) -> str
"""Convert `width_str` with rst length to LaTeX length.""" """Convert `width_str` with rst length to LaTeX length."""
match = re.match(r'^(\d*\.?\d*)\s*(\S*)$', width_str) match = re.match(r'^(\d*\.?\d*)\s*(\S*)$', width_str)
if not match: if not match:
raise ValueError raise ValueError
res = width_str res = width_str
amount, unit = match.groups()[:2] amount, unit = match.groups()[:2]
float(amount) # validate amount is float if scale == 100:
if unit in ('', "px"): float(amount) # validate amount is float
res = "%s\\sphinxpxdimen" % amount if unit in ('', "px"):
elif unit == 'pt': res = "%s\\sphinxpxdimen" % amount
res = '%sbp' % amount # convert to 'bp' elif unit == 'pt':
elif unit == "%": res = '%sbp' % amount # convert to 'bp'
res = "%.3f\\linewidth" % (float(amount) / 100.0) elif unit == "%":
res = "%.3f\\linewidth" % (float(amount) / 100.0)
else:
amount_float = float(amount) * scale / 100.0
if unit in ('', "px"):
res = "%.5f\\sphinxpxdimen" % amount_float
elif unit == 'pt':
res = '%.5fbp' % amount_float
elif unit == "%":
res = "%.5f\\linewidth" % (amount_float / 100.0)
else:
res = "%.5f%s" % (amount_float, unit)
return res return res
@ -1502,10 +1513,10 @@ class LaTeXTranslator(SphinxTranslator):
# type: (nodes.Element) -> None # type: (nodes.Element) -> None
pass pass
def latex_image_length(self, width_str): def latex_image_length(self, width_str, scale = 100):
# type: (str) -> str # type: (str, int) -> str
try: try:
return rstdim_to_latexdim(width_str) return rstdim_to_latexdim(width_str, scale)
except ValueError: except ValueError:
logger.warning(__('dimension unit %s is invalid. Ignored.'), width_str) logger.warning(__('dimension unit %s is invalid. Ignored.'), width_str)
return None return None
@ -1524,20 +1535,21 @@ class LaTeXTranslator(SphinxTranslator):
include_graphics_options = [] include_graphics_options = []
is_inline = self.is_inline(node) is_inline = self.is_inline(node)
if 'width' in attrs: if 'width' in attrs:
w = self.latex_image_length(attrs['width']) if 'scale' in attrs:
w = self.latex_image_length(attrs['width'], attrs['scale'])
else:
w = self.latex_image_length(attrs['width'])
if w: if w:
include_graphics_options.append('width=%s' % w) include_graphics_options.append('width=%s' % w)
if 'height' in attrs: if 'height' in attrs:
h = self.latex_image_length(attrs['height']) if 'scale' in attrs:
h = self.latex_image_length(attrs['height'], attrs['scale'])
else:
h = self.latex_image_length(attrs['height'])
if h: if h:
include_graphics_options.append('height=%s' % h) include_graphics_options.append('height=%s' % h)
if 'scale' in attrs: if 'scale' in attrs:
if include_graphics_options: if not include_graphics_options:
# unfortunately passing "height=1cm,scale=2.0" to \includegraphics
# does not result in a height of 2cm. We must scale afterwards.
pre.append('\\scalebox{%f}{' % (attrs['scale'] / 100.0,))
post.append('}')
else:
# if no "width" nor "height", \sphinxincludegraphics will fit # if no "width" nor "height", \sphinxincludegraphics will fit
# to the available text width if oversized after rescaling. # to the available text width if oversized after rescaling.
include_graphics_options.append('scale=%s' include_graphics_options.append('scale=%s'

View File

@ -1260,7 +1260,7 @@ def test_latex_image_in_parsed_literal(app, status, warning):
result = (app.outdir / 'python.tex').text(encoding='utf8') result = (app.outdir / 'python.tex').text(encoding='utf8')
assert ('{\\sphinxunactivateextrasandspace \\raisebox{-0.5\\height}' assert ('{\\sphinxunactivateextrasandspace \\raisebox{-0.5\\height}'
'{\\scalebox{2.000000}{\\sphinxincludegraphics[height=1cm]{{pic}.png}}}' '{\\sphinxincludegraphics[height=2.00000cm]{{pic}.png}}'
'}AFTER') in result '}AFTER') in result