Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 03f83d3

Browse files
committed
Optimize C code
1 parent e240080 commit 03f83d3

File tree

5 files changed

+46
-34
lines changed

5 files changed

+46
-34
lines changed

lib/matplotlib/tests/test_axes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4921,8 +4921,11 @@ def test_lines_with_colors(fig_test, fig_ref, data):
49214921
colors=expect_color, linewidth=5)
49224922

49234923

4924-
@image_comparison(['step_linestyle', 'step_linestyle'], remove_text=True)
4924+
@image_comparison(['step_linestyle', 'step_linestyle'], remove_text=True,
4925+
tol=0.2)
49254926
def test_step_linestyle():
4927+
# Tolerance caused by reordering of floating-point operations
4928+
# Remove when regenerating the images
49264929
x = y = np.arange(10)
49274930

49284931
# First illustrate basic pyplot interface, using defaults where possible.

lib/matplotlib/tests/test_lines.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ def test_invalid_line_data():
101101
line.set_ydata(0)
102102

103103

104-
@image_comparison(['line_dashes'], remove_text=True)
104+
@image_comparison(['line_dashes'], remove_text=True, tol=0.002)
105105
def test_line_dashes():
106+
# Tolerance introduced after reordering of floating-point operations
107+
# Remove when regenerating the images
106108
fig, ax = plt.subplots()
107109

108110
ax.plot(range(10), linestyle=(0, (3, 3)), lw=5)

src/_backend_agg.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ inline void RendererAgg::draw_text_image(GCAgg &gc, ImageArray &image, int x, in
745745

746746
agg::trans_affine mtx;
747747
mtx *= agg::trans_affine_translation(0, -image.dim(0));
748-
mtx *= agg::trans_affine_rotation(-angle * agg::pi / 180.0);
748+
mtx *= agg::trans_affine_rotation(-angle * (agg::pi / 180.0));
749749
mtx *= agg::trans_affine_translation(x, y);
750750

751751
agg::path_storage rect;
@@ -773,8 +773,10 @@ inline void RendererAgg::draw_text_image(GCAgg &gc, ImageArray &image, int x, in
773773
} else {
774774
agg::rect_i fig, text;
775775

776+
int deltay = y - image.dim(0);
777+
776778
fig.init(0, 0, width, height);
777-
text.init(x, y - image.dim(0), x + image.dim(1), y);
779+
text.init(x, deltay, x + image.dim(1), y);
778780
text.clip(fig);
779781

780782
if (gc.cliprect.x1 != 0.0 || gc.cliprect.y1 != 0.0 || gc.cliprect.x2 != 0.0 || gc.cliprect.y2 != 0.0) {
@@ -788,9 +790,11 @@ inline void RendererAgg::draw_text_image(GCAgg &gc, ImageArray &image, int x, in
788790
}
789791

790792
if (text.x2 > text.x1) {
793+
int deltax = text.x2 - text.x1;
794+
int deltax2 = text.x1 - x;
791795
for (int yi = text.y1; yi < text.y2; ++yi) {
792-
pixFmt.blend_solid_hspan(text.x1, yi, (text.x2 - text.x1), gc.color,
793-
&image(yi - (y - image.dim(0)), text.x1 - x));
796+
pixFmt.blend_solid_hspan(text.x1, yi, deltax, gc.color,
797+
&image(yi - deltay, deltax2));
794798
}
795799
}
796800
}
@@ -945,6 +949,7 @@ inline void RendererAgg::_draw_path_collection_generic(GCAgg &gc,
945949
facepair_t face;
946950
face.first = Nfacecolors != 0;
947951
agg::trans_affine trans;
952+
bool do_clip = !face.first && !gc.has_hatchpath();
948953

949954
for (int i = 0; i < (int)N; ++i) {
950955
typename PathGenerator::path_iterator path = path_generator(i);
@@ -992,8 +997,6 @@ inline void RendererAgg::_draw_path_collection_generic(GCAgg &gc,
992997
}
993998
}
994999

995-
bool do_clip = !face.first && !gc.has_hatchpath();
996-
9971000
if (check_snap) {
9981001
gc.isaa = antialiaseds(i % Naa);
9991002

src/_backend_agg_basic_types.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,19 @@ class Dashes
5252
template <class T>
5353
void dash_to_stroke(T &stroke, double dpi, bool isaa)
5454
{
55+
double scaleddpi = dpi / 72.0;
5556
for (dash_t::const_iterator i = dashes.begin(); i != dashes.end(); ++i) {
5657
double val0 = i->first;
5758
double val1 = i->second;
58-
val0 = val0 * dpi / 72.0;
59-
val1 = val1 * dpi / 72.0;
59+
val0 = val0 * scaleddpi;
60+
val1 = val1 * scaleddpi;
6061
if (!isaa) {
6162
val0 = (int)val0 + 0.5;
6263
val1 = (int)val1 + 0.5;
6364
}
6465
stroke.add_dash(val0, val1);
6566
}
66-
stroke.dash_start(get_dash_offset() * dpi / 72.0);
67+
stroke.dash_start(get_dash_offset() * scaleddpi);
6768
}
6869
};
6970

src/ft2font.cpp

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ ft_outline_move_to(FT_Vector const* to, void* user)
236236
*(d->vertices++) = 0;
237237
*(d->codes++) = CLOSEPOLY;
238238
}
239-
*(d->vertices++) = to->x / 64.;
240-
*(d->vertices++) = to->y / 64.;
239+
*(d->vertices++) = to->x * (1. / 64.);
240+
*(d->vertices++) = to->y * (1. / 64.);
241241
*(d->codes++) = MOVETO;
242242
}
243243
d->index += d->index ? 2 : 1;
@@ -249,8 +249,8 @@ ft_outline_line_to(FT_Vector const* to, void* user)
249249
{
250250
ft_outline_decomposer* d = reinterpret_cast<ft_outline_decomposer*>(user);
251251
if (d->codes) {
252-
*(d->vertices++) = to->x / 64.;
253-
*(d->vertices++) = to->y / 64.;
252+
*(d->vertices++) = to->x * (1. / 64.);
253+
*(d->vertices++) = to->y * (1. / 64.);
254254
*(d->codes++) = LINETO;
255255
}
256256
d->index++;
@@ -262,10 +262,10 @@ ft_outline_conic_to(FT_Vector const* control, FT_Vector const* to, void* user)
262262
{
263263
ft_outline_decomposer* d = reinterpret_cast<ft_outline_decomposer*>(user);
264264
if (d->codes) {
265-
*(d->vertices++) = control->x / 64.;
266-
*(d->vertices++) = control->y / 64.;
267-
*(d->vertices++) = to->x / 64.;
268-
*(d->vertices++) = to->y / 64.;
265+
*(d->vertices++) = control->x * (1. / 64.);
266+
*(d->vertices++) = control->y * (1. / 64.);
267+
*(d->vertices++) = to->x * (1. / 64.);
268+
*(d->vertices++) = to->y * (1. / 64.);
269269
*(d->codes++) = CURVE3;
270270
*(d->codes++) = CURVE3;
271271
}
@@ -279,12 +279,12 @@ ft_outline_cubic_to(
279279
{
280280
ft_outline_decomposer* d = reinterpret_cast<ft_outline_decomposer*>(user);
281281
if (d->codes) {
282-
*(d->vertices++) = c1->x / 64.;
283-
*(d->vertices++) = c1->y / 64.;
284-
*(d->vertices++) = c2->x / 64.;
285-
*(d->vertices++) = c2->y / 64.;
286-
*(d->vertices++) = to->x / 64.;
287-
*(d->vertices++) = to->y / 64.;
282+
*(d->vertices++) = c1->x * (1. / 64.);
283+
*(d->vertices++) = c1->y * (1. / 64.);
284+
*(d->vertices++) = c2->x * (1. / 64.);
285+
*(d->vertices++) = c2->y * (1. / 64.);
286+
*(d->vertices++) = to->x * (1. / 64.);
287+
*(d->vertices++) = to->y * (1. / 64.);
288288
*(d->codes++) = CURVE4;
289289
*(d->codes++) = CURVE4;
290290
*(d->codes++) = CURVE4;
@@ -485,13 +485,16 @@ void FT2Font::set_text(
485485
{
486486
FT_Matrix matrix; /* transformation matrix */
487487

488-
angle = angle / 360.0 * 2 * M_PI;
488+
angle = angle * (2 * M_PI / 360.0);
489489

490490
// this computes width and height in subpixels so we have to multiply by 64
491-
matrix.xx = (FT_Fixed)(cos(angle) * 0x10000L);
492-
matrix.xy = (FT_Fixed)(-sin(angle) * 0x10000L);
493-
matrix.yx = (FT_Fixed)(sin(angle) * 0x10000L);
494-
matrix.yy = (FT_Fixed)(cos(angle) * 0x10000L);
491+
double cosangle = cos(angle) * 0x10000L;
492+
double sinangle = sin(angle) * 0x10000L;
493+
494+
matrix.xx = (FT_Fixed)cosangle;
495+
matrix.xy = (FT_Fixed)-sinangle;
496+
matrix.yx = (FT_Fixed)sinangle;
497+
matrix.yy = (FT_Fixed)cosangle;
495498

496499
clear();
497500

@@ -762,8 +765,8 @@ void FT2Font::draw_glyphs_to_bitmap(bool antialiased)
762765
// now, draw to our target surface (convert position)
763766

764767
// bitmap left and top in pixel, string bbox in subpixel
765-
FT_Int x = (FT_Int)(bitmap->left - (bbox.xMin / 64.));
766-
FT_Int y = (FT_Int)((bbox.yMax / 64.) - bitmap->top + 1);
768+
FT_Int x = (FT_Int)(bitmap->left - (bbox.xMin * (1. / 64.)));
769+
FT_Int y = (FT_Int)((bbox.yMax * (1. / 64.)) - bitmap->top + 1);
767770

768771
image.draw_bitmap(&bitmap->bitmap, x, y);
769772
}
@@ -782,8 +785,8 @@ void FT2Font::get_xys(bool antialiased, std::vector<double> &xys)
782785
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyphs[n];
783786

784787
// bitmap left and top in pixel, string bbox in subpixel
785-
FT_Int x = (FT_Int)(bitmap->left - bbox.xMin / 64.);
786-
FT_Int y = (FT_Int)(bbox.yMax / 64. - bitmap->top + 1);
788+
FT_Int x = (FT_Int)(bitmap->left - bbox.xMin * (1. / 64.));
789+
FT_Int y = (FT_Int)(bbox.yMax * (1. / 64.) - bitmap->top + 1);
787790
// make sure the index is non-neg
788791
x = x < 0 ? 0 : x;
789792
y = y < 0 ? 0 : y;

0 commit comments

Comments
 (0)