Skip to content

Commit 68d0dcd

Browse files
committed
EquationStyle: allow setting font size and color
- fallback to the parent's style if these are not set - rename InlineEquation to Equation since it takes an inline argument - rename Equation to DisplayEquation
1 parent 29b7892 commit 68d0dcd

12 files changed

Lines changed: 137 additions & 55 deletions

File tree

src/rinoh/data/stylesheets/sphinx.rts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ typeface=$(mono_typeface)
5252
hyphenate=False
5353
ligatures=False
5454

55-
[inline math]
56-
base=monospaced
57-
5855
[quote]
5956
font_slant=italic
6057

@@ -160,14 +157,17 @@ padding_bottom=3pt
160157
base=caption
161158
keep_with_next=true
162159

163-
[equation]
160+
[math block]
164161
label_position=right
165162

166-
[equation paragraph]
163+
[math block paragraph]
164+
base=default
167165
border=none
168166
tab_stops=50% CENTER, 100% RIGHT
169167

170-
[equation label]
168+
[math block equation]
169+
170+
[math block label]
171171
number_format=number
172172
numbering_level = 1
173173
label_prefix = '('

src/rinoh/frontend/rst/nodes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def _ids(self):
256256
return [self.get('label'), *self.get('ids')]
257257

258258
def build_flowable(self):
259-
return rt.Equation(self.text)
259+
return rt.DisplayEquation(self.text)
260260

261261

262262
class Admonition(DocutilsGroupingNode):
@@ -342,7 +342,7 @@ class Math(Inline):
342342
style = 'math'
343343

344344
def build_styled_text(self):
345-
return rt.InlineEquation(self.text)
345+
return rt.Equation(self.text)
346346

347347

348348
class Superscript(DocutilsInlineNode):

src/rinoh/math.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11

22
from io import BytesIO
33

4-
from .attribute import OverrideDefault
5-
from .dimension import PT
4+
from .attribute import Attribute, OverrideDefault
5+
from .color import BLACK, Color
6+
from .dimension import Dimension, PT
67
from .flowable import LabeledFlowable
78
from .image import InlineImage
8-
from .inline import InlineFlowable
9+
from .inline import InlineFlowable, InlineFlowableStyle
910
from .number import Label, NumberFormat, NumberStyle
1011
from .paragraph import Paragraph, ParagraphBase, ParagraphStyle
1112
from .text import MixedStyledText, Tab
1213

1314

14-
__all__ = ['Equation', 'EquationLabel', 'InlineEquation']
15+
__all__ = ['DisplayEquation', 'EquationLabel', 'Equation']
1516

1617

1718
try:
@@ -43,34 +44,47 @@ def text(self, container):
4344
return MixedStyledText(label, parent=self)
4445

4546

46-
class Equation(LabeledFlowable):
47+
class DisplayEquation(LabeledFlowable):
4748

4849
category = 'equation'
4950

5051
def __init__(self, latex_equation, custom_label=None,
5152
id=None, style=None, parent=None, source=None):
52-
paragraph = Paragraph(Tab() + InlineEquation(latex_equation, inline=False))
53+
paragraph = Paragraph(Tab() + Equation(latex_equation, inline=False))
5354
label = EquationLabel()
5455
super().__init__(label, paragraph, id=id, style=style, parent=parent)
5556

5657

57-
class InlineEquation(InlineFlowable):
58+
class EquationStyle(InlineFlowableStyle):
59+
font_size = Attribute(Dimension, 9*PT, 'Height of characters')
60+
font_color = Attribute(Color, BLACK, 'Color of the font')
61+
62+
63+
class Equation(InlineFlowable):
64+
style_class = EquationStyle
65+
5866
def __init__(self, latex_equation, inline=True, id=None, style=None, parent=None, source=None):
59-
if not MATH_ENABLED:
60-
equation = baseline = None
61-
else:
62-
equation = ziamath.Latex(latex_equation, inline=inline)
63-
baseline = - equation.getyofst() * PT
64-
super().__init__(baseline, id, style, parent, source)
67+
super().__init__(id=id, style=style, parent=parent, source=source)
6568
self.latex_equation = latex_equation
66-
self.equation = equation
69+
self.inline = inline
70+
71+
def fallback_to_parent(self, attribute):
72+
return attribute in ('font_size', 'font_color')
73+
74+
def prepare(self, flowable_target):
75+
super().prepare(flowable_target)
76+
if MATH_ENABLED:
77+
size = float(self.get_style('font_size', flowable_target))
78+
color = str(self.get_style('font_color', flowable_target))
79+
self._math = ziamath.Latex(self.latex_equation, inline=self.inline,
80+
size=size, color=color)
81+
self.baseline = - self._math.getyofst() * PT
6782

6883
def render(self, container, descender, state, space_below=0, **kwargs):
69-
if self.equation is None:
84+
if not MATH_ENABLED:
7085
self.warn("Math rendering requires the 'ziamath' and "
7186
"'cairosvg' packages to be installed.", container)
7287
return 0, 0, 0
73-
else:
74-
pdf_file = BytesIO(cairosvg.svg2pdf(self.equation.svg()))
75-
inline_image = InlineImage(pdf_file, baseline=self.baseline)
76-
return inline_image.render(container, descender, state, **kwargs)
88+
pdf_file = BytesIO(cairosvg.svg2pdf(self._math.svg()))
89+
inline_image = InlineImage(pdf_file)
90+
return inline_image.render(container, descender, state, **kwargs)

src/rinoh/styleds.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from .image import Figure, Caption
1212
from .image import ListOfFiguresSection, ListOfFigures
1313
from .image import Image, InlineImage
14-
from .math import Equation, EquationLabel, InlineEquation
14+
from .math import DisplayEquation, EquationLabel, Equation
1515
from .flowable import Flowable, Float
1616
from .flowable import GroupedFlowables, StaticGroupedFlowables
1717
from .flowable import LabeledFlowable, GroupedLabeledFlowables

src/rinoh/stylesheets/matcher.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
'strong': StyledText.like('strong'),
3737
'literal emphasis': StyledText.like('literal emphasis'),
3838
'literal strong': StyledText.like('literal strong'),
39-
'inline math': StyledText.like('math'),
39+
'inline equation': Equation.like(inline=True),
4040

4141
'inline definition': StyledText.like('definition'),
4242
'quote': StyledText.like('quote'),
@@ -74,16 +74,17 @@
7474
matcher('body', Paragraph)
7575
matcher('code block', +CodeBlock)
7676
matcher('code block caption', CodeBlockWithCaption / Caption)
77-
matcher('equation', Equation)
78-
matcher('equation paragraph', 'equation' / +Paragraph)
79-
matcher('equation label', EquationLabel)
8077
matcher('graphviz code block', Paragraph.like('graphviz'))
8178
matcher('attribution', Paragraph.like('attribution'))
8279
matcher('centered', Paragraph.like('centered'))
8380
matcher('line block', Paragraph.like('line block'))
8481

8582
matcher('block quote', GroupedFlowables.like('block quote'))
8683

84+
matcher('math block', DisplayEquation)
85+
matcher('math block paragraph', 'math block' / +Paragraph)
86+
matcher('math block equation', 'math block paragraph' / Equation)
87+
matcher('math block label', EquationLabel)
8788

8889
#
8990

tests_regression/rst/math.pdf

7.14 KB
Binary file not shown.

tests_regression/rst/math.pxml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,10 @@
1111
<reference name="myeq" refid="myeq">
1212
myeq
1313
.
14+
<paragraph>
15+
The area of a circle is
16+
<math classes="large">
17+
A_\text{c} = (\pi/4) d^2
18+
.
19+
<math_block classes="large" xml:space="preserve">
20+
\frac{ \sum_{t=0}^{N}f(t,k) }{N}

tests_regression/rst/math.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,15 @@ The area of a circle is :math:`A_\text{c} = (\pi/4) d^2`.
88
99
1010
Referencing myeq_.
11+
12+
13+
.. role:: large-math(math)
14+
:class: large
15+
16+
17+
The area of a circle is :large-math:`A_\text{c} = (\pi/4) d^2`.
18+
19+
.. math::
20+
:class: large
21+
22+
\frac{ \sum_{t=0}^{N}f(t,k) }{N}

tests_regression/rst/math.rts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[STYLESHEET]
2+
base = sphinx_base14
3+
4+
5+
[default:Paragraph]
6+
font_size = 12pt
7+
8+
[math block paragraph]
9+
font_color = #F00
10+
11+
[large inline equation : Equation(has_class='large', inline=True)]
12+
font_size = 14pt
13+
font_color = #0000FF
14+
15+
[large equation : DisplayEquation(has_class='large') / ... / Equation]
16+
font_size = 20pt

tests_regression/rst/math.stylelog

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,32 @@
88
#### ChainedContainer('column1')
99
StaticGroupedFlowables()
1010
DocumentTree() math.rst <document>
11-
Paragraph('The area of a circle is InlineEq...') math.rst:2 <paragraph>
11+
Paragraph('The area of a circle is Equation...') math.rst:2 <paragraph>
1212
> (0,0,0,0,2) body [Sphinx] > default
13-
MixedStyledText('The area of a circle is InlineEq...')
14-
MixedStyledText('The area of a circle is InlineEq...')
13+
MixedStyledText('The area of a circle is Equation...')
14+
MixedStyledText('The area of a circle is Equation...')
1515
SingleStyledText('The area of a circle is ')
16-
InlineEquation() <math>
16+
Equation() <math>
17+
x (0,0,0,1,2) inline equation
1718
SingleStyledText('.')
18-
InlineEquation() <math>
19-
Equation() math.rst:4 <math_block>
20-
> (0,0,0,0,2) equation [Sphinx] > DEFAULT
19+
Equation() <math>
20+
x (0,0,0,1,2) inline equation
21+
DisplayEquation() math.rst:4 <math_block>
22+
> (0,0,0,0,2) math block [Sphinx] > DEFAULT
2123
EquationLabel('(1)')
22-
> (0,0,0,0,2) equation label [Sphinx] > DEFAULT
24+
> (0,0,0,0,2) math block label [Sphinx] > DEFAULT
2325
MixedStyledText('(1)', style='label')
2426
SingleStyledText('(')
2527
SingleStyledText('1')
2628
SingleStyledText(')')
27-
Paragraph(' InlineEquation')
28-
> (1,0,0,0,4) equation paragraph [Sphinx] > DEFAULT
29+
Paragraph(' Equation')
30+
> (1,0,0,0,4) math block paragraph [math.rts] > DEFAULT
2931
(0,0,0,0,2) body [Sphinx] > default
30-
MixedStyledText(' InlineEquation')
31-
MixedStyledText(' InlineEquation')
32+
MixedStyledText(' Equation')
33+
MixedStyledText(' Equation')
3234
Tab(' ')
33-
InlineEquation()
34-
InlineEquation()
35+
Equation()
36+
Equation()
3537
Paragraph('Referencing Equation 1.') math.rst:10 <paragraph>
3638
> (0,0,0,0,2) body [Sphinx] > default
3739
MixedStyledText('Referencing Equation 1.')
@@ -45,3 +47,33 @@
4547
SingleStyledText(' ')
4648
SingleStyledText('1')
4749
SingleStyledText('.')
50+
Paragraph('The area of a circle is Equation...') math.rst:17 <paragraph>
51+
> (0,0,0,0,2) body [Sphinx] > default
52+
MixedStyledText('The area of a circle is Equation...')
53+
MixedStyledText('The area of a circle is Equation...')
54+
SingleStyledText('The area of a circle is ')
55+
Equation() <math classes='large'>
56+
> (0,0,0,2,2) large inline equation [math.rts] > DEFAULT
57+
x (0,0,0,1,2) inline equation
58+
SingleStyledText('.')
59+
Equation() <math classes='large'>
60+
> (0,0,0,2,2) large inline equation [math.rts] > DEFAULT
61+
x (0,0,0,1,2) inline equation
62+
DisplayEquation() math.rst:19 <math_block classes='large'>
63+
> (0,0,0,0,2) math block [Sphinx] > DEFAULT
64+
EquationLabel('(2)')
65+
> (0,0,0,0,2) math block label [Sphinx] > DEFAULT
66+
MixedStyledText('(2)', style='label')
67+
SingleStyledText('(')
68+
SingleStyledText('2')
69+
SingleStyledText(')')
70+
Paragraph(' Equation')
71+
> (1,0,0,0,4) math block paragraph [math.rts] > DEFAULT
72+
(0,0,0,0,2) body [Sphinx] > default
73+
MixedStyledText(' Equation')
74+
MixedStyledText(' Equation')
75+
Tab(' ')
76+
Equation()
77+
> (0,0,0,1,4) large equation [math.rts] > DEFAULT
78+
Equation()
79+
> (0,0,0,1,4) large equation [math.rts] > DEFAULT

0 commit comments

Comments
 (0)