cairomm 1.19.0
path-iter.cc

An example of iterating through a Cairo::Path.

An example of iterating through a Cairo::Path

/* Copyright (C) 2025 The cairomm Development Team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <https://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <cassert>
#include <cairomm/cairomm.h>
// This program shows how to iterate through a Cairo::Path.
{
out << " x: " << p.x << ", y: " << p.y << std::endl;
return out;
}
void print_path(const Cairo::RefPtr<const Cairo::Path>& path)
{
// A Path is a series of Path::Element items. The custom Path::const_iterator
// calculates the size of the cairo_path_data_t internally (i.e. how many
// points are there before the next header) so the application doesn't need to
// calculate the length manually, it just needs to increment the const_iterator.
// Alternatively a range-for statement: for (const auto& elt : *path)
for (Cairo::Path::const_iterator it = path->begin(); it != path->end(); ++it)
{
// A Path::Element object is a collection of 0 or more Path::Element::Point
// objects, the precise number depends on the type of data.
const Cairo::Path::Element elt = *it;
const Cairo::Path::ElementType data_type = elt.type();
switch (data_type)
{
std::cout << "LINE_TO element" << std::endl;
assert(elt.size() == 1); // LINE_TO has 1 data point
std::cout << elt[0];
break;
std::cout << "MOVE_TO element" << std::endl;
assert(elt.size() == 1); // MOVE_TO has 1 data point
std::cout << elt[0];
break;
std::cout << "CURVE_TO element" << std::endl;
assert(elt.size() == 3); // CURVE_TO has 3 data points
std::cout << elt[0] << elt[1] << elt[2];
break;
std::cout << "CLOSE_PATH element" << std::endl;
assert(elt.size() == 0); // CLOSE_PATH has no data points
break;
default:
break;
}
}
}
int main(int /* argc */, char** /* argv */)
{
// Draw some random stuff.
cr->move_to(10.0, 10.0);
cr->line_to(500.0, 300.0);
cr->curve_to(250.0, 250.0, 100.0, 100.0, 300.0, 300.0);
cr->close_path();
cr->rectangle(50.0, 50.0, 200.0, 150.0);
cr->arc(300.0, 200.0, 75.0, 0.0, 1.8 * M_PI);
std::cout << "NORMAL PATH" << std::endl
<< "===========" << std::endl;
print_path(cr->copy_path2());
std::cout << "FLATTENED PATH" << std::endl
<< "==============" << std::endl;
print_path(cr->copy_path_flat2());
}
basic_ostream< _CharT, _Traits > & endl(basic_ostream< _CharT, _Traits > &__os)
basic_ostream< _CharT, _Traits > & operator<<(basic_ostream< _CharT, _Traits > &__os, const basic_string< _CharT, _Traits, _Alloc > &__str)
ostream cout
static RefPtr< Context > create(const RefPtr< Surface > &target)
static RefPtr< ImageSurface > create(Format format, int width, int height)
Creates an image surface of the specified format and dimensions.
A single element of a path.
Definition path.h:119
ElementType type() const
Gets the type of element for this path element.
Definition path.h:166
unsigned int size() const
Get the number of points in this path element.
A custom const_iterator for iterating over a Path.
Definition path.h:189
ElementType
Describes the type of one portion of a path when represented as a Path::Element.
Definition path.h:98
@ MOVE_TO
A move-to operation.
@ LINE_TO
A line-to operation.
@ CURVE_TO
A curve-to operation.
@ CLOSE_PATH
A close-path operation.
@ ARGB32
Each pixel is a 32-bit quantity, with alpha in the upper 8 bits, then red, then green,...
A simple structure for holding an X and Y coordinate pair.
Definition path.h:146
double y
Definition path.h:147
double x
Definition path.h:147