Comments (11)
Implementation items
TODOs
- Minimum working example with MathJax
- Refactor JS front-end code for better modularity and readability
- Parsing and rendering basic labels (e.g.:
f
,g
...) - Supporting more complex Latex statements
- Create labeling statements (
Label f $f$
) - Adding syntactic sugar for auto labels (
AutoLabel f, g, h
) - Gaining access to Bezier curves
- How to represent Bezier curves in our Runtime? Currently, we only represent them as a list of point, which could be insufficient for optimization purposes
- We still need to parse the SVG of TeX labels back into our Runtime representation in order to optimize them. Here's an SVG parser library I found: svg-parser.
Design decisions
- On the highest level, all labels are converted to SVG strings by MathJax the first time the scene is rendered and will then be kept inside of a global dictionary called
label_svgs
indexed byuniqueShapeName
- Everytime
Render.scene
is called,_renderLabel
will parse the strings into SVG fragments using Snap and place them onto canvas - MathJax has an asynchronous API and requires queuing its "Queue" with typesetting requests. Therefore, I used Promises,
await
andasync
for all related functions (seetex2svg
and_collectLabels
for details)
Notes
A package called mathjax-node does it with one function call.
- The bounding box of the result is inaccurate though, as stated in the source code
Alternatively, someone on SOF wrote a script that also works. Here's a modified version of it:
window.MathJax = {
jax: ["input/TeX", "output/SVG"],
extensions: ["tex2jax.js", "MathMenu.js", "MathZoom.js"],
showMathMenu: false,
showProcessingMessages: false,
messageStyle: "none",
SVG: { useGlobalCache: false },
TeX: { extensions: ["AMSmath.js", "AMSsymbols.js", "autoload-all.js"] },
AuthorInit: function() {
MathJax.Hub.Register.StartupHook("End", function() {
var mj2img = function(texstring, callback) {
var input = texstring;
var wrapper = document.createElement("div");
wrapper.innerHTML = input;
var svg = ""
MathJax.Hub.Queue(["Typeset", MathJax.Hub, wrapper]);
MathJax.Hub.Queue(function() {
var mjOut = wrapper.getElementsByTagName("svg")[0];
mjOut.setAttribute("xmlns", "http://www.w3.org/2000/svg");
svg = mjOut.outerHTML;
callback(svg);
});
}
mj2img("\\[f: X \\to Y\\]", function(svg){
var parser = new DOMParser();
var doc = parser.parseFromString(svg, "image/svg+xml");
document.getElementsByTagName('body')[0].appendChild(doc.documentElement);
console.log(svg)
});
});
}
};
The output looks like:
from penrose.
Great!
To get the bounding box of an SVG, can we use a different library's method? (Relevant SO)
Does using mathjax-node
require that our frontend have a proper js build system, like webpack
? I'm not super clear on how node works.
Also, does the library support using a more natural TeX syntax than "\\[f: X \\to Y\\]"
? Ideally we'd write $f : X \to Y$
.
from penrose.
Current problems:
mathjax-node
and many other npm packages do it in a function call, but our JS frontend is all clientside JS code. Should we browserify the packages?- If not, we will need to write something like the code snippet in the last comment. In that case, how does this code fit into our current module?
- More generally, the JS file,
client.js
is getting too big to maintain and needs to be modularized. Maybe this is a good place to start dong so.
from penrose.
I rewrote the JS frontend using the module pattern, which is more readable and maintainable.
During that process, I found the added code for new shapes to be non-readable. I have not tested the following shapes yet:
- Angle Marks (significant refactoring needed)
- Parallelogram (significant refactoring needed)
- Ellipses
One other thing is I noticed that we are dealing with SVG path string more. I don't think string concatenation is the best way to handle them. Therefore, we need to use a library like paths.js to compose these SVG strings.
from penrose.
Thanks for refactoring the front-end code! I agree that we should use a library like paths.js to compose SVG strings. d3 can do it too.
Can you coordinate with @dormaayan to refactor / test / document the new shapes?
from penrose.
A quick teaser of Penrose with \LaTeX, coming soon:
from penrose.
from penrose.
from penrose.
Yes! I used MathJax to process all labels on startup 😄
from penrose.
Suggestions from GC:
- set diagram font size as
global { global.fontSize = CANVAS * scaleFactor }
and let individual selectors use different font sizes locally e.g.X.shape.text.fontSize = global.fontSize + 10
- what unit are we using for font size? em, px?
- let people optimize labels, e.g. size and angle
- allow label rotation
- in future, leave labels unrendered on top so people can render with their fonts of choice, instead of MathJax's default
from penrose.
Closing the issue for now since PR has been merged. We'll come back to the abovementioned features in later stages of the implementation
from penrose.
Related Issues (20)
- Cannot share gists with empty programs in the IDE
- Example at https://penrose.cs.cmu.edu/docs/ref/api does not compile
- Error when parsing floating-point numbers in Substance
- Improper handling of `Prop`-typed expressions in Substance HOT 1
- Nested function calls in Substance
- Multiple bugs related to tracking state changes when saving drafts and workspaces in the editor HOT 1
- UX improvements for saving/editing in the editor
- Incorrect handling of indexed sets with flipped ranges HOT 2
- Style `toString` functionality HOT 2
- Run in non-browser environments HOT 4
- Document arrowhead types
- Impossible n-gon example broken
- `saveWorkspaceEffect` in editor functionality should be split based on whether state update should be immediate or debounced
- `AutoLabel` on Substance types
- Calling `random` with varying or computed values results in uncaught errors in the IDE
- Using Substance variables not declared in Style header
- Unresolved "compiling" toasts in `roger`-mode IDE
- Multiplicity in Domain type declarations HOT 6
- Unified language for notation and styling HOT 1
- feat: Warn user if they're about to close a browser tab with unsaved Penrose code HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from penrose.