Shapes

This document is about how to set up and use the basic shapes of a document: rectangles, ovals, polygons, graphic lines and groups. Once you have understood one of them, you have understood them all — they share the same API where it makes sense.

Table of contents

Which shape to choose

Shape Use it for
Rectangle Frames, boxes, backgrounds — and the most common carrier for images
Oval Round shapes; behaves exactly like a rectangle
Polygon Free shapes defined by path points — with or without curves
GraphicLine A straight line, defined by a start- and an end-point
Group Moving or transforming several shapes as one unit

Every shape needs a layer, lives on a page and can hold a fill color, a stroke and (except the graphic line) an image.

Size and Position

All shapes expect numbers in points per default. Add Unit::MILLIMETERS as the last parameter if you prefer millimeters:

<?php

use BitAndBlack\IdmlCreator\Content\Enum\Unit;

/** @var \BitAndBlack\IdmlCreator\Content\Rectangle $shape any shape — rectangles, ovals and polygons work exactly the same */
$shape
    ->setPosition(10, 10)
    ->setSize(100, 200)
;

$shape
    ->setPosition(10, 10, Unit::MILLIMETERS)
    ->setSize(100, 200, Unit::MILLIMETERS)
;

If you want to set your own unique identifier, hand it to the constructor — e.g. new Rectangle('UniqueName');.

Rectangle

Set up a Rectangle object and add it to a page like that:

<?php

use BitAndBlack\IdmlCreator\Content\Rectangle;

$rectangle = new Rectangle();
$rectangle
    ->setPosition(10, 10)
    ->setSize(100, 200)
;

/** @var \BitAndBlack\IdmlCreator\Content\Page $page a page, created in the "pages" chapter */
$page->addElement($rectangle);

To give it a color, a gradient or a stroke:

<?php

/** @var \BitAndBlack\IdmlCreator\Content\Rectangle $rectangle a rectangle, created above */
/** @var \BitAndBlack\IdmlCreator\Content\Color\Color $color a color, created in the "colors" chapter */
$rectangle
    ->setFillColor($color)
    ->setStrokeColor($color)
    ->setStrokeWeight(1)
;

A rectangle can also carry an image:

<?php

/** @var \BitAndBlack\IdmlCreator\Content\Rectangle $rectangle a rectangle, created above */
/** @var \BitAndBlack\IdmlCreator\Content\Image\Image $image an image, created in the "image" chapter */
$rectangle->setImage($image);

Oval

Ovals behave completely like Rectangles. The only difference is that they have round shapes instead of edges.

<?php

use BitAndBlack\IdmlCreator\Content\Oval;

$oval = new Oval();
$oval
    ->setPosition(10, 10)
    ->setSize(100, 200)
;

/** @var \BitAndBlack\IdmlCreator\Content\Page $page a page, created in the "pages" chapter */
$page->addElement($oval);

All chapters of the Rectangle apply to the Oval as well — including setFillColor(), setStrokeColor() and setImage().

Polygon

A polygon is a shape with freely defined corners. Set it up like any other shape, but add its form via path points:

<?php

use BitAndBlack\IdmlCreator\Content\Polygon;
use BitAndBlack\IdmlCreator\Content\SplineItem\PathPoint;

$polygon = new Polygon();
/** @var \BitAndBlack\IdmlCreator\Content\Color\Color $color a color, created in the "colors" chapter */
$polygon
    ->setPosition(10, 10)
    ->setFillColor($color)
    ->addPathPointGroup(
        new PathPoint(0, 0),
        new PathPoint(80, 0),
        new PathPoint(80, 80)
    )
;

/** @var \BitAndBlack\IdmlCreator\Content\Page $page a page, created in the "pages" chapter */
$page->addElement($polygon);

Path points

A PathPoint defines one corner of the polygon:

<?php

new PathPoint(80, 80);

The first two parameters are the left and the top position. The optional third and fourth parameters define the anchor points of a curve — polygon. Several path points are grouped with addPathPointGroup(); a polygon can contain multiple groups to create disconnected parts. Like the rectangle, a polygon can carry an image:

<?php

/** @var \BitAndBlack\IdmlCreator\Content\Polygon $polygon the polygon created above */
/** @var \BitAndBlack\IdmlCreator\Content\Image\Image $image an image, created in the "image" chapter */
$polygon
    ->setImage($image)
    ->fillFrameWithImageProportional()
;

GraphicLine

A GraphicLine has no position and no size. Instead, a start point and an end point need to be set:

<?php

use BitAndBlack\IdmlCreator\Content\GraphicLine;

$graphicLine = new GraphicLine();
/** @var \BitAndBlack\IdmlCreator\Content\Color\Color $color a color, created in the "colors" chapter */
$graphicLine
    ->setStartPoint(50, 50)
    ->setEndPoint(50, 100)
    ->setStrokeColor($color)
    ->setStrokeWeight(2)
;

/** @var \BitAndBlack\IdmlCreator\Content\Page $page a page, created in the "pages" chapter */
$page->addElement($graphicLine);

The example results in a 50-point long GraphicLine. Add Unit::MILLIMETERS as the third parameter of each method if you prefer numbers in millimeters.

Fitting an image

An image inside a frame can be scaled and cropped automatically with a FrameFittingOption — on a Rectangle, Oval, Polygon or GraphicLine:

<?php

use BitAndBlack\IdmlCreator\Content\Enum\AnchoredObjectSetting\AnchorPoint;
use BitAndBlack\IdmlCreator\Content\SplineItem\FrameFittingOption;

$frameFittingOption = new FrameFittingOption();
$frameFittingOption
    ->setAutoFit(true)
    ->setFittingAlignment(AnchorPoint::CENTER)
;

/** @var \BitAndBlack\IdmlCreator\Content\Rectangle $rectangle a rectangle, created above */
$rectangle->setFrameFittingOption($frameFittingOption);

Group

To move or transform several shapes as one unit, put them into a group:

<?php

use BitAndBlack\IdmlCreator\Content\Group;

$group = new Group();
/** @var \BitAndBlack\IdmlCreator\Content\Rectangle $rectangle1 the first rectangle of the group */
/** @var \BitAndBlack\IdmlCreator\Content\Rectangle $rectangle2 the second rectangle of the group */
$group->addElement($rectangle1, $rectangle2);

/** @var \BitAndBlack\IdmlCreator\Content\Page $page a page, created in the "pages" chapter */
$page->addElement($group);

Groups can be nested arbitrarily, and every member keeps its own position and layer. Please note that the group itself renders without its children — they are placed on the page individually, so the position calculation stays intact.