"Fossies" - the Fresh Open Source Software Archive

Member "PDFlib-Lite-7.0.5p3/bind/pdflib/cpp/starter_color.cpp" (6 Jun 2012, 12531 Bytes) of package /linux/misc/old/PDFlib-Lite-7.0.5p3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "starter_color.cpp" see the Fossies "Dox" file reference documentation.

    1 /* $Id: starter_color.cpp,v 1.1.2.1 2007/12/20 16:28:32 kurt Exp $
    2  * Starter color:
    3  * Demonstrate the basic use of supported color spaces
    4  *
    5  * Apply the following color spaces to text and vector graphics:
    6  * - gray
    7  * - rgb
    8  * - cmyk
    9  * - iccbasedgray/rgb/cmyk
   10  * - spot
   11  * - lab
   12  * - pattern
   13  * - shadings
   14  *
   15  * Required software: PDFlib/PDFlib+PDI/PPS 7
   16  * Required data: none
   17  */
   18 
   19 #include <iostream>
   20 
   21 #include "pdflib.hpp"
   22 
   23 using namespace std;
   24 
   25 int
   26 main(void)
   27 {
   28     try {
   29 
   30         /* This is where the data files are. Adjust as necessary. */
   31         string searchpath = "../data";
   32         string outfile = "starter_color.pdf";
   33 
   34 #define BUFLEN 1024
   35         char buf[BUFLEN];
   36         PDFlib p;
   37         int font, spot;
   38         int y = 800, x = 50, xoffset1=80, xoffset2 = 100, yoffset = 70, r = 30;
   39         double icchandle;
   40 
   41         p.set_parameter("SearchPath", searchpath);
   42 
   43         /* This means we must check return values of load_font() etc. */
   44         p.set_parameter("errorpolicy", "return");
   45 
   46         if (p.begin_document(outfile, "") == -1) {
   47             cerr << "Error: " << p.get_errmsg() << endl; return 2;
   48         }
   49 
   50         p.set_info("Creator", "PDFlib starter sample");
   51         p.set_info("Title", "starter_color");
   52 
   53         /* Load the font */
   54         font = p.load_font("Helvetica", "winansi", "");
   55 
   56         if (font == -1) {
   57             cerr << "Error: " << p.get_errmsg() << endl; return 2;
   58         }
   59 
   60         /* Start the page */
   61         p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
   62 
   63         p.setfont(font, 14);
   64 
   65 
   66         /* -------------------------------------------------------------------
   67          * Use default colors
   68          *
   69          * If no special color is set the default values will be used. The
   70          * default values are restored at the beginning of the page.
   71          * 0=black in the Gray color space is the default fill and stroke
   72          * color in many cases, as shown in our sample.
   73          * -------------------------------------------------------------------
   74          */
   75 
   76         /* Fill a circle with the default black fill color */
   77         p.circle(x, y-=yoffset, r);
   78         p.fill();
   79 
   80         /* Output text with default black fill color */
   81         p.fit_textline( "Circle and text filled with default color {gray 0}",
   82                 x+xoffset2, y, "");
   83 
   84         p.fit_textline("1.",  x+xoffset1, y, "");
   85 
   86         /* -------------------------------------------------------------------
   87          * Use the Gray color space
   88          *
   89          * Gray color is defined by Gray values between 0=black and 1=white.
   90          * -------------------------------------------------------------------
   91          */
   92 
   93         /* Using setcolor(), set the current fill color to a light gray
   94          * represented by (0.5, 0, 0, 0) which defines 50% gray. Since gray
   95          * colors are defined by only one value, the last three function
   96          * parameters must be set to 0.
   97          */
   98         p.setcolor("fill", "gray", 0.5, 0, 0, 0);
   99 
  100         /* Fill a circle with the current fill color defined above */
  101         p.circle(x, y-=yoffset, r);
  102         p.fill();
  103 
  104         /* Output text with the current fill color */
  105         p.fit_textline("Circle and text filled with {gray 0.5}",
  106                 x+xoffset2, y, "");
  107 
  108         /* Alternatively, you can set the fill color in the call to
  109          * fit_textline() using the "fillcolor" option. This case applies the
  110          * fill color just the single function call. The current fill color
  111          * won't be affected.
  112          */
  113         p.fit_textline("2.", x+xoffset1, y, "fillcolor={gray 0.5}");
  114 
  115 
  116         /* --------------------------------------------------------------------
  117          * Use the RGB color space
  118          *
  119          * RGB color is defined by RGB triples, i.e. three values between 0 and
  120          * 1 specifying the percentage of red, green, and blue.
  121          * (0, 0, 0) is black and (1, 1, 1) is white. The commonly used RGB
  122          * color values in the range 0...255 must be divided by 255 in order to
  123          * scale them to the range 0...1 as required by PDFlib.
  124          * --------------------------------------------------------------------
  125          */
  126 
  127         /* Use setcolor() to set the fill color to a grass-green
  128          * represented by (0.1, 0.95, 0.3, 0) which defines 10% red, 95% green,
  129          * 30% blue. Since RGB colors are defined by only three values, the last
  130          * function parameter must be set to 0.
  131          */
  132         p.setcolor("fill", "rgb", 0.1, 0.95, 0.3, 0);
  133 
  134         /* Draw a circle with the current fill color defined above */
  135         p.circle(x, y-=yoffset, r);
  136         p.fill();
  137 
  138         /* Output a text line with the RGB fill color defined above */
  139         p.fit_textline("Circle and text filled with {rgb 0.1 0.95 0.3}",
  140                 x+xoffset2, y, "");
  141 
  142         /* Alternatively, you can set the fill color in the call to
  143          * fit_textline() using the "fillcolor" option. This case applies the
  144          * fill color just the single function call. The current fill color
  145          * won't be affected.
  146          */
  147         p.fit_textline("3.", x+xoffset1, y, "fillcolor={rgb 0.1 0.95 0.3}");
  148 
  149 
  150         /* --------------------------------------------------------------------
  151          * Use the CMYK color space
  152          *
  153          * CMYK color is defined by four CMYK values between 0 = no color and
  154          * 1 = full color representing cyan, magenta, yellow, and black values;
  155          * (0, 0, 0, 0) is white and (0, 0, 0, 1) is black.
  156          * --------------------------------------------------------------------
  157          */
  158 
  159         /* Use setcolor() to set the current fill color to a pale
  160          * orange, represented by (0.1, 0.7, 0.7, 0.1) which defines 10% Cyan,
  161          * 70% Magenta, 70% Yellow, and 10% Black.
  162          */
  163         p.setcolor("fill", "cmyk", 0.1, 0.7, 0.7, 0.1);
  164 
  165         /* Fill a circle with the current fill color defined above */
  166         p.circle(x, y-=yoffset, r);
  167         p.fill();
  168 
  169         /* Output a text line with the CMYK fill color defined above */
  170         p.fit_textline("Circle and text filled with {cmyk 0.1 0.7 0.7 0.1}",
  171                 x+xoffset2, y, "");
  172 
  173         /* Alternatively, you can set the fill color in the call to
  174          * fit_textline() using the "fillcolor" option. This case applies the
  175          * fill color just the single function call. The current fill color
  176          * won't be affected.
  177          */
  178         p.fit_textline("4.", x+xoffset1, y, "fillcolor={cmyk 0.1 0.7 0.7 0.1}");
  179 
  180 
  181         /* --------------------------------------------------------------------
  182          * Use a Lab color
  183          *
  184          * Device-independent color in the CIE L*a*b* color space is specified
  185          * by a luminance value in the range 0-100 and two color values in the
  186          * range -127 to 128. The first value contains the green-red axis,
  187          * while the second value contains the blue-yellow axis.
  188          * --------------------------------------------------------------------
  189          */
  190 
  191         /* Set the current fill color to a loud blue, represented by
  192          * (100, -127, -127, 0). Since Lab colors are defined by only three
  193          * values, the last function parameter must be set to 0.
  194          */
  195         p.setcolor("fill", "lab", 100, -127, -127, 0);
  196 
  197         /* Fill a circle with the fill color defined above */
  198         p.circle(x, y-=yoffset, r);
  199         p.fill();
  200 
  201         /* Output a text line with the Lab fill color defined above */
  202         p.fit_textline("Circle and text filled with {lab 100 -127 -127}",
  203                 x+xoffset2, y, "");
  204 
  205         /* Alternatively, you can set the fill color in the call to
  206          * fit_textline() using the "fillcolor" option. This case applies the
  207          * fill color just the single function call. The current fill color
  208          * won't be affected.
  209          */
  210         p.fit_textline("5.", x+xoffset1, y, "fillcolor={lab 100 -127 -127}");
  211 
  212 
  213         /* ---------------------------------------------------------------
  214          * Use an ICC based color
  215          *
  216          * ICC-based colors are specified with the help of an ICC profile.
  217          * ---------------------------------------------------------------
  218          */
  219 
  220         /* Load the sRGB profile. sRGB is guaranteed to be always available */
  221         icchandle = p.load_iccprofile("sRGB", "usage=iccbased");
  222 
  223         /* Set the sRGB profile. (Accordingly, you can use
  224          * "setcolor:iccprofilergb" or "setcolor:iccprofilegray" with an
  225          * appropriate profile)
  226          */
  227         p.set_value("setcolor:iccprofilergb", icchandle);
  228 
  229         /* Use setcolor() with the "iccbasedrgb" color space to set the current
  230          * fill and stroke color to a grass-green, represented
  231          * by the RGB color values (0.1 0.95 0.3 0) which define 10% Red,
  232          * 95% Green, and 30% Blue. Since iccbasedrgb colors are defined by only
  233          * three values, the last function parameter must be set to 0.
  234          */
  235         p.setcolor("fill", "iccbasedrgb", 0.1, 0.95, 0.3, 0);
  236 
  237         /* Fill a circle with the ICC based RGB fill color defined above */
  238         p.circle(x, y-=yoffset, r);
  239         p.fill();
  240 
  241         /* Output a text line with the ICC based RGB fill color defined above */
  242         p.fit_textline(
  243                 "Circle and text filled with {iccbasedrgb 0.1 0.95 0.3}",
  244                 x+xoffset2, y, "");
  245 
  246         /* Alternatively, you can set the fill color in the call to
  247          * fit_textline() using the "fillcolor" option. This case applies the
  248          * fill color just the single function call. The current fill color
  249          * won't be affected.
  250          */
  251         p.fit_textline("6.", x+xoffset1, y,
  252                 "fillcolor={iccbasedrgb 0.1 0.95 0.3}");
  253 
  254 
  255         /* --------------------------------------------------------------------
  256          * Use a spot color
  257          *
  258          * Spot color (separation color space) is a predefined or arbitrarily
  259          * named custom color with an alternate representation in one of the
  260          * other color spaces above; this is generally used for preparing
  261          * documents which are intended to be printed on an offset printing
  262          * machine with one or more custom colors. The tint value (percentage)
  263          * ranges from 0 = no color to 1 = maximum intensity of the spot color.
  264          * --------------------------------------------------------------------
  265          */
  266 
  267         /* Define the spot color "PANTONE 281 U" from the builtin color
  268          * library PANTONE
  269          */
  270         spot = p.makespotcolor("PANTONE 281 U");
  271 
  272         /* Set the spot color "PANTONE 281 U" with a tint value of 1 (=100%)
  273          * and output some text. Since spot colors are defined by only two
  274          * values, the last two function parameters must be set to 0.
  275          */
  276         p.setcolor("fill", "spot", spot, 1.0, 0, 0);
  277 
  278         /* Fill a circle with the ICC based RGB fill color defined above */
  279         p.circle(x, y-=yoffset, r);
  280         p.fill();
  281 
  282         p.fit_textline(
  283                 "Circle and text filled with {spotname {PANTONE 281 U} 1}",
  284                 x+xoffset2, y, "");
  285 
  286         /* Alternatively, you can set the fill color in the call to
  287          * fit_textline() using the "fillcolor" option. This case applies the
  288          * fill color just the single function call. The current fill color
  289          * won't be affected.
  290          */
  291         p.fit_textline("7.", x+xoffset1, y,
  292             "fillcolor={spotname {PANTONE 281 U} 1}");
  293 
  294         /* or */
  295         sprintf(buf, "fillcolor={spot %d 1}", spot);
  296         p.fit_textline("7.", x+xoffset1, y, buf);
  297 
  298 
  299         /* ----------------------------------------------------------
  300          * For using the Pattern color space, see the Cookbook topics
  301          * graphics/fill_pattern and images/background_pattern.
  302          * ----------------------------------------------------------
  303          */
  304 
  305         /* ---------------------------------------------------------
  306          * For using the Shading color space, see the Cookbook topic
  307          * color/color_gradient.
  308          * ---------------------------------------------------------
  309          */
  310 
  311         p.end_page_ext("");
  312 
  313         p.end_document("");
  314 
  315     }
  316 
  317     catch (PDFlib::Exception &ex) {
  318         cerr << "PDFlib exception occurred:" << endl;
  319         cerr << "[" << ex.get_errnum() << "] " << ex.get_apiname()
  320             << ": " << ex.get_errmsg() << endl;
  321         return 2;
  322     }
  323 
  324     return 0;
  325 }