2016-12-21 100 views
5

Kullanıcıların yazı tipi (veya sistem varsayılanı) seçimiyle stil vermelerine izin vermek istediğim bir GtkEntry var. Yazı tipini tanımlamak için "Monospace 10" gibi bir Pango açıklama dizesiyle bitiriyorum.Pango.FontDescription öğesinden GtkEntry yazı tipini ayarlama

Şu anda override_font kullanıyorum, CSS styling lehine önerilmemektedir.

En azından "doğru" yapmayı denemek isterim, ancak şimdi CSS'yi Pango dizesinden almak için oldukça kıvrımlı ve kırılgan bir iş akışı gibi görünüyor. İşte bir example from Github var: CSS bir dizede olduğu

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 

sonra sonra bir CSSProvider oluşturabilir ve stil bağlam en add_provider() o geçmesine (bu arada, bu uca biriken mu CSS sağlayıcıları?).

Bu, fontun tekrar Pango'ya geri döndüğü sisteme geri getirilmesi için çok iş gibi görünüyor!

Bu gerçekten bunun için doğru yol mu?

cevap

0

PangoContext'u kullanın.

#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; 
} 

Sonuç:

Resulting window