Skip to content

Commit

Permalink
Style.
Browse files Browse the repository at this point in the history
  • Loading branch information
anntzer committed Sep 16, 2021
1 parent 6f2bb54 commit ac6d5fd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 39 deletions.
49 changes: 23 additions & 26 deletions src/_mplcairo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,9 +613,9 @@ void GraphicsContextRenderer::set_clip_path(
auto const& [path, transform] =
transformed_path->attr("get_transformed_path_and_affine")()
.cast<std::tuple<py::object, py::object>>();
auto const& matrix =
auto const& mtx =
matrix_from_transform(transform, get_additional_state().height);
load_path_exact(cr_, path, &matrix);
load_path_exact(cr_, path, &mtx);
get_additional_state().clip_path =
{transformed_path, {cairo_copy_path(cr_), cairo_path_destroy}};
} else {
Expand Down Expand Up @@ -778,8 +778,7 @@ void GraphicsContextRenderer::draw_gouraud_triangles(
throw std::invalid_argument{"non-matching GraphicsContext"};
}
auto const& ac = _additional_context();
auto matrix =
matrix_from_transform(transform, get_additional_state().height);
auto mtx = matrix_from_transform(transform, get_additional_state().height);
auto const& tri_raw = triangles.unchecked<3>();
auto const& col_raw = colors.unchecked<3>();
auto const& n = tri_raw.shape(0);
Expand All @@ -803,8 +802,8 @@ void GraphicsContextRenderer::draw_gouraud_triangles(
}
cairo_mesh_pattern_end_patch(pattern);
}
cairo_matrix_invert(&matrix);
cairo_pattern_set_matrix(pattern, &matrix);
cairo_matrix_invert(&mtx);
cairo_pattern_set_matrix(pattern, &mtx);
cairo_set_source(cr_, pattern);
cairo_pattern_destroy(pattern);
cairo_paint(cr_);
Expand Down Expand Up @@ -879,9 +878,9 @@ void GraphicsContextRenderer::draw_image(
}
auto const& pattern = cairo_pattern_create_for_surface(surface);
cairo_surface_destroy(surface);
auto const& matrix =
auto const& mtx =
cairo_matrix_t{1, 0, 0, -1, -x, -y + get_additional_state().height};
cairo_pattern_set_matrix(pattern, &matrix);
cairo_pattern_set_matrix(pattern, &mtx);
cairo_set_source(cr_, pattern);
cairo_pattern_destroy(pattern);
cairo_paint(cr_);
Expand Down Expand Up @@ -909,7 +908,7 @@ void GraphicsContextRenderer::draw_markers(
auto const& n_vertices = vertices.shape(0);

auto const& marker_matrix = matrix_from_transform(marker_transform);
auto const& matrix =
auto const& mtx =
matrix_from_transform(transform, get_additional_state().height);

auto const& fc_raw_opt =
Expand Down Expand Up @@ -998,7 +997,7 @@ void GraphicsContextRenderer::draw_markers(
auto worker = [&](cairo_t* ctx, int start, int stop) {
for (auto i = start; i < stop; ++i) {
auto x = vertices(i, 0), y = vertices(i, 1);
cairo_matrix_transform_point(&matrix, &x, &y);
cairo_matrix_transform_point(&mtx, &x, &y);
auto const& target_x = x + x0,
& target_y = y + y0;
if (!(std::isfinite(target_x) && std::isfinite(target_y))) {
Expand All @@ -1011,7 +1010,7 @@ void GraphicsContextRenderer::draw_markers(
auto const& idx =
int(n_subpix * f_target_x) * n_subpix + int(n_subpix * f_target_y);
auto const& pattern = patterns[idx];
// Offsetting by height is already taken care of by matrix.
// Offsetting by height is already taken care of by mtx.
auto const& pattern_matrix =
cairo_matrix_t{1, 0, 0, 1, -i_target_x, -i_target_y};
cairo_pattern_set_matrix(pattern, &pattern_matrix);
Expand Down Expand Up @@ -1068,7 +1067,7 @@ void GraphicsContextRenderer::draw_markers(
cairo_surface_flush(surface);
for (auto i = 0; i < n_vertices; ++i) {
auto x = vertices(i, 0), y = vertices(i, 1);
cairo_matrix_transform_point(&matrix, &x, &y);
cairo_matrix_transform_point(&mtx, &x, &y);
if (!(std::isfinite(x) && std::isfinite(y))) {
continue;
}
Expand All @@ -1082,7 +1081,7 @@ void GraphicsContextRenderer::draw_markers(
for (auto i = 0; i < n_vertices; ++i) {
cairo_save(cr_);
auto x = vertices(i, 0), y = vertices(i, 1);
cairo_matrix_transform_point(&matrix, &x, &y);
cairo_matrix_transform_point(&mtx, &x, &y);
if (!(std::isfinite(x) && std::isfinite(y))) {
cairo_restore(cr_);
continue;
Expand All @@ -1104,19 +1103,18 @@ void GraphicsContextRenderer::draw_path(
}
auto const& ac = _additional_context();
auto path_loaded = false;
auto matrix =
matrix_from_transform(transform, get_additional_state().height);
auto mtx = matrix_from_transform(transform, get_additional_state().height);
auto const& load_path = [&] {
if (!path_loaded) {
load_path_exact(cr_, path, &matrix);
load_path_exact(cr_, path, &mtx);
path_loaded = true;
}
};
if (auto const& sketch = get_additional_state().sketch) {
path =
path.attr("cleaned")(
"transform"_a=transform, "curves"_a=true, "sketch"_a=sketch);
matrix = cairo_matrix_t{1, 0, 0, -1, 0, get_additional_state().height};
mtx = cairo_matrix_t{1, 0, 0, -1, 0, get_additional_state().height};
}
if (fc) {
load_path();
Expand All @@ -1140,11 +1138,11 @@ void GraphicsContextRenderer::draw_path(
hatch_cr, double(dpi), double(dpi), double(dpi)};
hatch_gcr.get_additional_state().snap = false;
hatch_gcr.set_linewidth(get_additional_state().get_hatch_linewidth());
auto const& matrix =
auto const& mtx =
cairo_matrix_t{double(dpi), 0, 0, -double(dpi), 0, double(dpi)};
auto const& hatch_color = get_additional_state().get_hatch_color();
fill_and_stroke_exact(
hatch_cr, *hatch_path, &matrix, hatch_color, hatch_color);
hatch_cr, *hatch_path, &mtx, hatch_color, hatch_color);
auto const& hatch_pattern =
cairo_pattern_create_for_surface(cairo_get_target(hatch_cr));
cairo_pattern_set_extend(hatch_pattern, CAIRO_EXTEND_REPEAT);
Expand All @@ -1163,8 +1161,7 @@ void GraphicsContextRenderer::draw_path(
auto const& vertices = path.attr("vertices").cast<py::array_t<double>>();
auto const& n = vertices.shape(0);
for (auto i = decltype(n)(0); i < n; i += chunksize) {
load_path_exact(
cr_, vertices, i, std::min(i + chunksize + 1, n), &matrix);
load_path_exact(cr_, vertices, i, std::min(i + chunksize + 1, n), &mtx);
cairo_stroke(cr_);
}
}
Expand Down Expand Up @@ -1270,7 +1267,7 @@ void GraphicsContextRenderer::draw_path_collection(
// FIXME: Implement parallelization.
for (auto i = 0; i < n; ++i) {
auto const& path = paths[i % n_paths];
auto const& matrix = matrices[i % n_transforms];
auto const& mtx = matrices[i % n_transforms];
auto x = offsets_raw(i % n_offsets, 0), y = offsets_raw(i % n_offsets, 1);
cairo_matrix_transform_point(&offset_matrix, &x, &y);
if (!(std::isfinite(x) && std::isfinite(y))) {
Expand All @@ -1281,7 +1278,7 @@ void GraphicsContextRenderer::draw_path_collection(
cairo_set_source_rgba(
cr_, fcs_raw(i_mod, 0), fcs_raw(i_mod, 1),
fcs_raw(i_mod, 2), fcs_raw(i_mod, 3));
cache.mask(cr_, path, matrix, draw_func_t::Fill, 0, {}, x, y);
cache.mask(cr_, path, mtx, draw_func_t::Fill, 0, {}, x, y);
}
if (ecs_raw.size()) {
auto const& i_mod = i % ecs_raw.shape(0);
Expand All @@ -1292,7 +1289,7 @@ void GraphicsContextRenderer::draw_path_collection(
? points_to_pixels(lws_raw[i % lws_raw.size()])
: cairo_get_line_width(cr_);
auto const& dash = dashes_raw[i % n_dashes];
cache.mask(cr_, path, matrix, draw_func_t::Stroke, lw, dash, x, y);
cache.mask(cr_, path, mtx, draw_func_t::Stroke, lw, dash, x, y);
}
// NOTE: We drop antialiaseds because that just seems silly.
// We drop urls as they should be handled in a post-processing step anyways
Expand Down Expand Up @@ -1325,7 +1322,7 @@ void GraphicsContextRenderer::draw_quad_mesh(
throw std::invalid_argument{"non-matching GraphicsContext"};
}
auto const& ac = _additional_context();
auto const& matrix =
auto const& mtx =
matrix_from_transform(master_transform, get_additional_state().height);
auto const& fcs_raw = fcs.unchecked<2>(),
& ecs_raw = ecs.unchecked<2>();
Expand Down Expand Up @@ -1353,7 +1350,7 @@ void GraphicsContextRenderer::draw_quad_mesh(
for (auto i = 0; i < mesh_height + 1; ++i) {
for (auto j = 0; j < mesh_width + 1; ++j) {
cairo_matrix_transform_point(
&matrix,
&mtx,
coords_raw.mutable_data(i, j, 0), coords_raw.mutable_data(i, j, 1));
}
}
Expand Down
24 changes: 11 additions & 13 deletions src/_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ cairo_matrix_t matrix_from_transform(
"not {.shape}"_format(transform).cast<std::string>()};
}
// The y flip is already handled by the master matrix.
auto matrix = cairo_matrix_t{
auto mtx = cairo_matrix_t{
py_matrix(0, 0), py_matrix(1, 0),
py_matrix(0, 1), py_matrix(1, 1),
py_matrix(0, 2), py_matrix(1, 2)};
cairo_matrix_multiply(&matrix, &matrix, master_matrix);
return matrix;
cairo_matrix_multiply(&mtx, &mtx, master_matrix);
return mtx;
}

bool has_vector_surface(cairo_t* cr)
Expand Down Expand Up @@ -184,24 +184,22 @@ bool has_vector_surface(cairo_t* cr)
// for cairo_t*'s that we may not have initialized.
AdditionalState& get_additional_state(cairo_t* cr)
{
auto const& data = cairo_get_user_data(cr, &detail::STATE_KEY);
if (!data) {
auto const stack = static_cast<std::stack<AdditionalState>*>(
cairo_get_user_data(cr, &detail::STATE_KEY));
if (!stack || stack->empty()) {
throw std::runtime_error{"cairo_t* missing additional state"};
}
auto& stack = *static_cast<std::stack<AdditionalState>*>(data);
if (stack.empty()) {
throw std::runtime_error{"cairo_t* missing additional state"};
}
return stack.top();
return stack->top();
}

void restore_init_matrix(cairo_t* cr)
{
auto const& data = cairo_get_user_data(cr, &detail::INIT_MATRIX_KEY);
if (!data) {
auto const mtx = static_cast<cairo_matrix_t*>(
cairo_get_user_data(cr, &detail::INIT_MATRIX_KEY));
if (!mtx) {
cairo_identity_matrix(cr);
} else {
cairo_set_matrix(cr, static_cast<cairo_matrix_t*>(data));
cairo_set_matrix(cr, mtx);
}
}

Expand Down

0 comments on commit ac6d5fd

Please sign in to comment.