DannyBen / victor

Ruby SVG Image Builder

Home Page:https://victor.dannyb.co

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is it possible to nest tspan elements inside text elements?

brunchboy opened this issue · comments

I could not find any examples that showed how I would do this (and forgive me if it is obvious, I am coming back to Ruby after many years away, as I contemplate using this gem to help me port the basic features of the bytefield LaTeX package to something that can be used inside Asciidoctor). One thing I need to be able to do is center a span of text in which elements have different styles, which seems possible to do in SVG by nesting tspan objects inside text objects, but I can’t see how I would emit such a structure with Victor.

The way to place text with spans inside a text element, is to use the text...do syntax, and put all the elements that are inside the text element in tspans (or any other element that makes sense):

For example:

Input

require 'victor'

svg = Victor::SVG.new

svg.build do 
  text font_size: 20, font_family: 'Verdana' do
    tspan 'You are'
    tspan 'not', fill: "red", font_weight: "bold"
    tspan 'a banana'
  end
end

puts svg.to_s

Output:

<text font-size="20" font-family="Verdana">
  <tspan>You are</tspan>
  <tspan fill="red" font-weight="bold">not</tspan>
  <tspan>a banana</tspan>
</text>

But - it is an interesting question. I am going to look into it, maybe we can easily provide a way to generate a text without a surrounding tag.

What do you think about the new PR #36? Will this help your case?

Oh, thanks very much! I did not realize the text…do syntax was even an option, since I did not see any examples like that, but it would certainly work, and the new PR makes things even nicer.

Great. I will take a look again at the README and examples, and try to make it clearer - anything can be used with a block, to nest elements under it.

Released version 0.2.6 with the tagless function.