Hash :
c918b54f
Author :
Date :
2020-08-18T10:28:16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
#include <freetype/internal/ftobjs.h>
#include <freetype/internal/ftdebug.h>
#include <freetype/fttrigon.h>
#include "ftsdf.h"
#include "ftsdferrs.h"
/**************************************************************************
*
* for tracking used memory
*
*/
/* The memory tracker only works when `FT_DEBUG_MEMORY` is defined; */
/* we need some variables such as `_ft_debug_file`, which aren't */
/* available otherwise. */
#if defined( FT_DEBUG_LEVEL_TRACE ) && defined( FT_DEBUG_MEMORY )
#undef FT_DEBUG_INNER
#undef FT_ASSIGNP_INNER
#define FT_DEBUG_INNER( exp ) ( _ft_debug_file = __FILE__, \
_ft_debug_lineno = line, \
(exp) )
#define FT_ASSIGNP_INNER( p, exp ) ( _ft_debug_file = __FILE__, \
_ft_debug_lineno = line, \
FT_ASSIGNP( p, exp ) )
/* To be used with `FT_Memory::user' in order to track */
/* memory allocations. */
typedef struct SDF_MemoryUser_
{
void* prev_user;
FT_Long total_usage;
} SDF_MemoryUser;
/*
* These functions are used while allocating and deallocating memory.
* They restore the previous user pointer before calling the allocation
* functions.
*/
static FT_Pointer
sdf_alloc( FT_Memory memory,
FT_Long size,
FT_Error* err,
FT_Int line )
{
SDF_MemoryUser* current_user;
FT_Pointer ptr;
FT_Error error;
current_user = (SDF_MemoryUser*)memory->user;
memory->user = current_user->prev_user;
if ( !FT_QALLOC( ptr, size ) )
current_user->total_usage += size;
memory->user = (void*)current_user;
*err = error;
return ptr;
}
static void
sdf_free( FT_Memory memory,
FT_Pointer ptr,
FT_Int line )
{
SDF_MemoryUser* current_user;
current_user = (SDF_MemoryUser*)memory->user;
memory->user = current_user->prev_user;
FT_FREE( ptr );
memory->user = (void*)current_user;
}
#define SDF_ALLOC( ptr, size ) \
( ptr = sdf_alloc( memory, size, \
&error, __LINE__ ), \
error != 0 )
#define SDF_FREE( ptr ) \
sdf_free( memory, ptr, __LINE__ )
#define SDF_MEMORY_TRACKER_DECLARE() SDF_MemoryUser sdf_memory_user
#define SDF_MEMORY_TRACKER_SETUP() \
sdf_memory_user.prev_user = memory->user; \
sdf_memory_user.total_usage = 0; \
memory->user = &sdf_memory_user
#define SDF_MEMORY_TRACKER_DONE() \
memory->user = sdf_memory_user.prev_user; \
\
FT_TRACE0(( "[sdf] sdf_raster_render:" \
" Total memory used = %ld\n", \
sdf_memory_user.total_usage ))
#else /* !FT_DEBUG_LEVEL_TRACE */
#define SDF_ALLOC FT_QALLOC
#define SDF_FREE FT_FREE
#define SDF_MEMORY_TRACKER_DECLARE() FT_DUMMY_STMNT
#define SDF_MEMORY_TRACKER_SETUP() FT_DUMMY_STMNT
#define SDF_MEMORY_TRACKER_DONE() FT_DUMMY_STMNT
#endif /* !FT_DEBUG_LEVEL_TRACE */
/**************************************************************************
*
* definitions
*
*/
/*
* If set to 1, the rasterizer uses Newton-Raphson's method for finding
* the shortest distance from a point to a conic curve.
*
* If set to 0, an analytical method gets used instead, which computes the
* roots of a cubic polynomial to find the shortest distance. However,
* the analytical method can currently underflow; we thus use Newton's
* method by default.
*/
#ifndef USE_NEWTON_FOR_CONIC
#define USE_NEWTON_FOR_CONIC 1
#endif
/*
* The number of intervals a Bezier curve gets sampled and checked to find
* the shortest distance.
*/
#define MAX_NEWTON_DIVISIONS 4
/*
* The number of steps of Newton's iterations in each interval of the
* Bezier curve. Basically, we run Newton's approximation
*
* x -= Q(t) / Q'(t)
*
* for each division to get the shortest distance.
*/
#define MAX_NEWTON_STEPS 4
/*
* The epsilon distance (in 16.16 fractional units) used for corner
* resolving. If the difference of two distances is less than this value
* they will be checked for a corner if they are ambiguous.
*/
#define CORNER_CHECK_EPSILON 32
#if 0
/*
* Coarse grid dimension. Will probably be removed in the future because
* coarse grid optimization is the slowest algorithm.
*/
#define CG_DIMEN 8
#endif
/**************************************************************************
*
* macros
*
*/
#define MUL_26D6( a, b ) ( ( ( a ) * ( b ) ) / 64 )
#define VEC_26D6_DOT( p, q ) ( MUL_26D6( p.x, q.x ) + \
MUL_26D6( p.y, q.y ) )
/**************************************************************************
*
* structures and enums
*
*/
/**************************************************************************
*
* @Struct:
* SDF_TRaster
*
* @Description:
* This struct is used in place of @FT_Raster and is stored within the
* internal FreeType renderer struct. While rasterizing it is passed to
* the @FT_Raster_RenderFunc function, which then can be used however we
* want.
*
* @Fields:
* memory ::
* Used internally to allocate intermediate memory while raterizing.
*
*/
typedef struct SDF_TRaster_
{
FT_Memory memory;
} SDF_TRaster;
/**************************************************************************
*
* @Enum:
* SDF_Edge_Type
*
* @Description:
* Enumeration of all curve types present in fonts.
*
* @Fields:
* SDF_EDGE_UNDEFINED ::
* Undefined edge, simply used to initialize and detect errors.
*
* SDF_EDGE_LINE ::
* Line segment with start and end point.
*
* SDF_EDGE_CONIC ::
* A conic/quadratic Bezier curve with start, end, and one control
* point.
*
* SDF_EDGE_CUBIC ::
* A cubic Bezier curve with start, end, and two control points.
*
*/
typedef enum SDF_Edge_Type_
{
SDF_EDGE_UNDEFINED = 0,
SDF_EDGE_LINE = 1,
SDF_EDGE_CONIC = 2,
SDF_EDGE_CUBIC = 3
} SDF_Edge_Type;
/**************************************************************************
*
* @Enum:
* SDF_Contour_Orientation
*
* @Description:
* Enumeration of all orientation values of a contour. We determine the
* orientation by calculating the area covered by a contour. Contrary
* to values returned by @FT_Outline_Get_Orientation,
* `SDF_Contour_Orientation` is independent of the fill rule, which can
* be different for different font formats.
*
* @Fields:
* SDF_ORIENTATION_NONE ::
* Undefined orientation, used for initialization and error detection.
*
* SDF_ORIENTATION_CW ::
* Clockwise orientation (positive area covered).
*
* SDF_ORIENTATION_ACW ::
* Anti-clockwise orientation (negative area covered).
*
* @Note:
* See @FT_Outline_Get_Orientation for more details.
*
*/
typedef enum SDF_Contour_Orientation_
{
SDF_ORIENTATION_NONE = 0,
SDF_ORIENTATION_CW = 1,
SDF_ORIENTATION_ACW = 2
} SDF_Contour_Orientation;
/**************************************************************************
*
* @Struct:
* SDF_Edge
*
* @Description:
* Represent an edge of a contour.
*
* @Fields:
* start_pos ::
* Start position of an edge. Valid for all types of edges.
*
* end_pos ::
* Etart position of an edge. Valid for all types of edges.
*
* control_a ::
* A control point of the edge. Valid only for `SDF_EDGE_CONIC`
* and `SDF_EDGE_CUBIC`.
*
* control_b ::
* Another control point of the edge. Valid only for
* `SDF_EDGE_CONIC`.
*
* edge_type ::
* Type of the edge, see @SDF_Edge_Type for all possible edge types.
*
* next ::
* Used to create a singly linked list, which can be interpreted
* as a contour.
*
*/
typedef struct SDF_Edge_
{
FT_26D6_Vec start_pos;
FT_26D6_Vec end_pos;
FT_26D6_Vec control_a;
FT_26D6_Vec control_b;
SDF_Edge_Type edge_type;
struct SDF_Edge_* next;
} SDF_Edge;
/**************************************************************************
*
* @Struct:
* SDF_Contour
*
* @Description:
* Represent a complete contour, which contains a list of edges.
*
* @Fields:
* last_pos ::
* Contains the value of `end_pos' of the last edge in the list of
* edges. Useful while decomposing the outline with
* @FT_Outline_Decompose.
*
* edges ::
* Linked list of all the edges that make the contour.
*
* next ::
* Used to create a singly linked list, which can be interpreted as a
* complete shape or @FT_Outline.
*
*/
typedef struct SDF_Contour_
{
FT_26D6_Vec last_pos;
SDF_Edge* edges;
struct SDF_Contour_* next;
} SDF_Contour;
/**************************************************************************
*
* @Struct:
* SDF_Shape
*
* @Description:
* Represent a complete shape, which is the decomposition of
* @FT_Outline.
*
* @Fields:
* memory ::
* Used internally to allocate memory.
*
* contours ::
* Linked list of all the contours that make the shape.
*
*/
typedef struct SDF_Shape_
{
FT_Memory memory;
SDF_Contour* contours;
} SDF_Shape;
/**************************************************************************
*
* @Struct:
* SDF_Signed_Distance
*
* @Description:
* Represent signed distance of a point, i.e., the distance of the edge
* nearest to the point.
*
* @Fields:
* distance ::
* Distance of the point from the nearest edge. Can be squared or
* absolute depending on the `USE_SQUARED_DISTANCES` macro defined in
* file `ftsdfcommon.h`.
*
* cross ::
* Cross product of the shortest distance vector (i.e., the vector
* from the point to the nearest edge) and the direction of the edge
* at the nearest point. This is used to resolve ambiguities of
* `sign`.
*
* sign ::
* A value used to indicate whether the distance vector is outside or
* inside the contour corresponding to the edge.
*
* @Note:
* `sign` may or may not be correct, therefore it must be checked
* properly in case there is an ambiguity.
*
*/
typedef struct SDF_Signed_Distance_
{
FT_16D16 distance;
FT_16D16 cross;
FT_Char sign;
} SDF_Signed_Distance;
/**************************************************************************
*
* @Struct:
* SDF_Params
*
* @Description:
* Yet another internal parameters required by the rasterizer.
*
* @Fields:
* orientation ::
* This is not the @SDF_Contour_Orientation value but @FT_Orientation,
* which determines whether clockwise-oriented outlines are to be
* filled or anti-clockwise-oriented ones.
*
* flip_sign ::
* If set to true, flip the sign. By default the points filled by the
* outline are positive.
*
* flip_y ::
* If set to true the output bitmap is upside-down. Can be useful
* because OpenGL and DirectX use different coordinate systems for
* textures.
*
* overload_sign ::
* In the subdivision and bounding box optimization, the default
* outside sign is taken as -1. This parameter can be used to modify
* that behaviour. For example, while generating SDF for a single
* counter-clockwise contour, the outside sign should be 1.
*
*/
typedef struct SDF_Params_
{
FT_Orientation orientation;
FT_Bool flip_sign;
FT_Bool flip_y;
FT_Int overload_sign;
} SDF_Params;
/**************************************************************************
*
* constants, initializer, and destructor
*
*/
static
const FT_Vector zero_vector = { 0, 0 };
static
const SDF_Edge null_edge = { { 0, 0 }, { 0, 0 },
{ 0, 0 }, { 0, 0 },
SDF_EDGE_UNDEFINED, NULL };
static
const SDF_Contour null_contour = { { 0, 0 }, NULL, NULL };
static
const SDF_Shape null_shape = { NULL, NULL };
static
const SDF_Signed_Distance max_sdf = { INT_MAX, 0, 0 };
/* Create a new @SDF_Edge on the heap and assigns the `edge` */
/* pointer to the newly allocated memory. */
static FT_Error
sdf_edge_new( FT_Memory memory,
SDF_Edge** edge )
{
FT_Error error = FT_Err_Ok;
SDF_Edge* ptr = NULL;
if ( !memory || !edge )
{
error = FT_THROW( Invalid_Argument );
goto Exit;
}
if ( !SDF_ALLOC( ptr, sizeof ( *ptr ) ) )
{
*ptr = null_edge;
*edge = ptr;
}
Exit:
return error;
}
/* Free the allocated `edge` variable. */
static void
sdf_edge_done( FT_Memory memory,
SDF_Edge** edge )
{
if ( !memory || !edge || !*edge )
return;
SDF_FREE( *edge );
}
/* Create a new @SDF_Contour on the heap and assign */
/* the `contour` pointer to the newly allocated memory. */
static FT_Error
sdf_contour_new( FT_Memory memory,
SDF_Contour** contour )
{
FT_Error error = FT_Err_Ok;
SDF_Contour* ptr = NULL;
if ( !memory || !contour )
{
error = FT_THROW( Invalid_Argument );
goto Exit;
}
if ( !SDF_ALLOC( ptr, sizeof ( *ptr ) ) )
{
*ptr = null_contour;
*contour = ptr;
}
Exit:
return error;
}
/* Free the allocated `contour` variable. */
/* Also free the list of edges. */
static void
sdf_contour_done( FT_Memory memory,
SDF_Contour** contour )
{
SDF_Edge* edges;
SDF_Edge* temp;
if ( !memory || !contour || !*contour )
return;
edges = (*contour)->edges;
/* release all edges */
while ( edges )
{
temp = edges;
edges = edges->next;
sdf_edge_done( memory, &temp );
}
SDF_FREE( *contour );
}
/* Create a new @SDF_Shape on the heap and assign */
/* the `shape` pointer to the newly allocated memory. */
static FT_Error
sdf_shape_new( FT_Memory memory,
SDF_Shape** shape )
{
FT_Error error = FT_Err_Ok;
SDF_Shape* ptr = NULL;
if ( !memory || !shape )
{
error = FT_THROW( Invalid_Argument );
goto Exit;
}
if ( !SDF_ALLOC( ptr, sizeof ( *ptr ) ) )
{
*ptr = null_shape;
ptr->memory = memory;
*shape = ptr;
}
Exit:
return error;
}
/* Free the allocated `shape` variable. */
/* Also free the list of contours. */
static void
sdf_shape_done( SDF_Shape** shape )
{
FT_Memory memory;
SDF_Contour* contours;
SDF_Contour* temp;
if ( !shape || !*shape )
return;
memory = (*shape)->memory;
contours = (*shape)->contours;
if ( !memory )
return;
/* release all contours */
while ( contours )
{
temp = contours;
contours = contours->next;
sdf_contour_done( memory, &temp );
}
/* release the allocated shape struct */
SDF_FREE( *shape );
}
/**************************************************************************
*
* shape decomposition functions
*
*/
/* This function is called when starting a new contour at `to`, */
/* which gets added to the shape's list. */
static FT_Error
sdf_move_to( const FT_26D6_Vec* to,
void* user )
{
SDF_Shape* shape = ( SDF_Shape* )user;
SDF_Contour* contour = NULL;
FT_Error error = FT_Err_Ok;
FT_Memory memory = shape->memory;
if ( !to || !user )
{
error = FT_THROW( Invalid_Argument );
goto Exit;
}
FT_CALL( sdf_contour_new( memory, &contour ) );
contour->last_pos = *to;
contour->next = shape->contours;
shape->contours = contour;
Exit:
return error;
}
/* This function is called when there is a line in the */
/* contour. The line starts at the previous edge point and */
/* stops at `to`. */
static FT_Error
sdf_line_to( const FT_26D6_Vec* to,
void* user )
{
SDF_Shape* shape = ( SDF_Shape* )user;
SDF_Edge* edge = NULL;
SDF_Contour* contour = NULL;
FT_Error error = FT_Err_Ok;
FT_Memory memory = shape->memory;
if ( !to || !user )
{
error = FT_THROW( Invalid_Argument );
goto Exit;
}
contour = shape->contours;
if ( contour->last_pos.x == to->x &&
contour->last_pos.y == to->y )
goto Exit;
FT_CALL( sdf_edge_new( memory, &edge ) );
edge->edge_type = SDF_EDGE_LINE;
edge->start_pos = contour->last_pos;
edge->end_pos = *to;
edge->next = contour->edges;
contour->edges = edge;
contour->last_pos = *to;
Exit:
return error;
}
/* This function is called when there is a conic Bezier curve */
/* in the contour. The curve starts at the previous edge point */
/* and stops at `to`, with control point `control_1`. */
static FT_Error
sdf_conic_to( const FT_26D6_Vec* control_1,
const FT_26D6_Vec* to,
void* user )
{
SDF_Shape* shape = ( SDF_Shape* )user;
SDF_Edge* edge = NULL;
SDF_Contour* contour = NULL;
FT_Error error = FT_Err_Ok;
FT_Memory memory = shape->memory;
if ( !control_1 || !to || !user )
{
error = FT_THROW( Invalid_Argument );
goto Exit;
}
contour = shape->contours;
FT_CALL( sdf_edge_new( memory, &edge ) );
edge->edge_type = SDF_EDGE_CONIC;
edge->start_pos = contour->last_pos;
edge->control_a = *control_1;
edge->end_pos = *to;
edge->next = contour->edges;
contour->edges = edge;
contour->last_pos = *to;
Exit:
return error;
}
/* This function is called when there is a cubic Bezier curve */
/* in the contour. The curve starts at the previous edge point */
/* and stops at `to`, with two control points `control_1` and */
/* `control_2`. */
static FT_Error
sdf_cubic_to( const FT_26D6_Vec* control_1,
const FT_26D6_Vec* control_2,
const FT_26D6_Vec* to,
void* user )
{
SDF_Shape* shape = ( SDF_Shape* )user;
SDF_Edge* edge = NULL;
SDF_Contour* contour = NULL;
FT_Error error = FT_Err_Ok;
FT_Memory memory = shape->memory;
if ( !control_2 || !control_1 || !to || !user )
{
error = FT_THROW( Invalid_Argument );
goto Exit;
}
contour = shape->contours;
FT_CALL( sdf_edge_new( memory, &edge ) );
edge->edge_type = SDF_EDGE_CUBIC;
edge->start_pos = contour->last_pos;
edge->control_a = *control_1;
edge->control_b = *control_2;
edge->end_pos = *to;
edge->next = contour->edges;
contour->edges = edge;
contour->last_pos = *to;
Exit:
return error;
}
/* Construct the structure to hold all four outline */
/* decomposition functions. */
FT_DEFINE_OUTLINE_FUNCS(
sdf_decompose_funcs,
(FT_Outline_MoveTo_Func) sdf_move_to, /* move_to */
(FT_Outline_LineTo_Func) sdf_line_to, /* line_to */
(FT_Outline_ConicTo_Func)sdf_conic_to, /* conic_to */
(FT_Outline_CubicTo_Func)sdf_cubic_to, /* cubic_to */
0, /* shift */
0 /* delta */
)
/* Decompose `outline` and put it into the `shape` structure. */
static FT_Error
sdf_outline_decompose( FT_Outline* outline,
SDF_Shape* shape )
{
FT_Error error = FT_Err_Ok;
if ( !outline || !shape )
{
error = FT_THROW( Invalid_Argument );
goto Exit;
}
error = FT_Outline_Decompose( outline,
&sdf_decompose_funcs,
(void*)shape );
Exit:
return error;
}
/**************************************************************************
*
* utility functions
*
*/
/* Return the control box of a edge. The control box is a rectangle */
/* in which all the control points can fit tightly. */
static FT_CBox
get_control_box( SDF_Edge edge )
{
FT_CBox cbox;
FT_Bool is_set = 0;
switch ( edge.edge_type )
{
case SDF_EDGE_CUBIC:
cbox.xMin = edge.control_b.x;
cbox.xMax = edge.control_b.x;
cbox.yMin = edge.control_b.y;
cbox.yMax = edge.control_b.y;
is_set = 1;
/* fall through */
case SDF_EDGE_CONIC:
if ( is_set )
{
cbox.xMin = edge.control_a.x < cbox.xMin
? edge.control_a.x
: cbox.xMin;
cbox.xMax = edge.control_a.x > cbox.xMax
? edge.control_a.x
: cbox.xMax;
cbox.yMin = edge.control_a.y < cbox.yMin
? edge.control_a.y
: cbox.yMin;
cbox.yMax = edge.control_a.y > cbox.yMax
? edge.control_a.y
: cbox.yMax;
}
else
{
cbox.xMin = edge.control_a.x;
cbox.xMax = edge.control_a.x;
cbox.yMin = edge.control_a.y;
cbox.yMax = edge.control_a.y;
is_set = 1;
}
/* fall through */
case SDF_EDGE_LINE:
if ( is_set )
{
cbox.xMin = edge.start_pos.x < cbox.xMin
? edge.start_pos.x
: cbox.xMin;
cbox.xMax = edge.start_pos.x > cbox.xMax
? edge.start_pos.x
: cbox.xMax;
cbox.yMin = edge.start_pos.y < cbox.yMin
? edge.start_pos.y
: cbox.yMin;
cbox.yMax = edge.start_pos.y > cbox.yMax
? edge.start_pos.y
: cbox.yMax;
}
else
{
cbox.xMin = edge.start_pos.x;
cbox.xMax = edge.start_pos.x;
cbox.yMin = edge.start_pos.y;
cbox.yMax = edge.start_pos.y;
}
cbox.xMin = edge.end_pos.x < cbox.xMin
? edge.end_pos.x
: cbox.xMin;
cbox.xMax = edge.end_pos.x > cbox.xMax
? edge.end_pos.x
: cbox.xMax;
cbox.yMin = edge.end_pos.y < cbox.yMin
? edge.end_pos.y
: cbox.yMin;
cbox.yMax = edge.end_pos.y > cbox.yMax
? edge.end_pos.y
: cbox.yMax;
break;
default:
break;
}
return cbox;
}
/* Return orientation of a single contour. */
/* Note that the orientation is independent of the fill rule! */
/* So, for TTF a clockwise-oriented contour has to be filled */
/* and the opposite for OTF fonts. */
static SDF_Contour_Orientation
get_contour_orientation ( SDF_Contour* contour )
{
SDF_Edge* head = NULL;
FT_26D6 area = 0;
/* return none if invalid parameters */
if ( !contour || !contour->edges )
return SDF_ORIENTATION_NONE;
head = contour->edges;
/* Calculate the area of the control box for all edges. */
while ( head )
{
switch ( head->edge_type )
{
case SDF_EDGE_LINE:
area += MUL_26D6( ( head->end_pos.x - head->start_pos.x ),
( head->end_pos.y + head->start_pos.y ) );
break;
case SDF_EDGE_CONIC:
area += MUL_26D6( head->control_a.x - head->start_pos.x,
head->control_a.y + head->start_pos.y );
area += MUL_26D6( head->end_pos.x - head->control_a.x,
head->end_pos.y + head->control_a.y );
break;
case SDF_EDGE_CUBIC:
area += MUL_26D6( head->control_a.x - head->start_pos.x,
head->control_a.y + head->start_pos.y );
area += MUL_26D6( head->control_b.x - head->control_a.x,
head->control_b.y + head->control_a.y );
area += MUL_26D6( head->end_pos.x - head->control_b.x,
head->end_pos.y + head->control_b.y );
break;
default:
return SDF_ORIENTATION_NONE;
}
head = head->next;
}
/* Clockwise contours cover a positive area, and anti-clockwise */
/* contours cover a negative area. */
if ( area > 0 )
return SDF_ORIENTATION_CW;
else
return SDF_ORIENTATION_ACW;
}
/* This function is exactly the same as the one */
/* in the smooth renderer. It splits a conic */
/* into two conics exactly half way at t = 0.5. */
static void
split_conic( FT_26D6_Vec* base )
{
FT_26D6 a, b;
base[4].x = base[2].x;
a = base[0].x + base[1].x;
b = base[1].x + base[2].x;
base[3].x = b / 2;
base[2].x = ( a + b ) / 4;
base[1].x = a / 2;
base[4].y = base[2].y;
a = base[0].y + base[1].y;
b = base[1].y + base[2].y;
base[3].y = b / 2;
base[2].y = ( a + b ) / 4;
base[1].y = a / 2;
}
/* This function is exactly the same as the one */
/* in the smooth renderer. It splits a cubic */
/* into two cubics exactly half way at t = 0.5. */
static void
split_cubic( FT_26D6_Vec* base )
{
FT_26D6 a, b, c;
base[6].x = base[3].x;
a = base[0].x + base[1].x;
b = base[1].x + base[2].x;
c = base[2].x + base[3].x;
base[5].x = c / 2;
c += b;
base[4].x = c / 4;
base[1].x = a / 2;
a += b;
base[2].x = a / 4;
base[3].x = ( a + c ) / 8;
base[6].y = base[3].y;
a = base[0].y + base[1].y;
b = base[1].y + base[2].y;
c = base[2].y + base[3].y;
base[5].y = c / 2;
c += b;
base[4].y = c / 4;
base[1].y = a / 2;
a += b;
base[2].y = a / 4;
base[3].y = ( a + c ) / 8;
}
/* Split a conic Bezier curve into a number of lines */
/* and add them to `out'. */
/* */
/* This function uses recursion; we thus need */
/* parameter `max_splits' for stopping. */
static FT_Error
split_sdf_conic( FT_Memory memory,
FT_26D6_Vec* control_points,
FT_Int max_splits,
SDF_Edge** out )
{
FT_Error error = FT_Err_Ok;
FT_26D6_Vec cpos[5];
SDF_Edge* left,* right;
if ( !memory || !out )
{
error = FT_THROW( Invalid_Argument );
goto Exit;
}
/* split conic outline */
cpos[0] = control_points[0];
cpos[1] = control_points[1];
cpos[2] = control_points[2];
split_conic( cpos );
/* If max number of splits is done */
/* then stop and add the lines to */
/* the list. */
if ( max_splits <= 2 )
goto Append;
/* Otherwise keep splitting. */
FT_CALL( split_sdf_conic( memory, &cpos[0], max_splits / 2, out ) );
FT_CALL( split_sdf_conic( memory, &cpos[2], max_splits / 2, out ) );
/* [NOTE]: This is not an efficient way of */
/* splitting the curve. Check the deviation */
/* instead and stop if the deviation is less */
/* than a pixel. */
goto Exit;
Append:
/* Do allocation and add the lines to the list. */
FT_CALL( sdf_edge_new( memory, &left ) );
FT_CALL( sdf_edge_new( memory, &right ) );
left->start_pos = cpos[0];
left->end_pos = cpos[2];
left->edge_type = SDF_EDGE_LINE;
right->start_pos = cpos[2];
right->end_pos = cpos[4];
right->edge_type = SDF_EDGE_LINE;
left->next = right;
right->next = (*out);
*out = left;
Exit:
return error;
}
/* Split a cubic Bezier curve into a number of lines */
/* and add them to `out`. */
/* */
/* This function uses recursion; we thus need */
/* parameter `max_splits' for stopping. */
static FT_Error
split_sdf_cubic( FT_Memory memory,
FT_26D6_Vec* control_points,
FT_Int max_splits,
SDF_Edge** out )
{
FT_Error error = FT_Err_Ok;
FT_26D6_Vec cpos[7];
SDF_Edge* left,* right;
if ( !memory || !out )
{
error = FT_THROW( Invalid_Argument );
goto Exit;
}
/* split the conic */
cpos[0] = control_points[0];
cpos[1] = control_points[1];
cpos[2] = control_points[2];
cpos[3] = control_points[3];
split_cubic( cpos );
/* If max number of splits is done */
/* then stop and add the lines to */
/* the list. */
if ( max_splits <= 2 )
goto Append;
/* Otherwise keep splitting. */
FT_CALL( split_sdf_cubic( memory, &cpos[0], max_splits / 2, out ) );
FT_CALL( split_sdf_cubic( memory, &cpos[3], max_splits / 2, out ) );
/* [NOTE]: This is not an efficient way of */
/* splitting the curve. Check the deviation */
/* instead and stop if the deviation is less */
/* than a pixel. */
goto Exit;
Append:
/* Do allocation and add the lines to the list. */
FT_CALL( sdf_edge_new( memory, &left) );
FT_CALL( sdf_edge_new( memory, &right) );
left->start_pos = cpos[0];
left->end_pos = cpos[3];
left->edge_type = SDF_EDGE_LINE;
right->start_pos = cpos[3];
right->end_pos = cpos[6];
right->edge_type = SDF_EDGE_LINE;
left->next = right;
right->next = (*out);
*out = left;
Exit:
return error;
}
/* Subdivide an entire shape into line segments */
/* such that it doesn't look visually different */
/* from the original curve. */
static FT_Error
split_sdf_shape( SDF_Shape* shape )
{
FT_Error error = FT_Err_Ok;
FT_Memory memory;
SDF_Contour* contours;
SDF_Contour* new_contours = NULL;
if ( !shape || !shape->memory )
{
error = FT_THROW( Invalid_Argument );
goto Exit;
}
contours = shape->contours;
memory = shape->memory;
/* for each contour */
while ( contours )
{
SDF_Edge* edges = contours->edges;
SDF_Edge* new_edges = NULL;
SDF_Contour* tempc;
/* for each edge */
while ( edges )
{
SDF_Edge* edge = edges;
SDF_Edge* temp;
switch ( edge->edge_type )
{
case SDF_EDGE_LINE:
/* Just create a duplicate edge in case */
/* it is a line. We can use the same edge. */
FT_CALL( sdf_edge_new( memory, &temp ) );
ft_memcpy( temp, edge, sizeof ( *edge ) );
temp->next = new_edges;
new_edges = temp;
break;
case SDF_EDGE_CONIC:
/* Subdivide the curve and add it to the list. */
{
FT_26D6_Vec ctrls[3];
ctrls[0] = edge->start_pos;
ctrls[1] = edge->control_a;
ctrls[2] = edge->end_pos;
error = split_sdf_conic( memory, ctrls, 32, &new_edges );
}
break;
case SDF_EDGE_CUBIC:
/* Subdivide the curve and add it to the list. */
{
FT_26D6_Vec ctrls[4];
ctrls[0] = edge->start_pos;
ctrls[1] = edge->control_a;
ctrls[2] = edge->control_b;
ctrls[3] = edge->end_pos;
error = split_sdf_cubic( memory, ctrls, 32, &new_edges );
}
break;
default:
error = FT_THROW( Invalid_Argument );
goto Exit;
}
edges = edges->next;
}
/* add to the contours list */
FT_CALL( sdf_contour_new( memory, &tempc ) );
tempc->next = new_contours;
tempc->edges = new_edges;
new_contours = tempc;
new_edges = NULL;
/* deallocate the contour */
tempc = contours;
contours = contours->next;
sdf_contour_done( memory, &tempc );
}
shape->contours = new_contours;
Exit:
return error;
}
/**************************************************************************
*
* math functions
*
*/
#if !USE_NEWTON_FOR_CONIC
/* [NOTE]: All the functions below down until rasterizer */
/* can be avoided if we decide to subdivide the */
/* curve into lines. */
/* This function uses Newton's iteration to find */
/* the cube root of a fixed-point integer. */
static FT_16D16
cube_root( FT_16D16 val )
{
/* [IMPORTANT]: This function is not good as it may */
/* not break, so use a lookup table instead. Or we */
/* can use an algorithm similar to `square_root`. */
FT_Int v, g, c;
if ( val == 0 ||
val == -FT_INT_16D16( 1 ) ||
val == FT_INT_16D16( 1 ) )
return val;
v = val < 0 ? -val : val;
g = square_root( v );
c = 0;
while ( 1 )
{
c = FT_MulFix( FT_MulFix( g, g ), g ) - v;
c = FT_DivFix( c, 3 * FT_MulFix( g, g ) );
g -= c;
if ( ( c < 0 ? -c : c ) < 30 )
break;
}
return val < 0 ? -g : g;
}
/* Calculate the perpendicular by using '1 - base^2'. */
/* Then use arctan to compute the angle. */
static FT_16D16
arc_cos( FT_16D16 val )
{
FT_16D16 p;
FT_16D16 b = val;
FT_16D16 one = FT_INT_16D16( 1 );
if ( b > one )
b = one;
if ( b < -one )
b = -one;
p = one - FT_MulFix( b, b );
p = square_root( p );
return FT_Atan2( b, p );
}
/* Compute roots of a quadratic polynomial, assign them to `out`, */
/* and return number of real roots. */
/* */
/* The procedure can be found at */
/* */
/* https://mathworld.wolfram.com/QuadraticFormula.html */
static FT_UShort
solve_quadratic_equation( FT_26D6 a,
FT_26D6 b,
FT_26D6 c,
FT_16D16 out[2] )
{
FT_16D16 discriminant = 0;
a = FT_26D6_16D16( a );
b = FT_26D6_16D16( b );
c = FT_26D6_16D16( c );
if ( a == 0 )
{
if ( b == 0 )
return 0;
else
{
out[0] = FT_DivFix( -c, b );
return 1;
}
}
discriminant = FT_MulFix( b, b ) - 4 * FT_MulFix( a, c );
if ( discriminant < 0 )
return 0;
else if ( discriminant == 0 )
{
out[0] = FT_DivFix( -b, 2 * a );
return 1;
}
else
{
discriminant = square_root( discriminant );
out[0] = FT_DivFix( -b + discriminant, 2 * a );
out[1] = FT_DivFix( -b - discriminant, 2 * a );
return 2;
}
}
/* Compute roots of a cubic polynomial, assign them to `out`, */
/* and return number of real roots. */
/* */
/* The procedure can be found at */
/* */
/* https://mathworld.wolfram.com/CubicFormula.html */
static FT_UShort
solve_cubic_equation( FT_26D6 a,
FT_26D6 b,
FT_26D6 c,
FT_26D6 d,
FT_16D16 out[3] )
{
FT_16D16 q = 0; /* intermediate */
FT_16D16 r = 0; /* intermediate */
FT_16D16 a2 = b; /* x^2 coefficients */
FT_16D16 a1 = c; /* x coefficients */
FT_16D16 a0 = d; /* constant */
FT_16D16 q3 = 0;
FT_16D16 r2 = 0;
FT_16D16 a23 = 0;
FT_16D16 a22 = 0;
FT_16D16 a1x2 = 0;
/* cutoff value for `a` to be a cubic, otherwise solve quadratic */
if ( a == 0 || FT_ABS( a ) < 16 )
return solve_quadratic_equation( b, c, d, out );
if ( d == 0 )
{
out[0] = 0;
return solve_quadratic_equation( a, b, c, out + 1 ) + 1;
}
/* normalize the coefficients; this also makes them 16.16 */
a2 = FT_DivFix( a2, a );
a1 = FT_DivFix( a1, a );
a0 = FT_DivFix( a0, a );
/* compute intermediates */
a1x2 = FT_MulFix( a1, a2 );
a22 = FT_MulFix( a2, a2 );
a23 = FT_MulFix( a22, a2 );
q = ( 3 * a1 - a22 ) / 9;
r = ( 9 * a1x2 - 27 * a0 - 2 * a23 ) / 54;
/* [BUG]: `q3` and `r2` still cause underflow. */
q3 = FT_MulFix( q, q );
q3 = FT_MulFix( q3, q );
r2 = FT_MulFix( r, r );
if ( q3 < 0 && r2 < -q3 )
{
FT_16D16 t = 0;
q3 = square_root( -q3 );
t = FT_DivFix( r, q3 );
if ( t > ( 1 << 16 ) )
t = ( 1 << 16 );
if ( t < -( 1 << 16 ) )
t = -( 1 << 16 );
t = arc_cos( t );
a2 /= 3;
q = 2 * square_root( -q );
out[0] = FT_MulFix( q, FT_Cos( t / 3 ) ) - a2;
out[1] = FT_MulFix( q, FT_Cos( ( t + FT_ANGLE_PI * 2 ) / 3 ) ) - a2;
out[2] = FT_MulFix( q, FT_Cos( ( t + FT_ANGLE_PI * 4 ) / 3 ) ) - a2;
return 3;
}
else if ( r2 == -q3 )
{
FT_16D16 s = 0;
s = cube_root( r );
a2 /= -3;
out[0] = a2 + ( 2 * s );
out[1] = a2 - s;
return 2;
}
else
{
FT_16D16 s = 0;
FT_16D16 t = 0;
FT_16D16 dis = 0;
if ( q3 == 0 )
dis = FT_ABS( r );
else
dis = square_root( q3 + r2 );
s = cube_root( r + dis );
t = cube_root( r - dis );
a2 /= -3;
out[0] = ( a2 + ( s + t ) );
return 1;
}
}
#endif /* !USE_NEWTON_FOR_CONIC */
/*************************************************************************/
/*************************************************************************/
/** **/
/** RASTERIZER **/
/** **/
/*************************************************************************/
/*************************************************************************/
/**************************************************************************
*
* @Function:
* resolve_corner
*
* @Description:
* At some places on the grid two edges can give opposite directions;
* this happens when the closest point is on one of the endpoint. In
* that case we need to check the proper sign.
*
* This can be visualized by an example:
*
* ```
* x
*
* o
* ^ \
* / \
* / \
* (a) / \ (b)
* / \
* / \
* / v
* ```
*
* Suppose `x` is the point whose shortest distance from an arbitrary
* contour we want to find out. It is clear that `o` is the nearest
* point on the contour. Now to determine the sign we do a cross
* product of the shortest distance vector and the edge direction, i.e.,
*
* ```
* => sign = cross(x - o, direction(a))
* ```
*
* Using the right hand thumb rule we can see that the sign will be
* positive.
*
* If we use `b', however, we have
*
* ```
* => sign = cross(x - o, direction(b))
* ```
*
* In this case the sign will be negative. To determine the correct
* sign we thus divide the plane in two halves and check which plane the
* point lies in.
*
* ```
* |
* x |
* |
* o
* ^|\
* / | \
* / | \
* (a) / | \ (b)
* / | \
* / \
* / v
* ```
*
* We can see that `x` lies in the plane of `a`, so we take the sign
* determined by `a`. This test can be easily done by calculating the
* orthogonality and taking the greater one.
*
* The orthogonality is simply the sinus of the two vectors (i.e.,
* x - o) and the corresponding direction. We efficiently pre-compute
* the orthogonality with the corresponding `get_min_distance_`
* functions.
*
* @Input:
* sdf1 ::
* First signed distance (can be any of `a` or `b`).
*
* sdf1 ::
* Second signed distance (can be any of `a` or `b`).
*
* @Return:
* The correct signed distance, which is computed by using the above
* algorithm.
*
* @Note:
* The function does not care about the actual distance, it simply
* returns the signed distance which has a larger cross product. As a
* consequence, this function should not be used if the two distances
* are fairly apart. In that case simply use the signed distance with
* a shorter absolute distance.
*
*/
static SDF_Signed_Distance
resolve_corner( SDF_Signed_Distance sdf1,
SDF_Signed_Distance sdf2 )
{
return FT_ABS( sdf1.cross ) > FT_ABS( sdf2.cross ) ? sdf1 : sdf2;
}
/* END */