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

library forces coordinates to be millimeters #36

Open
nmarkham opened this issue Jan 25, 2016 · 3 comments
Open

library forces coordinates to be millimeters #36

nmarkham opened this issue Jan 25, 2016 · 3 comments
Assignees

Comments

@nmarkham
Copy link

(By way of background, I recently found this library when I was looking for alternatives to FreeHEP. I'm impressed so far!)

I've noticed some odd (to me) behavior regarding sizes and scaling, particularly when creating EPS graphics. Both the size of the page I created and the size of my graphics weren't what I thought they should be. After browsing the code, I discovered that this is because VectorGraphics2D treats all dimensions as millimeters, while I expected it to be pixels or maybe points — basically, the native unit of the format in question.

For SVG this is only a minor annoyance that I can easily correct if I care enough. It's more of a concern for EPS and PDF though — even if I convert the size canvas I want from points to millimeters (just to have VectorGraphics2D convert it back) I'm still forced to do all my drawing in millimeters. This leads to output with unintuitive arguments to the scale command and all of the drawing commands. What could be a simple "newpath 0 0 612 792 rect Z fill" becomes "2.834645669291339 -2.834645669291339 scale" followed by "newpath 0 0 215.9 279.4 rect Z fill".

Bottom line, could there be a way to create a Graphics object with a different base unit? Or at least to skip the built-in scaling and work in the format's native units? Thanks!

@seifertm
Copy link
Collaborator

It's always nice to hear feedback from the users, thanks!
That's correct, VG2D enforces millimeters as units and there is clearly a need to specify measurements in other units.

I think this is closely related to the missing support for setting the DPI (or PPI) of a document (see issue #16). Currently, the pixel density is a constant value, which makes it difficult if you want to specify the size in pixels for example. If DPI can be adjusted, the VectorGraphics2D API could offer different means to set the size. From my current point of view, this approach would be preferable over multiple VG2D implementations. I cannot make any promises on when we will get around to implementing it, though. Sorry :/

@nairnj
Copy link

nairnj commented Mar 24, 2016

I fixed it by scaling the graphics as shown below. The output PDF then had size in inches assuming pixels in java were 72 DPI (a hard coded number). On MacOS 10.8 with Java 6, this caused some setClip errors failing to validate rectangle, but I could not detect any consequences of those errors. On MacOS 10.11 (El Capitan) and Java 8, those errors went away (although now every time I open a PDF file in Adobe, it complains of problem with bounding box for 'Lucinda Grande" font even though I never use that font? Opening in Apple's Preview does not complain?)

Here is code snippet to create scaled PDF:

Dimension psize = plot2DView.getSize();
double scale = 25.4/72.;
double pwidth = scale*psize.width;
double pheight = scale*psize.height;
PDFGraphics2D g = new PDFGraphics2D(0.0, 0.0, pwidth, pheight);
AffineTransform savedState=g.getTransform();
g.scale(scale, scale);
plot2DView.paintComponent(g);
g.setTransform(savedState);

@eseifert
Copy link
Owner

Thanks a lot for sharing your code @nairnj. That's definitely the workaround until a solution for issue #16 can be implemented.

Please, open new issues for the errors you mentioned, so we can solve them quickly.

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

4 participants