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
|
using Gdk;
using Gtk;
namespace Dino.Ui {
class ScalingImage : Image {
public int min_width { get; set; default = -1; }
public int target_width { get; set; default = -1; }
public int max_width { get; set; default = -1; }
public int min_height { get; set; default = -1; }
public int max_height { get; set; default = -1; }
private Pixbuf image;
private double image_ratio;
private int image_height = 0;
private int image_width = 0;
private int last_allocation_height = -1;
private int last_allocation_width = -1;
private int last_scale_factor = -1;
public void load(Pixbuf image) {
this.image = image;
this.image_ratio = ((double)image.height) / ((double)image.width);
this.image_height = image.height;
this.image_width = image.width;
queue_resize();
}
private void calculate_size(ref double exact_width, ref double exact_height) {
if (exact_width == -1 && exact_height == -1) {
if (target_width == -1) {
exact_width = ((double)image_width) / ((double)scale_factor);
exact_height = ((double)image_height) / ((double)scale_factor);
} else {
exact_width = target_width;
exact_height = exact_width * image_ratio;
}
} else if (exact_width != -1) {
exact_height = exact_width * image_ratio;
} else if (exact_height != -1) {
exact_width = exact_height / image_ratio;
} else {
if (exact_width * image_ratio > exact_height + scale_factor) {
exact_width = exact_height / image_ratio;
} else if (exact_height / image_ratio > exact_width + scale_factor) {
exact_height = exact_width * image_ratio;
}
}
if (max_width != -1 && exact_width > max_width) {
exact_width = max_width;
exact_height = exact_width * image_ratio;
}
if (max_height != -1 && exact_height > max_height) {
exact_height = max_height;
exact_width = exact_height / image_ratio;
}
if (exact_width < min_width) exact_width = min_width;
if (exact_height < min_height) exact_height = min_height;
}
public override void size_allocate(Allocation allocation) {
if (max_width != -1) allocation.width = int.min(allocation.width, max_width);
if (max_height != -1) allocation.height = int.min(allocation.height, max_height);
allocation.height = int.max(allocation.height, min_height);
allocation.width = int.max(allocation.width, min_width);
double exact_width = allocation.width, exact_height = allocation.height;
calculate_size(ref exact_width, ref exact_height);
base.size_allocate(allocation);
if (last_allocation_height != allocation.height || last_allocation_width != allocation.width || last_scale_factor != scale_factor) {
last_allocation_height = allocation.height;
last_allocation_width = allocation.width;
last_scale_factor = scale_factor;
Pixbuf scaled = image.scale_simple((int) Math.floor(exact_width * scale_factor), (int) Math.floor(exact_height * scale_factor), Gdk.InterpType.BILINEAR);
scaled = crop_corners(scaled, 3 * scale_factor);
Util.image_set_from_scaled_pixbuf(this, scaled);
}
}
private static Gdk.Pixbuf crop_corners(Gdk.Pixbuf pixbuf, double radius = 3) {
Cairo.Context ctx = new Cairo.Context(new Cairo.ImageSurface(Cairo.Format.ARGB32, pixbuf.width, pixbuf.height));
Gdk.cairo_set_source_pixbuf(ctx, pixbuf, 0, 0);
double degrees = Math.PI / 180.0;
ctx.new_sub_path();
ctx.arc(pixbuf.width - radius, radius, radius, -90 * degrees, 0 * degrees);
ctx.arc(pixbuf.width - radius, pixbuf.height - radius, radius, 0 * degrees, 90 * degrees);
ctx.arc(radius, pixbuf.height - radius, radius, 90 * degrees, 180 * degrees);
ctx.arc(radius, radius, radius, 180 * degrees, 270 * degrees);
ctx.close_path();
ctx.clip();
ctx.paint();
return Gdk.pixbuf_get_from_surface(ctx.get_target(), 0, 0, pixbuf.width, pixbuf.height);
}
public override void get_preferred_width(out int minimum_width, out int natural_width) {
minimum_width = int.max(0, min_width);
double exact_width = -1, exact_height = -1;
calculate_size(ref exact_width, ref exact_height);
natural_width = (int) Math.ceil(exact_width);
}
public override void get_preferred_height(out int minimum_height, out int natural_height) {
minimum_height = int.max(0, min_height);
double exact_width = -1, exact_height = -1;
calculate_size(ref exact_width, ref exact_height);
natural_height = (int) Math.ceil(exact_height);
}
public override void get_preferred_height_for_width(int width, out int minimum_height, out int natural_height) {
double exact_width = width, exact_height = -1;
calculate_size(ref exact_width, ref exact_height);
natural_height = (int) Math.ceil(exact_height);
minimum_height = natural_height;
}
public override void get_preferred_width_for_height(int height, out int minimum_width, out int natural_width) {
double exact_width = -1, exact_height = height;
calculate_size(ref exact_width, ref exact_height);
natural_width = (int) Math.ceil(exact_width);
minimum_width = natural_width;
}
public override SizeRequestMode get_request_mode() {
return SizeRequestMode.HEIGHT_FOR_WIDTH;
}
}
}
|