2D Vector Text Help

BlitzPlus Forums/BlitzPlus Programming/2D Vector Text Help

Rage_Matrix(Posted 2003) [#1]
Hi all,

I'm getting my head round vector graphics in 2D at the moment and trying to construct some routines to draw vector graphics text based on a normal text string to a certain XY co-ordinate.

I would like some advice or suggestions for ways of doing this. My first thought was to have a generic data structure for each letter made up of nine points (each with an XY co-ordinate enclosed in a Type) like this: -

1     2     3
*     *     *


4     5     6
*     *     *


7     8     9
*     *     *


So, for example, to draw the letter 'A', you would store the pattern like this: -

FROM   TO
7      4        2 Bytes
4      2        2 Bytes
2      6        2 Bytes
6      9        2 Bytes
4      6        2 Bytes


The generic data structure of these nine points (with 5 being the local origin) would be used to draw the letters only, but not stored for each letter (I think).

Problem is, I'm not really sure where to go from here. What data structures could I use for this? Arrays? Data banks? Any ideas on how to implement this? Is it too complicated or is there an easier way?

Thank you for any advice.

Rage_Matrix


semar(Posted 2003) [#2]
IMO I think you are going in a good direction; the problem here is how to store the information about each letter, as you spotted.

So, giving an origin x,y point, the algo should be able to draw the requested letter.

Personally - but this is a question of taste - I would go for types.

I would store each type collection in a way that when you find the first element of a letter, ex. C, then you read the elements until you find the first element of the next letter (D).

So the type structure would be:
type t_vector
field letter$ ;ex "A"
field v_from
field v_to
end type


So, for example, the letter "A" would be represented with:

"A",7,4
"A",4,2
"A",2,6
"A",6,9
"A",4,6


Just right now, I come in mind with another idea: Data statements:

Data "A",7,4,4,2,2,6,6,9,4,6
Data "C",3,2,2,1,1,4,4,7,7,8,8,9
;and so on


Bare in mind that the vector representation that you posted, is not good to draw all the letters; for example, if you want to draw "B" or "Q" for example, then you run into problems..

Anyway, if I were you, I would simplify my life using just a pre-made graphic font for that.. just draw your own char set with a 'vector' style, and you are ready to fill the screen with all the text you like..

Each letter would be then an image, and you can store it in a normal array of images.

Assumed that the letter "A" has the ascii n. 50 (it's only an example), then you can easily draw any char with:

;fill the array of images
;assuming that each char image is saved like CHAR_N.PNG where N goes from 0 through 25

dim arr_img(26)
for n = 0 to 25
arr_img(n) = loadimage("char_" + n + ".PNG")
next
draw_char(x,y,"A")
;
;
;================================
function draw_char(x,y,the_char$)
;================================

text x,y,arr_img(asc(the_char) - 50)

end function


Hope this has sense for you,
Sergio.


Andy_A(Posted 2003) [#3]
Thought about this a bit, and believe that you will be best served by doing like they did in the old days
(circa 1960-1980), using true vectors. Vectors can be implemented with Cosine/Sine and radii (direction and magnitude).

The basic algorithm is this:

Design Considerations:
Choose an origin to plot a path using the least number of vectors to draw the character line by line.
If you always use position 1 or 7 as the origin for every character,
you can easily rotate text and update character spacing with a minimum of calculations.

Implementation:
There are a finite number of vectors that are used to draw between "To" and "From" points.
Pre-calculate exact sequence for origin and "To/From" coordinates into an array for every character.
Example: vecArray%(charNum%, numVecs%, orgX%, orgY%, destX%, destY%)

I mention an array because even though types are fast, there will be some overhead in searching for the right character.
With arrays you can do a one-to-one mapping with ASCII codes to enhance speed, at the cost of using more memory. It's always a trade off.

If Text Scale or Text Angle are changed these should be relatively fast and easy to update. They will remain the same until the next Text Scale, Text Angle update.
So it'll be slightly slower for Scale/Angle updates but relatively constistent when no change occurs.

Using this scheme the actual character drawing code just reads the unique set of x1, y1 to x2, y2 coordinates for
each character from a type variable.

Then of course, if it's just the retro look of vector chars your looking for, bitmaps are pretty fast and easy.

Good Luck,

Andy


Rage_Matrix(Posted 2003) [#4]
Thanks very much guys, that was exactly the kind of catalyst I was looking for. I'm going to go with the "true vector" method. It *would* be easier to just use some retro looking vector bitmaps, but thats not the way I want to do it, so I'm going to do with the true vector method. If I come up with anything halfway useable, I'll post the result.

This is a work in progress, so if anyone else has any comments or anything like that, that would be great.

Rage_Matrix


Mark1nc(Posted 2003) [#5]
Have a look at what I did for my Quantum remake (called Blitzum)

Source is here:

http://www.incitti.com/Blitz/

I had a vector text drawing routine that I could scale the text with that I used to create a bitmap of a fixed size for more speed.


Rage_Matrix(Posted 2003) [#6]
Thanks Mark1nc :)