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

text following circular path #21

Closed
marekyggdrasil opened this issue Sep 21, 2020 · 6 comments
Closed

text following circular path #21

marekyggdrasil opened this issue Sep 21, 2020 · 6 comments

Comments

@marekyggdrasil
Copy link
Contributor

Hello!
I was wondering if it is possible to recreate effect like on following example? Of text following a curved path.

<svg xmlns="http://www.w3.org/2000/svg"
     width="300" height="100" viewBox="0 0 300 100">

  <path id="MyPath" stroke="lightblue" fill="none"
	d="M 50,50 C 100,0 200,100 250,50"/>

  <text style="font: 24px sans-serif;">
    <textPath href="#MyPath">Text on a path.</textPath>
  </text>

</svg>

Screenshot 2020-09-21 at 10 49 52 PM

reference: https://www.w3.org/TR/SVG/text.html

more examples:
http://thenewcode.com/482/Placing-Text-on-a-Circle-with-SVG
https://css-tricks.com/snippets/svg/curved-text-along-path/
https://codepen.io/rdfriedl/pen/rOegaP

Thanks a lot for hints!

@cduck
Copy link
Owner

cduck commented Sep 21, 2020

The textPath element has not been implemented yet. You can subclass DrawingBasicElement or DrawingParentElement yourself to make any SVG tag (and maybe make a pull request).

This should get you started:

import drawSvg as draw
import xml.sax.saxutils as xml

class _TextPathNode(draw.DrawingBasicElement):
    TAG_NAME = 'textPath'
    hasContent = True
    def __init__(self, text, path, **kwargs):
        super().__init__(xlink__href=path, **kwargs)
        self.escapedText = xml.escape(text)
    def writeContent(self, idGen, isDuplicate, outputFile, dryRun):
        if dryRun: return
        outputFile.write(self.escapedText)

class TextOnPath(draw.DrawingParentElement):
    TAG_NAME = 'text'
    def __init__(self, text, fontSize, path, **kwargs):
        super().__init__(font_size=fontSize, **kwargs)
        self.append(_TextPathNode(text, path))


d = draw.Drawing(300, 100, origin=(0, 0), displayInline=False)

path = draw.Path(stroke='lightblue', fill='none')
path.M(50, 100-50).C(100, 100-0, 200, 100-100, 250, 100-50)
d.append(path)

text = TextOnPath('Text on a path.', 24, path)
d.append(text)

d

output image

@marekyggdrasil
Copy link
Contributor Author

Thanks for a starter! I'll be happy to contribute!

@lthumbe
Copy link

lthumbe commented Feb 12, 2021

The textPath element has not been implemented yet. You can subclass DrawingBasicElement or DrawingParentElement yourself to make any SVG tag (and maybe make a pull request).

Does this apply to the 'basic drawing elements' example? I can't get path text or multi-line text working (lines 32-33). The error states required positional arguments 'x' and 'y' are missing.

@cduck
Copy link
Owner

cduck commented Feb 12, 2021

@lthumbe You probably have an old version of drawSvg. The pull request linked right above this added the feature for the version 1.8.1 which was recently published (and I forgot to close this issue). From your terminal run pip show drawSvg to check the version. To update, run pip install --upgrade drawSvg.

Let me know if this works then I'll close this issue.

@lthumbe
Copy link

lthumbe commented Feb 12, 2021

@cduck Upgrade fixed it. Thanks!

@cduck
Copy link
Owner

cduck commented Feb 12, 2021

Closing because text following a path was implemented by #22.

@cduck cduck closed this as completed Feb 12, 2021
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

No branches or pull requests

3 participants