1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  * Copyright (C) 2016 Mopria Alliance, Inc.
4  * Copyright (C) 2013 Hewlett-Packard Development Company, L.P.
5  *
6  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 #include <math.h>
20 #include "lib_printable_area.h"
21 #include "wprint_debug.h"
22 #include "../plugins/media.h"
23 
24 #define TAG "printable_area"
25 
printable_area_get(wprint_job_params_t * job_params,float top_margin,float left_margin,float right_margin,float bottom_margin)26 void printable_area_get(wprint_job_params_t *job_params, float top_margin, float left_margin,
27         float right_margin, float bottom_margin) {
28     if (job_params == NULL) return;
29 
30     job_params->printable_area_width = job_params->printable_area_height = 0.0f;
31     job_params->width = job_params->height = 0.0f;
32     job_params->page_top_margin = job_params->page_bottom_margin = 0.0f;
33     job_params->page_right_margin = job_params->page_left_margin = 0.0f;
34 
35     job_params->page_width = 0.0f;
36     job_params->page_height = 0.0f;
37     int i;
38     for (i = 0; i < SUPPORTED_MEDIA_SIZE_COUNT; i++) {
39         if (job_params->media_size == SupportedMediaSizes[i].media_size) {
40             job_params->page_width = SupportedMediaSizes[i].WidthInInches / 1000;
41             job_params->page_height = SupportedMediaSizes[i].HeightInInches / 1000;
42         }
43     }
44     // don't adjust for margins if job is borderless and PCLm.  dimensions of image will not
45     // match (will be bigger than) the dimensions of the page size and a corrupt image will render
46     // in genPCLm
47     if (job_params->borderless && job_params->pcl_type == PCLm) {
48         job_params->printable_area_width = (unsigned int) _MI_TO_PIXELS(
49                 job_params->page_width * 1000, job_params->pixel_units);
50         job_params->printable_area_height = (unsigned int) _MI_TO_PIXELS(
51                 job_params->page_height * 1000, job_params->pixel_units);
52     } else {
53         job_params->printable_area_width = (unsigned int) floorf(((job_params->page_width -
54                 (left_margin + right_margin)) * (float)job_params->pixel_units));
55         job_params->printable_area_height =  (unsigned int) floorf(((job_params->page_height -
56                 (top_margin + bottom_margin)) * (float)job_params->pixel_units));
57     }
58 
59     job_params->page_top_margin = top_margin;
60     job_params->page_left_margin = left_margin;
61     job_params->page_right_margin = right_margin;
62     job_params->page_bottom_margin = bottom_margin;
63 
64     if (!job_params->borderless) {
65         if (job_params->job_top_margin > top_margin) {
66             job_params->print_top_margin = floorf(
67                     ((job_params->job_top_margin - top_margin) * (float) job_params->pixel_units));
68         } else {
69             job_params->print_top_margin = floorf(((top_margin) * (float) job_params->pixel_units));
70         }
71         if (job_params->job_left_margin > left_margin) {
72             job_params->print_left_margin = floorf(((job_params->job_left_margin - left_margin) *
73                     (float) job_params->pixel_units));
74         } else {
75             job_params->print_left_margin = floorf(
76                     ((left_margin) * (float) job_params->pixel_units));
77         }
78         if (job_params->job_right_margin > right_margin) {
79             job_params->print_right_margin = floorf(((job_params->job_right_margin - right_margin) *
80                     (float) job_params->pixel_units));
81         } else {
82             job_params->print_right_margin = floorf(
83                     ((right_margin) * (float) job_params->pixel_units));
84         }
85         if (job_params->job_bottom_margin > bottom_margin) {
86             job_params->print_bottom_margin = floorf(
87                     ((job_params->job_bottom_margin - bottom_margin) *
88                             (float) job_params->pixel_units));
89         } else {
90             job_params->print_bottom_margin = floorf(
91                     ((bottom_margin) * (float) job_params->pixel_units));
92         }
93     }
94 
95     job_params->width = (job_params->printable_area_width -
96             (job_params->print_left_margin + job_params->print_right_margin));
97     job_params->height = (job_params->printable_area_height -
98             (job_params->print_top_margin + job_params->print_bottom_margin));
99 }
100 
printable_area_get_default_margins(const wprint_job_params_t * job_params,const printer_capabilities_t * printer_cap,float * top_margin,float * left_margin,float * right_margin,float * bottom_margin)101 void printable_area_get_default_margins(const wprint_job_params_t *job_params,
102         const printer_capabilities_t *printer_cap,
103         float *top_margin,
104         float *left_margin, float *right_margin,
105         float *bottom_margin) {
106     if ((job_params == NULL) || (printer_cap == NULL)) {
107         return;
108     }
109 
110     bool useDefaultMargins = true;
111 
112     if (job_params->borderless) {
113         useDefaultMargins = false;
114         switch (job_params->pcl_type) {
115             case PCLm:
116             case PCLPWG:
117                 *top_margin = 0.0f;
118                 *left_margin = 0.0f;
119                 *right_margin = 0.0f;
120                 *bottom_margin = 0.00f;
121                 break;
122             default:
123                 *top_margin = -0.065f;
124                 *left_margin = -0.10f;
125                 *right_margin = -0.118f;
126                 *bottom_margin = -0.10f;
127                 break;
128         }
129     } else {
130         switch (job_params->pcl_type) {
131             case PCLm:
132                 *top_margin = (float) printer_cap->printerTopMargin / 2540;
133                 *bottom_margin = (float) printer_cap->printerBottomMargin / 2540;
134                 *left_margin = (float) printer_cap->printerLeftMargin / 2540;
135                 *right_margin = (float) printer_cap->printerRightMargin / 2540;
136                 useDefaultMargins = false;
137                 break;
138             case PCLPWG:
139                 *top_margin = 0.0f;
140                 *left_margin = 0.0f;
141                 *right_margin = 0.0f;
142                 *bottom_margin = 0.00f;
143                 useDefaultMargins = false;
144                 break;
145             default:
146                 break;
147         }
148     }
149 
150     if (useDefaultMargins) {
151         if (!printer_cap->inkjet) {
152             // default laser margins
153             *top_margin = 0.2f;
154             *left_margin = 0.25f;
155             *right_margin = 0.25f;
156             *bottom_margin = 0.2f;
157         } else {
158             // default inkjet margins
159             *top_margin = 0.125f;
160             *left_margin = 0.125f;
161             *right_margin = 0.125f;
162             if ((job_params->duplex != DUPLEX_MODE_NONE) || !printer_cap->borderless) {
163                 *bottom_margin = 0.5f;
164             } else {
165                 *bottom_margin = 0.125f;
166             }
167         }
168     }
169 
170     LOGD("printable_area_get_default_margins(): top_margin=%f, left_margin=%f, "
171             "right_margin=%f, bottom_margin=%f", *top_margin, *left_margin, *right_margin,
172             *bottom_margin);
173 }