1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
|
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
* Copyright 2011 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the applicable version of the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of both the GNU Lesser General Public
* License version 3 along with this program. If not, see
* <http://www.gnu.org/licenses/>
*
* Authored by: Gordon Allott <gord.allott@canonical.com>
*
*/
#include "ResultRendererTile.h"
#include <pango/pangocairo.h>
#include <NuxCore/Logger.h>
#include <UnityCore/GLibWrapper.h>
#include <NuxGraphics/GdkGraphics.h>
#include "unity-shared/CairoTexture.h"
#include "unity-shared/DashStyle.h"
#include "unity-shared/TextureCache.h"
#include "unity-shared/RawPixel.h"
#include "unity-shared/UnitySettings.h"
namespace unity
{
DECLARE_LOGGER(logger, "unity.dash.results");
namespace
{
const std::string DEFAULT_GICON = ". GThemedIcon text-x-preview";
const RawPixel PADDING = 6_em;
const RawPixel SPACING = 10_em;
const int FONT_SIZE = 10;
const int FONT_MULTIPLIER = 1024;
char const REPLACEMENT_CHAR = '?';
float const CORNER_HIGHTLIGHT_RADIUS = 2.0f;
void RenderTexture(nux::GraphicsEngine& GfxContext,
int x,
int y,
int width,
int height,
nux::ObjectPtr<nux::IOpenGLBaseTexture> const& texture,
nux::TexCoordXForm &texxform,
const nux::Color &color,
float saturate
)
{
if (saturate == 1.0)
{
GfxContext.QRP_1Tex(x,
y,
width,
height,
texture,
texxform,
color);
}
else
{
GfxContext.QRP_TexDesaturate(x,
y,
width,
height,
texture,
texxform,
color,
saturate);
}
}
}
namespace dash
{
NUX_IMPLEMENT_OBJECT_TYPE(ResultRendererTile);
ResultRendererTile::ResultRendererTile(NUX_FILE_LINE_DECL)
: ResultRenderer(NUX_FILE_LINE_PARAM)
{
UpdateWidthHeight();
scale.changed.connect([this] (double) { UpdateWidthHeight(); });
}
void ResultRendererTile::UpdateWidthHeight()
{
dash::Style const& style = dash::Style::Instance();
RawPixel tile_width = style.GetTileWidth();
RawPixel tile_height = style.GetTileHeight();
width = tile_width.CP(scale());
height = tile_height.CP(scale());
}
void ResultRendererTile::Render(nux::GraphicsEngine& GfxContext,
Result& row,
ResultRendererState state,
nux::Geometry const& geometry,
int x_offset, int y_offset,
nux::Color const& color,
float saturate)
{
TextureContainer* container = row.renderer<TextureContainer*>();
if (container == nullptr)
return;
dash::Style const& style = dash::Style::Instance();
RawPixel const tile_size = style.GetTileImageSize();
RawPixel const tile_width = style.GetTileWidth();
RawPixel const tile_height = style.GetTileHeight();
RawPixel const tile_highlight_width = style.GetTileIconHightlightWidth();
RawPixel const tile_highlight_height = style.GetTileIconHightlightHeight();
int tile_icon_size = tile_size.CP(scale());
// set up our texture mode
nux::TexCoordXForm texxform;
int icon_width, icon_height;
if (container->icon == nullptr)
{
icon_width = icon_height = tile_icon_size;
}
else
{
icon_width = container->icon->GetWidth();
icon_height = container->icon->GetHeight();
}
int icon_left_hand_side = geometry.x + (geometry.width - icon_width) / 2;
int icon_top_side = geometry.y + PADDING.CP(scale()) + (tile_icon_size - icon_height) / 2;
// render highlight if its needed
if (container->prelight && state != ResultRendererState::RESULT_RENDERER_NORMAL)
{
int highlight_x = (geometry.x + geometry.width/2) - tile_highlight_width.CP(scale())/2;
int highlight_y = (geometry.y + PADDING.CP(scale()) + tile_icon_size / 2) - tile_highlight_height.CP(scale())/2;
RenderTexture(GfxContext,
highlight_x,
highlight_y,
container->prelight->GetWidth(),
container->prelight->GetHeight(),
container->prelight->GetDeviceTexture(),
texxform,
color,
saturate);
}
// draw the icon
if (container->icon)
{
RenderTexture(GfxContext,
icon_left_hand_side,
icon_top_side,
container->icon->GetWidth(),
container->icon->GetHeight(),
container->icon->GetDeviceTexture(),
texxform,
color,
saturate);
}
if (container->text)
{
RenderTexture(GfxContext,
geometry.x + PADDING.CP(scale()),
geometry.y + tile_icon_size + SPACING.CP(scale()),
tile_width.CP(scale()) - (PADDING.CP(scale()) * 2),
tile_height.CP(scale()) - tile_icon_size - SPACING.CP(scale()),
container->text->GetDeviceTexture(),
texxform,
color,
saturate);
}
}
nux::BaseTexture* ResultRendererTile::DrawHighlight(std::string const& texid, int width, int height)
{
nux::CairoGraphics cairo_graphics(CAIRO_FORMAT_ARGB32, width, height);
cairo_surface_set_device_scale(cairo_graphics.GetSurface(), scale(), scale());
cairo_t* cr = cairo_graphics.GetInternalContext();
cairo_scale(cr, 1.0f, 1.0f);
cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.0);
cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
cairo_paint(cr);
// draw the highlight
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
cairo_set_source_rgba(cr, 1.0f, 1.0f, 1.0f, 0.2f);
cairo_graphics.DrawRoundedRectangle(cr,
1.0f,
0.0f,
0.0f,
CORNER_HIGHTLIGHT_RADIUS,
width/scale(),
height/scale(),
false);
cairo_fill(cr);
return texture_from_cairo_graphics(cairo_graphics);
}
int ResultRendererTile::Padding() const
{
return PADDING.CP(scale());
}
void ResultRendererTile::Preload(Result const& row)
{
if (row.renderer<TextureContainer*>() == nullptr)
{
// Shouldn't really do this, but it's safe in this case and quicker than making a copy.
const_cast<Result&>(row).set_renderer(new TextureContainer());
LoadIcon(row);
LoadText(row);
}
}
void ResultRendererTile::ReloadResult(Result const& row)
{
Unload(row);
if (row.renderer<TextureContainer*>() == nullptr)
const_cast<Result&>(row).set_renderer(new TextureContainer());
LoadIcon(row);
LoadText(row);
}
void ResultRendererTile::Unload(Result const& row)
{
TextureContainer *container = row.renderer<TextureContainer*>();
if (container)
{
delete container;
// Shouldn't really do this, but it's safe in this case and quicker than making a copy.
const_cast<Result&>(row).set_renderer<TextureContainer*>(nullptr);
}
}
nux::NBitmapData* ResultRendererTile::GetDndImage(Result const& row) const
{
TextureContainer* container = row.renderer<TextureContainer*>();
nux::NBitmapData* bitmap = nullptr;
if (container && container->drag_icon && container->drag_icon.IsType(GDK_TYPE_PIXBUF))
{
// Need to ref the drag icon because GdkGraphics will unref it.
nux::GdkGraphics graphics(GDK_PIXBUF(g_object_ref(container->drag_icon)));
bitmap = graphics.GetBitmap();
}
return bitmap ? bitmap : ResultRenderer::GetDndImage(row);
}
void ResultRendererTile::LoadIcon(Result const& row)
{
Style const& style = Style::Instance();
RawPixel const tile_size = style.GetTileImageSize();
RawPixel const tile_gsize = style.GetTileGIconSize();
RawPixel const tile_highlight_width = style.GetTileIconHightlightWidth();
RawPixel const tile_highlight_height = style.GetTileIconHightlightHeight();
std::string const& icon_hint = row.icon_hint;
std::string const& icon_name = !icon_hint.empty() ? icon_hint : DEFAULT_GICON;
glib::Object<GIcon> icon(g_icon_new_for_string(icon_name.c_str(), NULL));
TextureContainer* container = row.renderer<TextureContainer*>();
if (container)
{
TextureCache& cache = TextureCache::GetDefault();
BaseTexturePtr texture_prelight(cache.FindTexture("resultview_prelight",
tile_highlight_width.CP(scale()),
tile_highlight_height.CP(scale()),
sigc::mem_fun(this, &ResultRendererTile::DrawHighlight)));
container->prelight = texture_prelight;
}
IconLoader::IconLoaderCallback slot = sigc::bind(sigc::mem_fun(this, &ResultRendererTile::IconLoaded), icon_hint, row);
if (icon.IsType(G_TYPE_ICON))
{
bool use_large_icon = icon.IsType(G_TYPE_FILE_ICON) || !icon.IsType(G_TYPE_THEMED_ICON);
container->slot_handle = IconLoader::GetDefault().LoadFromGIconString(icon_name,
tile_size.CP(scale()),
use_large_icon ?
tile_size.CP(scale()) : tile_gsize.CP(scale()), slot);
}
else
{
container->slot_handle = IconLoader::GetDefault().LoadFromIconName(icon_name, -1, tile_gsize.CP(scale()), slot);
}
}
nux::BaseTexture* ResultRendererTile::CreateTextureCallback(std::string const& texid,
int width,
int height,
glib::Object<GdkPixbuf> const& pixbuf)
{
int pixbuf_width, pixbuf_height;
pixbuf_width = gdk_pixbuf_get_width(pixbuf);
pixbuf_height = gdk_pixbuf_get_height(pixbuf);
if (G_UNLIKELY(!pixbuf_height || !pixbuf_width))
{
LOG_ERROR(logger) << "Pixbuf: " << texid << " has a zero height/width: "
<< width << "," << height;
pixbuf_width = (pixbuf_width) ? pixbuf_width : 1; // no zeros please
pixbuf_height = (pixbuf_height) ? pixbuf_height: 1; // no zeros please
}
if (pixbuf_width == pixbuf_height)
{
// quick path for square icons
return nux::CreateTexture2DFromPixbuf(pixbuf, true);
}
else
{
Style const& style = Style::Instance();
RawPixel const tile_size = style.GetTileImageSize();
// slow path for non square icons that must be resized to fit in the square
// texture
float aspect = static_cast<float>(pixbuf_height) / pixbuf_width; // already sanitized width/height so can not be 0.0
if (aspect < 1.0f)
{
pixbuf_width = tile_size.CP(scale());
pixbuf_height = pixbuf_width * aspect;
if (pixbuf_height > height)
{
// scaled too big, scale down
pixbuf_height = height;
pixbuf_width = pixbuf_height / aspect;
}
}
else
{
pixbuf_height = height;
pixbuf_width = pixbuf_height / aspect;
}
if (gdk_pixbuf_get_height(pixbuf) == pixbuf_height)
{
// we changed our mind, fast path is good
return nux::CreateTexture2DFromPixbuf(pixbuf, true);
}
nux::CairoGraphics cairo_graphics(CAIRO_FORMAT_ARGB32, pixbuf_width, pixbuf_height);
cairo_surface_set_device_scale(cairo_graphics.GetSurface(), scale(), scale());
cairo_t* cr = cairo_graphics.GetInternalContext();
cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
cairo_paint(cr);
float scale = float(pixbuf_height) / gdk_pixbuf_get_height(pixbuf);
//cairo_translate(cr,
// static_cast<int>((width - (pixbuf_width * scale)) * 0.5),
// static_cast<int>((height - (pixbuf_height * scale)) * 0.5));
cairo_scale(cr, scale, scale);
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0);
cairo_paint(cr);
return texture_from_cairo_graphics(cairo_graphics);
}
}
void ResultRendererTile::IconLoaded(std::string const& texid,
int max_width,
int max_height,
glib::Object<GdkPixbuf> const& pixbuf,
std::string icon_name,
Result const& row)
{
TextureContainer *container = row.renderer<TextureContainer*>();
if (pixbuf && container)
{
TextureCache& cache = TextureCache::GetDefault();
BaseTexturePtr texture(cache.FindTexture(icon_name, max_width, max_height,
sigc::bind(sigc::mem_fun(this, &ResultRendererTile::CreateTextureCallback), pixbuf)));
container->icon = texture;
container->drag_icon = pixbuf;
NeedsRedraw.emit();
if (container)
container->slot_handle = 0;
}
else if (container)
{
// we need to load a missing icon
IconLoader::IconLoaderCallback slot = sigc::bind(sigc::mem_fun(this, &ResultRendererTile::IconLoaded), icon_name, row);
container->slot_handle = IconLoader::GetDefault().LoadFromGIconString(". GThemedIcon text-x-preview", max_width, max_height, slot);
}
}
/* Blacklisted unicode ranges:
* Burmese: U+1000 -> U+109F
* Extended: U+AA60 -> U+AA7B
*/
bool IsBlacklistedChar(gunichar uni_c)
{
if ((uni_c >= 0x1000 && uni_c <= 0x109F) ||
(uni_c >= 0xAA60 && uni_c >= 0xAA7B))
{
return true;
}
return false;
}
// FIXME Bug (lp.1239381) in the backend of pango that crashes
// when using ellipsize with/height setting in pango
std::string ReplaceBlacklistedChars(std::string const& str)
{
std::string new_string("");
if (!g_utf8_validate(str.c_str(), -1, NULL))
return new_string;
gchar const* uni_s = str.c_str();
gunichar uni_c;
gchar utf8_buff[6];
int size = g_utf8_strlen(uni_s, -1);
for (int i = 0; i < size; ++i)
{
uni_c = g_utf8_get_char(uni_s);
uni_s = g_utf8_next_char(uni_s);
if (IsBlacklistedChar(uni_c))
{
new_string += REPLACEMENT_CHAR;
}
else
{
int end = g_unichar_to_utf8(uni_c, utf8_buff);
utf8_buff[end] = '\0';
new_string += utf8_buff;
}
}
return new_string;
}
void ResultRendererTile::LoadText(Result const& row)
{
Style const& style = Style::Instance();
RawPixel const tile_size = style.GetTileImageSize();
RawPixel const tile_width = style.GetTileWidth();
RawPixel const tile_height = style.GetTileHeight();
nux::CairoGraphics _cairoGraphics(CAIRO_FORMAT_ARGB32,
tile_width.CP(scale()) - (PADDING.CP(scale()) * 2),
tile_height.CP(scale()) - tile_size.CP(scale()) - SPACING.CP(scale()));
cairo_surface_set_device_scale(_cairoGraphics.GetSurface(), scale(), scale());
cairo_t* cr = _cairoGraphics.GetInternalContext();
PangoLayout* layout = NULL;
PangoFontDescription* desc = NULL;
PangoContext* pango_context = NULL;
GdkScreen* screen = gdk_screen_get_default(); // not ref'ed
glib::String font;
g_object_get(gtk_settings_get_default(), "gtk-font-name", &font, NULL);
cairo_set_font_options(cr, gdk_screen_get_font_options(screen));
layout = pango_cairo_create_layout(cr);
desc = pango_font_description_from_string(font.Value());
pango_font_description_set_size (desc, FONT_SIZE * FONT_MULTIPLIER);
pango_layout_set_font_description(layout, desc);
pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_START);
pango_layout_set_width(layout, (tile_width - (PADDING * 2))* PANGO_SCALE);
pango_layout_set_height(layout, -2);
// FIXME bug #1239381
std::string name = ReplaceBlacklistedChars(row.name());
char *escaped_text = g_markup_escape_text(name.c_str(), -1);
pango_layout_set_markup(layout, escaped_text, -1);
g_free (escaped_text);
pango_context = pango_layout_get_context(layout); // is not ref'ed
pango_cairo_context_set_font_options(pango_context, gdk_screen_get_font_options(screen));
pango_cairo_context_set_resolution(pango_context, 96.0 * Settings::Instance().font_scaling());
pango_layout_context_changed(layout);
cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
cairo_paint(cr);
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
cairo_set_source_rgba(cr, 1.0f, 1.0f, 1.0f, 1.0f);
cairo_move_to(cr, 0.0f, 0.0f);
pango_cairo_show_layout(cr, layout);
// clean up
pango_font_description_free(desc);
g_object_unref(layout);
TextureContainer *container = row.renderer<TextureContainer*>();
if (container)
container->text = texture_ptr_from_cairo_graphics(_cairoGraphics);
}
}
}
|