PDA

View Full Version : How to draw freetype fonts on QGraphicsScene?



sarbh20ss
5th September 2013, 12:15
Hey i am trying to draw freetype font on QGraphicsScene....

I have attached my header and source files for program.

I am using freetype lib functions to get glyph outlins for a given character from a give font
I am drawing character on image then converting it to pixmap and drawing that pixmap on scene.
is this the right way to draw fonts on QGraphicsScene?
how to make that character move, or select or re scale?
what is the another way to draw fonts in Qt?

I also want to pop up this font drawing window when i click on the option or say button of my base aplication.
how to do that?
i tried doing that also ...even the window comes up but when I close the window...the 'Application output' tab shows that 'The program has unexpectedly finished' .
I can't figure out why is that?

how to solve these problems??

wysota
5th September 2013, 14:42
is this the right way to draw fonts on QGraphicsScene?
No. Use QGraphicsSimpleTextItem.


I also want to pop up this font drawing window when i click on the option or say button of my base aplication.
how to do that?
Use signals and slots.


i tried doing that also ...even the window comes up but when I close the window...the 'Application output' tab shows that 'The program has unexpectedly finished' .
I can't figure out why is that?
You can use a debugger to debug your code to see where it crashes and why.

sarbh20ss
6th September 2013, 06:32
hey thanks for quick reply.

I read the documentation of QGraphicsSimpleTextItem.
It allows to draw simple text with given font,pen.

But in my program I want to render the glyph outlines read from a font file for a give character.
So i don't directly have a text to draw but I want to draw these glyphs on scene so that I can make it selectable,movable, re scalable.
Further when this succeeds I want to implement a logic to edit these outlines.
So is it possible to do this with QGraphicsScene.
I want to do my whole application in Qt because it is very simple to develop GUI using Qt.

I am using Signals and Slots to pop out the window but as I mentioned it gives me the output as 'The program has unexpectedly finished'.

So how do I solve this problem?

wysota
6th September 2013, 09:28
You can draw the character on a painter path and then convert that path to a curve. That is essentially what the text item does.

sarbh20ss
10th September 2013, 17:53
Hey sorry for late reply..got busy with some work at home.
After I followed your advice I did this:

QString myfname="FreeMono.ttf";
QRawFont *myfont=new QRawFont(myfname,324,QFont::PreferDefaultHinting);
//this creates a valid raw font
myfont->glyphIndexesForChars(mychars,numofchars,myglyphind exes,&numofglyphs);
//this gives me glyph indexes for characters I stored in array 'mychars'
QPainterPath mypath=myfont->pathForGlyph(myglyphindexes[0]);
//this path I am drawing on pixmap using painter.

With this I was able to draw the glyph outline for a character at given index.
I am setting this pixmap to Graphics Scene.

is this the right way?
Also how will I come to know that how that outline is being drawn?
I mean how many curves or arcs it contains?

Thanks in advance for help.

wysota
10th September 2013, 20:01
There is QPainterPath::elementAt() and QPainterPath::elementCount().

sarbh20ss
11th September 2013, 07:03
Hey I am able to get no. of elements and elements at given index.

mypath.elementCount();
//this return count as 49
mypath.elementAt(i);
//return the QPointF values for i from 0 to 48;

now is this the right way to use 'elementAt(index)' function?

I am storing these points in array. I am passing these points to path with lineTo() function and drawing this path on a pixmap. I do this for all 49 points. This draws outline of given character.
But when I change the no of iterations to even as less as 2 it still draws the outline correctly. Also when I change the function from 'lineTo( QPointF )' to 'moveTo( QPointF ) it still draws same outline correctly. I can not understand why this is happening?
Also how can I know that out of the 49 elements which are of 'moveTo' or 'lineTo' or any other type?

I have attached my code.

Please help.

wysota
11th September 2013, 07:39
But when I change the no of iterations to even as less as 2 it still draws the outline correctly.

Also when I change the function from 'lineTo( QPointF )' to 'moveTo( QPointF ) it still draws same outline correctly. I can not understand why this is happening?
You are always drawing the full path so that's hardly surprising:


p.drawPath(mypath);



Also how can I know that out of the 49 elements which are of 'moveTo' or 'lineTo' or any other type?
elementAt() returns a type where one of the attributes is the type of the element.

sarbh20ss
11th September 2013, 10:40
hey thanks a lot !
I was able to sort out which points are connected using which function.
many Thanks..

Added after 48 minutes:

hey I am able to connect each point and draw the path using either 'cubicTo' or 'lineTo' or 'moveTo'.

Now if i click on any point on scene how to check if that point is on the curve or not?
also how to make the path follow the dragging of mouse pointer on scene and modify it's shape?

Thanks in advance.