From 3842b45ed589842e3aa94a3706ceebdd79442cf1 Mon Sep 17 00:00:00 2001 From: arif Date: Fri, 31 Oct 2008 21:22:10 +0000 Subject: [PATCH] *** empty log message *** --- lib/glcomp/glpangofont.c | 148 +++++++++++++++++++++++++++++++++++++++ lib/glcomp/glpangofont.h | 11 +++ 2 files changed, 159 insertions(+) create mode 100644 lib/glcomp/glpangofont.c create mode 100644 lib/glcomp/glpangofont.h diff --git a/lib/glcomp/glpangofont.c b/lib/glcomp/glpangofont.c new file mode 100644 index 000000000..d4cd7abc8 --- /dev/null +++ b/lib/glcomp/glpangofont.c @@ -0,0 +1,148 @@ +#include "glpangofont.h" +#define DEFAULT_FONT_FAMILY "Arial" +#define DEFAULT_FONT_SIZE 32 +#define ANTIALIAS + + +static int file_exists(const char * filename); + +static int file_exists(const char * filename) +{ + FILE * file; + if ( file = fopen(filename, "r")) + { + fclose(file); + return 1; + } + return 0; +} + +PangoLayout * get_pango_layout (cairo_t *cr, + char *markup_text, + char* fontdescription, + double *width, + double *height + ) +{ + PangoFontDescription *desc; + PangoFontMap* fontmap; + PangoContext *context; + PangoLayout *layout; + int pango_width, pango_height; + char *text; + PangoAttrList *attr_list; + cairo_font_options_t* options; + fontmap = pango_cairo_font_map_get_default(); + context = pango_cairo_font_map_create_context(PANGO_CAIRO_FONT_MAP(fontmap)); + options=cairo_font_options_create(); +#ifdef ANTIALIAS +// cairo_font_options_set_antialias(options,CAIRO_ANTIALIAS_GRAY); +#endif + cairo_font_options_set_hint_style(options,CAIRO_HINT_STYLE_FULL); + cairo_font_options_set_hint_metrics(options,CAIRO_HINT_METRICS_ON); + cairo_font_options_set_subpixel_order(options,CAIRO_SUBPIXEL_ORDER_BGR); +// pango_cairo_context_set_font_options(context, options); + + desc = pango_font_description_from_string(fontdescription); +// pango_font_description_set_family(desc, "CENTAUR.TTF"); + //pango_font_description_set_size (desc, DEFAULT_FONT_SIZE * PANGO_SCALE/2.0); +// pango_font_description_set_style (desc,PANGO_STYLE_ITALIC); + + if (! pango_parse_markup (markup_text, -1, '\0', &attr_list, &text, NULL, NULL)) + return (PangoLayout*)0; + layout = pango_layout_new (context); + pango_layout_set_text (layout, text, -1); + pango_layout_set_font_description (layout, desc); + pango_layout_set_attributes (layout, attr_list); + pango_font_description_free (desc); + pango_layout_set_alignment (layout,PANGO_ALIGN_CENTER); + + if (width || height) + pango_layout_get_size (layout, &pango_width, &pango_height); + + if (width) + *width = (double) pango_width / PANGO_SCALE; + + if (height) + *height = (double) pango_height / PANGO_SCALE; + + return layout; +} + +cairo_status_t +writer (void *closure, const unsigned char *data, unsigned int length) +{ + int a; + if (length == fwrite(data, 1, length, (FILE *)closure)) + { + return CAIRO_STATUS_SUCCESS; + + } + + return CAIRO_STATUS_WRITE_ERROR; +} + + + + + + +int create_font_file(char* fontdescription,char* fontfile,float gw,float gh) +{ + + char buf[]=" "; + int ncolumns=16; + int counter=0; + int X=0; + int Y=0; + cairo_t *cr; + cairo_surface_t *surface; + PangoLayout *layout; + double width, height; + FILE* output_file; + char* c; + int return_value=-1; + + if(file_exists(fontfile)) //checking if font file has already been created + return 0; + //create the right size canvas for character set + surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,(float)ncolumns*gw,gh*(float)ncolumns); + cr = cairo_create(surface); + //draw a rectangle with same size of canvas + cairo_rectangle(cr, 0, 0,(float)ncolumns*gw,gh*(float)ncolumns); + //fill rectangle with black + cairo_set_source_rgb(cr, 0, 0, 0); + cairo_fill(cr); + //set pen color to white + cairo_set_source_rgb(cr, 1, 1, 1); + + for (c=0;c < 256 ; c++) + { + counter++; + if ((c !=38) && (c !=60)&& (c !=128) && (c < 129)) + buf[0]=c; + else + buf[0]=' '; + cairo_move_to(cr,X,Y); + layout = get_pango_layout (cr,buf,fontdescription, &width, &height); + pango_cairo_show_layout(cr, layout); + X=X+gw; + if (counter==ncolumns) + { + X=0;Y=Y+gh; + counter=0; + } + } + output_file = fopen(fontfile, "wb+"); + if(output_file) + { + cairo_surface_write_to_png_stream(surface, writer, output_file); + return_value=0; + } + fclose(output_file); + g_object_unref (layout); + cairo_destroy(cr); + cairo_surface_destroy(surface); + return return_value; +} + diff --git a/lib/glcomp/glpangofont.h b/lib/glcomp/glpangofont.h new file mode 100644 index 000000000..3b1fd482a --- /dev/null +++ b/lib/glcomp/glpangofont.h @@ -0,0 +1,11 @@ + +#ifndef GLPANGOFONT_H +#define GLPANGOFONT_H +#include +#include +#include +//creates a font file with given name and font description +//returns non-zero if fails +int create_font_file(char* fontdescription,char* fontfile,float gw,float gh); +#endif + -- 2.40.0