RE: What's the meaning of the height of layout



Hi Behdad Esfahbod :

    Sorry, its the first time I use the mail-list to discuss question with other people, seems I am not familiar with it and make some trouble. Thanks sincerely again for your patience and attention.

    I have described my problem and give an example code as following :

   I meet a problem with pango_layout_get_pixel_size() and pango_layout_line_get_pixel_extents(). It seems that the PangoLayout's height retrived from the previous function is not equal to PangoLine's height multiplied by the total number of lines but a little larger than it.
 
   The issue is if I get the width and height of the text through function : pango_layout_get_pixel_size(), then create a surface according to the size retrived, then paint the text line by line onto the surface, there will be a blank at the end of the surface which was not filled by any text. But if I paint the text directly through function : pang_cairo_show_layout(), the blank disappeared.
  
   In my project, I would show a large amount of text, which indicates I shouldn't accomplish painting the text in one action because it is time-consuming.
 
   Following are the example code showing the problem, and I also attached two pictures to help you understand it. Could any one give some help?
 
 
   Thanks a lot
 
 
 
#include <stdio.h>
#include <cairo.h>
#include <pango/pangocairo.h>
#define TEST_PANGO_CAIRO_SHOW_LAYOUT_LINE 0

int main(int argc, char* argv[]){
    cairo_surface_t* surface = NULL;
    cairo_t* cr;
 
    PangoLayout* layout = NULL;
    PangoFontDescription* font_description = NULL;
 
    font_description = pango_font_description_from_string("Sans 27");
    layout = pango_cairo_create_layout(cr);
    pango_layout_set_font_description(layout, font_description);
    char* pText = (char*)"抗战时,沦陷后的上海,在国军地下工作负责人钟国强的策划下,接二连三的日军官被刺,火车被炸,日军被击,闹得驻沪日军司令佐藤大佐,手 忙脚乱,严令情报组陈组长,三天之内,将抗日份子统统抓来。\n\n情报组黄主任奉命出勤,率领大批军警,驰抵华声戏院,将在该院演出的一班剧团男女演 员,集体抓去,只放走女主角李丹妮一人。\n\n丹妮虽获漏网,但不明其故,返家后,才发觉丈夫陈希强已参加伪组织作汉奸,不由伤痛欲绝,但犹图婉转规劝 希强,不料希强反恼羞成怒,两人大起冲突,女佣阿香闻之,立迫希强枪杀丹妮。原来阿香实为日寇潜伏希强家之情报人员,希强不敢抗命,不料丹妮己先期遁走, 两人急忙追出。 \n\n丹妮逃经后巷,遇一日本哨兵,丹妮人急智生,指说后边有强盗追来,日兵不察,将希强与阿香截住,丹妮乘机脱身。\n\n丹妮奔出后巷,见一辆汽车 驶来,急截住跳入车厢,不料司机竟是情报组的黄主任,当场大惊昏倒。\n\n丹妮醒来,已在酒店,黄主任故意挑起丹妮心事。原来丹妮在下嫁陈希强前,曾有 爱人,即钟国强,两人且有一子,现已五岁,后因国强赴后方参力抗日工作,一去无音讯,传闻已死,丹妮心灰意冷而下嫁希强。不料此时黄就告以国强未死,丹妮 大喜。\n\n丹妮遵黄之嘱,至留真照相馆找寻国强,职员老周见丹妮所说暗语不差,引往太平洋造船访国强,谁知黄已率日军跟踪而至,希图将我军地下人员, 一网打尽,尚幸国强应变有方,卒获安全撤退。\n\n但众人已视丹妮如祸水,国强也不便挽留,只得将丹妮送往苏州乡下母亲家中。不料阿香与陈希强已在恭 候,丹妮成了自投罗网。\n\n阿香将丹妮押回上海,佐藤大佐欲利用丹妮与国强之关系,刺探国军情报,谁知丹妮不允合作,佐藤无计可施,将丹妮母逮捕,迫 丹妮就范,不料丹妮母大义凛然,竟跳楼自尽。佐藤遂又命黄主任将丹妮爱子逮来,丹妮见爱子落入虎口,惟有俯首就范。\n\n佐藤又恐丹妮无法取信国强,决 居牺牲陈希强,命阿香协助丹妮,将希强击毙。果然国强阅报获悉丹妮枪击希强事,即千方百计设法与丹妮取得联络,但国强似已知丹妮事,反供给假情报,嘱丹妮 做反间谍,借刀杀人。佐藤不察,竟然中计,将黄主任拉出枪毙。 \n\n这时国强又接奉上级命令,刺杀日军南进指挥官山下板郎,及爆炸日军准备南进的军火库。谁知山下狡猾,地下人员屡击不中,国强亲自出马,亦师老无 功。最后还是陈组长设法,将丹妮献于山下。原来陈组长也属国军地下人员,潜伏日军情报部作反间谍。 \n\n丹妮击毙山下后,又随陈至军火庳埋放炸药,不料佐藤闻讯赶来,与陈发生纠缠,丹妮乘机脱身。国强率众驾小轮赶来接应,只见火光烛天,军火库耽开始 爆炸,丹妮奔逃上船,日军追来,国强等开枪阻截,小轮向江中飞驶而去。";
    pango_layout_set_text(layout, pText, -1);
    pango_layout_set_width(layout, PANGO_SCALE * 440);
    pango_layout_set_wrap(layout, PANGO_WRAP_CHAR);
    pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
    PangoLayoutLine* pLine = NULL;
    int LineCount = pango_layout_get_line_count(layout);
    printf("LineCount  = %d\n", LineCount);
    int width = 0;
    int height = 0;
    pango_layout_get_pixel_size(layout, &width, &height);
    printf("layout height : %d\n", height);
 
    pLine = pango_layout_get_line_readonly(layout, 0);
    PangoRectangle LR;
    pango_layout_line_get_pixel_extents(pLine, NULL, &LR);
    printf("line heigth : %d\n", LR.height);
    surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
    cr = cairo_create(surface);
    cairo_set_source_rgb(cr, 0.0, 0.0, 1.0);
    cairo_move_to(cr, 0.0, 0.0);
#if TEST_PANGO_CAIRO_SHOW_LAYOUT_LINE
    printf("Show Layout line by line\n");   
 
    cairo_move_to(cr, 0.0, LR.height);
    PangoLayoutIter* pIter = pango_layout_get_iter(layout);
    int i = 0;
    for(i = 0; i < LineCount; ++i) {
       pLine = pango_layout_iter_get_line_readonly(pIter);
       pango_layout_iter_next_line(pIter);
       pango_cairo_show_layout_line(cr, pLine);
       cairo_rel_move_to(cr, 0.0, LR.height);
    }
#else
    printf("show layout directly\n");
    pango_cairo_show_layout(cr, layout);
#endif
    cairo_move_to(cr, 0.0, 0.0);
    cairo_rectangle(cr, 0.0, 0.0, width, height);
    cairo_stroke(cr);
   
    cairo_destroy(cr);
    cairo_surface_write_to_png(surface, "hello.png");
    cairo_surface_destroy(surface);
   
    g_object_unref(layout);
    pango_font_description_free(font_description);
    return 0;
}

Hotmail: Free, trusted and rich email service. Get it now.


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