Skip to content

Commit fe7003d

Browse files
authored
Merge pull request #709 from Mathics3/allow-asy-pen-font-size
Allow fontsize on Asymptote pen creation...
2 parents 664cebd + 45c1ec0 commit fe7003d

File tree

4 files changed

+81
-49
lines changed

4 files changed

+81
-49
lines changed

mathics/builtin/box/graphics.py

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -692,26 +692,40 @@ def boxes_to_svg(self, elements=None, **options) -> str:
692692
svg_body = format_fn(self, elements, data=data, **options)
693693
return svg_body
694694

695-
def create_axes(self, elements, graphics_options, xmin, xmax, ymin, ymax):
695+
def create_axes(self, elements, graphics_options, xmin, xmax, ymin, ymax) -> tuple:
696+
697+
# Note that Asymptote has special commands for drawing axes, like "xaxis"
698+
# "yaxis", "xtick" "labelx", "labely". Entend our language
699+
# here and use those in render-like routines.
700+
696701
use_log_for_y_axis = graphics_options.get("System`LogPlot", False)
697-
axes = graphics_options.get("System`Axes")
698-
if axes is SymbolTrue:
702+
axes_option = graphics_options.get("System`Axes")
703+
704+
if axes_option is SymbolTrue:
699705
axes = (True, True)
700-
elif axes.has_form("List", 2):
701-
axes = (axes.elements[0] is SymbolTrue, axes.elements[1] is SymbolTrue)
706+
elif axes_option.has_form("List", 2):
707+
axes = (
708+
axes_option.elements[0] is SymbolTrue,
709+
axes_option.elements[1] is SymbolTrue,
710+
)
702711
else:
703712
axes = (False, False)
704-
ticks_style = graphics_options.get("System`TicksStyle")
705-
axes_style = graphics_options.get("System`AxesStyle")
713+
714+
# The Style option pushes its setting down into graphics components
715+
# like ticks, axes, and labels.
716+
ticks_style_option = graphics_options.get("System`TicksStyle")
717+
axes_style_option = graphics_options.get("System`AxesStyle")
706718
label_style = graphics_options.get("System`LabelStyle")
707-
if ticks_style.has_form("List", 2):
708-
ticks_style = ticks_style.elements
719+
720+
if ticks_style_option.has_form("List", 2):
721+
ticks_style = ticks_style_option.elements
709722
else:
710-
ticks_style = [ticks_style] * 2
711-
if axes_style.has_form("List", 2):
712-
axes_style = axes_style.elements
723+
ticks_style = [ticks_style_option] * 2
724+
725+
if axes_style_option.has_form("List", 2):
726+
axes_style = axes_style_option.elements
713727
else:
714-
axes_style = [axes_style] * 2
728+
axes_style = [axes_style_option] * 2
715729

716730
ticks_style = [elements.create_style(s) for s in ticks_style]
717731
axes_style = [elements.create_style(s) for s in axes_style]
@@ -723,12 +737,16 @@ def add_element(element):
723737
element.is_completely_visible = True
724738
elements.elements.append(element)
725739

740+
# Units seem to be in point size units
741+
726742
ticks_x, ticks_x_small, origin_x = self.axis_ticks(xmin, xmax)
727743
ticks_y, ticks_y_small, origin_y = self.axis_ticks(ymin, ymax)
728744

729745
axes_extra = 6
746+
730747
tick_small_size = 3
731748
tick_large_size = 5
749+
732750
tick_label_d = 2
733751

734752
ticks_x_int = all(floor(x) == x for x in ticks_x)
@@ -791,8 +809,10 @@ def add_element(element):
791809
)
792810
)
793811
ticks_lines = []
812+
794813
tick_label_style = ticks_style[index].clone()
795814
tick_label_style.extend(label_style)
815+
796816
for x in ticks:
797817
ticks_lines.append(
798818
[
@@ -816,6 +836,7 @@ def add_element(element):
816836
content = String(
817837
"%g" % tick_value
818838
) # fix e.g. 0.6000000000000001
839+
819840
add_element(
820841
InsetBox(
821842
elements,
@@ -839,31 +860,32 @@ def add_element(element):
839860
add_element(LineBox(elements, axes_style[0], lines=ticks_lines))
840861
return axes
841862

842-
"""if axes[1]:
843-
add_element(LineBox(elements, axes_style[1], lines=[[Coords(elements, pos=(origin_x,ymin), d=(0,-axes_extra)),
844-
Coords(elements, pos=(origin_x,ymax), d=(0,axes_extra))]]))
845-
ticks = []
846-
tick_label_style = ticks_style[1].clone()
847-
tick_label_style.extend(label_style)
848-
for k in range(start_k_y, start_k_y+steps_y+1):
849-
if k != origin_k_y:
850-
y = k * step_y
851-
if y > ymax:
852-
break
853-
pos = (origin_x,y)
854-
ticks.append([Coords(elements, pos=pos),
855-
Coords(elements, pos=pos, d=(tick_large_size,0))])
856-
add_element(InsetBox(elements, tick_label_style, content=Real(y), pos=Coords(elements, pos=pos,
857-
d=(-tick_label_d,0)), opos=(1,0)))
858-
for k in range(start_k_y_small, start_k_y_small+steps_y_small+1):
859-
if k % sub_y != 0:
860-
y = k * step_y_small
861-
if y > ymax:
862-
break
863-
pos = (origin_x,y)
864-
ticks.append([Coords(elements, pos=pos),
865-
Coords(elements, pos=pos, d=(tick_small_size,0))])
866-
add_element(LineBox(elements, axes_style[1], lines=ticks))"""
863+
# Old code?
864+
# if axes[1]:
865+
# add_element(LineBox(elements, axes_style[1], lines=[[Coords(elements, pos=(origin_x,ymin), d=(0,-axes_extra)),
866+
# Coords(elements, pos=(origin_x,ymax), d=(0,axes_extra))]]))
867+
# ticks = []
868+
# tick_label_style = ticks_style[1].clone()
869+
# tick_label_style.extend(label_style)
870+
# for k in range(start_k_y, start_k_y+steps_y+1):
871+
# if k != origin_k_y:
872+
# y = k * step_y
873+
# if y > ymax:
874+
# break
875+
# pos = (origin_x,y)
876+
# ticks.append([Coords(elements, pos=pos),
877+
# Coords(elements, pos=pos, d=(tick_large_size,0))])
878+
# add_element(InsetBox(elements, tick_label_style, content=Real(y), pos=Coords(elements, pos=pos,
879+
# d=(-tick_label_d,0)), opos=(1,0)))
880+
# for k in range(start_k_y_small, start_k_y_small+steps_y_small+1):
881+
# if k % sub_y != 0:
882+
# y = k * step_y_small
883+
# if y > ymax:
884+
# break
885+
# pos = (origin_x,y)
886+
# ticks.append([Coords(elements, pos=pos),
887+
# Coords(elements, pos=pos, d=(tick_small_size,0))])
888+
# add_element(LineBox(elements, axes_style[1], lines=ticks))
867889

868890

869891
class FilledCurveBox(_GraphicsElementBox):

mathics/format/asy.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,11 @@ def inset_box(self, **options) -> str:
411411
x, y = self.pos.pos()
412412
opacity_value = self.opacity.opacity if self.opacity else None
413413
content = self.content.boxes_to_tex(evaluation=self.graphics.evaluation)
414-
pen = asy_create_pens(edge_color=self.color, edge_opacity=opacity_value)
414+
# FIXME: don't hard code text_style_opts, but allow these to be adjustable.
415+
font_size = 3
416+
pen = asy_create_pens(
417+
edge_color=self.color, edge_opacity=opacity_value, fontsize=font_size
418+
)
415419
asy = """// InsetBox
416420
label("$%s$", (%s,%s), (%s,%s), %s);\n""" % (
417421
content,

mathics/format/asy_fns.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"""
66

77
from itertools import chain
8+
from typing import Optional, Type, Union
9+
10+
RealType = Type[Union[int, float]]
811

912

1013
def asy_add_bezier_fn(self) -> str:
@@ -103,13 +106,14 @@ def asy_color(self):
103106

104107

105108
def asy_create_pens(
106-
edge_color=None,
107-
face_color=None,
108-
edge_opacity=None,
109-
face_opacity=None,
109+
edge_color: Optional[str] = None,
110+
face_color: Optional[str] = None,
111+
edge_opacity: Optional[RealType] = None,
112+
face_opacity: Optional[RealType] = None,
110113
stroke_width=None,
111114
is_face_element=False,
112115
dotfactor=None,
116+
fontsize: Optional[RealType] = None,
113117
) -> str:
114118
"""
115119
Return an asymptote string fragment that creates a drawing pen.
@@ -132,6 +136,8 @@ def asy_create_pens(
132136
opacity = edge_opacity
133137
if opacity is not None and opacity != 1:
134138
pen += f"+opacity({asy_number(opacity)})"
139+
if fontsize is not None:
140+
pen += f"+fontsize({fontsize})"
135141
if stroke_width is not None:
136142
pen += f"+linewidth({asy_number(stroke_width)})"
137143
result.append(pen)

test/format/test_format.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -588,10 +588,10 @@
588588
"System`OutputForm": '<mglyph width="294px" height="350px" src="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjEuMHB4IiBoZWlnaHQ9IjI1LjBweCIgeG1sbnM6c3ZnPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIKICAgICAgICAgICAgICAgIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIKICAgICAgICAgICAgICAgIHZlcnNpb249IjEuMSIKICAgICAgICAgICAgICAgIHZpZXdCb3g9IjEzNi41MDAwMDAgMTYyLjUwMDAwMCAyMS4wMDAwMDAgMjUuMDAwMDAwIj4KICAgICAgICAgICAgICAgIDwhLS1HcmFwaGljc0VsZW1lbnRzLS0+Cjx0ZXh0IHg9IjE0Ny4wIiB5PSIxNzUuMCIgb3g9IjAiIG95PSIwIiBmb250LXNpemU9IjEwcHgiIHN0eWxlPSJ0ZXh0LWFuY2hvcjplbmQ7IGRvbWluYW50LWJhc2VsaW5lOmhhbmdpbmc7IHN0cm9rZTogcmdiKDAuMDAwMDAwJSwgMC4wMDAwMDAlLCAwLjAwMDAwMCUpOyBzdHJva2Utb3BhY2l0eTogMTsgZmlsbDogcmdiKDAuMDAwMDAwJSwgMC4wMDAwMDAlLCAwLjAwMDAwMCUpOyBmaWxsLW9wYWNpdHk6IDE7IGNvbG9yOiByZ2IoMC4wMDAwMDAlLCAwLjAwMDAwMCUsIDAuMDAwMDAwJSk7IG9wYWNpdHk6IDEuMCI+YV5iPC90ZXh0Pgo8L3N2Zz4K"/>',
589589
},
590590
"latex": {
591-
"System`StandardForm": '\n\\begin{asy}\nusepackage("amsmath");\nsize(4.9cm, 5.8333cm);\n\n// InsetBox\nlabel("$a^b$", (147.0,175.0), (0,0), rgb(0, 0, 0));\n\nclip(box((136.5,162.5), (157.5,187.5)));\n\n\\end{asy}\n',
592-
"System`TraditionalForm": '\n\\begin{asy}\nusepackage("amsmath");\nsize(4.9cm, 5.8333cm);\n\n// InsetBox\nlabel("$a^b$", (147.0,175.0), (0,0), rgb(0, 0, 0));\n\nclip(box((136.5,162.5), (157.5,187.5)));\n\n\\end{asy}\n',
591+
"System`StandardForm": '\n\\begin{asy}\nusepackage("amsmath");\nsize(4.9cm, 5.8333cm);\n\n// InsetBox\nlabel("$a^b$", (147.0,175.0), (0,0), rgb(0, 0, 0)+fontsize(3));\n\nclip(box((136.5,162.5), (157.5,187.5)));\n\n\\end{asy}\n',
592+
"System`TraditionalForm": '\n\\begin{asy}\nusepackage("amsmath");\nsize(4.9cm, 5.8333cm);\n\n// InsetBox\nlabel("$a^b$", (147.0,175.0), (0,0), rgb(0, 0, 0)+fontsize(3));\n\nclip(box((136.5,162.5), (157.5,187.5)));\n\n\\end{asy}\n',
593593
"System`InputForm": "\\text{Graphics}\\left[\\left\\{\\text{Text}\\left[\\text{Power}\\left[a, b\\right], \\left\\{0, 0\\right\\}\\right]\\right\\}\\right]",
594-
"System`OutputForm": '\n\\begin{asy}\nusepackage("amsmath");\nsize(4.9cm, 5.8333cm);\n\n// InsetBox\nlabel("$a^b$", (147.0,175.0), (0,0), rgb(0, 0, 0));\n\nclip(box((136.5,162.5), (157.5,187.5)));\n\n\\end{asy}\n',
594+
"System`OutputForm": '\n\\begin{asy}\nusepackage("amsmath");\nsize(4.9cm, 5.8333cm);\n\n// InsetBox\nlabel("$a^b$", (147.0,175.0), (0,0), rgb(0, 0, 0)+fontsize(3));\n\nclip(box((136.5,162.5), (157.5,187.5)));\n\n\\end{asy}\n',
595595
},
596596
},
597597
"TableForm[{Graphics[{Text[a^b,{0,0}]}], Graphics[{Text[a^b,{0,0}]}]}]": {
@@ -609,10 +609,10 @@
609609
"System`OutputForm": '<mtable columnalign="center">\n<mtr><mtd columnalign="center"><mglyph width="147px" height="175px" src="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjEuMHB4IiBoZWlnaHQ9IjI1LjBweCIgeG1sbnM6c3ZnPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIKICAgICAgICAgICAgICAgIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIKICAgICAgICAgICAgICAgIHZlcnNpb249IjEuMSIKICAgICAgICAgICAgICAgIHZpZXdCb3g9IjYzLjAwMDAwMCA3NS4wMDAwMDAgMjEuMDAwMDAwIDI1LjAwMDAwMCI+CiAgICAgICAgICAgICAgICA8IS0tR3JhcGhpY3NFbGVtZ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aWxsOiByZ2IoMC4wMDAwMDAlLCAwLjAwMDAwMCUsIDAuMDAwMDAwJSk7IGZpbGwtb3BhY2l0eTogMTsgY29sb3I6IHJnYigwLjAwMDAwMCUsIDAuMDAwMDAwJSwgMC4wMDAwMDAlKTsgb3BhY2l0eTogMS4wIj5hXmI8L3RleHQ+Cjwvc3ZnPgo="/></mtd></mtr>\n</mtable>',
610610
},
611611
"latex": {
612-
"System`StandardForm": '\\begin{array}{c} \n\\begin{asy}\nusepackage("amsmath");\nsize(2.45cm, 2.9167cm);\n\n// InsetBox\nlabel("$a^b$", (73.5,87.5), (0,0), rgb(0, 0, 0));\n\nclip(box((63,75), (84,100)));\n\n\\end{asy}\n\\\\ \n\\begin{asy}\nusepackage("amsmath");\nsize(2.45cm, 2.9167cm);\n\n// InsetBox\nlabel("$a^b$", (73.5,87.5), (0,0), rgb(0, 0, 0));\n\nclip(box((63,75), (84,100)));\n\n\\end{asy}\n\\end{array}',
613-
"System`TraditionalForm": '\\begin{array}{c} \n\\begin{asy}\nusepackage("amsmath");\nsize(2.45cm, 2.9167cm);\n\n// InsetBox\nlabel("$a^b$", (73.5,87.5), (0,0), rgb(0, 0, 0));\n\nclip(box((63,75), (84,100)));\n\n\\end{asy}\n\\\\ \n\\begin{asy}\nusepackage("amsmath");\nsize(2.45cm, 2.9167cm);\n\n// InsetBox\nlabel("$a^b$", (73.5,87.5), (0,0), rgb(0, 0, 0));\n\nclip(box((63,75), (84,100)));\n\n\\end{asy}\n\\end{array}',
612+
"System`StandardForm": '\\begin{array}{c} \n\\begin{asy}\nusepackage("amsmath");\nsize(2.45cm, 2.9167cm);\n\n// InsetBox\nlabel("$a^b$", (73.5,87.5), (0,0), rgb(0, 0, 0)+fontsize(3));\n\nclip(box((63,75), (84,100)));\n\n\\end{asy}\n\\\\ \n\\begin{asy}\nusepackage("amsmath");\nsize(2.45cm, 2.9167cm);\n\n// InsetBox\nlabel("$a^b$", (73.5,87.5), (0,0), rgb(0, 0, 0)+fontsize(3));\n\nclip(box((63,75), (84,100)));\n\n\\end{asy}\n\\end{array}',
613+
"System`TraditionalForm": '\\begin{array}{c} \n\\begin{asy}\nusepackage("amsmath");\nsize(2.45cm, 2.9167cm);\n\n// InsetBox\nlabel("$a^b$", (73.5,87.5), (0,0), rgb(0, 0, 0)+fontsize(3));\n\nclip(box((63,75), (84,100)));\n\n\\end{asy}\n\\\\ \n\\begin{asy}\nusepackage("amsmath");\nsize(2.45cm, 2.9167cm);\n\n// InsetBox\nlabel("$a^b$", (73.5,87.5), (0,0), rgb(0, 0, 0)+fontsize(3));\n\nclip(box((63,75), (84,100)));\n\n\\end{asy}\n\\end{array}',
614614
"System`InputForm": "\\text{TableForm}\\left[\\left\\{\\text{Graphics}\\left[\\left\\{\\text{Text}\\left[\\text{Power}\\left[a, b\\right], \\left\\{0, 0\\right\\}\\right]\\right\\}\\right], \\text{Graphics}\\left[\\left\\{\\text{Text}\\left[\\text{Power}\\left[a, b\\right], \\left\\{0, 0\\right\\}\\right]\\right\\}\\right]\\right\\}\\right]",
615-
"System`OutputForm": '\\begin{array}{c} \n\\begin{asy}\nusepackage("amsmath");\nsize(2.45cm, 2.9167cm);\n\n// InsetBox\nlabel("$a^b$", (73.5,87.5), (0,0), rgb(0, 0, 0));\n\nclip(box((63,75), (84,100)));\n\n\\end{asy}\n\\\\ \n\\begin{asy}\nusepackage("amsmath");\nsize(2.45cm, 2.9167cm);\n\n// InsetBox\nlabel("$a^b$", (73.5,87.5), (0,0), rgb(0, 0, 0));\n\nclip(box((63,75), (84,100)));\n\n\\end{asy}\n\\end{array}',
615+
"System`OutputForm": '\\begin{array}{c} \n\\begin{asy}\nusepackage("amsmath");\nsize(2.45cm, 2.9167cm);\n\n// InsetBox\nlabel("$a^b$", (73.5,87.5), (0,0), rgb(0, 0, 0)+fontsize(3));\n\nclip(box((63,75), (84,100)));\n\n\\end{asy}\n\\\\ \n\\begin{asy}\nusepackage("amsmath");\nsize(2.45cm, 2.9167cm);\n\n// InsetBox\nlabel("$a^b$", (73.5,87.5), (0,0), rgb(0, 0, 0)+fontsize(3));\n\nclip(box((63,75), (84,100)));\n\n\\end{asy}\n\\end{array}',
616616
},
617617
},
618618
}

0 commit comments

Comments
 (0)