.
1 /*
2 Copyright (C) 2015 Johan Mattsson
3
4 This library is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 3 of the
7 License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13 */
14
15 using Cairo;
16 using Math;
17 using Gee;
18
19 namespace BirdFont {
20
21 public class ScaledBackgrounds : GLib.Object {
22 ImageSurface original;
23 ArrayList<ScaledBackground> scaled;
24
25 public ScaledBackgrounds (ImageSurface original) {
26 this.original = original;
27 scaled = new ArrayList<ScaledBackground> ();
28
29 ScaledBackground image = scale (0.01);
30 scaled.add (image);
31
32 for (double scale_factor = 0.1; scale_factor <= 1; scale_factor += 0.1) {
33 image = scale (scale_factor);
34 scaled.add (image);
35 }
36 }
37
38 public ScaledBackground get_image (double scale) {
39 foreach (ScaledBackground image in scaled) {
40 if (image.get_scale () < scale) {
41 continue;
42 }
43
44 return image;
45 }
46
47 return scaled.get (scaled.size - 1);
48 }
49
50 private ScaledBackground scale (double scale_factor) {
51 ImageSurface scaled_image;
52
53 if (scale_factor <= 0) {
54 warning ("scale_factor <= 0");
55 scale_factor = 1;
56 }
57
58 int width = (int) (original.get_width () * scale_factor);
59 int height = (int) (original.get_height () * scale_factor);
60
61 scaled_image = new ImageSurface (Format.ARGB32, width, height);
62 Context context = new Context (scaled_image);
63 context.scale (scale_factor, scale_factor);
64 context.set_source_surface (original, 0, 0);
65 context.paint ();
66
67 return new ScaledBackground (scaled_image, scale_factor);
68 }
69 }
70
71 }
72