| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osg/Geode> |
|---|
| 15 | #include <osgDB/ReadFile> |
|---|
| 16 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 17 | |
|---|
| 18 | #include <osgWidget/PdfReader> |
|---|
| 19 | |
|---|
| 20 | #include <osg/io_utils> |
|---|
| 21 | |
|---|
| 22 | using namespace osgWidget; |
|---|
| 23 | |
|---|
| 24 | PdfReader::PdfReader(const std::string& filename, const GeometryHints& hints) |
|---|
| 25 | { |
|---|
| 26 | open(filename, hints); |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | bool PdfReader::assign(PdfImage* pdfImage, const GeometryHints& hints) |
|---|
| 30 | { |
|---|
| 31 | if (!pdfImage) return false; |
|---|
| 32 | |
|---|
| 33 | _pdfImage = pdfImage; |
|---|
| 34 | _pdfImage->setBackgroundColor(hints.backgroundColor); |
|---|
| 35 | |
|---|
| 36 | bool flip = _pdfImage->getOrigin()==osg::Image::TOP_LEFT; |
|---|
| 37 | |
|---|
| 38 | float aspectRatio = (_pdfImage->t()>0 && _pdfImage->s()>0) ? float(_pdfImage->t()) / float(_pdfImage->s()) : 1.0; |
|---|
| 39 | |
|---|
| 40 | osg::Vec3 widthVec(hints.widthVec); |
|---|
| 41 | osg::Vec3 heightVec(hints.heightVec); |
|---|
| 42 | |
|---|
| 43 | switch(hints.aspectRatioPolicy) |
|---|
| 44 | { |
|---|
| 45 | case(GeometryHints::RESIZE_HEIGHT_TO_MAINTAINCE_ASPECT_RATIO): |
|---|
| 46 | heightVec *= aspectRatio; |
|---|
| 47 | break; |
|---|
| 48 | case(GeometryHints::RESIZE_WIDTH_TO_MAINTAINCE_ASPECT_RATIO): |
|---|
| 49 | widthVec /= aspectRatio; |
|---|
| 50 | break; |
|---|
| 51 | default: |
|---|
| 52 | |
|---|
| 53 | break; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | osg::Geometry* pictureQuad = osg::createTexturedQuadGeometry(hints.position, widthVec, heightVec, |
|---|
| 57 | 0.0f, flip ? 1.0f : 0.0f , 1.0f, flip ? 0.0f : 1.0f); |
|---|
| 58 | |
|---|
| 59 | osg::Texture2D* texture = new osg::Texture2D(_pdfImage.get()); |
|---|
| 60 | texture->setResizeNonPowerOfTwoHint(false); |
|---|
| 61 | texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); |
|---|
| 62 | texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); |
|---|
| 63 | texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); |
|---|
| 64 | |
|---|
| 65 | pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0, |
|---|
| 66 | texture, |
|---|
| 67 | osg::StateAttribute::ON); |
|---|
| 68 | |
|---|
| 69 | pictureQuad->setEventCallback(new osgViewer::InteractiveImageHandler(_pdfImage.get())); |
|---|
| 70 | |
|---|
| 71 | addDrawable(pictureQuad); |
|---|
| 72 | |
|---|
| 73 | return true; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | bool PdfReader::open(const std::string& filename, const GeometryHints& hints) |
|---|
| 77 | { |
|---|
| 78 | osg::ref_ptr<osg::Image> image = osgDB::readImageFile(filename); |
|---|
| 79 | return assign(dynamic_cast<PdfImage*>(image.get()), hints); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | bool PdfReader::page(int pageNum) |
|---|
| 83 | { |
|---|
| 84 | if (!_pdfImage) return false; |
|---|
| 85 | |
|---|
| 86 | return _pdfImage->page(pageNum); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | bool PdfReader::previous() |
|---|
| 90 | { |
|---|
| 91 | if (!_pdfImage) return false; |
|---|
| 92 | |
|---|
| 93 | return _pdfImage->previous(); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | bool PdfReader::next() |
|---|
| 97 | { |
|---|
| 98 | if (!_pdfImage) return false; |
|---|
| 99 | |
|---|
| 100 | return _pdfImage->next(); |
|---|
| 101 | } |
|---|