2016-12-21 10 views
5

사용자가 글꼴 (또는 시스템 기본값)을 사용하여 스타일을 지정할 수있게하려는 GtkEntry가 있습니다. 글꼴을 설명하는 "Monospace 10"과 같은 Pango 설명 문자열로 끝납니다.Pango.FontDescription에서 GtkEntry 글꼴 설정

저는 현재 override_font을 사용하고 있습니다.이 스타일은 CSS 스타일링에 사용되지 않습니다.

적어도 "올바르게"하려고 시도하고 싶지만, Pango 문자열에서 CSS를 가져 오는 것은 꽤 복잡하고 연약한 워크 플로처럼 보입니다. 여기 example from Github의 다음 CSS는 문자열에

def _get_editor_font_css(): 
    """Return CSS for custom editor font.""" 
    font_desc = Pango.FontDescription("monospace") 
    if (gaupol.conf.editor.custom_font and 
     gaupol.conf.editor.use_custom_font): 
     font_desc = Pango.FontDescription(gaupol.conf.editor.custom_font) 
    # They fucking broke theming again with GTK+ 3.22. 
    unit = "pt" if Gtk.check_version(3, 22, 0) is None else "px" 
    css = """ 
    .gaupol-custom-font {{ 
     font-family: {family},monospace; 
     font-size: {size}{unit}; 
     font-weight: {weight}; 
    }}""".format(
     family=font_desc.get_family().split(",")[0], 
     size=int(round(font_desc.get_size()/Pango.SCALE)), 
     unit=unit, 
     weight=int(font_desc.get_weight())) 
    css = css.replace("font-size: 0{unit};".format(unit=unit), "") 
    css = css.replace("font-weight: 0;", "") 
    css = "\n".join(filter(lambda x: x.strip(), css.splitlines())) 
    return css 

후, 나는이 CSSProvider을 만들 수 있고, 스타일 문맥의 add_provider()에 그 전달 (그런데,이를 업 축적 않습니다 CSS 제공자?).

이 모든 것은 글꼴을 시스템으로 다시 가져 오는 작업과 비슷합니다.이 글꼴은 아마도 Pango로 바로 돌아갈 것입니다.

정말이 방법이 적합할까요?

답변

0

PangoContext을 사용하십시오.

#include <gtkmm.h> 

int main(int argc, char* argv[]) 
{ 
    auto GtkApp = Gtk::Application::create(); 

    Gtk::Window window; 

    Gtk::Label label; 
    label.set_label("asdasdfdfg dfgsdfg "); 
    auto context = label.get_pango_context(); 
    auto fontDescription = context->get_font_description(); 
    fontDescription.set_family("Monospace"); 
    fontDescription.set_absolute_size(10*Pango::SCALE); 
    context->set_font_description(fontDescription); 

    Gtk::Label label2; 
    label2.set_label("xcv"); 

    Gtk::VBox box; 
    box.pack_start(label); 
    box.pack_start(label2); 
    window.add(box); 
    window.show_all(); 
    GtkApp->run(window); 
    return 0; 
} 

결과 :

Resulting window