Fw: Re: Tutorial for using advanced text features in pango library



Hi Basil,

I just got into pango recently so I am no expert, I have
seen many posts answered by Behdad Esfahbod I guess he is
the Pango Guru.

I will explain what I have been using (which might be
totally wrong) but is working for me. 
First you need to decide if you want pango to use FT2 or
Cairo as a 
backend ( to extract the font tables do rendering etc).

If you use Cairo, in win32 it uses MS Uniscribe by default
and the anti-aliasing is 
not very good. Freetype has better anti-aliasing. 
Here are the steps I use (Gurus of pango please correct me
if I am wrong.)

You can use Pango to just extract the glyphs or to do your
rendering of paragraphs for you.


1) Initialize pango.

#include <pango/pangoft2.h>
#include <fontconfig.h>

PangoLayout *layout;
PangoFontMap* PFM ;
PangoContext *context;
PangoFontDescription *desc;


PFM = pango_ft2_font_map_new();
context =
pango_font_map_create_context(PANGO_FONT_MAP(PFM));
layout = pango_layout_new(context);

// This is just a starting since pango decides based on the
language/script
// which font to use from fontconfig info (more on that
later).

desc = pango_font_description_from_string("Arial, 20");
pango_layout_set_font_description(layout, desc);
pango_font_description_free(desc);

2) Send the String to Pango

pango_layout_set_text(layout, "Testing this is a paragraph"
, -1);
pango_layout_context_changed(layout);


3.1)  Now We render the string, this is similar to
Freetype where the bitmap
has 255 gray shades. Since the bitmap contains the redered
paragraph, You can copy 
entire bitmap to your target device.

// Setup the resolution

int width=300,height=300

// This sets the resolution of the device
pango_ft2_font_map_set_resolution(PANGO_FT2_FONT_MAP(PFM),width,
height);


// Setup the Bitmap to which Pango will render the string
to

bm = g_slice_new(FT_Bitmap);
bm->rows = height;
bm->width = width;
bm->pitch = width;
bm->num_grays = 256;
bm->pixel_mode = FT_PIXEL_MODE_GRAY;

bm->buffer = (unsigned char*)g_malloc (bm->pitch *
bm->rows);
memset(bm->buffer, 0x00, bm->pitch * bm->rows);


//for text wrapping set the width
//pango_layout_set_width(layout,width);

pango_ft2_render_layout(bm, layout, 0, 0);

//display the rendered string

    i=0;
    for(y=0;y<bm->rows;y++)
    {
       
for(x=0;x<bm->width;x++)
        {
            if
(bm->buffer[i++]==0) printf(" ");else printf("*");
        }
        printf("\n");
    }

Special things that you can do with the layout,

*) You can align the texts to left right justify etc.
*) When the string over flows you can put ... at the end
(ellipsize).
*) You can do Cursor position to XY and XY to CP similar to
uniscribe
    this is usefull for editors.

You check these apis for other layout related operations
http://library.gnome.org/devel/pango/stable/pango-Layout-Objects.html



3.2)  From the layout you can extract the glyphs.
Pango breaks the unicode strings into runs
which belong to the same script (I think). You dont need to
allocate bitmaps to extract glyphs.

PangoLayoutIter* pIter=0;
pIter=pango_layout_get_iter(layout);
do {
    PangoLayoutIter*
pIter=pango_layout_get_iter(layout);
    PangoLayoutLine*
pLine=pango_layout_iter_get_line(pIter); // no need for
unref
    int         
    plOffset=pLine->start_index;


    GSList* curR=pLine->runs;
    while ( curR ) 
    {
           
PangoLayoutRun* pRun=(PangoLayoutRun*)curR->data;
           
int         
   prOffset=pRun->item->offset;
            if
( pRun ) 
            {
           
    int i;

           
    const char
*Language=pango_language_to_string(pRun->item->analysis.language);
           
    PangoFont *
font=pRun->item->analysis.font;
           
    PangoFontDescription *
pf_desc=pango_font_describe(font);
           
    char *
dest=pango_font_description_to_filename(pf_desc);
           
    printf("Lang Used %s\n",Language);
           
    printf("Font Used %s\n",dest);


           
    for
(i=0;i<pRun->glyphs->num_glyphs;i++)
           
    {
           
        printf("GlyphID
%d\n",pRun->glyphs->glyphs[i].glyph);

           
        // glyphs[i] also has
other info like x_offset etc.
           
        // http://www.google.com/codesearch/p?hl=en#vHzQ_vvIlZw/inkscape/tags/RELEASE_0_42_0/src/libnrtype/TextWrapper.cpp&q=geometry.x_offset&sa=N&cd=12&ct=rc
           
        //
           
        //
pRun->glyphs->glyphs[i].geometry.x_offset
           
        //
pRun->glyphs->glyphs[i].geometry.y_offset
           
        //
glyphs->glyphs[i].geometry.width
           
    }
            }
           
curR=curR->next;
    }

} while ( pango_layout_iter_next_line(pIter) );
pango_layout_iter_free(pIter);


4) Pango uses Fontconfig for getting info about which fonts
to use for which scripts. To
Force fontconfig to use your fonts, put your fonts in a
different directory
and set this directory in /etc/fonts/fonts.conf 
<dir></dirtag. There probably
is a better way to do this, but this worked for me since I
have seperate fonts for 
individual scripts. 

<!-- Font directory list -->
<dir>/myproject/fonts</dir>
<dir>~/.fonts</dir>

Then run fc-cache -v, this should look up the fonts. You
can also set where font config
takes the fonts.conf from by setting the  env var.
export FONTCONFIG_FILE=/etc/fonts/fonts.conf

I did see somewhere that you can specify the font order in
pango.aliases file.

Regds
Ravi Kiran

> > From: Basil Saji <sajibasil gmail com>
> > Subject: Re: Tutorial for using advanced text features
> in pango library
> > To: "Ravi Kiran." <kiranps yahoo com>
> > Date: Wednesday, March 3, 2010, 12:43 AM
> > Hi Ravi,
> > 
> > 
> > 
> > Thank you for your quick response.
> > 
> > 
> > I want to use pango to extract
> > the glyphs or glyph string from an opentype
> font(*.otf) and
> > a truetype(*.ttf) font. I also was to use the opentype
> table
> > support library from pango to position the glyphs. I
> am a
> > beginner in pango but i have used freetype. The
> opentype
> > table support however is not present in freetype and
> they
> > have referred to pango for its support. How can i use
> pango
> > for glyph extraction and advanced positioning using
> opentype
> > tables. Kindly let me know of the basic apis required
> to
> > :
> > 
> > 
> > 
> > 1. Initialise pango library(in freetype we have 
> > 
> > 
> >      
> >     
> >      
> >     
> > FT_Init_FreeType)
> > .
> > 2. Open a font face in pango(in freetype we have
> > FT_New_Face).
> > 
> > 
> > 3. Setting the various metrics
> > and resolution of the font.
> > 4. extracting the glyphs or
> > glyph strings.
> > 
> > 
> > 5. extracting the advanced
> > positioning data.
> > 
> > 
> > 
> > Can a user specified font be
> > opened by pango or does pango open only system
> installed
> > fonts?
> > 
> > 
> > If yes, please let me know
> > how.
> > 
> > 
> > 
> > Thank you
> > 
> > 
> > 
> > Basil
> > 
> > 
> > On Tue, Mar 2, 2010 at 10:34 AM,
> > Ravi Kiran. <kiranps yahoo com>
> > wrote:
> > 
> > 
> > Hi Basil,
> > 
> > 
> > 
> >   I did not come across any tutorials for
> advance stuff.
> > For the basic
> > 
> > tutorials, here are the links
> > 
> > 
> > 
> > http://x11.gp2x.de/personal/google/
> > 
> > http://www.ibm.com/developerworks/library/l-u-pango2/
> > 
> > 
> > 
> > For advanced stuff, I use Google CodeSearch with the
> API I
> > am looking for
> > 
> > This gets me code snippets. Since advanced stuff is
> usually
> > strict in
> > 
> > the order in which you call things, I use the flow as
> it
> > is.
> > 
> > 
> > 
> > http://www.google.com/codesearch
> > 
> > 
> > 
> > You can also look at the samples that I have cobbled
> > together in the previous post. I used code snippets
> from
> > Inkscape for that .
> > 
> > 
> > 
> > 
> > 
> > Regds
> > 
> > Ravi Kiran
> > 
> > 
> > 
> > 
> > 
> > --- On Mon, 3/1/10, Basil Saji <sajibasil gmail com>
> > wrote:
> > 
> > 
> > 
> > > From: Basil Saji <sajibasil gmail com>
> > 
> > > Subject: Tutorial for using advanced text
> features in
> > pango library
> > 
> > > To: gtk-i18n-list gnome org
> > 
> > > Date: Monday, March 1, 2010, 8:13 AM
> > 
> > > Hi
> > 
> > >
> > 
> > > Kindly let me know if there is any tutorial for
> using
> > 
> > > advanced text features in the pango library.
> > 
> > >
> > 
> > > Basil
> > 
> > >
> > 
> > > --
> > 
> > > The end is only the beginning
> > 
> > >
> > 
> > >
> > 
> > > -----Inline Attachment Follows-----
> > 
> > >
> > 
> > > _______________________________________________
> > 
> > > gtk-i18n-list mailing list
> > 
> > > gtk-i18n-list gnome org
> > 
> > > http://mail.gnome.org/mailman/listinfo/gtk-i18n-list
> > 
> > >
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > -- 
> > The end is only the beginning
> > 
> > 
> 
> 
> 
> 





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]