From 65507cfe9403713ec1d510011eb675f4947d1525 Mon Sep 17 00:00:00 2001 From: rocky Date: Fri, 2 Apr 2021 21:47:55 -0400 Subject: [PATCH 1/2] Make axis text opacity 0.5 More generally also allow InsetBox opacity to be specified. --- mathics/builtin/graphics.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/mathics/builtin/graphics.py b/mathics/builtin/graphics.py index b208afcef8..7b58e6ab02 100644 --- a/mathics/builtin/graphics.py +++ b/mathics/builtin/graphics.py @@ -119,25 +119,26 @@ def cut(value): return value -def create_css(edge_color=None, face_color=None, stroke_width=None, font_color=None): +def create_css(edge_color=None, face_color=None, stroke_width=None, font_color=None, opacity=1.0): css = [] if edge_color is not None: - color, opacity = edge_color.to_css() + color, stroke_opacity = edge_color.to_css() css.append("stroke: %s" % color) - css.append("stroke-opacity: %s" % opacity) + css.append("stroke-opacity: %s" % stroke_opacity) else: css.append("stroke: none") if stroke_width is not None: css.append("stroke-width: %fpx" % stroke_width) if face_color is not None: - color, opacity = face_color.to_css() + color, fill_opacity = face_color.to_css() css.append("fill: %s" % color) - css.append("fill-opacity: %s" % opacity) + css.append("fill-opacity: %s" % fill_opacity) else: css.append("fill: none") if font_color is not None: - color, opacity = font_color.to_css() + color, _ = font_color.to_css() css.append("color: %s" % color) + css.append("opacity: %s" % opacity) return "; ".join(css) @@ -537,11 +538,12 @@ def convert(content): class _GraphicsElement(InstanceableBuiltin): - def init(self, graphics, item=None, style=None): + def init(self, graphics, item=None, style=None, opacity=1.0): if item is not None and not item.has_form(self.get_name(), None): raise BoxConstructError self.graphics = graphics self.style = style + self.opacity = opacity self.is_completely_visible = False # True for axis elements @staticmethod @@ -2567,12 +2569,13 @@ def default_arrow(px, py, vx, vy, t1, s): class InsetBox(_GraphicsElement): - def init(self, graphics, style, item=None, content=None, pos=None, opos=(0, 0)): + def init(self, graphics, style, item=None, content=None, pos=None, opos=(0, 0), opacity=1.0): super(InsetBox, self).init(graphics, item, style) self.color = self.style.get_option("System`FontColor") if self.color is None: self.color, _ = style.get_style(_Color, face_element=False) + self.opacity = opacity if item is not None: if len(item.leaves) not in (1, 2, 3): @@ -2615,7 +2618,7 @@ def to_svg(self, offset=None): svg = "\n" + content + "\n" else: style = create_css( - font_color=self.color, edge_color=self.color, face_color=self.color + font_color=self.color, edge_color=self.color, face_color=self.color, opacity=self.opacity ) content = self.content.boxes_to_text(evaluation=self.graphics.evaluation) svg = ( @@ -3477,6 +3480,7 @@ def add_element(element): elements, pos=p_origin(x), d=p_self0(-tick_label_d) ), opos=p_self0(1), + opacity=0.5, ) ) for x in ticks_small: From d824b18c6f1be0e4183270930bf3605278495e2c Mon Sep 17 00:00:00 2001 From: rocky Date: Fri, 2 Apr 2021 22:06:36 -0400 Subject: [PATCH 2/2] Simplify svg formatting Split parameters in template into text_pos_opts, text_style_opts, and css_style. --- mathics/builtin/graphics.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/mathics/builtin/graphics.py b/mathics/builtin/graphics.py index 7b58e6ab02..86ff0c4368 100644 --- a/mathics/builtin/graphics.py +++ b/mathics/builtin/graphics.py @@ -2617,15 +2617,17 @@ def to_svg(self, offset=None): content = self.content.to_svg(noheader=True, offset=(x, y)) svg = "\n" + content + "\n" else: - style = create_css( + css_style = create_css( font_color=self.color, edge_color=self.color, face_color=self.color, opacity=self.opacity ) + text_pos_opts = f'x="{x}" y="{y}" ox="{self.opos[0]}" oy="{self.opos[1]}"' + # FIXME: don't hard code text_style_opts, but allow these to be adjustable. + text_style_opts = "text-anchor:middle; dominant-baseline:middle;" content = self.content.boxes_to_text(evaluation=self.graphics.evaluation) svg = ( - '' - "%s" - "" - ) % (x, y, self.opos[0], self.opos[1], style, content,) + f'{content}' + ) + # content = self.content.boxes_to_mathml(evaluation=self.graphics.evaluation) # style = create_css(font_color=self.color) # svg = (