1. Changed license year
[apps/home/mobileprint.git] / mobileprint / previewgen / lib / paper_size.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 #include <string.h>
22
23 #include "pgen_debug.h"
24
25 #include "paper_size.h"
26
27
28 /* could be used as stub for paper size */
29 struct paper_size_pts paper_sizes[] = {
30         {"L", {890,1270}},
31         {"2L", {1270,1780}},
32         {"Hagaki_Postcard", {1000,1480}},
33         {"Business_card", {540,856}},
34         {"4x6", {1016,1524}},
35         {"8x10", {2032,2540}},
36         {"Letter", { 612, 792 }},
37         {"11x17", {2794,4318}},
38         {"A4", { 595, 842 }},
39         {"A5", { 420, 595 }},
40         {"B5", { 516, 729 }},
41         {NULL, { 0, 0 }}
42 };
43
44
45 void switch_size_pts_coords(struct size_pts *s)
46 {
47         PGEN_RET_IF(s == NULL, "Invalid argument");
48         double val;
49         val = s->x;
50         s->x = s->y;
51         s->y = val;
52 }
53
54 void switch_size_px_coords(struct size_px *s)
55 {
56         PGEN_RET_IF(s == NULL, "Invalid argument");
57         int val;
58         val = s->x;
59         s->x = s->y;
60         s->y = val;
61 }
62
63 void add_size_pts(const struct size_pts *from, struct size_pts *to)
64 {
65         PGEN_RET_IF(from == NULL || to == NULL, "Invalid argument");
66         to->x += from->x;
67         to->y += from->y;
68 }
69
70
71 void neg_size_pts(struct size_pts *s)
72 {
73         PGEN_RET_IF(s == NULL, "Invalid argument");
74         s->x = -s->x;
75         s->y = -s->y;
76 }
77
78
79 /**
80  * @brief      Works with fixed list of sizes!
81  *              In mobileprint only for usage as stub.
82  * @param[in]  name    paper name
83  * @param[out] s       paper size
84  */
85 int get_paper_size_pts(const char *name, struct paper_size_pts *s)
86 {
87         int i;
88         struct paper_size_pts *cur_s;
89
90         PGEN_RETV_IF(name == NULL || s == NULL, -1, "Invalid argument");
91
92         for (i = 0; paper_sizes[i].name != NULL; ++i) {
93                 cur_s = &(paper_sizes[i]);
94                 /* call without of limit is safe till we have static page
95                    sizes */
96                 if (strcmp(cur_s->name, name) == 0) {
97                         //memcpy(s, cur_s, sizeof(*cur_s));
98                         *s = *cur_s;
99                         return 0;
100                 }
101         }
102
103         return 1;
104 }
105
106
107 /**
108  * @brief                       get raster size for specifyed borders
109  * @param[in]   s_pts           size in points
110  * @param[in]   border_s_px     specified borders of available size
111  * @param[out]  px              raster size
112  */
113 int pts_size2px(const struct size_pts *s_pts,
114                                 const struct size_px *border_s_px,
115                                 struct size_px *px)
116 {
117         PGEN_TRACE_BEGIN;
118
119         double ratio_x;
120         double ratio_y;
121
122         PGEN_RETV_IF(s_pts == NULL || border_s_px == NULL || px == NULL, -1 , "Invalid argument");
123
124         ratio_x = border_s_px->x / s_pts->x;
125         ratio_y = border_s_px->y / s_pts->y;
126
127         /* get smallest one */
128         if (ratio_x > ratio_y) {
129                 ratio_x = ratio_y;
130         } else {
131                 ratio_y = ratio_x;
132         }
133
134         /* result */
135         px->x = ratio_x * s_pts->x;
136         px->y = ratio_y * s_pts->y;
137
138         PGEN_DEBUG("px->x px->y :(%d, %d)",px->x, px->y);
139         PGEN_TRACE_END;
140         return 0;
141 }
142