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

Jumat, 27 November 2015

Happy Thanksgiving — Holiday Images Of New York - Vanseo Design

Happy Thanksgiving — Holiday Images Of New York - Vanseo Design


Happy Thanksgiving — Holiday Images Of New York

Posted: 26 Nov 2015 09:36 AM PST

Happy Thanksgiving to everyone in the U.S. and a very belated Happy Thanksgiving to everyone in Canada. Happy Thursday to the rest of the world.

As has become tradition for me, I'm in New York visiting family and as usual I've been taking pictures of my trip, including our long walk yesterday in Manhattan.

For the last few months I've been reading and learning how to take better pictures with my iPhone, hoping to put what I've learned into practice. Then we walk through the city and I realize I barely have time to snap shots and so most of my learning went out the window. Still I thought I'd share some of the 300+ images I captured yesterday to give you a feel for what's going on.

Window Displays

One of the things we like to take in during our walk are the window displays in some of the department stores. It's hard to take pictures of them as there's little time where someone isn't blocking the view and the camera captures an equal amount of display and reflection of the city across the street.

Macy's went with a Peanuts theme this year. Perhaps not the most original theme, but who doesn't like Charlie Brown and the gang.

Window Display Macy's

Lord & Taylor's had a candy and confection theme.

Window Display at Lord and Taylor's

I was able to press my camera up against the glass of one of the smaller windows at Lord & Taylor’s that wasn't part of the theme and was able to get rid of the reflection on this shot.

Window Display at Lord and Taylor's

For a change Saks had their display ready. It usually goes up after our visit. They had a wedding theme going and I like how the refection mixed with the display in this image.

Window Display Saks

St. Peter's Cathedral

Just past Sak's is St. Patrick's Cathedral. I find it hard to capture what you see in photographs, but hopefully these images give you a feel for what it's like to be inside such a beautiful building.

St. Patrick's Cathedral
St. Patrick's Cathedral
St. Patrick's Cathedral

Egypt at the Met

Before going inside my brother and I were sitting next to the fountain in front of the Met. I tried putting some of my learning into practice by taking a picture in a puddle just outside the fountain and I captured this reflection of the museum and water from the fountain.

Reflection of the Metropolitan Museum of Art

We only took in one exhibit this year. Egypt is part of the permanent collection at the Metropolitan Museum, but I don't think I've ever walked through the entire exhibit before.

Egypt Exhibit at the Metropolitan Museum of Art
Egypt Exhibit at the Metropolitan Museum of Art
Egypt Exhibit at the Metropolitan Museum of Art

Central Park

No walk trip through Manhattan would be complete without some time in Central Park. I love the colors of the park in the fall.

Central Park
Central Park
Central Park

Parade Floats

15 years ago when we first stumbled on the parade floats being inflated, seeing them was a casual experience. Over the years more and more security has arrived and this year it was tighter than usual. We spent as much time trying to get out as we did checking out the floats.

Macy's Thanksgiving Parade Float
Macy's Thanksgiving Parade Float
Macy's Thanksgiving Parade Float

I also realized that the older I get, the less I recognize most of the floats. I know the Kool-Aide guy, but I'm clueless who the other two are.

Happy Thanksgiving

I'll leave you with a couple of pictures of Christmas trees to get you ready for the coming holiday season. The first is the tree inside the New York Public Library The second is an origami Christmas tree from inside the Museum of Natural History.

Christmas Tree New York Public Library
Origami Christmas Tree at The Museum of Natural History

Once again, have a Happy Thanksgiving, a belated Happy Thanksgiving, or the best Thursday you can have.

Download a free sample from my book Design Fundamentals.

The post Happy Thanksgiving — Holiday Images Of New York appeared first on Vanseo Design.

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

Rabu, 25 November 2015

How To Adjust The Baseline Alignment Of SVG Text - Vanseo Design

How To Adjust The Baseline Alignment Of SVG Text - Vanseo Design


How To Adjust The Baseline Alignment Of SVG Text

Posted: 24 Nov 2015 05:30 AM PST

When you set coordinates to position SVG text, you're setting the location of the left edge of the text and you're setting the location of the baseline of the font. Last week I showed how you could adjust the former through the text-anchor attribute and today I want to show you how you can adjust the latter.

If you haven't been following along with this series about SVG text you may want to read the first post where I talk about things like font tables and EM boxes. I'll do my best to to keep things clear in this post, but an understanding of what's in the earlier post will probably be helpful.

Baseline Alignment

In the first post in this series I talked a little about font tables and how they include information such as the position to display the font's glyphs. Information about the location of the baseline of the glyph in relation to the EM box is part of the information in the tables.

Different fonts can have different baselines and so different points where they're located within the EM box. For example:

  • horizontal writing, ideographic scripts (Han Ideographs, Katakana, Hiragana, and Hangul) are aligned to a baseline near the bottom of the glyph.
  • alphabetic based scripts (Latin, Cyrillic, Hebrew, Arabic) are aligned to a baseline at the bottom of most glyphs, but some glyphs descend below the baseline.
  • Indic based scripts on the other hand are aligned at a point that is near the top of the glyphs.

If you stick with a single font there's no issue, but if you change fonts along the line it is. Changing fonts is possibly more realistic than you realize too.

For example it's not uncommon for two words in a logo to use different fonts or you might switch to a monospaced font to show code in the middle of a sentence. Depending on the fonts in question they might not align along the same baseline.

SVG uses a model that assumes a single font at a single size is the "dominant run" and all other baselines are defined in relation to it. Each font should provide information about offsets for the alternate baselines as well as an offset for math baselines in some math fonts.

The model further assumes each glyph has an alignment-baseline value to align the baseline of that glyph with the dominant baseline.

It's possible your head might be spinning after reading everything to this point. I know mine was when I first looked at baseline alignment and it's still spinning just a little so I'll skip further details.

The gist is the font tables associated with each font contain information about different baselines the glyphs might be aligned to and SVG uses the information to align the glyphs in different fonts relative to each other.

Baseline Alignment Properties

SVG provides three properties to allow you adjust these different baselines.

  • dominant-baseline – used to determine or re-determine a scaled-baseline-table
  • alignment-baseline – specifies which baseline is to be aligned with the corresponding baseline of the parent
  • baseline-shift – allows repositioning of the dominant-baseline relative to the dominant-baseline of the parent

My guess is, the last baseline-shift is the one you'll be tempted to use the most, but let me offer an example of each to try to illustrate how each can be used to refine the location of the baseline of the font you're using.

dominant-baseline

The dominant-baseline property can take any of the following values auto, use-script, no-change, reset-size, ideographic, alphabetic, hanging, mathematical, central, middle, text-after-edge, text-before-edge, inherit.

Instead of copying and pasting the definition of each I'll point you to the definitions in the spec. I'll present an example showing some of the values. I should mention the examples in this post won't work in Firefox, but they do work in Safari and Chrome. I haven't tested in other browsers.

In this example I created what should be a familiar <svg> element with viewport dimensions set along with some styles I hope are self-explanatory.

Inside the SVG I created six <text> elements. The first three are positioned so their default baseline sits on the top edge of the viewport and the last three on top of the bottom edge of the viewport.

1  2  3  4  5  6  7  8  
<svg width="660" height="220" style="outline: 1px solid red; font-size: 1.5em; overflow: visible;">      <text x="0" y="0">SVG</text>      <text x="100" y="0" dominant-baseline="hanging">SVG (hanging)</text>      <text x="300" y="0" dominant-baseline="mathematical">SVG (mathematical)</text>      <text x="0" y="220" dominant-baseline="central">SVG (central)</text>      <text x="200" y="220" dominant-baseline="middle">SVG (middle)</text>      <text x="400" y="220" dominant-baseline="text-before-edge">SVG (text-before-edge)</text>    </svg>

The dominant-baseline of the first <text> element hasn't been adjusted. The other five have and I placed the value in parenthesis so you can see the value used.

SVGSVG (hanging)SVG (mathematical)SVG (central)SVG (middle)SVG (text-before-edge)

If you'd like to see examples of the other values you can play around with the code or check here. If you do the latter the same browser warning from above applies.

alignment-baseline

The alignment-baseline property takes a similar set of values. They are auto, baseline, before-edge, text-before-edge, middle, central, after-edge, text-after-edge, ideographic, alphabetic, hanging, mathematical, and inherit.

Again I'll refrain from copying and pasting definitions of the values and point you one more time to the spec. I will present another example similar to the previous one, but using alignment-baseline instead of dominant-baseline.

The SVG element is the same as before. Because alignment-baseline is an adjustment relative to the parent element, I created two text elements, one at the top and one at the bottom and inside each I placed several tspan elements with different alignment-baseline values.

1  2  3  4  5  6  7  8  9  10  11  12  13  14  
<svg width="660" height="220" style="outline: 1px solid red; font-size: 1.25em; overflow: visible;">      <text x="0" y="0">SVG        <tspan alignment-baseline="baseline">SVG (baseline)</tspan>        <tspan alignment-baseline="middle">SVG (middle)</tspan>        <tspan alignment-baseline="central">SVG (central)</tspan>        <tspan alignment-baseline="hanging">SVG (hanging)</tspan>      </text>      <text x="0" y="220">SVG        <tspan alignment-baseline="before-edge">SVG (before-edge)</tspan>        <tspan alignment-baseline="after-edge">SVG (after-edge)</tspan>        <tspan alignment-baseline="mathematical">SVG (mathematical)</tspan>      </text>    </svg>

Again the value I used is in parenthesis to help you see the value as you're looking at the result.

SVGSVG (baseline)SVG (middle)SVG (central)SVG (hanging)SVGSVG (before-edge)SVG (after-edge)SVG (mathematical)

I'll leave it to you to try the values I didn't use in these examples if you want to see what each does or you can check here with the same browser warning.

baseline-shift

Of the three baseline alignment properties, baseline-shift is probably the one you'll use most often and it has the fewest possible values.

The baseline-shift property "allows repositioning of the dominant-baseline relative to the dominant-baseline of the parent text content element." In other words it lets you shift text up or down or rather perpendicular to the direction the text flows when displayed.

The baseline-shift property takes four values (sub, super, <percent>, <length>) and the first two will give you an idea when and why you'd want to use the property. With the latter two values, a positive value moves the text up (super) and negative value moves down (sub).

In this next example I created four text elements, each with a <tspan> inside containing the number 2. I added the following baseline-shifts to the tspans from top to bottom in the code, sub, super, –60%, and 20px. The last two values I adjusted by eye to hopefully match the sub and super values.

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">SVG<tspan baseline-shift="sub">2</tspan></text>      <text x="220" y="100">SVG<tspan baseline-shift="super">2</tspan></text>      <text x="320" y="100">SVG<tspan baseline-shift="-60%">2</tspan></text>      <text x="420" y="100">SVG<tspan baseline-shift="20px">2</tspan></text>    </svg>

SVG2SVG2SVG2SVG2

Closing Thoughts

My apologies for what might be another information dense and mostly dry post. My eyes glazed over a bit when I first took a look at the spec, but it's ultimately not all that complicated.

Fonts deliver information through font tables about where their glyphs should be positioned, including where the baseline of the glyph should be positioned.

Because different fonts will have different baseline information it's possible when using multiple fonts that they won't line up the way you want. When that happens you can adjust the three baseline properties so the glyphs are better aligned.

Next week I'll take a look at some properties you likely use all the time in CSS and show you how to apply them to SVG. We'll talk about font properties you know like font-family and font-size and some you might not know like font-stretch.

Download a free sample from my book Design Fundamentals.

The post How To Adjust The Baseline Alignment Of SVG Text appeared first on Vanseo Design.

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

Rabu, 18 November 2015

SVG Text Layout And Alignment - Vanseo Design

SVG Text Layout And Alignment - Vanseo Design


SVG Text Layout And Alignment

Posted: 17 Nov 2015 05:30 AM PST

At the start of this series on SVG text I mentioned font tables. I said that certain alignment details for rendering a font's glyphs were contained inside those tables. I also mentioned at some point in the series we'd see how we could alter some of this information

When you position SVG text, the default is to display the left edge of the EM box and the baseline of the character at the position you specify. Both can be adjusted.

However, before we get to those adjustments let's talk about how we might display SVG text for international fonts. SVG includes support for international writing directions and we can change the default direction.

Writing Modes in SVG

You can use the writing-mode property to change the inline direction from the default, which is left-to-right. You can set the text to display either right-to-left or top-to-bottom instead.

It works like the css writing-mode property which does the same thing. In SVG, writing-mode is an attribute with the following values.

  • lr-tb | lr — either sets direction as left to right (default)
  • rl-tb | rl — either sets direction as right to left
  • tb-rl | tb — either sets direction as top to bottom

The values above show there are two ways to set the specific value you want depending on which direction you want the text to flow. I'll stick to the two character values and save the extra bit of typing.

As always let's start with an example and build on it. I created an SVG element, set the viewport size, gave it an outline, bumped up the font-size, and set the overflow to visible in case any text renders outside of the viewport.

1  2  3  
<svg width="660" height="220" style="outline: 1px solid red; font-size: 2em; overflow: visible;">      <text x="300" y="100" writing-mode="tb">SVG</text>    </svg>

I set the writing-mode as tb or top to bottom and you can see the text flows vertically, though the characters are now rendered on their sides.

SVG

Unfortunately I haven't been able to get a left-to-right writing-mode working at all. The values I showed for writing-mode have been deprecated for everything other than SVG, though they should still work with SVG.

There's a different set of values if you want to use the CSS writing-mode property, though the values don't include a right-to-left direction.

Regardless of my issues you should be able to set the writing-mode so it renders right-to-left using either rl or rl-tb as the writing-mode value.

Let's get back to the example again and note that while the text was rendered from top to bottom, the characters are also rotated 90 degrees. You might think the solution is to use the SVG rotate attribute and set them back.

1  2  3  
<svg width="660" height="220" style="outline: 1px solid red; font-size: 2em; overflow: visible;">      <text x="300" y="100" writing-mode="tb" rotate="-90">SVG</text>    </svg>

I added a rotation of –90, which does rotate the characters so they're upright, but as you can see it doesn't look quite right.

SVG

Let's try something else.

Glyph Orientation

You can rotate individual characters with either of the two glyph orientation properties.

  • glyph-orientation-horizontal — when writing-mode is left-to-right or right-to-left.
  • glyph-orientation-vertical — when writing-mode is top-to-bottom.

The values of both are limited to 0, 90, 180, and 270.

1  2  3  
<svg width="660" height="220" style="outline: 1px solid red; font-size: 2em; overflow: visible;">      <text x="300" y="100" writing-mode="lr" glyph-orientation-horizontal="90">SVG</text>    </svg>

Here I set writing-mode to its default left to right and changed glyph-orientation-horizontal to 90. Each character is now rotated 90 degrees.

SVG

Let's fix the problem we encountered with top-to-bottom writing-mode in the last section using glyph-orientation-vertical.

1  2  3  
<svg width="660" height="220" style="outline: 1px solid red; font-size: 2em; overflow: visible;">      <text x="300" y="100" writing-mode="tb" glyph-orientation-vertical="0">SVG</text>    </svg>

Note that 0 is the value to get the characters to sit upright, which may or may not be what you expect.

SVG

I'll let you experiment with the other values. This probably isn't something you'll use in practice a lot unless you often use a writing-direction of top-to-bottom as some Eastern Asian languages do.

Then again, it's possible you just want to get creative and play with different writing-modes and glyph orientations.

The text-anchor Property

By default when you position SVG text the position you specify is aligned with the left edge and the baseline of the text.

One property you may find useful is the text-anchor property, which lets you align text horizontally at the start, middle, or end of the EM box.

Here's an example comparing all three values. I set three lines of the same text at different y-coordinates. All three lines have an x-coordinate of 0 so we can see how they render against the left edge of the viewport.

1  2  3  4  5  
<svg width="660" height="220" style="outline: 1px solid red; font-size: 2em; overflow: visible;">      <text x="0" y="60" text-anchor="start">SVG</text>      <text x="0" y="100" text-anchor="middle">SVG</text>      <text x="0" y="140" text-anchor="end">SVG</text>    </svg>

I set the text-anchor of each to the three different values. You can probably tell by looking which line of text has which value set, but the order from top to bottom is start, middle, end.

SVGSVGSVG

There are also some properties for adjusting SVG text along the baseline, but let's save those for next week.

Closing Thoughts

The writing mode of SVG text can be changed from its default left-to-right orientation to display either right-to-left or top-to-bottom. While I've been able to get the latter working, I haven't had much success with the former.

When the writing-mode is changed from top-to-bottom, the glyphs are rotated with the line of text. If you want to rotate the glyphs in any writing-mode you can use either glyph-orientation-horizontal or glyph-orientation-vertical, depending on the writing-mode being used.

Finally if you want to change where the EM box around a glyph is aligned you can set the text-anchor property to either start, middle, or end.

Next week I'll cover baseline alignment of SVG text.v

Download a free sample from my book Design Fundamentals.

The post SVG Text Layout And Alignment appeared first on Vanseo Design.

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

Rabu, 11 November 2015

How To Reuse SVG Text With The tref Element - Vanseo Design

How To Reuse SVG Text With The tref Element - Vanseo Design


How To Reuse SVG Text With The tref Element

Posted: 10 Nov 2015 05:30 AM PST

Writing SVG code once and being able to use it in multiple places helps you write more modular code and it helps make maintenance easier. SVG makes it easy to reuse text through the tref element.

Last week I walked you through the tspan element and showed how you could use it to style and position lines of text independent from one another. I presented some examples using the positioning attributes x, y, dx, and dy and mentioned there were a couple more attributes to look at.

Let's pick things up with the two remaining attributes and then I'll talk about reusing SVG text with the tref element. Please note that while the tref element is part of the SVG 1.1 spec, it’s being removed from the SVG 2.0 spec. I included in this series, in case you run across it somewhere.

The textLength and lengthAdjust Attributes

The two remaining attributes are textLength and lengthAdjust, and either or both can be added directly to any tspan.

As I've done with most of the examples in this series. I created an SVG element, defined the size of the viewport, and styled it so we can see an outline of the viewport and any text that renders outside of it. I also bumped up the font-size.

1  2  3  4  5  6  
<svg width="660" height="220" style="outline: 1px solid red; font-size: 2em; overflow: visible;">      <text textLength="400" lengthAdjust="spacingAndGlyphs">        <tspan x="280" y="80">SVG 1</tspan>        <tspan x="280" dy="36" fill="red">SVG 2</tspan>      </text>    </svg>

I positioned the two tspans so one sits below the other and styled the second with a red fill. I also added both textLength and lengthAdjust attributes to the <text> element. The text isn't stretched as you might expect.

SVG 1SVG 2

However, adding the attributes to the first tspan does what you would expect. It stretches both the space and glyphs as the next example shows.

1  2  3  4  5  6  
<svg width="660" height="220" style="outline: 1px solid red; font-size: 2em; overflow: visible;">      <text x="280" y="40">        <tspan x="280" y="80" textLength="400" lengthAdjust="spacingAndGlyphs">SVG 1</tspan>        <tspan x="280" dy="36" fill="red">SVG 2</tspan>      </text>    </svg>

SVG 1SVG 2

This suggests that unlike the rotate attribute, which we saw last week, textLength and lengthAdjust set on the text element don't propagate to the tspans.

One exception I found while playing around is if you add the textLength and/or lengthAdjust to the text element and haven't set x and y values for the first tspan, the textLength and lengthAdjust do seem to propagate.

I'm not sure if this is an error or the intended behavior, but I wanted to mention it in case it happens and you're wondering why.

1  2  3  4  5  6  
<svg width="660" height="220" style="outline: 1px solid red; font-size: 2em; overflow: visible;">      <text x="280" y="80" textLength="400" lengthAdjust="spacingAndGlyphs">hello        <tspan>SVG 1</tspan>        <tspan x="280" dy="36" fill="red">SVG 2</tspan>      </text>    </svg>

In this example I added the text "hello" directly to the text element and before the first tspan and I added our two attributes directly on the text element. I also positioned the text element and since I didn't give any x or y coordinate to the first tspan it gets positioned just after the "hello" text.

The word "hello" is stretched as you would expect, but so is the SVG 1 text from the first tspan. It seems setting textLength and lengthAdjust stretch the line they're applied to regardless of which element they're set on.

helloSVG 1SVG 2

If you take away the coordinates on the second tspan, it will also become part of the stretched line of SVG text.

helloSVG 1SVG 2

Again I'm not sure if this is the expected behavior, a bug, or if I'm doing something wrong, but I thought I'd point it out in case you run across it.

The tref Element

Note: The tref element is part of the SVG 1.1 spec, but it’s being removed from the SVG 2.0 spec. What follows may or not work by the time you read this. I’ll be using images to show the results of the examples.

Earlier in the year during the previous round of this SVG series I showed how you could define SVG code in one location and then use and reuse it in other parts of your SVG document.

The tref element works the same way. It lets you define reference text inside a <defs> element and then reference it later. I'll refer you back to my post about <defs> if you need a reminder about how this works.

Or you can just follow along with the example in which I created a <text> element with SVG 1 inside and then I wrapped the <text> element with <defs> tags. Notice that I added an id of "referenced" to the <text> element.

To reference the text you use the xlink:href attribute inside a <tref> element where you want the text to appear.

1  2  3  4  5  6  7  8  9  10  11  12  
<svg width="660" height="220" style="outline: 1px solid red; font-size: 2em; overflow: visible;">      <defs>        <text id="referenced">SVG</text>      </defs>      <text>        <tspan x="280" y="80">          <tref xlink:href="#referenced"/>        </tspan>        <tspan x="280" dy="36" fill="red">SVG 2</tspan>      </text>    </svg>

Here I referenced the SVG 1 text inside a tref element while SVG 2 is inside a tspan that I've positioned beneath SVG 1.

SVG tref

I could be more efficient and set the defs to contain the text SVG and reference it in both tspans adding the 1 or 2 at the end.

1  2  3  4  5  6  7  8  9  10  11  12  13  14  
<svg width="660" height="220" style="outline: 1px solid red; font-size: 2em; overflow: visible;">      <defs>        <text id="referenced-2">SVG</text>      </defs>      <text>        <tspan x="280" y="80">          <tref xlink:href="#referenced-2"/> 1        </tspan>        <tspan x="280" dy="36" fill="red">          <tref xlink:href="#referenced-2"/> 2        </tspan>      </text>    </svg>

Here I added the 1 and 2 on each tspan, but have the SVG text as a reference inside trefs in each. I also changed the id to referenced–2 so that it doesn't conflict with the previous example.

SVG tref

In these examples I placed the tref inside tspans, but the tref could also have been placed directly inside the text element. The referenced text will appear wherever the tref is added.

Closing Thoughts

If you understand how to work with <defs> and how to generally reuse SVG code, I'm guessing you won't have any problems working with the <tref> element either as it works the same way.

You define the text you want to reuse inside <defs> tags and then reference the text inside a <tref> element using the xlink:href attribute. It's more complicated to explain that to use.

There's still plenty more to cover in regards to SVG text. Over the next two week I'll talk about text layout features that are supported by SVG and show you some properties to help align SVG text in both the horizontal and vertical directions.

Download a free sample from my book Design Fundamentals.

The post How To Reuse SVG Text With The tref Element appeared first on Vanseo Design.

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