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

Same margin before and after a list between paragraphs? #7

Closed
aried3r opened this issue Mar 3, 2020 · 13 comments
Closed

Same margin before and after a list between paragraphs? #7

aried3r opened this issue Mar 3, 2020 · 13 comments

Comments

@aried3r
Copy link
Contributor

aried3r commented Mar 3, 2020

Hey and thanks for this gem!

I'm currently playing around with this gem and was wondering if there is a way to have the same bottom and top margin/padding in a list surrounded by paragraphs?

<p>Top paragraph</p>
<!-- Add a margin here -->
<ul>
  <li><p>A</p></li>
  <li><p>list</p></li>
  <li><p>with</p></li>
  <li><p>elements</p></li>
</ul>
<!-- Add same margin here -->
<p>Bottom Paragraph</p>

Expected result (HTML rendered in a browser)

Screenshot 2020-03-03 at 16 24 25

Actual result

BASE_LINE_GRID = 13

markup_options = {
    text: {
        leading: BASE_LINE_GRID - font.height_at(8.5),
        # Default margin_bottom seems like a sensible default, let's keep
        # it for now.
        # margin_bottom: 0 # Margin after each <p>, <ol>, <ul> or <table>
    },
    list: {
        vertical_margin: 0, # margin at the top and the bottom of the list
        bullet: {
            margin: 0 # Margin before bullet
        },
        content: {
            leading: BASE_LINE_GRID - font.height_at(8.5), # Vertical padding between list items
            margin: 4 # Between bullet and content
        }
    }
}

Screenshot 2020-03-03 at 16 09 37

I assume what I'm seeing here is the margin_bottom of the "Top Paragraph" <p> + an additional padding/margin on top of the list (although vertical_margin is 0).

If I change it text: { margin_bottom: 0 } of course the margins at the bottom of the first <p> are gone, and so is the margin at the bottom of the <ul>. However there is still a margin before the list and I'm not sure where it's coming from.

Screenshot 2020-03-03 at 16 17 27

Even when I set everything (well, I guess not everything otherwise it'd be gone) to 0, there's still a distance between the top paragraph and the bottom one.

markup_options = {
    text: {
        leading: 0
        margin_bottom: 0 # Margin after each <p>, <ol>, <ul> or <table>
    },
    list: {
        vertical_margin: 0, # margin at the top and the bottom of the list
        bullet: {
            margin: 0 # Margin before bullet
        },
        content: {
            leading: 0 # Vertical padding between list items
            margin: 4 # Between bullet and content
        }
    }
}

Screenshot 2020-03-03 at 16 22 08

@aried3r aried3r changed the title Just "bottom_padding" for list possible? Same padding before and after a list between paragraphs? Mar 3, 2020
@aried3r aried3r changed the title Same padding before and after a list between paragraphs? Same margin before and after a list between paragraphs? Mar 3, 2020
@codez
Copy link
Collaborator

codez commented Mar 5, 2020

I found an unexpected move_down in the code of prawn table, which is used to render lists. Could you try the branch adjust_list_gaps with your example to see if it fixes your issue?

@aried3r
Copy link
Contributor Author

aried3r commented Mar 6, 2020

Hey! Thanks for the quick reply. It seems that it doesn't fix the issue. I dug around a bit more (and hopefully don't confuse the screenshots), and I'm not quite sure which values get mixed up.

This is with all leadings set to 0. That definitely doesn't seem correct to me.
Screenshot 2020-03-06 at 10 35 59

Thats with [:text][:leading] set properly.
Screenshot 2020-03-06 at 10 36 18

This, IIRC, is with [:text][:leading] set to 0, and [:list][:leading] set properly.
Screenshot 2020-03-06 at 10 51 16

Same as above but without borders to see what it looks like.
Screenshot 2020-03-06 at 10 54 21

Here's two with horizontal lines being drawn before and after the text and borders around the list.
[:text][:leading] is 0, [:list][:leading] is correctly set, [:text][:margin_bottom] is 0, [:list][:vertical_margin] is 0.
Screenshot 2020-03-06 at 11 10 43
And once without the table borders.
Screenshot 2020-03-06 at 11 11 32

Sorry, that it's a bit messy, I was just quickly playing around with it. I should probably at this point just write a test PDF.

Anyway, prawn_list_gap in your branch is 1.8062500000000001 in my case which isn't much (btw, we could use move_up instead of move_down with a negative number). So I think it's a case where the leading of text and the leading of lists are somehow interfering, but I might be wrong. WDYT?

@codez
Copy link
Collaborator

codez commented Mar 6, 2020

Thanks for your analysis. Are you actually using a special font or is this the standard Helvetica in your example? This really seems to be some font spacing issue depending on descenders, ascenders and line gaps. Unfortunately, I am not really an expert in this field. Probably some more digging in the Prawn/Prawn-Table source code would be necessary.

There are various specs to start from. I used

it 'creates an unordered list' do
to play around. Inserting lookatit calls after processor.parse lines will directly open the generated PDF on Linux or Mac when running the specs.

@aried3r
Copy link
Contributor Author

aried3r commented Mar 6, 2020

Are you actually using a special font or is this the standard Helvetica in your example?

I'm using "Gilroy", here's the same with Helvetica:
[:text][:leading] = 0, [:text][:margin_bottom] = 0, [:list][:leading] = 13 - font.height_at(8.5) (13 is the height of our lines)

Screenshot 2020-03-06 at 12 00 22

@aried3r
Copy link
Contributor Author

aried3r commented Mar 6, 2020

Ok, a more complete example:

BASE_LINE_GRID = 13

markup_options = {
    text: {
        font: 'Helvetica',
        size: 8.5,
        leading: BASE_LINE_GRID - font.height_at(8.5)
        # margin_bottom: 0 # Margin after each <p>, <ol>, <ul> or <table>
    },
    list: {
        vertical_margin: 0, # margin at the top and the bottom of the list
        bullet: {
            margin: 0 # Margin before bullet
        },
        content: {
            leading: BASE_LINE_GRID - font.height_at(8.5), # Vertical padding between list items
            margin: 4 # Between bullet and content
        }
    }
}

And I added another pdf.move_up(text_leading * 2) to your pdf.move_down(-prawn_list_gap). I'm not sure if that should be something else like text_leading + list_leading (list_leading doesn't exist yet) instead. Have to think about it some more.

But this is the result:
Screenshot 2020-03-06 at 12 09 36

As you can see, I have quite sophisticated graphics skills. The lines almost line up, haha.

Edit: Found this regarding the gap they add, prawnpdf/prawn#539

@codez
Copy link
Collaborator

codez commented Mar 7, 2020

I guess I found the issue. The problem seems to be that the move_down in Prawn::Table::Cell::Text#draw_content and the used with_font do not use the actual cell's font size. So our prawn_list_gap is calculated from the cell font size, the one used in draw_content from the document font size. This also affects regular tables and is well visible when using a much larger font size in cells, where the vertical alignment is quite out of place.

Probably we should open an issue in prawn-table?

@codez
Copy link
Collaborator

codez commented Mar 7, 2020

Created prawnpdf/prawn-table#120.

Considering the last commit to the prawn-table code was 4 years ago, I will fix it here anyways.

@codez
Copy link
Collaborator

codez commented Mar 7, 2020

@codez
Copy link
Collaborator

codez commented Mar 12, 2020

@aried3r did you get a chance to test the changes? If everything is ok, I will cut a new version of the gem.

@aried3r
Copy link
Contributor Author

aried3r commented Mar 12, 2020

Hey! So far I haven't, but I hope to take a closer look either tomorrow or beginning of next week.

Thanks for your work so far!

@aried3r
Copy link
Contributor Author

aried3r commented Mar 16, 2020

I had a chance to test your changes, it seems to work! I think the move_up is not technically correct (see: prawnpdf/prawn#539, texts are not centered anymore within their cells), but it seems that way we actually get the result we expect, equal margins on top and bottom.

Result:
Screenshot 2020-03-16 at 16 15 04

With table borders:
Screenshot 2020-03-16 at 16 18 16

@codez
Copy link
Collaborator

codez commented Mar 16, 2020

Great, I'm about to release a new version.

@codez codez closed this as completed Mar 16, 2020
@aried3r
Copy link
Contributor Author

aried3r commented Mar 17, 2020

Awesome, thank you for your quick response!

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