Welcome Guest, Not a member yet? Register   Sign In
Really challenging PHP/CI requirement
#11

[eluser]rogierb[/eluser]
didn't see the "text" part. Thought it was just lines.

Getting a text width is a lot more complicated since php cannot return font width(at least to my knowledge). I used imagettfbbox to calculate the width (GD2) of every single char in points, put the char and width in an array.
Then printed the array and moved to a new line if there was a space when reaching the maximum width in points.

A real pain to do and it worked somehow. This also assumes you have the font file.
Also slow as hell.
#12

[eluser]Dam1an[/eluser]
Assuming you only need to suport 1 (or a limited number of fonts) you could store the width of each character of that font in an array, and then multiply the number of times each char occurs by its width by some multiplier for the font size.
The efficiency of this will of course depend on how fast you can count each of the chars (a single pass through the text with lots of comparisons)
#13

[eluser]tomcode[/eluser]
@RogierB : I didn't know imagettfbbox(), nice, but still, this is not all, since You do not have the kerning so :

- it will be approximative
- and this is doing the work twice because the PDF library will do a similar job anyway to render the line.

That's why I still think these kind of infos should be provided by the PDF library as You render.
#14

[eluser]rogierb[/eluser]
@tomcode hence the "it worked somehow" and the "slow as hell":-)

I was lucky enough to only have to support a couple of fonts. So Kerning and letter-spacing was not really an issue. I just used an average for both for a group of 10 characters.
#15

[eluser]tomcode[/eluser]
@RogierB : thanks for the info.
#16

[eluser]Myles Wakeham[/eluser]
It would seem that you all are certainly understanding my dilemma. The problem is that I need to know the number of lines that will be required in total to produce 'flowing' text, and yes the font will vary. Our desktop software that is being replaced with this CI app calls a 3rd party function where you can pass in the text block, font, width of the area to print in, and it returns back an 'exploded' array of the text cut into elements that will work. Then I can count the number of lines required, and work out the number of pages by using the fixed no of lines that the area it has to print in will accept. Its a bit of trickery to do this, but it works and does the job.

I found some similar routines in PDF classes and I'm probably just going to extract the methods from those classes and re-create the function I have just gotten used to having in other worlds. However if anyone knows of such a function out there, I'm all ears....

Myles
#17

[eluser]Myles Wakeham[/eluser]
I think I found the answer. There's a PHP class on Sourceforge for Postscript handling that has exactly the methods I need to do this. It seems to rely on the GD module to be installed with PHP (looks for function 'ImagePSLoadFont' and uses base level Postscript routines to calculate text width, and split strings into blocks based on text width (and it seems to handle text height as well).

If anyone is interested, here's the link to it:

http://www.koders.com/php/fidEA8BDDEB114...AC95B.aspx

Look for the methods '_get_string_split()' and '_get_text_length()'. I'll create this as a CI library and give it some tests. I'll post back here on the outcome.

Myles
#18

[eluser]tomcode[/eluser]
Good luck, and yeah, would be interesting to see whether it worked for You.




Theme © iAndrew 2016 - Forum software by © MyBB