|
Post by bigbass on Aug 10, 2012 17:45:19 GMT 1
It would be easy if all the fonts had the same pixel sizes for each character but that is not the case every font has its own quirky pixel count for each character so it would involve at the very least
reading an ASC$ of every character saving this output and having a table to convert each one then another routine to add up all the characters +whitespace which gets more complex with newlines and markup font changes
then this would need to be repeated for every font and DPI size
ouch!
The way I did it by making an image of the text then getting the pixel size automatically just seems so much easier maybe doing it with SVG or a built in Pango function would make it easier
but making a "function" would be a very involved project
Joe
|
|
|
Post by bigbass on Aug 11, 2012 16:41:03 GMT 1
Well I haven't given up but I have to pass the the code to you guys that code gtk this should get you some results P.S if you can make a simple example so It could be added to all HUG code easily Good luck Joe www.gtk.org/api/2.6/gtk/gtk-question-index.html1.12. How do I draw text ? 1.13. How do I measure the size of a piece of text ? To obtain the size of a piece of text, use a Pango layout and pango_layout_get_pixel_size(), using code like the following: layout = gtk_widget_create_pango_layout (widget, text); fontdesc = pango_font_description_from_string ("Luxi Mono 12"); pango_layout_set_font_description (layout, fontdesc); pango_layout_get_pixel_size (layout, &width, &height); pango_font_description_free (fontdesc); g_object_unref (layout); ---------------------------------------------------------- I also found this to get the fonts this works I tested it www.lemoda.net/pango/list-fonts/index.htmlThe compile options in the post doesn't work but this does gcc `pkg-config --cflags --libs gtk+-2.0` -o "pango-list-typefaces" "pango-list-typefaces.c"
|
|
|
Post by bigbass on Aug 11, 2012 20:27:34 GMT 1
use pango and cairo to make labels pango-cairo1.cthe result is out.png in the same directory compile with gcc `pkg-config --cflags --libs gtk+-2.0` -o "pango-cairo1" "pango-cairo1.c"
#include <stdio.h>#include <math.h> #include <pango/pangocairo.h>
void rendertext(cairo_t *cr);
int main(int argc, char* argv[]) { cairo_t *cr; // cairo instance cairo_status_t status; // variable to hold the various states of the cairo lib cairo_surface_t *surface; // the cairo surface to render to
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 150, 60); // cairo_image_surface_create(pixelformat, width, height); cr = cairo_create(surface); // tell the cairo instance 'cr' to output to 'surface' cairo_set_source_rgb(cr, 1.0, 1.0, 1.0); // sets the drawing colour to white //cairo_paint_with_alpha (cr, 0.5); //cairo_paint(cr); // fill the surface with the active colour (if you don't do this, you will // be given a surface with a transparent background to draw on)
rendertext(cr); // do pango stuff here cairo_destroy(cr); // free the cairo instance status = cairo_surface_write_to_png(surface, "out.png"); // output the contents of 'surface' to the file "out.png" cairo_surface_destroy(surface); // free the surface if (status != CAIRO_STATUS_SUCCESS) { // if 'status' was not set to indicate a successful operation when saving as a png, error printf("Could not save to png, \"out.png\"\n"); return 1; }
return 0; }
void rendertext(cairo_t *cr) { PangoLayout *layout; // layout for a paragraph of text PangoFontDescription *desc; // this structure stores a description of the style of font you'd most like cairo_translate(cr, 10, 20); // set the origin of cairo instance 'cr' to (10,20) (i.e. this is where // drawing will start from). layout = pango_cairo_create_layout(cr); // init pango layout ready for use pango_layout_set_text(layout, "Hello BaCon!", -1); // sets the text to be associated with the layout (final arg is length, -1 // to calculate automatically when passing a nul-terminated string) desc = pango_font_description_from_string("Sans Bold 12"); // specify the font that would be ideal for your particular use pango_layout_set_font_description(layout, desc); // assign the previous font description to the layout pango_font_description_free(desc); // free the description
cairo_set_source_rgb(cr, 0.0, 0.0, 1.0); // set the colour to blue pango_cairo_update_layout(cr, layout); // if the target surface or transformation properties of the cairo instance // have changed, update the pango layout to reflect this pango_cairo_show_layout(cr, layout); // draw the pango layout onto the cairo surface
g_object_unref(layout); // free the layout }
|
|
|
Post by SunBurnt on Aug 12, 2012 20:10:21 GMT 1
Joe; Did I understand your post above that you were trying to get the system fonts?
If so...
fc-list
I don`t know, but can`t you automate the image creation into the rest of the code? So then the function could be made.
|
|
|
Post by vovchik on Mar 14, 2013 16:06:11 GMT 1
Dear Terry, I now have a function that returns width and height in pixels for marked up labels, as you originally requested. That took a long time Sorry. The source and a Lucid 32-bit stripped and UPX'ed binary, together with the source, are in the next post. Have fun. With kind regards, vovchik
|
|
|
Post by vovchik on Mar 14, 2013 16:07:26 GMT 1
Source and binary... Attachments:
|
|
|
Post by bigbass on Mar 14, 2013 17:14:42 GMT 1
Hello vovchik ! I knew you could do it I also had tried to do this several ways and on the surface it seems easy but taking a closer look it isnt easy it is rather invovled Thank you for showing how to call the the GTK pango part directly and get the pixel info another nice widget to the collection for bacon and HUG Joe
|
|
|
Post by alexfish on Mar 14, 2013 17:27:28 GMT 1
Another Nice bit of coding Vovchik , worth the wait . Thought about giving Mark a different angle of approach. can this , INCLUDE "hug.bac"
IMPORT "gtk_label_set_angle(long,double)" FROM "libgtk-x11-2.0.so" TYPE void
Win = WINDOW("Mark - angles",300,300) mark1=MARK("45 Degrees",100,100) ' angle the label to 45 degrees gtk_label_set_angle(mark1,45) ATTACH(Win,mark1,0,0) DISPLAY BR Alex
|
|
|
Post by vovchik on Mar 14, 2013 18:18:48 GMT 1
Dear Joe,
It was you who posted the essential code! Thanks. I like it that we can, to some degree, now make the size of some widgets intelligently dynamic.
Cheers, vovchik
|
|
|
Post by vovchik on Mar 14, 2013 18:23:08 GMT 1
Dear Alex,
Also nice and useful. I once found a QT4 programe that did a page full of labels at different angles. I think we can now duplicate it - and it will be tiny.
With kind regards, vovchik
PS. I wonder whether I shouldn't add an angle argument to that pango function. What do you think?
|
|
|
Post by alexfish on Mar 14, 2013 19:37:31 GMT 1
Dear Alex, Also nice and useful. I once found a QT4 programe that did a page full of labels at different angles. I think we can now duplicate it - and it will be tiny. With kind regards, vovchik PS. I wonder whether I shouldn't add an angle argument to that pango function. What do you think? Hi vovchik , not sure about that one , will it depend on pango_cairo , the only context I can find as pango direct is to rotate 90 dgr increments. But since can now get the layout size in pixels , a bit of math's , a bit of label resize could do the trick I Know U are a master at this then use the gtk_label_set_angle(mark1,45) after gtk_label_set_justify(logo, 2) like gtk_label_set_justify(logo, 2) gtk_label_set_angle(mark1,45)
BR Alex
|
|
|
Post by vovchik on Mar 14, 2013 23:19:03 GMT 1
Dear Alex, Here it is..the screenshot with angle and autcalulation of the bounding box. Just took a few calculations. Source and binary in next post With kind regards, vovchik
|
|
|
Post by vovchik on Mar 14, 2013 23:21:33 GMT 1
Autoscale label with rotation source and binary here.... Attachments:
|
|
|
Post by alexfish on Mar 15, 2013 1:38:37 GMT 1
Hi Vovchik.
What can I say ;D
If there was ever a Label worth a Mark ,this has got to be it.
Though not sure about "langle" , can only laugh at that one.
Worth documenting , Joe .
BR
Alex
|
|
|
Post by vovchik on Mar 15, 2013 11:22:23 GMT 1
Dear all, In my rush to post the rotating markup function, I neglected to check the bounding box values for angles greater than 270. I have now fixed that so please use this version. With kind regards, vovchik Attachments:
|
|