ftgrays.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159
  1. /***************************************************************************/
  2. /* */
  3. /* ftgrays.c */
  4. /* */
  5. /* A new `perfect' anti-aliasing renderer (body). */
  6. /* */
  7. /* Copyright 2000-2001, 2002 by */
  8. /* David Turner, Robert Wilhelm, and Werner Lemberg. */
  9. /* */
  10. /* This file is part of the FreeType project, and may only be used, */
  11. /* modified, and distributed under the terms of the FreeType project */
  12. /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
  13. /* this file you indicate that you have read the license and */
  14. /* understand and accept it fully. */
  15. /* */
  16. /***************************************************************************/
  17. /*************************************************************************/
  18. /* */
  19. /* This file can be compiled without the rest of the FreeType engine, by */
  20. /* defining the _STANDALONE_ macro when compiling it. You also need to */
  21. /* put the files `ftgrays.h' and `ftimage.h' into the current */
  22. /* compilation directory. Typically, you could do something like */
  23. /* */
  24. /* - copy `src/smooth/ftgrays.c' (this file) to your current directory */
  25. /* */
  26. /* - copy `include/freetype/ftimage.h' and `src/smooth/ftgrays.h' to the */
  27. /* same directory */
  28. /* */
  29. /* - compile `ftgrays' with the _STANDALONE_ macro defined, as in */
  30. /* */
  31. /* cc -c -D_STANDALONE_ ftgrays.c */
  32. /* */
  33. /* The renderer can be initialized with a call to */
  34. /* `ft_gray_raster.raster_new'; an anti-aliased bitmap can be generated */
  35. /* with a call to `ft_gray_raster.raster_render'. */
  36. /* */
  37. /* See the comments and documentation in the file `ftimage.h' for more */
  38. /* details on how the raster works. */
  39. /* */
  40. /*************************************************************************/
  41. /*************************************************************************/
  42. /* */
  43. /* This is a new anti-aliasing scan-converter for FreeType 2. The */
  44. /* algorithm used here is _very_ different from the one in the standard */
  45. /* `ftraster' module. Actually, `ftgrays' computes the _exact_ */
  46. /* coverage of the outline on each pixel cell. */
  47. /* */
  48. /* It is based on ideas that I initially found in Raph Levien's */
  49. /* excellent LibArt graphics library (see http://www.levien.com/libart */
  50. /* for more information, though the web pages do not tell anything */
  51. /* about the renderer; you'll have to dive into the source code to */
  52. /* understand how it works). */
  53. /* */
  54. /* Note, however, that this is a _very_ different implementation */
  55. /* compared to Raph's. Coverage information is stored in a very */
  56. /* different way, and I don't use sorted vector paths. Also, it doesn't */
  57. /* use floating point values. */
  58. /* */
  59. /* This renderer has the following advantages: */
  60. /* */
  61. /* - It doesn't need an intermediate bitmap. Instead, one can supply a */
  62. /* callback function that will be called by the renderer to draw gray */
  63. /* spans on any target surface. You can thus do direct composition on */
  64. /* any kind of bitmap, provided that you give the renderer the right */
  65. /* callback. */
  66. /* */
  67. /* - A perfect anti-aliaser, i.e., it computes the _exact_ coverage on */
  68. /* each pixel cell. */
  69. /* */
  70. /* - It performs a single pass on the outline (the `standard' FT2 */
  71. /* renderer makes two passes). */
  72. /* */
  73. /* - It can easily be modified to render to _any_ number of gray levels */
  74. /* cheaply. */
  75. /* */
  76. /* - For small (< 20) pixel sizes, it is faster than the standard */
  77. /* renderer. */
  78. /* */
  79. /*************************************************************************/
  80. /* experimental support for gamma correction within the rasterizer */
  81. #define xxxGRAYS_USE_GAMMA
  82. /*************************************************************************/
  83. /* */
  84. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  85. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  86. /* messages during execution. */
  87. /* */
  88. #undef FT_COMPONENT
  89. #define FT_COMPONENT trace_smooth
  90. #define ErrRaster_MemoryOverflow -4
  91. #ifdef _STANDALONE_
  92. #include <string.h> /* for ft_memcpy() */
  93. #include <setjmp.h>
  94. #include <limits.h>
  95. #define FT_UINT_MAX UINT_MAX
  96. #define ft_memset memset
  97. #define ft_setjmp setjmp
  98. #define ft_longjmp longjmp
  99. #define ft_jmp_buf jmp_buf
  100. #define ErrRaster_Invalid_Mode -2
  101. #define ErrRaster_Invalid_Outline -1
  102. #define FT_BEGIN_HEADER
  103. #define FT_END_HEADER
  104. #include "ftimage.h"
  105. #include "ftgrays.h"
  106. /* This macro is used to indicate that a function parameter is unused. */
  107. /* Its purpose is simply to reduce compiler warnings. Note also that */
  108. /* simply defining it as `(void)x' doesn't avoid warnings with certain */
  109. /* ANSI compilers (e.g. LCC). */
  110. #define FT_UNUSED( x ) (x) = (x)
  111. /* Disable the tracing mechanism for simplicity -- developers can */
  112. /* activate it easily by redefining these two macros. */
  113. #ifndef FT_ERROR
  114. #define FT_ERROR( x ) do ; while ( 0 ) /* nothing */
  115. #endif
  116. #ifndef FT_TRACE
  117. #define FT_TRACE( x ) do ; while ( 0 ) /* nothing */
  118. #endif
  119. #else /* _STANDALONE_ */
  120. #include <ft2build.h>
  121. #include "ftgrays.h"
  122. #include FT_INTERNAL_OBJECTS_H
  123. #include FT_INTERNAL_DEBUG_H
  124. #include FT_OUTLINE_H
  125. #include "ftsmerrs.h"
  126. #define ErrRaster_Invalid_Mode Smooth_Err_Cannot_Render_Glyph
  127. #define ErrRaster_Invalid_Outline Smooth_Err_Invalid_Outline
  128. #endif /* _STANDALONE_ */
  129. #ifndef FT_MEM_SET
  130. #define FT_MEM_SET( d, s, c ) ft_memset( d, s, c )
  131. #endif
  132. #ifndef FT_MEM_ZERO
  133. #define FT_MEM_ZERO( dest, count ) FT_MEM_SET( dest, 0, count )
  134. #endif
  135. /* define this to dump debugging information */
  136. #define xxxDEBUG_GRAYS
  137. /* as usual, for the speed hungry :-) */
  138. #ifndef FT_STATIC_RASTER
  139. #define RAS_ARG PRaster raster
  140. #define RAS_ARG_ PRaster raster,
  141. #define RAS_VAR raster
  142. #define RAS_VAR_ raster,
  143. #define ras (*raster)
  144. #else /* FT_STATIC_RASTER */
  145. #define RAS_ARG /* empty */
  146. #define RAS_ARG_ /* empty */
  147. #define RAS_VAR /* empty */
  148. #define RAS_VAR_ /* empty */
  149. static TRaster ras;
  150. #endif /* FT_STATIC_RASTER */
  151. /* must be at least 6 bits! */
  152. #define PIXEL_BITS 8
  153. #define ONE_PIXEL ( 1L << PIXEL_BITS )
  154. #define PIXEL_MASK ( -1L << PIXEL_BITS )
  155. #define TRUNC( x ) ( (TCoord)((x) >> PIXEL_BITS) )
  156. #define SUBPIXELS( x ) ( (TPos)(x) << PIXEL_BITS )
  157. #define FLOOR( x ) ( (x) & -ONE_PIXEL )
  158. #define CEILING( x ) ( ( (x) + ONE_PIXEL - 1 ) & -ONE_PIXEL )
  159. #define ROUND( x ) ( ( (x) + ONE_PIXEL / 2 ) & -ONE_PIXEL )
  160. #if PIXEL_BITS >= 6
  161. #define UPSCALE( x ) ( (x) << ( PIXEL_BITS - 6 ) )
  162. #define DOWNSCALE( x ) ( (x) >> ( PIXEL_BITS - 6 ) )
  163. #else
  164. #define UPSCALE( x ) ( (x) >> ( 6 - PIXEL_BITS ) )
  165. #define DOWNSCALE( x ) ( (x) << ( 6 - PIXEL_BITS ) )
  166. #endif
  167. /* Define this if you want to use a more compact storage scheme. This */
  168. /* increases the number of cells available in the render pool but slows */
  169. /* down the rendering a bit. It is useful if you have a really tiny */
  170. /* render pool. */
  171. #undef GRAYS_COMPACT
  172. /*************************************************************************/
  173. /* */
  174. /* TYPE DEFINITIONS */
  175. /* */
  176. /* don't change the following types to FT_Int or FT_Pos, since we might */
  177. /* need to define them to "float" or "double" when experimenting with */
  178. /* new algorithms */
  179. typedef int TCoord; /* integer scanline/pixel coordinate */
  180. typedef long TPos; /* sub-pixel coordinate */
  181. /* determine the type used to store cell areas. This normally takes at */
  182. /* least PIXEL_BYTES*2 + 1. On 16-bit systems, we need to use `long' */
  183. /* instead of `int', otherwise bad things happen */
  184. #if PIXEL_BITS <= 7
  185. typedef int TArea;
  186. #else /* PIXEL_BITS >= 8 */
  187. /* approximately determine the size of integers using an ANSI-C header */
  188. #if FT_UINT_MAX == 0xFFFFU
  189. typedef long TArea;
  190. #else
  191. typedef int TArea;
  192. #endif
  193. #endif /* PIXEL_BITS >= 8 */
  194. /* maximal number of gray spans in a call to the span callback */
  195. #define FT_MAX_GRAY_SPANS 32
  196. #ifdef GRAYS_COMPACT
  197. typedef struct TCell_
  198. {
  199. short x : 14;
  200. short y : 14;
  201. int cover : PIXEL_BITS + 2;
  202. int area : PIXEL_BITS * 2 + 2;
  203. } TCell, *PCell;
  204. #else /* GRAYS_COMPACT */
  205. typedef struct TCell_
  206. {
  207. TCoord x;
  208. TCoord y;
  209. int cover;
  210. TArea area;
  211. } TCell, *PCell;
  212. #endif /* GRAYS_COMPACT */
  213. typedef struct TRaster_
  214. {
  215. PCell cells;
  216. int max_cells;
  217. int num_cells;
  218. TPos min_ex, max_ex;
  219. TPos min_ey, max_ey;
  220. TArea area;
  221. int cover;
  222. int invalid;
  223. TCoord ex, ey;
  224. TCoord cx, cy;
  225. TPos x, y;
  226. TPos last_ey;
  227. FT_Vector bez_stack[32 * 3 + 1];
  228. int lev_stack[32];
  229. FT_Outline outline;
  230. FT_Bitmap target;
  231. FT_BBox clip_box;
  232. FT_Span gray_spans[FT_MAX_GRAY_SPANS];
  233. int num_gray_spans;
  234. FT_Raster_Span_Func render_span;
  235. void* render_span_data;
  236. int span_y;
  237. int band_size;
  238. int band_shoot;
  239. int conic_level;
  240. int cubic_level;
  241. void* memory;
  242. ft_jmp_buf jump_buffer;
  243. #ifdef GRAYS_USE_GAMMA
  244. unsigned char gamma[257];
  245. #endif
  246. } TRaster, *PRaster;
  247. /*************************************************************************/
  248. /* */
  249. /* Initialize the cells table. */
  250. /* */
  251. static void
  252. gray_init_cells( RAS_ARG_ void* buffer,
  253. long byte_size )
  254. {
  255. ras.cells = (PCell)buffer;
  256. ras.max_cells = byte_size / sizeof ( TCell );
  257. ras.num_cells = 0;
  258. ras.area = 0;
  259. ras.cover = 0;
  260. ras.invalid = 1;
  261. }
  262. /*************************************************************************/
  263. /* */
  264. /* Compute the outline bounding box. */
  265. /* */
  266. static void
  267. gray_compute_cbox( RAS_ARG )
  268. {
  269. FT_Outline* outline = &ras.outline;
  270. FT_Vector* vec = outline->points;
  271. FT_Vector* limit = vec + outline->n_points;
  272. if ( outline->n_points <= 0 )
  273. {
  274. ras.min_ex = ras.max_ex = 0;
  275. ras.min_ey = ras.max_ey = 0;
  276. return;
  277. }
  278. ras.min_ex = ras.max_ex = vec->x;
  279. ras.min_ey = ras.max_ey = vec->y;
  280. vec++;
  281. for ( ; vec < limit; vec++ )
  282. {
  283. TPos x = vec->x;
  284. TPos y = vec->y;
  285. if ( x < ras.min_ex ) ras.min_ex = x;
  286. if ( x > ras.max_ex ) ras.max_ex = x;
  287. if ( y < ras.min_ey ) ras.min_ey = y;
  288. if ( y > ras.max_ey ) ras.max_ey = y;
  289. }
  290. /* truncate the bounding box to integer pixels */
  291. ras.min_ex = ras.min_ex >> 6;
  292. ras.min_ey = ras.min_ey >> 6;
  293. ras.max_ex = ( ras.max_ex + 63 ) >> 6;
  294. ras.max_ey = ( ras.max_ey + 63 ) >> 6;
  295. }
  296. /*************************************************************************/
  297. /* */
  298. /* Record the current cell in the table. */
  299. /* */
  300. static void
  301. gray_record_cell( RAS_ARG )
  302. {
  303. PCell cell;
  304. if ( !ras.invalid && ( ras.area | ras.cover ) )
  305. {
  306. if ( ras.num_cells >= ras.max_cells )
  307. ft_longjmp( ras.jump_buffer, 1 );
  308. cell = ras.cells + ras.num_cells++;
  309. cell->x = (TCoord)(ras.ex - ras.min_ex);
  310. cell->y = (TCoord)(ras.ey - ras.min_ey);
  311. cell->area = ras.area;
  312. cell->cover = ras.cover;
  313. }
  314. }
  315. /*************************************************************************/
  316. /* */
  317. /* Set the current cell to a new position. */
  318. /* */
  319. static void
  320. gray_set_cell( RAS_ARG_ TCoord ex,
  321. TCoord ey )
  322. {
  323. int invalid, record, clean;
  324. /* Move the cell pointer to a new position. We set the `invalid' */
  325. /* flag to indicate that the cell isn't part of those we're interested */
  326. /* in during the render phase. This means that: */
  327. /* */
  328. /* . the new vertical position must be within min_ey..max_ey-1. */
  329. /* . the new horizontal position must be strictly less than max_ex */
  330. /* */
  331. /* Note that if a cell is to the left of the clipping region, it is */
  332. /* actually set to the (min_ex-1) horizontal position. */
  333. record = 0;
  334. clean = 1;
  335. invalid = ( ey < ras.min_ey || ey >= ras.max_ey || ex >= ras.max_ex );
  336. if ( !invalid )
  337. {
  338. /* All cells that are on the left of the clipping region go to the */
  339. /* min_ex - 1 horizontal position. */
  340. if ( ex < ras.min_ex )
  341. ex = (TCoord)(ras.min_ex - 1);
  342. /* if our position is new, then record the previous cell */
  343. if ( ex != ras.ex || ey != ras.ey )
  344. record = 1;
  345. else
  346. clean = ras.invalid; /* do not clean if we didn't move from */
  347. /* a valid cell */
  348. }
  349. /* record the previous cell if needed (i.e., if we changed the cell */
  350. /* position, of changed the `invalid' flag) */
  351. if ( ras.invalid != invalid || record )
  352. gray_record_cell( RAS_VAR );
  353. if ( clean )
  354. {
  355. ras.area = 0;
  356. ras.cover = 0;
  357. }
  358. ras.invalid = invalid;
  359. ras.ex = ex;
  360. ras.ey = ey;
  361. }
  362. /*************************************************************************/
  363. /* */
  364. /* Start a new contour at a given cell. */
  365. /* */
  366. static void
  367. gray_start_cell( RAS_ARG_ TCoord ex,
  368. TCoord ey )
  369. {
  370. if ( ex < ras.min_ex )
  371. ex = (TCoord)(ras.min_ex - 1);
  372. ras.area = 0;
  373. ras.cover = 0;
  374. ras.ex = ex;
  375. ras.ey = ey;
  376. ras.last_ey = SUBPIXELS( ey );
  377. ras.invalid = 0;
  378. gray_set_cell( RAS_VAR_ ex, ey );
  379. }
  380. /*************************************************************************/
  381. /* */
  382. /* Render a scanline as one or more cells. */
  383. /* */
  384. static void
  385. gray_render_scanline( RAS_ARG_ TCoord ey,
  386. TPos x1,
  387. TCoord y1,
  388. TPos x2,
  389. TCoord y2 )
  390. {
  391. TCoord ex1, ex2, fx1, fx2, delta;
  392. long p, first, dx;
  393. int incr, lift, mod, rem;
  394. dx = x2 - x1;
  395. ex1 = TRUNC( x1 ); /* if (ex1 >= ras.max_ex) ex1 = ras.max_ex-1; */
  396. ex2 = TRUNC( x2 ); /* if (ex2 >= ras.max_ex) ex2 = ras.max_ex-1; */
  397. fx1 = (TCoord)( x1 - SUBPIXELS( ex1 ) );
  398. fx2 = (TCoord)( x2 - SUBPIXELS( ex2 ) );
  399. /* trivial case. Happens often */
  400. if ( y1 == y2 )
  401. {
  402. gray_set_cell( RAS_VAR_ ex2, ey );
  403. return;
  404. }
  405. /* everything is located in a single cell. That is easy! */
  406. /* */
  407. if ( ex1 == ex2 )
  408. {
  409. delta = y2 - y1;
  410. ras.area += (TArea)( fx1 + fx2 ) * delta;
  411. ras.cover += delta;
  412. return;
  413. }
  414. /* ok, we'll have to render a run of adjacent cells on the same */
  415. /* scanline... */
  416. /* */
  417. p = ( ONE_PIXEL - fx1 ) * ( y2 - y1 );
  418. first = ONE_PIXEL;
  419. incr = 1;
  420. if ( dx < 0 )
  421. {
  422. p = fx1 * ( y2 - y1 );
  423. first = 0;
  424. incr = -1;
  425. dx = -dx;
  426. }
  427. delta = (TCoord)( p / dx );
  428. mod = (TCoord)( p % dx );
  429. if ( mod < 0 )
  430. {
  431. delta--;
  432. mod += (TCoord)dx;
  433. }
  434. ras.area += (TArea)( fx1 + first ) * delta;
  435. ras.cover += delta;
  436. ex1 += incr;
  437. gray_set_cell( RAS_VAR_ ex1, ey );
  438. y1 += delta;
  439. if ( ex1 != ex2 )
  440. {
  441. p = ONE_PIXEL * ( y2 - y1 + delta );
  442. lift = (TCoord)( p / dx );
  443. rem = (TCoord)( p % dx );
  444. if ( rem < 0 )
  445. {
  446. lift--;
  447. rem += (TCoord)dx;
  448. }
  449. mod -= dx;
  450. while ( ex1 != ex2 )
  451. {
  452. delta = lift;
  453. mod += rem;
  454. if ( mod >= 0 )
  455. {
  456. mod -= (TCoord)dx;
  457. delta++;
  458. }
  459. ras.area += (TArea)ONE_PIXEL * delta;
  460. ras.cover += delta;
  461. y1 += delta;
  462. ex1 += incr;
  463. gray_set_cell( RAS_VAR_ ex1, ey );
  464. }
  465. }
  466. delta = y2 - y1;
  467. ras.area += (TArea)( fx2 + ONE_PIXEL - first ) * delta;
  468. ras.cover += delta;
  469. }
  470. /*************************************************************************/
  471. /* */
  472. /* Render a given line as a series of scanlines. */
  473. /* */
  474. static void
  475. gray_render_line( RAS_ARG_ TPos to_x,
  476. TPos to_y )
  477. {
  478. TCoord ey1, ey2, fy1, fy2;
  479. TPos dx, dy, x, x2;
  480. long p, first;
  481. int delta, rem, mod, lift, incr;
  482. ey1 = TRUNC( ras.last_ey );
  483. ey2 = TRUNC( to_y ); /* if (ey2 >= ras.max_ey) ey2 = ras.max_ey-1; */
  484. fy1 = (TCoord)( ras.y - ras.last_ey );
  485. fy2 = (TCoord)( to_y - SUBPIXELS( ey2 ) );
  486. dx = to_x - ras.x;
  487. dy = to_y - ras.y;
  488. /* XXX: we should do something about the trivial case where dx == 0, */
  489. /* as it happens very often! */
  490. /* perform vertical clipping */
  491. {
  492. TCoord min, max;
  493. min = ey1;
  494. max = ey2;
  495. if ( ey1 > ey2 )
  496. {
  497. min = ey2;
  498. max = ey1;
  499. }
  500. if ( min >= ras.max_ey || max < ras.min_ey )
  501. goto End;
  502. }
  503. /* everything is on a single scanline */
  504. if ( ey1 == ey2 )
  505. {
  506. gray_render_scanline( RAS_VAR_ ey1, ras.x, fy1, to_x, fy2 );
  507. goto End;
  508. }
  509. /* vertical line - avoid calling gray_render_scanline */
  510. incr = 1;
  511. if ( dx == 0 )
  512. {
  513. TCoord ex = TRUNC( ras.x );
  514. TCoord two_fx = (TCoord)( ( ras.x - SUBPIXELS( ex ) ) << 1 );
  515. TPos area;
  516. first = ONE_PIXEL;
  517. if ( dy < 0 )
  518. {
  519. first = 0;
  520. incr = -1;
  521. }
  522. delta = (int)( first - fy1 );
  523. ras.area += (TArea)two_fx * delta;
  524. ras.cover += delta;
  525. ey1 += incr;
  526. gray_set_cell( raster, ex, ey1 );
  527. delta = (int)( first + first - ONE_PIXEL );
  528. area = (TArea)two_fx * delta;
  529. while ( ey1 != ey2 )
  530. {
  531. ras.area += area;
  532. ras.cover += delta;
  533. ey1 += incr;
  534. gray_set_cell( raster, ex, ey1 );
  535. }
  536. delta = (int)( fy2 - ONE_PIXEL + first );
  537. ras.area += (TArea)two_fx * delta;
  538. ras.cover += delta;
  539. goto End;
  540. }
  541. /* ok, we have to render several scanlines */
  542. p = ( ONE_PIXEL - fy1 ) * dx;
  543. first = ONE_PIXEL;
  544. incr = 1;
  545. if ( dy < 0 )
  546. {
  547. p = fy1 * dx;
  548. first = 0;
  549. incr = -1;
  550. dy = -dy;
  551. }
  552. delta = (int)( p / dy );
  553. mod = (int)( p % dy );
  554. if ( mod < 0 )
  555. {
  556. delta--;
  557. mod += (TCoord)dy;
  558. }
  559. x = ras.x + delta;
  560. gray_render_scanline( RAS_VAR_ ey1, ras.x, fy1, x, (TCoord)first );
  561. ey1 += incr;
  562. gray_set_cell( RAS_VAR_ TRUNC( x ), ey1 );
  563. if ( ey1 != ey2 )
  564. {
  565. p = ONE_PIXEL * dx;
  566. lift = (int)( p / dy );
  567. rem = (int)( p % dy );
  568. if ( rem < 0 )
  569. {
  570. lift--;
  571. rem += (int)dy;
  572. }
  573. mod -= (int)dy;
  574. while ( ey1 != ey2 )
  575. {
  576. delta = lift;
  577. mod += rem;
  578. if ( mod >= 0 )
  579. {
  580. mod -= (int)dy;
  581. delta++;
  582. }
  583. x2 = x + delta;
  584. gray_render_scanline( RAS_VAR_ ey1, x,
  585. (TCoord)( ONE_PIXEL - first ), x2,
  586. (TCoord)first );
  587. x = x2;
  588. ey1 += incr;
  589. gray_set_cell( RAS_VAR_ TRUNC( x ), ey1 );
  590. }
  591. }
  592. gray_render_scanline( RAS_VAR_ ey1, x,
  593. (TCoord)( ONE_PIXEL - first ), to_x,
  594. fy2 );
  595. End:
  596. ras.x = to_x;
  597. ras.y = to_y;
  598. ras.last_ey = SUBPIXELS( ey2 );
  599. }
  600. static void
  601. gray_split_conic( FT_Vector* base )
  602. {
  603. TPos a, b;
  604. base[4].x = base[2].x;
  605. b = base[1].x;
  606. a = base[3].x = ( base[2].x + b ) / 2;
  607. b = base[1].x = ( base[0].x + b ) / 2;
  608. base[2].x = ( a + b ) / 2;
  609. base[4].y = base[2].y;
  610. b = base[1].y;
  611. a = base[3].y = ( base[2].y + b ) / 2;
  612. b = base[1].y = ( base[0].y + b ) / 2;
  613. base[2].y = ( a + b ) / 2;
  614. }
  615. static void
  616. gray_render_conic( RAS_ARG_ FT_Vector* control,
  617. FT_Vector* to )
  618. {
  619. TPos dx, dy;
  620. int top, level;
  621. int* levels;
  622. FT_Vector* arc;
  623. dx = DOWNSCALE( ras.x ) + to->x - ( control->x << 1 );
  624. if ( dx < 0 )
  625. dx = -dx;
  626. dy = DOWNSCALE( ras.y ) + to->y - ( control->y << 1 );
  627. if ( dy < 0 )
  628. dy = -dy;
  629. if ( dx < dy )
  630. dx = dy;
  631. level = 1;
  632. dx = dx / ras.conic_level;
  633. while ( dx > 0 )
  634. {
  635. dx >>= 2;
  636. level++;
  637. }
  638. /* a shortcut to speed things up */
  639. if ( level <= 1 )
  640. {
  641. /* we compute the mid-point directly in order to avoid */
  642. /* calling gray_split_conic() */
  643. TPos to_x, to_y, mid_x, mid_y;
  644. to_x = UPSCALE( to->x );
  645. to_y = UPSCALE( to->y );
  646. mid_x = ( ras.x + to_x + 2 * UPSCALE( control->x ) ) / 4;
  647. mid_y = ( ras.y + to_y + 2 * UPSCALE( control->y ) ) / 4;
  648. gray_render_line( RAS_VAR_ mid_x, mid_y );
  649. gray_render_line( RAS_VAR_ to_x, to_y );
  650. return;
  651. }
  652. arc = ras.bez_stack;
  653. levels = ras.lev_stack;
  654. top = 0;
  655. levels[0] = level;
  656. arc[0].x = UPSCALE( to->x );
  657. arc[0].y = UPSCALE( to->y );
  658. arc[1].x = UPSCALE( control->x );
  659. arc[1].y = UPSCALE( control->y );
  660. arc[2].x = ras.x;
  661. arc[2].y = ras.y;
  662. while ( top >= 0 )
  663. {
  664. level = levels[top];
  665. if ( level > 1 )
  666. {
  667. /* check that the arc crosses the current band */
  668. TPos min, max, y;
  669. min = max = arc[0].y;
  670. y = arc[1].y;
  671. if ( y < min ) min = y;
  672. if ( y > max ) max = y;
  673. y = arc[2].y;
  674. if ( y < min ) min = y;
  675. if ( y > max ) max = y;
  676. if ( TRUNC( min ) >= ras.max_ey || TRUNC( max ) < 0 )
  677. goto Draw;
  678. gray_split_conic( arc );
  679. arc += 2;
  680. top++;
  681. levels[top] = levels[top - 1] = level - 1;
  682. continue;
  683. }
  684. Draw:
  685. {
  686. TPos to_x, to_y, mid_x, mid_y;
  687. to_x = arc[0].x;
  688. to_y = arc[0].y;
  689. mid_x = ( ras.x + to_x + 2 * arc[1].x ) / 4;
  690. mid_y = ( ras.y + to_y + 2 * arc[1].y ) / 4;
  691. gray_render_line( RAS_VAR_ mid_x, mid_y );
  692. gray_render_line( RAS_VAR_ to_x, to_y );
  693. top--;
  694. arc -= 2;
  695. }
  696. }
  697. return;
  698. }
  699. static void
  700. gray_split_cubic( FT_Vector* base )
  701. {
  702. TPos a, b, c, d;
  703. base[6].x = base[3].x;
  704. c = base[1].x;
  705. d = base[2].x;
  706. base[1].x = a = ( base[0].x + c ) / 2;
  707. base[5].x = b = ( base[3].x + d ) / 2;
  708. c = ( c + d ) / 2;
  709. base[2].x = a = ( a + c ) / 2;
  710. base[4].x = b = ( b + c ) / 2;
  711. base[3].x = ( a + b ) / 2;
  712. base[6].y = base[3].y;
  713. c = base[1].y;
  714. d = base[2].y;
  715. base[1].y = a = ( base[0].y + c ) / 2;
  716. base[5].y = b = ( base[3].y + d ) / 2;
  717. c = ( c + d ) / 2;
  718. base[2].y = a = ( a + c ) / 2;
  719. base[4].y = b = ( b + c ) / 2;
  720. base[3].y = ( a + b ) / 2;
  721. }
  722. static void
  723. gray_render_cubic( RAS_ARG_ FT_Vector* control1,
  724. FT_Vector* control2,
  725. FT_Vector* to )
  726. {
  727. TPos dx, dy, da, db;
  728. int top, level;
  729. int* levels;
  730. FT_Vector* arc;
  731. dx = DOWNSCALE( ras.x ) + to->x - ( control1->x << 1 );
  732. if ( dx < 0 )
  733. dx = -dx;
  734. dy = DOWNSCALE( ras.y ) + to->y - ( control1->y << 1 );
  735. if ( dy < 0 )
  736. dy = -dy;
  737. if ( dx < dy )
  738. dx = dy;
  739. da = dx;
  740. dx = DOWNSCALE( ras.x ) + to->x - 3 * ( control1->x + control2->x );
  741. if ( dx < 0 )
  742. dx = -dx;
  743. dy = DOWNSCALE( ras.y ) + to->y - 3 * ( control1->x + control2->y );
  744. if ( dy < 0 )
  745. dy = -dy;
  746. if ( dx < dy )
  747. dx = dy;
  748. db = dx;
  749. level = 1;
  750. da = da / ras.cubic_level;
  751. db = db / ras.conic_level;
  752. while ( da > 0 || db > 0 )
  753. {
  754. da >>= 2;
  755. db >>= 3;
  756. level++;
  757. }
  758. if ( level <= 1 )
  759. {
  760. TPos to_x, to_y, mid_x, mid_y;
  761. to_x = UPSCALE( to->x );
  762. to_y = UPSCALE( to->y );
  763. mid_x = ( ras.x + to_x +
  764. 3 * UPSCALE( control1->x + control2->x ) ) / 8;
  765. mid_y = ( ras.y + to_y +
  766. 3 * UPSCALE( control1->y + control2->y ) ) / 8;
  767. gray_render_line( RAS_VAR_ mid_x, mid_y );
  768. gray_render_line( RAS_VAR_ to_x, to_y );
  769. return;
  770. }
  771. arc = ras.bez_stack;
  772. arc[0].x = UPSCALE( to->x );
  773. arc[0].y = UPSCALE( to->y );
  774. arc[1].x = UPSCALE( control2->x );
  775. arc[1].y = UPSCALE( control2->y );
  776. arc[2].x = UPSCALE( control1->x );
  777. arc[2].y = UPSCALE( control1->y );
  778. arc[3].x = ras.x;
  779. arc[3].y = ras.y;
  780. levels = ras.lev_stack;
  781. top = 0;
  782. levels[0] = level;
  783. while ( top >= 0 )
  784. {
  785. level = levels[top];
  786. if ( level > 1 )
  787. {
  788. /* check that the arc crosses the current band */
  789. TPos min, max, y;
  790. min = max = arc[0].y;
  791. y = arc[1].y;
  792. if ( y < min ) min = y;
  793. if ( y > max ) max = y;
  794. y = arc[2].y;
  795. if ( y < min ) min = y;
  796. if ( y > max ) max = y;
  797. y = arc[3].y;
  798. if ( y < min ) min = y;
  799. if ( y > max ) max = y;
  800. if ( TRUNC( min ) >= ras.max_ey || TRUNC( max ) < 0 )
  801. goto Draw;
  802. gray_split_cubic( arc );
  803. arc += 3;
  804. top ++;
  805. levels[top] = levels[top - 1] = level - 1;
  806. continue;
  807. }
  808. Draw:
  809. {
  810. TPos to_x, to_y, mid_x, mid_y;
  811. to_x = arc[0].x;
  812. to_y = arc[0].y;
  813. mid_x = ( ras.x + to_x + 3 * ( arc[1].x + arc[2].x ) ) / 8;
  814. mid_y = ( ras.y + to_y + 3 * ( arc[1].y + arc[2].y ) ) / 8;
  815. gray_render_line( RAS_VAR_ mid_x, mid_y );
  816. gray_render_line( RAS_VAR_ to_x, to_y );
  817. top --;
  818. arc -= 3;
  819. }
  820. }
  821. return;
  822. }
  823. /* a macro comparing two cell pointers. Returns true if a <= b. */
  824. #if 1
  825. #define PACK( a ) ( ( (long)(a)->y << 16 ) + (a)->x )
  826. #define LESS_THAN( a, b ) ( PACK( a ) < PACK( b ) )
  827. #else /* 1 */
  828. #define LESS_THAN( a, b ) ( (a)->y < (b)->y || \
  829. ( (a)->y == (b)->y && (a)->x < (b)->x ) )
  830. #endif /* 1 */
  831. #define SWAP_CELLS( a, b, temp ) do \
  832. { \
  833. temp = *(a); \
  834. *(a) = *(b); \
  835. *(b) = temp; \
  836. } while ( 0 )
  837. #define DEBUG_SORT
  838. #define QUICK_SORT
  839. #ifdef SHELL_SORT
  840. /* a simple shell sort algorithm that works directly on our */
  841. /* cells table */
  842. static void
  843. gray_shell_sort ( PCell cells,
  844. int count )
  845. {
  846. PCell i, j, limit = cells + count;
  847. TCell temp;
  848. int gap;
  849. /* compute initial gap */
  850. for ( gap = 0; ++gap < count; gap *= 3 )
  851. ;
  852. while ( gap /= 3 )
  853. {
  854. for ( i = cells + gap; i < limit; i++ )
  855. {
  856. for ( j = i - gap; ; j -= gap )
  857. {
  858. PCell k = j + gap;
  859. if ( LESS_THAN( j, k ) )
  860. break;
  861. SWAP_CELLS( j, k, temp );
  862. if ( j < cells + gap )
  863. break;
  864. }
  865. }
  866. }
  867. }
  868. #endif /* SHELL_SORT */
  869. #ifdef QUICK_SORT
  870. /* This is a non-recursive quicksort that directly process our cells */
  871. /* array. It should be faster than calling the stdlib qsort(), and we */
  872. /* can even tailor our insertion threshold... */
  873. #define QSORT_THRESHOLD 9 /* below this size, a sub-array will be sorted */
  874. /* through a normal insertion sort */
  875. static void
  876. gray_quick_sort( PCell cells,
  877. int count )
  878. {
  879. PCell stack[40]; /* should be enough ;-) */
  880. PCell* top; /* top of stack */
  881. PCell base, limit;
  882. TCell temp;
  883. limit = cells + count;
  884. base = cells;
  885. top = stack;
  886. for (;;)
  887. {
  888. int len = (int)( limit - base );
  889. PCell i, j, pivot;
  890. if ( len > QSORT_THRESHOLD )
  891. {
  892. /* we use base + len/2 as the pivot */
  893. pivot = base + len / 2;
  894. SWAP_CELLS( base, pivot, temp );
  895. i = base + 1;
  896. j = limit - 1;
  897. /* now ensure that *i <= *base <= *j */
  898. if ( LESS_THAN( j, i ) )
  899. SWAP_CELLS( i, j, temp );
  900. if ( LESS_THAN( base, i ) )
  901. SWAP_CELLS( base, i, temp );
  902. if ( LESS_THAN( j, base ) )
  903. SWAP_CELLS( base, j, temp );
  904. for (;;)
  905. {
  906. do i++; while ( LESS_THAN( i, base ) );
  907. do j--; while ( LESS_THAN( base, j ) );
  908. if ( i > j )
  909. break;
  910. SWAP_CELLS( i, j, temp );
  911. }
  912. SWAP_CELLS( base, j, temp );
  913. /* now, push the largest sub-array */
  914. if ( j - base > limit - i )
  915. {
  916. top[0] = base;
  917. top[1] = j;
  918. base = i;
  919. }
  920. else
  921. {
  922. top[0] = i;
  923. top[1] = limit;
  924. limit = j;
  925. }
  926. top += 2;
  927. }
  928. else
  929. {
  930. /* the sub-array is small, perform insertion sort */
  931. j = base;
  932. i = j + 1;
  933. for ( ; i < limit; j = i, i++ )
  934. {
  935. for ( ; LESS_THAN( j + 1, j ); j-- )
  936. {
  937. SWAP_CELLS( j + 1, j, temp );
  938. if ( j == base )
  939. break;
  940. }
  941. }
  942. if ( top > stack )
  943. {
  944. top -= 2;
  945. base = top[0];
  946. limit = top[1];
  947. }
  948. else
  949. break;
  950. }
  951. }
  952. }
  953. #endif /* QUICK_SORT */
  954. #ifdef DEBUG_GRAYS
  955. #ifdef DEBUG_SORT
  956. static int
  957. gray_check_sort( PCell cells,
  958. int count )
  959. {
  960. PCell p, q;
  961. for ( p = cells + count - 2; p >= cells; p-- )
  962. {
  963. q = p + 1;
  964. if ( !LESS_THAN( p, q ) )
  965. return 0;
  966. }
  967. return 1;
  968. }
  969. #endif /* DEBUG_SORT */
  970. #endif /* DEBUG_GRAYS */
  971. static int
  972. gray_move_to( FT_Vector* to,
  973. FT_Raster raster )
  974. {
  975. TPos x, y;
  976. /* record current cell, if any */
  977. gray_record_cell( (PRaster)raster );
  978. /* start to a new position */
  979. x = UPSCALE( to->x );
  980. y = UPSCALE( to->y );
  981. gray_start_cell( (PRaster)raster, TRUNC( x ), TRUNC( y ) );
  982. ((PRaster)raster)->x = x;
  983. ((PRaster)raster)->y = y;
  984. return 0;
  985. }
  986. static int
  987. gray_line_to( FT_Vector* to,
  988. FT_Raster raster )
  989. {
  990. gray_render_line( (PRaster)raster,
  991. UPSCALE( to->x ), UPSCALE( to->y ) );
  992. return 0;
  993. }
  994. static int
  995. gray_conic_to( FT_Vector* control,
  996. FT_Vector* to,
  997. FT_Raster raster )
  998. {
  999. gray_render_conic( (PRaster)raster, control, to );
  1000. return 0;
  1001. }
  1002. static int
  1003. gray_cubic_to( FT_Vector* control1,
  1004. FT_Vector* control2,
  1005. FT_Vector* to,
  1006. FT_Raster raster )
  1007. {
  1008. gray_render_cubic( (PRaster)raster, control1, control2, to );
  1009. return 0;
  1010. }
  1011. static void
  1012. gray_render_span( int y,
  1013. int count,
  1014. FT_Span* spans,
  1015. PRaster raster )
  1016. {
  1017. unsigned char* p;
  1018. FT_Bitmap* map = &raster->target;
  1019. /* first of all, compute the scanline offset */
  1020. p = (unsigned char*)map->buffer - y * map->pitch;
  1021. if ( map->pitch >= 0 )
  1022. p += ( map->rows - 1 ) * map->pitch;
  1023. for ( ; count > 0; count--, spans++ )
  1024. {
  1025. unsigned char coverage = spans->coverage;
  1026. #ifdef GRAYS_USE_GAMMA
  1027. coverage = raster->gamma[coverage];
  1028. #endif
  1029. if ( coverage )
  1030. #if 1
  1031. FT_MEM_SET( p + spans->x, (unsigned char)coverage, spans->len );
  1032. #else /* 1 */
  1033. {
  1034. q = p + spans->x;
  1035. limit = q + spans->len;
  1036. for ( ; q < limit; q++ )
  1037. q[0] = (unsigned char)coverage;
  1038. }
  1039. #endif /* 1 */
  1040. }
  1041. }
  1042. #ifdef DEBUG_GRAYS
  1043. #include <stdio.h>
  1044. static void
  1045. gray_dump_cells( RAS_ARG )
  1046. {
  1047. PCell cell, limit;
  1048. int y = -1;
  1049. cell = ras.cells;
  1050. limit = cell + ras.num_cells;
  1051. for ( ; cell < limit; cell++ )
  1052. {
  1053. if ( cell->y != y )
  1054. {
  1055. fprintf( stderr, "\n%2d: ", cell->y );
  1056. y = cell->y;
  1057. }
  1058. fprintf( stderr, "[%d %d %d]",
  1059. cell->x, cell->area, cell->cover );
  1060. }
  1061. fprintf(stderr, "\n" );
  1062. }
  1063. #endif /* DEBUG_GRAYS */
  1064. static void
  1065. gray_hline( RAS_ARG_ TCoord x,
  1066. TCoord y,
  1067. TPos area,
  1068. int acount )
  1069. {
  1070. FT_Span* span;
  1071. int count;
  1072. int coverage;
  1073. /* compute the coverage line's coverage, depending on the */
  1074. /* outline fill rule */
  1075. /* */
  1076. /* the coverage percentage is area/(PIXEL_BITS*PIXEL_BITS*2) */
  1077. /* */
  1078. coverage = (int)( area >> ( PIXEL_BITS * 2 + 1 - 8 ) );
  1079. /* use range 0..256 */
  1080. if ( coverage < 0 )
  1081. coverage = -coverage;
  1082. if ( ras.outline.flags & FT_OUTLINE_EVEN_ODD_FILL )
  1083. {
  1084. coverage &= 511;
  1085. if ( coverage > 256 )
  1086. coverage = 512 - coverage;
  1087. else if ( coverage == 256 )
  1088. coverage = 255;
  1089. }
  1090. else
  1091. {
  1092. /* normal non-zero winding rule */
  1093. if ( coverage >= 256 )
  1094. coverage = 255;
  1095. }
  1096. y += (TCoord)ras.min_ey;
  1097. x += (TCoord)ras.min_ex;
  1098. if ( coverage )
  1099. {
  1100. /* see if we can add this span to the current list */
  1101. count = ras.num_gray_spans;
  1102. span = ras.gray_spans + count - 1;
  1103. if ( count > 0 &&
  1104. ras.span_y == y &&
  1105. (int)span->x + span->len == (int)x &&
  1106. span->coverage == coverage )
  1107. {
  1108. span->len = (unsigned short)( span->len + acount );
  1109. return;
  1110. }
  1111. if ( ras.span_y != y || count >= FT_MAX_GRAY_SPANS )
  1112. {
  1113. if ( ras.render_span && count > 0 )
  1114. ras.render_span( ras.span_y, count, ras.gray_spans,
  1115. ras.render_span_data );
  1116. /* ras.render_span( span->y, ras.gray_spans, count ); */
  1117. #ifdef DEBUG_GRAYS
  1118. if ( ras.span_y >= 0 )
  1119. {
  1120. int n;
  1121. fprintf( stderr, "y=%3d ", ras.span_y );
  1122. span = ras.gray_spans;
  1123. for ( n = 0; n < count; n++, span++ )
  1124. fprintf( stderr, "[%d..%d]:%02x ",
  1125. span->x, span->x + span->len - 1, span->coverage );
  1126. fprintf( stderr, "\n" );
  1127. }
  1128. #endif /* DEBUG_GRAYS */
  1129. ras.num_gray_spans = 0;
  1130. ras.span_y = y;
  1131. count = 0;
  1132. span = ras.gray_spans;
  1133. }
  1134. else
  1135. span++;
  1136. /* add a gray span to the current list */
  1137. span->x = (short)x;
  1138. span->len = (unsigned short)acount;
  1139. span->coverage = (unsigned char)coverage;
  1140. ras.num_gray_spans++;
  1141. }
  1142. }
  1143. static void
  1144. gray_sweep( RAS_ARG_ FT_Bitmap* target )
  1145. {
  1146. TCoord x, y, cover;
  1147. TArea area;
  1148. PCell start, cur, limit;
  1149. FT_UNUSED( target );
  1150. if ( ras.num_cells == 0 )
  1151. return;
  1152. cur = ras.cells;
  1153. limit = cur + ras.num_cells;
  1154. cover = 0;
  1155. ras.span_y = -1;
  1156. ras.num_gray_spans = 0;
  1157. for (;;)
  1158. {
  1159. start = cur;
  1160. y = start->y;
  1161. x = start->x;
  1162. area = start->area;
  1163. cover += start->cover;
  1164. /* accumulate all start cells */
  1165. for (;;)
  1166. {
  1167. ++cur;
  1168. if ( cur >= limit || cur->y != start->y || cur->x != start->x )
  1169. break;
  1170. area += cur->area;
  1171. cover += cur->cover;
  1172. }
  1173. /* if the start cell has a non-null area, we must draw an */
  1174. /* individual gray pixel there */
  1175. if ( area && x >= 0 )
  1176. {
  1177. gray_hline( RAS_VAR_ x, y, cover * ( ONE_PIXEL * 2 ) - area, 1 );
  1178. x++;
  1179. }
  1180. if ( x < 0 )
  1181. x = 0;
  1182. if ( cur < limit && start->y == cur->y )
  1183. {
  1184. /* draw a gray span between the start cell and the current one */
  1185. if ( cur->x > x )
  1186. gray_hline( RAS_VAR_ x, y,
  1187. cover * ( ONE_PIXEL * 2 ), cur->x - x );
  1188. }
  1189. else
  1190. {
  1191. /* draw a gray span until the end of the clipping region */
  1192. if ( cover && x < ras.max_ex - ras.min_ex )
  1193. gray_hline( RAS_VAR_ x, y,
  1194. cover * ( ONE_PIXEL * 2 ),
  1195. (int)( ras.max_ex - x - ras.min_ex ) );
  1196. cover = 0;
  1197. }
  1198. if ( cur >= limit )
  1199. break;
  1200. }
  1201. if ( ras.render_span && ras.num_gray_spans > 0 )
  1202. ras.render_span( ras.span_y, ras.num_gray_spans,
  1203. ras.gray_spans, ras.render_span_data );
  1204. #ifdef DEBUG_GRAYS
  1205. {
  1206. int n;
  1207. FT_Span* span;
  1208. fprintf( stderr, "y=%3d ", ras.span_y );
  1209. span = ras.gray_spans;
  1210. for ( n = 0; n < ras.num_gray_spans; n++, span++ )
  1211. fprintf( stderr, "[%d..%d]:%02x ",
  1212. span->x, span->x + span->len - 1, span->coverage );
  1213. fprintf( stderr, "\n" );
  1214. }
  1215. #endif /* DEBUG_GRAYS */
  1216. }
  1217. #ifdef _STANDALONE_
  1218. /*************************************************************************/
  1219. /* */
  1220. /* The following function should only compile in stand_alone mode, */
  1221. /* i.e., when building this component without the rest of FreeType. */
  1222. /* */
  1223. /*************************************************************************/
  1224. /*************************************************************************/
  1225. /* */
  1226. /* <Function> */
  1227. /* FT_Outline_Decompose */
  1228. /* */
  1229. /* <Description> */
  1230. /* Walks over an outline's structure to decompose it into individual */
  1231. /* segments and Bezier arcs. This function is also able to emit */
  1232. /* `move to' and `close to' operations to indicate the start and end */
  1233. /* of new contours in the outline. */
  1234. /* */
  1235. /* <Input> */
  1236. /* outline :: A pointer to the source target. */
  1237. /* */
  1238. /* func_interface :: A table of `emitters', i.e,. function pointers */
  1239. /* called during decomposition to indicate path */
  1240. /* operations. */
  1241. /* */
  1242. /* user :: A typeless pointer which is passed to each */
  1243. /* emitter during the decomposition. It can be */
  1244. /* used to store the state during the */
  1245. /* decomposition. */
  1246. /* */
  1247. /* <Return> */
  1248. /* Error code. 0 means sucess. */
  1249. /* */
  1250. static
  1251. int FT_Outline_Decompose( FT_Outline* outline,
  1252. const FT_Outline_Funcs* func_interface,
  1253. void* user )
  1254. {
  1255. #undef SCALED
  1256. #if 0
  1257. #define SCALED( x ) ( ( (x) << shift ) - delta )
  1258. #else
  1259. #define SCALED( x ) (x)
  1260. #endif
  1261. FT_Vector v_last;
  1262. FT_Vector v_control;
  1263. FT_Vector v_start;
  1264. FT_Vector* point;
  1265. FT_Vector* limit;
  1266. char* tags;
  1267. int n; /* index of contour in outline */
  1268. int first; /* index of first point in contour */
  1269. int error;
  1270. char tag; /* current point's state */
  1271. #if 0
  1272. int shift = func_interface->shift;
  1273. TPos delta = func_interface->delta;
  1274. #endif
  1275. first = 0;
  1276. for ( n = 0; n < outline->n_contours; n++ )
  1277. {
  1278. int last; /* index of last point in contour */
  1279. last = outline->contours[n];
  1280. limit = outline->points + last;
  1281. v_start = outline->points[first];
  1282. v_last = outline->points[last];
  1283. v_start.x = SCALED( v_start.x ); v_start.y = SCALED( v_start.y );
  1284. v_last.x = SCALED( v_last.x ); v_last.y = SCALED( v_last.y );
  1285. v_control = v_start;
  1286. point = outline->points + first;
  1287. tags = outline->tags + first;
  1288. tag = FT_CURVE_TAG( tags[0] );
  1289. /* A contour cannot start with a cubic control point! */
  1290. if ( tag == FT_CURVE_TAG_CUBIC )
  1291. goto Invalid_Outline;
  1292. /* check first point to determine origin */
  1293. if ( tag == FT_CURVE_TAG_CONIC )
  1294. {
  1295. /* first point is conic control. Yes, this happens. */
  1296. if ( FT_CURVE_TAG( outline->tags[last] ) == FT_CURVE_TAG_ON )
  1297. {
  1298. /* start at last point if it is on the curve */
  1299. v_start = v_last;
  1300. limit--;
  1301. }
  1302. else
  1303. {
  1304. /* if both first and last points are conic, */
  1305. /* start at their middle and record its position */
  1306. /* for closure */
  1307. v_start.x = ( v_start.x + v_last.x ) / 2;
  1308. v_start.y = ( v_start.y + v_last.y ) / 2;
  1309. v_last = v_start;
  1310. }
  1311. point--;
  1312. tags--;
  1313. }
  1314. error = func_interface->move_to( &v_start, user );
  1315. if ( error )
  1316. goto Exit;
  1317. while ( point < limit )
  1318. {
  1319. point++;
  1320. tags++;
  1321. tag = FT_CURVE_TAG( tags[0] );
  1322. switch ( tag )
  1323. {
  1324. case FT_CURVE_TAG_ON: /* emit a single line_to */
  1325. {
  1326. FT_Vector vec;
  1327. vec.x = SCALED( point->x );
  1328. vec.y = SCALED( point->y );
  1329. error = func_interface->line_to( &vec, user );
  1330. if ( error )
  1331. goto Exit;
  1332. continue;
  1333. }
  1334. case FT_CURVE_TAG_CONIC: /* consume conic arcs */
  1335. {
  1336. v_control.x = SCALED( point->x );
  1337. v_control.y = SCALED( point->y );
  1338. Do_Conic:
  1339. if ( point < limit )
  1340. {
  1341. FT_Vector vec;
  1342. FT_Vector v_middle;
  1343. point++;
  1344. tags++;
  1345. tag = FT_CURVE_TAG( tags[0] );
  1346. vec.x = SCALED( point->x );
  1347. vec.y = SCALED( point->y );
  1348. if ( tag == FT_CURVE_TAG_ON )
  1349. {
  1350. error = func_interface->conic_to( &v_control, &vec, user );
  1351. if ( error )
  1352. goto Exit;
  1353. continue;
  1354. }
  1355. if ( tag != FT_CURVE_TAG_CONIC )
  1356. goto Invalid_Outline;
  1357. v_middle.x = ( v_control.x + vec.x ) / 2;
  1358. v_middle.y = ( v_control.y + vec.y ) / 2;
  1359. error = func_interface->conic_to( &v_control, &v_middle, user );
  1360. if ( error )
  1361. goto Exit;
  1362. v_control = vec;
  1363. goto Do_Conic;
  1364. }
  1365. error = func_interface->conic_to( &v_control, &v_start, user );
  1366. goto Close;
  1367. }
  1368. default: /* FT_CURVE_TAG_CUBIC */
  1369. {
  1370. FT_Vector vec1, vec2;
  1371. if ( point + 1 > limit ||
  1372. FT_CURVE_TAG( tags[1] ) != FT_CURVE_TAG_CUBIC )
  1373. goto Invalid_Outline;
  1374. point += 2;
  1375. tags += 2;
  1376. vec1.x = SCALED( point[-2].x ); vec1.y = SCALED( point[-2].y );
  1377. vec2.x = SCALED( point[-1].x ); vec2.y = SCALED( point[-1].y );
  1378. if ( point <= limit )
  1379. {
  1380. FT_Vector vec;
  1381. vec.x = SCALED( point->x );
  1382. vec.y = SCALED( point->y );
  1383. error = func_interface->cubic_to( &vec1, &vec2, &vec, user );
  1384. if ( error )
  1385. goto Exit;
  1386. continue;
  1387. }
  1388. error = func_interface->cubic_to( &vec1, &vec2, &v_start, user );
  1389. goto Close;
  1390. }
  1391. }
  1392. }
  1393. /* close the contour with a line segment */
  1394. error = func_interface->line_to( &v_start, user );
  1395. Close:
  1396. if ( error )
  1397. goto Exit;
  1398. first = last + 1;
  1399. }
  1400. return 0;
  1401. Exit:
  1402. return error;
  1403. Invalid_Outline:
  1404. return ErrRaster_Invalid_Outline;
  1405. }
  1406. #endif /* _STANDALONE_ */
  1407. typedef struct TBand_
  1408. {
  1409. TPos min, max;
  1410. } TBand;
  1411. static int
  1412. gray_convert_glyph_inner( RAS_ARG )
  1413. {
  1414. static
  1415. const FT_Outline_Funcs func_interface =
  1416. {
  1417. (FT_Outline_MoveTo_Func) gray_move_to,
  1418. (FT_Outline_LineTo_Func) gray_line_to,
  1419. (FT_Outline_ConicTo_Func)gray_conic_to,
  1420. (FT_Outline_CubicTo_Func)gray_cubic_to,
  1421. 0,
  1422. 0
  1423. };
  1424. volatile int error = 0;
  1425. if ( ft_setjmp( ras.jump_buffer ) == 0 )
  1426. {
  1427. error = FT_Outline_Decompose( &ras.outline, &func_interface, &ras );
  1428. gray_record_cell( RAS_VAR );
  1429. }
  1430. else
  1431. {
  1432. error = ErrRaster_MemoryOverflow;
  1433. }
  1434. return error;
  1435. }
  1436. static int
  1437. gray_convert_glyph( RAS_ARG )
  1438. {
  1439. TBand bands[40];
  1440. volatile TBand* band;
  1441. volatile int n, num_bands;
  1442. volatile TPos min, max, max_y;
  1443. FT_BBox* clip;
  1444. /* Set up state in the raster object */
  1445. gray_compute_cbox( RAS_VAR );
  1446. /* clip to target bitmap, exit if nothing to do */
  1447. clip = &ras.clip_box;
  1448. if ( ras.max_ex <= clip->xMin || ras.min_ex >= clip->xMax ||
  1449. ras.max_ey <= clip->yMin || ras.min_ey >= clip->yMax )
  1450. return 0;
  1451. if ( ras.min_ex < clip->xMin ) ras.min_ex = clip->xMin;
  1452. if ( ras.min_ey < clip->yMin ) ras.min_ey = clip->yMin;
  1453. if ( ras.max_ex > clip->xMax ) ras.max_ex = clip->xMax;
  1454. if ( ras.max_ey > clip->yMax ) ras.max_ey = clip->yMax;
  1455. /* simple heuristic used to speed-up the bezier decomposition -- see */
  1456. /* the code in gray_render_conic() and gray_render_cubic() for more */
  1457. /* details */
  1458. ras.conic_level = 32;
  1459. ras.cubic_level = 16;
  1460. {
  1461. int level = 0;
  1462. if ( ras.max_ex > 24 || ras.max_ey > 24 )
  1463. level++;
  1464. if ( ras.max_ex > 120 || ras.max_ey > 120 )
  1465. level++;
  1466. ras.conic_level <<= level;
  1467. ras.cubic_level <<= level;
  1468. }
  1469. /* setup vertical bands */
  1470. num_bands = (int)( ( ras.max_ey - ras.min_ey ) / ras.band_size );
  1471. if ( num_bands == 0 ) num_bands = 1;
  1472. if ( num_bands >= 39 ) num_bands = 39;
  1473. ras.band_shoot = 0;
  1474. min = ras.min_ey;
  1475. max_y = ras.max_ey;
  1476. for ( n = 0; n < num_bands; n++, min = max )
  1477. {
  1478. max = min + ras.band_size;
  1479. if ( n == num_bands - 1 || max > max_y )
  1480. max = max_y;
  1481. bands[0].min = min;
  1482. bands[0].max = max;
  1483. band = bands;
  1484. while ( band >= bands )
  1485. {
  1486. TPos bottom, top, middle;
  1487. int error;
  1488. ras.num_cells = 0;
  1489. ras.invalid = 1;
  1490. ras.min_ey = band->min;
  1491. ras.max_ey = band->max;
  1492. #if 1
  1493. error = gray_convert_glyph_inner( RAS_VAR );
  1494. #else
  1495. error = FT_Outline_Decompose( outline, &func_interface, &ras ) ||
  1496. gray_record_cell( RAS_VAR );
  1497. #endif
  1498. if ( !error )
  1499. {
  1500. #ifdef SHELL_SORT
  1501. gray_shell_sort( ras.cells, ras.num_cells );
  1502. #else
  1503. gray_quick_sort( ras.cells, ras.num_cells );
  1504. #endif
  1505. #ifdef DEBUG_GRAYS
  1506. gray_check_sort( ras.cells, ras.num_cells );
  1507. gray_dump_cells( RAS_VAR );
  1508. #endif
  1509. gray_sweep( RAS_VAR_ &ras.target );
  1510. band--;
  1511. continue;
  1512. }
  1513. else if ( error != ErrRaster_MemoryOverflow )
  1514. return 1;
  1515. /* render pool overflow, we will reduce the render band by half */
  1516. bottom = band->min;
  1517. top = band->max;
  1518. middle = bottom + ( ( top - bottom ) >> 1 );
  1519. /* waoow! This is too complex for a single scanline, something */
  1520. /* must be really rotten here! */
  1521. if ( middle == bottom )
  1522. {
  1523. #ifdef DEBUG_GRAYS
  1524. fprintf( stderr, "Rotten glyph!\n" );
  1525. #endif
  1526. return 1;
  1527. }
  1528. if ( bottom-top >= ras.band_size )
  1529. ras.band_shoot++;
  1530. band[1].min = bottom;
  1531. band[1].max = middle;
  1532. band[0].min = middle;
  1533. band[0].max = top;
  1534. band++;
  1535. }
  1536. }
  1537. if ( ras.band_shoot > 8 && ras.band_size > 16 )
  1538. ras.band_size = ras.band_size / 2;
  1539. return 0;
  1540. }
  1541. extern int
  1542. gray_raster_render( PRaster raster,
  1543. FT_Raster_Params* params )
  1544. {
  1545. FT_Outline* outline = (FT_Outline*)params->source;
  1546. FT_Bitmap* target_map = params->target;
  1547. if ( !raster || !raster->cells || !raster->max_cells )
  1548. return -1;
  1549. /* return immediately if the outline is empty */
  1550. if ( outline->n_points == 0 || outline->n_contours <= 0 )
  1551. return 0;
  1552. if ( !outline || !outline->contours || !outline->points )
  1553. return ErrRaster_Invalid_Outline;
  1554. if ( outline->n_points !=
  1555. outline->contours[outline->n_contours - 1] + 1 )
  1556. return ErrRaster_Invalid_Outline;
  1557. /* if direct mode is not set, we must have a target bitmap */
  1558. if ( ( params->flags & FT_RASTER_FLAG_DIRECT ) == 0 &&
  1559. ( !target_map || !target_map->buffer ) )
  1560. return -1;
  1561. /* this version does not support monochrome rendering */
  1562. if ( !( params->flags & FT_RASTER_FLAG_AA ) )
  1563. return ErrRaster_Invalid_Mode;
  1564. /* compute clipping box */
  1565. if ( ( params->flags & FT_RASTER_FLAG_DIRECT ) == 0 )
  1566. {
  1567. /* compute clip box from target pixmap */
  1568. ras.clip_box.xMin = 0;
  1569. ras.clip_box.yMin = 0;
  1570. ras.clip_box.xMax = target_map->width;
  1571. ras.clip_box.yMax = target_map->rows;
  1572. }
  1573. else if ( params->flags & FT_RASTER_FLAG_CLIP )
  1574. {
  1575. ras.clip_box = params->clip_box;
  1576. }
  1577. else
  1578. {
  1579. ras.clip_box.xMin = -32768L;
  1580. ras.clip_box.yMin = -32768L;
  1581. ras.clip_box.xMax = 32767L;
  1582. ras.clip_box.yMax = 32767L;
  1583. }
  1584. ras.outline = *outline;
  1585. ras.num_cells = 0;
  1586. ras.invalid = 1;
  1587. if ( target_map )
  1588. ras.target = *target_map;
  1589. ras.render_span = (FT_Raster_Span_Func)gray_render_span;
  1590. ras.render_span_data = &ras;
  1591. if ( params->flags & FT_RASTER_FLAG_DIRECT )
  1592. {
  1593. ras.render_span = (FT_Raster_Span_Func)params->gray_spans;
  1594. ras.render_span_data = params->user;
  1595. }
  1596. return gray_convert_glyph( (PRaster)raster );
  1597. }
  1598. /**** RASTER OBJECT CREATION: In standalone mode, we simply use *****/
  1599. /**** a static object. *****/
  1600. #ifdef GRAYS_USE_GAMMA
  1601. /* initialize the "gamma" table. Yes, this is really a crummy function */
  1602. /* but the results look pretty good for something that simple. */
  1603. /* */
  1604. #define M_MAX 255
  1605. #define M_X 128
  1606. #define M_Y 192
  1607. static void
  1608. grays_init_gamma( PRaster raster )
  1609. {
  1610. unsigned int x, a;
  1611. for ( x = 0; x < 256; x++ )
  1612. {
  1613. if ( x <= M_X )
  1614. a = ( x * M_Y + M_X / 2) / M_X;
  1615. else
  1616. a = M_Y + ( ( x - M_X ) * ( M_MAX - M_Y ) +
  1617. ( M_MAX - M_X ) / 2 ) / ( M_MAX - M_X );
  1618. raster->gamma[x] = (unsigned char)a;
  1619. }
  1620. }
  1621. #endif /* GRAYS_USE_GAMMA */
  1622. #ifdef _STANDALONE_
  1623. static int
  1624. gray_raster_new( void* memory,
  1625. FT_Raster* araster )
  1626. {
  1627. static TRaster the_raster;
  1628. FT_UNUSED( memory );
  1629. *araster = (FT_Raster)&the_raster;
  1630. FT_MEM_ZERO( &the_raster, sizeof ( the_raster ) );
  1631. #ifdef GRAYS_USE_GAMMA
  1632. grays_init_gamma( (PRaster)*araster );
  1633. #endif
  1634. return 0;
  1635. }
  1636. static void
  1637. gray_raster_done( FT_Raster raster )
  1638. {
  1639. /* nothing */
  1640. FT_UNUSED( raster );
  1641. }
  1642. #else /* _STANDALONE_ */
  1643. static int
  1644. gray_raster_new( FT_Memory memory,
  1645. FT_Raster* araster )
  1646. {
  1647. FT_Error error;
  1648. PRaster raster;
  1649. *araster = 0;
  1650. if ( !FT_ALLOC( raster, sizeof ( TRaster ) ) )
  1651. {
  1652. raster->memory = memory;
  1653. *araster = (FT_Raster)raster;
  1654. #ifdef GRAYS_USE_GAMMA
  1655. grays_init_gamma( raster );
  1656. #endif
  1657. }
  1658. return error;
  1659. }
  1660. static void
  1661. gray_raster_done( FT_Raster raster )
  1662. {
  1663. FT_Memory memory = (FT_Memory)((PRaster)raster)->memory;
  1664. FT_FREE( raster );
  1665. }
  1666. #endif /* _STANDALONE_ */
  1667. static void
  1668. gray_raster_reset( FT_Raster raster,
  1669. const char* pool_base,
  1670. long pool_size )
  1671. {
  1672. PRaster rast = (PRaster)raster;
  1673. if ( raster && pool_base && pool_size >= 4096 )
  1674. gray_init_cells( rast, (char*)pool_base, pool_size );
  1675. rast->band_size = (int)( ( pool_size / sizeof ( TCell ) ) / 8 );
  1676. }
  1677. const FT_Raster_Funcs ft_grays_raster =
  1678. {
  1679. FT_GLYPH_FORMAT_OUTLINE,
  1680. (FT_Raster_New_Func) gray_raster_new,
  1681. (FT_Raster_Reset_Func) gray_raster_reset,
  1682. (FT_Raster_Set_Mode_Func)0,
  1683. (FT_Raster_Render_Func) gray_raster_render,
  1684. (FT_Raster_Done_Func) gray_raster_done
  1685. };
  1686. /* END */