Home | History | Annotate | Download | only in lib
      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 
     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     job_params->printable_area_width = floorf(
     48             ((job_params->page_width - (left_margin + right_margin)) *
     49                     (float) job_params->pixel_units));
     50     job_params->printable_area_height = floorf(
     51             ((job_params->page_height - (top_margin + bottom_margin)) *
     52                     (float) job_params->pixel_units));
     53 
     54     job_params->page_top_margin = top_margin;
     55     job_params->page_left_margin = left_margin;
     56     job_params->page_right_margin = right_margin;
     57     job_params->page_bottom_margin = bottom_margin;
     58 
     59     if (!job_params->borderless) {
     60         if (job_params->job_top_margin > top_margin) {
     61             job_params->print_top_margin = floorf(
     62                     ((job_params->job_top_margin - top_margin) * (float) job_params->pixel_units));
     63         } else {
     64             job_params->print_top_margin = floorf(((top_margin) * (float) job_params->pixel_units));
     65         }
     66         if (job_params->job_left_margin > left_margin) {
     67             job_params->print_left_margin = floorf(((job_params->job_left_margin - left_margin) *
     68                     (float) job_params->pixel_units));
     69         } else {
     70             job_params->print_left_margin = floorf(
     71                     ((left_margin) * (float) job_params->pixel_units));
     72         }
     73         if (job_params->job_right_margin > right_margin) {
     74             job_params->print_right_margin = floorf(((job_params->job_right_margin - right_margin) *
     75                     (float) job_params->pixel_units));
     76         } else {
     77             job_params->print_right_margin = floorf(
     78                     ((right_margin) * (float) job_params->pixel_units));
     79         }
     80         if (job_params->job_bottom_margin > bottom_margin) {
     81             job_params->print_bottom_margin = floorf(
     82                     ((job_params->job_bottom_margin - bottom_margin) *
     83                             (float) job_params->pixel_units));
     84         } else {
     85             job_params->print_bottom_margin = floorf(
     86                     ((bottom_margin) * (float) job_params->pixel_units));
     87         }
     88     }
     89 
     90     job_params->width = (job_params->printable_area_width -
     91             (job_params->print_left_margin + job_params->print_right_margin));
     92     job_params->height = (job_params->printable_area_height -
     93             (job_params->print_top_margin + job_params->print_bottom_margin));
     94 }
     95 
     96 void printable_area_get_default_margins(const wprint_job_params_t *job_params,
     97         const printer_capabilities_t *printer_cap,
     98         float *top_margin,
     99         float *left_margin, float *right_margin,
    100         float *bottom_margin) {
    101     if ((job_params == NULL) || (printer_cap == NULL)) {
    102         return;
    103     }
    104 
    105     bool useDefaultMargins = true;
    106 
    107     if (job_params->borderless) {
    108         useDefaultMargins = false;
    109         switch (job_params->pcl_type) {
    110             case PCLm:
    111             case PCLPWG:
    112                 *top_margin = 0.0f;
    113                 *left_margin = 0.0f;
    114                 *right_margin = 0.0f;
    115                 *bottom_margin = 0.00f;
    116                 break;
    117             default:
    118                 *top_margin = -0.065f;
    119                 *left_margin = -0.10f;
    120                 *right_margin = -0.118f;
    121                 *bottom_margin = -0.10f;
    122                 break;
    123         }
    124     } else {
    125         switch (job_params->pcl_type) {
    126             case PCLm:
    127                 *top_margin = (float) printer_cap->printerTopMargin / 2540;
    128                 *bottom_margin = (float) printer_cap->printerBottomMargin / 2540;
    129                 *left_margin = (float) printer_cap->printerLeftMargin / 2540;
    130                 *right_margin = (float) printer_cap->printerRightMargin / 2540;
    131                 useDefaultMargins = false;
    132                 break;
    133             case PCLPWG:
    134                 *top_margin = 0.0f;
    135                 *left_margin = 0.0f;
    136                 *right_margin = 0.0f;
    137                 *bottom_margin = 0.00f;
    138                 useDefaultMargins = false;
    139                 break;
    140             default:
    141                 break;
    142         }
    143     }
    144 
    145     if (useDefaultMargins) {
    146         if (!printer_cap->inkjet) {
    147             // default laser margins
    148             *top_margin = 0.2f;
    149             *left_margin = 0.25f;
    150             *right_margin = 0.25f;
    151             *bottom_margin = 0.2f;
    152         } else {
    153             // default inkjet margins
    154             *top_margin = 0.125f;
    155             *left_margin = 0.125f;
    156             *right_margin = 0.125f;
    157             if ((job_params->duplex != DUPLEX_MODE_NONE) || !printer_cap->borderless) {
    158                 *bottom_margin = 0.5f;
    159             } else {
    160                 *bottom_margin = 0.125f;
    161             }
    162         }
    163     }
    164 
    165     LOGD("printable_area_get_default_margins(): top_margin=%f, left_margin=%f, "
    166             "right_margin=%f, bottom_margin=%f", *top_margin, *left_margin, *right_margin,
    167             *bottom_margin);
    168 }