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.
24 #include "litehtml/litehtml.h"
26 #include "lh_widget.h"
28 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
)
30 PangoFontDescription
*desc
=
31 pango_font_description_from_string(faceName
);
33 pango_font_description_set_size(desc
, size
* PANGO_SCALE
);
34 pango_font_description_set_weight(desc
, (PangoWeight
)weight
);
36 if (italic
== litehtml::fontStyleItalic
)
37 pango_font_description_set_style(desc
, PANGO_STYLE_ITALIC
);
39 pango_font_description_set_style(desc
, PANGO_STYLE_NORMAL
);
42 PangoContext
*context
= gtk_widget_get_pango_context(m_drawing_area
);
43 PangoFontMetrics
*metrics
= pango_context_get_metrics(
45 pango_context_get_language(context
));
46 PangoLayout
*x_layout
;
49 x_layout
= pango_layout_new(context
);
50 pango_layout_set_font_description(x_layout
, desc
);
51 pango_layout_set_text(x_layout
, "x", -1);
52 pango_layout_get_pixel_extents(x_layout
, NULL
, &rect
);
54 fm
->ascent
= pango_font_metrics_get_ascent(metrics
) / PANGO_SCALE
;
55 fm
->descent
= pango_font_metrics_get_descent(metrics
) / PANGO_SCALE
;
56 fm
->height
= fm
->ascent
+ fm
->descent
;
57 fm
->x_height
= rect
.height
;
59 g_object_unref(x_layout
);
60 pango_font_metrics_unref(metrics
);
63 pango_font
*ret
= new pango_font
;
65 ret
->strikethrough
= (decoration
& litehtml::font_decoration_linethrough
) ? true : false;
66 ret
->underline
= (decoration
& litehtml::font_decoration_underline
) ? true : false;
68 return (litehtml::uint_ptr
) ret
;
71 void lh_widget::delete_font( litehtml::uint_ptr hFont
)
73 pango_font
*fnt
= (pango_font
*)hFont
;
76 pango_font_description_free(fnt
->font
);
81 int lh_widget::text_width( const litehtml::tchar_t
* text
, litehtml::uint_ptr hFont
)
83 pango_font
*fnt
= (pango_font
*) hFont
;
84 PangoContext
*context
= gtk_widget_get_pango_context(m_drawing_area
);
85 PangoLayout
*layout
= pango_layout_new(context
);
89 pango_layout_set_font_description(layout
, fnt
->font
);
91 pango_layout_set_text(layout
, text
, -1);
92 pango_layout_get_pixel_extents(layout
, NULL
, &rect
);
94 g_object_unref(layout
);
99 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
)
101 pango_font
*fnt
= (pango_font
*)hFont
;
102 cairo_t
*cr
= (cairo_t
*)hdc
;
103 PangoLayout
*layout
= pango_cairo_create_layout(cr
);
104 PangoContext
*context
= pango_layout_get_context(layout
);
105 GdkScreen
* screen
= gdk_screen_get_default();
106 double dpi
= gdk_screen_get_resolution(screen
);
108 pango_cairo_context_set_resolution(context
, dpi
);
112 pango_layout_set_font_description(layout
, fnt
->font
);
114 /* Set additional font attributes */
115 if (fnt
->underline
|| fnt
->strikethrough
) {
116 PangoAttrList
*attr_list
= pango_attr_list_new();
120 ul
= PANGO_UNDERLINE_SINGLE
;
122 ul
= PANGO_UNDERLINE_NONE
;
124 pango_attr_list_insert(attr_list
,
125 pango_attr_underline_new(ul
));
126 pango_attr_list_insert(attr_list
,
127 pango_attr_strikethrough_new(fnt
->strikethrough
));
129 pango_layout_set_attributes(layout
, attr_list
);
130 pango_attr_list_unref(attr_list
);
134 /* Set actual text content */
135 pango_layout_set_text(layout
, text
, -1);
139 /* Draw the text where it's supposed to be */
141 set_color(cr
, color
);
142 cairo_move_to(cr
, pos
.left(), pos
.top());
143 pango_cairo_show_layout(cr
, layout
);
146 g_object_unref(layout
);