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

Enable counter-increment and -reset in page context #631

Merged
merged 1 commit into from May 24, 2018

Conversation

Projects
None yet
2 participants
@Tontyna
Contributor

Tontyna commented May 18, 2018

This PR makes it possible to reset and increment the page-based page counter. Furthermore own page-based counters can be definded.
The predefined pages counter is immutable and pretty useless when the page counter is manipulated.

Should fix #289 and (partly) #93.

Concerning counter-reset: page:
It's fine for giving the document an initial page number -- in @page:first -- but to make e.g. chapters start with page n° 1 you better utilize appropriate document-based counters combined with string-set there is no easy & intuitive solution.
The only solution I found was inserting empty divs, styled with page: resetpage; break-before:always;

@Tontyna

This comment has been minimized.

Contributor

Tontyna commented May 19, 2018

@liZe: Does the spec allow to reset or increment page-based counters in document-context?

@liZe

This comment has been minimized.

Member

liZe commented May 19, 2018

@liZe: Does the spec allow to reset or increment page-based counters in document-context?

One thing is sure: resetting a counter creates a new instance, so you can't reset the counter in the document.

About incrementing (and even accessing) the counter in the document, here is the first sentence of the Page based counter chapter:

Counters can be defined and controlled within an @page rule, and used as content in page-margin boxes.

It's not 100% clear, but I think that page-based counters can only be controlled (ie. incremented, reset) in @page rules, and only displayed in page-margin boxes.

@liZe

This comment has been minimized.

Member

liZe commented May 19, 2018

but to make e.g. chapters start with page n° 1 there is no easy & intuitive solution.

Using a counter different from page and displaying it with string-set should do the trick.

@Tontyna

This comment has been minimized.

Contributor

Tontyna commented May 19, 2018

Using a counter different from page and displaying it with string-set should do the trick.

No it doesnt. Because in the document-context we dont know when to properly increment this counter.
Only page-context knows when a new page is created. In the document we merely can force a page-break.

@Tontyna

This comment has been minimized.

Contributor

Tontyna commented May 19, 2018

It's not 100% clear, but I think that page-based counters can only be controlled (ie. incremented, reset) in @page rules, and only displayed in page-margin boxes.

draft allows counter properties in margin context

@Tontyna

This comment has been minimized.

Contributor

Tontyna commented May 19, 2018

resetting a [page based] counter [in document context] creates a new instance.

Only when we consider the involved document element being a descendant of its page 😬

Can it be that controlling page numbering (a useful, widespread, normal, necessary task) is so hard to achieve? Isn't token into account by the spec draft?!

@liZe

This comment has been minimized.

Member

liZe commented May 19, 2018

No it doesnt. Because in the document-context we dont know when to properly increment this counter.
Only page-context knows when a new page is created. In the document we merely can force a page-break.

Of course, you're right.

draft allows counter properties in margin context

Yes, but using counter-reset and counter-increment creates a new instance of counters.

Only when we consider the involved document element being a descendant of its page

Not exactly. Only when we consider the involved document not being the same block as its page (and it's definitely not).

Can it be that controlling page numbering (a useful, widespread, normal, necessary task) is so hard to achieve? Isn't token into account by the spec draft?!

Isn't that what you want?

<html>
  <style>
    @page {
      @bottom-center {
        content: counter(page);
      }
    }
    @page chapter {
      counter-reset: page 1;
    }
    h2 {
      page: chapter;
    }
  </style>
  <body>
    <h1>Book title</h1>

    <h2>Chapter 1</h2>
    <p>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur a venenatis metus. Sed sed libero lacinia, rutrum metus vel, vehicula sapien. Nulla semper lobortis lectus, in consectetur quam sollicitudin eget. Praesent eu mauris convallis, lacinia quam et, mattis nulla. Ut semper dolor ut pretium tempor. Praesent malesuada vel massa sed vestibulum. Etiam condimentum, quam at rutrum volutpat, mauris nisl cursus turpis, imperdiet venenatis massa velit nec ligula. Sed condimentum euismod nisl ut interdum. Donec nec lacus ac turpis efficitur pulvinar at et risus. Nulla imperdiet tincidunt lacus quis hendrerit. Nulla sed pretium augue. Proin eu ante augue.
    </p>
    <p>
      Nam efficitur odio ac nisl cursus, eget vestibulum urna congue. Morbi porta at lectus vel gravida. Maecenas rhoncus, nulla in elementum mattis, purus ex volutpat nulla, ut rutrum elit dui non neque. Donec sed pulvinar enim, vel consectetur enim. Aliquam aliquet dui urna, ac imperdiet felis laoreet ac. Aenean eleifend ligula nec ante finibus, nec facilisis erat imperdiet. In hac habitasse platea dictumst. Suspendisse in augue ultrices, iaculis elit non, commodo nisl. Nulla ut sodales lorem. Pellentesque est erat, lacinia ut velit id, accumsan aliquet ligula. Nunc eget nulla venenatis, interdum libero et, vehicula dui. Fusce venenatis odio tellus, id molestie purus porttitor nec. Nam in ipsum eu mauris hendrerit feugiat.
    </p>
    <p>
      Ut rhoncus eleifend nisl, nec aliquam ante mollis sed. Nunc rutrum ligula sed turpis egestas pretium. Nam fermentum molestie lobortis. Aenean ultrices erat et erat lacinia tincidunt. Integer blandit tempus ligula, ac tempor metus gravida et. Nam ac dapibus metus, et tincidunt velit. Donec vel mi quis mauris lobortis sagittis in id augue. Quisque aliquet augue nec sapien eleifend varius. Pellentesque ipsum metus, pulvinar sit amet eros ac, cursus mollis purus. Duis vulputate finibus augue, at faucibus neque tincidunt eu. Aliquam dictum dignissim odio eu varius. Pellentesque magna sapien, viverra et interdum vitae, lacinia at nibh. Praesent ac venenatis arcu. Vestibulum nec nunc a lectus auctor tempor ut a nisl.
    </p>
    <p>
      Mauris sit amet felis sodales, pharetra tellus eget, lacinia turpis. Praesent vel pretium ipsum, tincidunt pellentesque sem. Donec eu sapien sit amet urna lobortis rhoncus feugiat a arcu. Aenean lacinia lobortis erat, eget lobortis sapien. Vestibulum eleifend nibh eget nibh porta, a fringilla nibh vulputate. Vivamus fringilla nec mauris consectetur rhoncus. Duis porta maximus nibh, vitae fermentum purus venenatis ac. Praesent maximus hendrerit fermentum. In sed leo a nisi dictum efficitur mattis nec arcu. Suspendisse nec eros justo. Etiam risus diam, mattis in turpis eu, rhoncus vehicula eros. Cras cursus hendrerit mi et lacinia. Nulla sollicitudin risus et vestibulum sollicitudin.
    </p>
    <p>
      Nulla iaculis magna et volutpat malesuada. Nulla condimentum eros vitae urna euismod tincidunt. Ut vitae dapibus nisi. Suspendisse eleifend, diam blandit interdum iaculis, nisi enim pulvinar felis, nec fringilla leo turpis vitae felis. Duis malesuada odio quis ante cursus, id porta leo egestas. Phasellus a dolor quis tortor pharetra convallis. Aliquam suscipit purus nulla, et suscipit odio lacinia id. Praesent malesuada tempor arcu, vitae dapibus ligula placerat non. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse non urna elit. Morbi accumsan enim vel neque maximus, ac imperdiet nunc sollicitudin. Curabitur a lacinia elit. Sed quis lobortis tellus. Sed ultricies, mi ac elementum blandit, urna sapien tristique urna, et suscipit quam libero id nibh.
    </p>

    <h2>Chapter 2</h2>
    <p>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur a venenatis metus. Sed sed libero lacinia, rutrum metus vel, vehicula sapien. Nulla semper lobortis lectus, in consectetur quam sollicitudin eget. Praesent eu mauris convallis, lacinia quam et, mattis nulla. Ut semper dolor ut pretium tempor. Praesent malesuada vel massa sed vestibulum. Etiam condimentum, quam at rutrum volutpat, mauris nisl cursus turpis, imperdiet venenatis massa velit nec ligula. Sed condimentum euismod nisl ut interdum. Donec nec lacus ac turpis efficitur pulvinar at et risus. Nulla imperdiet tincidunt lacus quis hendrerit. Nulla sed pretium augue. Proin eu ante augue.
    </p>
    <p>
      Nam efficitur odio ac nisl cursus, eget vestibulum urna congue. Morbi porta at lectus vel gravida. Maecenas rhoncus, nulla in elementum mattis, purus ex volutpat nulla, ut rutrum elit dui non neque. Donec sed pulvinar enim, vel consectetur enim. Aliquam aliquet dui urna, ac imperdiet felis laoreet ac. Aenean eleifend ligula nec ante finibus, nec facilisis erat imperdiet. In hac habitasse platea dictumst. Suspendisse in augue ultrices, iaculis elit non, commodo nisl. Nulla ut sodales lorem. Pellentesque est erat, lacinia ut velit id, accumsan aliquet ligula. Nunc eget nulla venenatis, interdum libero et, vehicula dui. Fusce venenatis odio tellus, id molestie purus porttitor nec. Nam in ipsum eu mauris hendrerit feugiat.
    </p>
    <p>
      Ut rhoncus eleifend nisl, nec aliquam ante mollis sed. Nunc rutrum ligula sed turpis egestas pretium. Nam fermentum molestie lobortis. Aenean ultrices erat et erat lacinia tincidunt. Integer blandit tempus ligula, ac tempor metus gravida et. Nam ac dapibus metus, et tincidunt velit. Donec vel mi quis mauris lobortis sagittis in id augue. Quisque aliquet augue nec sapien eleifend varius. Pellentesque ipsum metus, pulvinar sit amet eros ac, cursus mollis purus. Duis vulputate finibus augue, at faucibus neque tincidunt eu. Aliquam dictum dignissim odio eu varius. Pellentesque magna sapien, viverra et interdum vitae, lacinia at nibh. Praesent ac venenatis arcu. Vestibulum nec nunc a lectus auctor tempor ut a nisl.
    </p>
    <p>
      Mauris sit amet felis sodales, pharetra tellus eget, lacinia turpis. Praesent vel pretium ipsum, tincidunt pellentesque sem. Donec eu sapien sit amet urna lobortis rhoncus feugiat a arcu. Aenean lacinia lobortis erat, eget lobortis sapien. Vestibulum eleifend nibh eget nibh porta, a fringilla nibh vulputate. Vivamus fringilla nec mauris consectetur rhoncus. Duis porta maximus nibh, vitae fermentum purus venenatis ac. Praesent maximus hendrerit fermentum. In sed leo a nisi dictum efficitur mattis nec arcu. Suspendisse nec eros justo. Etiam risus diam, mattis in turpis eu, rhoncus vehicula eros. Cras cursus hendrerit mi et lacinia. Nulla sollicitudin risus et vestibulum sollicitudin.
    </p>
    <p>
      Nulla iaculis magna et volutpat malesuada. Nulla condimentum eros vitae urna euismod tincidunt. Ut vitae dapibus nisi. Suspendisse eleifend, diam blandit interdum iaculis, nisi enim pulvinar felis, nec fringilla leo turpis vitae felis. Duis malesuada odio quis ante cursus, id porta leo egestas. Phasellus a dolor quis tortor pharetra convallis. Aliquam suscipit purus nulla, et suscipit odio lacinia id. Praesent malesuada tempor arcu, vitae dapibus ligula placerat non. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse non urna elit. Morbi accumsan enim vel neque maximus, ac imperdiet nunc sollicitudin. Curabitur a lacinia elit. Sed quis lobortis tellus. Sed ultricies, mi ac elementum blandit, urna sapien tristique urna, et suscipit quam libero id nibh.
    </p>

    <h2>Chapter 3</h2>
    <p>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur a venenatis metus. Sed sed libero lacinia, rutrum metus vel, vehicula sapien. Nulla semper lobortis lectus, in consectetur quam sollicitudin eget. Praesent eu mauris convallis, lacinia quam et, mattis nulla. Ut semper dolor ut pretium tempor. Praesent malesuada vel massa sed vestibulum. Etiam condimentum, quam at rutrum volutpat, mauris nisl cursus turpis, imperdiet venenatis massa velit nec ligula. Sed condimentum euismod nisl ut interdum. Donec nec lacus ac turpis efficitur pulvinar at et risus. Nulla imperdiet tincidunt lacus quis hendrerit. Nulla sed pretium augue. Proin eu ante augue.
    </p>
    <p>
      Nam efficitur odio ac nisl cursus, eget vestibulum urna congue. Morbi porta at lectus vel gravida. Maecenas rhoncus, nulla in elementum mattis, purus ex volutpat nulla, ut rutrum elit dui non neque. Donec sed pulvinar enim, vel consectetur enim. Aliquam aliquet dui urna, ac imperdiet felis laoreet ac. Aenean eleifend ligula nec ante finibus, nec facilisis erat imperdiet. In hac habitasse platea dictumst. Suspendisse in augue ultrices, iaculis elit non, commodo nisl. Nulla ut sodales lorem. Pellentesque est erat, lacinia ut velit id, accumsan aliquet ligula. Nunc eget nulla venenatis, interdum libero et, vehicula dui. Fusce venenatis odio tellus, id molestie purus porttitor nec. Nam in ipsum eu mauris hendrerit feugiat.
    </p>
    <p>
      Ut rhoncus eleifend nisl, nec aliquam ante mollis sed. Nunc rutrum ligula sed turpis egestas pretium. Nam fermentum molestie lobortis. Aenean ultrices erat et erat lacinia tincidunt. Integer blandit tempus ligula, ac tempor metus gravida et. Nam ac dapibus metus, et tincidunt velit. Donec vel mi quis mauris lobortis sagittis in id augue. Quisque aliquet augue nec sapien eleifend varius. Pellentesque ipsum metus, pulvinar sit amet eros ac, cursus mollis purus. Duis vulputate finibus augue, at faucibus neque tincidunt eu. Aliquam dictum dignissim odio eu varius. Pellentesque magna sapien, viverra et interdum vitae, lacinia at nibh. Praesent ac venenatis arcu. Vestibulum nec nunc a lectus auctor tempor ut a nisl.
    </p>
    <p>
      Mauris sit amet felis sodales, pharetra tellus eget, lacinia turpis. Praesent vel pretium ipsum, tincidunt pellentesque sem. Donec eu sapien sit amet urna lobortis rhoncus feugiat a arcu. Aenean lacinia lobortis erat, eget lobortis sapien. Vestibulum eleifend nibh eget nibh porta, a fringilla nibh vulputate. Vivamus fringilla nec mauris consectetur rhoncus. Duis porta maximus nibh, vitae fermentum purus venenatis ac. Praesent maximus hendrerit fermentum. In sed leo a nisi dictum efficitur mattis nec arcu. Suspendisse nec eros justo. Etiam risus diam, mattis in turpis eu, rhoncus vehicula eros. Cras cursus hendrerit mi et lacinia. Nulla sollicitudin risus et vestibulum sollicitudin.
    </p>
    <p>
      Nulla iaculis magna et volutpat malesuada. Nulla condimentum eros vitae urna euismod tincidunt. Ut vitae dapibus nisi. Suspendisse eleifend, diam blandit interdum iaculis, nisi enim pulvinar felis, nec fringilla leo turpis vitae felis. Duis malesuada odio quis ante cursus, id porta leo egestas. Phasellus a dolor quis tortor pharetra convallis. Aliquam suscipit purus nulla, et suscipit odio lacinia id. Praesent malesuada tempor arcu, vitae dapibus ligula placerat non. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse non urna elit. Morbi accumsan enim vel neque maximus, ac imperdiet nunc sollicitudin. Curabitur a lacinia elit. Sed quis lobortis tellus. Sed ultricies, mi ac elementum blandit, urna sapien tristique urna, et suscipit quam libero id nibh.
    </p>
  </body>
</html>
@Tontyna

This comment has been minimized.

Contributor

Tontyna commented May 19, 2018

OMG! You're right! It works!

My error when trying to achieve this was: I didnt apply the page: chapter to the <h2> solely, but to a <div class="chapter">, wrapping the whole chapter, its heading together with its paragraphs. And of course, every page was number 1...

@Tontyna

This comment has been minimized.

Contributor

Tontyna commented May 19, 2018

Are you shure that in margin context

using counter-reset and counter-increment creates a new instance of counters.

counter-reset definitely does. That's implemented in this PR.
But counter-increment? I thought counter-increment creates a new counter only when that counter doesn't exist yet.
Edit: That's how it's implemented in this PR by calling formatting_structures.build.update_counters()

Probably youre right. I'll never grasp those nested counters and their scope.

BTW: I was surprised when I detected that as soon as there is a list in my document there is a counter named list-item present until the html ends. Naively had expected that counter to vanish at the list's closing tag.

@liZe

This comment has been minimized.

Member

liZe commented May 19, 2018

But counter-increment? I thought counter-increment creates a new counter only when that counter doesn't exist yet.

counter-increment doesn't create a new instance in a document, but it does in a page margin: "If a counter is reset or incremented within a margin context, it is in scope for that page-margin box and obscures any counters of the same name in both the page context and the document."

@Tontyna

This comment has been minimized.

Contributor

Tontyna commented May 20, 2018

I dont' get it without an example.

@page {
  counter-increment: pagebased;
  @top-center {
    counter-reset: page 1 pagebased 1 topbased 1;
    content: "top-center:: page: " counters(page, '.') " pagebased: " counters(pagebased, '.') " topbased: " counters(topbased, '.')
  }
  @bottom-center {
    counter-increment: page pagebased topbased;
    content: "bottom-center:: page: " counters(page, '.') " pagebased: " counters(pagebased, '.') " topbased: " counters(topbased, '.')
  }
}

Currently this PR produces (on the first page):

  • top-center:: page: 1.1 pagebased: 1.1 topbased: 1
  • bottom-center:: page: 2 pagebased: 2 topbased: 1

The bottom-center looked right until your explanation made me doubt. If a "new instance" is created there should be two numbers for page and pagebased...

@liZe

This comment has been minimized.

Member

liZe commented May 21, 2018

The bottom-center looked right until your explanation made me doubt. If a "new instance" is created there should be two numbers for page and pagebased...

As far as I understand the spec, the content of @bottom-center should be page: 1.1 pagebased: 1.1 topbased: 1.

But… Who cares 😉?

@Tontyna

This comment has been minimized.

Contributor

Tontyna commented May 21, 2018

So let's decide for the no new instance version.
Feels organic, formatting_structures.build.update_counters() works out of the box.

BTW: That's what everybody expects until reading the spec distracts his brain. See your comment

@liZe

This comment has been minimized.

Member

liZe commented May 21, 2018

BTW: That's what everybody expects until reading the spec distracts his brain. See your comment

Good catch! We should get in touch with the W3C.

Can I merge?

@Tontyna

This comment has been minimized.

Contributor

Tontyna commented May 21, 2018

Can I merge?

Yes.

@liZe liZe merged commit 85279f4 into Kozea:master May 24, 2018

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

@Tontyna Tontyna deleted the Tontyna:pagebased-counters branch Jun 12, 2018

@liZe liZe added this to the 43 milestone Oct 11, 2018

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Nov 14, 2018

py-weasyprint: Update to 43.
Version 43
----------

Released on 2018-11-09.

Bug fixes:

* `#726 <https://github.com/Kozea/WeasyPrint/issues/726>`_:
  Make empty strings clear previous values of named strings
* `#729 <https://github.com/Kozea/WeasyPrint/issues/729>`_:
  Include tools in packaging

This version also includes the changes from unstable rc1 and rc2 versions
listed below.

Version 43rc2
-------------

Released on 2018-11-02.

**This version is experimental, don't use it in production. If you find bugs,
please report them!**

Bug fixes:

* `#706 <https://github.com/Kozea/WeasyPrint/issues/706>`_:
  Fix text-indent at the beginning of a page
* `#687 <https://github.com/Kozea/WeasyPrint/issues/687>`_:
  Allow query strings in file:// URIs
* `#720 <https://github.com/Kozea/WeasyPrint/issues/720>`_:
  Optimize minimum size calculation of long inline elements
* `#717 <https://github.com/Kozea/WeasyPrint/issues/717>`_:
  Display <details> tags as blocks
* `#691 <https://github.com/Kozea/WeasyPrint/issues/691>`_:
  Don't recalculate max content widths when distributing extra space for tables
* `#722 <https://github.com/Kozea/WeasyPrint/issues/722>`_:
  Fix bookmarks and strings set on images
* `#723 <https://github.com/Kozea/WeasyPrint/issues/723>`_:
  Warn users when string() is not used in page margin


Version 43rc1
-------------

Released on 2018-10-15.

**This version is experimental, don't use it in production. If you find bugs,
please report them!**

Dependencies:

* Python 3.4+ is now needed, Python 2.x is not supported anymore
* Cairo 1.15.4+ is now needed, but 1.10+ should work with missing features
  (such as links, outlines and metadata)
* Pdfrw is not needed anymore

New features:

* `Beautiful website <https://weasyprint.org>`_
* `#579 <https://github.com/Kozea/WeasyPrint/issues/579>`_:
  Initial support of flexbox
* `#592 <https://github.com/Kozea/WeasyPrint/pull/592>`_:
  Support @font-face on Windows
* `#306 <https://github.com/Kozea/WeasyPrint/issues/306>`_:
  Add a timeout parameter to the URL fetcher functions
* `#594 <https://github.com/Kozea/WeasyPrint/pull/594>`_:
  Split tests using modern pytest features
* `#599 <https://github.com/Kozea/WeasyPrint/pull/599>`_:
  Make tests pass on Windows
* `#604 <https://github.com/Kozea/WeasyPrint/pull/604>`_:
  Handle target counters and target texts
* `#631 <https://github.com/Kozea/WeasyPrint/pull/631>`_:
  Enable counter-increment and counter-reset in page context
* `#622 <https://github.com/Kozea/WeasyPrint/issues/622>`_:
  Allow pathlib.Path objects for HTML, CSS and Attachment classes
* `#674 <https://github.com/Kozea/WeasyPrint/issues/674>`_:
  Add extensive installation instructions for Windows

Bug fixes:

* `#558 <https://github.com/Kozea/WeasyPrint/issues/558>`_:
  Fix attachments
* `#565 <https://github.com/Kozea/WeasyPrint/issues/565>`_,
  `#596 <https://github.com/Kozea/WeasyPrint/issues/596>`_,
  `#539 <https://github.com/Kozea/WeasyPrint/issues/539>`_:
  Fix many PDF rendering, printing and compatibility problems
* `#614 <https://github.com/Kozea/WeasyPrint/issues/614>`_:
  Avoid crashes and endless loops caused by a Pango bug
* `#662 <https://github.com/Kozea/WeasyPrint/pull/662>`_:
  Fix warnings and errors when generating documentation
* `#666 <https://github.com/Kozea/WeasyPrint/issues/666>`_,
  `#685 <https://github.com/Kozea/WeasyPrint/issues/685>`_:
  Fix many table layout rendering problems
* `#680 <https://github.com/Kozea/WeasyPrint/pull/680>`_:
  Don't crash when there's no font available
* `#662 <https://github.com/Kozea/WeasyPrint/pull/662>`_:
  Fix support of some align values in tables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment