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

Bullet point circles don't indent with encompassing LeadingMarginSpan #10

Closed
GlennNZ opened this issue Nov 1, 2017 · 3 comments
Closed

Comments

@GlennNZ
Copy link

GlennNZ commented Nov 1, 2017

I've noticed that the bullet points from a Markdown render don't indent when they're encompassed by a LeadingMarginSpan. The text of the bullet points will indent correctly though. The circles appear to be absolutely positioned to the left margin.

The following code should should the issue.

SpannableConfiguration spannableConfiguration = SpannableConfiguration.create(this);
SpannableStringBuilder ssb = new SpannableStringBuilder();

ssb.append("First line\n");
int start = ssb.length();
ssb.append(Markwon.markdown(spannableConfiguration, "* first item\n * second item\n" ));
ssb.setSpan(new LeadingMarginSpan() {
      @Override
      public int getLeadingMargin(boolean first) {
          return 200;
      }
      @Override
      public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout layout) {
      }
}, start, ssb.length(), 0);

ssb.append("last line");

textView.setText(ssb);

Should BulletListItemSpan::drawLeadingMargin be receiving an indented x value? It will always have zero with this example.

@noties
Copy link
Owner

noties commented Nov 1, 2017

Hey @GlennNZ !

Yeah, it's a nasty thing. I remember why I had to pass current block indent to spans directly - exactly because x would be 0. Now I have investigated a bit and found out that spans that are set to a SpannableStringBuilder are stored in an array. So, a span that is added first, will be drawn first, thus bullets will always have x == 0 (in your sample snippet). To fix that a reverse operation will be required. For example, like this answer on StackOverflow suggests: https://stackoverflow.com/a/14358324 . I'm thinking of adding the reverse operation to the library and removing manual block indents (that Markwon internally keeps track of).

@GlennNZ
Copy link
Author

GlennNZ commented Nov 2, 2017

Oh, interesting. I didn't know that the draw order could be set.
I see I can partially solve the above example by making the LeadingMarginSpan highest priority i.e.

int priority = 255; // Should be in range [0..255].
int spanFlags = 0;
spanFlags &= ~Spannable.SPAN_PRIORITY;
spanFlags |= (priority << Spannable.SPAN_PRIORITY_SHIFT) & Spannable.SPAN_PRIORITY;

and settings it's flag param to spanFlags. https://stackoverflow.com/questions/46002947/how-to-define-order-setpriority-of-spannable-in-android

That will at least allow the BulletListItemSpan::drawLeadingMargin to get an x value but the circle drawing would have to be modified so that it has
final int marginLeft = x + (width - side) / 2; (with the x param)

@noties
Copy link
Owner

noties commented Nov 5, 2017

Hey! I've looked into this thing and come up with a simple solution - just to revert supplied spans. Right now it's in pull request #11 . In short: Markwon applies all the spans in reverted order automatically (this resolves the wrong x value passed to drawLeadingMargin). If there is a need to concat parsed markdown with other spans, one could use your solution (to shift the priority), or the class that is bundled (and used internally): SpannableBuilder. It will manage correct order automatically.

@noties noties closed this as completed Nov 11, 2017
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

2 participants