title: SDL2_ttf Subpixel Antialiasing
body: <p>Is it possible to render text with the SDL2 library using a <a href="https://en.wikipedia.org/wiki/Subpixel_rendering" rel="nofollow noreferrer">subpixel rendering</a> technique to increase the quality on LCD screens?  (Preferably with standard SDL2_ttf.)</p>
  
  <p>The following image demonstrates precisely what I'm aiming for, where the image on the far-left is the actual result that would be seen upon zooming in:</p>
  
  <p><a href="https://i.stack.imgur.com/B4GIR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/B4GIR.png" alt="subpixel rendering example"></a></p>
  
  <p>If this is not possible 'out of the box', but you believe it <em>is possible to implement</em> in the open-source SDL2_ttf; I'd appreciate some ideas or pointers in the right direction as to how it could be implemented.</p>
  
  <hr>
  
  <h3>EDIT:</h3>
  
  <p>I've started looking through the <a href="https://www.libsdl.org/projects/SDL_ttf/" rel="nofollow noreferrer">source code of SDL2_ttf</a>, and came to the conclusion that it I would have to modify it in order to achieve subpixel LCD antialiasing.<br><br>
  From what I've researched about FreeType, I would have to change the following lines of code:</p>
  
  <pre><code>// (from SDL_ttf.c, approx. line 650)
  /* Render the glyph */
  error = FT_Render_Glyph(glyph, mono ? ft_render_mode_mono : ft_render_mode_normal);
  </code></pre>
  
  <p>To</p>
  
  <pre><code>/* Render the glyph, (ft_render_mode_mono is also deprecated) */
  error = FT_Render_Glyph(glyph, mono ? FT_RENDER_MODE_MONO : FT_RENDER_MODE_LCD);
  </code></pre>
  
  <p>Additionally, some code needs to be added to write to the bitmap. I have absolutely no idea how to do this, as I barely understand what's already there, due to the lack of comments. The following is the 'grey 2-bit' pixel-mode's code:</p>
  
  <pre><code>// (from SDL_ttf.c, approx. line 800)
  // ... other pixel modes handled above this...
  else if (src-&gt;pixel_mode == FT_PIXEL_MODE_GRAY2) {
      // srcp appears to be the source buffer, 
      // and dstp the destination buffer.
      unsigned char *srcp = src-&gt;buffer + soffset;
      unsigned char *dstp = dst-&gt;buffer + doffset;
      unsigned char c;
      unsigned int j, k;
  
      // No idea how any of this works.
      // I'd guess 'j' should increment by 1
      // instead of 4, since LCD uses 8-bits.
      for (j = 0; j &lt; src-&gt;width; j += 4) {
          c = *srcp++;
          for (k = 0; k &lt; 4; ++k) 
          {
              // Literally no idea where they pulled these numbers out.
              if ((c&amp;0xA0) &gt;&gt; 6) {
                  *dstp++ = NUM_GRAYS * ((c&amp;0xA0) &gt;&gt; 6) / 3 - 1;
              } 
              else {
                  *dstp++ = 0x00;
              }
          }
          c &lt;&lt;= 2;
      }
  }
  // 4-bit grey is done down here...
  </code></pre>
  
  <p>I'm hoping someone is capable of helping me out on this one...<br>
  Cheers.</p>