Repository navigation
Fix: Labels & co. - #1579
Fix: Labels & co.#1579
Conversation
* Fix 🐞: Nondeterministic labels, i.e. certain labels sometimes appear, sometimes not, everything else the same. (This bug has been bothering us for the last cca 10 years!) * Fix 🐞: Better label overlap avoidance algorithm * Fix 🐞: Label overlap check not refreshed on map rotation * Android: Label space now includes a small padding when checking for overlap (avoids placing labels too close together, which can sometimes appear as one word) * Android: Better handling of multiline labels * Android: Test for label overlap algorithm
| @@ -1,16 +1,24 @@ | |||
| apply plugin: 'java-library' | |||
| apply plugin: 'com.android.library' | |||
There was a problem hiding this comment.
@Sublimis We do not want such a conversion.
Android module contains only Java code, so it should remain a simple Java module.
Only the tests do not justify such a conversion.
If they are useful, they can exist in their own separate module.
There was a problem hiding this comment.
No problem to revert. Is there any downside to using com.android.library for an Android library?
Testing for label overlap is very important because it's easy to mess things up (as evidenced by the longevity of the bug).
There was a problem hiding this comment.
@Sublimis If a library contains only Java code and not Android resources,
there is no real reason to have a more complicated Android aar library.
|
@Sublimis Please check the code, it does not compile. |
|
May I ask what happened in the other work with the hill shading improvements? If you're working on other areas of the library, that's perfectly fine. 👍 |
Haven't reverted build.gradle yet, please advise: should we revert or fix this error? It says the following, so the fix seems simple:
|
This requires a bit more work to disentangle our playfulness, we'll definitely do a PR or two for this as soon as it's ready! 🙂 |
* Fix 🐞: Nondeterministic labels, i.e. certain labels sometimes appear, sometimes not, everything else the same. (This bug has been bothering us for the last cca 10 years!) * Fix 🐞: Better label overlap avoidance algorithm * Fix 🐞: Label overlap check not refreshed on map rotation * Android: Label space now includes a small padding when checking for overlap (avoids placing labels too close together, which can sometimes appear as one word) * Android: Better handling of multiline labels * PATCH: Test for label overlap algorithm (AWT)
* Fix 🐞: Nondeterministic labels, i.e. certain labels sometimes appear, sometimes not, everything else the same. (This bug has been bothering us for the last cca 10 years!) * Fix 🐞: Better label overlap avoidance algorithm * Fix 🐞: Label overlap check not refreshed on map rotation * Android: Label space now includes a small padding when checking for overlap (avoids placing labels too close together, which can sometimes appear as one word) * Android: Better handling of multiline labels * Test for label overlap algorithm (AWT)
|
Sorry for the third commit, for some reason build.gradle wasn't reverted even though we pushed the original version along with the other files. |
|
@Sublimis Aren't Android improvements possible on the desktop as well (e.g. padding, ...) ? |
|
|
||
| int getTextHeight(String text); | ||
|
|
||
| default int getTextHeight(final String text, boolean includePadding) { |
There was a problem hiding this comment.
The default methods in the interface are only used on Android.
So they can be hosted in AndroidPaint and avoid loading the interface.
Also, this is a feature of Java 8 and we're trying to be as backwards compatible as possible.
There was a problem hiding this comment.
We can transfer those to Android classes, but then AWT will be left without label font padding concept. Maybe better to leave signatures in the interface and implement in both classes.
There was a problem hiding this comment.
We will have to choose one of 2 alternatives,
either implement and use the code on both platforms
or put the code in the platform class that is only used.
| int getTextHeight(String text); | ||
|
|
||
| default int getTextHeight(final String text, boolean includePadding) { | ||
| int retVal = 0; |
| int getTextWidth(String text); | ||
|
|
||
| default int getTextWidth(String text, boolean includePadding) { | ||
| int retVal = 0; |
| myPaint = new TextPaint(this.paint); | ||
| } | ||
|
|
||
| final StaticLayout layout = new StaticLayout(text, myPaint, widthMax, Layout.Alignment.ALIGN_NORMAL, 1, 0, includePadding); |
There was a problem hiding this comment.
Please add in the method:
@SuppressWarnings("deprecation")There was a problem hiding this comment.
@Sublimis
Also it is better to use the StaticLayout.Builder for newer Androids.
See the implementation in AndroidPointTextContainer.
| /** | ||
| * @return New rotated x,y position of the container relative to the origin point | ||
| */ | ||
| protected Point getRotRelPosition(double originX, double originY, Rotation rotation) { |
There was a problem hiding this comment.
Better use full name getRotatedRelativePosition for clear understanding.
| /** | ||
| * @return New rotated x,y position of the container relative to the origin point | ||
| */ | ||
| public static Point getRotRelPosition(PointTextContainer pointTextContainer, double originX, double originY, Rotation rotation) { |
There was a problem hiding this comment.
Better use full name getRotatedRelativePosition for clear understanding.
Sure, but we're talking only about label padding and drawing clash rectangles outline for debugging, that could probably be done easily, but it's not our primary platform so we left that for somebody else to do if possible. |
| * | ||
| * @return Clash rectangle in absolute coordinate space | ||
| */ | ||
| public static Rectangle getClashRect(PointTextContainer pointTextContainer, Rotation rotation) { |
There was a problem hiding this comment.
Better use full name getClashRectangle.
| * | ||
| * @return Clash rectangle transformed back to label space | ||
| */ | ||
| public static Rectangle getClashRectTransformed(PointTextContainer pointTextContainer, double originX, double originY, Rotation rotation) { |
There was a problem hiding this comment.
Better use full name getClashRectangleTransformed.
@Sublimis Is it just for debugging? The screenshots show otherwise. |
Yes, |
|
@Sublimis The description mentions:
Is this only for debugging? |
No, this is not only for debugging. This is (tiny) label padding we're talking about, it has nothing to do with the |
* Fix 🐞: Nondeterministic labels, i.e. certain labels sometimes appear, sometimes not, everything else the same. (This bug has been bothering us for the last cca 10 years!) * Fix 🐞: Better label overlap avoidance algorithm * Fix 🐞: Label overlap check not refreshed on map rotation * Label space now includes a small padding when checking for overlap (avoids placing labels too close together, which can sometimes appear as one word) * Android: Better handling of multiline labels * Test for label overlap algorithm (AWT)
|
|
||
| @Override | ||
| public int getTextWidth(final String text, final int widthMax, boolean includePadding) { | ||
| return getTextWidth(text, includePadding); |
There was a problem hiding this comment.
Here we can use code from AwtPointTextContainer to measure the width instead of default.
Like we use in AndroidPaint code from AndroidPointTextContainer
There was a problem hiding this comment.
It's probably easier to just remove these three-argument getTextWidth methods, not used anyway.
There was a problem hiding this comment.
It's probably easier to just remove these three-argument getTextWidth methods, not used anyway.
@Sublimis Better do this. 👍
| public int getTextWidth(final String text, final int widthMax, boolean includePadding) { | ||
| int retVal = 0; | ||
|
|
||
| if (text != null && widthMax >= 0) { |
There was a problem hiding this comment.
@Sublimis Why widthMax >= 0 and not widthMax > 0?
There was a problem hiding this comment.
This snippet is taken from our app so not sure any more, however by looking at the StaticLayout, width parameter value of zero is valid, so that is probably the reason. Although there's no explanation anywhere in the StaticLayout documentation why width=0 is accepted. But it is.
Note that these three-argument getTextWidth(, ,) methods are not currently used anywhere, but thought they might be useful in the future (the AndroidPaint one, that is). We used it a little while developing this PR, until decided on a different approach. They can be safely deleted for now (from the Paint interface too), you decide.
There was a problem hiding this comment.
Note that these three-argument getTextWidth(, ,) methods are not currently used anywhere
They can be safely deleted for now (from the Paint interface too), you decide.
If they are not used, it is better to remove them, as the public API is difficult to remove in the future.
And we avoid for now the desktop implementation. 🙂
|
@Sublimis Thanks, it looks very nice work! 👍 Please just check my comments on the code and if / how we can improve some things. |
* Fix 🐞: Nondeterministic labels, i.e. certain labels sometimes appear, sometimes not, everything else the same. (This bug has been bothering us for the last cca 10 years!) * Fix 🐞: Better label overlap avoidance algorithm * Fix 🐞: Label overlap check not refreshed on map rotation * Label space now includes a small padding when checking for overlap (avoids placing labels too close together, which can sometimes appear as one word) * Android: Better handling of multiline labels * Test for label overlap algorithm (AWT)
Yes, exactly that. Clash rectangle height is basically a text container boundary height, and container boundary is computed using that code. |
We took care of that now, as said earlier 🙂 |
Here you can see an example with exaggerated padding: |
|
@Sublimis Even on Android the rectangles seem attached at descent with much more free space at the top. |
Ok, see it now. Please wait until we look into it. |
|
@Sublimis More important is if the padding is used correctly in the label placement calculations? The debug rectangles are less important, we can improve them if the calculations work correctly. |
|
It seems like this cannot be fixed without knowing exactly where the baseline starts in the boundary/clash rectangle, which would require more coding |
@Sublimis For the debug rectangles? So it is just a painting issue? |
Please note these rectangles are used for labels clashing decisions. However yes, if some descent here or there touches another label is not so bad, probably. |
Painting of the text labels, yes, not the clash rectangles 🙂 |
👍 @Sublimis So let's not put more time on this. 🙂 When you are ready with this PR please let me know to merge it. |
…r, sometimes not, everything else the same. (This bug has been bothering us for the last cca 10 years!) * Fix 🐞: Better label overlap avoidance algorithm * Fix 🐞: Label overlap check not refreshed on map rotation * Label space now includes a small padding when checking for overlap (avoids placing labels too close together, which can sometimes appear as one word) * Android: Better handling of multiline labels * Test for label overlap algorithm (AWT) * PointTextContainer.DEBUG_CLASH_BOUNDS flag
|
@devemux86 |
|
@Sublimis So we are ready for merge now? |
|
Yes, unless you are willing to wait some more hours (or until tomorrow) so we maybe take descent value into consideration too 🙂 . The rough idea is (both Android and AWT): Add getDescent() to the Paint interface, implement, and use that to obtain descent value when drawing label. Move the y-coordinate up by this value before drawText call. |
|
@Sublimis Sure we can wait and try if there is a better implementation. 👍 |
…r, sometimes not, everything else the same. (This bug has been bothering us for the last cca 10 years!) * Fix 🐞: Better label overlap avoidance algorithm * Fix 🐞: Label overlap check not refreshed on map rotation * Label space now includes a small padding when checking for overlap (avoids placing labels too close together, which can sometimes appear as one word) * Android: Better handling of multiline labels * Test for label overlap algorithm (AWT) * PointTextContainer.DEBUG_CLASH_BOUNDS flag
|
This should be it. |
| break; | ||
| default: | ||
| throw new IllegalArgumentException("Position invalid"); | ||
| } |
There was a problem hiding this comment.
@Sublimis Why was this section removed? Was it replaced somewhere else?
There was a problem hiding this comment.
Those lines interfere with correct drawing of labels, but only on Android, AWT lives just fine without it:
So removing label Paint-level align on Android basically makes the code that draws the labels more symmetrical (between platforms) and simpler. Do you think there are any drawbacks to this? Haven't found any.
There was a problem hiding this comment.
Label text align is handled elsewhere, of course:
There was a problem hiding this comment.
@Sublimis Tests must be made with the position parameter of the caption rule.
With master branch this section is needed, otherwise it breaks the Android rendering.
With the PR code it seems to not be needed. That's why I ask where it is handled now?
There was a problem hiding this comment.
@Sublimis Tests must be made with the
positionparameter of thecaptionrule. Withmasterbranch this section is needed, otherwise it breaks the Android rendering.With the PR code it seems to not be needed. That's why I ask where it is handled now?
Here: #1579 (comment)
|
@Sublimis It seems working fine now. Let me know if there is more work here or more testing is needed. |
|
That should be all regarding caption/point text labels for this PR. What did you mean by saying "tests must be made..."? |
@Sublimis That we should test all e.g. locate a restaurant on the map and change the |
We've done such tests, of course. Let us know if you find any problems during testing 👍 |
|
@Sublimis Thanks! |

After:
PointTextContainer.DEBUG_CLASH_BOUNDSflag example:Before: