ftlcdfil.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /****************************************************************************
  2. *
  3. * ftlcdfil.h
  4. *
  5. * FreeType API for color filtering of subpixel bitmap glyphs
  6. * (specification).
  7. *
  8. * Copyright (C) 2006-2023 by
  9. * David Turner, Robert Wilhelm, and Werner Lemberg.
  10. *
  11. * This file is part of the FreeType project, and may only be used,
  12. * modified, and distributed under the terms of the FreeType project
  13. * license, LICENSE.TXT. By continuing to use, modify, or distribute
  14. * this file you indicate that you have read the license and
  15. * understand and accept it fully.
  16. *
  17. */
  18. #ifndef FTLCDFIL_H_
  19. #define FTLCDFIL_H_
  20. #include <freetype/freetype.h>
  21. #include <freetype/ftparams.h>
  22. #ifdef FREETYPE_H
  23. #error "freetype.h of FreeType 1 has been loaded!"
  24. #error "Please fix the directory search order for header files"
  25. #error "so that freetype.h of FreeType 2 is found first."
  26. #endif
  27. FT_BEGIN_HEADER
  28. /**************************************************************************
  29. *
  30. * @section:
  31. * lcd_rendering
  32. *
  33. * @title:
  34. * Subpixel Rendering
  35. *
  36. * @abstract:
  37. * API to control subpixel rendering.
  38. *
  39. * @description:
  40. * FreeType provides two alternative subpixel rendering technologies.
  41. * Should you define `FT_CONFIG_OPTION_SUBPIXEL_RENDERING` in your
  42. * `ftoption.h` file, this enables ClearType-style rendering.
  43. * Otherwise, Harmony LCD rendering is enabled. These technologies are
  44. * controlled differently and API described below, although always
  45. * available, performs its function when appropriate method is enabled
  46. * and does nothing otherwise.
  47. *
  48. * ClearType-style LCD rendering exploits the color-striped structure of
  49. * LCD pixels, increasing the available resolution in the direction of
  50. * the stripe (usually horizontal RGB) by a factor of~3. Using the
  51. * subpixel coverages unfiltered can create severe color fringes
  52. * especially when rendering thin features. Indeed, to produce
  53. * black-on-white text, the nearby color subpixels must be dimmed
  54. * evenly. Therefore, an equalizing 5-tap FIR filter should be applied
  55. * to subpixel coverages regardless of pixel boundaries and should have
  56. * these properties:
  57. *
  58. * 1. It should be symmetrical, like {~a, b, c, b, a~}, to avoid
  59. * any shifts in appearance.
  60. *
  61. * 2. It should be color-balanced, meaning a~+ b~=~c, to reduce color
  62. * fringes by distributing the computed coverage for one subpixel to
  63. * all subpixels equally.
  64. *
  65. * 3. It should be normalized, meaning 2a~+ 2b~+ c~=~1.0 to maintain
  66. * overall brightness.
  67. *
  68. * Boxy 3-tap filter {0, 1/3, 1/3, 1/3, 0} is sharper but is less
  69. * forgiving of non-ideal gamma curves of a screen (and viewing angles),
  70. * beveled filters are fuzzier but more tolerant.
  71. *
  72. * Use the @FT_Library_SetLcdFilter or @FT_Library_SetLcdFilterWeights
  73. * API to specify a low-pass filter, which is then applied to
  74. * subpixel-rendered bitmaps generated through @FT_Render_Glyph.
  75. *
  76. * Harmony LCD rendering is suitable to panels with any regular subpixel
  77. * structure, not just monitors with 3 color striped subpixels, as long
  78. * as the color subpixels have fixed positions relative to the pixel
  79. * center. In this case, each color channel can be rendered separately
  80. * after shifting the outline opposite to the subpixel shift so that the
  81. * coverage maps are aligned. This method is immune to color fringes
  82. * because the shifts do not change integral coverage.
  83. *
  84. * The subpixel geometry must be specified by xy-coordinates for each
  85. * subpixel. By convention they may come in the RGB order: {{-1/3, 0},
  86. * {0, 0}, {1/3, 0}} for standard RGB striped panel or {{-1/6, 1/4},
  87. * {-1/6, -1/4}, {1/3, 0}} for a certain PenTile panel.
  88. *
  89. * Use the @FT_Library_SetLcdGeometry API to specify subpixel positions.
  90. * If one follows the RGB order convention, the same order applies to the
  91. * resulting @FT_PIXEL_MODE_LCD and @FT_PIXEL_MODE_LCD_V bitmaps. Note,
  92. * however, that the coordinate frame for the latter must be rotated
  93. * clockwise. Harmony with default LCD geometry is equivalent to
  94. * ClearType with light filter.
  95. *
  96. * As a result of ClearType filtering or Harmony shifts, the resulting
  97. * dimensions of LCD bitmaps can be slightly wider or taller than the
  98. * dimensions the original outline with regard to the pixel grid.
  99. * For example, for @FT_RENDER_MODE_LCD, the filter adds 2~subpixels to
  100. * the left, and 2~subpixels to the right. The bitmap offset values are
  101. * adjusted accordingly, so clients shouldn't need to modify their layout
  102. * and glyph positioning code when enabling the filter.
  103. *
  104. * The ClearType and Harmony rendering is applicable to glyph bitmaps
  105. * rendered through @FT_Render_Glyph, @FT_Load_Glyph, @FT_Load_Char, and
  106. * @FT_Glyph_To_Bitmap, when @FT_RENDER_MODE_LCD or @FT_RENDER_MODE_LCD_V
  107. * is specified. This API does not control @FT_Outline_Render and
  108. * @FT_Outline_Get_Bitmap.
  109. *
  110. * The described algorithms can completely remove color artefacts when
  111. * combined with gamma-corrected alpha blending in linear space. Each of
  112. * the 3~alpha values (subpixels) must by independently used to blend one
  113. * color channel. That is, red alpha blends the red channel of the text
  114. * color with the red channel of the background pixel.
  115. */
  116. /**************************************************************************
  117. *
  118. * @enum:
  119. * FT_LcdFilter
  120. *
  121. * @description:
  122. * A list of values to identify various types of LCD filters.
  123. *
  124. * @values:
  125. * FT_LCD_FILTER_NONE ::
  126. * Do not perform filtering. When used with subpixel rendering, this
  127. * results in sometimes severe color fringes.
  128. *
  129. * FT_LCD_FILTER_DEFAULT ::
  130. * This is a beveled, normalized, and color-balanced five-tap filter
  131. * with weights of [0x08 0x4D 0x56 0x4D 0x08] in 1/256 units.
  132. *
  133. * FT_LCD_FILTER_LIGHT ::
  134. * this is a boxy, normalized, and color-balanced three-tap filter with
  135. * weights of [0x00 0x55 0x56 0x55 0x00] in 1/256 units.
  136. *
  137. * FT_LCD_FILTER_LEGACY ::
  138. * FT_LCD_FILTER_LEGACY1 ::
  139. * This filter corresponds to the original libXft color filter. It
  140. * provides high contrast output but can exhibit really bad color
  141. * fringes if glyphs are not extremely well hinted to the pixel grid.
  142. * This filter is only provided for comparison purposes, and might be
  143. * disabled or stay unsupported in the future. The second value is
  144. * provided for compatibility with FontConfig, which historically used
  145. * different enumeration, sometimes incorrectly forwarded to FreeType.
  146. *
  147. * @since:
  148. * 2.3.0 (`FT_LCD_FILTER_LEGACY1` since 2.6.2)
  149. */
  150. typedef enum FT_LcdFilter_
  151. {
  152. FT_LCD_FILTER_NONE = 0,
  153. FT_LCD_FILTER_DEFAULT = 1,
  154. FT_LCD_FILTER_LIGHT = 2,
  155. FT_LCD_FILTER_LEGACY1 = 3,
  156. FT_LCD_FILTER_LEGACY = 16,
  157. FT_LCD_FILTER_MAX /* do not remove */
  158. } FT_LcdFilter;
  159. /**************************************************************************
  160. *
  161. * @function:
  162. * FT_Library_SetLcdFilter
  163. *
  164. * @description:
  165. * This function is used to change filter applied to LCD decimated
  166. * bitmaps, like the ones used when calling @FT_Render_Glyph with
  167. * @FT_RENDER_MODE_LCD or @FT_RENDER_MODE_LCD_V.
  168. *
  169. * @input:
  170. * library ::
  171. * A handle to the target library instance.
  172. *
  173. * filter ::
  174. * The filter type.
  175. *
  176. * You can use @FT_LCD_FILTER_NONE here to disable this feature, or
  177. * @FT_LCD_FILTER_DEFAULT to use a default filter that should work well
  178. * on most LCD screens.
  179. *
  180. * @return:
  181. * FreeType error code. 0~means success.
  182. *
  183. * @note:
  184. * Since 2.10.3 the LCD filtering is enabled with @FT_LCD_FILTER_DEFAULT.
  185. * It is no longer necessary to call this function explicitly except
  186. * to choose a different filter or disable filtering altogether with
  187. * @FT_LCD_FILTER_NONE.
  188. *
  189. * This function does nothing but returns `FT_Err_Unimplemented_Feature`
  190. * if the configuration macro `FT_CONFIG_OPTION_SUBPIXEL_RENDERING` is
  191. * not defined in your build of the library.
  192. *
  193. * @since:
  194. * 2.3.0
  195. */
  196. FT_EXPORT( FT_Error )
  197. FT_Library_SetLcdFilter( FT_Library library,
  198. FT_LcdFilter filter );
  199. /**************************************************************************
  200. *
  201. * @function:
  202. * FT_Library_SetLcdFilterWeights
  203. *
  204. * @description:
  205. * This function can be used to enable LCD filter with custom weights,
  206. * instead of using presets in @FT_Library_SetLcdFilter.
  207. *
  208. * @input:
  209. * library ::
  210. * A handle to the target library instance.
  211. *
  212. * weights ::
  213. * A pointer to an array; the function copies the first five bytes and
  214. * uses them to specify the filter weights in 1/256 units.
  215. *
  216. * @return:
  217. * FreeType error code. 0~means success.
  218. *
  219. * @note:
  220. * This function does nothing but returns `FT_Err_Unimplemented_Feature`
  221. * if the configuration macro `FT_CONFIG_OPTION_SUBPIXEL_RENDERING` is
  222. * not defined in your build of the library.
  223. *
  224. * LCD filter weights can also be set per face using @FT_Face_Properties
  225. * with @FT_PARAM_TAG_LCD_FILTER_WEIGHTS.
  226. *
  227. * @since:
  228. * 2.4.0
  229. */
  230. FT_EXPORT( FT_Error )
  231. FT_Library_SetLcdFilterWeights( FT_Library library,
  232. unsigned char *weights );
  233. /**************************************************************************
  234. *
  235. * @type:
  236. * FT_LcdFiveTapFilter
  237. *
  238. * @description:
  239. * A typedef for passing the five LCD filter weights to
  240. * @FT_Face_Properties within an @FT_Parameter structure.
  241. *
  242. * @since:
  243. * 2.8
  244. *
  245. */
  246. #define FT_LCD_FILTER_FIVE_TAPS 5
  247. typedef FT_Byte FT_LcdFiveTapFilter[FT_LCD_FILTER_FIVE_TAPS];
  248. /**************************************************************************
  249. *
  250. * @function:
  251. * FT_Library_SetLcdGeometry
  252. *
  253. * @description:
  254. * This function can be used to modify default positions of color
  255. * subpixels, which controls Harmony LCD rendering.
  256. *
  257. * @input:
  258. * library ::
  259. * A handle to the target library instance.
  260. *
  261. * sub ::
  262. * A pointer to an array of 3 vectors in 26.6 fractional pixel format;
  263. * the function modifies the default values, see the note below.
  264. *
  265. * @return:
  266. * FreeType error code. 0~means success.
  267. *
  268. * @note:
  269. * Subpixel geometry examples:
  270. *
  271. * - {{-21, 0}, {0, 0}, {21, 0}} is the default, corresponding to 3 color
  272. * stripes shifted by a third of a pixel. This could be an RGB panel.
  273. *
  274. * - {{21, 0}, {0, 0}, {-21, 0}} looks the same as the default but can
  275. * specify a BGR panel instead, while keeping the bitmap in the same
  276. * RGB888 format.
  277. *
  278. * - {{0, 21}, {0, 0}, {0, -21}} is the vertical RGB, but the bitmap
  279. * stays RGB888 as a result.
  280. *
  281. * - {{-11, 16}, {-11, -16}, {22, 0}} is a certain PenTile arrangement.
  282. *
  283. * This function does nothing and returns `FT_Err_Unimplemented_Feature`
  284. * in the context of ClearType-style subpixel rendering when
  285. * `FT_CONFIG_OPTION_SUBPIXEL_RENDERING` is defined in your build of the
  286. * library.
  287. *
  288. * @since:
  289. * 2.10.0
  290. */
  291. FT_EXPORT( FT_Error )
  292. FT_Library_SetLcdGeometry( FT_Library library,
  293. FT_Vector sub[3] );
  294. /* */
  295. FT_END_HEADER
  296. #endif /* FTLCDFIL_H_ */
  297. /* END */