2 * Claws Mail -- A GTK+ based, lightweight, and fast e-mail client
3 * Copyright(C) 2019 the Claws Mail Team
5 * litehtml callbacks related to text rendering
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write tothe Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "litehtml/litehtml.h"
24 #include "lh_widget.h"
26 litehtml::uint_ptr lh_widget::create_font( const litehtml::tchar_t* faceName, int size, int weight, litehtml::font_style italic, unsigned int decoration, litehtml::font_metrics* fm )
28 PangoFontDescription *desc =
29 pango_font_description_from_string(faceName);
31 pango_font_description_set_size(desc, size * PANGO_SCALE);
32 pango_font_description_set_weight(desc, (PangoWeight)weight);
34 if (italic == litehtml::fontStyleItalic)
35 pango_font_description_set_style(desc, PANGO_STYLE_ITALIC);
37 pango_font_description_set_style(desc, PANGO_STYLE_NORMAL);
40 PangoContext *context = gtk_widget_get_pango_context(m_drawing_area);
41 PangoFontMetrics *metrics = pango_context_get_metrics(
43 pango_context_get_language(context));
44 PangoLayout *x_layout;
47 x_layout = pango_layout_new(context);
48 pango_layout_set_font_description(x_layout, desc);
49 pango_layout_set_text(x_layout, "x", -1);
50 pango_layout_get_pixel_extents(x_layout, NULL, &rect);
52 fm->ascent = pango_font_metrics_get_ascent(metrics) / PANGO_SCALE;
53 fm->descent = pango_font_metrics_get_descent(metrics) / PANGO_SCALE;
54 fm->height = fm->ascent + fm->descent;
55 fm->x_height = rect.height;
57 g_object_unref(x_layout);
58 pango_font_metrics_unref(metrics);
61 pango_font *ret = new pango_font;
63 ret->strikethrough = (decoration & litehtml::font_decoration_linethrough) ? true : false;
64 ret->underline = (decoration & litehtml::font_decoration_underline) ? true : false;
66 return (litehtml::uint_ptr) ret;
69 void lh_widget::delete_font( litehtml::uint_ptr hFont )
71 pango_font *fnt = (pango_font *)hFont;
74 pango_font_description_free(fnt->font);
79 int lh_widget::text_width( const litehtml::tchar_t* text, litehtml::uint_ptr hFont )
81 pango_font *fnt = (pango_font *) hFont;
82 PangoContext *context = gtk_widget_get_pango_context(m_drawing_area);
83 PangoLayout *layout = pango_layout_new(context);
87 pango_layout_set_font_description(layout, fnt->font);
89 pango_layout_set_text(layout, text, -1);
90 pango_layout_get_pixel_extents(layout, NULL, &rect);
92 g_object_unref(layout);
97 void lh_widget::draw_text( litehtml::uint_ptr hdc, const litehtml::tchar_t* text, litehtml::uint_ptr hFont, litehtml::web_color color, const litehtml::position& pos )
99 pango_font *fnt = (pango_font *)hFont;
100 cairo_t *cr = (cairo_t *)hdc;
101 PangoLayout *layout = pango_cairo_create_layout(cr);
102 PangoContext *context = pango_layout_get_context(layout);
106 pango_layout_set_font_description(layout, fnt->font);
108 /* Set additional font attributes */
109 if (fnt->underline || fnt->strikethrough) {
110 PangoAttrList *attr_list = pango_attr_list_new();
114 ul = PANGO_UNDERLINE_SINGLE;
116 ul = PANGO_UNDERLINE_NONE;
118 pango_attr_list_insert(attr_list,
119 pango_attr_underline_new(ul));
120 pango_attr_list_insert(attr_list,
121 pango_attr_strikethrough_new(fnt->strikethrough));
123 pango_layout_set_attributes(layout, attr_list);
124 pango_attr_list_unref(attr_list);
128 /* Set actual text content */
129 pango_layout_set_text(layout, text, -1);
133 /* Draw the text where it's supposed to be */
135 set_color(cr, color);
136 cairo_move_to(cr, pos.left(), pos.top());
137 pango_cairo_show_layout(cr, layout);
140 g_object_unref(layout);