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
in an admonition
* #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
--------

View File

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