sammycage / lunasvg

SVG rendering and manipulation library in C++
MIT License
866 stars 124 forks source link

After calling the function Document::rotate() the original SVG canvas is not recalculated #96

Closed rossanoparis closed 2 years ago

rossanoparis commented 2 years ago

After calling the function Document::rotate() the original SVG canvas is not recalculated, this leads to a wrong bitmap size calling this function: auto bitmap = document->renderToBitmap(); If it could be possible to inflate the SVG canvas accordingly with its rotation, the two parameters (cx & cy) to translate the image in ::rotate() wouldn’t make sense.

remark: even using the two parameters called cx and cy it is not possible to arrange certain images after a rotation.

image

Regards Rossano

sammycage commented 2 years ago

Use Document::render instead.

Example :

Original

original

Rotate


Matrix matrix;
matrix.rotate(45, document->width() / 2, document->height() / 2);

Box box(0, 0, document->width(), document->height());
box.transform(matrix);

Bitmap bitmap(box.w, box.h);
bitmap.clear(0x8080FFFF);
document->render(bitmap, matrix * Matrix::translated(-box.x, -box.y));

rotate

Flip X


Matrix matrix;
matrix.scale(-1, 1);

Box box(0, 0, document->width(), document->height());
box.transform(matrix);

Bitmap bitmap(box.w, box.h);
bitmap.clear(0x8080FFFF);
document->render(bitmap, matrix * Matrix::translated(-box.x, -box.y));

flipx

Flip Y


Matrix matrix;
matrix.scale(1, -1);

Box box(0, 0, document->width(), document->height());
box.transform(matrix);

Bitmap bitmap(box.w, box.h);
bitmap.clear(0x8080FFFF);
document->render(bitmap, matrix * Matrix::translated(-box.x, -box.y));

flipy

Skew


Matrix matrix;
matrix.shear(45, 0);

Box box(0, 0, document->width(), document->height());
box.transform(matrix);

Bitmap bitmap(box.w, box.h);
bitmap.clear(0x8080FFFF);
document->render(bitmap, matrix * Matrix::translated(-box.x, -box.y));

skew

Scale


Matrix matrix;
matrix.scale(2, 2);

Box box(0, 0, document->width(), document->height());
box.transform(matrix);

Bitmap bitmap(box.w, box.h);
bitmap.clear(0x8080FFFF);
document->render(bitmap, matrix * Matrix::translated(-box.x, -box.y));

scale

rossanoparis commented 2 years ago

Thank you @sammycage for your examples They work well !