Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graphics overhault rebase1 #1373

Draft
wants to merge 54 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
90aac8c
basic LayoutEngine working
poke1024 Aug 30, 2016
a253dd7
adds FontSize
poke1024 Aug 30, 2016
a156481
added --svg-math parameter to enable layout engine; not used by default
poke1024 Aug 31, 2016
50dbf64
LayoutEngine constructor now raises Exceptions
poke1024 Aug 31, 2016
bd08c19
fixes error reporting
poke1024 Aug 31, 2016
aaf113f
error handling cosmetics
poke1024 Aug 31, 2016
6a3ec6b
got rid of zerorpc since it is such a dependency hell
poke1024 Sep 1, 2016
f95027f
allow environment variables in node paths
poke1024 Sep 1, 2016
f0900ee
fixes NODE_MODULES
poke1024 Sep 1, 2016
24edc95
work in progress: mix mglyph and svg-only approach
poke1024 Sep 3, 2016
c30595d
several small improvement for more stability
poke1024 Sep 4, 2016
be7106e
major cleanup
poke1024 Sep 4, 2016
f5c5e6d
fixes node.js output; better warnings
poke1024 Sep 4, 2016
ef52066
basic version of Rasterize[], fixes for the classic Mathics frontend
poke1024 Sep 5, 2016
dcda65a
better error handling
poke1024 Sep 5, 2016
4d725a3
fixes Python 2 syntax error
poke1024 Sep 10, 2016
0116477
added nowebeng message
poke1024 Sep 14, 2016
c7c54c7
got rid of NODE and NODE_MODULES in settings.py; the cleanest way to …
poke1024 Sep 14, 2016
f9144b2
Rasterize[] changed to use PIL
poke1024 Sep 15, 2016
ea0b17a
fixes Rasterize[]: avoid embedding svgs in svgs
poke1024 Sep 15, 2016
6e694d8
fixes a bug in TerminalOutput
poke1024 Sep 15, 2016
e6bbb13
better node config/startup
poke1024 Oct 18, 2016
e51b5a6
merge with transforms
poke1024 Oct 21, 2016
44adcfd
merge transforms
poke1024 Oct 21, 2016
d9407ed
merge transforms
poke1024 Oct 21, 2016
72abc7a
merge transforms
poke1024 Oct 21, 2016
cb915dc
merge transforms
poke1024 Oct 21, 2016
6729d0c
merge transforms
poke1024 Oct 21, 2016
941a839
various fixes regarding text and points
poke1024 Oct 21, 2016
6c70521
fixes test case
poke1024 Oct 21, 2016
d7aeb05
preparations for Mathics computed transforms
poke1024 Nov 5, 2016
7bff167
fixes Plot[1+x*0.000001, {x, 0, 1}]
poke1024 Nov 5, 2016
86d4f1c
various cleanups and fixes
poke1024 Nov 5, 2016
fb2d1db
fixes test case
poke1024 Nov 5, 2016
e85bef5
fix node.js server startup
poke1024 Aug 30, 2020
0854801
merge 1.1
mmatera May 12, 2021
08c4f8f
merge 1.1.1
mmatera May 12, 2021
ace3235
merge 2.0
mmatera May 12, 2021
22471da
merge 2.1
mmatera May 12, 2021
7b077e3
merge master
mmatera May 13, 2021
386767f
fix
mmatera May 13, 2021
1d1979f
fix3
mmatera May 13, 2021
346dadf
fix6
mmatera May 14, 2021
b5ecad4
fix test
mmatera May 14, 2021
ee18ee0
fix 8
mmatera May 14, 2021
82e3ad8
fixaa
mmatera May 14, 2021
e26e1d2
fixxxxx
mmatera May 15, 2021
6448755
fixxx
mmatera May 15, 2021
abd24d3
merge
mmatera May 15, 2021
9bc1128
fix plot
mmatera May 15, 2021
4327ba8
tmp
mmatera May 15, 2021
23309b4
implementig boxerror
mmatera May 15, 2021
3ea6856
clean
mmatera May 15, 2021
0a283aa
Merge remote-tracking branch 'upstream/master' into graphics-overhaul…
mmatera May 15, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions mathics/builtin/drawing/graphics3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,8 +764,6 @@ def total_extent_3d(extents):


class Graphics3DElements(_GraphicsElements):
coords = Coords3D

def __init__(self, content, evaluation, neg_y=False):
super(Graphics3DElements, self).__init__(content, evaluation)
self.neg_y = neg_y
Expand All @@ -774,6 +772,10 @@ def __init__(self, content, evaluation, neg_y=False):
) = (
self.pixel_width
) = self.pixel_height = self.extent_width = self.extent_height = None
self.local_to_screen = None

def make_coords(self, points):
return [Coords3D(self, p) for p in points]

def extent(self, completely_visible_only=False):
return total_extent_3d([element.extent() for element in self.elements])
Expand Down Expand Up @@ -835,7 +837,11 @@ def to_asy(self):

return "".join(
"path3 g={0}--cycle;dot(g, {1});".format(
"--".join("(%.5g,%.5g,%.5g)" % coords.pos()[0] for coords in line), pen
"--".join(
"(%.5g,%.5g,%.5g)" % tuple(float(x) for x in coords.pos()[0])
for coords in line
),
pen,
)
for line in self.lines
)
Expand Down
46 changes: 46 additions & 0 deletions mathics/builtin/drawing/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
convert as convert_color,
colorspaces as known_colorspaces,
)
from mathics.layout.client import WebEngineError

import base64
import functools
Expand Down Expand Up @@ -2561,3 +2562,48 @@ def color_func(

image = wc.to_image()
return Image(numpy.array(image), "RGB")


class Rasterize(Builtin):
requires = _image_requires

options = {
"RasterSize": "300",
}

def apply(self, expr, evaluation, options):
"Rasterize[expr_, OptionsPattern[%(name)s]]"

raster_size = self.get_option(options, "RasterSize", evaluation)
if isinstance(raster_size, Integer):
s = raster_size.get_int_value()
py_raster_size = (s, s)
elif raster_size.has_form("List", 2) and all(
isinstance(s, Integer) for s in raster_size.leaves
):
py_raster_size = tuple(s.get_int_value for s in raster_size.leaves)
else:
return

mathml = evaluation.format_output(expr, "xml")
try:
svg = evaluation.output.mathml_to_svg(mathml)
png = evaluation.output.rasterize(svg, py_raster_size)

stream = BytesIO()
stream.write(png)
stream.seek(0)
im = PIL.Image.open(stream)
# note that we need to get these pixels as long as stream is still open,
# otherwise PIL will generate an IO error.
pixels = numpy.array(im)
stream.close()

return Image(pixels, "RGB")
except WebEngineError as e:
evaluation.message(
"General",
"nowebeng",
"Rasterize[] did not succeed: " + str(e),
once=True,
)
10 changes: 10 additions & 0 deletions mathics/builtin/drawing/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
except ImportError:
has_compile = False


def gradient_palette(color_function, n, evaluation): # always returns RGB values
if isinstance(color_function, String):
color_data = Expression("ColorData", color_function).evaluate(evaluation)
Expand Down Expand Up @@ -696,6 +697,15 @@ def find_excl(excl):
Expression("Point", Expression(SymbolList, *meshpoints))
)

# We need the PrecomputeTransformations option here. To understand why, try Plot[1+x*0.000001, {x, 0, 1}]
# without it. in Graphics[], we set up a transformation that scales a very tiny area to a very large area.
# unfortunately, most browsers seem to have problems with scaling stroke width properly. since we scale a
# very tiny area, we specify a very small stroke width (e.g. 1e-6) which is then scaled. but most browsers
# simply round this stroke width to 0 before scaling, so we end up with an empty plot. in order to fix this,
# Transformation -> Precomputed simply gets rid of the SVG transformations and passes the scaled coordinates
# into the SVG. this also has the advantage that we can precompute with arbitrary precision using mpmath.
options["System`Transformation"] = String("Precomputed")

return Expression(
"Graphics", Expression(SymbolList, *graphics), *options_to_rules(options)
)
Expand Down
Loading