2013-03-15 13:11:33 +00:00
|
|
|
namespace phoenix {
|
|
|
|
|
|
|
|
string pFont::serif(unsigned size, string style) {
|
|
|
|
if(size == 0) size = 8;
|
|
|
|
if(style == "") style = "Normal";
|
|
|
|
return {"Serif, ", size, ", ", style};
|
|
|
|
}
|
|
|
|
|
|
|
|
string pFont::sans(unsigned size, string style) {
|
|
|
|
if(size == 0) size = 8;
|
|
|
|
if(style == "") style = "Normal";
|
|
|
|
return {"Sans, ", size, ", ", style};
|
|
|
|
}
|
|
|
|
|
|
|
|
string pFont::monospace(unsigned size, string style) {
|
|
|
|
if(size == 0) size = 8;
|
|
|
|
return {"Liberation Mono, ", size, ", ", style};
|
|
|
|
}
|
|
|
|
|
2013-05-05 09:21:30 +00:00
|
|
|
Size pFont::size(string font, string text) {
|
2013-05-02 11:25:45 +00:00
|
|
|
PangoFontDescription* description = create(font);
|
2013-03-15 13:11:33 +00:00
|
|
|
Size size = pFont::size(description, text);
|
|
|
|
free(description);
|
|
|
|
return size;
|
2011-03-22 12:56:49 +00:00
|
|
|
}
|
|
|
|
|
2013-05-05 09:21:30 +00:00
|
|
|
PangoFontDescription* pFont::create(string description) {
|
2011-09-05 03:48:23 +00:00
|
|
|
lstring part;
|
2013-03-15 13:11:33 +00:00
|
|
|
part.split<2>(",", description);
|
2013-05-02 11:25:45 +00:00
|
|
|
for(auto& item : part) item.trim(" ");
|
2011-09-05 03:48:23 +00:00
|
|
|
|
2011-10-16 09:44:48 +00:00
|
|
|
string family = "Sans";
|
|
|
|
unsigned size = 8u;
|
|
|
|
bool bold = false;
|
|
|
|
bool italic = false;
|
|
|
|
|
|
|
|
if(part[0] != "") family = part[0];
|
|
|
|
if(part.size() >= 2) size = decimal(part[1]);
|
2014-02-09 05:59:46 +00:00
|
|
|
if(part.size() >= 3) bold = (bool)part[2].find("Bold");
|
|
|
|
if(part.size() >= 3) italic = (bool)part[2].find("Italic");
|
2011-02-24 09:27:21 +00:00
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
PangoFontDescription* font = pango_font_description_new();
|
2011-09-05 03:48:23 +00:00
|
|
|
pango_font_description_set_family(font, family);
|
|
|
|
pango_font_description_set_size(font, size * PANGO_SCALE);
|
|
|
|
pango_font_description_set_weight(font, !bold ? PANGO_WEIGHT_NORMAL : PANGO_WEIGHT_BOLD);
|
|
|
|
pango_font_description_set_style(font, !italic ? PANGO_STYLE_NORMAL : PANGO_STYLE_OBLIQUE);
|
|
|
|
return font;
|
2011-02-24 09:27:21 +00:00
|
|
|
}
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
void pFont::free(PangoFontDescription* font) {
|
2011-09-05 03:48:23 +00:00
|
|
|
pango_font_description_free(font);
|
2011-02-24 09:27:21 +00:00
|
|
|
}
|
|
|
|
|
2013-05-05 09:21:30 +00:00
|
|
|
Size pFont::size(PangoFontDescription* font, string text) {
|
2013-05-02 11:25:45 +00:00
|
|
|
PangoContext* context = gdk_pango_context_get_for_screen(gdk_screen_get_default());
|
|
|
|
PangoLayout* layout = pango_layout_new(context);
|
2011-09-05 03:48:23 +00:00
|
|
|
pango_layout_set_font_description(layout, font);
|
|
|
|
pango_layout_set_text(layout, text, -1);
|
|
|
|
int width = 0, height = 0;
|
|
|
|
pango_layout_get_pixel_size(layout, &width, &height);
|
|
|
|
g_object_unref((gpointer)layout);
|
2013-03-15 13:11:33 +00:00
|
|
|
return {width, height};
|
2011-02-24 09:27:21 +00:00
|
|
|
}
|
|
|
|
|
2013-05-05 09:21:30 +00:00
|
|
|
void pFont::setFont(GtkWidget* widget, string font) {
|
2011-09-05 03:48:23 +00:00
|
|
|
auto gtkFont = pFont::create(font);
|
|
|
|
pFont::setFont(widget, (gpointer)gtkFont);
|
|
|
|
pFont::free(gtkFont);
|
2011-02-24 09:27:21 +00:00
|
|
|
}
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
void pFont::setFont(GtkWidget* widget, gpointer font) {
|
2013-03-15 13:11:33 +00:00
|
|
|
if(font == nullptr) return;
|
2011-09-05 03:48:23 +00:00
|
|
|
gtk_widget_modify_font(widget, (PangoFontDescription*)font);
|
|
|
|
if(GTK_IS_CONTAINER(widget)) {
|
|
|
|
gtk_container_foreach(GTK_CONTAINER(widget), (GtkCallback)pFont::setFont, font);
|
|
|
|
}
|
2011-02-24 09:27:21 +00:00
|
|
|
}
|
2013-03-15 13:11:33 +00:00
|
|
|
|
|
|
|
}
|