-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDashStyle.cpp
executable file
·2427 lines (2007 loc) · 67.8 KB
/
DashStyle.cpp
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
* Copyright (C) 2011 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Mirco Müller <mirco.mueller@canonical.com
* Neil Jagdish Patel <neil.patel@canonical.com>
*/
#include "DashStyle.h"
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <glib.h>
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include <pango/pango.h>
#include <NuxCore/Color.h>
#include <NuxCore/Logger.h>
#include <NuxGraphics/ImageSurface.h>
#include <NuxGraphics/CairoGraphics.h>
#include <Nux/PaintLayer.h>
#include <UnityCore/GLibSignal.h>
#include <UnityCore/GLibWrapper.h>
#include "CairoTexture.h"
#include "JSONParser.h"
#include "UnitySettings.h"
#include "config.h"
#define DASH_WIDGETS_FILE DATADIR"/unity/themes/dash-widgets.json"
typedef nux::ObjectPtr<nux::BaseTexture> BaseTexturePtr;
namespace unity
{
namespace dash
{
namespace
{
nux::logging::Logger logger("unity.dash");
Style* style_instance = nullptr;
const int STATES = 5;
// These cairo overrides may also be reused somewhere...
void cairo_set_source_rgba(cairo_t* cr, nux::Color const& color)
{
::cairo_set_source_rgba(cr, color.red, color.green, color.blue, color.alpha);
}
inline double _align(double val, bool odd=true)
{
double fract = val - (int) val;
if (odd)
{
// for strokes with an odd line-width
if (fract != 0.5f)
return (double) ((int) val + 0.5f);
else
return val;
}
else
{
// for strokes with an even line-width
if (fract != 0.0f)
return (double) ((int) val);
else
return val;
}
}
class LazyLoadTexture
{
public:
LazyLoadTexture(std::string const& filename, int size = -1);
nux::BaseTexture* texture();
private:
void LoadTexture();
private:
std::string filename_;
int size_;
BaseTexturePtr texture_;
};
} // anon namespace
class Style::Impl
{
public:
Impl(Style* owner);
~Impl();
void Blur(cairo_t* cr, int size);
void SetDefaultValues();
void GetTextExtents(int& width,
int& height,
int maxWidth,
int maxHeight,
std::string const& text);
void Text(cairo_t* cr,
nux::Color const& color,
std::string const& label,
int font_size,
double horizMargin = 4.0,
Alignment alignment = Alignment::CENTER);
void ButtonOutlinePath(cairo_t* cr, bool align);
void ButtonOutlinePathSegment(cairo_t* cr, Segment segment);
void ArrowPath(cairo_t* cr, Arrow arrow);
cairo_operator_t SetBlendMode(cairo_t* cr, BlendMode mode);
void DrawOverlay(cairo_t* cr,
double opacity,
BlendMode mode,
int blurSize);
void RoundedRectSegment(cairo_t* cr,
double aspect,
double x,
double y,
double cornerRadius,
double width,
double height,
Segment segment,
Arrow arrow,
nux::ButtonVisualState state);
void Refresh();
void OnFontChanged(GtkSettings* object, GParamSpec* pspec);
// Members
Style* owner_;
cairo_font_options_t* default_font_options_;
std::vector<nux::Color> button_label_border_color_;
std::vector<double> button_label_border_size_;
double button_label_text_size_;
std::vector<nux::Color> button_label_text_color_;
std::vector<nux::Color> button_label_fill_color_;
std::vector<double> button_label_overlay_opacity_;
std::vector<BlendMode> button_label_overlay_mode_;
std::vector<int> button_label_blur_size_;
nux::Color regular_text_color_;
double regular_text_size_;
BlendMode regular_text_mode_;
FontWeight regular_text_weight_;
double separator_size_;
nux::Color separator_color_;
double separator_overlay_opacity_;
BlendMode separator_overlay_mode_;
int separator_blur_size_;
nux::Color scrollbar_color_;
double scrollbar_overlay_opacity_;
BlendMode scrollbar_overlay_mode_;
int scrollbar_blur_size_;
int scrollbar_size_;
double scrollbar_corner_radius_;
glib::SignalManager signal_manager_;
nux::Color text_color_;
int text_width_;
int text_height_;
int number_of_columns_;
LazyLoadTexture category_texture_;
LazyLoadTexture category_texture_no_filters_;
LazyLoadTexture dash_bottom_texture_;
LazyLoadTexture dash_bottom_texture_mask_;
LazyLoadTexture dash_right_texture_;
LazyLoadTexture dash_right_texture_mask_;
LazyLoadTexture dash_corner_texture_;
LazyLoadTexture dash_corner_texture_mask_;
LazyLoadTexture dash_fullscreen_icon_;
LazyLoadTexture dash_left_edge_;
LazyLoadTexture dash_left_corner_;
LazyLoadTexture dash_left_corner_mask_;
LazyLoadTexture dash_left_tile_;
LazyLoadTexture dash_top_corner_;
LazyLoadTexture dash_top_corner_mask_;
LazyLoadTexture dash_top_tile_;
LazyLoadTexture dash_shine_;
LazyLoadTexture search_magnify_texture_;
LazyLoadTexture search_circle_texture_;
LazyLoadTexture search_close_texture_;
LazyLoadTexture search_spin_texture_;
LazyLoadTexture refine_gradient_corner_;
LazyLoadTexture refine_gradient_dash_;
LazyLoadTexture refine_gradient_no_refine_dash_;
LazyLoadTexture group_unexpand_texture_;
LazyLoadTexture group_expand_texture_;
LazyLoadTexture star_deselected_texture_;
LazyLoadTexture star_selected_texture_;
LazyLoadTexture star_highlight_texture_;
};
Style::Impl::Impl(Style* owner)
: owner_(owner)
, button_label_border_color_(STATES)
, button_label_border_size_(STATES)
, button_label_text_color_(STATES)
, button_label_fill_color_(STATES)
, button_label_overlay_opacity_(STATES)
, button_label_overlay_mode_(STATES)
, button_label_blur_size_(STATES)
, text_color_(nux::color::White)
, text_width_(0)
, text_height_(0)
, number_of_columns_(6)
, category_texture_("/category_gradient.png")
, category_texture_no_filters_("/category_gradient_no_refine.png")
, dash_bottom_texture_("/dash_bottom_border_tile.png")
, dash_bottom_texture_mask_("/dash_bottom_border_tile_mask.png")
, dash_right_texture_("/dash_right_border_tile.png")
, dash_right_texture_mask_("/dash_right_border_tile_mask.png")
, dash_corner_texture_("/dash_bottom_right_corner.png")
, dash_corner_texture_mask_("/dash_bottom_right_corner_mask.png")
, dash_fullscreen_icon_("/dash_fullscreen_icon.png")
, dash_left_edge_("/dash_left_edge.png")
, dash_left_corner_("/dash_bottom_left_corner.png")
, dash_left_corner_mask_("/dash_bottom_left_corner_mask.png")
, dash_left_tile_("/dash_left_tile.png")
, dash_top_corner_("/dash_top_right_corner.png")
, dash_top_corner_mask_("/dash_top_right_corner_mask.png")
, dash_top_tile_("/dash_top_tile.png")
, dash_shine_("/dash_sheen.png")
, search_magnify_texture_("/search_magnify.png")
, search_circle_texture_("/search_circle.svg", 32)
, search_close_texture_("/search_close.svg", 32)
, search_spin_texture_("/search_spin.svg", 32)
, refine_gradient_corner_("/refine_gradient_corner.png")
, refine_gradient_dash_("/refine_gradient_dash.png")
, refine_gradient_no_refine_dash_("/refine_gradient_dash_no_refine.png")
, group_unexpand_texture_("/dash_group_unexpand.png")
, group_expand_texture_("/dash_group_expand.png")
, star_deselected_texture_("/star_deselected.png")
, star_selected_texture_("/star_selected.png")
, star_highlight_texture_("/star_highlight.png")
{
signal_manager_.Add(new glib::Signal<void, GtkSettings*, GParamSpec*>
(gtk_settings_get_default(),
"notify::gtk-font-name",
sigc::mem_fun(this, &Impl::OnFontChanged)));
signal_manager_.Add(new glib::Signal<void, GtkSettings*, GParamSpec*>
(gtk_settings_get_default(),
"notify::gtk-xft-dpi",
sigc::mem_fun(this, &Impl::OnFontChanged)));
Refresh();
// create fallback font-options
default_font_options_ = cairo_font_options_create();
if (cairo_font_options_status(default_font_options_) == CAIRO_STATUS_SUCCESS)
{
cairo_font_options_set_antialias(default_font_options_,
CAIRO_ANTIALIAS_GRAY);
cairo_font_options_set_subpixel_order(default_font_options_,
CAIRO_SUBPIXEL_ORDER_RGB);
cairo_font_options_set_hint_style(default_font_options_,
CAIRO_HINT_STYLE_SLIGHT);
cairo_font_options_set_hint_metrics(default_font_options_,
CAIRO_HINT_METRICS_ON);
}
json::Parser parser;
// Since the parser skips values if they are not found, make sure everything
// is initialised.
SetDefaultValues();
if (!parser.Open(DASH_WIDGETS_FILE))
return;
// button-label
parser.ReadColors("button-label", "border-color", "border-opacity",
button_label_border_color_);
parser.ReadDoubles("button-label", "border-size", button_label_border_size_);
parser.ReadDouble("button-label", "text-size", button_label_text_size_);
parser.ReadColors("button-label", "text-color", "text-opacity",
button_label_text_color_);
parser.ReadColors("button-label", "fill-color", "fill-opacity",
button_label_fill_color_);
parser.ReadDoubles("button-label", "overlay-opacity", button_label_overlay_opacity_);
std::map<std::string, BlendMode> blend_mode_map;
blend_mode_map["normal"] = BlendMode::NORMAL;
blend_mode_map["multiply"] = BlendMode::MULTIPLY;
blend_mode_map["screen"] = BlendMode::SCREEN;
parser.ReadMappedStrings("button-label", "overlay-mode", blend_mode_map,
button_label_overlay_mode_);
parser.ReadInts("button-label", "blur-size", button_label_blur_size_);
// regular-text
parser.ReadColor("regular-text", "text-color", "text-opacity",
regular_text_color_);
parser.ReadDouble("regular-text", "text-size", regular_text_size_);
parser.ReadMappedString("regular-text", "text-mode", blend_mode_map,
regular_text_mode_);
std::map<std::string, FontWeight> font_weight_map;
font_weight_map["light"] = FontWeight::LIGHT;
font_weight_map["regular"] = FontWeight::REGULAR;
font_weight_map["bold"] = FontWeight::BOLD;
parser.ReadMappedString("regular-text", "text-weight", font_weight_map,
regular_text_weight_);
// separator
parser.ReadDouble("separator", "size", separator_size_);
parser.ReadColor("separator", "color", "opacity", separator_color_);
parser.ReadDouble("separator", "overlay-opacity", separator_overlay_opacity_);
parser.ReadMappedString("separator", "overlay-mode", blend_mode_map,
separator_overlay_mode_);
parser.ReadInt("separator", "blur-size", separator_blur_size_);
// scrollbar
parser.ReadColor("scrollbar", "color", "opacity", scrollbar_color_);
parser.ReadDouble("scrollbar", "overlay-opacity", scrollbar_overlay_opacity_);
parser.ReadMappedString("scrollbar", "overlay-mode", blend_mode_map,
scrollbar_overlay_mode_);
parser.ReadInt("scrollbar", "blur-size", scrollbar_blur_size_);
parser.ReadInt("scrollbar", "size", scrollbar_size_);
parser.ReadDouble("scrollbar", "corner-radius", scrollbar_corner_radius_);
}
Style::Impl::~Impl()
{
if (cairo_font_options_status(default_font_options_) == CAIRO_STATUS_SUCCESS)
cairo_font_options_destroy(default_font_options_);
}
void Style::Impl::Refresh()
{
const char* const SAMPLE_MAX_TEXT = "Chromium Web Browser";
GtkSettings* settings = ::gtk_settings_get_default();
nux::CairoGraphics util_cg(CAIRO_FORMAT_ARGB32, 1, 1);
cairo_t* cr = util_cg.GetInternalContext();
glib::String font_description;
int dpi = 0;
::g_object_get(settings,
"gtk-font-name", &font_description,
"gtk-xft-dpi", &dpi,
NULL);
PangoFontDescription* desc = ::pango_font_description_from_string(font_description);
::pango_font_description_set_weight(desc, PANGO_WEIGHT_NORMAL);
::pango_font_description_set_size(desc, 9 * PANGO_SCALE);
glib::Object<PangoLayout> layout(::pango_cairo_create_layout(cr));
::pango_layout_set_font_description(layout, desc);
::pango_layout_set_text(layout, SAMPLE_MAX_TEXT, -1);
PangoContext* cxt = ::pango_layout_get_context(layout);
GdkScreen* screen = ::gdk_screen_get_default();
::pango_cairo_context_set_font_options(cxt, ::gdk_screen_get_font_options(screen));
float pango_scale = PANGO_SCALE;
::pango_cairo_context_set_resolution(cxt, dpi / pango_scale);
::pango_layout_context_changed(layout);
PangoRectangle log_rect;
::pango_layout_get_extents(layout, NULL, &log_rect);
text_width_ = log_rect.width / PANGO_SCALE;
text_height_ = log_rect.height / PANGO_SCALE;
owner_->changed.emit();
pango_font_description_free(desc);
}
void Style::Impl::OnFontChanged(GtkSettings* object, GParamSpec* pspec)
{
Refresh();
}
Style::Style()
: always_maximised(false)
, pimpl(new Impl(this))
{
if (style_instance)
{
LOG_ERROR(logger) << "More than one dash::Style created.";
}
else
{
style_instance = this;
}
auto formfactor_lambda = [this] (FormFactor)
{
FormFactor formfactor = Settings::Instance().form_factor();
always_maximised = (formfactor == FormFactor::NETBOOK || formfactor == FormFactor::TV);
};
Settings::Instance().form_factor.changed.connect(formfactor_lambda);
formfactor_lambda(FormFactor());
}
Style::~Style ()
{
delete pimpl;
if (style_instance == this)
style_instance = nullptr;
}
Style& Style::Instance()
{
if (!style_instance)
{
LOG_ERROR(logger) << "No dash::Style created yet.";
}
return *style_instance;
}
void Style::RoundedRect(cairo_t* cr,
double aspect,
double x,
double y,
double cornerRadius,
double width,
double height)
{
// sanity check
if (cairo_status(cr) != CAIRO_STATUS_SUCCESS &&
cairo_surface_get_type(cairo_get_target(cr)) != CAIRO_SURFACE_TYPE_IMAGE)
return;
bool odd = true;
odd = cairo_get_line_width (cr) == 2.0 ? false : true;
double radius = cornerRadius / aspect;
// top-left, right of the corner
cairo_move_to(cr, _align (x + radius, odd), _align (y, odd));
// top-right, left of the corner
cairo_line_to(cr, _align(x + width - radius, odd), _align(y, odd));
// top-right, below the corner
cairo_arc(cr,
_align(x + width - radius, odd),
_align(y + radius, odd),
radius,
-90.0f * G_PI / 180.0f,
0.0f * G_PI / 180.0f);
// bottom-right, above the corner
cairo_line_to(cr, _align(x + width, odd), _align(y + height - radius, odd));
// bottom-right, left of the corner
cairo_arc(cr,
_align(x + width - radius, odd),
_align(y + height - radius, odd),
radius,
0.0f * G_PI / 180.0f,
90.0f * G_PI / 180.0f);
// bottom-left, right of the corner
cairo_line_to(cr, _align(x + radius, odd), _align(y + height, odd));
// bottom-left, above the corner
cairo_arc(cr,
_align(x + radius, odd),
_align(y + height - radius, odd),
radius,
90.0f * G_PI / 180.0f,
180.0f * G_PI / 180.0f);
// top-left, right of the corner
cairo_arc(cr,
_align(x + radius, odd),
_align(y + radius, odd),
radius,
180.0f * G_PI / 180.0f,
270.0f * G_PI / 180.0f);
}
static inline void _blurinner(guchar* pixel,
gint* zR,
gint* zG,
gint* zB,
gint* zA,
gint alpha,
gint aprec,
gint zprec)
{
gint r;
gint g;
gint b;
guchar a;
r = *pixel;
g = *(pixel + 1);
b = *(pixel + 2);
a = *(pixel + 3);
*zR += (alpha * ((r << zprec) - *zR)) >> aprec;
*zG += (alpha * ((g << zprec) - *zG)) >> aprec;
*zB += (alpha * ((b << zprec) - *zB)) >> aprec;
*zA += (alpha * ((a << zprec) - *zA)) >> aprec;
*pixel = *zR >> zprec;
*(pixel + 1) = *zG >> zprec;
*(pixel + 2) = *zB >> zprec;
*(pixel + 3) = *zA >> zprec;
}
static inline void _blurrow(guchar* pixels,
gint width,
gint height,
gint channels,
gint line,
gint alpha,
gint aprec,
gint zprec)
{
gint zR;
gint zG;
gint zB;
gint zA;
gint index;
guchar* scanline;
scanline = &(pixels[line * width * channels]);
zR = *scanline << zprec;
zG = *(scanline + 1) << zprec;
zB = *(scanline + 2) << zprec;
zA = *(scanline + 3) << zprec;
for (index = 0; index < width; index ++)
_blurinner(&scanline[index * channels], &zR, &zG, &zB, &zA, alpha, aprec,
zprec);
for (index = width - 2; index >= 0; index--)
_blurinner(&scanline[index * channels], &zR, &zG, &zB, &zA, alpha, aprec,
zprec);
}
static inline void _blurcol(guchar* pixels,
gint width,
gint height,
gint channels,
gint x,
gint alpha,
gint aprec,
gint zprec)
{
gint zR;
gint zG;
gint zB;
gint zA;
gint index;
guchar* ptr;
ptr = pixels;
ptr += x * channels;
zR = *((guchar*) ptr ) << zprec;
zG = *((guchar*) ptr + 1) << zprec;
zB = *((guchar*) ptr + 2) << zprec;
zA = *((guchar*) ptr + 3) << zprec;
for (index = width; index < (height - 1) * width; index += width)
_blurinner((guchar*) &ptr[index * channels], &zR, &zG, &zB, &zA, alpha,
aprec, zprec);
for (index = (height - 2) * width; index >= 0; index -= width)
_blurinner((guchar*) &ptr[index * channels], &zR, &zG, &zB, &zA, alpha,
aprec, zprec);
}
//
// pixels image-data
// width image-width
// height image-height
// channels image-channels
//
// in-place blur of image 'img' with kernel of approximate radius 'radius'
//
// blurs with two sided exponential impulse response
//
// aprec = precision of alpha parameter in fixed-point format 0.aprec
//
// zprec = precision of state parameters zR,zG,zB and zA in fp format 8.zprec
//
void _expblur(guchar* pixels,
gint width,
gint height,
gint channels,
gint radius,
gint aprec,
gint zprec)
{
gint alpha;
gint row = 0;
gint col = 0;
if (radius < 1)
return;
// calculate the alpha such that 90% of
// the kernel is within the radius.
// (Kernel extends to infinity)
alpha = (gint) ((1 << aprec) * (1.0f - expf (-2.3f / (radius + 1.f))));
for (; row < height; row++)
_blurrow(pixels, width, height, channels, row, alpha, aprec, zprec);
for(; col < width; col++)
_blurcol(pixels, width, height, channels, col, alpha, aprec, zprec);
return;
}
void Style::Blur(cairo_t* cr, int size)
{
pimpl->Blur(cr, size);
}
void Style::Impl::Blur(cairo_t* cr, int size)
{
// sanity check
if (cairo_status(cr) != CAIRO_STATUS_SUCCESS &&
cairo_surface_get_type(cairo_get_target(cr)) != CAIRO_SURFACE_TYPE_IMAGE)
return;
cairo_surface_t* surface;
guchar* pixels;
guint width;
guint height;
cairo_format_t format;
surface = cairo_get_target(cr);
// before we mess with the surface execute any pending drawing
cairo_surface_flush(surface);
pixels = cairo_image_surface_get_data(surface);
width = cairo_image_surface_get_width(surface);
height = cairo_image_surface_get_height(surface);
format = cairo_image_surface_get_format(surface);
switch (format)
{
case CAIRO_FORMAT_ARGB32:
_expblur(pixels, width, height, 4, size, 16, 7);
break;
case CAIRO_FORMAT_RGB24:
_expblur(pixels, width, height, 3, size, 16, 7);
break;
case CAIRO_FORMAT_A8:
_expblur(pixels, width, height, 1, size, 16, 7);
break;
default :
// do nothing
break;
}
// inform cairo we altered the surfaces contents
cairo_surface_mark_dirty(surface);
}
void Style::Impl::SetDefaultValues()
{
// button-label
button_label_border_color_[nux::VISUAL_STATE_NORMAL] = nux::Color(0.53, 1.0, 0.66, 0.5);
button_label_border_color_[nux::VISUAL_STATE_PRESSED] = nux::Color(1.0, 1.0, 1.0, 0.8);
button_label_border_color_[nux::VISUAL_STATE_PRELIGHT] = nux::Color(0.06, 0.13, 1.0, 0.5);
//button_label_border_color_[nux::NUX_STATE_SELECTED] = nux::Color(0.07, 0.2, 0.33, 0.5);
//button_label_border_color_[nux::NUX_STATE_INSENSITIVE] = nux::Color(0.39, 0.26, 0.12, 0.5);
button_label_border_size_[nux::VISUAL_STATE_NORMAL] = 0.5;
button_label_border_size_[nux::VISUAL_STATE_PRESSED] = 2.0;
button_label_border_size_[nux::VISUAL_STATE_PRELIGHT] = 0.5;
//button_label_border_size_[nux::NUX_STATE_SELECTED] = 0.5;
//button_label_border_size_[nux::NUX_STATE_INSENSITIVE] = 0.5;
button_label_text_size_ = 1.0;
button_label_text_color_[nux::VISUAL_STATE_NORMAL] = nux::color::White;
button_label_text_color_[nux::VISUAL_STATE_PRESSED] = nux::color::Black;
button_label_text_color_[nux::VISUAL_STATE_PRELIGHT] = nux::color::White;
//button_label_text_color_[nux::NUX_STATE_SELECTED] = nux::color::White;
//button_label_text_color_[nux::NUX_STATE_INSENSITIVE] = nux::color::White;
button_label_fill_color_[nux::VISUAL_STATE_NORMAL] = nux::color::Transparent;
button_label_fill_color_[nux::VISUAL_STATE_PRESSED] = nux::color::Transparent;
button_label_fill_color_[nux::VISUAL_STATE_PRELIGHT] = nux::color::Transparent;
//button_label_fill_color_[nux::NUX_STATE_SELECTED] = nux::color::Transparent;
//button_label_fill_color_[nux::NUX_STATE_INSENSITIVE] = nux::color::Transparent;
button_label_overlay_opacity_[nux::VISUAL_STATE_NORMAL] = 0.0;
button_label_overlay_opacity_[nux::VISUAL_STATE_PRESSED] = 0.3;
button_label_overlay_opacity_[nux::VISUAL_STATE_PRELIGHT] = 0.0;
//button_label_overlay_opacity_[nux::NUX_STATE_SELECTED] = 0.0;
//button_label_overlay_opacity_[nux::NUX_STATE_INSENSITIVE] = 0.0;
button_label_overlay_mode_[nux::VISUAL_STATE_NORMAL] = BlendMode::NORMAL;
button_label_overlay_mode_[nux::VISUAL_STATE_PRESSED] = BlendMode::NORMAL;
button_label_overlay_mode_[nux::VISUAL_STATE_PRELIGHT] = BlendMode::NORMAL;
//button_label_overlay_mode_[nux::NUX_STATE_SELECTED] = BlendMode::NORMAL;
//button_label_overlay_mode_[nux::NUX_STATE_INSENSITIVE] = BlendMode::NORMAL;
button_label_blur_size_[nux::VISUAL_STATE_NORMAL] = 0;
button_label_blur_size_[nux::VISUAL_STATE_PRESSED] = 5;
button_label_blur_size_[nux::VISUAL_STATE_PRELIGHT] = 0;
//button_label_blur_size_[nux::NUX_STATE_SELECTED] = 0;
//button_label_blur_size_[nux::NUX_STATE_INSENSITIVE] = 0;
// regular-text
regular_text_color_ = nux::color::White;
regular_text_size_ = 13.0;
regular_text_mode_ = BlendMode::NORMAL;
regular_text_weight_ = FontWeight::LIGHT;
// separator
separator_size_ = 1.0;
separator_color_ = nux::Color(1.0, 1.0, 1.0, 0.15);
separator_overlay_opacity_ = 0.47;
separator_overlay_mode_ = BlendMode::NORMAL;
separator_blur_size_ = 6;
// scrollbar
scrollbar_color_ = nux::color::White;
scrollbar_overlay_opacity_ = 0.3;
scrollbar_overlay_mode_ = BlendMode::NORMAL;
scrollbar_blur_size_ = 5;
scrollbar_size_ = 3;
scrollbar_corner_radius_ = 1.5;
}
void Style::Impl::ArrowPath(cairo_t* cr, Arrow arrow)
{
double x = 0.0;
double y = 0.0;
double w = cairo_image_surface_get_width(cairo_get_target(cr));
double h = cairo_image_surface_get_height(cairo_get_target(cr));
/*double xt = 0.0;
double yt = 0.0;*/
// the real shape from the SVG
// 3.5/1.5, 20.5/22.5, (17x21)
/*xt = 16.25;
yt = 9.028;
cairo_move_to (cr, xt, yt);
cairo_curve_to (cr, xt - 1.511, yt - 1.006, xt - 3.019, yt - 1.971, xt - 4.527, yt - 2.897);
xt -= 4.527;
yt -= 2.897
cairo_curve_to (cr, 10.213, 5.2, 8.743, 4.335, 7.313, 3.532);
cairo_curve_to (cr, 8.743, 4.335, 4.613, 2.051, 3.5, 1.5);
cairo_rel_line_to (cr, 0.0, 21.0);
cairo_rel_curve_to (cr, 1.164, -0.552, 2.461, -1.229, 3.892, -2.032);
cairo_rel_curve_to (cr, 1.431, -0.803, 2.9, -1.682, 4.409, -2.634);
cairo_rel_curve_to (cr, 1.51, -0.953, 3.004, -1.932, 4.488, -2.937);
cairo_rel_curve_to (cr, 1.481, -1.002, 2.887, -1.981, 4.211, -2.935);
cairo_curve_to (cr, 19.176, 11.009, 17.759, 10.03, 16.25, 9.028);
cairo_close_path (cr);*/
if (arrow == Arrow::LEFT || arrow == Arrow::BOTH)
{
x = 1.0;
y = h / 2.0 - 3.5;
cairo_move_to(cr, _align(x), _align(y));
cairo_line_to(cr, _align(x + 5.0), _align(y + 3.5));
cairo_line_to(cr, _align(x), _align(y + 7.0));
cairo_close_path(cr);
}
if (arrow == Arrow::RIGHT || arrow == Arrow::BOTH)
{
x = w - 1.0;
y = h / 2.0 - 3.5;
cairo_move_to(cr, _align(x), _align(y));
cairo_line_to(cr, _align(x - 5.0), _align(y + 3.5));
cairo_line_to(cr, _align(x), _align(y + 7.0));
cairo_close_path(cr);
}
}
void Style::Impl::ButtonOutlinePath (cairo_t* cr, bool align)
{
double x = 2.0;
double y = 2.0;
double w = cairo_image_surface_get_width(cairo_get_target(cr)) - 4.0;
double h = cairo_image_surface_get_height(cairo_get_target(cr)) - 4.0;
double xt = 0.0;
double yt = 0.0;
// - these absolute values are the "cost" of getting only a SVG from design
// and not a generic formular how to approximate the curve-shape, thus
// the smallest possible button-size is 22.18x24.0
double width = w - 22.18;
double height = h - 24.0;
xt = x + width + 22.18;
yt = y + 12.0;
if (align)
{
// top right
cairo_move_to(cr, _align(xt), _align(yt));
cairo_curve_to(cr, _align(xt - 0.103),
_align(yt - 4.355),
_align(xt - 1.037),
_align(yt - 7.444),
_align(xt - 2.811),
_align(yt - 9.267));
xt -= 2.811;
yt -= 9.267;
cairo_curve_to(cr, _align(xt - 1.722),
_align(yt - 1.823),
_align(xt - 4.531),
_align(yt - 2.735),
_align(xt - 8.28),
_align(yt - 2.735));
xt -= 8.28;
yt -= 2.735;
// top
cairo_line_to(cr, _align(xt - width), _align(yt));
xt -= width;
// top left
cairo_curve_to(cr, _align(xt - 3.748),
_align(yt),
_align(xt - 6.507),
_align(yt + 0.912),
_align(xt - 8.279),
_align(yt + 2.735));
xt -= 8.279;
yt += 2.735;
cairo_curve_to(cr, _align(xt - 1.773),
_align(yt + 1.822),
_align(xt - 2.708),
_align(yt + 4.911),
_align(xt - 2.811),
_align(yt + 9.267));
xt -= 2.811;
yt += 9.267;
// left
cairo_line_to(cr, _align(xt), _align(yt + height));
yt += height;
// bottom left
cairo_curve_to(cr, _align(xt + 0.103),
_align(yt + 4.355),
_align(xt + 1.037),
_align(yt + 7.444),
_align(xt + 2.811),
_align(yt + 9.267));
xt += 2.811;
yt += 9.267;
cairo_curve_to(cr, _align(xt + 1.772),
_align(yt + 1.823),
_align(xt + 4.531),
_align(yt + 2.735),
_align(xt + 8.28),
_align(yt + 2.735));
xt += 8.28;
yt += 2.735;
// bottom
cairo_line_to(cr, _align(xt + width), _align(yt));
xt += width;
// bottom right
cairo_curve_to(cr, _align(xt + 3.748),
_align(yt),
_align(xt + 6.507),
_align(yt - 0.912),
_align(xt + 8.279),
_align(yt - 2.735));
xt += 8.279;
yt -= 2.735;
cairo_curve_to(cr, _align(xt + 1.773),
_align(yt - 1.822),
_align(xt + 2.708),
_align(yt - 4.911),
_align(xt + 2.811),
_align(yt - 9.267));
}
else
{
// top right
cairo_move_to(cr, x + width + 22.18, y + 12.0);
cairo_rel_curve_to(cr, -0.103, -4.355, -1.037, -7.444, -2.811, -9.267);
cairo_rel_curve_to(cr, -1.722, -1.823, -4.531, -2.735, -8.28, -2.735);
// top
cairo_rel_line_to(cr, -width, 0.0);
// top left
cairo_rel_curve_to(cr, -3.748, 0.0, -6.507, 0.912, -8.279, 2.735);
cairo_rel_curve_to(cr, -1.773, 1.822, -2.708, 4.911, -2.811, 9.267);
// left
cairo_rel_line_to(cr, 0.0, height);
// bottom left
cairo_rel_curve_to(cr, 0.103, 4.355, 1.037, 7.444, 2.811, 9.267);
cairo_rel_curve_to(cr, 1.772, 1.823, 4.531, 2.735, 8.28, 2.735);
// bottom
cairo_rel_line_to(cr, width, 0.0);
// bottom right
cairo_rel_curve_to(cr, 3.748, 0.0, 6.507, -0.912, 8.279, -2.735);
cairo_rel_curve_to(cr, 1.773, -1.822, 2.708, -4.911, 2.811, -9.267);
}
// right
cairo_close_path(cr);
}
void Style::Impl::RoundedRectSegment(cairo_t* cr,
double aspect,
double x,
double y,
double cornerRadius,
double width,
double height,
Segment segment,
Arrow arrow,
nux::ButtonVisualState state)
{
double radius = cornerRadius / aspect;
double arrow_w = radius / 1.5;
double arrow_h = radius / 2.0;
bool odd = true;
odd = cairo_get_line_width (cr) == 2.0 ? false : true;
switch (segment)
{
case Segment::LEFT:
// top-left, right of the corner
cairo_move_to(cr, _align(x + radius, odd), _align(y, odd));
// top-right
cairo_line_to(cr, _align(x + width, odd), _align(y, odd));
if (arrow == Arrow::RIGHT && state == nux::VISUAL_STATE_PRESSED)
{
cairo_line_to(cr, _align(x + width, odd), _align(y + height / 2.0 - arrow_h, odd));
cairo_line_to(cr, _align(x + width - arrow_w, odd), _align(y + height / 2.0, odd));
cairo_line_to(cr, _align(x + width, odd), _align(y + height / 2.0 + arrow_h, odd));
}
// bottom-right
cairo_line_to(cr, _align(x + width, odd), _align(y + height, odd));