1. Changed license year
[apps/home/mobileprint.git] / mobileprint / previewgen / lib / preview_coords.c
1 /*
2 *  Mobileprint
3 *
4 * Copyright 2012  Samsung Electronics Co., Ltd
5
6 * Licensed under the Flora License, Version 1.1 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9
10 * http://floralicense.org/license/
11
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20 #include <stdlib.h>
21
22 #include "paper_size.h"
23 #include "pgen_debug.h"
24
25 #include "preview_coords.h"
26
27
28 void debug_print_preview_page_req(const struct preview_page_req *r)
29 {
30         PGEN_DEBUG("+-----------------------------+");
31         PGEN_DEBUG("| struct preview_page_req     |");
32         PGEN_DEBUG("+---------------------+-------+");
33         PGEN_DEBUG("| paper_size.x        | %5f |", r->paper_size.x);
34         PGEN_DEBUG("| paper_size.y        | %5f |", r->paper_size.y);
35         PGEN_DEBUG("| available_size_px.x | %5d |", r->available_size_px.x);
36         PGEN_DEBUG("| available_size_px.y | %5d |", r->available_size_px.y);
37         PGEN_DEBUG("| shadow_offset.x     | %5d |", r->shadow_offset.x);
38         PGEN_DEBUG("| shadow_offset.y     | %5d |", r->shadow_offset.y);
39         PGEN_DEBUG("| is_rotate90         | %5d |", r->is_rotate90);
40         PGEN_DEBUG("+---------------------+-------+");
41 }
42
43
44 void debug_print_preview_page_px(const struct preview_page_px *s)
45 {
46         PGEN_DEBUG("+------------------------+");
47         PGEN_DEBUG("| struct preview_page_px |");
48         PGEN_DEBUG("+---------------+--------+");
49         PGEN_DEBUG("| full_size.x   | %6d |", s->full_size.x);
50         PGEN_DEBUG("| full_size.y   | %6d |", s->full_size.y);
51         PGEN_DEBUG("| paper_size.x  | %6d |", s->paper_size.x);
52         PGEN_DEBUG("| paper_size.y  | %6d |", s->paper_size.y);
53         PGEN_DEBUG("| area_size.x   | %6d |", s->area_size.x);
54         PGEN_DEBUG("| area_size.y   | %6d |", s->area_size.y);
55         PGEN_DEBUG("| area_offset.x | %6d |", s->area_offset.x);
56         PGEN_DEBUG("| area_offset.y | %6d |", s->area_offset.y);
57         PGEN_DEBUG("| is_grayscale  | %6d |", s->is_grayscale);
58         PGEN_DEBUG("| is_rotate90   | %6d |", s->is_rotate90);
59         PGEN_DEBUG("+---------------+--------+");
60 }
61
62
63 void debug_print_preview_settings_px(const struct preview_settings_px *s)
64 {
65         PGEN_DEBUG("+----------------------------+");
66         PGEN_DEBUG("| struct preview_settings_px |");
67         PGEN_DEBUG("+-------------+--------------+");
68         PGEN_DEBUG("| center_off  | %9d    |", s->center_off);
69         PGEN_DEBUG("| left_off    | %9d    |", s->left_off);
70         PGEN_DEBUG("| right_off   | %9d    |", s->right_off);
71         PGEN_DEBUG("| full_size.x | %9d    |", s->full_size.x);
72         PGEN_DEBUG("| full_size.y | %9d    |", s->full_size.y);
73         PGEN_DEBUG("+-------------+--------------+");
74 }
75
76
77 int process_preview_page_settings(const struct preview_page_req *req,
78                                                                   struct preview_page_px *res)
79 {
80         PGEN_RETV_IF(req == NULL || res == NULL, -1, "Invalid argument");
81
82         /*debug_print_preview_page_req(req);*/
83
84         if (pts_size2px(&(req->paper_size), &(req->available_size_px),
85                                         &(res->full_size)) < 0) {
86                 PGEN_DEBUG("ERROR: pts_size2px()\n");
87                 return -1;
88         }
89
90         res->paper_size.x = res->full_size.x - req->shadow_offset.x;
91         res->paper_size.y = res->full_size.y - req->shadow_offset.y;
92
93         res->area_offset.x = 2;
94         res->area_offset.y = 2;
95
96         res->area_size.x = res->paper_size.x - res->area_offset.x * 2;
97         res->area_size.y = res->paper_size.y - res->area_offset.y * 2;
98
99         res->is_grayscale = req->is_grayscale;
100         res->is_rotate90 = req->is_rotate90;
101
102         /*debug_print_preview_page_px(res);*/
103
104         return 0;
105 }
106
107 /* for backward compatibility */
108 int get_preview_page_settings(const struct preview_page_req *req,
109                                                           struct preview_page_px *res)
110 {
111         struct preview_page_req newreq;
112
113         PGEN_RETV_IF(req == NULL || res == NULL, -1, "Invalid argument");
114
115         newreq = *req;
116
117         if (req->is_rotate90) {
118                 switch_size_pts_coords(&(newreq.paper_size));
119         }
120
121         return process_preview_page_settings(&newreq, res);
122 }
123
124
125 int get_preview_settings(const struct preview_page_req *req,
126                                                  struct preview_settings_px *res)
127 {
128         struct size_pts canvas_size;
129         double side_w_pts;
130         double space_w_pts;
131         struct preview_page_req newreq;
132
133         PGEN_RETV_IF(req == NULL || res == NULL, -1, "Invalid argument");
134
135         /*debug_print_preview_page_req(req);*/
136
137         newreq = *req;
138
139         if (req->is_rotate90) {
140                 switch_size_pts_coords(&(newreq.paper_size));
141         }
142
143         /* calculate space for page images */
144         /* we will use smaller side of page */
145         side_w_pts = req->paper_size.x * PAGE_SIDE_SHOW_COEFF;
146         space_w_pts = req->paper_size.x * MIN_PAGE_SPACE_COEFF;
147         canvas_size.y = newreq.paper_size.y;
148         canvas_size.x = newreq.paper_size.x + side_w_pts * 2 + space_w_pts * 2;
149
150         if (pts_size2px(&canvas_size, &(req->available_size_px),
151                                         &(newreq.available_size_px)) < 0) {
152                 PGEN_DEBUG("ERROR: full_size: pts_size2px()\n");
153                 return -1;
154         }
155
156         /* calculate single-page size */
157         //newreq.available_size_px = res->full_size;
158         process_preview_page_settings(&newreq, &(res->page_px));
159
160         res->full_size.x = req->available_size_px.x;
161         res->full_size.y = newreq.available_size_px.y;
162
163         /* calculate offsets */
164         res->center_off = res->full_size.x / 2 - res->page_px.full_size.x / 2;
165         res->left_off = res->page_px.full_size.x * (PAGE_SIDE_SHOW_COEFF - 1);
166         res->right_off = res->full_size.x
167                                          - res->page_px.full_size.x * PAGE_SIDE_SHOW_COEFF;
168
169         /*debug_print_preview_settings_px(res);*/
170
171         return 0;
172 }
173