NCBI C++ Toolkit Cross Reference

  C++/src/gui/print/pdf.cpp


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 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
/* $Id: pdf.cpp 35534 2016-05-18 14:56:27Z falkrb $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software / database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software / database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Authors: Peter Meric * * File Description: * CPdf - Adobe PDF output * */ #include <ncbi_pch.hpp> #include <gui/print/pdf.hpp> #include <gui/print/pdf_object.hpp> #include "page_handler.hpp" #include "pdf_object_writer.hpp" #include <gui/print/pdf_font_handler.hpp> #include <gui/opengl/glpane.hpp> #include <gui/opengl/glrendernode.hpp> #include <gui/opengl/glvbonode.hpp> #include <gui/opengl/glcolortable.hpp> #include <corelib/ncbitime.hpp> #include <cmath> BEGIN_NCBI_SCOPE CPdf::CPdf() : m_ObjectWriter(new CPdfObjectWriter()), m_ObjIdGenerator(new CIdGenerator()), m_IsGreyScale(false), m_PageDictionary(new CPdfDictionary()), m_FontHandler(new CPdfFontHandler(m_ObjIdGenerator)) { m_PageHandler.reset(new CPageHandler(*m_PageBuffers, *m_ObjectWriter, m_ObjIdGenerator, m_FontHandler)); } CPdf::~CPdf() { } void CPdf::SetOptions(const CPrintOptions& options) { CVectorPrinter::SetOptions(options); m_PageHandler->SetOptions(m_Options); } void CPdf::SetOutputStream(CNcbiOstream* ostream) { CVectorPrinter::SetOutputStream(ostream); m_ObjectWriter->SetOutputStream(ostream); } void CPdf::BeginDocument() { // version 1.6 (and beyond) provide support for page sizes > 200x200 inches *m_Strm << "%PDF-1.7" << pdfeol; // output binary characters in a comment to alert programs this is a binary file *m_Strm << '%'; const unsigned int N = 10; const char binchar[N] = { char(0x80), char(0x81), char(0x82), char(0x83), char(0x84), char(0x85), char(0x86), char(0x87), char(0x88), char(0x89) }; m_Strm->write(binchar, N); *m_Strm << pdfeol; // create objects TPdfObjectRef info(new CPdfObject(m_ObjIdGenerator->NextId())); m_Catalog.Reset(new CPdfObject(m_ObjIdGenerator->NextId())); TPdfObjectRef outlines(new CPdfObject(m_ObjIdGenerator->NextId())); // populate info CPdfObject& _info = *info; _info["Author"] = new CPdfString("National Center for Biotechnology Information"); _info["Creator"] = new CPdfString("NCBI Genome Workbench http://www.ncbi.nlm.nih.gov/tools/gbench/"); _info["Producer"] = new CPdfString("NCBI PDF Generator"); _info["Title"] = new CPdfString(m_Options.GetTitle()); // Set date time in pdf time format. Ignore time zone (for now). //(D:YYYYMMDDHHmmSSOHH'mm'); http://www.gnupdf.org/Date CTime t; t.SetCurrent(); t.SetFormat(CTimeFormat("YMdhms")); _info["CreationDate"] = new CPdfString("D:" + t.AsString()); m_ObjectWriter->WriteObject(info); // populate catalog CPdfObject& catalog = *m_Catalog; catalog["Type"] = new CPdfName("Catalog"); catalog["Outlines"] = new CPdfIndirectObj(outlines); catalog["Pages"] = new CPdfIndirectObj(m_PageHandler->GetObject()); catalog["PageLayout"] = new CPdfName(m_Options.GetNumPages() > 1 ? "OneColumn" : "SinglePage"); catalog["PageMode"] = new CPdfName("UseNone"); catalog["ViewerPreferences"] = new CPdfElement("<</DisplayDocTitle true>>"); if (m_Options.GetPrintOutput()) { // Add print action to Catalog dictionary catalog["OpenAction"] = new CPdfElement("<</S/Named /N/Print>>"); } catalog["PageLabels"] = new CPdfElement("<</Nums[0<</S/D/P(Panel )>>]>>"); m_ObjectWriter->WriteObject(m_Catalog); // populate outlines CPdfObject& ol = *outlines; ol["Type"] = new CPdfName("Outlines"); ol["Count"] = new CPdfNumber(int(0)); m_ObjectWriter->WriteObject(outlines); // populate trailer m_Trailer.Reset(new CPdfTrailer()); CPdfTrailer& trailer = *m_Trailer; trailer["Info"] = new CPdfIndirectObj(info); trailer["Root"] = new CPdfIndirectObj(m_Catalog); m_Fonts.Reset(new CPdfObject(m_ObjIdGenerator->NextId())); CPdfObject& fonts = *m_Fonts; ITERATE (CPdfFontHandler::TObjectList, it, m_FontHandler->GetFontObjects()) { CRef<CPdfObject> font(*it); const string fontname((*font)["Name"]->GetValue()); fonts[fontname] = new CPdfIndirectObj(font); m_PrintInEndDoc.push_back(font); } m_PrintInEndDoc.push_back(m_Fonts); CRef<CPdfDictionary> resources(new CPdfDictionary()); CPdfDictionary& res = *resources; res["Font"] = new CPdfIndirectObj(m_Fonts); (*m_PageDictionary)["Resources"] = resources; } void CPdf::WriteObject(TPdfObjectRef& obj) { m_ObjectWriter->WriteObject(obj); } CPdf::TPdfObjectRef CPdf::BeginContent(EContentType t) { m_CurrentContent.Reset(new CPdfObject(m_ObjIdGenerator->NextId())); return m_CurrentContent; } void CPdf::EndContent() { m_PageHandler->AddContent(m_CurrentContent); //m_CurrentContent.Release(); } CPdf::TPdfObjectRef CPdf::BeginAnnot() { m_CurrentAnnot.Reset(new CPdfObject(m_ObjIdGenerator->NextId())); return m_CurrentAnnot; } void CPdf::EndAnnot() { m_PageHandler->AddAnnot(m_CurrentAnnot); } CPdf::TPdfObjectRef CPdf::BeginReference() { m_CurrentReference.Reset(new CPdfObject(m_ObjIdGenerator->NextId())); return m_CurrentReference; } void CPdf::EndReference() { m_ObjectWriter->WriteObject(m_CurrentReference); } void CPdf::BeginPage() { } void CPdf::EndPage() { if ( !m_CurrentContent ) { return; } //m_PageHandler->AddContent(m_CurrentContent); //m_PageCount += m_PageHandler->WritePages(m_PageCount + 1); m_PageCount += m_PageHandler->WritePages(); } void CPdf::PrintText(CRef<CPdfFontHandler> font_handler, EFontFace face, float font_size, CVect2<float>& p, const char* txt, const CRgbaColor& c) { if (!m_CurrentContent.IsNull()) { m_CurrentContent->SetFillColor(c); string alpha_state = x_GetAlphaGraphicsState(c.GetAlpha()); if (alpha_state != "") { m_CurrentContent->SetGraphicsState(alpha_state); } float uu_inv = 1.0f/m_Options.GetUserUnit(); font_size *= uu_inv; m_CurrentContent->Text(font_handler, face, font_size, p, txt); } else { LOG_POST("Unable to write text - current content object is NULL."); } } void CPdf::PrintText(CRef<CPdfFontHandler> font_handler, EFontFace face, float font_size, CMatrix3<double>& mat, const char* txt, const CRgbaColor& c) { if (!m_CurrentContent.IsNull()) { m_CurrentContent->SetFillColor(c); string alpha_state = x_GetAlphaGraphicsState(c.GetAlpha()); if (alpha_state != "") { m_CurrentContent->SetGraphicsState(alpha_state); } float uu_inv = 1.0f/m_Options.GetUserUnit(); font_size *= uu_inv; m_CurrentContent->Text(font_handler, face, font_size, mat, txt); } else { LOG_POST("Unable to write text - current content object is NULL."); } } void CPdf::PrintModel(CGlPane& pane, CGlModel2D& model, CRgbaGradColorTable* color_table) { vector<CGlVboNode*>& geom_nodes = model.GetNodes(); CGlUtils::CheckGlError(); for (size_t i=0; i<geom_nodes.size(); ++i) { if (geom_nodes[i]->IsVisible() && !geom_nodes[i]->IsSkipped(eRenderPDF) ) { if (geom_nodes[i]->GetState().ScaleInvarientSet() && geom_nodes[i]->GetState().GetScaleInvarient()) { float uu_inv = 1.0f / GetOptions().GetUserUnit(); CVect2<double> scale(pane.GetScale()*(double)uu_inv); if (geom_nodes[i]->GetState().GetScaleInvarient()) { geom_nodes[i]->GetState().ScaleFactor(scale); } } PrintBuffer(geom_nodes[i], pane.GetViewport(), color_table); } } vector<CGlVboNode*>& temp_geom_nodes = model.GetTempNodes(); for (size_t i=0; i<temp_geom_nodes.size(); ++i) { if (temp_geom_nodes[i]->IsVisible() && !temp_geom_nodes[i]->IsSkipped(eRenderPDF) ) { if (temp_geom_nodes[i]->GetState().ScaleInvarientSet() && temp_geom_nodes[i]->GetState().GetScaleInvarient()) { float uu_inv = 1.0f / GetOptions().GetUserUnit(); CVect2<double> scale(pane.GetScale()*(double)uu_inv); if (temp_geom_nodes[i]->GetState().GetScaleInvarient()) { temp_geom_nodes[i]->GetState().ScaleFactor(scale); } } PrintBuffer(temp_geom_nodes[i], pane.GetViewport(), color_table); } } } void CPdf::AddTooltip(CGlPane& pane, const string& txt, CVect4<float>& rect) { CRef<CPdfObject> popup = BeginAnnot(); // Project the coordinates of the label (model coordinates) onto // viewport coordinates which are used for the popup. TVPRect vp = pane.GetViewport(); GLint viewport[4]; GLdouble model_view_matrix[16]; GLdouble projection_matrix[16]; viewport[0] = GLint(vp.Left()); viewport[1] = GLint(vp.Bottom()); viewport[2] = GLint(vp.Width()); viewport[3] = GLint(vp.Height()); glGetDoublev(GL_MODELVIEW_MATRIX, model_view_matrix); glGetDoublev(GL_PROJECTION_MATRIX, projection_matrix); double llx = (double)rect.X(); double lly = (double)rect.Y(); double urx = (double)rect.Z(); double ury = (double)rect.W(); double px,py,px2,py2,dummyz; gluProject(llx, lly, 0.0, model_view_matrix, projection_matrix, viewport, &px, &py, &dummyz); string pos = " [" + NStr::NumericToString(int(px)) + " " + NStr::NumericToString(int(py)) + " "; gluProject(urx, ury, 0.0, model_view_matrix, projection_matrix, viewport, &px2, &py2, &dummyz); pos += NStr::NumericToString(int(px2)) + " " + NStr::NumericToString(int(py2)) + "]"; // You still get an annoying little popup CPdfDictionary& popup_dict = popup->GetDictionary(); popup_dict["Border"]= new CPdfElement(" [0 0 0]"); // colorloss annotation //popup_dict["RD"]= new CPdfElement(" [ 2.0 2.0 2.0 2.0 ]"); popup_dict["Contents"] = new CPdfString(txt); popup_dict["Type"] = new CPdfName("Annot"); popup_dict["Rect"] = new CPdfElement(pos); popup_dict["Subtype"] = new CPdfName("Square"); popup_dict["F"] = new CPdfNumber(64); // makes it read-only EndAnnot(); // Where to put the popup box string pos2 = " [" + NStr::NumericToString(int(px+50)) + " " + NStr::NumericToString(int(py+50)) + " " + NStr::NumericToString(int(px+300)) + " " + NStr::NumericToString(int(py+200)) + "]"; CRef<CPdfObject> annot = BeginAnnot(); CPdfDictionary& annot_dict = annot->GetDictionary(); popup_dict["Popup"] = new CPdfIndirectObj(annot); annot_dict["Parent"] = new CPdfIndirectObj(popup); annot_dict["Subtype"] = new CPdfName("Popup"); annot_dict["Type"] = new CPdfName("Annot"); // This is where it initially pops up annot_dict["Rect"] = new CPdfElement(pos2); annot_dict["Open"] = new CPdfElement(" false"); EndAnnot(); } void CPdf::AddJSTooltip(CGlPane& pane, const string& txt, const string& title, CVect4<float>& rect) { CRef<CPdfObject> popup = BeginAnnot(); // Project the coordinates of the label (model coordinates) onto // viewport coordinates which are used for the popup. TVPRect vp = pane.GetViewport(); GLint viewport[4]; GLdouble model_view_matrix[16]; GLdouble projection_matrix[16]; viewport[0] = GLint(vp.Left()); viewport[1] = GLint(vp.Bottom()); viewport[2] = GLint(vp.Width()); viewport[3] = GLint(vp.Height()); glGetDoublev(GL_MODELVIEW_MATRIX, model_view_matrix); glGetDoublev(GL_PROJECTION_MATRIX, projection_matrix); double llx = (double)rect.X(); double lly = (double)rect.Y(); double urx = (double)rect.Z(); double ury = (double)rect.W(); double px,py,dummyz; gluProject(llx, lly, 0.0, model_view_matrix, projection_matrix, viewport, &px, &py, &dummyz); string pos = " [" + NStr::NumericToString(int(px)) + " " + NStr::NumericToString(int(py)) + " "; gluProject(urx, ury, 0.0, model_view_matrix, projection_matrix, viewport, &px, &py, &dummyz); pos += NStr::NumericToString(int(px)) + " " + NStr::NumericToString(int(py)) + "]"; //pos = string(" [110.0 110.0 150.0 150.0]"); // You still get an annoying little popup CPdfDictionary& popup_dict = popup->GetDictionary(); //popup_dict["C"]= new CPdfElement(" [1 0 0 1.0]"); // red box popup_dict["Border"]= new CPdfElement(" [0 0 0]"); // colorless annotation popup_dict["Type"] = new CPdfName("Annot"); popup_dict["Rect"] = new CPdfElement(pos); popup_dict["Subtype"] = new CPdfName("Link"); popup_dict["H"] = new CPdfName("I"); CPdfDictionary* d = new CPdfDictionary(); (*d)["JS"] = new CPdfElement(" (app.alert\\(\"" + txt + "\", 3, 0, \"" + title + "\"\\))"); (*d)["S"] = new CPdfName("JavaScript"); (*d)["Type"] = new CPdfName("Action"); popup_dict["A"] = d; EndAnnot(); } void CPdf::PrintBuffer(CGlVboNode* node, const TVPRect& viewport, CRgbaGradColorTable* color_table) { for (size_t j=0; j<node->GetPositions().size(); ++j) { glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); CMatrix4<float> mat = node->GetTransformedPosition(j); mat.Transpose(); glLoadMatrixf(mat.GetData()); if (node->GetDrawMode() == GL_LINES || node->GetDrawMode() == GL_LINE_STRIP || node->GetDrawMode() == GL_LINE_LOOP) { PrintLineBuffer(node, viewport, color_table); } if (node->GetDrawMode() == GL_POINTS) { PrintPointBuffer(node, viewport, color_table); } else if (node->GetDrawMode() == GL_TRIANGLES || node->GetDrawMode() == GL_TRIANGLE_FAN || node->GetDrawMode() == GL_TRIANGLE_STRIP) { PrintTriBuffer(node, viewport, color_table); } else if (node->GetDrawMode() == GL_QUADS) { PrintQuadBuffer(node, viewport, color_table); } glPopMatrix(); }; } CRgbaColor CPdf::x_GetGreyscale(const CRgbaColor& c) { CRgbaColor gs_color = c; // Don't do a conversion if it's already greyscale since that // is normally intentional (most rendering code sets greyscale // colors automatically) if (std::abs(c.GetRed()-c.GetGreen()) > 0.01f || std::abs(c.GetGreen()-c.GetBlue()) > 0.01f) { // Use standard NTSC conversion weights float grey = c.GetRed()*0.299f + c.GetGreen()*0.587f + c.GetBlue()*0.114f; gs_color.Set(grey, grey, grey, gs_color.GetAlpha()); } return gs_color; } bool CPdf::x_GetColors(CGlVboNode* node, vector<CRgbaColor>& colors, CRgbaGradColorTable* color_table) { bool has_color = false; size_t vert_count = node->GetVertexBuffer2D().GetActiveBufferSize(); if (vert_count == 0) vert_count = node->GetVertexBuffer3D().GetActiveBufferSize(); CGlRenderBuffer<CRgbaColor>& cb = node->GetColorBuffer(); if (cb.GetActiveBufferSize() == GLsizei(vert_count)) { has_color = true; cb.GetData(colors); } // colors may be stored as unsigned chars (to keep buffer smaller) // convert to rgba in this case: else if (node->GetColorBufferUC().GetActiveBufferSize() == GLsizei(vert_count)) { has_color = true; vector<CVect4<unsigned char> > colors_uc; colors.clear(); node->GetColorBufferUC().GetData(colors_uc); for (size_t i=0; i<colors_uc.size(); ++i) { colors.push_back(CRgbaColor(colors_uc[i][0], colors_uc[i][1], colors_uc[i][2], colors_uc[i][3])); } } // We may have stored colors in 1-dimensional texture buffer: else if (color_table != NULL) { CGlRenderBuffer<float>& texbuf = node->GetTexCoordBuffer1D(); if (texbuf.GetActiveBufferSize() == GLsizei(vert_count)) { has_color = true; vector<float> tex_coords; texbuf.GetData(tex_coords); for (size_t i=0; i<tex_coords.size(); ++i) { CRgbaColor c = color_table->GetTexCoordColor(tex_coords[i]); colors.push_back(c); } } } // If we are in grey scale mode, make sure all colors are greyscale. if (has_color && m_IsGreyScale) { for (size_t i=0; i<colors.size(); ++i) { colors[i] = x_GetGreyscale(colors[i]); } } return has_color; } bool CPdf::x_GetDefaultColor(CGlVboNode* node, CRgbaColor& default_color) { default_color = node->GetState().GetColor(); if (m_IsGreyScale) { default_color = x_GetGreyscale(default_color); } return node->GetState().ColorSet(); } void CPdf::PrintPointBuffer(CGlVboNode* node, const TVPRect& vp, CRgbaGradColorTable* color_table) { vector<CVect2<float> > vertices; vector<CRgbaColor> colors; x_GetVertexBuffer(node, vertices); if (vertices.size() == 0) return; GLint viewport[4]; GLdouble model_view_matrix[16]; GLdouble projection_matrix[16]; // Get viewport from caller since Opengl value may be truncated // since pdf page extents can in many cases be larger than the // OpenGL maximum viewport size. viewport[0] = GLint(vp.Left()); viewport[1] = GLint(vp.Bottom()); viewport[2] = GLint(vp.Width()); viewport[3] = GLint(vp.Height()); glGetDoublev(GL_MODELVIEW_MATRIX, model_view_matrix); glGetDoublev(GL_PROJECTION_MATRIX, projection_matrix); bool has_color = x_GetColors(node, colors, color_table); string alpha_state = x_GetAlphaGraphicsState(node); CRef<CPdfObject> content = BeginContent(CPdf::ePdfPoints); /// Mostly do this for the clipping (push state) so that it doesn't stay active content->PushGraphicsState(); content->SetClipBox(viewport[0], viewport[1], viewport[2], viewport[3]); // Use color buffer for color or, if none, use color in State, if set CRgbaColor default_color; if (!has_color && x_GetDefaultColor(node, default_color)) { content->SetColor(default_color); } float w = 1.0f; if (node->GetState().PointSizeSet()) { w = node->GetState().GetPointSize(); } float uu_inv = 1.0f/m_Options.GetUserUnit(); w *= uu_inv; content->SetLineWidth(w); content->SetLineCapStyle((int)CGlState::eButtCap); content->SetLineJoinStyle((int)CGlState::eMiteredJoin); if (alpha_state != "") { content->SetGraphicsState(alpha_state); } // No actual point type in PDF. A line of 0 length with the right width // and square caps (which are 1/2 the width on each end) should give // the same results. if (node->GetDrawMode() == GL_POINTS) { for (size_t i=0; i<vertices.size(); i+=2) { CVect2<float> v1 = vertices[i]; CVect2<float> v1p, v2p; double px, py; double dummyz; gluProject(v1.X(), v1.Y(), 0.0, model_view_matrix, projection_matrix, viewport, &px, &py, &dummyz); v1p.X() = (float)px; v1p.Y() = (float)py; v2p = v1p; // Make length of line (in screen coordinates) same as point size // to turn line into a point. v2p.X() += w/2.0f; v1p.X() -= w/2.0f; if (has_color) { content->SetColor(colors[i]); } content->Line(v1p, v2p); /* if (m_Options.GetMedia().Inside(v1p) && m_Options.GetMedia().Inside(v2p)) content->Line(v1p, v2p); else _TRACE("Geom outisde"); */ } } content->PushGraphicsState(); EndContent(); } void CPdf::PrintLineBuffer(CGlVboNode* node, const TVPRect& vp, CRgbaGradColorTable* color_table) { vector<CVect2<float> > vertices; vector<CRgbaColor> colors; x_GetVertexBuffer(node, vertices); if (vertices.size() == 0) return; GLint viewport[4]; GLdouble model_view_matrix[16]; GLdouble projection_matrix[16]; viewport[0] = GLint(vp.Left()); viewport[1] = GLint(vp.Bottom()); viewport[2] = GLint(vp.Width()); viewport[3] = GLint(vp.Height()); glGetDoublev(GL_MODELVIEW_MATRIX, model_view_matrix); glGetDoublev(GL_PROJECTION_MATRIX, projection_matrix); // Write out vertices... CRef<CPdfObject> content = BeginContent(CPdf::ePdfLines); /// Mostly do this for the clipping (push state) so that it doesn't stay active content->PushGraphicsState(); content->SetClipBox(viewport[0], viewport[1], viewport[2], viewport[3]); bool has_color = x_GetColors(node, colors, color_table); string alpha_state = x_GetAlphaGraphicsState(node); // Use color buffer for color or, if none, use color in State. CRgbaColor default_color; if (x_GetDefaultColor(node, default_color)) { content->SetColor(default_color); } if (node->GetState().LineWidthSet()) { float w = node->GetState().GetLineWidth(); float uu_inv = 1.0f/m_Options.GetUserUnit(); w *= uu_inv; content->SetLineWidth(w); } if (node->GetState().LineCapStyleSet()) { content->SetLineCapStyle((int)node->GetState().GetLineCapStyle()); } if (node->GetState().LineJoinStyleSet()) { content->SetLineJoinStyle((int)node->GetState().GetLineJoinStyle()); } if (node->GetState().IsEnabled(GL_LINE_STIPPLE) && node->GetState().LineStippleSet()) { GLint factor; GLushort pattern; node->GetState().GetLineStipple(factor, pattern); content->SetLineDashStyle(factor, pattern); } else { // explicitly set to 0 when not enabled to avoid accidental stippling content->SetLineDashStyle(0, 0); } if (alpha_state != "") { content->SetGraphicsState(alpha_state); } if (node->GetDrawMode() == GL_LINES) { for (size_t i=0; i<vertices.size(); i+=2) { if (i+1 >= vertices.size()) break; CVect2<float> v1 = vertices[i]; CVect2<float> v2 = vertices[i+1]; CVect2<float> v1p; CVect2<float> v2p; double px, py; double dummyz; gluProject(v1.X(), v1.Y(), 0.0, model_view_matrix, projection_matrix, viewport, &px, &py, &dummyz); v1p.X() = (float)px; v1p.Y() = (float)py; gluProject(v2.X(), v2.Y(), 0.0, model_view_matrix, projection_matrix, viewport, &px, &py, &dummyz); v2p.X() = (float)px; v2p.Y() = (float)py; if (has_color) { CRgbaColor c1 = colors[i]; CRgbaColor c2 = colors[i+1]; // If ends are different colors, need to draw with a gradient. // PDF has support for gradients with axial rendering and shfill // https://partners.adobe.com/public/developer/en/ps/sdk/TN5600.SmoothShading.pdf // but we have not currrently implmented support for these yet other // than triangle shading. float cdist = CRgbaColor::ColorDistance(c1, c2); float dist = (v1p-v2p).Length(); if (cdist > 0.1f && dist > 5.0f) { // segment line based on color change and distance - for // maximum color change (cdist=1) have a vertex every 5 pixels float offset = 5.0f/cdist; int num_segments = (int)ceilf(dist/offset); // so at least 1 float delta_pct = 1.0f/(float)num_segments; float delta = 0.0f; CVect2<float> seg_start = v1p; CVect2<float> seg_end; for (int i=0; i<num_segments; ++i) { CRgbaColor c = c1*(1.0f-delta) + c2*delta; delta += delta_pct; seg_end = v1p*(1.0f-delta) + v2p*delta; string current_alpha_state = x_GetAlphaGraphicsState(node, c.GetAlpha()); if (alpha_state != current_alpha_state) { content->SetGraphicsState(current_alpha_state); alpha_state = current_alpha_state; } content->SetColor(c); content->Line(seg_start, seg_end); seg_start = seg_end; } } else { string current_alpha_state = x_GetAlphaGraphicsState(node, colors[i].GetAlpha()); if (alpha_state != current_alpha_state) { content->SetGraphicsState(current_alpha_state); alpha_state = current_alpha_state; } content->SetColor(colors[i]); content->Line(v1p, v2p); } } else { content->Line(v1p, v2p); } } } else if (node->GetDrawMode() == GL_LINE_STRIP) { for (size_t i=1; i<vertices.size(); i++) { CVect2<float> v1 = vertices[i-1]; CVect2<float> v2 = vertices[i]; CVect2<float> v1p; CVect2<float> v2p; double px, py; double dummyz; gluProject(v1.X(), v1.Y(), 0.0, model_view_matrix, projection_matrix, viewport, &px, &py, &dummyz); v1p.X() = (float)px; v1p.Y() = (float)py; gluProject(v2.X(), v2.Y(), 0.0, model_view_matrix, projection_matrix, viewport, &px, &py, &dummyz); v2p.X() = (float)px; v2p.Y() = (float)py; if (has_color) { content->SetColor(colors[i]); string current_alpha_state = x_GetAlphaGraphicsState(node, colors[i].GetAlpha()); if (alpha_state != current_alpha_state) { content->SetGraphicsState(current_alpha_state); alpha_state = current_alpha_state; } } content->Line(v1p, v2p); } } else if (node->GetDrawMode() == GL_LINE_LOOP) { for (size_t i=1; i<vertices.size(); i++) { CVect2<float> v1 = vertices[i-1]; CVect2<float> v2 = vertices[i]; CVect2<float> v1p; CVect2<float> v2p; double px, py; double dummyz; gluProject(v1.X(), v1.Y(), 0.0, model_view_matrix, projection_matrix, viewport, &px, &py, &dummyz); v1p.X() = (float)px; v1p.Y() = (float)py; gluProject(v2.X(), v2.Y(), 0.0, model_view_matrix, projection_matrix, viewport, &px, &py, &dummyz); v2p.X() = (float)px; v2p.Y() = (float)py; if (has_color) { content->SetColor(colors[i]); string current_alpha_state = x_GetAlphaGraphicsState(node, colors[i].GetAlpha()); if (alpha_state != current_alpha_state) { content->SetGraphicsState(current_alpha_state); alpha_state = current_alpha_state; } } content->Line(v1p, v2p); } CVect2<float> v1 = vertices[0]; CVect2<float> v2 = vertices[vertices.size()-1]; CVect2<float> v1p; CVect2<float> v2p; double px, py; double dummyz; gluProject(v1.X(), v1.Y(), 0.0, model_view_matrix, projection_matrix, viewport, &px, &py, &dummyz); v1p.X() = (float)px; v1p.Y() = (float)py; gluProject(v2.X(), v2.Y(), 0.0, model_view_matrix, projection_matrix, viewport, &px, &py, &dummyz); v2p.X() = (float)px; v2p.Y() = (float)py; content->Line(v1p, v2p); } content->PopGraphicsState(); EndContent(); } /// /// Simple helper class to take a bunch of triangles and extract from /// that set, if possible, a single perimeter. If the triangle set happens /// to have no perimeters or multiple perimeters, nothing will be returned. class TriPerimeter { public: size_t AddTri(CVect2<float>& v1p, CVect2<float>& v2p, CVect2<float>& v3p) { size_t idx1 = AddVertex(v1p); size_t idx2 = AddVertex(v2p); size_t idx3 = AddVertex(v3p); size_t e1 = AddEdge(idx1, idx2); size_t e2 = AddEdge(idx2, idx3); size_t e3 = AddEdge(idx3, idx1); m_Triangles.push_back(CVect3<size_t>(e1, e2, e3)); return (m_Triangles.size()-1); } size_t AddVertex(CVect2<float>& v) { float min_dist = 1e10f; size_t min_idx = (size_t)-1; for (size_t i=0; i<m_Vertices.size(); ++i) { float dist = (v-m_Vertices[i]).Length2(); if (dist < min_dist) { min_idx = i; min_dist = dist; } } if (min_dist > 1e-6f) { min_idx = m_Vertices.size(); m_Vertices.push_back(v); } return min_idx; } size_t AddEdge(size_t vert1, size_t vert2) { // edges will have lower index first CVect2<size_t> edge(vert1, vert2); if (vert1 > vert2) { edge.Set(vert2, vert1); } vector<CVect2<size_t> >::iterator iter; iter = find(m_Edges.begin(), m_Edges.end(), edge); if (iter != m_Edges.end()) return (size_t(iter-m_Edges.begin())); m_Edges.push_back(edge); return m_Edges.size() - 1; } /// returns empty vec if no perimeter can be found. vector<CVect2<float> > GetPerimiter() const { // First find all unshared edges vector<int> edge_tris(m_Edges.size(), 0); size_t i; for (i=0; i<m_Triangles.size(); ++i) { for (int j=0; j<3; ++j) edge_tris[m_Triangles[i][j]] += 1; } vector<size_t> unshared_edges; for (i=0; i<edge_tris.size(); ++i) { if (edge_tris[i]==1) unshared_edges.push_back(i); } vector<size_t> perimeter_vertices; vector<CVect2<float> > perimeter; // Take the first unshared edge then loop over the edges repeatedly taking // each time an edge that connects to the second vertex until the second // vertex of an edge connects to the first vertex of the first edge or // no match is found. bool done = false; if (unshared_edges.size() < 3) return perimeter; size_t first_vert = m_Edges[unshared_edges.back()][0]; size_t last_vert = m_Edges[unshared_edges.back()][1]; perimeter_vertices.push_back(first_vert); perimeter_vertices.push_back(last_vert); unshared_edges.pop_back(); while (!done && unshared_edges.size() > 0) { int next_edge = -1; for (i=0; i<unshared_edges.size() && next_edge == -1; ++i) { if (m_Edges[unshared_edges[i]][0] == last_vert) { last_vert = m_Edges[unshared_edges[i]][1]; if (last_vert == first_vert) done = true; else perimeter_vertices.push_back(last_vert); next_edge = (int)i; } else if (m_Edges[unshared_edges[i]][1] == last_vert) { last_vert = m_Edges[unshared_edges[i]][0]; if (last_vert == first_vert) done = true; else perimeter_vertices.push_back(last_vert); next_edge = (int)i; } } if (next_edge != -1) { unshared_edges.erase(unshared_edges.begin() + next_edge); } else { done = true; } } if (unshared_edges.size() == 0) { for (i=0; i<perimeter_vertices.size(); ++i) perimeter.push_back(m_Vertices[perimeter_vertices[i]]); } return perimeter; } protected: vector<CVect2<float> > m_Vertices; vector<CVect2<size_t> > m_Edges; vector<CVect3<size_t> > m_Triangles; }; void CPdf::x_PrintFlatTriBuffer(CGlVboNode* node, const TVPRect& vp, CRgbaGradColorTable* color_table) { vector<CVect2<float> > vertices; vector<CRgbaColor> colors; x_GetVertexBuffer(node, vertices); if (vertices.size() == 0) return; GLint viewport[4]; GLdouble model_view_matrix[16]; GLdouble projection_matrix[16]; viewport[0] = GLint(vp.Left()); viewport[1] = GLint(vp.Bottom()); viewport[2] = GLint(vp.Width()); viewport[3] = GLint(vp.Height()); glGetDoublev(GL_MODELVIEW_MATRIX, model_view_matrix); glGetDoublev(GL_PROJECTION_MATRIX, projection_matrix); string alpha_state = x_GetAlphaGraphicsState(node); // Write out vertices... CRef<CPdfObject> content = BeginContent(CPdf::ePdfFlatTris); /// Mostly do this for the clipping (push state) so that it doesn't stay active content->PushGraphicsState(); content->SetClipBox(viewport[0], viewport[1], viewport[2], viewport[3]); // Normally a color buffer would force ShadingType 4, but if user // wants to force flat rendering would could wind up here in which case // we can user the buffer colors per-triangle rather than per vertex // (or per-perimeter) bool has_color = x_GetColors(node, colors, color_table); // use color in State. Since triangles are flat, there is no color buffer // (if we see a buffer we assume it is not flat) CRgbaColor default_color; if (x_GetDefaultColor(node, default_color)) { content->SetColor(default_color); content->SetFillColor(default_color); } size_t i; for (i=0; i<vertices.size(); ++i) { CVect2<float> v1 = vertices[i]; CVect2<float> v1p; double px, py; double dummyz; gluProject(v1.X(), v1.Y(), 0.0, model_view_matrix, projection_matrix, viewport, &px, &py, &dummyz); vertices[i].Set(float(px), float(py)); } // GL_LINE or GL_FILL bool fill = true; if (node->GetState().GetPolygonMode() == GL_LINE) { fill = false; if (node->GetState().LineCapStyleSet()) { content->SetLineCapStyle((int)node->GetState().GetLineCapStyle()); } if (node->GetState().LineJoinStyleSet()) { content->SetLineJoinStyle((int)node->GetState().GetLineJoinStyle()); } } if (alpha_state != "") { content->SetGraphicsState(alpha_state); } if (node->GetDrawMode() == GL_TRIANGLES) { TriPerimeter tp; for (i=0; i<vertices.size(); i+=3) { if (i+2 >= vertices.size()) break; tp.AddTri(vertices[i], vertices[i+1], vertices[i+2]); } // Can we convert this into a single perimeter? vector<CVect2<float> > perimeter = tp.GetPerimiter(); if (perimeter.size() > 2 && fill) { if (has_color) { content->SetFillColor(colors[0]); content->SetColor(colors[0]); string current_alpha_state = x_GetAlphaGraphicsState(node, colors[0].GetAlpha()); if (alpha_state != current_alpha_state) { content->SetGraphicsState(current_alpha_state); alpha_state = current_alpha_state; } } content->Poly(perimeter); } else { for (i=0; i<vertices.size(); i+=3) { if (i+2 >= vertices.size()) break; CVect2<float> v1p = vertices[i]; CVect2<float> v2p = vertices[i+1]; CVect2<float> v3p = vertices[i+2]; if (has_color) { content->SetFillColor(colors[i]); content->SetColor(colors[i]); string current_alpha_state = x_GetAlphaGraphicsState(node, colors[i].GetAlpha()); if (alpha_state != current_alpha_state) { content->SetGraphicsState(current_alpha_state); alpha_state = current_alpha_state; } } if (fill) { content->Tri(v1p, v2p, v3p); } else { content->Line(v1p, v2p); content->Line(v2p, v3p); content->Line(v3p, v1p); } } } } else if (node->GetDrawMode() == GL_TRIANGLE_STRIP) { TriPerimeter tp; for (size_t i=2; i<vertices.size(); i++) { tp.AddTri(vertices[i], vertices[i-1], vertices[i-2]); } // Can we convert this into a single perimeter? vector<CVect2<float> > perimeter = tp.GetPerimiter(); if (perimeter.size() > 2 && fill) { if (has_color) { content->SetFillColor(colors[0]); content->SetColor(colors[0]); string current_alpha_state = x_GetAlphaGraphicsState(node, colors[0].GetAlpha()); if (alpha_state != current_alpha_state) { content->SetGraphicsState(current_alpha_state); alpha_state = current_alpha_state; } } content->Poly(perimeter); } else { for (size_t i=2; i<vertices.size(); i++) { CVect2<float> v1p = vertices[i]; CVect2<float> v2p = vertices[i-1]; CVect2<float> v3p = vertices[i-2]; if (has_color) { content->SetFillColor(colors[i]); content->SetColor(colors[i]); string current_alpha_state = x_GetAlphaGraphicsState(node, colors[i].GetAlpha()); if (alpha_state != current_alpha_state) { content->SetGraphicsState(current_alpha_state); alpha_state = current_alpha_state; } } if (fill) { content->Tri(v1p, v2p, v3p); } else { content->Line(v1p, v2p); content->Line(v2p, v3p); content->Line(v3p, v1p); } } } } else if (node->GetDrawMode() == GL_TRIANGLE_FAN) { CVect2<float> v1p = vertices[0]; vector<CVect2<float> > perimeter; for (size_t i=1; i<vertices.size(); i++) { perimeter.push_back(vertices[i]); } // would user maybe want perimiter for edges? The could have always // just drawn edges for that, rather than tris. if (fill) { if (has_color) { content->SetFillColor(colors[0]); content->SetColor(colors[0]); string current_alpha_state = x_GetAlphaGraphicsState(node, colors[0].GetAlpha()); if (alpha_state != current_alpha_state) { content->SetGraphicsState(current_alpha_state); alpha_state = current_alpha_state; } } content->Poly(perimeter); } else { for (size_t i=2; i<vertices.size(); i++) { CVect2<float> v2p = vertices[i-1]; CVect2<float> v3p = vertices[i]; if (has_color) { content->SetFillColor(colors[i]); content->SetColor(colors[i]); string current_alpha_state = x_GetAlphaGraphicsState(node, colors[i].GetAlpha()); if (alpha_state != current_alpha_state) { content->SetGraphicsState(current_alpha_state); alpha_state = current_alpha_state; } } else { content->Line(v1p, v2p); content->Line(v2p, v3p); content->Line(v3p, v1p); } } } } content->PopGraphicsState(); EndContent(); } string CPdf::AddShadedTris(CGlVboNode* node, const TVPRect& vp, CRgbaGradColorTable* color_table, CPdfObject::EBitCount bit_count) { // Iterate over all vertices and find the min/max values that capture the range // for this shading object (set of shaded triangles). You can give the pdf file // separate ranges for x and y but we will leave that out for now since most data // is clustered near the origin (x and y ranges are similar) if (node == NULL) return ""; vector<CVect2<float> > vertices; x_GetVertexBuffer(node, vertices); if (vertices.size() < 3) return ""; float minx = std::numeric_limits<float>::max(); float miny = std::numeric_limits<float>::max(); float maxx = -std::numeric_limits<float>::max(); float maxy = -std::numeric_limits<float>::max(); for (size_t v=0; v<vertices.size(); ++v) { minx = std::min(vertices[v].X(), minx); miny = std::min(vertices[v].Y(), miny); maxx = std::max(vertices[v].X(), maxx); maxy = std::max(vertices[v].Y(), maxy); } // Can't have a 0 or near 0 range size (the range is used in the pdf file // to set up values to convert the floating point values to integers in // a larger range so a little larger is no problem (see ShadingType 4, Decode) return AddShadedTris(node, vp, bit_count, color_table, int(minx-10.0f), int(maxx+10.0f), int(miny-10.0f), int(maxy+10.0f)); } void CPdf::x_GetVertexBuffer(CGlVboNode* node, vector<CVect2<float> >& verts) { verts.clear(); CGlUtils::CheckGlError(); if (node->GetVertexBuffer2D().GetActiveBufferSize() > 0) { CGlRenderBuffer<CVect2<float> >& buf = node->GetVertexBuffer2D(); buf.GetData(verts); } else if (node->GetVertexBuffer3D().GetActiveBufferSize() > 0) { CGlRenderBuffer<CVect3<float> >& buf = node->GetVertexBuffer3D(); vector<CVect3<float> > verts3d; buf.GetData(verts3d); verts.resize(verts3d.size()); for (size_t i=0; i<verts3d.size(); ++i) { verts[i].Set(verts3d[i].X(), verts3d[i].Y()); } } } string CPdf::AddShadedTris(CGlVboNode* node, const TVPRect& vp, CPdfObject::EBitCount bit_count, CRgbaGradColorTable* color_table, int range_minx, int range_maxx, int range_miny, int range_maxy) { if (node == NULL) return ""; vector<CVect2<float> > vertices; vector<CRgbaColor> colors; x_GetVertexBuffer(node, vertices); if (vertices.size() < 3) return ""; CRgbaColor c; x_GetDefaultColor(node, c); CRgbaColor c1 = c; CRgbaColor c2 = c; CRgbaColor c3 = c; bool has_color = x_GetColors(node, colors, color_table); CPdf::TPdfObjectRef tris = BeginReference(); tris->StartTris(CPdfObject::eASCIIHex, bit_count, range_minx, range_maxx, range_miny, range_maxy); if (node->GetDrawMode() == GL_TRIANGLES) { for (size_t v=0; v+2<vertices.size(); v+=3) { if (has_color) { c1 = colors[v]; c2 = colors[v+1]; c3 = colors[v+2]; } tris->Tri(0, vertices[v], c1, vertices[v+1], c2, vertices[v+2], c3); } } else if (node->GetDrawMode() == GL_TRIANGLE_STRIP) { if (has_color) { c1 = colors[0]; c2 = colors[1]; c3 = colors[2]; } tris->Tri(0, vertices[0], c1, vertices[1], c2, vertices[2], c3); for (size_t v=3; v<vertices.size(); ++v) { if (has_color) { c1 = colors[v]; } tris->Tri(1, vertices[v], c1); } } else if (node->GetDrawMode() == GL_TRIANGLE_FAN) { if (has_color) { c1 = colors[0]; c2 = colors[1]; c3 = colors[2]; } tris->Tri(0, vertices[0], c1, vertices[1], c2, vertices[2], c3); for (size_t v=3; v<vertices.size(); ++v) { if (has_color) { c1 = colors[v]; } tris->Tri(2, vertices[v], c1); } } tris->EndTris(); EndReference(); CRef<CPdfElement>& res = (*PageDictionary())["Resources"]; if ( !res ) { res.Reset(new CPdfDictionary()); } CRef<CPdfDictionary> _res(dynamic_cast <CPdfDictionary*>(res.GetPointer())); CRef<CPdfElement>& shaders = (*_res)["Shading"]; if ( !shaders ) { shaders.Reset(new CPdfDictionary()); } CRef<CPdfDictionary> _shaders(dynamic_cast <CPdfDictionary*>(shaders.GetPointer())); string shader_id = _shaders->GenShaderID(); (*_shaders)[shader_id.c_str()] = new CPdfIndirectObj(tris); return shader_id; } void CPdf::RenderShaderInstance(CGlVboNode* node, const string& shader_id, const TVPRect& vp) { GLint viewport[4]; GLdouble model_view_matrix[16]; GLdouble projection_matrix[16]; viewport[0] = GLint(vp.Left()); viewport[1] = GLint(vp.Bottom()); viewport[2] = GLint(vp.Width()); viewport[3] = GLint(vp.Height()); glGetDoublev(GL_MODELVIEW_MATRIX, model_view_matrix); glGetDoublev(GL_PROJECTION_MATRIX, projection_matrix); CMatrix3<double> mat, matvt, matvs; matvt.Identity(); matvs.Identity(); // Compute modelview projection matrix and then get // homogeneous 2D (3x3) matrix for PDF from that result (ignore z) CMatrix4<double> mp(projection_matrix); CMatrix4<double> mm(model_view_matrix); mp.Transpose(); mm.Transpose(); CMatrix4<double> mvp = mp*mm; CMatrix3<double> mvp33 = CMatrix3<double>(mvp(0, 0), mvp(0, 1), mvp(0, 3), mvp(1, 0), mvp(1, 1), mvp(1, 3), mvp(3, 0), mvp(3, 1), mvp(3, 3)); matvs(0,0) = ((double)viewport[2])/2.0; matvs(1,1) = ((double)viewport[3])/2.0; matvt(0,2) = matvs(0,0) + double(viewport[0]); matvt(1,2) = matvs(1,1) + double(viewport[1]); mat = matvt*matvs*mvp33; // can't do alpha in tris vertex-by-vertex, but if there is a color array, use alpha from there, // but only from the first element in the array! string alpha_state = x_GetAlphaGraphicsState(node); CPdf::TPdfObjectRef shade_obj = BeginContent(CPdf::ePdfTris); /// Mostly do this for the clipping (push state) so that it doesn't stay active shade_obj->PushGraphicsState(); shade_obj->SetClipBox(viewport[0], viewport[1], viewport[2], viewport[3]); //(*shade_obj) << CPdfElement("q") << pdfbrk; (*shade_obj) << CPdfTransform(mat) << '\n'; if (alpha_state != "") shade_obj->SetGraphicsState(alpha_state); (*shade_obj) << "/" << shader_id << " sh" << '\n'; //(*shade_obj) << CPdfElement("Q") << pdfbrk; shade_obj->PopGraphicsState(); EndContent(); } void CPdf::PrintTriBuffer(CGlVboNode* node, const TVPRect& vp, CRgbaGradColorTable* color_table) { // decide if the triangles are gouraud (diff colors for each // vertex) or not. If the shading state is flat, or the color // buffer is empty, we have flat shaded tris. // Another issue with pdf is that a region tiled together out // of many polygons will show cracks if it is done using flat polygons - // only shaded polys seem to be able to join to hide cracks. The // alternative for flat is to provide the convex hull of the poly, // but we can't just do that since we don't know if it is convex. bool shaded = true; if (node->GetState().GetPolygonMode() == GL_LINE || (node->GetState().GetShadeModel() == GL_FLAT && node->GetState().GetPdfShadeStyle() == CGlState::eFlat)) { shaded = false; } if (shaded && !m_Options.GetGouraudShadingDisabled()) { string id = AddShadedTris(node, vp, color_table); if (id != "") RenderShaderInstance(node, id, vp); } else { x_PrintFlatTriBuffer(node, vp, color_table); } } void CPdf::PrintQuadBuffer(CGlVboNode* node, const TVPRect& vp, CRgbaGradColorTable* color_table) { vector<CVect2<float> > vertices; vector<CRgbaColor> colors; x_GetVertexBuffer(node, vertices); if (vertices.size() == 0) return; GLint viewport[4]; GLdouble model_view_matrix[16]; GLdouble projection_matrix[16]; viewport[0] = GLint(vp.Left()); viewport[1] = GLint(vp.Bottom()); viewport[2] = GLint(vp.Width()); viewport[3] = GLint(vp.Height()); glGetDoublev(GL_MODELVIEW_MATRIX, model_view_matrix); glGetDoublev(GL_PROJECTION_MATRIX, projection_matrix); bool has_color = x_GetColors(node, colors, color_table); if (has_color) { if (node->GetState().GetShadeModel() != GL_FLAT) { // Warn we see that there is a // color buffer and that the colors in the buffer are not all the // same for the vertices of at least one quad. for (size_t i=3; i<colors.size(); i+=4) { if (!(colors[i] == colors[i-3]) || !(colors[i] == colors[i-2]) || !(colors[i] == colors[i-1])) { LOG_POST(Warning << "Warning - printing quads flat while \ OpenGL state is shaded (only tris may be shaded)"); break; } } } } string alpha_state = x_GetAlphaGraphicsState(node); // Start new pdf object CRef<CPdfObject> content = BeginContent(CPdf::ePdfQuads); /// Mostly do this for the clipping (push state) so that it doesn't stay active content->PushGraphicsState(); content->SetClipBox(viewport[0], viewport[1], viewport[2], viewport[3]); // use color in State. Since triangles are flat, there is no color buffer // (if we see a buffer we assume it is not flat) CRgbaColor c; if (x_GetDefaultColor(node, c)) { content->SetColor(c); content->SetFillColor(c); } // GL_LINE or GL_FILL bool fill = true; if (node->GetState().GetPolygonMode() == GL_LINE) { fill = false; if (node->GetState().LineCapStyleSet()) { content->SetLineCapStyle((int)node->GetState().GetLineCapStyle()); } if (node->GetState().LineJoinStyleSet()) { content->SetLineJoinStyle((int)node->GetState().GetLineJoinStyle()); } } if (alpha_state != "") { content->SetGraphicsState(alpha_state); } for (size_t i=3; i<vertices.size(); i+=4) { CVect2<float> v1 = vertices[i-3]; CVect2<float> v2 = vertices[i-2]; CVect2<float> v3 = vertices[i-1]; CVect2<float> v4 = vertices[i]; CVect2<float> v1p; CVect2<float> v2p; CVect2<float> v3p; CVect2<float> v4p; double px, py; double dummyz; gluProject(v1.X(), v1.Y(), 0.0, model_view_matrix, projection_matrix, viewport, &px, &py, &dummyz); v1p.X() = (float)px; v1p.Y() = (float)py; gluProject(v2.X(), v2.Y(), 0.0, model_view_matrix, projection_matrix, viewport, &px, &py, &dummyz); v2p.X() = (float)px; v2p.Y() = (float)py; gluProject(v3.X(), v3.Y(), 0.0, model_view_matrix, projection_matrix, viewport, &px, &py, &dummyz); v3p.X() = (float)px; v3p.Y() = (float)py; gluProject(v4.X(), v4.Y(), 0.0, model_view_matrix, projection_matrix, viewport, &px, &py, &dummyz); v4p.X() = (float)px; v4p.Y() = (float)py; if (has_color) { content->SetFillColor(colors[i]); content->SetColor(colors[i]); string current_alpha_state = x_GetAlphaGraphicsState(node, colors[i].GetAlpha()); if (alpha_state != current_alpha_state) { content->SetGraphicsState(current_alpha_state); alpha_state = current_alpha_state; } } if (fill) { content->Quad(v1p, v2p, v3p, v4p); } else { content->Line(v1p, v2p); content->Line(v2p, v3p); content->Line(v3p, v4p); content->Line(v4p, v1p); } } content->PopGraphicsState(); EndContent(); } void CPdf::EndDocument() { m_PageHandler->WritePageTree(m_PageDictionary); ITERATE(vector<TPdfObjectRef>, it, m_PrintInEndDoc) { m_ObjectWriter->WriteObject(*it); } // the number of objects is defined as one more than the highest // object number const unsigned int num_objects = m_ObjIdGenerator->NextId(); // write the cross reference const CT_POS_TYPE xref_start = m_ObjectWriter->WriteXRef(num_objects); // write the trailer (*m_Trailer)["Size"] = new CPdfNumber(num_objects); *m_Strm << *m_Trailer; // output the location of the last xref section *m_Strm << "startxref" << endl << (xref_start - CT_POS_TYPE(0)) << endl; *m_Strm << "%%EOF" << pdfeol; } string CPdf::x_GetAlphaGraphicsState(CGlVboNode* node) { size_t vertex_buf_size = node->GetVertexBuffer2D().GetActiveBufferSize(); if (vertex_buf_size == 0) vertex_buf_size = node->GetVertexBuffer3D().GetActiveBufferSize(); if (vertex_buf_size == 0) return ""; float alpha = 1.0f; // Only support most standard (alpha/one-minus-alpha) blending. If that is // not the mode set in OpenGL do not have blending. (It is the default too // so if blending is enabled but function not set, we enable it). if ((node->GetState().IsEnabled(GL_BLEND) && !node->GetState().BlendFuncSet()) || (node->GetState().GetSourceBlendFactor() == GL_SRC_ALPHA && node->GetState().GetTargetBlendFactor() == GL_ONE_MINUS_SRC_ALPHA)) { // First get alpha value from color in state if (node->GetState().ColorSet()) alpha = node->GetState().GetColor().GetAlpha(); // Then check for color (and alpha) in the color buffer. Note that this takes alpha // from the FIRST COLOR. If other vertices have different alpha values we will not // capture that. CGlRenderBuffer<CRgbaColor>& cb = node->GetColorBuffer(); if (cb.GetActiveBufferSize() == (GLsizei)vertex_buf_size) { if (!cb.IsMapped()) { if (cb.MapBuffer(GL_READ_ONLY) != NULL) { alpha = cb[0].GetAlpha(); cb.UnmapBuffer(); } } else { alpha = cb[0].GetAlpha(); } } } string state_name = x_GetAlphaGraphicsState(node, alpha); return state_name; } string CPdf::x_GetAlphaGraphicsState(CGlVboNode* node, float alpha) { string state_name; // If blending is not enabled or enabled but set to a blending function // other than src_alpha, one_minus_src_alpha, do not blend (alpha=1.0f) if (!node->GetState().IsEnabled(GL_BLEND)) { alpha = 1.0f; } else { if (node->GetState().BlendFuncSet() && (node->GetState().GetSourceBlendFactor() != GL_SRC_ALPHA || node->GetState().GetTargetBlendFactor() != GL_ONE_MINUS_SRC_ALPHA)) { alpha = 1.0f; } } return x_GetAlphaGraphicsState(alpha); } string CPdf::x_GetAlphaGraphicsState(float alpha) { string state_name; int rounded_alpha = int(alpha*100.0f); state_name = "A" + NStr::IntToString(rounded_alpha); CRef<CPdfElement>& res = (*PageDictionary())["Resources"]; if ( !res ) { res.Reset(new CPdfDictionary()); } CRef<CPdfDictionary> _res(dynamic_cast <CPdfDictionary*>(res.GetPointer())); CRef<CPdfElement>& extgstate = (*_res)["ExtGState"]; if ( !extgstate ) { extgstate.Reset(new CPdfDictionary()); } CRef<CPdfDictionary> _extgstate(dynamic_cast <CPdfDictionary*>(extgstate.GetPointer())); CRef<CPdfElement>& gs = (*_extgstate)[state_name]; if (gs.IsNull()) { TPdfObjectRef graphics_state; graphics_state.Reset(new CPdfObject(m_ObjIdGenerator->NextId())); CPdfObject& state = *graphics_state; state["Type"] = new CPdfName("ExtGState"); state["ca"] = new CPdfNumber(alpha); state["CA"] = new CPdfNumber(alpha); m_ObjectWriter->WriteObject(graphics_state); gs.Reset(new CPdfIndirectObj(graphics_state)); } return state_name; } END_NCBI_SCOPE

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.