2012-05-23 8 views
3

나는 코드가 이미지를 만들 수 CairoPango을 사용 있습니다파일에서 pango로 글꼴로드?

#include<stdio.h> 
#include<cairo.h> 
#include<pango/pangocairo.h> 

#define IMAGE_WIDTH 650 
#define IMAGE_HEIGHT 150 

#define TEXT "HELLO WORLD" 
#define FONT "MizuFontAlphabet Normal 40" 

/* 
* $ gcc $(pkg-config pangocairo cairo --cflags --libs) file.c 
*/ 


int main(int argc , char** argv) { 

    cairo_surface_t *surface; 
    cairo_t *cr; 

    PangoLayout *layout; 
    PangoFontDescription *desc; 
    PangoRectangle extents; 

    surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, IMAGE_WIDTH, IMAGE_HEIGHT); 
    cr  = cairo_create(surface); 

    cairo_rectangle(cr, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT); 
    cairo_set_source_rgb(cr, 0, 0, 0); 
    cairo_fill(cr); 
    cairo_stroke(cr); 

    /* the font is needed to be installed in fonts directory */ 
    layout = pango_cairo_create_layout(cr); 
    pango_layout_set_text(layout, TEXT, -1); 
    desc = pango_font_description_from_string(FONT); 
    pango_layout_set_font_description(layout, desc); 
    pango_font_description_free (desc); 

    pango_layout_get_pixel_extents(layout, NULL, &extents); 
    int x = (int) (IMAGE_WIDTH - extents.width)/2; 
    int y = (int) (IMAGE_HEIGHT - extents.height)/2; 

    cairo_set_source_rgb(cr, 0.33, 0.55, 0.88); 
    cairo_move_to(cr, x, y); 
    pango_cairo_show_layout(cr, layout); 

    g_object_unref(layout); 

    cairo_surface_write_to_png(surface, "image.png"); 

    cairo_destroy(cr); 
    cairo_surface_destroy(surface); 

    return(0); 
} 

내가 MizuFontAlphabet을 사용하는 폰트를, 내가 사용할 수있는 글꼴 디렉토리에 설치하는 데 필요한 있도록 없음 표준 글꼴입니다 Pango와 함께 :

desc = pango_font_description_from_string(FONT); 

글꼴을 설치하지 않은 경우 어떻게해야합니까?

답변

3

이 문제는 로컬 fontconfig 라이브러리 상태에 사용자 정의 글꼴을 추가하여 비교적 쉽게 해결할 수 있습니다

#include <fontconfig/fontconfig.h> 


std::string yourFontFilePath = "/home/testUser/bla.ttf"; 
const FcChar8 * file = (const FcChar8 *)yourFontFilePath.c_str(); 
FcBool fontAddStatus = FcConfigAppFontAddFile(FcConfigGetCurrent(), file); 

이제 pango와 렌더링 엔진은 단지 PangoFontDescription를 사용하여 사용자 정의 글꼴을 사용합니다.

+0

FcConfigAppFOntAddFile을 FcConfigAppFontAddFile로 변경하십시오. –

+1

이 대답은 pango가 fontconfig 백엔드를 사용한다고 가정합니다. 판고 수준에서 작동하는 교차 플랫폼 솔루션에 대해 알고 있습니까? –

+0

주어진 bla.ttf 글꼴을 참조하는 PangoFontDescription은 어떻게 만듭니 까? –