<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Vjeux</title>
	<atom:link href="http://blog.vjeux.com/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.vjeux.com</link>
	<description>French Web Developer</description>
	<lastBuildDate>Wed, 01 May 2013 08:55:38 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Javascript &#8211; Private methods are not really private</title>
		<link>http://blog.vjeux.com/2013/javascript/javascript-private-methods-are-not-really-private.html</link>
		<comments>http://blog.vjeux.com/2013/javascript/javascript-private-methods-are-not-really-private.html#comments</comments>
		<pubDate>Fri, 05 Apr 2013 01:24:59 +0000</pubDate>
		<dc:creator>vjeux</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://blog.vjeux.com/?p=4623</guid>
		<description><![CDATA[In 2001, Douglas Crockford evangelized private members design pattern and since then has been widely adopted by the Javascript community. However, are they really private? Javascript is an extremely dynamic language in which it is possible to access a lot of things on way or another. We'll see how to get a reference to most [...]]]></description>
				<content:encoded><![CDATA[<p>In 2001, Douglas Crockford evangelized <a href="http://javascript.crockford.com/private.html">private members design pattern</a> and since then has been widely adopted by the Javascript community. However, are they really private?</p>
<p>Javascript is an extremely dynamic language in which it is possible to access a lot of things on way or another. We'll see how to get a reference to most of the "private" methods you can find in the wild.</p>
<h3>Example</h3>
<p>Here's a snippet of code from <a href="http://underscorejs.org/underscore.js">underscore.js</a>. We are interested in getting a reference to the following "private" functions: <code>lookupIterator</code> and <code>group</code>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">/* ... */</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// An internal function to generate lookup iterators.</span>
  <span style="color: #000066; font-weight: bold;">var</span> lookupIterator <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> _.<span style="color: #660066;">isFunction</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> value <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> obj<span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// An internal function used for aggregate &quot;group by&quot; operations.</span>
  <span style="color: #000066; font-weight: bold;">var</span> group <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> value<span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> behavior<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">var</span> iterator <span style="color: #339933;">=</span> lookupIterator<span style="color: #009900;">&#40;</span>value <span style="color: #339933;">||</span> _.<span style="color: #660066;">identity</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    each<span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>value<span style="color: #339933;">,</span> index<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">var</span> key <span style="color: #339933;">=</span> iterator.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>context<span style="color: #339933;">,</span> value<span style="color: #339933;">,</span> index<span style="color: #339933;">,</span> obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      behavior<span style="color: #009900;">&#40;</span>result<span style="color: #339933;">,</span> key<span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Groups the object's values by a criterion. Pass either a string attribute</span>
  <span style="color: #006600; font-style: italic;">// to group by, or a function that returns the criterion.</span>
  _.<span style="color: #660066;">groupBy</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> value<span style="color: #339933;">,</span> context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> group<span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> value<span style="color: #339933;">,</span> context<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>result<span style="color: #339933;">,</span> key<span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #009900;">&#40;</span>_.<span style="color: #660066;">has</span><span style="color: #009900;">&#40;</span>result<span style="color: #339933;">,</span> key<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> result<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">/* ... */</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Here's the code to do it: (<a href="http://jsfiddle.net/vjeux/eLSpC/1/">Demo on JSFiddle</a>)</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> lookupIterator<span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> group<span style="color: #339933;">;</span>
&nbsp;
_.<span style="color: #660066;">isFunction</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  lookupIterator <span style="color: #339933;">=</span> arguments.<span style="color: #660066;">callee</span>.<span style="color: #660066;">caller</span><span style="color: #339933;">;</span>
  group <span style="color: #339933;">=</span> lookupIterator.<span style="color: #660066;">caller</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
_.<span style="color: #660066;">groupBy</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'lookupIterator'</span><span style="color: #339933;">,</span> lookupIterator<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'group'</span><span style="color: #339933;">,</span> group<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The way it works is by <a href="http://blog.vjeux.com/2011/javascript/hook.html">hooking</a> a function deep inside the internals of the module you want to break into and use <code>arguments.callee.caller</code> to get references to all the functions upward.</p>
<p>In this case, it was easy as we could hook <code>_.isFunction</code>. You can break into a lot of functions overriding <code>String.prototype</code> and using <code>__defineGetter__</code>.</p>
<h3>Conclusion</h3>
<p>Trying to break into existing Javascript code and make it do what you want is an extremely fun exercise. If this topic is of interest to you, take a look at <a href="https://code.google.com/p/google-caja/wiki/AttackVectors">this big list of Javascript attack vectors</a> written by Google Caja team.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vjeux.com/2013/javascript/javascript-private-methods-are-not-really-private.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Conversion from uint8 to int8 (x &gt; 24)</title>
		<link>http://blog.vjeux.com/2013/javascript/conversion-from-uint8-to-int8-x-24.html</link>
		<comments>http://blog.vjeux.com/2013/javascript/conversion-from-uint8-to-int8-x-24.html#comments</comments>
		<pubDate>Sat, 30 Mar 2013 09:25:57 +0000</pubDate>
		<dc:creator>vjeux</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://blog.vjeux.com/?p=4465</guid>
		<description><![CDATA[Daniel Baulig, a co-worker at Facebook, told me a little trick related to jDataView function to convert from a uint8 to a int8 in Javascript. Here's the version I had: function getInt8&#40;&#41; &#123; var b = this.getUint8&#40;&#41;; if &#40;b &#62; Math.pow&#40;2, 7&#41; - 1&#41; &#123; return b - Math.pow&#40;2, 8&#41;; &#125; return b; &#125; Compare [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.danielbaulig.de/">Daniel Baulig</a>, a co-worker at Facebook, told me a little trick related to <a href="https://github.com/vjeux/jDataView">jDataView</a> function to convert from a uint8 to a int8 in Javascript.</p>
<p>Here's the version I had:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> getInt8<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getUint8</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>b <span style="color: #339933;">&gt;</span> <span style="">Math</span>.<span style="color: #660066;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">7</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> b <span style="color: #339933;">-</span> <span style="">Math</span>.<span style="color: #660066;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> b<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Compare it to his version:</p>
<div id="hidefirsttwo">

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> <span style="color: #339933;">&lt;</span><span style="color: #000066; font-weight: bold;">function</span> getInt8<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getUint8</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #CC0000;">24</span> <span style="color: #339933;">&gt;&gt;</span> <span style="color: #CC0000;">24</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</div>
<p>I was really confused because it seems like it's doing a no-op. Here's the full explanation of why the two versions are working.</p>
<h3>How it works?</h3>
<p>The following table (<a href="http://en.wikipedia.org/wiki/Two%27s_complement">borrowed from Wikipedia</a>) shows how various 8 bits values are in represented with bits and how they are interpreted in unsigned and signed (using two-complement rule).</p>
<style>
.uint8table {margin: 0 auto; border-collapse: collapse; }
.uint8table td, .uint8table th { padding: 2px 5px; border: 1px solid #ccc; text-align: center;  }
.uint8table b { background-color: #FFE0ED; font-weight: normal; }
.uint8table i { color: purple; font-style: normal; }
.uint8table .sep td { border-bottom: 2px solid #bbb; }
#hidefirsttwo span:nth-child(1), #hidefirsttwo span:nth-child(2) { display: none; }
#hidefirsttwo span:nth-child(3) { margin-left: -7px; }
</style>
<table class="uint8table">
<thead>
<tr>
<th>Bits</th>
<th>uint8</th>
<th>int8</th>
</tr>
</thead>
<tbody>
<tr>
<td>0000&nbsp;0000</td>
<td align="right">0</td>
<td align="right">0</td>
</tr>
<tr>
<td>0000 0001</td>
<td align="right">1</td>
<td align="right">1</td>
</tr>
<tr>
<td>0000 0010</td>
<td align="right">2</td>
<td align="right">2</td>
</tr>
<tr>
<td>0111 1110</td>
<td align="right">126</td>
<td align="right">126</td>
</tr>
<tr class="sep">
<td>0111 1111</td>
<td align="right">127</td>
<td align="right">127</td>
</tr>
<tr>
<td>1000 0000</td>
<td align="right">128</td>
<td align="right">−128</td>
</tr>
<tr>
<td>1000 0001</td>
<td align="right">129</td>
<td align="right">−127</td>
</tr>
<tr>
<td>1000 0010</td>
<td align="right">130</td>
<td align="right">−126</td>
</tr>
<tr>
<td>1111 1110</td>
<td align="right">254</td>
<td align="right">−2</td>
</tr>
<tr>
<td>1111 1111</td>
<td align="right">255</td>
<td align="right">−1</td>
</tr>
</tbody>
<tfoot></tfoot>
</table>
<p>Javascript doesn't natively have a 8 bit integer type, it only has a 32 bits one. When you put a 8 bit integer into a 32 bits one, Javascript is going to fill the remaining bits on the left with zeros as the following table shows.</p>
<table class="uint8table">
<thead>
<tr>
<th>Bits</th>
<th>int32</th>
</tr>
</thead>
<tbody>
<tr>
<td>0000 0000 ... 0000&nbsp;0000</td>
<td align="right">0</td>
</tr>
<tr>
<td>0000 0000 ... 0000 0001</td>
<td align="right">1</td>
</tr>
<tr>
<td>0000 0000 ... 0000 0010</td>
<td align="right">2</td>
</tr>
<tr>
<td>0000 0000 ... 0111 1110</td>
<td align="right">126</td>
</tr>
<tr class="sep">
<td>0000 0000 ... 0111 1111</td>
<td align="right">127</td>
</tr>
<tr>
<td>0000 0000 ... 1000 0000</td>
<td align="right">128</td>
</tr>
<tr>
<td>0000 0000 ... 1000 0001</td>
<td align="right">129</td>
</tr>
<tr>
<td>0000 0000 ... 1000 0010</td>
<td align="right">130</td>
</tr>
<tr>
<td>0000 0000 ... 1111 1110</td>
<td align="right">254</td>
</tr>
<tr>
<td>0000 0000 ... 1111 1111</td>
<td align="right">255</td>
</tr>
</tbody>
<tfoot></tfoot>
</table>
<p>Unfortunately, this doesn't properly handle negative numbers. Because we use two-complement, we've got to fill all the bits with 1 for negative numbers in order to have the same number in a signed 32 bits representation.</p>
<table class="uint8table">
<thead>
<tr>
<th>Bits</th>
<th>int32</th>
</tr>
</thead>
<tbody>
<tr>
<td>0000 0000 ... 0000&nbsp;0000</td>
<td align="right">0</td>
</tr>
<tr>
<td>0000 0000 ... 0000 0001</td>
<td align="right">1</td>
</tr>
<tr>
<td>0000 0000 ... 0000 0010</td>
<td align="right">2</td>
</tr>
<tr>
<td>0000 0000 ... 0111 1110</td>
<td align="right">126</td>
</tr>
<tr class="sep">
<td>0000 0000 ... 0111 1111</td>
<td align="right">127</td>
</tr>
<tr>
<td>1111 1111 ... 1000 0000</td>
<td align="right">−128</td>
</tr>
<tr>
<td>1111 1111 ... 1000 0001</td>
<td align="right">−127</td>
</tr>
<tr>
<td>1111 1111 ... 1000 0010</td>
<td align="right">−126</td>
</tr>
<tr>
<td>1111 1111 ... 1111 1110</td>
<td align="right">−2</td>
</tr>
<tr>
<td>1111 1111 ... 1111 1111</td>
<td align="right">−1</td>
</tr>
</tbody>
<tfoot></tfoot>
</table>
<p>So basically, we've got to fill the 24 remaining bits on the left with the same first bit we have: 0 for positive numbers and 1 for negative numbers.</p>
<p>This is when the trick comes into place. In javascript, there's a binary operator: <code>&gt;&gt;</code> <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Bitwise_Operators#>>_(Sign-propagating_right_shift)">Sign-propagating right shift</a> that moves all the bits to the right and fills the missing bits with the first bit.</p>
<p>So all we have to do is to put our 8 good digits to the far left using <code>&lt;&lt;</code> and then use the previous trick to fill the bits with the proper ones <img src='http://blog.vjeux.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<table class="uint8table">
<thead>
<tr>
<th>x</th>
<th>x < < 24</th>
</th>
<th>(x &lt;< 24) >> 24</th>
</tr>
</thead>
<tbody>
<tr>
<td>0000 0000 ... <i>0000 0000</i></td>
<td><i>0000 0000</i> ... 0000 0000</td>
<td><b>0000 0000 ... <i>0</i></b><i>000 0000</i></td>
</tr>
<tr>
<td>0000 0000 ... <i>0000 0001</i></td>
<td><i>0000 0001</i> ... 0000 0000</td>
<td><b>0000 0000 ... <i>0</i></b><i>000 0001</i></td>
</tr>
<tr>
<td>0000 0000 ... <i>0000 0010</i></td>
<td><i>0000 0010</i> ... 0000 0000</td>
<td><b>0000 0000 ... <i>0</i></b><i>000 0010</i></td>
</tr>
<tr>
<td>0000 0000 ... <i>0111 1110</i></td>
<td><i>0111 1110</i> ... 0000 0000</td>
<td><b>0000 0000 ... <i>0</i></b><i>111 1110</i></td>
</tr>
<tr class="sep">
<td>0000 0000 ... <i>0111 1111</i></td>
<td><i>0111 1111</i> ... 0000 0000</td>
<td><b>0000 0000 ... <i>0</i></b><i>111 1111</i></td>
</tr>
<tr>
<td>0000 0000 ... <i>1000 0000</i></td>
<td><i>1000 0000</i> ... 0000 0000</td>
<td><b>1111 1111 ... <i>1</i></b><i>000 0000</i></td>
</tr>
<tr>
<td>0000 0000 ... <i>1000 0001</i></td>
<td><i>1000 0001</i> ... 0000 0000</td>
<td><b>1111 1111 ... <i>1</i></b><i>000 0001</i></td>
</tr>
<tr>
<td>0000 0000 ... <i>1000 0010</i></td>
<td><i>1000 0010</i> ... 0000 0000</td>
<td><b>1111 1111 ... <i>1</i></b><i>000 0010</i></td>
</tr>
<tr>
<td>0000 0000 ... <i>1111 1110</i></td>
<td><i>1111 1110</i> ... 0000 0000</td>
<td><b>1111 1111 ... <i>1</i></b><i>111 1110</i></td>
</tr>
<tr>
<td>0000 0000 ... <i>1111 1111</i></td>
<td><i>1111 1111</i> ... 0000 0000</td>
<td><b>1111 1111 ... <i>1</i></b><i>111 1111</i></td>
</tr>
</tbody>
<tfoot></tfoot>
</table>
]]></content:encoded>
			<wfw:commentRss>http://blog.vjeux.com/2013/javascript/conversion-from-uint8-to-int8-x-24.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XSON &#8211; Smallest JSON equivalent in XML</title>
		<link>http://blog.vjeux.com/2013/javascript/xson-smallest-json-equivalent-in-xml.html</link>
		<comments>http://blog.vjeux.com/2013/javascript/xson-smallest-json-equivalent-in-xml.html#comments</comments>
		<pubDate>Mon, 18 Feb 2013 19:59:44 +0000</pubDate>
		<dc:creator>vjeux</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://blog.vjeux.com/?p=4463</guid>
		<description><![CDATA[It's often said that XML is very verbose and therefore JSON is better. I wanted to challenge that assumption and find the smallest way to represent any JSON value using XML. Constants true &#60;t/&#62; 0 false &#60;f/&#62; - 1 null &#60;l/&#62; 0 Number NaN &#60;n/&#62; + 1 123.45 &#60;n&#62;123.45&#60;/n&#62; + 6 String &#34;&#34; &#60;s/&#62; + [...]]]></description>
				<content:encoded><![CDATA[<p>It's often said that XML is very verbose and therefore JSON is better. I wanted to challenge that assumption and find the smallest way to represent any JSON value using XML.</p>
<p><script src="https://raw.github.com/vjeux/XSON/master/src/xson.js"></script></p>
<style>
table#xson { border-collapse: collapse; margin: 0 auto; }
table#xson td, table#xson th { border: 1px solid #999; padding: 5px 10px; background-color: transparent; }
table#xson th { text-align: center; }
table#xson .wp_syntax { border: none; background-color: transparent; margin: 0; }
table#xson strong { font-family: monospace; color: black; font-size: 12px; padding-left: 5px; }
</style>
<table id="xson">
<tr>
<th rowspan="3">Constants</th>
<td>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">true</span></pre></td></tr></table></div>

</td>
<td><strong>&lt;t/&gt;</strong></td>
<th>0</th>
</tr>
<tr>
<td>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">false</span></pre></td></tr></table></div>

</td>
<td><strong>&lt;f/&gt;</strong></td>
<th>- 1</th>
</tr>
<tr>
<td>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">null</span></pre></td></tr></table></div>

</td>
<td><strong>&lt;l/&gt;</strong></td>
<th>0</th>
</tr>
<tr>
<th rowspan="2">Number</th>
<td>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">NaN</span></pre></td></tr></table></div>

</td>
<td><strong>&lt;n/&gt;</strong></td>
<th>+ 1</th>
</tr>
<tr>
<td>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #CC0000;">123.45</span></pre></td></tr></table></div>

</td>
<td>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;n<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>123.45<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/n<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

</td>
<th>+ 6</th>
</tr>
<tr>
<th rowspan="2">String
<td>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">&quot;&quot;</span></pre></td></tr></table></div>

</td>
<td><strong>&lt;s/&gt;</strong></td>
</th>
<th>+ 2</th>
</tr>
<tr>
<td>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">&quot;Abcd&quot;</span></pre></td></tr></table></div>

</td>
<td>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Abcd<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

</td>
<th>+ 4</th>
</tr>
<tr>
<th rowspan="2">Array</th>
<td>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span></pre></td></tr></table></div>

</td>
<td><strong>&lt;a/&gt;</strong></td>
<th>+ 2</th>
</tr>
<tr>
<td>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;two&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#93;</span></pre></td></tr></table></div>

</td>
<td>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;n<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/n<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>two<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;f<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/f<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

</td>
<th>+ 4 - n</th>
</tr>
<tr>
<th rowspan="2">Object</th>
<td>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</td>
<td><strong>&lt;o/&gt;</strong></td>
<th>+ 2</th>
</tr>
<tr>
<td>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">&quot;first&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;second&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;two&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;third&quot;</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</td>
<td>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;o<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;n</span> <span style="color: #000066;">k</span>=<span style="color: #ff0000;">&quot;first&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/n<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s</span> <span style="color: #000066;">k</span>=<span style="color: #ff0000;">&quot;second&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>two<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;f</span> <span style="color: #000066;">k</span>=<span style="color: #ff0000;">&quot;third&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/f<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/o<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

</td>
<th>+ 5 + n</th>
</tr>
</table>
<p>As you can see, the XML counter part is a bit more verbose and less readable because of the way syntax highlighting is setup. However, while it is bigger, it isn't out of proportion bigger. The structure can be at most twice as big.</p>
<h3>Implementation</h3>
<p>In order to implement it, I decided to go with the same API as JSON:</p>
<ul>
<li>XSON.stringify(object, formatter, space)</li>
<li>XSON.parse(string)</li>
</ul>
<p>You can play with it on this current page or can check it out on <a href="https://github.com/vjeux/XSON">GitHub</a>.</p>
<p>The implementation was more straightforward than I expected thanks to the fact that <a href="http://www.w3schools.com/xml/xml_parser.asp">there's a XML Parser inside browsers</a>. However, I had to deal with nasty encoding issues <img src='http://blog.vjeux.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<h4>String encoding</h4>
<p>There are some characters such as <code>&lt;</code> and <code>\0</code> that we want to escape because otherwise they are likely to be problematic while parsing the XML. The way to encode those characters in XML is to use the <code>&#number;</code> notation where number is the character code. For example <code>a</code> is represented by <code>&#97;</code>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&gt;</span> <span style="color: #000066; font-weight: bold;">new</span> DOMParser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parseFromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;a&gt;&amp;#97;&lt;/a&gt;'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'text/xml'</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">textContent</span>
<span style="color: #3366CC;">&quot;a&quot;</span></pre></td></tr></table></div>

<p>Unfortunately, you cannot express all the characters with this notation. The XML specification introduces <a href="http://www.w3.org/TR/xml11/#charsets">Restricted Characters</a> in the following ranges: <code>[#x1-#x8]</code>, <code>[#xB-#xC]</code>, <code>[#xE-#x1F]</code>, <code>[#x7F-#x84]</code> and <code>[#x86-#x9F]</code>. When you try to read those characters, then the XML parser generates an error.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&gt;</span> <span style="color: #000066; font-weight: bold;">new</span> DOMParser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parseFromString</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;a&gt;&amp;#0;&lt;/a&gt;'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'text/xml'</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">textContent</span>
<span style="color: #3366CC;">&quot;error on line 1 at column 8: xmlParseCharRef: invalid xmlChar value 0&quot;</span></pre></td></tr></table></div>

<p>Instead of fighting with the XML spec, I decided to use my own encoding. I replace the character by <code>\u0000</code>. Where the number is an hexadecimal representation of the number padding so it has exactly four digits.</p>
<p>To do that, we need first to escape all the <code>\</code> and can use a regex to do it in few lines of code <img src='http://blog.vjeux.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> encode<span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">return</span> str
     .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\\/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\\</span>'</span><span style="color: #009900;">&#41;</span>
     .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[\u0000-\u0008\u000b-\u001f&amp;&lt;&gt;&quot;\n\t]/g</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #000066; font-weight: bold;">var</span> hex <span style="color: #339933;">=</span> c.<span style="color: #660066;">charCodeAt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       while <span style="color: #009900;">&#40;</span>hex.<span style="color: #660066;">length</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         hex <span style="color: #339933;">=</span> <span style="color: #3366CC;">'0'</span> <span style="color: #339933;">+</span> hex<span style="color: #339933;">;</span>
       <span style="color: #009900;">&#125;</span>
       <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\\</span>u'</span> <span style="color: #339933;">+</span> hex<span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Then, in order to decode it, we do the opposite: we first decode all the unicode characters and remove the escapes. In order to make sure that the unicode character was not escape, I'm using a small trick. You can count the number of <code>\</code>. If it's an even number, then it is not escaped, otherwise it is escaped!</p>
</pre>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> decode<span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> str
    .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/(\\*)\\u([0-9a-f]{4})/g</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>match<span style="color: #339933;">,</span> backslash<span style="color: #339933;">,</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>backslash.<span style="color: #660066;">length</span> <span style="color: #339933;">%</span> <span style="color: #CC0000;">2</span> <span style="color: #339933;">!==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> match<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000066; font-weight: bold;">return</span> backslash <span style="color: #339933;">+</span> <span style="">String</span>.<span style="color: #660066;">fromCharCode</span><span style="color: #009900;">&#40;</span>parseInt<span style="color: #009900;">&#40;</span>n<span style="color: #339933;">,</span> <span style="color: #CC0000;">16</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\\\\/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\\</span>'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.vjeux.com/2013/javascript/xson-smallest-json-equivalent-in-xml.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Image Gallery &#8211; Left and Right Areas</title>
		<link>http://blog.vjeux.com/2013/image/image-gallery-left-and-right-areas.html</link>
		<comments>http://blog.vjeux.com/2013/image/image-gallery-left-and-right-areas.html#comments</comments>
		<pubDate>Thu, 24 Jan 2013 19:16:25 +0000</pubDate>
		<dc:creator>vjeux</dc:creator>
				<category><![CDATA[Image]]></category>

		<guid isPermaLink="false">http://blog.vjeux.com/?p=4436</guid>
		<description><![CDATA[Pete Hunt just showed me a cool trick today. When implementing an image gallery, chances are that you are going to let the user click on the image and based on the position, it will either display the next image or previous. The way you would implement it without too much thought is to let [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.petehunt.net/blog/">Pete Hunt</a> just showed me a cool trick today. When implementing an image gallery, chances are that you are going to let the user click on the image and based on the position, it will either display the next image or previous.</p>
<p>The way you would implement it without too much thought is to let the left part be for the previous action and the right part be for the next action as in the following drawing.</p>
<div style="position: relative; width: 300px; margin: 0 auto;"><img src="http://blog.vjeux.com/wp-content/uploads/2000/01/gallery.jpg" style="width: 100%;" />
<div style="width: 50%; top: 0; left: 0; bottom: 0; background-color: rgba(255, 0, 0, 0.3); position: absolute;"></div>
<div style="width: 50%; top: 0; right: 0; bottom: 0; background-color: rgba(0, 0, 255, 0.3); position: absolute;"></div>
</div>
<p>However, usually when you are viewing an image, you want to see the <code>next</code>, not the <code>previous</code> one. You also tend to just want to click anywhere on the image to make it go <code>next</code>. The <code>previous</code> action is not the default use case and is something you actively think about doing.</p>
<p>Instead of being 50%/50%, you can make the <code>next</code> action area bigger. Here is an example with 20%/80%.</p>
<div style="position: relative; width: 300px; margin: 0 auto;"><img src="http://blog.vjeux.com/wp-content/uploads/2000/01/gallery.jpg" style="width: 100%;" />
<div style="width: 20%; top: 0; left: 0; bottom: 0; background-color: rgba(255, 0, 0, 0.3); position: absolute;"></div>
<div style="width: 80%; top: 0; right: 0; bottom: 0; background-color: rgba(0, 0, 255, 0.3); position: absolute;"></div>
</div>
<p>In practice it works very well and is more user friendly that the naive one.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vjeux.com/2013/image/image-gallery-left-and-right-areas.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CSS &#8211; Contain &amp; Cover</title>
		<link>http://blog.vjeux.com/2013/image/css-container-and-cover.html</link>
		<comments>http://blog.vjeux.com/2013/image/css-container-and-cover.html#comments</comments>
		<pubDate>Mon, 14 Jan 2013 08:37:33 +0000</pubDate>
		<dc:creator>vjeux</dc:creator>
				<category><![CDATA[Image]]></category>

		<guid isPermaLink="false">http://blog.vjeux.com/?p=4317</guid>
		<description><![CDATA[In a previous article I explained how CSS Percentage Background Position was working. This time I'm going to talk about the two ways to resize an image to a viewport: contain and cover. This is such a fundamental operation that I explained all the formulas and where they come from. They are the base for [...]]]></description>
				<content:encoded><![CDATA[<p><script type="text/x-mathjax-config">MathJax.Hub.Config({"HTML-CSS": {scale: 130}});</script><script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML-full"></script>In a previous article I explained how <a href="http://blog.vjeux.com/2012/css/css-understanding-percentage-background-position.html">CSS Percentage Background Position</a> was working. This time I'm going to talk about the two ways to resize an image to a viewport: <code>contain</code> and <code>cover</code>. This is such a fundamental operation that I explained all the formulas and where they come from. They are the base for anything more complicated you want to do with images.</p>
<h3>Definitions</h3>
<p>We are going to manipulate two rectangles in this article: the image we want to display and the viewport in which we want to display it. Each rectangle has three properties: a width \(w\), a height \(h\) and an aspect ratio \(r\).</p>
<p><img src="http://blog.vjeux.com/wp-content/uploads/2013/01/image-viewport.png" alt="image-viewport" width="635" height="252" class="aligncenter size-full wp-image-4306" /></p>
<p>The aspect ratio of an image is defined by the following formula: \[r_{atio} = \frac{w_{idth}}{h_{eight}}\]</p>
<p>While there are an infinite amount of aspect ratios, we are going to be in contact with for major categories of aspect ratio when displaying photos on the internet.</p>
<p><img src="http://blog.vjeux.com/wp-content/uploads/2013/01/ratios1.png" alt="ratios" width="650" height="216" class="aligncenter size-full wp-image-4360" /></p>
<h3>Adapting the Image to the Viewport</h3>
<h4>Stretch the image</h4>
<p>The naive way to adapt an image to a viewport is to set both the width and height of the image to match the viewport width and height.</p>
<p><img src="http://blog.vjeux.com/wp-content/uploads/2013/01/stretch.png" alt="stretch" width="368" height="230" class="aligncenter size-full wp-image-4325" /></p>
<p>However, doing that is going to stretch your image and make it look very bad.</p>
<div style="text-align: center;">
<img src="http://blog.vjeux.com/wp-content/uploads/2013/01/car_resize1.jpg" alt="car_resize" width="100" height="150" /> <img src="http://blog.vjeux.com/wp-content/uploads/2013/01/car_resize1.jpg" alt="car_resize" width="200" height="150" /> <img src="http://blog.vjeux.com/wp-content/uploads/2013/01/car_resize1.jpg" alt="car_resize" width="300" height="150" />
</div>
<p>The problem with the previous scaling is that we didn't respect one fundamental rule: the aspect ratio must remain constant after the transformation.</p>
<p>\[r_{image} = r'_{image}\]</p>
<h4>Contain</h4>
<p>So, in order to make our image fit the viewport, we can make the image being <code>contain</code>ed in the viewport and have padding. Think black bars when you are watching a movie.</p>
<p><img src="http://blog.vjeux.com/wp-content/uploads/2013/01/contain.png" alt="contain" width="260" height="223" class="aligncenter size-full wp-image-4311" /></p>
<p>In term of equations, we are setting the width of the image to be the width of the viewport: \(w'_{image} = w_{viewport}\). Since you also have \(r_{image} = r'_{image}\), and \(r'_{image} = \frac{w'_{image}}{h'_{image}}\), it gets trivial to compute the remaining dimension.</p>
<p>\[<br />
h'_{image} = \frac{w_{viewport}}{r_{image}}<br />
\] </p>
<h4>Cover</h4>
<p>There is another possibility, you can also make the image <code>cover</code> the viewport. </p>
<p><img src="http://blog.vjeux.com/wp-content/uploads/2013/01/cover.png" alt="cover" width="529" height="229" class="aligncenter size-full wp-image-4314" /></p>
<p>Despite being conceptually different, the formula is nearly the same. Instead of fitting the width, we now fit the height and can compute the width the same way:</p>
<p>\[w'_{image} = h_{viewport} * r_{image}\]</p>
<h4>Aspect Ratios?</h4>
<p>In our examples so far, we considered that the image was more horizontal than the viewport. What happens if we do the opposite: switch the image and the viewport dimensions. Check out the following image with all the new equations and unknowns.</p>
<div style="padding-top: 20px;position: relative;">
<h4 style="position: absolute; left: 57px; top: 0">Cover</h4>
<h4 style="position: absolute; left: 457px; top: 0">Contain</h4>
<p><img src="http://blog.vjeux.com/wp-content/uploads/2013/01/contain_cover_rato.png" alt="contain_cover_rato" width="636" height="236" class="aligncenter size-full wp-image-4358" />
</div>
<p>The similarities are striking. The formulas are exactly the same but inverted, <code>cover</code> is now using <code>contain</code> formula and vis versa. It makes it even easier to implement the algorithm, only two cases to handle.</p>
<h4>Summary</h4>
<p>Here is a summary of the formulas if you want to implement <code>contain</code> and <code>cover</code>.</p>
<style>
table.contain-cover {
  border-collapse: collapse;
}
table.contain-cover td, table.contain-cover th {
  border: 1px solid #ccc;
  padding: 5px 10px;
}
</style>
<table width="100%" class="contain-cover" style="zoom: 0.87;">
<tr>
<td style="border: none;"></td>
<th>Contain</th>
<th>Cover</th>
</tr>
<tr>
<th>\(r_{image} \le r_{viewport}\)</th>
<td style="background-color: #D3FFB0;">\(w'_{image} = h_{viewport} * r_{image}\)<br />
        \(h'_{image} = h_{viewport}\)</td>
<td style="background-color: #C0C0F8;">\(w'_{image} = w_{viewport}\)<br />
        \(h'_{image} = \frac{w_{viewport}}{r_{image}}\)</td>
</tr>
<tr>
<th>\(r_{image} \ge r_{viewport}\)</th>
<td style="background-color: #C0C0F8;">\(w'_{image} = w_{viewport}\)<br />
        \(h'_{image} = \frac{w_{viewport}}{r_{image}}\)</td>
<td style="background-color: #D3FFB0;">\(w'_{image} = h_{viewport} * r_{image}\)<br />
        \(h'_{image} = h_{viewport}\)</td>
</tr>
</table>
<h3>Hidden Area</h3>
<p>We either introduced black bars or hidden some part of the photo. It's interesting to see how much it accounts for. For example, if we only hide 2% of the image, then it may not be useful to run an algorithm to <a href="http://blog.vjeux.com/2012/facebook/best-cropping-position.html">find the best cropping position</a>.</p>
<p><img src="http://blog.vjeux.com/wp-content/uploads/2013/01/hidden-area.png" alt="hidden area" width="366" height="158" class="aligncenter size-full wp-image-4385" /></p>
<p>The method is making sure that one of the dimensions is equal in both the adapted image and viewport, in this case the width. So all we are left to do is to compute the small height divided by the big height.</p>
<p>\[hidden = \frac{area'_{image}}{area_{viewport}} = \frac{w'_{image} * h'_{image}}{w_{viewport} * h_{viewport}} = \frac{h'_{image}}{h_{viewport}}\]</p>
<p>We can repeat the process for the four other cases and come up with the following summary:</p>
<table width="100%" class="contain-cover">
<tr>
<td style="border: none;"></td>
<th>Contain</th>
<th>Cover</th>
</tr>
<tr>
<th>\(r_{image} \le r_{viewport}\)</th>
<td>\(hidden = \frac{w'_{image}}{w_{viewport}}\)</td>
<td>\(hidden = \frac{h_{viewport}}{h'_{image}}\)</td>
</tr>
<tr>
<th>\(r_{image} \ge r_{viewport}\)</th>
<td>\(hidden = \frac{h'_{image}}{h_{viewport}}\)</td>
<td>\(hidden = \frac{w_{viewport}}{w'_{image}}\)</td>
</tr>
</table>
<p>Note that in this case, we have to evaluate the four different possibilities. As this is a bit error prone and annoying to read all the cases, we can find another way to write it down. Let's see what happens if we compare the ratios.</p>
<p>\[\frac{r'_{image}}{r_{viewport}} = \frac{\frac{w'_{image}}{h'_{image}}}{\frac{w_{image}}{h_{viewport}}} = \frac{w'_{image}}{h'_{image}} \frac{h_{image}}{w_{viewport}} = \frac{w'_{image}}{w_{viewport}} \frac{h_{image}}{h'_{image}}\]</p>
<p>We know that either the widths or the heights are equal. So it manages to find the proper two terms and the fraction. Now, nothing guarantees that they are in the good position. So we have either the result or \(\frac{1}{result}\). As we know we want a number smaller than 1, we can just take the minimum of both.</p>
<div style="border: 1px solid black; background-color: #fff74e;">\[hidden = min\left(\frac{r_{image}}{r_{viewport}}, \frac{r_{viewport}}{r_{image}}\right)\]</div>
<p>Note: We were able to transform \(r'_{image}\) in to \(r_{image}\) because they are equal.</p>
<h3>No Upscale</h3>
<p>If you look closely at the formulas, we never actually use the width and height of the original image, we only use its aspect ratio. Therefore, if the original image is too small, it will be upscaled and appear very pixelated.</p>
<div style="margin: 0 auto; width: 200px; height: 120px; border: 1px solid black; background-image: url(http://blog.vjeux.com/wp-content/uploads/2013/01/control_play.png); background-size: cover; background-position: center center;"></div>
<p>The easiest way to fix it is to compute the intended width and height, and make sure it is not bigger than the original width and height</p>
<div style="border: 1px solid black; background-color: #fff74e;">\[w'_{image} \leftarrow min(w'_{image}, w_{image})\]\[h'_{image} \leftarrow min(h'_{image}, h_{image})\]</div>
<p>And here is the result:</p>
<div style="margin: 0 auto; width: 200px; height: 120px; border: 1px solid black; background-image: url(http://blog.vjeux.com/wp-content/uploads/2013/01/control_play.png); background-repeat: no-repeat; background-position: center center;"></div>
<h4>Conclusion</h4>
<p>I wrote this article mainly in order to have a reference of all the formulas I wrote in the code and where they came from. All the formulas are really simple (I know, they look really scared in a computer screen unfortunately) and any time you want to display an image you have to play with their width, height and area. So it's good to actually practice using them.</p>
<p>One key learning from this blog article should be that the aspect ratio is the most important thing that defines an image. Width and height are not actually that important as you are going to scale your image all the time. If you uses aspect ratio everywhere instead of width and height, your code is going to be a lot less error prone as we are already dealing with 2 other groups of width and heights: the scaled image and viewport size.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vjeux.com/2013/image/css-container-and-cover.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CSS &#8211; Displaying a Justified Line of Text</title>
		<link>http://blog.vjeux.com/2012/css/css-displaying-a-justified-line-of-text.html</link>
		<comments>http://blog.vjeux.com/2012/css/css-displaying-a-justified-line-of-text.html#comments</comments>
		<pubDate>Thu, 27 Dec 2012 19:50:49 +0000</pubDate>
		<dc:creator>vjeux</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://blog.vjeux.com/?p=4120</guid>
		<description><![CDATA[Now that I am working everyday with an awesome designer, I'm starting to discover the designer side of things. I got introduced to typography and realized how bad support for good typography was in the browsers. The tale to implement proper text layout algorithms started. Line breaking and Hyphenation I first read two fundamental papers [...]]]></description>
				<content:encoded><![CDATA[<p>Now that I am working everyday with an <a href="http://andychung.ca/">awesome designer</a>, I'm starting to discover the designer side of things. I got introduced to typography and realized how bad support for good typography was in the browsers. The tale to implement proper text layout algorithms started.</p>
<h3>Line breaking and Hyphenation</h3>
<p>I first read two fundamental papers on the algorithms that power TeX. The first one, written by Franklin Mark Liang, explains how to properly <a href="http://www.tug.org/docs/liang/liang-thesis.pdf">hyphenate words</a>. When reading it in 2012, it is a bit unreal all the care taken to reduce memory as 20KB was a hard limit for the project. The second is written by Donald Knuth and Michael Plass and talks about <a href="http://bowman.infotech.monash.edu.au/~pmoulder/line-breaking/knuth-plass-breaking.pdf">finding when to break lines</a>. It gives a very good introduction of all the subtleties behind the seemingly easy line breaking operation.</p>
<p>I was about to implement the papers when I realized that <a href="http://www.bramstein.com/">Bram Stein</a> already wrote a Javascript version. <a href="http://www.bramstein.com/projects/hypher/">Hypher</a> to hyphenate and <a href="http://www.bramstein.com/projects/typeset/">TypeSet</a> for line breaking.</p>
<h3>Displaying a line</h3>
<p>In all our examples, we are going to try and display the first two paragraphs of the novel <a href="http://books.google.com/books?vid=0PO8Zb8GuSvhGZx2&#038;id=u8HOxy7lQYUC&#038;printsec=titlepage">Flatland</a> by Edwin Abbott. I've seen this being used by two designers interested in typography so I guess I'm going to use it too!</p>
<div style="text-align: center;">
  <img src="http://blog.vjeux.com/wp-content/uploads/2012/12/flatland.png" />
</div>
<h4>Absolute Position Everything</h4>
<p>The text algorithms give us for each character, its position in the paragraph. The first idea that comes to mind is to create one <code>span</code> for each element and absolutely position it in the DOM.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;left: 0px; top: 23.2px;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>I<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;left: 5px; top: 23.2px;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #ddbb00;">&amp;nbsp;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;left: 7.529411764705882px; top: 23.2px;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>c<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;left: 14.529411764705882px; top: 23.2px;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>a<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;left: 22.529411764705884px; top: 23.2px;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>l<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;left: 27.529411764705884px; top: 23.2px;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>l<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- ... --&gt;</span></pre></td></tr></table></div>

<p>First thing to notice is the presence of <code>&amp;nbsp;</code> instead of a white space. In HTML, white spaces around tags are generally omitted. This is useful as you can properly indent your HTML code and it will not add many unwanted white spaces in the result. It can also be annoying when you want to layout things with <code>display: inline-block;</code> but that is another problem. In this case, we use <code>&amp;nbsp;</code> to force a real white space.</p>
<p>Then, we can see that position have decimal precision. Nowadays, browser implement sub-pixel rendering and we can use it to evenly space words in a line. This makes less abrupt spacing changes.</p>
<p>The first downside of this technique is the weight of the generated output. Having one DOM element per letter is highly inefficient, especially on mobile devices.</p>
<p>The second is about text selection. It varies widely between browser but usually double click to select a word is not working. The highlight is not contiguous, there are unwanted white spaces around letters/words. For some reason, when you use shift+right, you have to go two times right to highlight the next letter. And finally, when copy and pasting, \n are not taken into account.</p>
<h4>White Space Flexbox</h4>
<p>The second approach is to display the text line by line and use real textNode for words instead of a DOM element for each character. The issue we are going to face from now on is to make sure spaces have proper width such that the line is justified.</p>
<p>The first technique I am going to present has been found by <a href="http://www.dirigibleflightcraft.com/kindle/">Kevin Lynagh</a>. We are going to use flexbox to stretch the spaces such that they fill the available space.</p>
<table width="100%">
<tr>
<td width="50%">

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;line&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  I<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;glue&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  call<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;glue&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  our<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;glue&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  world<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;glue&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  Flatland,<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;glue&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  not<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;glue&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #808080; font-style: italic;">&lt;!-- ... --&gt;</span>
  clearer
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

</td>
<td width="50%">

<div class="wp_syntax"><table><tr><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.line</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> box<span style="color: #00AA00;">;</span>
  box-pack<span style="color: #00AA00;">:</span> <span style="color: #993333;">justify</span><span style="color: #00AA00;">;</span>
  box-orient<span style="color: #00AA00;">:</span> horizontal<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.glue</span> <span style="color: #00AA00;">&#123;</span>
  flex<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

</td>
</tr>
</table>
<p>The visual display is perfect as it properly set the width of the glue elements with sub-pixel precision. However, copy and paste is not working as intended. All the spaces are being replaced by <code>\n</code> as glue elements are <code>display: block;</code>. Browser support of Flexbox is still early so this technique cannot be used on popular websites.</p>
<p>If you want to render links, this approach is going to be problematic when they span over two lines. You are going to have to break the <code><a></a></code> tag into two. This means that people clicking on the first part will not see the second highlight. If you had any Javascript listeners, you are going to have to duplicate them ... It is making things more complicated.</p>
<h4>One-line Justify</h4>
<p>We really want the browser to do the justification part, but by default, it will not justify only one line of text. The reason is that the last line of any paragraph is not going to be justified. Using a <a href="http://blog.vjeux.com/2011/css/css-one-line-justify.html">trick I explained in a previous blog article</a>, we can force it to do so.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;justify&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  I call our world Flatland, not because we call it so, but to make its nature clearer
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  to you, my happy readers, who are privileged to live in Space.
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.justify</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">justify</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">word-spacing</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-10px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.justify</span><span style="color: #3333ff;">:after </span><span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">content</span><span style="color: #00AA00;">:</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> inline-block<span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>The browser will not stretch the white space below the width of a white space. It will consider that there isn't enough room and put the last character on the next line and then justify. In order to force it to do so, we want to remove some pixels from spaces. We can do that using <code>word-spacing: -10px;</code>. You've got to make this value at least equal to the width of a white space not to worry about it anymore. Note: don't make it too important (like <code>-99999px</code>) as if the computed line width is negative, the browser will not attempt to justify the text.</p>
<p>This technique is working on all the major browsers (IE>7) and very light in term of DOM overhead. The copy and pasted text is not perfect as there will be <code>\n</code> at every end of line (80-columns newsgroup style) but is good enough. We still have the <code><a></a></code> tag issue as the previous technique.</p>
<h4>Word-spacing</h4>
<p>The previous techniques found ways to let the browser compute the proper white space widths. We made all the hard work of finding the best line-breaks, so we know the widths of the white spaces. Why don't we tell it to the browser. For each line, we can update the word-spacing accordingly. This is the technique used by <a href="http://www.bramstein.com/projects/typeset/">TypeSet</a>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;word-spacing: -1.4705882352941178px;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  I call our world Flatland, not because we call it so, but to make its nature clearer<span style="color: #ddbb00;">&amp;nbsp;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
to you, my happy readers, who are privileged to live in Space.
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>It is however to good to be true. Webkit has a bug where <a href="https://bugs.webkit.org/show_bug.cgi?id=20606">fractional pixel values are not supported with word-spacing</a>, it has been reported in 2008 but not yet being worked on by anyone. So if you really want to work with Webkit, you have got to distribute the fractional values between words.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;word-spacing: -2px;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  I call our world Flatland,<span style="color: #ddbb00;">&amp;nbsp;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;word-spacing: -1px;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  not because we call it so, but to make its nature clearer<span style="color: #ddbb00;">&amp;nbsp;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
to you, my happy readers, who are privileged to live in Space.
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The downside now is that the first few letters look really close to each other and the remaining letters are farther away. Since all the modifications we do is adding spans around, text selection and copy and paste are completely unaffected. We still have the <code><a></a></code> issue however.</p>
<h4>White Space Custom Width</h4>
<p>If <code>word-spacing</code> doesn't work, we can instead set the width of each white space by hand. We are going to update <code>margin-left</code> with the amount we would have sent to <code>word-spacing</code>.</p>
<p>Unfortunately, if you combine textNodes and spans with <code>&amp;nbsp;</code>, the white span will not appear. You have to wrap the textNode in a <code>span</code>. This makes the DOM more complicated than it should have been.</p>
<p>One last detail is that for some reason, the browser will not add any line break. Therefore you have to insert your own <code>br</code> tags.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>I<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;margin-left: -1.4705882352941178px;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #ddbb00;">&amp;nbsp;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>call<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;margin-left: -1.4705882352941178px;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #ddbb00;">&amp;nbsp;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- ... --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>nature<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;margin-left: -1.4705882352941178px;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #ddbb00;">&amp;nbsp;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>clearer<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;margin-left: -1.4705882352941178px;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #ddbb00;">&amp;nbsp;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
to you, my happy readers, who are privileged to live in Space.
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>This technique doesn't suffer from the <code><a></a></code> problem. The text copy and pasted still have <code>\n</code> at the end of each line. It enables sub-pixel text position in Chrome. The main downside is that it adds two new DOM elements per word.</p>
<h4>Horizontally Scale the Line</h4>
<p>In <a href="http://mozilla.github.com/pdf.js/">pdf.js</a>, they write all the text into a canvas but display transparent text on-top in order to get browser selection. Instead of handling justification properly, they cheat and scale the text horizontally to fit the width.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;transform: scale(0.9545764749570032, 1); transform-origin: 0% 0%;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  These detailed metrics allow us to estimate parameters for a
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;transform: scale(0.9401883384450235, 1); transform-origin: 0% 0%;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  simple model of tracing performance. These estimates should be
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The trick works because the text is not being displayed. If you attempt to scale horizontally your text to fit the width of your page, you are going to see very bad scaling artifacts on your letters.</p>
<p>This technique is not quite what we want. It scales both letters and white spaces where we only want to scale white spaces. Therefore, the displayed text and the overlaid text do not always exactly match as seen in the image:</p>
<div style="text-align: center;">
  <img src="http://blog.vjeux.com/wp-content/uploads/2012/12/pdfjs.png" />
</div>
<p>I'm not 100% sure what their constraints are but I'm hopeful that the one line justify trick would improve their text selection.</p>
<h4>End of Line Padding</h4>
<p>What we really want to do is to put a <code>&lt;br /></code> at the end of all the lines and let the browser justify for us. Unfortunately, this is not working, the browser doesn't justify when there are <code>&lt;br /></code>. Instead, we can exploit the fact that the browser uses a first-fit method to break the lines and force it to break where we want it to.</p>
<p>At the end of each line, we are going to add a empty <code>span</code> element that has the dimensions of the remaining space in the line. This way, the browser is going to see that the text and white space and our element make a full line, and then go to the next line.</p>
<p>Now, we really don't want this element to appear, or it would just be the equivalent of a <code>text-align: left;</code>. Here is the trick, the <code>margin-right</code> property of the last element of the line is being ignored after the browser line-breaking algorithm.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;text-align: justify;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  I call our world Flatland, not because we call it so, but to make its nature clearer
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;margin-right: 132px;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  to you, my happy readers, who are privileged to live in Space.
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>It works like a charm when you don't need to go below the default white-space size. If you do, then things get a little bit more complicated. We have to use <code>word-spacing: -10px</code> but then, the last line is going to be using this spacing instead of the default one.</p>
<p>The solution is to use our <code>justify</code> class from before that forces the last line to be justified and add an element at the end with the proper size to fill up the space. This time, we want the width of this element to remain after the line-breaking algorithm. So instead of doing a <code>margin-right</code>, we are just going to do <code>margin-left</code>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;justify&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  I call our world Flatland, not because we call it so, but to make its nature clearer
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;margin-right: 132px;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  to you, my happy readers, who are privileged to live in Space.
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;margin-left: 115px;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>This solution doesn't suffer from the <code><a></a></code> issue as we don't wrap lines into an element. It doesn't affect copy and paste as the end of line elements are empty and inline. It is also very lightweight as we only add one DOM element at the end of each line. And, it is cross-browser and supports sub-pixel word positioning.</p>
<h3>Conclusion</h3>
<p>By default, the browser doesn't let you hint where you want it to break in a justified paragraph, we have to send carefully crafted inputs to exploit the way its internal rendering algorithm work. If you are to use one of the described techniques, use the last one as it solved all the pain points the others have.</p>
<p>You can test all of them using this <a href="http://jsfiddle.net/vjeux/9hy77/">JSFiddle Demo</a>. Note: they have been hard-coded to work on Chrome Windows. If you are not using both, then it will likely be all screwed up because the font size is not the same and browser prefixes have not been added.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vjeux.com/2012/css/css-displaying-a-justified-line-of-text.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Beware of one pixel resizing</title>
		<link>http://blog.vjeux.com/2012/image/beware-of-one-pixel-resizing.html</link>
		<comments>http://blog.vjeux.com/2012/image/beware-of-one-pixel-resizing.html#comments</comments>
		<pubDate>Thu, 06 Dec 2012 20:17:04 +0000</pubDate>
		<dc:creator>vjeux</dc:creator>
				<category><![CDATA[Image]]></category>

		<guid isPermaLink="false">http://blog.vjeux.com/?p=4024</guid>
		<description><![CDATA[When displaying images naively, you may end up losing image quality because of a relatively unknown phenomena. If you happen to display an image with a dimension that is one pixel off the real image dimension, the resizing operation (which is costly in the browser) is going to be the equivalent of a blur. See [...]]]></description>
				<content:encoded><![CDATA[<p><script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><br />
When displaying images naively, you may end up losing image quality because of a relatively unknown phenomena. If you happen to display an image with a dimension that is one pixel off the real image dimension, the resizing operation (which is costly in the browser) is going to be the equivalent of a blur. See the following example:</p>
<div style="text-align: center;">
<table style="margin: 0 auto;">
<tr>
<td>
<img src="http://blog.vjeux.com/wp-content/uploads/2012/12/130x130.png" width="130" height="130" /></td>
<td><img src="http://blog.vjeux.com/wp-content/uploads/2012/12/130x130.png" width="129" height="129" /></td>
</tr>
<tr>
<td>130x130</td>
<td>129x129</td>
</tr>
</table>
</div>
<p>When you look at it from an external perspective, it seems to be very intentional to display and image with a dimension that is one pixel off. However it can happen for many reasons, some are bugs and some are legitimate.</p>
<h3>Grid Sizes</h3>
<p>Let's say the content area where you want to display a 4-columns image grid has a width of 500 pixels. And you want to have the same padding in the edges as in-between the images.</p>
<p>\[4 * image{ }width + 5 * padding = 500\]\[image{ }width = \frac{(500 - 5 * padding)}{4}\]</p>
<p>The only padding value between 2px and 8px that give an integer number for the image width are 4px and 8px. But unfortunately, none of them look good, you really want 6px padding.</p>
<div style="text-align: center;">120x120 and 4px padding.</div>
<div style="width: 496px; border: 1px solid #aaa; margin: 0 auto; font-size: 0; white-space: nowrap; padding: 2px;">
  <img src="http://placehold.it/120x120" style="margin: 2px;" /><img src="http://placehold.it/120x120" style="margin: 2px;" /><img src="http://placehold.it/120x120" style="margin: 2px;" /><img src="http://placehold.it/120x120" style="margin: 2px;" />
</div>
<p></p>
<div style="width: 492px; border: 1px solid #aaa; margin: 0 auto; font-size: 0; white-space: nowrap; padding: 4px;">
  <img src="http://placehold.it/115x115" style="margin: 4px;" /><img src="http://placehold.it/115x115" style="margin: 4px;" /><img src="http://placehold.it/115x115" style="margin: 4px;" /><img src="http://placehold.it/115x115" style="margin: 4px;" />
</div>
<div style="text-align: center;">115x115 and 8px padding.</div>
<p>In this case, you want to cheat and don't have all the same width and padding but make some of them 1 pixel smaller.</p>
<p>You can for example say that edges will have 5 pixel and inside 6 pixels. However this is a bad idea because it is going to be visually visible. By changing from 5 to 6 you are doing a variation of 17%.</p>
<p>\[5 + 118 + 6 + 118 + 6 + 118 + 6 + 118 + 5 = 500\]</p>
<div style="width: 496px; border: 1px solid #aaa; margin: 0 auto; font-size: 0; white-space: nowrap; padding: 2px;">
  <img src="http://placehold.it/118x118" style="margin: 3px;" /><img src="http://placehold.it/118x118" style="margin: 3px;" /><img src="http://placehold.it/118x118" style="margin: 3px;" /><img src="http://placehold.it/118x118" style="margin: 3px;" /><br /><img src="http://placehold.it/118x118" style="margin: 3px;" /><img src="http://placehold.it/118x118" style="margin: 3px;" /><img src="http://placehold.it/118x118" style="margin: 3px;" /><img src="http://placehold.it/118x118" style="margin: 3px;" />
</div>
<div style="text-align: center;">118x118 and 5px padding on the sides, 6px padding in-between.</div>
<p>Instead you want to borrow a pixel from the images. Having two with 127px width and two with 128px width. The difference is not visible by the eye.</p>
<p>\[6 + 117 + 6 + 118 + 6 + 117 + 6 + 118 + 6 = 500\]</p>
<div style="width: 494px; border: 1px solid #aaa; margin: 0 auto; font-size: 0; white-space: nowrap; padding: 3px;">
  <img src="http://placehold.it/117x118" style="margin: 3px;" /><img src="http://placehold.it/118x118" style="margin: 3px;" /><img src="http://placehold.it/117x118" style="margin: 3px;" /><img src="http://placehold.it/118x118" style="margin: 3px;" /><br /><img src="http://placehold.it/117x118" style="margin: 3px;" /><img src="http://placehold.it/118x118" style="margin: 3px;" /><img src="http://placehold.it/117x118" style="margin: 3px;" /><img src="http://placehold.it/118x118" style="margin: 3px;" />
</div>
<div style="text-align: center;">117x118 and 118x118 alternated and 6px padding.</div>
<p>So now we are in a situation where we want to display an image with 1 less pixel. In order to do that without bluring the image, the trick is to use a container with the size you want to display with <code>overflow: hidden;</code> and inside the properly sized image.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;overflow: hidden; width: 129px; height: 129px;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;130x130.png&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;130&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;130&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<div style="text-align: center;">
<table style="margin: 0 auto;">
<tr>
<td>
<img src="http://blog.vjeux.com/wp-content/uploads/2012/12/130x130.png" width="130" height="130" /></td>
<td>
<div style="display: inline-block; width: 129px; height: 129px; overflow: hidden;"><img src="http://blog.vjeux.com/wp-content/uploads/2012/12/130x130.png" width="130" height="130" /></div>
</td>
</tr>
<tr>
<td>130x130</td>
<td>129x129</td>
</tr>
</table>
</div>
<h3>Chrome bug</h3>
<p>Being one pixel off is really easy, the main cause is different rounding. One one part of the code you use <code>round()</code> and in another part you use <code>floor()</code>. If the number is decimal, you have half chances to get a wrong result. For example, there is currently a <a href="http://code.google.com/p/chromium/issues/detail?id=163960">bug in Chrome</a> where hardware accelerated rendering has similar issue.</p>
<p>In order to get good scrolling performance, we enable hardware acceleration using <code>transform: translateZ(0);</code> on all the visible images on the viewport. However, when we mouse over an image, we display some overlay and therefore decide to remove hardware acceleration for it to avoid thrashing GPU memory.</p>
<p>To display images, we use a container as described above with the CSS property <code>left: -7.92%;</code> to position the image properly in the viewport. The result is that the image is moving around when you mouse hover it on Chrome. There is probably a different rounding applied between the CPU and the GPU code. The net effect is the image being resized by one pixel and blurry by default. When you mouse over, the image has the correct size.</p>
<style>#hover_hardware_acceleration { -webkit-transform: translateZ(0); } #hover_hardware_acceleration:hover { -webkit-transform: none; } #hover_hardware_acceleration:hover #hover_label { display: block; } #hover_label { display: none; }</style>
<div style="width: 322px; height: 280px; position: relative; overflow: hidden; margin: 0 auto;" id="hover_hardware_acceleration">
<img src="https://sphotos-a.xx.fbcdn.net/hphotos-prn1/p280x280/156904_4651801489084_374548307_n.jpg" width="373" height="280" style="left: -7.92%; position: relative;" /></p>
<div style="position: absolute; bottom: 0; left: 0; right: 0; height: 50px; background: rgba(0, 0, 0, 0.5);" id="hover_label"></div>
</div>
<p>In order to fix the issue, we can use integer number in pixel <code>left: -24px;</code> instead. This way the browser doesn't have to round anything.</p>
<style>#hover_hardware_acceleration { -webkit-transform: translateZ(0); } #hover_hardware_acceleration:hover { -webkit-transform: none; } #hover_hardware_acceleration:hover #hover_label { display: block; } #hover_label { display: none; }</style>
<div style="width: 322px; height: 280px; position: relative; overflow: hidden; margin: 0 auto;" id="hover_hardware_acceleration">
<img src="https://sphotos-a.xx.fbcdn.net/hphotos-prn1/p280x280/156904_4651801489084_374548307_n.jpg" width="373" height="280" style="left: -24px; position: relative;" /></p>
<div style="position: absolute; bottom: 0; left: 0; right: 0; height: 50px; background: rgba(0, 0, 0, 0.5);" id="hover_label"></div>
</div>
<p>This is only one of the many similar issues with the browsers handling rounding differently. People implementing fluid layout suffer a lot <a href="http://steff.me/journal/fluid-inconsistencies/">because of browser inconsistencies</a>. If this is happening in browser implementations, there is also a high probability that this issue is going to appear in your own code if you didn't make sure it was rounding as expected.</p>
<h3>Conclusion</h3>
<p>This problem is very common and comes from many different sources, but always because of the same root cause: rounding issues. Since sub-pixel rendering is not widely implemented, it is not going to disappear. I hope that you are now aware of it and will address it to avoid affecting image quality of your thumbnails <img src='http://blog.vjeux.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vjeux.com/2012/image/beware-of-one-pixel-resizing.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>One Mistake Sequence</title>
		<link>http://blog.vjeux.com/2012/analysis/one-mistake-sequence.html</link>
		<comments>http://blog.vjeux.com/2012/analysis/one-mistake-sequence.html#comments</comments>
		<pubDate>Sun, 02 Dec 2012 20:53:30 +0000</pubDate>
		<dc:creator>vjeux</dc:creator>
				<category><![CDATA[Research]]></category>

		<guid isPermaLink="false">http://blog.vjeux.com/?p=3974</guid>
		<description><![CDATA[I'm working a lot with URLs that contain ids and very often, I made a mistake in one digit of the long id and end up with a completely different element. If I don't pay attention, then I end up looking at two elements thinking they are the same and am intrigued until I find [...]]]></description>
				<content:encoded><![CDATA[<p>I'm working a lot with URLs that contain ids and very often, I made a mistake in one digit of the long id and end up with a completely different element. If I don't pay attention, then I end up looking at two elements thinking they are the same and am intrigued until I find out the mistake.</p>
<p>In order to avoid that, I wanted to know if I could make a sequence of ids where making one mistake would not give a valid id. For example, if you have the id 473, then you have to black list all the ids where the first digit is wrong (073, 173, 273, 373, 573, 673, 773, 873, 973) and the second being wrong (403, 413, 423, 433, 443, 453, 463, 483, 493) and the last one (470, 471, 472, 474, 475, 476, 477, 478, 479).</p>
<div style="text-align: center;"><img src="http://blog.vjeux.com/wp-content/uploads/2012/11/473.png" /></div>
<p>Here is the list of the first pseudo-numbers:</p>
<style>#pseudo-list li { float: left; margin-left: 65px; }</style>
<ol id="pseudo-list">
<li>011</li>
<li>022</li>
<li>033</li>
<li>044</li>
<li>055</li>
<li>066</li>
<li>077</li>
<li>088</li>
<li>099</li>
<li>101</li>
<li>110</li>
<li>123</li>
<li>132</li>
<li>145</li>
<li>154</li>
<li>167</li>
<li>176</li>
<li>189</li>
<li>198</li>
<li>202</li>
<li>213</li>
<li>220</li>
<li>231</li>
<li>246</li>
<li>257</li>
<li>264</li>
<li>275</li>
<li>303</li>
<li>312</li>
<li>321</li>
<li>330</li>
<li>347</li>
<li>356</li>
<li>365</li>
<li>374</li>
<li>404</li>
<li>415</li>
<li>426</li>
<li>437</li>
<li>440</li>
<li>451</li>
<li>462</li>
<li>473</li>
<li>505</li>
<li>514</li>
<li>527</li>
<li>536</li>
<li>541</li>
<li>550</li>
<li>563</li>
<li>572</li>
<li>606</li>
<li>617</li>
<li>624</li>
<li>635</li>
<li>642</li>
<li>653</li>
<li>660</li>
<li>671</li>
<li>707</li>
<li>716</li>
<li>725</li>
<li>734</li>
<li>743</li>
<li>752</li>
<li>761</li>
<li>770</li>
<li>808</li>
<li>819</li>
<li>880</li>
<li>891</li>
<li>909</li>
<li>918</li>
<li>981</li>
<li>990</li>
</ol>
<div style="clear: left;"></div>
<p>And here is a visual representation of those numbers:</p>
<p><img src="https://docs.google.com/spreadsheet/oimg?key=0AokR61TVHyp6dEYwSXhGNnZTM3ZoUTJpRWNyMVlGWXc&#038;oid=2&#038;zx=268uiw48oyci" /></p>
<p>For each digit in the number, we blacklist 9 other numbers. For a 5 digits number (eg 12345), that means blacklisting 45 numbers. For a 10 digits number (eg 1234567890), that means blacklisting 90 numbers. The number of blacklisted numbers only grows at a logarithmic scale.</p>
<p>In order to see how many numbers we lose, I plotted the ratio of pseudo numbers count compared to the real numbers. We can roughly keep one number every fifteen. But the good news is that the ratio doesn't fall off the chart as the numbers grow.</p>
<p><img src="https://docs.google.com/spreadsheet/oimg?key=0AokR61TVHyp6dEYwSXhGNnZTM3ZoUTJpRWNyMVlGWXc&#038;oid=3&#038;zx=7t5vfx13e3ni" /></p>
<p>Looking at the numbers, they looked like to go from 10 to 10 but with some huge spikes and sometimes they were closer. So I plotted the difference between two consecutive numbers in a chart and it looks like the difference is centered around 10. But the variance is getting higher and higher as you move further.</p>
<p><img src="https://docs.google.com/spreadsheet/oimg?key=0AokR61TVHyp6dEYwSXhGNnZTM3ZoUTJpRWNyMVlGWXc&#038;oid=1&#038;zx=zd8dexyyde4o" /></p>
<p>I'm not really sure if this sequence can be really useful in practice but that was a fun week-end experiment. I hope it'll give you some, hopefully useful, ideas <img src='http://blog.vjeux.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.vjeux.com/2012/analysis/one-mistake-sequence.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Image Layout Algorithm &#8211; Lightbox Android</title>
		<link>http://blog.vjeux.com/2012/image/image-layout-algorithm-lightbox-android.html</link>
		<comments>http://blog.vjeux.com/2012/image/image-layout-algorithm-lightbox-android.html#comments</comments>
		<pubDate>Sun, 07 Oct 2012 19:39:05 +0000</pubDate>
		<dc:creator>vjeux</dc:creator>
				<category><![CDATA[Image]]></category>

		<guid isPermaLink="false">http://blog.vjeux.com/?p=3516</guid>
		<description><![CDATA[Layout Algorithms: Facebook &#124; Google Plus &#124; Lightbox &#124; Lightbox Android &#124; 500px I contacted the CEO of Lightbox to share some thoughts about its layout algorithm and he told me this wasn't the only one they made. Here is the description of another interesting algorithm How does it work? The golden nugget in the [...]]]></description>
				<content:encoded><![CDATA[<div style="background: #FAF9E2; border: solid #DDDAAA; border-width: initial; color: #5D5636; margin-bottom: 0em; padding: 0.75em 15px; text-align: center;">Layout Algorithms: <a href="/2012/javascript/image-layout-algorithm-facebook.html">Facebook</a> | <a href="/2012/javascript/image-layout-algorithm-google-plus.html">Google Plus</a> | <a href="/2012/javascript/image-layout-algorithm-lightbox.html">Lightbox</a> | <strong>Lightbox Android</strong> | <a href="/2012/javascript/image-layout-algorithm-500px.html">500px</a></div>
<p>I contacted the CEO of Lightbox to share some thoughts about its layout algorithm and he told me this wasn't the only one they made. Here is the description of another interesting algorithm <img src='http://blog.vjeux.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div style="text-align: center;">
<a href="http://blog.vjeux.com/wp-content/uploads/2012/05/lightbox-android-layout.html"><img src="http://blog.vjeux.com/wp-content/uploads/2012/09/lightbox-android.jpg" /></a>
</div>
<h3>How does it work?</h3>
<blockquote><p>
The golden nugget in the whole equation being the square root of two! Anything with the aspect ratio of 1.41421 (square root of two) can be divided in half and produce two more with the same aspect ratio. And as this was close enough to 4:3 (or 3:4) we were able to crop the photos in the collage view to this aspect ratio without it being too noticeable. This way we could have an arbitrary list of landscape and portrait photos and still generate a suitable layout. -- <a href="https://twitter.com/nilzvpatel">Nilesh Patel</a>
</p></blockquote>
<p>Let's take an example, we have an image where the height is 1 and width is square root of 2.</p>
<div style="text-align: center;">
<img src="http://blog.vjeux.com/wp-content/uploads/2012/09/cut-ratio.png" width="230" />
</div>
<p>Let's split it in half horizontally and calculate the aspect ratio of both the new and old images.</p>
<div style="text-align: center;">
<img src="http://blog.vjeux.com/wp-content/uploads/2012/09/same-ratio.png" width="500" />
</div>
<p>They are both the same <img src='http://blog.vjeux.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  It means that you can split the image as many time as you want and you will always keep the same aspect ratio:</p>
<div style="text-align: center;">
<img src="http://blog.vjeux.com/wp-content/uploads/2012/09/cut-cut-cut.png" />
</div>
<h3>How much do you crop?</h3>
<p>Let's say we have an image of 300 pixels in width, the image would be <code>300/(4/3) = 225</code>px in height but instead is <code>300/sqrt(2) = 212.12</code>px. It's a 12ish pixels difference, 6 pixels on each side. Let's look at how it looks in practice. The dark part is the full image and light one is the viewport.</p>
<style>
#outer-lightbox-android {
    width: 300px;
    height: 219px;
    background-color: #666;
    padding-top: 6px;    
    margin: 0 auto;
}
#inner-lightbox-android {
    width: 300px;
    height: 213px;
    background-color: #ccc;
}
</style>
<div id="outer-lightbox-android">
<div id="inner-lightbox-android">
    </div>
</div>
<p>​If you want to be rigorous, you also have to remove few pixels of padding every time you split the image in half. But that's only another 2 or 3 pixels per split, that's still a pretty good approximation.</p>
<h3>Image Sizes</h3>
<p>In order to keep images good looking, you have to set a minimum and maximum allowed size. Every time a split happens, the resulting image is half the size. Portrait and landscape images alternate at every split. This means that the next image with the same orientation is going to be a quarter of the previous image.</p>
<p>In practice, you can only have two sizes for each orientation or the images are either way too big or way too small. You end up with two different sizes for each orientation. This is good, each image must now only be labelled by "big" or "small" by the algorithm.</p>
<div style="text-align: center;"><img src="http://blog.vjeux.com/wp-content/uploads/2012/10/sizes.png" /></div>
<p>Another thing to keep in mind is that two images with a different orientation cannot have the same area. The closest setup you can have is one being two times bigger (or smaller) than the other one. The tricky thing is that this choice is not per image basis but global to the layout. At the beginning, you have to chose one orientation that is going to be twice as big as the other, this choice may not be easy to do.</p>
<h3>Conclusion</h3>
<p>Check out the <a href="http://blog.vjeux.com/wp-content/uploads/2012/05/lightbox-android-layout.html">Demo</a>!</p>
<p>Pros:</p>
<ul>
<li>Works with both landscape and portrait</li>
<li>Can chose between two sizes for each orientation</li>
<li>No holes</li>
</ul>
<p>Cons:</p>
<ul>
<li>Having to chose an orientation that is going to be twice as big as the other</li>
<li>Ordering isn't really respected when there are many sizes and orientations</li>
<li>Small cropping</li>
<li>End of stream is tricky to implement</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.vjeux.com/2012/image/image-layout-algorithm-lightbox-android.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Best Cropping Position</title>
		<link>http://blog.vjeux.com/2012/image/best-cropping-position.html</link>
		<comments>http://blog.vjeux.com/2012/image/best-cropping-position.html#comments</comments>
		<pubDate>Thu, 04 Oct 2012 02:27:02 +0000</pubDate>
		<dc:creator>vjeux</dc:creator>
				<category><![CDATA[Image]]></category>

		<guid isPermaLink="false">http://blog.vjeux.com/?p=3819</guid>
		<description><![CDATA[In Facebook image layout algorithm, we use square viewport to display the images. Since images are not usually square, we have an issue to solve. Contain Cover There are two possible ways to deal with it. You make the image fit entirely in the viewport and add black borders (think about viewing 4/3 movies in [...]]]></description>
				<content:encoded><![CDATA[<p>In <a href="http://blog.vjeux.com/2012/facebook/image-layout-algorithm-facebook.html">Facebook image layout algorithm</a>, we use square viewport to display the images. Since images are not usually square, we have an issue to solve.</p>
<h3>Contain Cover</h3>
<p>There are two possible ways to deal with it. You make the image fit entirely in the viewport and add black borders (think about viewing 4/3 movies in a wide screen). Or you can make the viewport fit entirely in the image. Instead of having black bars, you are going to remove some parts of the image.</p>
<p>In CSS, the names for those two concepts are implemented with <code>background-size</code> property that has two values: <code>contain</code> and <code>cover</code>.</p>
<div style="text-align: center;"><img src="http://blog.vjeux.com/wp-content/uploads/2012/10/containcover.png" /></div>
<p>In our case, we display images in a grid. The <code>cover</code> version works best because the images align nicely in the grid. It makes the edges much more visible that gives a structure to the page.</p>
<p><img src="http://blog.vjeux.com/wp-content/uploads/2012/10/containcover_real.png" /></p>
<h3>Set up the problem</h3>
<p>The choice we made raises another issue: we are no longer displaying the entire photo but only a subset of it. Therefore we have to know what parts of the image we want to keep, and what parts we want to hide.</p>
<div style="text-align: center;"><img src="http://blog.vjeux.com/wp-content/uploads/2012/10/best-crop.png" /></div>
<p>The first thing to notice, is that there is only one degree of freedom. You can either pan the image horizontally or vertically depending on the aspect ratio of the image and viewport.</p>
<p>In order to make that decision, we need to have an idea of what is important in the image. Thankfully, at Facebook, people can tag the images and tell us where the people and other point of interests are. We also know that people are important so we also use detected faces. In the future we could automatically find more such as text, animals ...</p>
<p>Now we have a clearer view of the inputs. We have a set of point of interests aligned in one dimension. We also have a window that we can slide on this dimension. Here's an example:</p>
<div style="text-align: center;"><img src="http://blog.vjeux.com/wp-content/uploads/2012/10/setup.png" /></div>
<h3>Find maximal window</h3>
<p>The idea of the algorithm is to find the position of the window that maximizes the number of point of interests it contains.</p>
<p>A window can be defined only by its starting position since its width is constant. This makes the search space to be the number of pixels in a column/line of the image (minus the size of the window as it must not go outside). Then for each position, you have to compute how many points are inside.</p>
<p>A naive implementation is going to be in the order of O(p * n) where p is the number of pixels and n the number of point of interests. For a typical image with people this means 960 * 3 = 2880 checks. This is way too costly because the number of pixels is an order of magnitude higher than the number of point of interests.</p>
<p>We want to approach the problem the starting from the point of interests. A window can contain, or not, a point of interest. Two windows next to each other that contain the same point of interests can be considered equivalent. With this definition, we can find all the windows much quicker.</p>
<p>We are going to iterate on all the point of interests and consider that they are in the left-most edge of the window. This is the boundary between it between inside and outside of the window. We compute how many points are in that window and keep it if it's bigger than what we had before.</p>
<div style="text-align: left; padding-left: 170px;"><img src="http://blog.vjeux.com/wp-content/uploads/2012/09/reposition-bigger-set.png" /></div>
<p>In order to implement this effectively, we can iterate on all the point of interests in O(n). Using binary search, we find the right-most point in O(log(n)), if the points of interests are sorted. It's a O(n * log(n)) to sort them at the beginning.</p>
<p>The total complexity is therefore O(n * log(n)), where n is the number of point of interests. Since most of our images have less than 10 point of interests, our algorithm is essentially free.</p>
<h3>Center the window</h3>
<p>With a window aligned on the left element, we can balance the right padding equally across left and right to center the window.</p>
<div style="text-align: center;"><img src="http://blog.vjeux.com/wp-content/uploads/2012/10/balance_space.png" /></div>
<p>The previous algorithm can return multiple windows that have the same number of points of interest. In this case, we use the window that has the bigger amount of padding. This ensures that heads are less likely to be cut-off in half.</p>
<h3>Force someone inside</h3>
<p>When you are viewing all the photos someone is tagged in, you would like to make sure that the person isn't being cropped out by the algorithm. Previously, what was done was to center the image on that person. We could instead keep using the current algorithm but force the person in.</p>
<p>Again, we are going to look at the boundaries. The person can be from the left edge to the right one. So we are going to place the person at the left and right of the viewport and remove all the points that cannot be in the same screen.</p>
<div style="text-align: center;"><img src="http://blog.vjeux.com/wp-content/uploads/2012/10/force_element.png" /></div>
<p>Then, we have the guarantee that all the sets of points we are going to chose also contain the person. We run the algorithm again with that limited set of points to find the window.</p>
<h3>Conclusion</h3>
<p>The current heuristic is to have images horizontally centered and vertically centered at the first third of the photo, where most faces at. Those are empiric values that work surprisingly well. Also, most images are 4:3, therefore cropping to a 1:1 ratio removes 25% of the photo total, which is only 12.5% on each side.</p>
<p>In order to test the algorithm, we use more extreme viewports. A quick run on my photos and some friends photos shows that it either leaves the crop as is or improves it. It is going to be interesting to test it on Facebook views.</p>
<div style="text-align: center;"><a href="http://blog.vjeux.com/wp-content/uploads/2012/10/cropping.html"><img src="http://blog.vjeux.com/wp-content/uploads/2012/10/before_after.png" /></a></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.vjeux.com/2012/image/best-cropping-position.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
