dbuenzli / vg

Declarative 2D vector graphics for OCaml
http://erratique.ch/software/vg
ISC License
86 stars 12 forks source link

Cairo renderer #7

Closed art-w closed 8 years ago

art-w commented 9 years ago

The main motivation for this backend is the ability to rasterize a Vg image to a custom surface in memory, but it can also be used to produce PNG and PS files (and more redundantly, PDF and SVG.) It adds an optional dependency to the cairo2 bindings for OCaml.

The implementation is based on the existing Vgr_htmlc renderer, as the semantics of the javascript canvas are very close to the cairo surface model, with the following known limitations:

The rcairo demo has an additional -format argument to specify the file backend.

Let me know if I overlooked anything!

dbuenzli commented 9 years ago

Thanks ! Will have a look at all this when I get some time.

Bold (arbitrarily chosen at W600.)

Could you rather use W700 for this ? This would make it consistent with CSS, see http://www.w3.org/TR/CSS2/fonts.html#font-boldness

Best,

Daniel

The rcairo demo has an additional -format argument to specify the file backend. Let me know if I overlooked anything! You can merge this Pull Request by running git pull https://github.com/art-w/vg cairo2 Or view, comment on, or merge it at: https://github.com/dbuenzli/vg/pull/7 Commit Summary Add a cairo2 renderer to the build system Add a minimal cairo2 example Vgr_cairo2: cost semantics Vgr_cairo2: subset implementation based on Vgr_htmlc Vgr_cairo2: stroke and dash Vgr_cairo2: gradients Vgr_cairo2: glyphs Add Vgr_cairo2 to the doc build Vgr_cairo2: multiple images Vgr_cairo2: document the API Vgr_cairo2: coding convention Vgr_cairo2: quadratic curves Vgr_cairo2: correct sizing of minimal example Vgr_cairo2: add PNG and PSD targets Vgr_cairo2: bugfix, gradients and arcs Vgr_cairo2: bugfix, sRGB Vgr_cairo2: update minimal example to output PNG Vgr_cairo2: output db examples to PDF Rstored: allow renderers to have multiple file formats Vgr_cairo2: rcairo2 has multiple formats Add cairo2 to .merlin Vgr_cairo2: add PS and SVG formats Vgr_cairo2: update copyright Rename to Vgr_cairo Vgr_cairo: update dependencies in README Vgr_cairo: documentation Vgr_cairo: resolution Vgr_cairo: update min_cairo example Vgr_cairo: documentation, correct resolution Vgr_cairo: small adjustments

File Changes M .merlin (https://github.com/dbuenzli/vg/pull/7/files#diff-0) (2)
M README.md (https://github.com/dbuenzli/vg/pull/7/files#diff-1) (14)
M _tags (https://github.com/dbuenzli/vg/pull/7/files#diff-2) (9)
M build (https://github.com/dbuenzli/vg/pull/7/files#diff-3) (2)
M doc/api.odocl (https://github.com/dbuenzli/vg/pull/7/files#diff-4) (1)
M doc/dev-api.odocl (https://github.com/dbuenzli/vg/pull/7/files#diff-5) (3)
M opam (https://github.com/dbuenzli/vg/pull/7/files#diff-6) (3)
M pkg/META (https://github.com/dbuenzli/vg/pull/7/files#diff-7) (11)
M pkg/build.ml (https://github.com/dbuenzli/vg/pull/7/files#diff-8) (3)
A src/vgr_cairo.ml (https://github.com/dbuenzli/vg/pull/7/files#diff-9) (439)
A src/vgr_cairo.mli (https://github.com/dbuenzli/vg/pull/7/files#diff-10) (117)
A src/vgr_cairo.mllib (https://github.com/dbuenzli/vg/pull/7/files#diff-11) (1)
A test/min_cairo.ml (https://github.com/dbuenzli/vg/pull/7/files#diff-12) (36)
A test/rcairo.ml (https://github.com/dbuenzli/vg/pull/7/files#diff-13) (58)
M test/rstored.ml (https://github.com/dbuenzli/vg/pull/7/files#diff-14) (17)
M test/tests.itarget (https://github.com/dbuenzli/vg/pull/7/files#diff-15) (4)

Patch Links: https://github.com/dbuenzli/vg/pull/7.patch https://github.com/dbuenzli/vg/pull/7.diff

— Reply to this email directly or view it on GitHub (https://github.com/dbuenzli/vg/pull/7).

dbuenzli commented 8 years ago

Are the glyph examples rendering correctly on your side ? Here it seems that none do. E.g. rendering pie-ambiguity to PNG gives:

pie-ambiguity

art-w commented 8 years ago

Uh, yes, they do. I'm not sure what's going on. Is it the correct font, or does cairo defaults to something else ?

pie-ambiguity

dbuenzli commented 8 years ago

Uh, yes, they do.

Ok cool. I was suspecting that.

I'm not sure what's going on. Is it the correct font, or does cairo defaults to something else ?

I'm using cairo on on osx. That may explain some things. I'll try to have a closer look at it.

art-w commented 8 years ago

I don't have a clue, but I only tested in on linux since I don't have access to anything else. I'll let you know if I think of some other explanation. You can also send me the rendering by mail if the bug isn't always "the texts are 50x larger than they should", it could help. Otherwise, the outputs I have are identical to the pdf backend (except for the gradients which are subtly not computed in the correct colorspace, but that's all.)

dbuenzli commented 8 years ago

"the texts are 50x larger than they should",

Yes that's the symptom. I don't think the problem is with your code. It seems rather to be a bug in cairo. See https://github.com/graphite-project/graphite-web/issues/1191#issuecomment-112208463.

dbuenzli commented 8 years ago

Am I correct in thinking that these two Cairo.save are useless since they are immediatly poped by this Cairo.restore ?

Also it seems to me that this Cairo.save is never Cairo.restored.

Chris00 commented 8 years ago

It is indeed a bit strange the save the pristine state. After all save and restore are more intended to be used to allow some construction not to affect the current state of the image (e.g. you want to draw a path but do not want the current point to be modified).

dbuenzli commented 8 years ago

Btw. @Chris00 I was trying to support multiple page output and it seems I get the following assertion failure:

 Assertion failed: (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&surface->ref_count)), function cairo_surface_destroy, file cairo-surface.c, line 930.
/bin/bash: line 1: 86018 Abort trap: 6           ./rcairo.native -pack bla -format ps

Any idea ?

dbuenzli commented 8 years ago

Any idea ?

Never mind can no longer reproduce...

dbuenzli commented 8 years ago

Ok so your PR has been squashed and merged as bbcabd4480a0b411 I changed a bit the API and made stored renderer for pdf and ps support multiple page output. Thanks for bootstrapping this and sorry again for the long delay. Release coming soon.

art-w commented 8 years ago

Great! Thanks for taking the time to make so many fixes, I'm very happy with all the improvements you made. Have a nice day!