Text in SVG: 3nd Attempt (Size Reduction)

September 10, 2014Category: WebTags: ,

Several days ago I wrote about finally solving the text in SVG problem. The key was to pick an open-source font, obtain the woff and svg font files, convert those files into base64 data streams, and plop the whole thing inside the actual SVG itself. And it works!

Problem is, I overlooked the file size. My original PNG logo file is 20KB. The new SVG is 400KB. That’s 2000% increase in size! And completely unacceptable to me. I set out to find ways to decrease the size.

My first find was the tspan element. This is an element, similar to HTML’s span, which can be embedded in a text element. Instead of having a new text element for every enlarged letter, I can just wrapper those letters in a tspan element and use CSS to apply the font. This helps in rendering time, but unfortunately doesn’t significantly change the file size (409KB to 408KB).

I then tried removing whitespace. It screwed up the file and didn’t really save space.

My final idea was to look at the cause of the large size. The current SVG had both an SVG and WOFF font file embedded. Looking at the font size, it turns out that the SVG font files were huge. 120ish KB each, for the two fonts I’m using. And an SVG font is readable if one opens it in a text editor. It’s kind of odd to look at, but I found a pattern, and I found saw each letter and typical character. I simply removed all characters that I’m not using for the logo. Doing this for both fonts and reencoding in base64 netted me a huge size reduction. The SVG logo is now down from 408KB to 120KB. That is still far larger than 20KB, but it’s a gain I can live with given the sharpness benefits of SVG.

This does mean that if the text ever changes in the logo, I’m going to have to go back to my original font files and add or subtract certain characters. But the text is a company name, meaning it won’t change often. I’ve tested in Chrome and Firefox on Android, along with Chrome, IE11, and Firefox on Windows. Take a look at the 408KG and 120KB versions below:

SVG 2 (408KB):

rmalogo2

SVG 3 (120KB):

rmalogo3