/modules/freetype2/src/base/ftoutln.c

http://github.com/zpao/v8monkey · C · 1129 lines · 762 code · 283 blank · 84 comment · 175 complexity · baddae4a33ff460396acfb26b10b656f MD5 · raw file

  1. /***************************************************************************/
  2. /* */
  3. /* ftoutln.c */
  4. /* */
  5. /* FreeType outline management (body). */
  6. /* */
  7. /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2010 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. /* All functions are declared in freetype.h. */
  20. /* */
  21. /*************************************************************************/
  22. #include <ft2build.h>
  23. #include FT_OUTLINE_H
  24. #include FT_INTERNAL_OBJECTS_H
  25. #include FT_INTERNAL_DEBUG_H
  26. #include FT_TRIGONOMETRY_H
  27. /*************************************************************************/
  28. /* */
  29. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  30. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  31. /* messages during execution. */
  32. /* */
  33. #undef FT_COMPONENT
  34. #define FT_COMPONENT trace_outline
  35. static
  36. const FT_Outline null_outline = { 0, 0, 0, 0, 0, 0 };
  37. /* documentation is in ftoutln.h */
  38. FT_EXPORT_DEF( FT_Error )
  39. FT_Outline_Decompose( FT_Outline* outline,
  40. const FT_Outline_Funcs* func_interface,
  41. void* user )
  42. {
  43. #undef SCALED
  44. #define SCALED( x ) ( ( (x) << shift ) - delta )
  45. FT_Vector v_last;
  46. FT_Vector v_control;
  47. FT_Vector v_start;
  48. FT_Vector* point;
  49. FT_Vector* limit;
  50. char* tags;
  51. FT_Error error;
  52. FT_Int n; /* index of contour in outline */
  53. FT_UInt first; /* index of first point in contour */
  54. FT_Int tag; /* current point's state */
  55. FT_Int shift;
  56. FT_Pos delta;
  57. if ( !outline || !func_interface )
  58. return FT_Err_Invalid_Argument;
  59. shift = func_interface->shift;
  60. delta = func_interface->delta;
  61. first = 0;
  62. for ( n = 0; n < outline->n_contours; n++ )
  63. {
  64. FT_Int last; /* index of last point in contour */
  65. FT_TRACE5(( "FT_Outline_Decompose: Outline %d\n", n ));
  66. last = outline->contours[n];
  67. if ( last < 0 )
  68. goto Invalid_Outline;
  69. limit = outline->points + last;
  70. v_start = outline->points[first];
  71. v_start.x = SCALED( v_start.x );
  72. v_start.y = SCALED( v_start.y );
  73. v_last = outline->points[last];
  74. v_last.x = SCALED( v_last.x );
  75. v_last.y = SCALED( v_last.y );
  76. v_control = v_start;
  77. point = outline->points + first;
  78. tags = outline->tags + first;
  79. tag = FT_CURVE_TAG( tags[0] );
  80. /* A contour cannot start with a cubic control point! */
  81. if ( tag == FT_CURVE_TAG_CUBIC )
  82. goto Invalid_Outline;
  83. /* check first point to determine origin */
  84. if ( tag == FT_CURVE_TAG_CONIC )
  85. {
  86. /* first point is conic control. Yes, this happens. */
  87. if ( FT_CURVE_TAG( outline->tags[last] ) == FT_CURVE_TAG_ON )
  88. {
  89. /* start at last point if it is on the curve */
  90. v_start = v_last;
  91. limit--;
  92. }
  93. else
  94. {
  95. /* if both first and last points are conic, */
  96. /* start at their middle and record its position */
  97. /* for closure */
  98. v_start.x = ( v_start.x + v_last.x ) / 2;
  99. v_start.y = ( v_start.y + v_last.y ) / 2;
  100. v_last = v_start;
  101. }
  102. point--;
  103. tags--;
  104. }
  105. FT_TRACE5(( " move to (%.2f, %.2f)\n",
  106. v_start.x / 64.0, v_start.y / 64.0 ));
  107. error = func_interface->move_to( &v_start, user );
  108. if ( error )
  109. goto Exit;
  110. while ( point < limit )
  111. {
  112. point++;
  113. tags++;
  114. tag = FT_CURVE_TAG( tags[0] );
  115. switch ( tag )
  116. {
  117. case FT_CURVE_TAG_ON: /* emit a single line_to */
  118. {
  119. FT_Vector vec;
  120. vec.x = SCALED( point->x );
  121. vec.y = SCALED( point->y );
  122. FT_TRACE5(( " line to (%.2f, %.2f)\n",
  123. vec.x / 64.0, vec.y / 64.0 ));
  124. error = func_interface->line_to( &vec, user );
  125. if ( error )
  126. goto Exit;
  127. continue;
  128. }
  129. case FT_CURVE_TAG_CONIC: /* consume conic arcs */
  130. v_control.x = SCALED( point->x );
  131. v_control.y = SCALED( point->y );
  132. Do_Conic:
  133. if ( point < limit )
  134. {
  135. FT_Vector vec;
  136. FT_Vector v_middle;
  137. point++;
  138. tags++;
  139. tag = FT_CURVE_TAG( tags[0] );
  140. vec.x = SCALED( point->x );
  141. vec.y = SCALED( point->y );
  142. if ( tag == FT_CURVE_TAG_ON )
  143. {
  144. FT_TRACE5(( " conic to (%.2f, %.2f)"
  145. " with control (%.2f, %.2f)\n",
  146. vec.x / 64.0, vec.y / 64.0,
  147. v_control.x / 64.0, v_control.y / 64.0 ));
  148. error = func_interface->conic_to( &v_control, &vec, user );
  149. if ( error )
  150. goto Exit;
  151. continue;
  152. }
  153. if ( tag != FT_CURVE_TAG_CONIC )
  154. goto Invalid_Outline;
  155. v_middle.x = ( v_control.x + vec.x ) / 2;
  156. v_middle.y = ( v_control.y + vec.y ) / 2;
  157. FT_TRACE5(( " conic to (%.2f, %.2f)"
  158. " with control (%.2f, %.2f)\n",
  159. v_middle.x / 64.0, v_middle.y / 64.0,
  160. v_control.x / 64.0, v_control.y / 64.0 ));
  161. error = func_interface->conic_to( &v_control, &v_middle, user );
  162. if ( error )
  163. goto Exit;
  164. v_control = vec;
  165. goto Do_Conic;
  166. }
  167. FT_TRACE5(( " conic to (%.2f, %.2f)"
  168. " with control (%.2f, %.2f)\n",
  169. v_start.x / 64.0, v_start.y / 64.0,
  170. v_control.x / 64.0, v_control.y / 64.0 ));
  171. error = func_interface->conic_to( &v_control, &v_start, user );
  172. goto Close;
  173. default: /* FT_CURVE_TAG_CUBIC */
  174. {
  175. FT_Vector vec1, vec2;
  176. if ( point + 1 > limit ||
  177. FT_CURVE_TAG( tags[1] ) != FT_CURVE_TAG_CUBIC )
  178. goto Invalid_Outline;
  179. point += 2;
  180. tags += 2;
  181. vec1.x = SCALED( point[-2].x );
  182. vec1.y = SCALED( point[-2].y );
  183. vec2.x = SCALED( point[-1].x );
  184. vec2.y = SCALED( point[-1].y );
  185. if ( point <= limit )
  186. {
  187. FT_Vector vec;
  188. vec.x = SCALED( point->x );
  189. vec.y = SCALED( point->y );
  190. FT_TRACE5(( " cubic to (%.2f, %.2f)"
  191. " with controls (%.2f, %.2f) and (%.2f, %.2f)\n",
  192. vec.x / 64.0, vec.y / 64.0,
  193. vec1.x / 64.0, vec1.y / 64.0,
  194. vec2.x / 64.0, vec2.y / 64.0 ));
  195. error = func_interface->cubic_to( &vec1, &vec2, &vec, user );
  196. if ( error )
  197. goto Exit;
  198. continue;
  199. }
  200. FT_TRACE5(( " cubic to (%.2f, %.2f)"
  201. " with controls (%.2f, %.2f) and (%.2f, %.2f)\n",
  202. v_start.x / 64.0, v_start.y / 64.0,
  203. vec1.x / 64.0, vec1.y / 64.0,
  204. vec2.x / 64.0, vec2.y / 64.0 ));
  205. error = func_interface->cubic_to( &vec1, &vec2, &v_start, user );
  206. goto Close;
  207. }
  208. }
  209. }
  210. /* close the contour with a line segment */
  211. FT_TRACE5(( " line to (%.2f, %.2f)\n",
  212. v_start.x / 64.0, v_start.y / 64.0 ));
  213. error = func_interface->line_to( &v_start, user );
  214. Close:
  215. if ( error )
  216. goto Exit;
  217. first = last + 1;
  218. }
  219. FT_TRACE5(( "FT_Outline_Decompose: Done\n", n ));
  220. return FT_Err_Ok;
  221. Exit:
  222. FT_TRACE5(( "FT_Outline_Decompose: Error %d\n", error ));
  223. return error;
  224. Invalid_Outline:
  225. return FT_Err_Invalid_Outline;
  226. }
  227. FT_EXPORT_DEF( FT_Error )
  228. FT_Outline_New_Internal( FT_Memory memory,
  229. FT_UInt numPoints,
  230. FT_Int numContours,
  231. FT_Outline *anoutline )
  232. {
  233. FT_Error error;
  234. if ( !anoutline || !memory )
  235. return FT_Err_Invalid_Argument;
  236. *anoutline = null_outline;
  237. if ( FT_NEW_ARRAY( anoutline->points, numPoints ) ||
  238. FT_NEW_ARRAY( anoutline->tags, numPoints ) ||
  239. FT_NEW_ARRAY( anoutline->contours, numContours ) )
  240. goto Fail;
  241. anoutline->n_points = (FT_UShort)numPoints;
  242. anoutline->n_contours = (FT_Short)numContours;
  243. anoutline->flags |= FT_OUTLINE_OWNER;
  244. return FT_Err_Ok;
  245. Fail:
  246. anoutline->flags |= FT_OUTLINE_OWNER;
  247. FT_Outline_Done_Internal( memory, anoutline );
  248. return error;
  249. }
  250. /* documentation is in ftoutln.h */
  251. FT_EXPORT_DEF( FT_Error )
  252. FT_Outline_New( FT_Library library,
  253. FT_UInt numPoints,
  254. FT_Int numContours,
  255. FT_Outline *anoutline )
  256. {
  257. if ( !library )
  258. return FT_Err_Invalid_Library_Handle;
  259. return FT_Outline_New_Internal( library->memory, numPoints,
  260. numContours, anoutline );
  261. }
  262. /* documentation is in ftoutln.h */
  263. FT_EXPORT_DEF( FT_Error )
  264. FT_Outline_Check( FT_Outline* outline )
  265. {
  266. if ( outline )
  267. {
  268. FT_Int n_points = outline->n_points;
  269. FT_Int n_contours = outline->n_contours;
  270. FT_Int end0, end;
  271. FT_Int n;
  272. /* empty glyph? */
  273. if ( n_points == 0 && n_contours == 0 )
  274. return 0;
  275. /* check point and contour counts */
  276. if ( n_points <= 0 || n_contours <= 0 )
  277. goto Bad;
  278. end0 = end = -1;
  279. for ( n = 0; n < n_contours; n++ )
  280. {
  281. end = outline->contours[n];
  282. /* note that we don't accept empty contours */
  283. if ( end <= end0 || end >= n_points )
  284. goto Bad;
  285. end0 = end;
  286. }
  287. if ( end != n_points - 1 )
  288. goto Bad;
  289. /* XXX: check the tags array */
  290. return 0;
  291. }
  292. Bad:
  293. return FT_Err_Invalid_Argument;
  294. }
  295. /* documentation is in ftoutln.h */
  296. FT_EXPORT_DEF( FT_Error )
  297. FT_Outline_Copy( const FT_Outline* source,
  298. FT_Outline *target )
  299. {
  300. FT_Int is_owner;
  301. if ( !source || !target ||
  302. source->n_points != target->n_points ||
  303. source->n_contours != target->n_contours )
  304. return FT_Err_Invalid_Argument;
  305. if ( source == target )
  306. return FT_Err_Ok;
  307. FT_ARRAY_COPY( target->points, source->points, source->n_points );
  308. FT_ARRAY_COPY( target->tags, source->tags, source->n_points );
  309. FT_ARRAY_COPY( target->contours, source->contours, source->n_contours );
  310. /* copy all flags, except the `FT_OUTLINE_OWNER' one */
  311. is_owner = target->flags & FT_OUTLINE_OWNER;
  312. target->flags = source->flags;
  313. target->flags &= ~FT_OUTLINE_OWNER;
  314. target->flags |= is_owner;
  315. return FT_Err_Ok;
  316. }
  317. FT_EXPORT_DEF( FT_Error )
  318. FT_Outline_Done_Internal( FT_Memory memory,
  319. FT_Outline* outline )
  320. {
  321. if ( memory && outline )
  322. {
  323. if ( outline->flags & FT_OUTLINE_OWNER )
  324. {
  325. FT_FREE( outline->points );
  326. FT_FREE( outline->tags );
  327. FT_FREE( outline->contours );
  328. }
  329. *outline = null_outline;
  330. return FT_Err_Ok;
  331. }
  332. else
  333. return FT_Err_Invalid_Argument;
  334. }
  335. /* documentation is in ftoutln.h */
  336. FT_EXPORT_DEF( FT_Error )
  337. FT_Outline_Done( FT_Library library,
  338. FT_Outline* outline )
  339. {
  340. /* check for valid `outline' in FT_Outline_Done_Internal() */
  341. if ( !library )
  342. return FT_Err_Invalid_Library_Handle;
  343. return FT_Outline_Done_Internal( library->memory, outline );
  344. }
  345. /* documentation is in ftoutln.h */
  346. FT_EXPORT_DEF( void )
  347. FT_Outline_Get_CBox( const FT_Outline* outline,
  348. FT_BBox *acbox )
  349. {
  350. FT_Pos xMin, yMin, xMax, yMax;
  351. if ( outline && acbox )
  352. {
  353. if ( outline->n_points == 0 )
  354. {
  355. xMin = 0;
  356. yMin = 0;
  357. xMax = 0;
  358. yMax = 0;
  359. }
  360. else
  361. {
  362. FT_Vector* vec = outline->points;
  363. FT_Vector* limit = vec + outline->n_points;
  364. xMin = xMax = vec->x;
  365. yMin = yMax = vec->y;
  366. vec++;
  367. for ( ; vec < limit; vec++ )
  368. {
  369. FT_Pos x, y;
  370. x = vec->x;
  371. if ( x < xMin ) xMin = x;
  372. if ( x > xMax ) xMax = x;
  373. y = vec->y;
  374. if ( y < yMin ) yMin = y;
  375. if ( y > yMax ) yMax = y;
  376. }
  377. }
  378. acbox->xMin = xMin;
  379. acbox->xMax = xMax;
  380. acbox->yMin = yMin;
  381. acbox->yMax = yMax;
  382. }
  383. }
  384. /* documentation is in ftoutln.h */
  385. FT_EXPORT_DEF( void )
  386. FT_Outline_Translate( const FT_Outline* outline,
  387. FT_Pos xOffset,
  388. FT_Pos yOffset )
  389. {
  390. FT_UShort n;
  391. FT_Vector* vec;
  392. if ( !outline )
  393. return;
  394. vec = outline->points;
  395. for ( n = 0; n < outline->n_points; n++ )
  396. {
  397. vec->x += xOffset;
  398. vec->y += yOffset;
  399. vec++;
  400. }
  401. }
  402. /* documentation is in ftoutln.h */
  403. FT_EXPORT_DEF( void )
  404. FT_Outline_Reverse( FT_Outline* outline )
  405. {
  406. FT_UShort n;
  407. FT_Int first, last;
  408. if ( !outline )
  409. return;
  410. first = 0;
  411. for ( n = 0; n < outline->n_contours; n++ )
  412. {
  413. last = outline->contours[n];
  414. /* reverse point table */
  415. {
  416. FT_Vector* p = outline->points + first;
  417. FT_Vector* q = outline->points + last;
  418. FT_Vector swap;
  419. while ( p < q )
  420. {
  421. swap = *p;
  422. *p = *q;
  423. *q = swap;
  424. p++;
  425. q--;
  426. }
  427. }
  428. /* reverse tags table */
  429. {
  430. char* p = outline->tags + first;
  431. char* q = outline->tags + last;
  432. char swap;
  433. while ( p < q )
  434. {
  435. swap = *p;
  436. *p = *q;
  437. *q = swap;
  438. p++;
  439. q--;
  440. }
  441. }
  442. first = last + 1;
  443. }
  444. outline->flags ^= FT_OUTLINE_REVERSE_FILL;
  445. }
  446. /* documentation is in ftoutln.h */
  447. FT_EXPORT_DEF( FT_Error )
  448. FT_Outline_Render( FT_Library library,
  449. FT_Outline* outline,
  450. FT_Raster_Params* params )
  451. {
  452. FT_Error error;
  453. FT_Bool update = FALSE;
  454. FT_Renderer renderer;
  455. FT_ListNode node;
  456. if ( !library )
  457. return FT_Err_Invalid_Library_Handle;
  458. if ( !outline || !params )
  459. return FT_Err_Invalid_Argument;
  460. renderer = library->cur_renderer;
  461. node = library->renderers.head;
  462. params->source = (void*)outline;
  463. error = FT_Err_Cannot_Render_Glyph;
  464. while ( renderer )
  465. {
  466. error = renderer->raster_render( renderer->raster, params );
  467. if ( !error || FT_ERROR_BASE( error ) != FT_Err_Cannot_Render_Glyph )
  468. break;
  469. /* FT_Err_Cannot_Render_Glyph is returned if the render mode */
  470. /* is unsupported by the current renderer for this glyph image */
  471. /* format */
  472. /* now, look for another renderer that supports the same */
  473. /* format */
  474. renderer = FT_Lookup_Renderer( library, FT_GLYPH_FORMAT_OUTLINE,
  475. &node );
  476. update = TRUE;
  477. }
  478. /* if we changed the current renderer for the glyph image format */
  479. /* we need to select it as the next current one */
  480. if ( !error && update && renderer )
  481. FT_Set_Renderer( library, renderer, 0, 0 );
  482. return error;
  483. }
  484. /* documentation is in ftoutln.h */
  485. FT_EXPORT_DEF( FT_Error )
  486. FT_Outline_Get_Bitmap( FT_Library library,
  487. FT_Outline* outline,
  488. const FT_Bitmap *abitmap )
  489. {
  490. FT_Raster_Params params;
  491. if ( !abitmap )
  492. return FT_Err_Invalid_Argument;
  493. /* other checks are delayed to FT_Outline_Render() */
  494. params.target = abitmap;
  495. params.flags = 0;
  496. if ( abitmap->pixel_mode == FT_PIXEL_MODE_GRAY ||
  497. abitmap->pixel_mode == FT_PIXEL_MODE_LCD ||
  498. abitmap->pixel_mode == FT_PIXEL_MODE_LCD_V )
  499. params.flags |= FT_RASTER_FLAG_AA;
  500. return FT_Outline_Render( library, outline, &params );
  501. }
  502. /* documentation is in freetype.h */
  503. FT_EXPORT_DEF( void )
  504. FT_Vector_Transform( FT_Vector* vector,
  505. const FT_Matrix* matrix )
  506. {
  507. FT_Pos xz, yz;
  508. if ( !vector || !matrix )
  509. return;
  510. xz = FT_MulFix( vector->x, matrix->xx ) +
  511. FT_MulFix( vector->y, matrix->xy );
  512. yz = FT_MulFix( vector->x, matrix->yx ) +
  513. FT_MulFix( vector->y, matrix->yy );
  514. vector->x = xz;
  515. vector->y = yz;
  516. }
  517. /* documentation is in ftoutln.h */
  518. FT_EXPORT_DEF( void )
  519. FT_Outline_Transform( const FT_Outline* outline,
  520. const FT_Matrix* matrix )
  521. {
  522. FT_Vector* vec;
  523. FT_Vector* limit;
  524. if ( !outline || !matrix )
  525. return;
  526. vec = outline->points;
  527. limit = vec + outline->n_points;
  528. for ( ; vec < limit; vec++ )
  529. FT_Vector_Transform( vec, matrix );
  530. }
  531. #if 0
  532. #define FT_OUTLINE_GET_CONTOUR( outline, c, first, last ) \
  533. do { \
  534. (first) = ( c > 0 ) ? (outline)->points + \
  535. (outline)->contours[c - 1] + 1 \
  536. : (outline)->points; \
  537. (last) = (outline)->points + (outline)->contours[c]; \
  538. } while ( 0 )
  539. /* Is a point in some contour? */
  540. /* */
  541. /* We treat every point of the contour as if it */
  542. /* it were ON. That is, we allow false positives, */
  543. /* but disallow false negatives. (XXX really?) */
  544. static FT_Bool
  545. ft_contour_has( FT_Outline* outline,
  546. FT_Short c,
  547. FT_Vector* point )
  548. {
  549. FT_Vector* first;
  550. FT_Vector* last;
  551. FT_Vector* a;
  552. FT_Vector* b;
  553. FT_UInt n = 0;
  554. FT_OUTLINE_GET_CONTOUR( outline, c, first, last );
  555. for ( a = first; a <= last; a++ )
  556. {
  557. FT_Pos x;
  558. FT_Int intersect;
  559. b = ( a == last ) ? first : a + 1;
  560. intersect = ( a->y - point->y ) ^ ( b->y - point->y );
  561. /* a and b are on the same side */
  562. if ( intersect >= 0 )
  563. {
  564. if ( intersect == 0 && a->y == point->y )
  565. {
  566. if ( ( a->x <= point->x && b->x >= point->x ) ||
  567. ( a->x >= point->x && b->x <= point->x ) )
  568. return 1;
  569. }
  570. continue;
  571. }
  572. x = a->x + ( b->x - a->x ) * (point->y - a->y ) / ( b->y - a->y );
  573. if ( x < point->x )
  574. n++;
  575. else if ( x == point->x )
  576. return 1;
  577. }
  578. return ( n % 2 );
  579. }
  580. static FT_Bool
  581. ft_contour_enclosed( FT_Outline* outline,
  582. FT_UShort c )
  583. {
  584. FT_Vector* first;
  585. FT_Vector* last;
  586. FT_Short i;
  587. FT_OUTLINE_GET_CONTOUR( outline, c, first, last );
  588. for ( i = 0; i < outline->n_contours; i++ )
  589. {
  590. if ( i != c && ft_contour_has( outline, i, first ) )
  591. {
  592. FT_Vector* pt;
  593. for ( pt = first + 1; pt <= last; pt++ )
  594. if ( !ft_contour_has( outline, i, pt ) )
  595. return 0;
  596. return 1;
  597. }
  598. }
  599. return 0;
  600. }
  601. /* This version differs from the public one in that each */
  602. /* part (contour not enclosed in another contour) of the */
  603. /* outline is checked for orientation. This is */
  604. /* necessary for some buggy CJK fonts. */
  605. static FT_Orientation
  606. ft_outline_get_orientation( FT_Outline* outline )
  607. {
  608. FT_Short i;
  609. FT_Vector* first;
  610. FT_Vector* last;
  611. FT_Orientation orient = FT_ORIENTATION_NONE;
  612. first = outline->points;
  613. for ( i = 0; i < outline->n_contours; i++, first = last + 1 )
  614. {
  615. FT_Vector* point;
  616. FT_Vector* xmin_point;
  617. FT_Pos xmin;
  618. last = outline->points + outline->contours[i];
  619. /* skip degenerate contours */
  620. if ( last < first + 2 )
  621. continue;
  622. if ( ft_contour_enclosed( outline, i ) )
  623. continue;
  624. xmin = first->x;
  625. xmin_point = first;
  626. for ( point = first + 1; point <= last; point++ )
  627. {
  628. if ( point->x < xmin )
  629. {
  630. xmin = point->x;
  631. xmin_point = point;
  632. }
  633. }
  634. /* check the orientation of the contour */
  635. {
  636. FT_Vector* prev;
  637. FT_Vector* next;
  638. FT_Orientation o;
  639. prev = ( xmin_point == first ) ? last : xmin_point - 1;
  640. next = ( xmin_point == last ) ? first : xmin_point + 1;
  641. if ( FT_Atan2( prev->x - xmin_point->x, prev->y - xmin_point->y ) >
  642. FT_Atan2( next->x - xmin_point->x, next->y - xmin_point->y ) )
  643. o = FT_ORIENTATION_POSTSCRIPT;
  644. else
  645. o = FT_ORIENTATION_TRUETYPE;
  646. if ( orient == FT_ORIENTATION_NONE )
  647. orient = o;
  648. else if ( orient != o )
  649. return FT_ORIENTATION_NONE;
  650. }
  651. }
  652. return orient;
  653. }
  654. #endif /* 0 */
  655. /* documentation is in ftoutln.h */
  656. FT_EXPORT_DEF( FT_Error )
  657. FT_Outline_Embolden( FT_Outline* outline,
  658. FT_Pos strength )
  659. {
  660. FT_Vector* points;
  661. FT_Vector v_prev, v_first, v_next, v_cur;
  662. FT_Angle rotate, angle_in, angle_out;
  663. FT_Int c, n, first;
  664. FT_Int orientation;
  665. if ( !outline )
  666. return FT_Err_Invalid_Argument;
  667. strength /= 2;
  668. if ( strength == 0 )
  669. return FT_Err_Ok;
  670. orientation = FT_Outline_Get_Orientation( outline );
  671. if ( orientation == FT_ORIENTATION_NONE )
  672. {
  673. if ( outline->n_contours )
  674. return FT_Err_Invalid_Argument;
  675. else
  676. return FT_Err_Ok;
  677. }
  678. if ( orientation == FT_ORIENTATION_TRUETYPE )
  679. rotate = -FT_ANGLE_PI2;
  680. else
  681. rotate = FT_ANGLE_PI2;
  682. points = outline->points;
  683. first = 0;
  684. for ( c = 0; c < outline->n_contours; c++ )
  685. {
  686. int last = outline->contours[c];
  687. v_first = points[first];
  688. v_prev = points[last];
  689. v_cur = v_first;
  690. for ( n = first; n <= last; n++ )
  691. {
  692. FT_Vector in, out;
  693. FT_Angle angle_diff;
  694. FT_Pos d;
  695. FT_Fixed scale;
  696. if ( n < last )
  697. v_next = points[n + 1];
  698. else
  699. v_next = v_first;
  700. /* compute the in and out vectors */
  701. in.x = v_cur.x - v_prev.x;
  702. in.y = v_cur.y - v_prev.y;
  703. out.x = v_next.x - v_cur.x;
  704. out.y = v_next.y - v_cur.y;
  705. angle_in = FT_Atan2( in.x, in.y );
  706. angle_out = FT_Atan2( out.x, out.y );
  707. angle_diff = FT_Angle_Diff( angle_in, angle_out );
  708. scale = FT_Cos( angle_diff / 2 );
  709. if ( scale < 0x4000L && scale > -0x4000L )
  710. in.x = in.y = 0;
  711. else
  712. {
  713. d = FT_DivFix( strength, scale );
  714. FT_Vector_From_Polar( &in, d, angle_in + angle_diff / 2 - rotate );
  715. }
  716. outline->points[n].x = v_cur.x + strength + in.x;
  717. outline->points[n].y = v_cur.y + strength + in.y;
  718. v_prev = v_cur;
  719. v_cur = v_next;
  720. }
  721. first = last + 1;
  722. }
  723. return FT_Err_Ok;
  724. }
  725. /* documentation is in ftoutln.h */
  726. FT_EXPORT_DEF( FT_Orientation )
  727. FT_Outline_Get_Orientation( FT_Outline* outline )
  728. {
  729. FT_Pos xmin = 32768L;
  730. FT_Pos xmin_ymin = 32768L;
  731. FT_Pos xmin_ymax = -32768L;
  732. FT_Vector* xmin_first = NULL;
  733. FT_Vector* xmin_last = NULL;
  734. short* contour;
  735. FT_Vector* first;
  736. FT_Vector* last;
  737. FT_Vector* prev;
  738. FT_Vector* point;
  739. int i;
  740. FT_Pos ray_y[3];
  741. FT_Orientation result[3] =
  742. { FT_ORIENTATION_NONE, FT_ORIENTATION_NONE, FT_ORIENTATION_NONE };
  743. if ( !outline || outline->n_points <= 0 )
  744. return FT_ORIENTATION_TRUETYPE;
  745. /* We use the nonzero winding rule to find the orientation. */
  746. /* Since glyph outlines behave much more `regular' than arbitrary */
  747. /* cubic or quadratic curves, this test deals with the polygon */
  748. /* only which is spanned up by the control points. */
  749. first = outline->points;
  750. for ( contour = outline->contours;
  751. contour < outline->contours + outline->n_contours;
  752. contour++, first = last + 1 )
  753. {
  754. FT_Pos contour_xmin = 32768L;
  755. FT_Pos contour_xmax = -32768L;
  756. FT_Pos contour_ymin = 32768L;
  757. FT_Pos contour_ymax = -32768L;
  758. last = outline->points + *contour;
  759. /* skip degenerate contours */
  760. if ( last < first + 2 )
  761. continue;
  762. for ( point = first; point <= last; ++point )
  763. {
  764. if ( point->x < contour_xmin )
  765. contour_xmin = point->x;
  766. if ( point->x > contour_xmax )
  767. contour_xmax = point->x;
  768. if ( point->y < contour_ymin )
  769. contour_ymin = point->y;
  770. if ( point->y > contour_ymax )
  771. contour_ymax = point->y;
  772. }
  773. if ( contour_xmin < xmin &&
  774. contour_xmin != contour_xmax &&
  775. contour_ymin != contour_ymax )
  776. {
  777. xmin = contour_xmin;
  778. xmin_ymin = contour_ymin;
  779. xmin_ymax = contour_ymax;
  780. xmin_first = first;
  781. xmin_last = last;
  782. }
  783. }
  784. if ( xmin == 32768L )
  785. return FT_ORIENTATION_TRUETYPE;
  786. ray_y[0] = ( xmin_ymin * 3 + xmin_ymax ) >> 2;
  787. ray_y[1] = ( xmin_ymin + xmin_ymax ) >> 1;
  788. ray_y[2] = ( xmin_ymin + xmin_ymax * 3 ) >> 2;
  789. for ( i = 0; i < 3; i++ )
  790. {
  791. FT_Pos left_x;
  792. FT_Pos right_x;
  793. FT_Vector* left1;
  794. FT_Vector* left2;
  795. FT_Vector* right1;
  796. FT_Vector* right2;
  797. RedoRay:
  798. left_x = 32768L;
  799. right_x = -32768L;
  800. left1 = left2 = right1 = right2 = NULL;
  801. prev = xmin_last;
  802. for ( point = xmin_first; point <= xmin_last; prev = point, ++point )
  803. {
  804. FT_Pos tmp_x;
  805. if ( point->y == ray_y[i] || prev->y == ray_y[i] )
  806. {
  807. ray_y[i]++;
  808. goto RedoRay;
  809. }
  810. if ( ( point->y < ray_y[i] && prev->y < ray_y[i] ) ||
  811. ( point->y > ray_y[i] && prev->y > ray_y[i] ) )
  812. continue;
  813. tmp_x = FT_MulDiv( point->x - prev->x,
  814. ray_y[i] - prev->y,
  815. point->y - prev->y ) + prev->x;
  816. if ( tmp_x < left_x )
  817. {
  818. left_x = tmp_x;
  819. left1 = prev;
  820. left2 = point;
  821. }
  822. if ( tmp_x > right_x )
  823. {
  824. right_x = tmp_x;
  825. right1 = prev;
  826. right2 = point;
  827. }
  828. }
  829. if ( left1 && right1 )
  830. {
  831. if ( left1->y < left2->y && right1->y > right2->y )
  832. result[i] = FT_ORIENTATION_TRUETYPE;
  833. else if ( left1->y > left2->y && right1->y < right2->y )
  834. result[i] = FT_ORIENTATION_POSTSCRIPT;
  835. else
  836. result[i] = FT_ORIENTATION_NONE;
  837. }
  838. }
  839. if ( result[0] != FT_ORIENTATION_NONE &&
  840. ( result[0] == result[1] || result[0] == result[2] ) )
  841. return result[0];
  842. if ( result[1] != FT_ORIENTATION_NONE && result[1] == result[2] )
  843. return result[1];
  844. return FT_ORIENTATION_TRUETYPE;
  845. }
  846. /* END */