ajstarks / svgo

Go Language Library for SVG generation
Other
2.14k stars 169 forks source link

Adds a way to set CSS styles on elements. #35

Closed ghost closed 7 years ago

ghost commented 7 years ago

Hi,

I needed a way to add styling to elements, so I added this function. I finally got tired of the mess having a patch on your repo makes of vendoring, so am asking you to merge it upstream.

Thanks,

ajstarks commented 7 years ago

Interesting. Can you show a use of the function?

ghost commented 7 years ago

Sure. I use it in code to embed CSS in the SVG being output:

    c := svg.New(os.Stdout)
    c.Start(640, 480)
    c.Def()
    c.Style(css)           // <--------- Right here
    c.DefEnd()
    c.End()

where css is some block of CSS code, maybe from an asset.

The commit message was misleading, implying that Style applies to XML Elements; it's just a way of including arbitrary CSS in the CSS definition section, a-la

<svg width="640" height="480">
   <defs>
      <style>
         svg rect {
            fill: inherit;
         }
         .boxybox {
            fill: blue;
            stroke: black;
            stroke-width: 2;
         }
      </style>
   </defs>
   <rect x="100" y="70" width="100" height="70" class="boxybox"/>
</svg>
ajstarks commented 7 years ago

I'm wondering if we want to have another API (see the Script function):

c.Style("text/css", "/path/to/asset.css")

or

c.Style("text/css", css)

ghost commented 7 years ago

Sure, that makes sense. So the payload is either a URL or a byte array of raw CSS? And Style would have to figure out which?

ajstarks commented 7 years ago

Yes, the code in Script does this.

ghost commented 7 years ago

Updated pull request such that Style() and Script() use the same code. It may have to be inlined if you decide to add tag-specific attributes, but right now the only difference in the two elements were the tag names so I have them using a helper function.

ajstarks commented 7 years ago

Updated with revision 19107c6 and f4dea3d