🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

635 lines
24 KiB

  1. How FreeType's rasterizer work
  2. by David Turner
  3. Revised 2007-Feb-01
  4. This file is an attempt to explain the internals of the FreeType
  5. rasterizer. The rasterizer is of quite general purpose and could
  6. easily be integrated into other programs.
  7. I. Introduction
  8. II. Rendering Technology
  9. 1. Requirements
  10. 2. Profiles and Spans
  11. a. Sweeping the Shape
  12. b. Decomposing Outlines into Profiles
  13. c. The Render Pool
  14. d. Computing Profiles Extents
  15. e. Computing Profiles Coordinates
  16. f. Sweeping and Sorting the Spans
  17. I. Introduction
  18. ===============
  19. A rasterizer is a library in charge of converting a vectorial
  20. representation of a shape into a bitmap. The FreeType rasterizer
  21. has been originally developed to render the glyphs found in
  22. TrueType files, made up of segments and second-order Béziers.
  23. Meanwhile it has been extended to render third-order Bézier curves
  24. also. This document is an explanation of its design and
  25. implementation.
  26. While these explanations start from the basics, a knowledge of
  27. common rasterization techniques is assumed.
  28. II. Rendering Technology
  29. ========================
  30. 1. Requirements
  31. ---------------
  32. We assume that all scaling, rotating, hinting, etc., has been
  33. already done. The glyph is thus described by a list of points in
  34. the device space.
  35. - All point coordinates are in the 26.6 fixed float format. The
  36. used orientation is:
  37. ^ y
  38. | reference orientation
  39. |
  40. *----> x
  41. 0
  42. `26.6' means that 26 bits are used for the integer part of a
  43. value and 6 bits are used for the fractional part.
  44. Consequently, the `distance' between two neighbouring pixels is
  45. 64 `units' (1 unit = 1/64th of a pixel).
  46. Note that, for the rasterizer, pixel centers are located at
  47. integer coordinates. The TrueType bytecode interpreter,
  48. however, assumes that the lower left edge of a pixel (which is
  49. taken to be a square with a length of 1 unit) has integer
  50. coordinates.
  51. ^ y ^ y
  52. | |
  53. | (1,1) | (0.5,0.5)
  54. +-----------+ +-----+-----+
  55. | | | | |
  56. | | | | |
  57. | | | o-----+-----> x
  58. | | | (0,0) |
  59. | | | |
  60. o-----------+-----> x +-----------+
  61. (0,0) (-0.5,-0.5)
  62. TrueType bytecode interpreter FreeType rasterizer
  63. A pixel line in the target bitmap is called a `scanline'.
  64. - A glyph is usually made of several contours, also called
  65. `outlines'. A contour is simply a closed curve that delimits an
  66. outer or inner region of the glyph. It is described by a series
  67. of successive points of the points table.
  68. Each point of the glyph has an associated flag that indicates
  69. whether it is `on' or `off' the curve. Two successive `on'
  70. points indicate a line segment joining the two points.
  71. One `off' point amidst two `on' points indicates a second-degree
  72. (conic) Bézier parametric arc, defined by these three points
  73. (the `off' point being the control point, and the `on' ones the
  74. start and end points). Similarly, a third-degree (cubic) Bézier
  75. curve is described by four points (two `off' control points
  76. between two `on' points).
  77. Finally, for second-order curves only, two successive `off'
  78. points forces the rasterizer to create, during rendering, an
  79. `on' point amidst them, at their exact middle. This greatly
  80. facilitates the definition of successive Bézier arcs.
  81. The parametric form of a second-order Bézier curve is:
  82. P(t) = (1-t)^2*P1 + 2*t*(1-t)*P2 + t^2*P3
  83. (P1 and P3 are the end points, P2 the control point.)
  84. The parametric form of a third-order Bézier curve is:
  85. P(t) = (1-t)^3*P1 + 3*t*(1-t)^2*P2 + 3*t^2*(1-t)*P3 + t^3*P4
  86. (P1 and P4 are the end points, P2 and P3 the control points.)
  87. For both formulae, t is a real number in the range [0..1].
  88. Note that the rasterizer does not use these formulae directly.
  89. They exhibit, however, one very useful property of Bézier arcs:
  90. Each point of the curve is a weighted average of the control
  91. points.
  92. As all weights are positive and always sum up to 1, whatever the
  93. value of t, each arc point lies within the triangle (polygon)
  94. defined by the arc's three (four) control points.
  95. In the following, only second-order curves are discussed since
  96. rasterization of third-order curves is completely identical.
  97. Here some samples for second-order curves.
  98. * # on curve
  99. * off curve
  100. __---__
  101. #-__ _-- -_
  102. --__ _- -
  103. --__ # \
  104. --__ #
  105. -#
  106. Two `on' points
  107. Two `on' points and one `off' point
  108. between them
  109. *
  110. # __ Two `on' points with two `off'
  111. \ - - points between them. The point
  112. \ / \ marked `0' is the middle of the
  113. - 0 \ `off' points, and is a `virtual
  114. -_ _- # on' point where the curve passes.
  115. -- It does not appear in the point
  116. * list.
  117. 2. Profiles and Spans
  118. ---------------------
  119. The following is a basic explanation of the _kind_ of computations
  120. made by the rasterizer to build a bitmap from a vector
  121. representation. Note that the actual implementation is slightly
  122. different, due to performance tuning and other factors.
  123. However, the following ideas remain in the same category, and are
  124. more convenient to understand.
  125. a. Sweeping the Shape
  126. The best way to fill a shape is to decompose it into a number of
  127. simple horizontal segments, then turn them on in the target
  128. bitmap. These segments are called `spans'.
  129. __---__
  130. _-- -_
  131. _- -
  132. - \
  133. / \
  134. / \
  135. | \
  136. __---__ Example: filling a shape
  137. _----------_ with spans.
  138. _--------------
  139. ----------------\
  140. /-----------------\ This is typically done from the top
  141. / \ to the bottom of the shape, in a
  142. | | \ movement called a `sweep'.
  143. V
  144. __---__
  145. _----------_
  146. _--------------
  147. ----------------\
  148. /-----------------\
  149. /-------------------\
  150. |---------------------\
  151. In order to draw a span, the rasterizer must compute its
  152. coordinates, which are simply the x coordinates of the shape's
  153. contours, taken on the y scanlines.
  154. /---/ |---| Note that there are usually
  155. /---/ |---| several spans per scanline.
  156. | /---/ |---|
  157. | /---/_______|---| When rendering this shape to the
  158. V /----------------| current scanline y, we must
  159. /-----------------| compute the x values of the
  160. a /----| |---| points a, b, c, and d.
  161. - - - * * - - - - * * - - y -
  162. / / b c| |d
  163. /---/ |---|
  164. /---/ |---| And then turn on the spans a-b
  165. /---/ |---| and c-d.
  166. /---/_______|---|
  167. /----------------|
  168. /-----------------|
  169. a /----| |---|
  170. - - - ####### - - - - ##### - - y -
  171. / / b c| |d
  172. b. Decomposing Outlines into Profiles
  173. For each scanline during the sweep, we need the following
  174. information:
  175. o The number of spans on the current scanline, given by the
  176. number of shape points intersecting the scanline (these are
  177. the points a, b, c, and d in the above example).
  178. o The x coordinates of these points.
  179. x coordinates are computed before the sweep, in a phase called
  180. `decomposition' which converts the glyph into *profiles*.
  181. Put it simply, a `profile' is a contour's portion that can only
  182. be either ascending or descending, i.e., it is monotonic in the
  183. vertical direction (we also say y-monotonic). There is no such
  184. thing as a horizontal profile, as we shall see.
  185. Here are a few examples:
  186. this square
  187. 1 2
  188. ---->---- is made of two
  189. | | | |
  190. | | profiles | |
  191. ^ v ^ + v
  192. | | | |
  193. | | | |
  194. ----<----
  195. up down
  196. this triangle
  197. P2 1 2
  198. |\ is made of two | \
  199. ^ | \ \ | \
  200. | | \ \ profiles | \ |
  201. | | \ v ^ | \ |
  202. | \ | | + \ v
  203. | \ | | \
  204. P1 ---___ \ ---___ \
  205. ---_\ ---_ \
  206. <--__ P3 up down
  207. A more general contour can be made of more than two profiles:
  208. __ ^
  209. / | / ___ / |
  210. / | / | / | / |
  211. | | / / => | v / /
  212. | | | | | | ^ |
  213. ^ | |___| | | ^ + | + | + v
  214. | | | v | |
  215. | | | up |
  216. |___________| | down |
  217. <-- up down
  218. Successive profiles are always joined by horizontal segments
  219. that are not part of the profiles themselves.
  220. For the rasterizer, a profile is simply an *array* that
  221. associates one horizontal *pixel* coordinate to each bitmap
  222. *scanline* crossed by the contour's section containing the
  223. profile. Note that profiles are *oriented* up or down along the
  224. glyph's original flow orientation.
  225. In other graphics libraries, profiles are also called `edges' or
  226. `edgelists'.
  227. c. The Render Pool
  228. FreeType has been designed to be able to run well on _very_
  229. light systems, including embedded systems with very few memory.
  230. A render pool will be allocated once; the rasterizer uses this
  231. pool for all its needs by managing this memory directly in it.
  232. The algorithms that are used for profile computation make it
  233. possible to use the pool as a simple growing heap. This means
  234. that this memory management is actually quite easy and faster
  235. than any kind of malloc()/free() combination.
  236. Moreover, we'll see later that the rasterizer is able, when
  237. dealing with profiles too large and numerous to lie all at once
  238. in the render pool, to immediately decompose recursively the
  239. rendering process into independent sub-tasks, each taking less
  240. memory to be performed (see `sub-banding' below).
  241. The render pool doesn't need to be large. A 4KByte pool is
  242. enough for nearly all renditions, though nearly 100% slower than
  243. a more comfortable 16KByte or 32KByte pool (that was tested with
  244. complex glyphs at sizes over 500 pixels).
  245. d. Computing Profiles Extents
  246. Remember that a profile is an array, associating a _scanline_ to
  247. the x pixel coordinate of its intersection with a contour.
  248. Though it's not exactly how the FreeType rasterizer works, it is
  249. convenient to think that we need a profile's height before
  250. allocating it in the pool and computing its coordinates.
  251. The profile's height is the number of scanlines crossed by the
  252. y-monotonic section of a contour. We thus need to compute these
  253. sections from the vectorial description. In order to do that,
  254. we are obliged to compute all (local and global) y extrema of
  255. the glyph (minima and maxima).
  256. P2 For instance, this triangle has only
  257. two y-extrema, which are simply
  258. |\
  259. | \ P2.y as a vertical maximum
  260. | \ P3.y as a vertical minimum
  261. | \
  262. | \ P1.y is not a vertical extremum (though
  263. | \ it is a horizontal minimum, which we
  264. P1 ---___ \ don't need).
  265. ---_\
  266. P3
  267. Note that the extrema are expressed in pixel units, not in
  268. scanlines. The triangle's height is certainly (P3.y-P2.y+1)
  269. pixel units, but its profiles' heights are computed in
  270. scanlines. The exact conversion is simple:
  271. - min scanline = FLOOR ( min y )
  272. - max scanline = CEILING( max y )
  273. A problem arises with Bézier Arcs. While a segment is always
  274. necessarily y-monotonic (i.e., flat, ascending, or descending),
  275. which makes extrema computations easy, the ascent of an arc can
  276. vary between its control points.
  277. P2
  278. *
  279. # on curve
  280. * off curve
  281. __-x--_
  282. _-- -_
  283. P1 _- - A non y-monotonic Bézier arc.
  284. # \
  285. - The arc goes from P1 to P3.
  286. \
  287. \ P3
  288. #
  289. We first need to be able to easily detect non-monotonic arcs,
  290. according to their control points. I will state here, without
  291. proof, that the monotony condition can be expressed as:
  292. P1.y <= P2.y <= P3.y for an ever-ascending arc
  293. P1.y >= P2.y >= P3.y for an ever-descending arc
  294. with the special case of
  295. P1.y = P2.y = P3.y where the arc is said to be `flat'.
  296. As you can see, these conditions can be very easily tested.
  297. They are, however, extremely important, as any arc that does not
  298. satisfy them necessarily contains an extremum.
  299. Note also that a monotonic arc can contain an extremum too,
  300. which is then one of its `on' points:
  301. P1 P2
  302. #---__ * P1P2P3 is ever-descending, but P1
  303. -_ is an y-extremum.
  304. -
  305. ---_ \
  306. -> \
  307. \ P3
  308. #
  309. Let's go back to our previous example:
  310. P2
  311. *
  312. # on curve
  313. * off curve
  314. __-x--_
  315. _-- -_
  316. P1 _- - A non-y-monotonic Bézier arc.
  317. # \
  318. - Here we have
  319. \ P2.y >= P1.y &&
  320. \ P3 P2.y >= P3.y (!)
  321. #
  322. We need to compute the vertical maximum of this arc to be able
  323. to compute a profile's height (the point marked by an `x'). The
  324. arc's equation indicates that a direct computation is possible,
  325. but we rely on a different technique, which use will become
  326. apparent soon.
  327. Bézier arcs have the special property of being very easily
  328. decomposed into two sub-arcs, which are themselves Bézier arcs.
  329. Moreover, it is easy to prove that there is at most one vertical
  330. extremum on each Bézier arc (for second-degree curves; similar
  331. conditions can be found for third-order arcs).
  332. For instance, the following arc P1P2P3 can be decomposed into
  333. two sub-arcs Q1Q2Q3 and R1R2R3:
  334. P2
  335. *
  336. # on curve
  337. * off curve
  338. original Bézier arc P1P2P3.
  339. __---__
  340. _-- --_
  341. _- -_
  342. - -
  343. / \
  344. / \
  345. # #
  346. P1 P3
  347. P2
  348. *
  349. Q3 Decomposed into two subarcs
  350. Q2 R2 Q1Q2Q3 and R1R2R3
  351. * __-#-__ *
  352. _-- --_
  353. _- R1 -_ Q1 = P1 R3 = P3
  354. - - Q2 = (P1+P2)/2 R2 = (P2+P3)/2
  355. / \
  356. / \ Q3 = R1 = (Q2+R2)/2
  357. # #
  358. Q1 R3 Note that Q2, R2, and Q3=R1
  359. are on a single line which is
  360. tangent to the curve.
  361. We have then decomposed a non-y-monotonic Bézier curve into two
  362. smaller sub-arcs. Note that in the above drawing, both sub-arcs
  363. are monotonic, and that the extremum is then Q3=R1. However, in
  364. a more general case, only one sub-arc is guaranteed to be
  365. monotonic. Getting back to our former example:
  366. Q2
  367. *
  368. __-x--_ R1
  369. _-- #_
  370. Q1 _- Q3 - R2
  371. # \ *
  372. -
  373. \
  374. \ R3
  375. #
  376. Here, we see that, though Q1Q2Q3 is still non-monotonic, R1R2R3
  377. is ever descending: We thus know that it doesn't contain the
  378. extremum. We can then re-subdivide Q1Q2Q3 into two sub-arcs and
  379. go on recursively, stopping when we encounter two monotonic
  380. subarcs, or when the subarcs become simply too small.
  381. We will finally find the vertical extremum. Note that the
  382. iterative process of finding an extremum is called `flattening'.
  383. e. Computing Profiles Coordinates
  384. Once we have the height of each profile, we are able to allocate
  385. it in the render pool. The next task is to compute coordinates
  386. for each scanline.
  387. In the case of segments, the computation is straightforward,
  388. using the Euclidean algorithm (also known as Bresenham).
  389. However, for Bézier arcs, the job is a little more complicated.
  390. We assume that all Béziers that are part of a profile are the
  391. result of flattening the curve, which means that they are all
  392. y-monotonic (ascending or descending, and never flat). We now
  393. have to compute the intersections of arcs with the profile's
  394. scanlines. One way is to use a similar scheme to flattening
  395. called `stepping'.
  396. Consider this arc, going from P1 to
  397. --------------------- P3. Suppose that we need to
  398. compute its intersections with the
  399. drawn scanlines. As already
  400. --------------------- mentioned this can be done
  401. directly, but the involved
  402. * P2 _---# P3 algorithm is far too slow.
  403. ------------- _-- --
  404. _-
  405. _/ Instead, it is still possible to
  406. ---------/----------- use the decomposition property in
  407. / the same recursive way, i.e.,
  408. | subdivide the arc into subarcs
  409. ------|-------------- until these get too small to cross
  410. | more than one scanline!
  411. |
  412. -----|--------------- This is very easily done using a
  413. | rasterizer-managed stack of
  414. | subarcs.
  415. # P1
  416. f. Sweeping and Sorting the Spans
  417. Once all our profiles have been computed, we begin the sweep to
  418. build (and fill) the spans.
  419. As both the TrueType and Type 1 specifications use the winding
  420. fill rule (but with opposite directions), we place, on each
  421. scanline, the present profiles in two separate lists.
  422. One list, called the `left' one, only contains ascending
  423. profiles, while the other `right' list contains the descending
  424. profiles.
  425. As each glyph is made of closed curves, a simple geometric
  426. property ensures that the two lists contain the same number of
  427. elements.
  428. Creating spans is thus straightforward:
  429. 1. We sort each list in increasing horizontal order.
  430. 2. We pair each value of the left list with its corresponding
  431. value in the right list.
  432. / / | | For example, we have here
  433. / / | | four profiles. Two of
  434. >/ / | | | them are ascending (1 &
  435. 1// / ^ | | | 2 3), while the two others
  436. // // 3| | | v are descending (2 & 4).
  437. / //4 | | | On the given scanline,
  438. a / /< | | the left list is (1,3),
  439. - - - *-----* - - - - *---* - - y - and the right one is
  440. / / b c| |d (4,2) (sorted).
  441. There are then two spans, joining
  442. 1 to 4 (i.e. a-b) and 3 to 2
  443. (i.e. c-d)!
  444. Sorting doesn't necessarily take much time, as in 99 cases out
  445. of 100, the lists' order is kept from one scanline to the next.
  446. We can thus implement it with two simple singly-linked lists,
  447. sorted by a classic bubble-sort, which takes a minimum amount of
  448. time when the lists are already sorted.
  449. A previous version of the rasterizer used more elaborate
  450. structures, like arrays to perform `faster' sorting. It turned
  451. out that this old scheme is not faster than the one described
  452. above.
  453. Once the spans have been `created', we can simply draw them in
  454. the target bitmap.
  455. ------------------------------------------------------------------------
  456. Copyright (C) 2003-2021 by
  457. David Turner, Robert Wilhelm, and Werner Lemberg.
  458. This file is part of the FreeType project, and may only be used,
  459. modified, and distributed under the terms of the FreeType project
  460. license, LICENSE.TXT. By continuing to use, modify, or distribute this
  461. file you indicate that you have read the license and understand and
  462. accept it fully.
  463. --- end of raster.txt ---
  464. Local Variables:
  465. coding: utf-8
  466. End: