GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Sep 02, 2014 2:04 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: writing a custom font on drawing area, dynamically
PostPosted: Tue Feb 21, 2012 8:15 am 
Offline
Familiar Face

Joined: Tue Dec 20, 2011 3:27 pm
Posts: 14
Dear all,

hope you can help me still this time.

I have the following issue:

I have a gtk drawing area and need to draw a text with a custom TTF font i have in a file. This need to be done "runtime", since i get the font file path/name while running.

I already read about pango, that seems doesn't support a filename to load a font, like it need a separate step to load the font,
so i can't find a good solution for now.

Any help is really appreciated.

many thanks !
Regards,
angelo


Top
 Profile  
 
 Post subject: Re: writing a custom font on drawing area, dynamically
PostPosted: Tue Feb 21, 2012 11:21 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
Fonts are generally only used if they have been installed.
1) Simplest way. Assume the user has installed the file. Then instead of asking the user for a file at runtime (after all how many users really keep track of where their font files are on their filesystem) you just ask for the font name and other elements that make up the pango_font_description.
2) More involved way. Request the name of the font and proceed as above if it exists otherwise ask the user to specify a file which is then installed, then proceed as above.
3) Catch all solution. As above but if they don't have root access or don't want to install the font then use the cairo_user_font functions to populate the details of the font.
This latter solution is not as easy as it sounds. Ways of rendering to different backends are constantly changing making it a nightmare to try to maintain your code. You really are best relying on the font being (or being able to be) installed.


Top
 Profile  
 
 Post subject: Re: writing a custom font on drawing area, dynamically
PostPosted: Wed Feb 22, 2012 9:16 am 
Offline
Familiar Face

Joined: Tue Dec 20, 2011 3:27 pm
Posts: 14
Hi Paul,

many thanks for your as always kind reply.

I try to explain better my scenario.

I am writing an interpreter for a graphic scripting language.
At script loading, a font, included as TTF file with the script, need to be loaded. After the loading i draw some texts and images in the expose event.

The execution of the script is generally done by a user, not from root. I would need to avoid asking for root passwords, keeping someway a fast script loading.

Many Thanks,
regards
angelo


Top
 Profile  
 
 Post subject: Re: writing a custom font on drawing area, dynamically
PostPosted: Wed Feb 22, 2012 10:11 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
Installing the fonts is generally done with root access though I think you may be able to get around doing so (e.g. as described in http://vietunicode.sourceforge.net/howto/fontlinux.html) by having a font directory in the users home folder. Difficulties will be that the process will be platform dependent (e.g. linux user X etc) and may even vary with distro. Restarting the X font server may need root access anyway.
The only other option is the option 3 listed above in which caase you'll have to code the translating of all the ttf information into a format understandable by cairo etc.


Top
 Profile  
 
 Post subject: Re: writing a custom font on drawing area, dynamically
PostPosted: Fri Feb 24, 2012 12:17 am 
Offline
Familiar Face

Joined: Tue Dec 20, 2011 3:27 pm
Posts: 14
hi Paul,

many thanks, i did it, but fortunately, the way, with the help of freetype library is much easier:

Code:
// font creation on the fly, from file
FT_Library ftl;
FT_Error error = FT_Init_FreeType(&ftl);
FT_Face ftf;

error = FT_New_Face( ftl, font.c_str(), 0, &ftf );
cairo_font_face_t *t.cff = cairo_ft_font_face_create_for_ft_face(ftf, 0);

.....
// drawing engine
cairo_set_font_face (cr, (cairo_font_face_t *)t.cff);
cairo_set_font_size (cr, t.fontsize);
cairo_move_to   (cr, t.x, t.y);
cairo_show_text (cr, t.text.c_str());

...



so many thanks
regards,

angelo


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google Adsense [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group