Skip to content

Fix: Labels & co. - #1579

Merged
devemux86 merged 10 commits into
mapsforge:masterfrom
Sublimis:labels01-1
Nov 26, 2024
Merged

devemux86 merged 10 commits into
mapsforge:masterfrom
Sublimis:labels01-1

Conversation

@Sublimis

Copy link
Copy Markdown
Contributor
  • 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

After:

20241122-after1

20241122-after2

PointTextContainer.DEBUG_CLASH_BOUNDS flag example:

20241122-after-debug

Before:

20241122-before1
20241122-before2

* 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
Comment thread mapsforge-map-android/build.gradle Outdated
@@ -1,16 +1,24 @@
apply plugin: 'java-library'
apply plugin: 'com.android.library'

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

@devemux86

devemux86 commented Nov 22, 2024 •

Copy link
Copy Markdown
Collaborator

@Sublimis Please check the code, it does not compile.

@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis

May I ask what happened in the other work with the hill shading improvements?
We discussed splitting it into multiple simpler pull requests.

If you're working on other areas of the library, that's perfectly fine. 👍
We definitely need improvements in all areas!

@Sublimis

Copy link
Copy Markdown
Contributor Author

@Sublimis Please check the code, it does not compile.

Haven't reverted build.gradle yet, please advise: should we revert or fix this error? It says the following, so the fix seems simple:

To create a baseline, run gradlew updateLintBaseline after adding the following to the module's build.gradle file:
/home/runner/work/mapsforge/mapsforge/mapsforge-map-android/src/main/java/org/mapsforge/map/android/graphics/AndroidCanvas.java:155: Error: Call requires API level 9 (current min is 1): java.lang.String#isEmpty [NewApi]

  android {
      lint {
          baseline = file("lint-baseline.xml")
      }
  }

For more details, see https://developer.android.com/studio/write/lint#snapshot

@Sublimis

Copy link
Copy Markdown
Contributor Author

May I ask what happened in the other work with the hill shading improvements? We discussed splitting it into multiple simpler pull requests.

If you're working on other areas of the library, that's perfectly fine. 👍 We definitely need improvements in all areas!

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)
@Sublimis

Copy link
Copy Markdown
Contributor Author

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.

@devemux86

Copy link
Copy Markdown
Collaborator

@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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Sublimis Sublimis Nov 23, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set variable 2 times?

int getTextWidth(String text);

default int getTextWidth(String text, boolean includePadding) {
int retVal = 0;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set variable 2 times?

myPaint = new TextPaint(this.paint);
}

final StaticLayout layout = new StaticLayout(text, myPaint, widthMax, Layout.Alignment.ALIGN_NORMAL, 1, 0, includePadding);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add in the method:

@SuppressWarnings("deprecation")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better use full name getRotatedRelativePosition for clear understanding.

@Sublimis

Sublimis commented Nov 23, 2024 •

Copy link
Copy Markdown
Contributor Author

@Sublimis Aren't Android improvements possible on the desktop as well (e.g. padding, ...) ?

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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better use full name getClashRectangleTransformed.

@devemux86

Copy link
Copy Markdown
Collaborator

Sure, but we're talking only about font 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.

@Sublimis Is it just for debugging? The screenshots show otherwise.
In any case both platforms are evolving simultaneously, we can't leave one behind.
In such a case the code should only exist in the platform-specific classes.

@Sublimis

Copy link
Copy Markdown
Contributor Author

@Sublimis Is it just for debugging? The screenshots show otherwise. In any case both platforms are evolving simultaneously, we can't leave one behind. In such a case the code should only exist in the platform-specific classes.

Yes, PointTextContainer.DEBUG_CLASH_BOUNDS flag is useful just for debugging labels overlap, why else would we draw rectangles around labels? 🙂

@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis The description mentions:

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)

Is this only for debugging?

@Sublimis

Copy link
Copy Markdown
Contributor Author

@Sublimis The description mentions:

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)

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 PointTextContainer.DEBUG_CLASH_BOUNDS mentioned above, sorry for the confusion.

* 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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we can use code from AwtPointTextContainer to measure the width instead of default.

Like we use in AndroidPaint code from AndroidPointTextContainer

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably easier to just remove these three-argument getTextWidth methods, not used anyway.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sublimis Why widthMax >= 0 and not widthMax > 0?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sublimis

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. 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very true!

@devemux86

Copy link
Copy Markdown
Collaborator

@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)
@Sublimis

Copy link
Copy Markdown
Contributor Author

@Sublimis What is needed? Change the line:

return (int) this.font.createGlyphVector(fontMetrics.getFontRenderContext(), text).getVisualBounds().getHeight();

Yes, exactly that. Clash rectangle height is basically a text container boundary height, and container boundary is computed using that code.

@Sublimis

Copy link
Copy Markdown
Contributor Author

@Sublimis As mentioned above, even if we change the getTextHeight, even we make it to return 2x value, the rectangle still starts at baseline.

We took care of that now, as said earlier 🙂

@Sublimis

Copy link
Copy Markdown
Contributor Author

@Sublimis As mentioned above, even if we change the getTextHeight, even we make it to return 2x value, the rectangle still starts at baseline.

We took care of that now, as said earlier 🙂

Here you can see an example with exaggerated padding:

image

@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis Even on Android the rectangles seem attached at descent with much more free space at the top.
It appears on the first screenshots for "Gostingen". The padding should have same free space on all 4 sides?

@Sublimis

Copy link
Copy Markdown
Contributor Author

@Sublimis Even on Android the rectangles seem attached at descent with much more free space at the top. It appears on the first screenshots for "Gostingen". The padding should have same free space on all 4 sides?

Ok, see it now. Please wait until we look into it.

@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis More important is if the padding is used correctly in the label placement calculations?
e.g. it is not shifted upwards like in the debug rectangles.

The debug rectangles are less important, we can improve them if the calculations work correctly.

@Sublimis

Copy link
Copy Markdown
Contributor Author

It seems like this cannot be fixed without knowing exactly where the baseline starts in the boundary/clash rectangle, which would require more coding

@devemux86

Copy link
Copy Markdown
Collaborator

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?

@Sublimis

Copy link
Copy Markdown
Contributor Author

@Sublimis More important is if the padding is used correctly in the label placement calculations? e.g. it is not shifted upwards like in the debug rectangles.

The debug rectangles are less important, we can improve them if the calculations work correctly.

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.

@Sublimis

Copy link
Copy Markdown
Contributor Author

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?

Painting of the text labels, yes, not the clash rectangles 🙂

@devemux86

Copy link
Copy Markdown
Collaborator

However yes, if some descent here or there touches another label is not so bad, probably.

👍

@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
@Sublimis

Copy link
Copy Markdown
Contributor Author

@devemux86
That should be it.

@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis So we are ready for merge now?

@Sublimis

Copy link
Copy Markdown
Contributor Author

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.

@devemux86

Copy link
Copy Markdown
Collaborator

@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
@Sublimis

Copy link
Copy Markdown
Contributor Author

This should be it.

break;
default:
throw new IllegalArgumentException("Position invalid");
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sublimis Why was this section removed? Was it replaced somewhere else?

@Sublimis Sublimis Nov 26, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those lines interfere with correct drawing of labels, but only on Android, AWT lives just fine without it:

@Override
public void setTextAlign(Align align) {
// this.align = align; //never read
}

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Label text align is handled elsewhere, of course:

switch (this.position) {
case CENTER:
default:
boundary = new Rectangle(-boxWidth / 2f, -boxHeight / 2f, boxWidth / 2f, boxHeight / 2f);
break;
case BELOW:
boundary = new Rectangle(-boxWidth / 2f, 0, boxWidth / 2f, boxHeight);
break;
case BELOW_LEFT:
boundary = new Rectangle(-boxWidth, 0, 0, boxHeight);
break;
case BELOW_RIGHT:
boundary = new Rectangle(0, 0, boxWidth, boxHeight);
break;
case ABOVE:
boundary = new Rectangle(-boxWidth / 2f, -boxHeight, boxWidth / 2f, 0);
break;
case ABOVE_LEFT:
boundary = new Rectangle(-boxWidth, -boxHeight, 0, 0);
break;
case ABOVE_RIGHT:
boundary = new Rectangle(0, -boxHeight, boxWidth, 0);
break;
case LEFT:
boundary = new Rectangle(-boxWidth, -boxHeight / 2f, 0, boxHeight / 2f);
break;
case RIGHT:
boundary = new Rectangle(0, -boxHeight / 2f, boxWidth, boxHeight / 2f);
break;
}

switch (this.position) {
case CENTER:
default:
return new Rectangle(-boxWidth / 2, -boxHeight / 2, boxWidth / 2, boxHeight / 2);
case BELOW:
return new Rectangle(-boxWidth / 2, 0, boxWidth / 2, boxHeight);
case BELOW_LEFT:
return new Rectangle(-boxWidth, 0, 0, boxHeight);
case BELOW_RIGHT:
return new Rectangle(0, 0, boxWidth, boxHeight);
case ABOVE:
return new Rectangle(-boxWidth / 2, -boxHeight, boxWidth / 2, 0);
case ABOVE_LEFT:
return new Rectangle(-boxWidth, -boxHeight, 0, 0);
case ABOVE_RIGHT:
return new Rectangle(0, -boxHeight, boxWidth, 0);
case LEFT:
return new Rectangle(-boxWidth, -boxHeight / 2, 0, boxHeight / 2);
case RIGHT:
return new Rectangle(0, -boxHeight / 2, boxWidth, boxHeight / 2);
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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?

Here: #1579 (comment)

@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis It seems working fine now. Let me know if there is more work here or more testing is needed.

@Sublimis

Copy link
Copy Markdown
Contributor Author

That should be all regarding caption/point text labels for this PR. What did you mean by saying "tests must be made..."?

@devemux86

Copy link
Copy Markdown
Collaborator

What did you mean by saying "tests must be made..."?

@Sublimis That we should test all position values in caption theme rule on Android + Desktop,
to make sure everything continues to work correctly like before.

e.g. locate a restaurant on the map and change the position on its caption theme rule to all available values.

@Sublimis

Sublimis commented Nov 26, 2024 •

Copy link
Copy Markdown
Contributor Author

What did you mean by saying "tests must be made..."?

@Sublimis That we should test all position values in caption theme rule on Android + Desktop, to make sure everything continues to work correctly like before.

e.g. locate a restaurant on the map and change the position on its caption theme rule to all available values.

We've done such tests, of course. Let us know if you find any problems during testing 👍

@devemux86 devemux86 added this to the 0.23.0 milestone Nov 26, 2024
@devemux86
devemux86 merged commit 9ebb290 into mapsforge:master Nov 26, 2024
@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants