Hi,
I am working on a tool to convert Markdown into PDF's and I'm using Prawn for the PDF output.
The basics seem to work well. I can do bold, italic, paragraphs, headings, indentation etc. and this works really well.
I have now started to tackle the problem of inline images which seem to be beyond my knowledge. Before I look into the insides of Prawn I would like to know if I'm on the right track or perhaps missing something obvious.
What I'm trying to achieve is that an image is somewhere in the middle of line, almost as if they are text. These inline images are supported by extended Markdown.
My logic so far is that I should first render the text before the image using a pdf.text_box, then render the image using pdf.image and then finally the text behind the image again using pdf.text_box. I know the wanted dimensions of the image so in order to aline the bottom of the image with the text I have to offset the Y-coordinate for the text a bit so that both the image and the text neatly fit below the line above.
The flow is like this:
- compute the difference between the height of the image and the font size.
- If this height is >0 move the cursor down this many points so that we know that the image fits nicely below the line above.
- Plot the leading text before the image using pdf.text_box
- Plot the image using pdf.image at an x-offset of the width of the leading text
- Plot the trailing text after the image using pdf.text_box with an x-offset of the width of the leading text plus the width of the image.
For simple cases this logic works well. However there are many cases that break this logic. E.g. if the leading text spans more than one line this breaks.
So alternatively I should be able to use :overlow => "truncate" on the text box and plot each line one by one. That however requires that I know before I plot if this line contains the image or not. If it doesn't I should use a normal line distance and if it does I should add the extra line distance to make it fit.
In an ideal world this could be something that is inline in the text itself, e.g. "This is a line with an <image name='example.png' height=30> in the middle"
Can I know upfront if a text box will give excess_text?
Would an extension of inline images make sense, i.e. does this fit the style guide?
Am I on the right track or is this completely off? Any advise is appreciated.
Regards,
Fred