graphics/fill_rules
Define some overlapping vector graphics and fill them using various methods.
Download PHP Code Switch to Java Code Show Output
<?php
/*
* Fill Rules:
* Define some overlapping vector graphics and fill them using
* various methods
*
* Use the default "nonzero winding number" rule to fill overlapping objects and
* create a ring, for example.
* Use the "evenodd" fill rule to make overlapping objects being
* filled alternately.
*
* Required software: PDFlib/PDFlib+PDI/PPS 9
* Required data: none
*/
/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";
$outfile = "";
$title = "Fill Rules";
try {
$p = new pdflib();
$p->set_option("searchpath={" . $searchpath . "}");
/* This means we must check return values of load_font() etc. */
$p->set_option("errorpolicy=return");
if ($p->begin_document($outfile, "") == 0)
throw new Exception("Error: " . $p->get_errmsg());
$p->set_info("Creator", "PDFlib Cookbook");
$p->set_info("Title", $title );
$font = $p->load_font("NotoSerif-Regular", "unicode", "");
if ($font == 0)
throw new Exception("Error: " . $p->get_errmsg());
/* --------------------------------------------------------------------
* Case I: The default "nonzero winding number" rule will be used. All
* defined objects will be filled with one $p->fill() call. In this case
* (without any coordinate transformation being performed), overlapping
* objects which are drawn counterclockwise will be filled with the
* current fill color while all objects drawn in clockwise direction
* won't be filled. Note that $p->circle() and $p->rect() functions define
* objects which will be drawn counterclockwise.
* --------------------------------------------------------------------
*/
/* Start Page 1 */
$p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
/* Set the color to purple */
$p->setcolor("fill", "rgb", 1.0, 0.0, 0.5, 0.0);
/* Define a big arc segment of 360 degrees in counterclockwise
* direction. Move to the starting point of the arc segment first
*/
$p->moveto(450, 600);
$p->arc(300, 600, 150, 0, 360);
/* Define a small arc segment of 360 degrees in counterclockwise
* direction. Move to the starting point of the arc segment first
*/
$p->moveto(400, 600);
$p->arc(300, 600, 100, 0, 360);
/* Define a big arc segment of 360 degrees in clockwise direction.
* Move to the starting point of the arc segment first
*/
$p->moveto(450, 200);
$p->arcn(300, 200, 150, 360, 0);
/* Define a small arc segment of 360 degrees in counterclockwise
* direction. Move to the starting point of the arc segment first
*/
$p->moveto(400, 200);
$p->arc(300, 200, 100, 0, 360);
$p->fill_stroke();
/* Set the color to black */
$p->setcolor("fill", "gray", 0, 0.0, 0, 0.0);
$p->set_text_option("font=" . $font . " fontsize=14");
$p->fit_textline("With the default \"nonzero winding number\" rule:",
20, 790, "");
$p->fit_textline(
"Stroke a big and a small arc segment counterclockwise "
. "and fill them with purple", 20, 770, "");
$p->fit_textline("With the default \"nonzero winding number\" rule:",
20, 400, "");
$p->fit_textline("Stroke a big arc segment clockwise and", 20, 380, "");
$p->fit_textline(
"a small arc segment counterclockwise and fill them with purple",
20, 360, "");
$p->end_page_ext("");
/* -----------------------------------------------------------------
* Case II: The fill rule of "evenodd" will be used. All defined
* objects will be filled with one $p->fill() call. In this case,
* overlapping objects will be drawn according to the "evenodd" rule
* and filled alternately.
* -----------------------------------------------------------------
*/
/* Start Page 2 */
$p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
$p->set_graphics_option("fillrule=evenodd");
/* Set the color to purple */
$p->setcolor("fill", "rgb", 1.0, 0.0, 0.5, 0.0);
/* Define a rectangle */
$p->rect(100, 100, 400, 300);
/* Define a big circle */
$p->circle(300, 500, 200);
/* Define an arc segment */
$p->moveto(300, 750);
$p->lineto(300, 500);
$p->arc(300, 500, 250, 0, 90);
/* Define a small circle */
$p->circle(300, 600, 50);
$p->fill_stroke();
/* Set the color to black */
$p->setcolor("fill", "gray", 0, 0.0, 0, 0.0);
$p->set_text_option("font=" . $font . " fontsize=14");
$p->fit_textline(
"With the \"evenodd\" fill rule: Draw a rectangle, a big circle,",
20, 810, "");
$p->fit_textline(
"an arc segment, and a small circle and fill them with purple",
20, 790, "");
$p->end_page_ext("");
$p->end_document("");
$buf = $p->get_buffer();
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=fill_rules.pdf");
print $buf;
} catch (PDFlibException $e){
echo("PDFlib exception occurred:\n" .
"[" . $e->get_errnum() . "] " . $e->get_apiname() .
": " . $e->get_errmsg() . "\n");
exit(1);
} catch (Throwable $e) {
echo("PHP exception occurred: " . $e->getMessage() . "\n");
exit(1);
}
$p = 0;
?>