gpt4 book ai didi

css - 从 Pango.FontDescription 设置 GtkEntry 字体

转载 作者:技术小花猫 更新时间:2023-10-29 11:22:50 27 4
gpt4 key购买 nike

我有一个 GtkEntry,我希望它允许用户使用他们选择的字体(或系统默认字体)来设置样式。我最终得到了一个 Pango 描述字符串,如“Monospace 10”来描述字体。

我目前正在使用 override_font,它已被弃用以支持 CSS 样式。

我想至少尝试“正确”地做这件事,但现在从 Pango 字符串中获取 CSS 似乎是一个相当复杂和脆弱的工作流程。这是一个 example from Github :

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 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

在 CSS 是一个字符串之后,然后我可以创建一个 CSSProvider 并将其传递给样式上下文的 add_provider() (这是否最终会累积 CSS 提供程序,顺便问一下?)。

要将字体恢复到系统中似乎需要做很多工作,它可能会直接恢复到 Pango 中!

这真的是正确的做法吗?

最佳答案

使用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

关于css - 从 Pango.FontDescription 设置 GtkEntry 字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41257101/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com