Skip to content

Commit 7484267

Browse files
authored
Merge pull request #1223 from mathics/svg-axis-and-text-opacity
Svg axis and text opacity
2 parents bd1f8bd + d824b18 commit 7484267

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

mathics/builtin/graphics.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,26 @@ def cut(value):
119119
return value
120120

121121

122-
def create_css(edge_color=None, face_color=None, stroke_width=None, font_color=None):
122+
def create_css(edge_color=None, face_color=None, stroke_width=None, font_color=None, opacity=1.0):
123123
css = []
124124
if edge_color is not None:
125-
color, opacity = edge_color.to_css()
125+
color, stroke_opacity = edge_color.to_css()
126126
css.append("stroke: %s" % color)
127-
css.append("stroke-opacity: %s" % opacity)
127+
css.append("stroke-opacity: %s" % stroke_opacity)
128128
else:
129129
css.append("stroke: none")
130130
if stroke_width is not None:
131131
css.append("stroke-width: %fpx" % stroke_width)
132132
if face_color is not None:
133-
color, opacity = face_color.to_css()
133+
color, fill_opacity = face_color.to_css()
134134
css.append("fill: %s" % color)
135-
css.append("fill-opacity: %s" % opacity)
135+
css.append("fill-opacity: %s" % fill_opacity)
136136
else:
137137
css.append("fill: none")
138138
if font_color is not None:
139-
color, opacity = font_color.to_css()
139+
color, _ = font_color.to_css()
140140
css.append("color: %s" % color)
141+
css.append("opacity: %s" % opacity)
141142
return "; ".join(css)
142143

143144

@@ -537,11 +538,12 @@ def convert(content):
537538

538539

539540
class _GraphicsElement(InstanceableBuiltin):
540-
def init(self, graphics, item=None, style=None):
541+
def init(self, graphics, item=None, style=None, opacity=1.0):
541542
if item is not None and not item.has_form(self.get_name(), None):
542543
raise BoxConstructError
543544
self.graphics = graphics
544545
self.style = style
546+
self.opacity = opacity
545547
self.is_completely_visible = False # True for axis elements
546548

547549
@staticmethod
@@ -2567,12 +2569,13 @@ def default_arrow(px, py, vx, vy, t1, s):
25672569

25682570

25692571
class InsetBox(_GraphicsElement):
2570-
def init(self, graphics, style, item=None, content=None, pos=None, opos=(0, 0)):
2572+
def init(self, graphics, style, item=None, content=None, pos=None, opos=(0, 0), opacity=1.0):
25712573
super(InsetBox, self).init(graphics, item, style)
25722574

25732575
self.color = self.style.get_option("System`FontColor")
25742576
if self.color is None:
25752577
self.color, _ = style.get_style(_Color, face_element=False)
2578+
self.opacity = opacity
25762579

25772580
if item is not None:
25782581
if len(item.leaves) not in (1, 2, 3):
@@ -2614,15 +2617,17 @@ def to_svg(self, offset=None):
26142617
content = self.content.to_svg(noheader=True, offset=(x, y))
26152618
svg = "\n" + content + "\n"
26162619
else:
2617-
style = create_css(
2618-
font_color=self.color, edge_color=self.color, face_color=self.color
2620+
css_style = create_css(
2621+
font_color=self.color, edge_color=self.color, face_color=self.color, opacity=self.opacity
26192622
)
2623+
text_pos_opts = f'x="{x}" y="{y}" ox="{self.opos[0]}" oy="{self.opos[1]}"'
2624+
# FIXME: don't hard code text_style_opts, but allow these to be adjustable.
2625+
text_style_opts = "text-anchor:middle; dominant-baseline:middle;"
26202626
content = self.content.boxes_to_text(evaluation=self.graphics.evaluation)
26212627
svg = (
2622-
'<text x="%f" y="%f" ox="%f" oy="%f" style="text-anchor:middle; dominant-baseline:middle; %s">'
2623-
"%s"
2624-
"</text>"
2625-
) % (x, y, self.opos[0], self.opos[1], style, content,)
2628+
f'<text {text_pos_opts} style="{text_style_opts} {css_style}">{content}</text>'
2629+
)
2630+
26262631
# content = self.content.boxes_to_mathml(evaluation=self.graphics.evaluation)
26272632
# style = create_css(font_color=self.color)
26282633
# svg = (
@@ -3477,6 +3482,7 @@ def add_element(element):
34773482
elements, pos=p_origin(x), d=p_self0(-tick_label_d)
34783483
),
34793484
opos=p_self0(1),
3485+
opacity=0.5,
34803486
)
34813487
)
34823488
for x in ticks_small:

0 commit comments

Comments
 (0)