jheinen / GR.jl

Plotting for Julia based on GR, a framework for visualisation applications
Other
356 stars 76 forks source link

Side effects of `GR.mathtex` and `GR.inqmathtex` #333

Open heliosdrm opened 4 years ago

heliosdrm commented 4 years ago

1. Side effect of GR.mathtex on subsequent primitives

There are various things that I find difficult to explain here:

# new session
using GR
GR.clearws()
GR.settextfontprec(232, 3)
tbx, tby = GR.inqmathtex(0.5, 0.5, "\$\\phi=\\pi\$")
# these rectangles should enclose the texts printed after
GR.drawrect(tbx[1], tbx[3], tby[1], tby[3])
GR.drawrect(tbx[1], tbx[3], tby[1], tby[3])
GR.mathtex(0.5, 0.5, "\$\\phi=\\pi\$")
GR.mathtex(0.5, 0.4, "\$\\phi=\\pi\$")
GR.updatews()

imagen

The rectangles do not match the texts, although I don't know if the problem is that GR.inqmathtex gives a wrong answer, or GR.mathtex does not print the text as it should do...

... or maybe the problem is the workflow? Actually, another funny thing happens after GR.mathtex is called for the first time. If the previous chunk is called again in the same session - or in a new session, if the lines with GR.mathtex are written before GR.drawrect, the rectangles are drawn in a different place, such that now they do fit the texts:

imagen

However, this is not as good as it might seem, because all other functions that draw primitives will draw things in a different place and a different size after GR.mathtex is called. Fortunately, this side effect can be cancelled if GR.savestate() and GR.restorestate() are called before and after GR.mathtex. Therefore, I can work around these unexpected behaviors taking into account these rules:

Anyway, it seems to me that this is a buggy behavior, in spite of having found a way to avoid it.

2. Interference of GR.inqmathtex on subsequent modifications of character height

The second problem I have found is even more complicated. See this code and its output (I have drawn the rectangles after calling GR.mathtex, to ensure they are aligned with the texts, as explained before):

GR.clearws()
GR.settextfontprec(232, 3)
GR.setcharheight(0.05)
tbx, tby = GR.inqmathtex(0.5, 0.5, "\$\\phi=\\pi\$")
GR.setcharheight(0.03)
GR.mathtex(0.5, 0.5, "\$\\phi=\\pi\$")
GR.mathtex(0.5, 0.4, "\$\\phi=\\pi\$")
GR.drawrect(tbx[1], tbx[3], tby[1], tby[3])
GR.drawrect(tbx[1], tbx[3], tby[1]-0.1, tby[3]-0.1)
GR.updatews()

imagen

The coordinates of the rectangles have been calculated with GR.inqmathtex after setting the character height as 0.05. But just before calling GR.mathtex, I reset the character height as 0.03, so I would expect that the texts would be smaller than the rectangles.

And that is what happens with the second text (the lower one), but the first one is printed as if the character height was still 0.05. Thus, the action of GR.setcharacterheight seems to be ignored by the first subsequent call to GR.mathtex. But, trying different variations of the previous code, I have observed that this only happens if GR.inqmathtex is called before.

In other words, the apparent behavior is that, whenever GR.inqmathtex is called, the character height that is set at that moment remains transiently fixed, such that the next call to GR.mathtex will use the same character height, even if there is another value set in the meantime. After GR.mathtex is called, that "memory" of the character height used by GR.inqmathtex seems to be forgotten. GR.savestate etc. are of no use here.

jheinen commented 4 years ago

You should use GR.fillarea(tbx, tby) to show the text bbox (it might be rotated) or use the same coordinate system (GR.selntran(0)) for text and rectangles.

jheinen commented 4 years ago

I will check whether GR.mathtex overwrites the GKS state - anyway.

heliosdrm commented 4 years ago

You should use GR.fillarea(tbx, tby) to show the text bbox (it might be rotated) or use the same coordinate system (GR.selntran(0)) for text and rectangles.

Ok. It seems that GR.mathtex modifies the state as if GR.selntran(0) had been called. Using GR.selntran(0) as suggested makes the primitives fit the text; and GR.selntran(1) undoes the cited side effect of GR.mathtex.

This solves the first question, but the second one remains open: why does GR.inqmathtex sort of "block" subsequent calls to GR.setcharheight, until GR.mathtex is called?

jheinen commented 4 years ago

The problem was fixed (today) in the development branch on our GitLab CI and will soon be merged into the GH master branch.