Page 1 of 1

Number of Chars/Spaces per Line Given a Font /Size?

PostPosted: Fri Apr 27, 2018 12:09 am
by arthurw
Dear lovely , kind, generous, blessed, prosperous readers,

So is there a way of finding out how many characters and spaces can fit into a line of word document given a certain font and font size and narrow margins etc?

I was thinking of finding out by trial and error but I figured before writing the code I may as well ask.

Thank you! and happy thursday!

Re: Number of Chars/Spaces per Line Given a Font /Size?

PostPosted: Fri Apr 27, 2018 9:49 am
by jason
This is difficult to do: you need to have a "layout engine" which mimic's Word's, like we have in the commercial PDF Converter. One open source approach might be to piggy-back off FOP: convert the line or partial line to XSL FO, then ask FOP for its layout. You might need to hack FOP to do this. Bear in mind that for a proportional font, you can fit "llllll" in similar space to "www", so if your question is "how much of this font can i fit?" you'll need to make assumptions about the distribution of characters.

Re: Number of Chars/Spaces per Line Given a Font /Size?

PostPosted: Fri Apr 27, 2018 2:35 pm
by arthurw
jason wrote:This is difficult to do: you need to have a "layout engine" which mimic's Word's, like we have in the commercial PDF Converter. One open source approach might be to piggy-back off FOP: convert the line or partial line to XSL FO, then ask FOP for its layout. You might need to hack FOP to do this. Bear in mind that for a proportional font, you can fit "llllll" in similar space to "www", so if your question is "how much of this font can i fit?" you'll need to make assumptions about the distribution of characters.



hmm I don't know I have an easier solution. Will get back to you when I have the answer by tomorrow.

Thanks Jason! you are awesome! your one page cheat sheet is brilliant and lovely and I love it.

Thanks for being so wonderful, helpful, kind! please forgive me if I have erred, may God forgive us and give us all the right spirit.

Re: Number of Chars/Spaces per Line Given a Font /Size?

PostPosted: Sat Apr 28, 2018 9:38 am
by arthurw
jason wrote:This is difficult to do: you need to have a "layout engine" which mimic's Word's, like we have in the commercial PDF Converter. One open source approach might be to piggy-back off FOP: convert the line or partial line to XSL FO, then ask FOP for its layout. You might need to hack FOP to do this. Bear in mind that for a proportional font, you can fit "llllll" in similar space to "www", so if your question is "how much of this font can i fit?" you'll need to make assumptions about the distribution of characters.



Dear Jason, I came up with a solution and it is accurate upto 5 decimal places as to location (for my purposes).
I will share with the community even though my solution is not very extensible, unfortunately, right now, I guess.
It only works for the time being for a font of size 9 , calibri and narrow margins.
I have not thought about how I may extend this but this is just a quick work around approach.