1. Changed license copyright year
[apps/home/mobileprint.git] / mobileprint / previewgen / lib / pdfgen.c
index de50fc4..703dd00 100644 (file)
@@ -1,9 +1,9 @@
 /*
 *  Mobileprint
 *
-* Copyright 2012  Samsung Electronics Co., Ltd
+* Copyright 2012-2013  Samsung Electronics Co., Ltd
 
-* Licensed under the Flora License, Version 1.0 (the "License");
+* Licensed under the Flora License, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 
 #define PPI_MAX_VAL            10000
 #define PPI_STR_MAX_LEN                sizeof("10000")
 
+#define PDFGEN_CMDSTR_MAXLEN   1024
+
+#define PDFUNITE_PATH          "/usr/bin/pdfunite"
+
 /**
  * @brief      This function securely executes cmd line request with popen
  * @param[in]  cmd_str        command line to execute
@@ -62,7 +66,7 @@ int call_cmd(const char *cmd_str)
                read_len = getline(&str_buf, &len, cmd_output);
        } while (read_len != -1);
 
-       PTS_IF_FREE_MEM(str_buf);
+       PGEN_IF_FREE_MEM(str_buf);
        pclose(cmd_output);
 
        PGEN_TRACE_END;
@@ -83,15 +87,15 @@ int call_pdftopdf(const char *infile, const char *outfile,
 {
        PGEN_TRACE_BEGIN;
 
-       PTS_RETV_IF(infile == NULL || outfile == NULL || settings == NULL || settings->ppd_filename == NULL,
+       PGEN_RETV_IF(infile == NULL || outfile == NULL || settings == NULL || settings->ppd_filename == NULL,
                                -1, "Invalid argument");
 
        int err_code = 0;
        char cmd_str[1024];
 
        /* placing n_up number of pages on the specified media */
-       snprintf(cmd_str, 1024, "PPD=%s " CUPS_FILTER_DIR "/pdftopdf job user title 1 \"%s number-up=%d PageSize=%s fit-to-page=True\" \"%s\""
-                        " >%s 2> /tmp/errlog",
+       snprintf(cmd_str, 1024, "PPD=%s " CUPS_FILTER_DIR "/pdftopdf job user title 1 \"%s number-up=%d PageSize=%s fit-to-page=True pdftopdfAutoRotate=false\" \"%s\""
+                        " >%s 2> " PREVIEW_TEMP_DIR "/errlog",
                         settings->ppd_filename,
                         PAGE_ORIENTATION_PORTRAIT == settings->orientation ? CUPS_OPTION_PORTRAIT :
                         PAGE_ORIENTATION_LANDSCAPE == settings->orientation ? CUPS_OPTION_LANDSCAPE : "",
@@ -100,12 +104,54 @@ int call_pdftopdf(const char *infile, const char *outfile,
 
        PGEN_DEBUG("call_pdftopdf(): %s", cmd_str);
        err_code = call_cmd(cmd_str);
-       PTS_RETV_IF(err_code, -1, "call_pdftopdf returned non-zero error code");
+       PGEN_RETV_IF(err_code, -1, "call_pdftopdf returned non-zero error code");
 
        PGEN_TRACE_END;
        return err_code;
 }
 
+
+int strcat_img_scale_option(char *res, const char *img_path,
+               const struct page_scale *scale)
+{
+       PGEN_TRACE_BEGIN;
+       PGEN_RETV_IF(res == NULL , -1 , "res is NULL");
+       PGEN_RETV_IF(img_path == NULL , -1 , "img_path is NULL");
+       PGEN_RETV_IF(scale == NULL , -1 , "scale is NULL");
+       //PGEN_RETV_IF(scale->w == 0 , -1 , "scale->w is 0");
+
+       int imgw;
+       int imgh;
+       int ppi;
+       char ppi_str[PPI_STR_MAX_LEN + 1];
+       int result;
+
+       switch (scale->type) {
+       case SCALE_FIT_TO_PAPER:
+               strcat(res, "fit-to-page=true");
+               return 0;
+       case SCALE_ORIGINAL:
+               strcat(res, "scaling=100");
+               return 0;
+       case SCALE_CUSTOM:
+               break;
+       }
+
+       result = get_image_resolution(img_path, &imgw, &imgh);
+       PGEN_RETV_IF(result < 0, -1 , "Can't get resolution");
+
+       ppi = (double)imgw / scale->w;
+       PGEN_RETV_IF(ppi >= PPI_MAX_VAL, -1 , "Resolution is too big");
+
+       snprintf(ppi_str, PPI_STR_MAX_LEN, "%d", ppi);
+       ppi_str[PPI_STR_MAX_LEN] = '\0';
+       strcat(res, "PPI=");
+       strcat(res, ppi_str);
+
+       return 0;
+}
+
+
 /**
  * @brief      This function forms and executes cmd line request to CUPS
  *             imagetopdf filter to convert image to PDF file
@@ -119,26 +165,71 @@ int call_imagetopdf(const char *infile, const char *outfile,
                                        const struct pdfgen_settings *settings)
 {
        PGEN_TRACE_BEGIN;
-       PTS_RETV_IF(infile== NULL || outfile == NULL || settings == NULL || settings->ppd_filename == NULL,
+       PGEN_RETV_IF(infile== NULL || outfile == NULL || settings == NULL || settings->ppd_filename == NULL,
                                -1 , "Invalid argument");
 
        int err_code = 0;
        char cmd_str[1024];
+       char scale_str[256];
+
+       scale_str[0] = '\0';
+       strcat_img_scale_option(scale_str, infile,  &(settings->scale));
 
        /* placing n_up number of pages on the specified media */
-       snprintf(cmd_str, 1024, "PPD=%s " CUPS_FILTER_DIR "/imagetopdf job user title 1 \"%s number-up=%d PageSize=%s fit-to-page=%s \" \"%s\""
-                        " >%s 2> /tmp/errlog",
-                        settings->ppd_filename,
-                        PAGE_ORIENTATION_PORTRAIT == settings->orientation ? CUPS_OPTION_PORTRAIT " " :
-                        PAGE_ORIENTATION_LANDSCAPE == settings->orientation ? CUPS_OPTION_LANDSCAPE " " : "",
-                        settings->n_up,
-                        settings->paper_name,
-                        SCALE_FIT_TO_PAPER == settings->scale.type ? "True" : "False",
-                        infile, outfile);
+       snprintf(cmd_str, 1024, "PPD=%s " CUPS_FILTER_DIR
+                       "/imagetopdf job user title 1 \"%s"
+                       " number-up=%d PageSize=%s"
+                       /*fit-to-page=%s*/
+                       " %s"
+                       " \" \"%s\""
+                       " >%s 2> /tmp/mobileprint/errlog",
+                       settings->ppd_filename,
+                       PAGE_ORIENTATION_PORTRAIT == settings->orientation
+                               ? CUPS_OPTION_PORTRAIT " "
+                       : PAGE_ORIENTATION_LANDSCAPE == settings->orientation
+                               ? CUPS_OPTION_LANDSCAPE " "
+                       : "",
+                       settings->n_up,
+                       settings->paper_name,
+                       /*SCALE_FIT_TO_PAPER == settings->scale.type
+                               ? "True" : "False",*/
+                       scale_str,
+                       infile, outfile);
 
        PGEN_DEBUG("call_imagetopdf(): %s", cmd_str);
        err_code = call_cmd(cmd_str);
-       PTS_RETV_IF(err_code, -1, "call_pdftopdf returned non-zero error code");
+       PGEN_RETV_IF(err_code, -1, "call_pdftopdf returned non-zero error code");
+
+       PGEN_TRACE_END;
+       return 0;
+}
+
+
+int call_pdfunite(char **const infiles_z, const char *outfile)
+{
+       int err_code = 0;
+       char cmd_str[PDFGEN_CMDSTR_MAXLEN + 1];
+       int i;
+
+       PGEN_TRACE_BEGIN;
+       PGEN_RETV_IF(NULL == infiles_z || NULL == infiles_z[0]
+                       || NULL == outfile, -1, "Argument error");
+
+       cmd_str[0] = '\0';
+       strncat(cmd_str, PDFUNITE_PATH, PDFGEN_CMDSTR_MAXLEN);
+
+       i = 0;
+       while (infiles_z[i] != NULL) {
+               strncat(cmd_str, " ", PDFGEN_CMDSTR_MAXLEN);
+               strncat(cmd_str, infiles_z[i], PDFGEN_CMDSTR_MAXLEN);
+               ++i;
+       }
+
+       strncat(cmd_str, " ", PDFGEN_CMDSTR_MAXLEN);
+       strncat(cmd_str, outfile, PDFGEN_CMDSTR_MAXLEN);
+       cmd_str[PDFGEN_CMDSTR_MAXLEN] = '\0';
+
+       err_code = call_cmd(cmd_str);
 
        PGEN_TRACE_END;
        return 0;