| 1 |
/* DocumentThumbnail.vala |
| 2 |
* |
| 3 |
* Copyright 2023-2024 Diego Iván <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 19 |
*/ |
| 20 |
|
| 21 |
public class PaperClip.DocumentThumbnail : Adw.Bin { |
| 22 |
private Gtk.Image thumbnail_image = new Gtk.Image (); |
| 23 |
private Services.Thumbnailer thumbnailer = Services.Thumbnailer.get_default (); |
| 24 |
|
| 25 |
const int MAX_SIZE = 350; |
| 26 |
|
| 27 |
private unowned Document _document; |
| 28 |
public unowned Document document { |
| 29 |
get { |
| 30 |
return _document; |
| 31 |
} |
| 32 |
set { |
| 33 |
_document = value; |
| 34 |
generate_png.begin (); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
~DocumentThumbnail () { |
| 39 |
thumbnail_image.clear (); |
| 40 |
} |
| 41 |
|
| 42 |
construct { |
| 43 |
child = thumbnail_image; |
| 44 |
thumbnail_image.add_css_class ("icon-dropshadow"); |
| 45 |
thumbnailer.max_size = MAX_SIZE; |
| 46 |
thumbnail_image.pixel_size = MAX_SIZE / 2; |
| 47 |
} |
| 48 |
|
| 49 |
private async void generate_png () { |
| 50 |
try { |
| 51 |
Poppler.Page page = document.get_page_for_index (0); |
| 52 |
string basename = document.original_file.get_basename (); |
| 53 |
thumbnail_image.clear (); |
| 54 |
Gdk.Texture thumbnail_texture = yield thumbnailer.create_thumbnail (page, basename); |
| 55 |
thumbnail_image.paintable = scale_thumbnail (thumbnail_texture); |
| 56 |
} |
| 57 |
catch (Error e) { |
| 58 |
critical (e.message); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
private Gdk.Paintable scale_thumbnail (Gdk.Texture thumbnail_texture) { |
| 63 |
float scaled_height, scaled_width; |
| 64 |
compute_scaled_size (thumbnail_texture.height, thumbnail_texture.width, |
| 65 |
out scaled_height, out scaled_width); |
| 66 |
|
| 67 |
var snapshot = new Gtk.Snapshot (); |
| 68 |
var thumbnail_rectangle = Graphene.Rect () { |
| 69 |
origin = { 0, 0 }, |
| 70 |
size = { Math.floorf (scaled_width), Math.floorf (scaled_height) } |
| 71 |
}; |
| 72 |
|
| 73 |
Gsk.ScalingFilter filter = LINEAR; |
| 74 |
if (scaled_width > thumbnail_texture.width || scaled_height > thumbnail_texture.height) { |
| 75 |
filter = NEAREST; |
| 76 |
} else { |
| 77 |
filter = TRILINEAR; |
| 78 |
} |
| 79 |
debug (@"Current filter for thumbnail: $filter"); |
| 80 |
|
| 81 |
// Append White Background in case the image is transparent |
| 82 |
snapshot.append_color ({1, 1, 1, 1}, thumbnail_rectangle); |
| 83 |
snapshot.append_scaled_texture (thumbnail_texture, filter, thumbnail_rectangle); |
| 84 |
|
| 85 |
return snapshot.to_paintable (null); |
| 86 |
} |
| 87 |
|
| 88 |
private void compute_scaled_size (int image_height, int image_width, out float scaled_height, |
| 89 |
out float scaled_width) { |
| 90 |
float size = MAX_SIZE * scale_factor; |
| 91 |
|
| 92 |
// Translated this from AdwAvatar source code. Works nicely :) |
| 93 |
if (image_width > image_height) { |
| 94 |
scaled_height = size; |
| 95 |
scaled_width = image_width * scaled_height / image_height; |
| 96 |
} else if (image_height > image_width) { |
| 97 |
scaled_width = size; |
| 98 |
scaled_height = image_height * scaled_width / image_width; |
| 99 |
} else { |
| 100 |
scaled_width = scaled_height = size; |
| 101 |
} |
| 102 |
} |
| 103 |
} |