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,
27 float left_margin, 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
45 if (job_params->print_at_scale) {
46 top_margin = left_margin = right_margin = bottom_margin = 0.0f;
47 }
48
49 // don't adjust for margins if job is PCLm. dimensions of image will not
50 // match (will be bigger than) the dimensions of the page size and a corrupt image will render
51 // in genPCLm
52 if (job_params->pcl_type == PCLm) {
53 if (job_params->borderless) {
54 job_params->printable_area_width = (unsigned int) _MI_TO_PIXELS(
55 job_params->page_width * 1000, job_params->pixel_units);
56 job_params->printable_area_height = (unsigned int) _MI_TO_PIXELS(
57 job_params->page_height * 1000, job_params->pixel_units);
58 } else {
59 job_params->printable_area_width =
60 (unsigned int) _MI_TO_PIXELS(job_params->page_width * 1000,
61 job_params->pixel_units)
62 - floorf(left_margin * (float) job_params->pixel_units)
63 - floorf(right_margin * (float) job_params->pixel_units);
64 job_params->printable_area_height =
65 (unsigned int) _MI_TO_PIXELS(job_params->page_height * 1000,
66 job_params->pixel_units)
67 - floorf(top_margin * (float) job_params->pixel_units)
68 - floorf(bottom_margin * (float) job_params->pixel_units);
69 }
70 } else {
71 job_params->printable_area_width = (unsigned int) floorf(((job_params->page_width -
72 (left_margin + right_margin)) * (float)job_params->pixel_units));
73 job_params->printable_area_height = (unsigned int) floorf(((job_params->page_height -
74 (top_margin + bottom_margin)) * (float)job_params->pixel_units));
75 }
76
77 job_params->page_top_margin = top_margin;
78 job_params->page_left_margin = left_margin;
79 job_params->page_right_margin = right_margin;
80 job_params->page_bottom_margin = bottom_margin;
81
82 if (!job_params->borderless) {
83 if (job_params->job_top_margin > top_margin) {
84 job_params->print_top_margin = floorf(
85 ((job_params->job_top_margin - top_margin) * (float) job_params->pixel_units));
86 } else {
87 job_params->print_top_margin = floorf(((top_margin) * (float) job_params->pixel_units));
88 }
89 if (job_params->job_left_margin > left_margin) {
90 job_params->print_left_margin = floorf(((job_params->job_left_margin - left_margin) *
91 (float) job_params->pixel_units));
92 } else {
93 job_params->print_left_margin = floorf(
94 ((left_margin) * (float) job_params->pixel_units));
95 }
96 if (job_params->job_right_margin > right_margin) {
97 job_params->print_right_margin = floorf(((job_params->job_right_margin - right_margin) *
98 (float) job_params->pixel_units));
99 } else {
100 job_params->print_right_margin = floorf(
101 ((right_margin) * (float) job_params->pixel_units));
102 }
103 if (job_params->job_bottom_margin > bottom_margin) {
104 job_params->print_bottom_margin = floorf(
105 ((job_params->job_bottom_margin - bottom_margin) *
106 (float) job_params->pixel_units));
107 } else {
108 job_params->print_bottom_margin = floorf(
109 ((bottom_margin) * (float) job_params->pixel_units));
110 }
111 }
112
113 job_params->width = (job_params->printable_area_width -
114 (job_params->print_left_margin + job_params->print_right_margin));
115 job_params->height = (job_params->printable_area_height -
116 (job_params->print_top_margin + job_params->print_bottom_margin));
117
118 LOGD("printable_area_get(): source dimensions: %fx%f",
119 job_params->source_width, job_params->source_height);
120 LOGD("printable_area_get(): page dimensions: %fx%f",
121 job_params->page_width, job_params->page_height);
122 }
123
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)124 void printable_area_get_default_margins(const wprint_job_params_t *job_params,
125 const printer_capabilities_t *printer_cap,
126 float *top_margin,
127 float *left_margin, float *right_margin,
128 float *bottom_margin) {
129 if ((job_params == NULL) || (printer_cap == NULL)) {
130 return;
131 }
132
133 bool useDefaultMargins = true;
134
135 if (job_params->borderless) {
136 useDefaultMargins = false;
137 switch (job_params->pcl_type) {
138 case PCLm:
139 case PCLPWG:
140 *top_margin = 0.0f;
141 *left_margin = 0.0f;
142 *right_margin = 0.0f;
143 *bottom_margin = 0.00f;
144 break;
145 default:
146 *top_margin = -0.065f;
147 *left_margin = -0.10f;
148 *right_margin = -0.118f;
149 *bottom_margin = -0.10f;
150 break;
151 }
152 } else {
153 switch (job_params->pcl_type) {
154 case PCLm:
155 case PCLPWG:
156 *top_margin = (float) printer_cap->printerTopMargin / 2540;
157 *bottom_margin = (float) printer_cap->printerBottomMargin / 2540;
158 *left_margin = (float) printer_cap->printerLeftMargin / 2540;
159 *right_margin = (float) printer_cap->printerRightMargin / 2540;
160 useDefaultMargins = false;
161 break;
162 default:
163 break;
164 }
165 }
166
167 if (useDefaultMargins) {
168 if (!printer_cap->inkjet) {
169 // default laser margins
170 *top_margin = 0.2f;
171 *left_margin = 0.25f;
172 *right_margin = 0.25f;
173 *bottom_margin = 0.2f;
174 } else {
175 // default inkjet margins
176 *top_margin = 0.125f;
177 *left_margin = 0.125f;
178 *right_margin = 0.125f;
179 if ((job_params->duplex != DUPLEX_MODE_NONE) || !printer_cap->borderless) {
180 *bottom_margin = 0.5f;
181 } else {
182 *bottom_margin = 0.125f;
183 }
184 }
185 }
186
187 LOGD("printable_area_get_default_margins(): top_margin=%f, left_margin=%f, "
188 "right_margin=%f, bottom_margin=%f", *top_margin, *left_margin, *right_margin,
189 *bottom_margin);
190 }