Wrong class widths after LaTeX export

0 votes
asked Sep 28 in bug by Dresaan (760 points)
This bug might be a bit tricky. In some cases the classes are rendered to small with LaTeX. As far as I can see from the TikZ output, the boxes are simply to small. Beow is a sample to demonstrate it:

@startuml
class Utility
class Uuuuuuu
class iiiiii
class Licensing
@enduml

1 Answer

0 votes
answered Sep 29 by plantuml (134,510 points)
selected Oct 2 by Dresaan
 
Best answer
This one is very difficult to solve.
Because box width are computed in Java using standard Java font.
And drawing is done by LaTeX using LaTeX font, which means that size does not match between both.

However, we've made few tests. We've written some heuristic which tries to guess how the font will size in LaTeX.

You can use beta 18 to check it:

https://www.dropbox.com/s/koo42q3d9gxw288/plantuml.jar?dl=0

Unfortunately the result will never be perfect here.
Tell us if it's somehow better!
Thanks
commented Oct 2 by Dresaan (760 points)
It is a lot better now. Some objects become too big, but that is much better than too small.

You might hate me now, but what about the idea of letting TikZ sizing the Boxes?
I tried the following TikZ-Code:

\node at (112.5pt,160pt)[    below right,
                    color=red,   
                    draw=green,        
                    line width = 1.5pt,    
                    inner sep = 9pt,
                    shade,
                     shading=axis,
                     top color=yellow,
                     bottom color=blue,
                    shading angle=45]   
                    {Utilities};

It reproduces nearly the same output (except the test-colors).
A little problem is that I use classes without anything at the moment. They are just rectangles. For more complex objects, you would need to define shapes. This might probably be the most designated way to do diagrams like this in TikZ.
Another point is that I don’t know how much that will change your layout, but probably not that much.
I have never defined a shape in TikZ before. If you want my help for that, I need to find time.
commented Oct 2 by plantuml (134,510 points)
No, we don't hate you :-) We thank you for your suggestions and your tests, it's really useful!

However unfortunately, we cannot let Tikz do the size computation. Size computations have to be done in Java because there might have some edges between classes, so Java do have to know the exact box sizes to exactly compute edges position.

Anyway, we are now using two parameters to extrapolate between Java font size and Tikz font size:

The first one (1.2 beeing it's default value) is a simple zoom factor. A value of 1.2 means that we take the Java "Serif" font size and we multiply the result by 1.2 to have the expected Tikz font width.

The second one (4.0 beeing it's default value) is more tricky to explain. We try to detect large characters like "w" which are really different in Tikz and in Java. To do so, we compute Java "Serif" width of the text to be printed using both a slightly smaller font size and a slightly bigger font size. The difference between those two values gives an idea of the quantity of "large" characters in the string to be printed. This difference will be multiply by 4.0 and decrease from the total width. (Ok that's weird, but it seems to give good results).

With last beta (beta20), you can change the default values of those parameters.

https://www.dropbox.com/s/koo42q3d9gxw288/plantuml.jar?dl=0

Just use the skinparam tikzFont and give two values separated by ;

@startuml
skinparam tikzFont 1.2;4.0
class Wwwwwwwwwwwwwwww
class Uuuuuuuuuuuuuuuuuuuuuuuu
class iiiiii
class Licensing
@enduml

Tell us if you find better values than our defaults :-)

Thanks!
commented Oct 4 by Dresaan (760 points)
I already suspected the rendering.
I will try different values if I notice problems. But I think, the default font even differs whether you compile the LaTeX with windows or linux.

Then another idea. What about to set your used font as the TikZ font?
That might have some disadvantages like a second font in the document but that might fix the size difficulty.
Since not everybody will like that, there should be an option to disable the PlantUML font to use the used latex font.
So that the user can decide between two modes:
1. (default) The export sets the PlantUML font
2. The export ignores the font and uses the 2-paramter trick.
commented Oct 4 by plantuml (134,510 points)
> What about to set your used font as the TikZ font?
This would mean import theTikZ font so that they could be used in Java.
It sounds like a good idea, but I have no idea about how to do that.
commented Oct 5 by Dresaan (760 points)
no, I mean the other direction :-D

In TikZ, you can change the font. So you would need to parse the LaTeX-files/-project for the font.

PlantUML is propably using a font, that is known to the system. That would mean that you can use the same font in LaTeX/TikZ. Just export the font that is used by PlantUML to TikZ. The widths should be correct with the same font.
commented Oct 5 by Dresaan (760 points)
Or you add an option to change the font in TikZ manually. That might also be a nice feature.
Something like:
skinparam font Arial;
commented Oct 5 by plantuml (134,510 points)
Sure: if Java And LaTeX would use the same fonts, things will be easier.

Basically, Java uses TTF fonts:
https://stackoverflow.com/questions/5652344/how-can-i-use-a-custom-font-in-java

In LaTeX, it seems to be somehow more complex:
http://william.famille-blum.org/software/latexttf/index.html
http://www.radamir.com/tex/ttf-tex.htm
https://tex.stackexchange.com/questions/226/installing-ttf-fonts-in-latex

However, something like "skinparam font Arial" if a good idea, and is easy to do.
We just need help in the LaTeX/TikZ generated text...
When we print text, we generate something like:

\node at (12pt,44pt)[below right,color=black]{foo1};

Any idea how/where to add "Arial" in the LaTeX command ?
Thanks!
commented Oct 10 by Dresaan (760 points)
I thought it would be possible to just specify a font family and the LaTeX-Engine automatically tries to parse the font and search it in the system fonts. Unfortunately, it does not. I found some information about how to set a font in LaTeX but they use predefined a package to set the font or need the font files. Both ways are not really flexible.
The best of these ways would be to use the predefined packages. But you could also only use these packages and that are just a few. Also, you need a usepackage for every font, which might obfuscating if you export without preamble.
...