Selasa, 08 Desember 2015

How To Work With The SVG Text Spacing Properties - Vanseo Design

How To Work With The SVG Text Spacing Properties - Vanseo Design


How To Work With The SVG Text Spacing Properties

Posted: 08 Dec 2015 05:30 AM PST

Most of the time the default spacing between characters and words is perfectly fine, but when working with display type in headlines and subheads you may want to adjust the spacing in your text. SVG provides a way to do just that.

Last week I walked you through the different font selection properties available for use as attributes on SVG text elements. This week I want to talk about some other typographic related properties, specifically the SVG spacing properties.

Kerning

Spacing Within Lines of Text

SVG 1.1 provides three different spacing properties, kerning, letter-spacing, and word-spacing. Kerning is being removed in SVG 2.0, though I'll include it in the discussion here in case you run across it.

Here are definitions for each. I'll offer a couple of examples showing the properties in use momentarily.

  • kerning—adjusts the space between specific letter pairs such as AV.
  • letter-spacing —adjusts the space between all letters. It's equivalent to tracking in non-CSS talk.
  • word-spacing —adjusts the space between words and not individual characters.

You may already be familiar with and have used the latter two properties, letter-spacing ad word-spacing, in your CSS. In fact we usually consider letter-spacing to be the CSS version of kerning, which isn't exactly true.

Letter spacing deals with every letter and kerning deals with specific pairs of letters that require adjusting. However while kerning and letter spacing are different things, they're going to do the same thing when you set their values. They're going to move letters further apart (positive value) or closer together (negative value).

Here's an example to illustrate.

1  2  3  4  5  6  
<svg width="660" height="220" style="outline: 1px solid red; font-size: 2em; overflow: visible;">      <text x="80" y="50" font-family="georgia" kerning="-2">Is this kerning or letter spacing?</text>      <text x="80" y="80" font-family="georgia" letter-spacing="-2">Is this kerning or letter spacing?</text>      <text x="80" y="150" font-family="georgia" kerning="2">Is this kerning or letter spacing?</text>      <text x="80" y="180" font-family="georgia" letter-spacing="2">Is this kerning or letter spacing?</text>    </svg>

In each pair the first line of text uses kerning and the second uses letter-spacing. The lines in the top pair both have values of –2 and the lines in the bottom pair both have a value of 2.

Is this kerning or letter spacing?Is this kerning or letter spacing?Is this kerning or letter spacing?Is this kerning or letter spacing?

You can see both pairs look the same. It's really up to you to use them properly. Kerning should be applied to specific pairs of letters, so you'd want to wrap the letters in a tspan and then apply kerning.

Whether to use one or the other is a non-issue in SVG 2.0 as kerning is being removed from the spec so you probably shouldn't use it at all at this point, but I thought I should mention it in case you come across it.

The word-spacing property adjusts the spaces between words and not letters themselves. Here's an example. I've used the same line of text inside two different <text> elements. The only difference between them is the word-spacing. It's set to –2 in the first line and 2 in the second.

1  2  3  4  
<svg width="660" height="220" style="outline: 1px solid red; font-size: 2em; overflow: visible;">      <text x="100" y="100" font-family="georgia" word-spacing="-2">This is a word-spacing of -2</text>      <text x="100" y="130" font-family="georgia" word-spacing="2">This is a word-spacing of 2</text>    </svg>

You can see that the word "This" has the same amount of space between characters and then the space between "This" and "is" varies due to the different word-spacing values. The same is true of the other words and spaces, though it's easiest to see with the first couple of words.

This is a word-spacing of -2This is a word-spacing of 2

White Space

One thing SVG text doesn't do is wrap text automatically. Instead SVG text will keep going and going in a straight line. Remember that the viewport we set using the width and height attributes is the size of the window that lets us peer into an infinite SVG canvas. The canvas is still infinite though, and while you may not see the text outside the viewport, it can still render outside the viewport.

If you want longer blocks of text to remain inside the viewport, you can use tspans and position them where you want. Other than explicitly positioning bits of text or using the properties I've discussed so far in this post, you have little control over the white space in your text.

SVG 2.0 is supporting the CSS white-space property, though at the moment I don't think any browser is supporting it as an SVG attribute. It's meant to replace the SVG 1.1 xml:space property, which while limited in what it can do, does work.

Let me briefly explain the idea of the white-space property and then I'll show you an example using xml:space.

The white-space property will specify:

  • whether and how white space inside the element is collapsed
  • whether lines may wrap at unforced soft wrap opportunities

The property takes any of five values. The spec didn't see fit to offer definitions and instead points you to definitions for six values in the CSS3 Text Module Level 3 spec. Below is an image of the table that summarizes the behavior of each value.

The white-space property

Here's a bit more definition from W3Schools. Scroll down to the Property Values section for the definitions.

The idea is that some white space is collapsed and some is preserved and sometimes a line of text can wrap and sometimes it can't. That makes me hopeful that SVG text will at some point be able to wrap automatically.

The white-space property is replacing xml:space from SVG 1.1, which offers less control as it only takes two values, default and preserve. However, since xml:space does work I'll show you an example.

I created two text elements with the text "Having fun with whitespace" and I added tspans so the word "whitespace" could be separated from the rest of the text. I then added some tabs before the whitespace span.

1  2  3  4  
<svg width="660" height="220" style="outline: 1px solid red; font-size: 1.5em; overflow: visible;">       <text x="50" y="100" xml:space="default"><tspan>Having fun with</tspan>             <tspan>whitespace (default)</tspan></text>       <text x="50" y="140" xml:space="preserve"><tspan>Having fun with</tspan>            <tspan>whitespace (preserve)</tspan></text>    </svg>

The first line uses xml:space="default" and you can see the tabs are collapsed into a single space. The second line uses xml:space="preserve" which preserves the extra whitespace.

Having fun with whitespace (default)Having fun with whitespace (preserve)

The SVG 2.0 white-space property will essentially do the same thing, but it will also provide a means to preserve new lines instead of whitespace on a single line.

Text Decoration

I probably should have included this section in last week's post on font properties, but since I didn't, here it is today. You can set the text decoration of SVG text. Since I assume you know how to use text-decoration as a CSS property, I'll simply offer an example using it as an SVG attribute.

1  2  3  4  5  
<svg width="660" height="220" style="outline: 1px solid red; font-size: 2em; overflow: visible;">      <text x="220" y="60" font-family="georgia" text-decoration="overline">Text Decoration</text>      <text x="220" y="120" font-family="georgia" text-decoration="underline">Text Decoration</text>      <text x="220" y="190" font-family="georgia" text-decoration="line-through">Text Decoration</text>    </svg>

From top to bottom values are overline, underline, and line-through.

Text DecorationText DecorationText Decoration

Believe it or not there's also a blink value, but fortunately I couldn't get it to work, which hopefully means it's been deprecated. We don't need a return of blink, except as an episode of Doctor Who, which is worth watching again and again.

Closing Thoughts

You probably don't use the CSS versions of these properties often. I know I don't change the spacing between letters and words in ordinary text, much, if ever.

However, when it comes to display type there are times when it makes sense to adjust the spacing and since you're more likely to use SVG text as display type, it's a good idea to know how to adjust the white space when you want.

There's one last topic I want to cover about SVG text and that's how to display your text along a path of your choosing instead of having it always be along a straight line. The next couple of weeks I'll take a look at the <textPath> element and its associated attributes.

Download a free sample from my book Design Fundamentals.

The post How To Work With The SVG Text Spacing Properties appeared first on Vanseo Design.

This posting includes an audio/video/photo media file: Download Now

Rabu, 02 Desember 2015

How To Use SVG Font Properties - Vanseo Design

How To Use SVG Font Properties - Vanseo Design


How To Use SVG Font Properties

Posted: 01 Dec 2015 05:30 AM PST

I've said throughout this series that SVG text gains the best of both worlds in that it's rendered as a graphic while still being selectable and searchable text.

So far, I've been focused in the "rendered as graphic" world. Today I want to talk more about the text side of things. I want to talk about how you can change the font of the text and set its weight and similar.

Much of what's in this post will likely be familiar to you in that you've set font-size as a CSS property. We'll look at these properties as attributes on SVG text elements here. You can think of this post as a review of what you can do with text in general and how to manipulate SVG text specifically.

SVG Font Selection Properties

SVG provides seven different font properties along with shorthand to set five of them. You're probably familiar with the five, though the other two are possibly new to you. They were for me.

Here's the list of all the font properties you can use with SVG. You can use any of them as attributes on the <text> or <tspan> elements. You can also set them as CSS properties, though I'll be using them as attributes throughout this post.

  • font-family
  • font-style
  • font-variant
  • font-weight
  • font-size
  • font-size-adjust
  • font-stretch
  • font

The font shorthand is only available as a CSS property and can't be used as an attribute on SVG elements. One thing to note when using it with SVG is the line-height has no effect. With SVG it's assumed to the the same as the font-size.

I trust you know how to work with the first five properties and have used them before in stylesheets. I'll show you a quick example that uses all five and then we can talk in a little more depth about font-size-adjust and font-stretch.

In the following example I repeated the text "SVG" four times, each time using a different font-family. I also adjusted an additional font property on each so you can that they work.

1  2  3  4  5  6  
<svg width="660" height="220" style="outline: 1px solid red; font-size: 2em; overflow: visible;">      <text x="130" y="100" font-family="Palatino" font-variant="small-caps">svg</text>      <text x="230" y="100" font-family="Helvetica" font-style="italic">SVG</text>      <text x="330" y="100" font-family="Georgia" font-weight="bold">SVG</text>      <text x="430" y="100" font-family="Verdana" font-size="1.5em">SVG</text>    </svg>

The font-families in order are Palatino (font-variant), Helvetica (font-style), Georgia (font-weight), and Verdana (font-size). In parenthesis is the property I adjusted for each. I set the first in lowercase, because I chose a value of small-caps for the font-variant and it's easier to notice the difference this way.

Again, while you could set all these properties in a CSS file, here they're all set as attributes on the <text> element.

svgSVGSVGSVG

I'm assuming you don't need any more explanation about this example and may not even have needed me to show it. Let's move on to the properties you may not be familiar with.

The font-stretch Property

The font-stretch property allows you to adjust how much the glyphs are expanded or condensed. It's done by having the browser choose the appropriate face from a family of fonts.

Browser support is currently limited to Internet Explorer, Edge, and Firefox and while you may never have used it, font-stretch is a valid CSS property with the following possible values.

  • normal
  • wider
  • narrower
  • ultra-condensed
  • extra-condensed
  • condensed
  • semi-condensed
  • semi-expanded
  • expanded
  • extra-expanded
  • ultra-expanded
  • inherit

You might think the property does exactly what it says and stretches or condenses whatever font you're using, but that's not quite right.

The font-stretch property is meant for fonts that come with several faces and it substitutes the best face based on the value set on the property.

I think this will make more sense with an example. Here I set three lines of text and for each I used the font Myriad Pro. I chose Myriad Pro, because it included a condensed version. I set the font-stretch property on the second line of text to condensed and the third line to expanded.

1  2  3  4  5  
<svg width="660" height="220" style="outline: 1px solid red; font-size: 2em; overflow: visible;">       <text x="120" y="80" font-family="myriad pro">This text is Myriad Pro</text>       <text x="120" y="120" font-family="myriad pro" font-stretch="condensed">This text is Myriad Pro condensed</text>       <text x="120" y="160" font-family="myriad pro" font-stretch="expanded">This text is Myriad Pro expanded</text>    </svg>

Because this only works in a few browsers and because you may or may not have Myriad Pro installed on your computer, I'm showing a screenshot of the results when I viewed the code in Firefox.

font-stretch

Let me explain what's going on. The first line of text doesn't have a font-stretch property set so you see the normal font-face. The second line of text is set as condensed so Firefox substituted the condensed face of the font. The third line is set to expanded. Since I don't have an expanded version of Myriad Pro, Firefox used the normal face.

I hope that makes sense. The idea is that supporting browsers choose the best face of the specific font based on the value of font-stretch and the available faces of the specific font.

The font-size-adjust Property

Hopefully when you set font-families on HTML elements you use a font-stack in case someone viewing the page doesn't have your first choice installed.

The downside is that different fonts have different x-heights and if the second choice in the stack is used, it might throw off the font-size and consequently throw off any measurements you've based on that font-size.

This happens because all fonts have an "aspect value," which is the difference in size between the lowercase x and uppercase X of that font. The font-size-adjust property allows you to change the aspect value so the different fonts will display at the same size.

Again support is limited, this time to Firefox only and as with font-stretch, I think this will make more sense with an example

Here I have two lines of SVG text each with two <text> elements. On each line Baskerville is the font on the left and Futura is the font on the right. I placed lowercase x's in the middle so you can better compare the different sizes of the same letter.

1  2  3  4  5  6  
<svg width="660" height="220" style="outline: 1px solid red; font-size: 2em; overflow: visible;">      <text x="120" y="100" font-family="baskerville">Baskerville xx</text>      <text x="300" y="100" font-family="futura">xx Futura</text>      <text x="120" y="130" font-family="baskerville">Baskerville xx</text>      <text x="300" y="130" font-family="futura" font-size-adjust="0.42">xx Futura</text>    </svg>

Due to the lack of browser support I'll again show a screenshot instead of the live code. On the first line of text I didn't set the font-size-adjust property. This is the control group. You can see that Futura has a larger x-height than Baskerville.

On the second line I set the font-size-adjust of Futura only to 0.42. You can see it now has a smaller x-height. I tried to make the x-heights of both equal, though it's possible I'm off a bit. Even if I am, I hope it's clear what happened.

font-size-adjust

Closing Thoughts

I suspect much of this post has been a review. Something tells me you've used most of these font properties before, but as CSS properties and not as attributes on SVG elements. I trust you're now aware they can be used as attributes.

The two properties that might have been new to you are font-stretch and font-size-adjust. Neither has much support at the moment so you probably won't be using them right away, but hopefully you'll know how when support is better.

My goal with this post was to show you that SVG text is text and the same font properties you often set on regular text will work with SVG text too.

Next week I want to continue to look at SVG text as text and talk about the spacing properties SVG provides. We'll look at word-spacing and letter-spacing and a couple of other properties as well.

Download a free sample from my book Design Fundamentals.

The post How To Use SVG Font Properties appeared first on Vanseo Design.

This posting includes an audio/video/photo media file: Download Now