Skip to main content

Ralsina.Me — Roberto Alsina's website

rst2pdf: progress in smartframe branch

Yes, it is get­ting bet­ter. Now there are left­-float­ing or right-float­ing el­e­ments, and you can float pret­ty much any­thing (ex­cept lit­er­al block­s, that's a prob­lem) by us­ing the class di­rec­tive. Here's how it look­s:

sidebar-beige

rst2pdf: smartframes branch

To­day I start­ed a branch called Smart­Frames. The main goal is to achieve a bet­ter text flow in the doc­u­ment (for ex­am­ple, for side­bars), and it is start­ing to get there, slow­ly.

Let's con­sid­er how ReST side­bars are ren­dered in the dif­fer­ent writ­er­s.

We'll work with an or­di­nary lorem ip­sum that has a side­bar de­clared just be­fore it.

Here's HTM­L:

sidebar-html

And here's La­TeX:

sidebar-latex

Each one has its good side and its bad side.

The HTML side­bar is a re­al side­bar, while the La­Tex one is some sort of in­sert.

OTO­H, the ragged text against the HTML side­bar is ... hor­rid.

So, I want­ed some­thing at least a bit bet­ter than that for rst2pdf. In the best of all pos­si­ble world­s, it would be the neat text align­ment of La­Tex with the float­ing HTML side­bar.

Here's how it looks now:

sidebar-pdf2

There are some mi­nor prob­lems with the cur­rent im­ple­men­ta­tion, such that the side­bar is al­ways aligned to the top of a para­graph, and some spac­ing is­sues.

How is it done? Let me tell you: it was not triv­ial :-)

In fact it's pret­ty evil, but here's a quick ex­pla­na­tion:

When I get a side­bar flow­able, I in­sert a new frame in the page tem­plate where the side­bar should go, then call a frame­break, in­sert the "re­al" side­bar, a "frame­cut­ter" and an­oth­er frame­break.

The frame­cut­ter is a flow­able that does noth­ing vis­i­ble, but in­serts an­oth­er two frames, one at the right of the side­bar with the same height, and an­oth­er be­low the side­bar, full width.

I need to use the frame­cut­ter be­cause I don't know the height of the side­bar un­til af­ter it's drawn.

So, we now have 4 frames in­stead of one:

  1. The orig­i­­nal frame, cov­­ers the whole page, but has a frame­break above the side­bar.

  2. The side­bar frame, which is very tal­l, but has a frame­break be­low the side­bar tex­t.

  3. A beside-the-side­bar frame, short and wide, start­ing at the right of the side­bar.

  4. A be­low-the-side­bar frame, wide and tal­l, start­ing be­low the side­bar.

The text should flow from 1 to 3 to 4 neat­ly and the seams should­n't show.

Here's a pic­ture that MAY make it clear (there are some odd dis­place­ments: those were bugs):

sidebar-wires

So, I'm not call­ing it a suc­cess yet, but it is look­ing de­cen­t.

rst2pdf is going to be one day late. But there's a good reason.

Be­sides ev­ery­thing I men­tioned yes­ter­day, to­day I im­ple­ment­ed two rather im­por­tant fea­tures: cas­cad­ing stylesheet­s, and user-de­fined page lay­out­s. Here is a screen­shot:

montecristo-3

That neat two-­col­umn lay­out is done by adding this to the stylesheet:

"pageTemplates" : {
  "firstPage": {
      "frames": [
          ["0cm", "0cm", "49%", "100%"],
          ["51%", "0cm", "49%", "100%"]
      ]
  }
},

The name "first­Page" is mag­i­cal right now, and there's no way to change from one tem­plate to an­oth­er (yet), and un­til I do that, I won't be re­leas­ing.

Here's what cas­cad­ing stylesheets does. Sup­pose you want to use A5 pa­per and size 12 Times New Ro­man fonts? Here's all the stylesheet you need:

{
  "pageSetup" : {
    "size": "A5",
  },
  "fontsAlias" : {
    "stdFont": "Times-Roman",
  },
  "styles" : [
    ["base" , {
      "fontSize": 14,
      "leading": 16
    }]
  ]
}

Al­so, you can spec­i­fy as many stylesheets as you want in the com­mand line. So you can have one that sets the pa­per size, one for page lay­out as above, one for font "set­s", etc.

Neat, is­n't it?

Rstpdf wil be released again tomorrow. And it's a good release.

How good? Let me tell you...

  • Sup­­port for PDF ta­ble of con­­tents

  • Sec­­tion names and num­bers in head­­er­s/­­foot­ers

  • Com­­pressed PDFs (or not)

  • Guess im­age sizes. Spe­­cial­­ly if you meant to use them in a web page and de­­clared just ":width: 50%"

  • Gut­ter mar­­gin sup­­port

  • Raw di­rec­­tive (in­sert page­breaks and ver­ti­­cal space man­u­al­­ly)

  • Of­fers a do­cu­til­s-­­com­­pli­ant API (and an­oth­er API, too)

  • In­­­clude full or par­­tial files for code-block. That means you can ex­­tract code and show it in your doc­u­­men­t!

  • Huge code cleanup lead by Nico­las Lau­rance.

  • Work­ing mul­ti­lin­gual hy­phen­a­tion. You can have a per-­­para­­graph lan­guage and hy­phen­ate it cor­rec­t­­ly.


Contents © 2000-2023 Roberto Alsina