Thursday 15 April 2010

true type fonts - Making ttf characters touch in an image with PHP -


I appreciate the experience with more than any PHP experience.

I use PHP to create pass-packed text pictures So far, it works like this:

  1. Firstly, it's the X number of characters Generates a random string.
  2. Then it creates an image with the background image.
  3. It keeps the first character in a specific location (with some random type of shape, rotation etc.)
  4. Then it keeps the next character so that its bottom of the bounding box Clicked at the bottom right corner of the bounding box of the character at the same point in the left corner (already some randomness etc.). For this, the code goes like this:

      $ coords = array (); $ Pos_y = $ this- & gt; Image_highthought2 ($ Counter = 0; $ counter = <$ this-> code_length; $ counter ++) {$ sbox = @imagettfbbox ($ size, $ angle, $ font, $ this-> code [$ counter] ]) for; If ($ counter == 0) $ pos_x = $ this- & gt; Image_width / $ this- & gt; Code_lambi; Else // element 2 is the X co-order of right bottom corner of TTF box $ pos_x = $ coords [$ counter-1] [2]; @imaggette te cost ($ - this-> image, $ size, $ angle, $ pos_x, $ pos_y, $ fontcolor, $ font, $ this- & gt; code [$ counter]); }    
  5. and repeat 4 for all the characters that come in the string.

    What can I do as much as possible as possible, every character that has touched my neighbor has done everything right now for characters like 'M' Where the lower edges of the glyph are made more or less in the corner of the alleged box, but it is not close to any letter like 'G' or 'I'

    Does anyone have the bounding box Rather than the actual dimensions of TTF glyph Find a way to be? Or can someone think of any better way to deal with this problem?

    Any thoughts / ideas / advice have been greatly appreciated.

    The bounding box is just - a box that shows the minimum / maximum X and Y values ​​for the text. If you have letter C and letter O, touching your box box will not touch the actual characters, because the line has joined two letters, the top is up and down, and o in the middle. / P>

    If you want to make sure that the letters have to be touched, you will need to get the glyph outline data instead, and use either clever algorithms that find the projection points for glyph 1 on glyph 2, Or use a conspiracy Minneux algorithm where you place it next to two others, rinse them with transparent color, see if any pixels are colored, then they It would be green, if so, then transfer the letters apart from distance D, try again, if they do not overlap, they go back together by distance D / 2, if they overlap, by D / 4 Turn out again, if they do not, then move more than D / 4, then correct with D / 8, D / 16 etc. until the distance is not less than the pixel.

    The bounding box alignment is cheap and easy, what do you want to do, unfortunately, not both. It can be cheap (but then the algorithm is hard) or easier (but then the algorithm is expensive).

    (Do you want to give a try using glyph outline data, you can try using FreeType2, or something like that)

No comments:

Post a Comment