"Fossies" - the Fresh Open Source Software Archive

Member "PDFlib-Lite-7.0.5p3/bind/pdflib/php/examples.php5/starter_color.php" (6 Jun 2012, 11525 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) PHP 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.php" see the Fossies "Dox" file reference documentation.

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