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

properly handle SVGs generated by SVGMath #468

Closed
akavel opened this issue May 4, 2017 · 8 comments
Closed

properly handle SVGs generated by SVGMath #468

akavel opened this issue May 4, 2017 · 8 comments

Comments

@akavel
Copy link
Contributor

akavel commented May 4, 2017

For #220, it would be nice if SVGs generated by SVGMath rendered correctly in SILE.

Sample such SVG, which didn't work for me: https://github.com/akavel/svgmath/blob/master/testdata/test13.out.svg

I tried a doc like below, with current codebase (7f677a1):

\begin{document}
\script[src=packages/svg]

\begin{script}
  local svg = require 'svg'

  local inp = assert(io.open('test13.svg'))
  local data = inp:read '*all'
  inp:close()

  local f = assert(io.opend('foo.tmp', 'w'))
  local fig, w, h = svg.svg_to_ps(data, 72)
  f:write(w, ' ', h, ' ', fig, ' ', data)   -- why doesn't '\n' work here?
  f:close()
\end{script}

Hello SILE! \include-svg-file[src=test13.svg] Included.

\end{document}

Initially, foo.tmp contained 0 0 [...], but after looking into nanosvg.h I noticed it doesn't accept XML namespaces. After sed -i 's/svg://g' test13.svg there was some more interesting stuff in foo.tmp, but still no image in the generated PDF.

Is it because the SVG seems to use mostly <svg:text> elements? Maybe nanosvg ignores them?

@akavel
Copy link
Contributor Author

akavel commented May 5, 2017

(continuing discussion from #220)

Looking at a list of elements described in SVG 1.1 spec vs. what nanosvg supports, there seem indeed to be some seemingly non-stupid missing entries (apart from numerous text-related ones), e.g.: clipPath, marker, mask, symbol & use, ...

@alerque
Copy link
Member

alerque commented May 8, 2017

I don't think nanosvg is going to serve SILE very well. As mentioned in #446 the support is just too rudimentary for even fairly basic use cases importing a figure.

@akavel
Copy link
Contributor Author

akavel commented Dec 25, 2017

Ok, got some code which does the parsing of svgmath-generated SVGs in pure Lua, and managed to get prototype rendering done in ZeroBrane Studio with love2d:

(compare with third-party rendering of the SVG at: https://github.com/akavel/svgmath/blob/master/testdata/test13.out.svg)

Not all elements supported yet, but matrices, glyphs and simple lines seem to work ok now.

So, now mostly waiting till someone provides me with a Windows binary of SILE so that I can try:

  1. moving/implementing the svg rendering (just enough for svgmath) there,
  2. integrating the whole pipeline in SILE.

Though I may have somewhat less time for that over the next 2 weeks.

@simoncozens
Copy link
Member

Very interesting! I think long term we are going to want to render SVG to PDF drawing operators ourselves, or use a better library which does that, to keep things in vector format. Doing it ourselves means we can keep the text pipeline going through SILE and leverage the existing fonts/rendering support.

@akavel
Copy link
Contributor Author

akavel commented Jan 10, 2018

When rendering an equation, how can I output some unicode glyph g at some position dx, dy relative to "current cursor" in the PDF from Lua? I understand that

pdf.add_content('q')

etc. is expected to let me draw curves, but I also need to render some glyphs. I tried using pdf.setstring() but I'm confused as to what can I put in the font argument? I tried some random hacks hoping to make it work:

\begin{document}
Sample inital text

\begin{script}

local pdf = require 'justenoughlibtexpdf'

local buf = 'A'
local font = SILE.font.cache(SILE.font.loadDefaults{}, SILE.shaper.getFace)
local f = pdf.loadfont(font)
if f< 0 then SU.error("Font loading error for "..options) end
font = f
pdf.setstring(100, 100, buf, string.len(buf), font, 0)

\end{script}

Sample final text

\end{document}

but got some assertion fail in dpxutil.c:

This is SILE 0.9.5-unreleased
<../silemath/sample.sil>
[fonts] Looking for Gentium Plus;10;400;;normal;;LTR
[fonts] Resolved font family Gentium Plus -> C:/Windows/Fonts/GentiumPlus-R.ttf
Assertion failed!

Program: C:\dnload\lua-etc\ZeroBraneStudio\bin\lua.exe
File: dpxutil.c, Line 299

Expression: ht && key

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Not sure what else I can try...

@akavel
Copy link
Contributor Author

akavel commented Jan 12, 2018

Ok, got some kind of even more slightly misaligned result by virtue of some unholy abomination of code - but hey, it worked! :D 😄 😂 will need a hell of a cleanup :) and integration work, obviously, for now it's just a one-off prototype of the final element of the pipeline. I cannot upload the code yet as I'm in travel, but here's a teaser screenshot:

@akavel
Copy link
Contributor Author

akavel commented Jan 12, 2018

Ok, to reduce the bus factor a bit, the code of the prototype is now at: https://github.com/akavel/silemath/blob/master/sample.sil.

  • I'd be especially interested to learn if the renderGlyph function can be simplified in some way.
  • Also, the <text> nodes in SVGs generated by svgmath have text-anchor="middle", so their horizontal position should be tweaked, but I couldn't yet find out how to do that properly (moving them by -shape[1].width/2 didn't work for me for now, it broke things more than fixed).

@akavel
Copy link
Contributor Author

akavel commented Jan 16, 2018

Ok, given that I have this sub-problem resolved in principle now, I'm closing this sub-issue and will get back to reporting/discussing in the main #220 issue.

@akavel akavel closed this as completed Jan 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants