Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust baseline position to configured angle #3331

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

M393
Copy link

@M393 M393 commented Dec 8, 2021

This adjusts the baseline according to the configured angle, see #3330

@M393 M393 marked this pull request as draft December 8, 2021 10:11
@M393 M393 marked this pull request as ready for review December 9, 2021 10:38
@M393
Copy link
Author

M393 commented Dec 9, 2021

This does not solve the multiline problem and it still is broken with alignments other than left (though arguably less).
Matrix transforms may need to be handled differently, but I didn't test it because unfortunatly I don't have the time to look deeper.
After:
jspdf-fix-after
Before:
jspdf-fix-before

Test code
<html>
    <head>
        <meta charset="utf-8">
        <title>PDF Test</title>
        <!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.4.0/jspdf.umd.min.js"></script> -->
        <script src="lib/jsPDF/jspdf.umd.js"></script>
    </head>
    <body>
        <button id="btn">Create PDF</button>
        <script>
            (function () {
                let url;
                function testPdf() {
                    if (url) {
                        URL.revokeObjectURL(url);
                    }
                    const pageSize = [ 100, 100 ];
                    const doc = new jspdf.jsPDF({
                        orientation: 'landscape',
                        unit: 'mm',
                        format: pageSize,
                    });

                    const pad = 20;

                    doc.setFillColor('yellow');
                    doc.rect(pad, pad, pageSize[0] - 2 * pad, pageSize[1] - 2 * pad, 'F');

                    doc.text(['TOP'],    pageSize[0] / 2,   pad,               { angle: 0,   baseline: 'bottom', align: 'left' });
                    doc.text(['RIGHT'],  pageSize[0] - pad, pageSize[1] / 2,   { angle: 270, baseline: 'bottom', align: 'left' });
                    doc.text(['BOTTOM'], pageSize[0] / 2,   pageSize[1] - pad, { angle: 180, baseline: 'bottom', align: 'left' });
                    doc.text(['LEFT'],   pad,               pageSize[1] / 2,   { angle: 90,  baseline: 'bottom', align: 'left' });

                    doc.text(['CORNER'], pad,               pad,               { angle: 45,  baseline: 'bottom', align: 'left' });
                    doc.text(['CORNER'], pageSize[0] - pad, pad,               { angle: 315, baseline: 'bottom', align: 'left' });
                    doc.text(['CORNER'], pad,               pageSize[1] - pad, { angle: 135, baseline: 'bottom', align: 'left' });
                    doc.text(['CORNER'], pageSize[0] - pad, pageSize[1] - pad, { angle: 225, baseline: 'bottom', align: 'left' });

                    const blob = doc.output('blob');
                    url = URL.createObjectURL(blob);
                    window.open(url);
                }
                document.querySelector('#btn').addEventListener('click', testPdf);
            })();
        </script>
    </body>
</html>

Copy link
Collaborator

@HackbrettXXX HackbrettXXX left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR. If we fix this, I think we should also fix it for text anchors other than left and for matrices. Could you implement this, as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants