Use Pango to render text in Litehtml plugin
[claws.git] / src / plugins / litehtml_viewer / lh_widget_text.cpp
1 /*
2  * Claws Mail -- A GTK+ based, lightweight, and fast e-mail client
3  * Copyright(C) 2019 the Claws Mail Team
4  *
5  * litehtml callbacks related to text rendering
6  *
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.
18  */
19
20 #include <glib.h>
21
22 #include "litehtml/litehtml.h"
23
24 #include "lh_widget.h"
25
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 )
27 {
28         PangoFontDescription *desc =
29                 pango_font_description_from_string(faceName);
30
31         pango_font_description_set_size(desc, size * PANGO_SCALE);
32         pango_font_description_set_weight(desc, (PangoWeight)weight);
33
34         if (italic == litehtml::fontStyleItalic)
35                 pango_font_description_set_style(desc, PANGO_STYLE_ITALIC);
36         else
37                 pango_font_description_set_style(desc, PANGO_STYLE_NORMAL);
38
39         if(fm != NULL) {
40                 PangoContext *context = gtk_widget_get_pango_context(m_drawing_area);
41                 PangoFontMetrics *metrics = pango_context_get_metrics(
42                                 context, desc,
43                                 pango_context_get_language(context));
44                 PangoLayout *x_layout;
45                 PangoRectangle rect;
46
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);
51
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;
56
57                 g_object_unref(x_layout);
58                 pango_font_metrics_unref(metrics);
59         }
60
61         pango_font *ret = new pango_font;
62         ret->font = desc;
63         ret->strikethrough = (decoration & litehtml::font_decoration_linethrough) ? true : false;
64         ret->underline = (decoration & litehtml::font_decoration_underline) ? true : false;
65
66         return (litehtml::uint_ptr) ret;
67 }
68
69 void lh_widget::delete_font( litehtml::uint_ptr hFont )
70 {
71         pango_font *fnt = (pango_font *)hFont;
72
73         if (fnt != NULL) {
74                 pango_font_description_free(fnt->font);
75                 delete fnt;
76         }
77 }
78
79 int lh_widget::text_width( const litehtml::tchar_t* text, litehtml::uint_ptr hFont )
80 {
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);
84         PangoRectangle rect;
85
86         if (fnt)
87                 pango_layout_set_font_description(layout, fnt->font);
88
89         pango_layout_set_text(layout, text, -1);
90         pango_layout_get_pixel_extents(layout, NULL, &rect);
91
92         g_object_unref(layout);
93
94         return rect.width;
95 }
96
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 )
98 {
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);
103
104         if (fnt != NULL) {
105                 /* Set font */
106                 pango_layout_set_font_description(layout, fnt->font);
107
108                 /* Set additional font attributes */
109                 if (fnt->underline || fnt->strikethrough) {
110                         PangoAttrList *attr_list = pango_attr_list_new();
111                         PangoUnderline ul;
112
113                         if (fnt->underline )
114                                 ul = PANGO_UNDERLINE_SINGLE;
115                         else
116                                 ul = PANGO_UNDERLINE_NONE;
117
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));
122
123                         pango_layout_set_attributes(layout, attr_list);
124                         pango_attr_list_unref(attr_list);
125                 }
126         }
127
128         /* Set actual text content */
129         pango_layout_set_text(layout, text, -1);
130
131         cairo_save(cr);
132
133         /* Draw the text where it's supposed to be */
134         apply_clip(cr);
135         set_color(cr, color);
136         cairo_move_to(cr, pos.left(), pos.top());
137         pango_cairo_show_layout(cr, layout);
138
139         /* Cleanup */
140         g_object_unref(layout);
141         cairo_restore(cr);
142 }