{"id":7231,"date":"2016-05-10T02:05:48","date_gmt":"2016-05-10T06:05:48","guid":{"rendered":"http:\/\/www.useragentman.com\/blog\/?p=7231"},"modified":"2016-05-10T12:02:14","modified_gmt":"2016-05-10T16:02:14","slug":"how-to-fix-small-transformed-text-in-html5-canvas-in-firefox-for-windows","status":"publish","type":"post","link":"https:\/\/www.useragentman.com\/blog\/2016\/05\/10\/how-to-fix-small-transformed-text-in-html5-canvas-in-firefox-for-windows\/","title":{"rendered":"How To Fix Transformed Text in HTML5 Canvas In Firefox For Windows"},"content":{"rendered":"<p>I was on Twitter yesterday, and saw a thread regarding a great HTML5 Canvas pie chart with rotated text made by <a href=\"http:\/\/www.towc.eu\/\">Matei Copot<\/a>.  In it, <a href=\"https:\/\/twitter.com\/anatudor\">Ana Tudor<\/a> noted that the typography didn&#8217;t look quite right in Firefox:<\/p>\n<blockquote class=\"twitter-tweet\" data-lang=\"en\">\n<p lang=\"en\" dir=\"ltr\">Refuse to use excel for piecharts for homework, so&#8230; <a href=\"https:\/\/t.co\/xjAa82O841\">https:\/\/t.co\/xjAa82O841<\/a><br \/>play with &quot;render&quot; and &quot;data.typeList&quot; <a href=\"https:\/\/t.co\/o7NxHdqXbW\">pic.twitter.com\/o7NxHdqXbW<\/a><\/p>\n<p>&mdash; towc (@MateiCopot) <a href=\"https:\/\/twitter.com\/MateiCopot\/status\/726463017404235777\">April 30, 2016<\/a><\/p><\/blockquote>\n<blockquote class=\"twitter-tweet\" data-lang=\"en\">\n<p lang=\"en\" dir=\"ltr\"><a href=\"https:\/\/twitter.com\/MateiCopot\">@MateiCopot<\/a> Ugh, that rotated text in Firefox :(<\/p>\n<p>&mdash; Ana Tudor (@anatudor) <a href=\"https:\/\/twitter.com\/anatudor\/status\/726468666741653505\">April 30, 2016<\/a><\/p><\/blockquote>\n<blockquote class=\"twitter-tweet\" data-lang=\"en\">\n<p lang=\"en\" dir=\"ltr\"><a href=\"https:\/\/twitter.com\/MateiCopot\">@MateiCopot<\/a> Firefox 46 (current stable). <a href=\"https:\/\/t.co\/FpmJH5APmD\">pic.twitter.com\/FpmJH5APmD<\/a><\/p>\n<p>&mdash; Ana Tudor (@anatudor) <a href=\"https:\/\/twitter.com\/anatudor\/status\/726488434525745152\">April 30, 2016<\/a><\/p><\/blockquote>\n<p>Let&#8217;s see these screenshots up close (IE10 Windows is on the left, Firefox is on the right):<\/p>\n<p><img decoding=\"async\" class=\"full-width-image\" src=\"https:\/\/www.useragentman.com\/og-preview\/7231.jpg\" \/><\/p>\n<p>I noticed that <strong>this was only happening in Windows version of Firefox,<\/strong> so I initially thought it was a variation of <a href=\"https:\/\/www.useragentman.com\/blog\/2014\/05\/04\/fixing-typography-inside-of-2-d-css-transforms\/\">a common issue that browsers have with rendering live text using CSS transforms<\/a>.  However, the fix to that problem wouldn&#8217;t apply since the chart above was using HTML5 Canvas to transform the text. I dug a little deeper, and noticed that this problem was happening because of some unique decisions the Firefox team about how Firefox renders typography in Windows.  In a very lengthy blogpost, <a href=\"https:\/\/twitter.com\/nattokirai\">John Daggett<\/a> explains how <a href=\"https:\/\/blog.mozilla.org\/nattokirai\/2011\/08\/11\/directwrite-text-rendering-in-firefox-6\/\">Firefox implemented DirectWrite in Firefox 6<\/a>.  I encourage everyone reading this to look at that post, but for the impatient out there: Mozilla received complaints that <a href=\"https:\/\/en.wikipedia.org\/wiki\/DirectWrite\">DirectWrite<\/a> (the newer graphics API introduced in Windows 7) blurred some fonts at small font-sizes, so Mozilla decided Firefox should use <a href=\"https:\/\/en.wikipedia.org\/wiki\/Graphics_Device_Interface\">GDI<\/a> (the older graphics API for Windows) for these fonts when their font-size is below 16 pixels.  <\/p>\n<p>I wasn&#8217;t sure if this was the culprit for Matei&#8217;s chart, so I replaced Verdana (which is one of the fonts that uses GDI at small font-sizes) with <a href=\"https:\/\/www.microsoft.com\/typography\/fonts\/font.aspx?FMID=1710\">Calibri<\/a> (which isn&#8217;t on the list) and, bingo, the typography didn&#8217;t look so ugly:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.useragentman.com\/blog\/wp-content\/uploads\/2016\/05\/canvas-transform-firefox-win-calibri.jpg\" alt=\"\" class=\"aligncenter\" \/><\/p>\n<p>For completeness sake, <a href=\"http:\/\/www.towc.eu\/school\/geo\/global-trade-data\/\">here is the original demo by Matei Copot<\/a>, and <a href=\"http:\/\/useragentman.com\/examples\/canvas-transform\/www.towc.eu\/school\/geo\/global-trade-data\/\">here is my version using Calibri<\/a>.  Keep in mind that, as far as I&#8217;m aware, the problem only appears in Firefox for Windows.<\/p>\n<p>You can change this using the <code>about:config<\/code> URL in Firefox by altering the <a href=\"http:\/\/hg.mozilla.org\/mozilla-central\/file\/f631e1b0e296\/modules\/libpref\/src\/init\/all.js#l1891\">gfx.font_rendering.cleartype_params.force_gdi_classic_for_families<\/a> preference, but this is not really a way for a web developer to fix how fonts render on a user&#8217;s machine.  You should just keep in mind that <strong>Firefox will not render Arial, Consolas, Courier New, Microsoft Sans Serif, Segoe UI, Tahoma, Trebuchet and MS, Verdana well when transformed in font-sizes 16 pixels and below<\/strong>.  This is true with CSS transforms as well.  Here&#8217;s an example of some CSS transformed text in Tahoma in Firefox and IE10 under Windows 8:<\/p>\n<table class=\"dataTable css-font-rendering-table\">\n<thead>\n<tr>\n<th>CSS Live Text<\/th>\n<th>Firefox 46<br \/>Windows 8<\/th>\n<th>IE10<br \/>Windows 8<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td class=\"live-text\">\n<div class=\"transformed-text\">Transformed text<\/div>\n<\/td>\n<td><img loading=\"lazy\" decoding=\"async\" width=\"194\" height=\"122\" src=\"https:\/\/www.useragentman.com\/blog\/wp-content\/uploads\/2016\/05\/firefox-css-transform-no-filter.jpg\" alt=\"\" class=\"alignnone size-full wp-image-7249\" \/><\/td>\n<td><img loading=\"lazy\" decoding=\"async\" width=\"213\" height=\"122\" src=\"https:\/\/www.useragentman.com\/blog\/wp-content\/uploads\/2016\/05\/ie10-css-transform-no-filter.jpg\" alt=\"\" class=\"alignnone size-full wp-image-7248\" \/><\/td>\n<\/tbody>\n<\/table>\n<p>However, since this is CSS, you can use <a href=\"https:\/\/www.useragentman.com\/blog\/2014\/05\/04\/fixing-typography-inside-of-2-d-css-transforms\/\">the filter hack that I describe in my previous blog post<\/a>:  <\/p>\n<table class=\"dataTable css-font-rendering-table\">\n<thead>\n<tr>\n<th>CSS Live Text<\/th>\n<th>Firefox 46<br \/>Windows 8<\/th>\n<th>IE10<br \/>Windows 8<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td class=\"live-text\">\n<div class=\"transformed-text with-fix\">Transformed text<\/div>\n<\/td>\n<td><img decoding=\"async\" src=\"https:\/\/www.useragentman.com\/blog\/wp-content\/uploads\/2016\/05\/firefox-css-transform-filter.jpg\" alt=\"\" class=\"alignnone size-full wp-image-7249\" \/><\/td>\n<td><img loading=\"lazy\" decoding=\"async\" width=\"213\" height=\"122\" src=\"https:\/\/www.useragentman.com\/blog\/wp-content\/uploads\/2016\/05\/ie10-css-transform-no-filter.jpg\" alt=\"\" class=\"alignnone size-full wp-image-7248\" \/><\/td>\n<\/tbody>\n<\/table>\n<p>I hope this post will save someone a lot of time in how to fix this issue in both canvas and CSS.<\/p>\n<h2>Further Reading<\/h2>\n<ol>\n<li><a href=\"https:\/\/blog.mozilla.org\/nattokirai\/2011\/08\/11\/directwrite-text-rendering-in-firefox-6\/\">DirectWrite Text Rendering in Firefox 6<\/a> by <a href=\"https:\/\/twitter.com\/nattokirai\">John Daggett<\/a><\/li>\n<li><a href=\"https:\/\/www.smashingmagazine.com\/2012\/04\/a-closer-look-at-font-rendering\/\">A Closer Look At Font Rendering<\/a> by <a href=\"https:\/\/www.smashingmagazine.com\/author\/tim-ahrens\/\">Tim Ahrens<\/a> (an older article, but some of the information in it is still quite relevant).<\/li>\n<\/ol>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.useragentman.com\/blog\/wp-content\/uploads\/2016\/05\/thumb.jpg\" \/>The Firefox team has made some unique choices when rendering fonts in the Windows operating system.  As a result, certain fonts under 16 pixels in size render really bad in HTML5 Canvas as well as CSS.  This blog post shows how you can fix this issue and have clean, transformed typography in any OS.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7231","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/posts\/7231","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/comments?post=7231"}],"version-history":[{"count":37,"href":"https:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/posts\/7231\/revisions"}],"predecessor-version":[{"id":7273,"href":"https:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/posts\/7231\/revisions\/7273"}],"wp:attachment":[{"href":"https:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/media?parent=7231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/categories?post=7231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/tags?post=7231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}