AsciiMath support #67
Replies: 2 comments 5 replies
-
Hello, Mr. GrimPixel, Thankfully, that is already feasible with the new features to be released in the new version (edit: this version is available already, it was released 02 weeks after this reply was posted). You can render the formula using your own custom nodes (with the help of third-party libraries, for instance) and use default nodes provided by Nodezator (in the version to be released) to display the data in the graph. By the way, I'm not delaying the release, it is just that I cannot predict the number of tasks because, as I finish the changes, I often identify improvement opportunities, sometimes find bugs and have to test everything that is added/fixed. It is all for the sake of delivering a more complete end product for the users, so it is worth the wait. Getting back to the topic, in summary, since the next version will allow users to display both text and pygame-ce surfaces in the graph beside the nodes, all users have to do to visualize a rendered AsciiMath formula, for instance, is to convert the rendered formula to a surface. Of course, parsing and rendering the formula is a separate problem and the users have to either write the code to do so or use third-party libraries to do the rendering. However, since Python is pretty popular, there is usually code already written for this kind of thing. Fortunately, since I've been working on converting data into pygame surfaces since a long time, and more recently have been working on implementing some nodes that help the user do that for this next Nodezator version, I made a quick research regarding this request of yours in specific and found out that there is a more or less straightforward way to do that. If you install the Python libraries py_asciimath and sympy (I just I even managed to put together a quick and dirty first working prototype yesterday. It has a lot of nodes, but the vast majority are very tiny atomic operations or quick hacks. The entire graph can be easily encapsulated into 02 to 03 custom nodes. You can see this first rough prototype in this video: ascii_experiment.mp4For the rendering performed by sympy to work though you need to install Latex in your system, since it uses the I'll refactor the code into a couple of custom nodes and give them to you, but for now I'd like to keep focusing on finishing my work on the next version. So, please, wait a bit more. In the meantime, let me know if you need anything else, and of course, feel free to keep the questions/feedback/requests coming. I'll answer each of them as soon as healthily possible. Peace. |
Beta Was this translation helpful? Give feedback.
-
Hello, Mr. GrimPixel, Here are the nodes I created: Don't forget that for them to work you must have latex installed in your system, as well as the py_asciimath and sympy libraries (just check my first reply in this discussion for extra instructions). As the names imply, you can just feed your ASCIIMath code into To visualize the surface, use the asciimath_nodes_demo_final.mp4(I'll publish this demo on social media and other channels of the Indie Python project as well) Whenever users want to save the surface as an image file, they just have to feed the surface into the Here are the node scripts (you or anyone can use this code as you see fit, I won't license it since it is just calls to external libraries and even if it had any code I created myself I would put it in the public domain just like Nodezator and any code from the Indie Python project, so really, just do what you want with it): For ### third-party import
from py_asciimath.translator.translator import ASCIIMath2Tex
## translator callable
latex_from_asciimath = ASCIIMath2Tex(log=False, inplace=True).translate
## main callable
def asciimath2latex(asciimath_code:str='') -> [
{'name': 'latex_code', 'type': str}
]:
return latex_from_asciimath(
asciimath_code,
displaystyle=True,
from_file=False,
pprint=False,
)
main_callable = asciimath2latex For ### standard library import
from io import BytesIO
### third-party imports
## pygame-ce
from pygame import Surface
from pygame.image import load as load_image
## sympy
from sympy import preview
def latex2surface(
latex_code: str = '',
font_size_prefix : {
'widget_name': 'option_menu',
'widget_kwargs': {
'options': [
'none',
r'\tiny',
r'\scriptsize',
r'\footnotesize',
r'\small',
r'\normalsize',
r'\large',
r'\Large',
r'\LARGE',
r'\huge',
r'\Huge',
],
},
'type': str,
} = r'\Large',
) -> [
{'name': 'surface', 'type': Surface}
]:
### add font size prefix if requested
if font_size_prefix != 'none':
latex_code = font_size_prefix + latex_code
### create bytes stream and populate it with bytes
### representing rendered PNG file
bytes_io = BytesIO()
preview(
latex_code,
output='png',
viewer='BytesIO',
outputbuffer=bytes_io,
)
### change the stream position to start of stream
### (this way the bytes can be read properly)
bytes_io.seek(0)
### convert bytes stream (a file-like object), into
### a surface
surface = load_image(bytes_io, 'file.png')
### close the bytes stream, since we won't need it anymore
bytes_io.close()
### finally return the surface
return surface
main_callable = latex2surface As always, let me know if you need anything else. Peace. |
Beta Was this translation helpful? Give feedback.
-
I would like to add the AsciiMath formula and display it on the node.
Beta Was this translation helpful? Give feedback.
All reactions