1
/*******************************************************************************
2
**3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
3
** 10 20 30 40 50 60 70 80
7
** util.c - all sorts of helper functions
9
** Copyright 2009 Canonical Ltd.
12
** Cody Russell <cody.russell@canonical.com>
13
** Mirco "MacSlow" Mueller <mirco.mueller@canonical.com>
15
** This program is free software: you can redistribute it and/or modify it
16
** under the terms of the GNU General Public License version 3, as published
17
** by the Free Software Foundation.
19
** This program is distributed in the hope that it will be useful, but
20
** WITHOUT ANY WARRANTY; without even the implied warranties of
21
** MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
22
** PURPOSE. See the GNU General Public License for more details.
24
** You should have received a copy of the GNU General Public License along
25
** with this program. If not, see <http://www.gnu.org/licenses/>.
27
*******************************************************************************/
33
#include <pango/pango.h>
36
#define CHARACTER_LT_REGEX "&(lt;|#60;|#x3c;)"
37
#define CHARACTER_GT_REGEX "&(gt;|#62;|#x3e;)"
38
#define CHARACTER_AMP_REGEX "&(amp;|#38;|#x26;)"
39
#define CHARACTER_APOS_REGEX "'"
40
#define CHARACTER_QUOT_REGEX """
41
#define CHARACTER_NEWLINE_REGEX " *((<br[^/>]*/?>|\r|\n)+ *)+"
43
#define TAG_MATCH_REGEX "<(b|i|u|big|a|img|span|s|sub|small|tt|html|qt)\\b[^>]*>(.*?)</\\1>|<(img|span|a)[^>]/>|<(img)[^>]*>"
44
#define TAG_REPLACE_REGEX "<(b|i|u|big|a|img|span|s|sub|small|tt|html|qt)\\b[^>]*>|</(b|i|u|big|a|img|span|s|sub|small|tt|html|qt)>"
46
struct _ReplaceMarkupData
52
typedef struct _ReplaceMarkupData ReplaceMarkupData;
55
strip_html (const gchar *text, const gchar *match_regex, const gchar* replace_regex)
59
gboolean match = FALSE;
60
GMatchInfo *info = NULL;
62
regex = g_regex_new (match_regex, G_REGEX_DOTALL | G_REGEX_OPTIMIZE, 0, NULL);
63
match = g_regex_match (regex, text, 0, &info);
64
g_regex_unref (regex);
67
regex = g_regex_new (replace_regex, G_REGEX_DOTALL | G_REGEX_OPTIMIZE, 0, NULL);
68
ret = g_regex_replace (regex, text, -1, 0, "", 0, NULL);
69
g_regex_unref (regex);
71
ret = g_strdup (text);
75
g_match_info_free (info);
81
replace_markup (const gchar *text, const gchar *match_regex, const gchar *replace_text)
86
regex = g_regex_new (match_regex, G_REGEX_DOTALL | G_REGEX_OPTIMIZE, 0, NULL);
87
ret = g_regex_replace (regex, text, -1, 0, replace_text, 0, NULL);
88
g_regex_unref (regex);
94
filter_text (const gchar *text)
98
text1 = strip_html (text, TAG_MATCH_REGEX, TAG_REPLACE_REGEX);
100
static ReplaceMarkupData data[] = {
101
{ CHARACTER_AMP_REGEX, "&" },
102
{ CHARACTER_LT_REGEX, "<" },
103
{ CHARACTER_GT_REGEX, ">" },
104
{ CHARACTER_APOS_REGEX, "'" },
105
{ CHARACTER_QUOT_REGEX, "\"" },
106
{ CHARACTER_NEWLINE_REGEX, "\n" }
109
ReplaceMarkupData* ptr = data;
110
ReplaceMarkupData* end = data + sizeof(data) / sizeof(ReplaceMarkupData);
111
for (; ptr != end; ++ptr) {
112
gchar* tmp = replace_markup (text1, ptr->regex, ptr->replacement);
121
newline_to_space (const gchar *text)
125
text1 = strip_html (text, TAG_MATCH_REGEX, TAG_REPLACE_REGEX);
127
static ReplaceMarkupData data[] = {
128
{ CHARACTER_NEWLINE_REGEX, " " }
131
ReplaceMarkupData* ptr = data;
132
ReplaceMarkupData* end = data + sizeof(data) / sizeof(ReplaceMarkupData);
133
for (; ptr != end; ++ptr) {
134
gchar* tmp = replace_markup (text1, ptr->regex, ptr->replacement);
143
destroy_cloned_surface (cairo_surface_t* surface)
145
gboolean finalref = FALSE;
146
g_return_val_if_fail (surface, FALSE);
148
if (cairo_surface_get_reference_count (surface) == 1) {
149
g_free (cairo_image_surface_get_data (surface));
152
cairo_surface_destroy (surface);
157
copy_surface (cairo_surface_t* orig)
159
cairo_surface_t* copy = NULL;
160
guchar* pixels_src = NULL;
161
guchar* pixels_cpy = NULL;
162
cairo_format_t format;
167
pixels_src = cairo_image_surface_get_data (orig);
171
format = cairo_image_surface_get_format (orig);
172
width = cairo_image_surface_get_width (orig);
173
height = cairo_image_surface_get_height (orig);
174
stride = cairo_image_surface_get_stride (orig);
176
pixels_cpy = g_malloc0 (stride * height);
180
memcpy ((void*) pixels_cpy, (void*) pixels_src, height * stride);
182
copy = cairo_image_surface_create_for_data (pixels_cpy,
191
// code of get_wm_name() based in large chunks on www.amsn-project.net
193
get_wm_name (Display* dpy)
198
unsigned long bytes_returned;
199
unsigned long n_returned;
200
unsigned char* buffer;
209
screen = DefaultScreen (dpy);
210
root = RootWindow (dpy, screen);
211
supwmcheck = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
212
wmname = XInternAtom (dpy, "_NET_WM_NAME", False);
214
XGetWindowProperty (dpy,
227
child = (Window*) buffer;
232
XGetWindowProperty (dpy,
250
// example wm-names as reported by get_wm_name()
258
return (gchar*) buffer;
262
extract_font_face (const gchar* string)
264
GRegex* regex = NULL;
265
GMatchInfo* match_info = NULL;
266
GString* font_face = NULL;
272
// extract font-face-name/style
273
font_face = g_string_new ("");
277
// setup regular expression to extract leading text before trailing int
278
regex = g_regex_new ("([A-Z a-z])+", 0, 0, NULL);
281
g_regex_match (regex, string, 0, &match_info);
282
while (g_match_info_matches (match_info))
286
word = g_match_info_fetch (match_info, 0);
289
g_string_append (font_face, word);
293
g_match_info_next (match_info, NULL);
297
g_match_info_free (match_info);
298
g_regex_unref (regex);