root/OpenSceneGraph/trunk/src/osgWidget/PdfReader.cpp @ 13041

Revision 13041, 3.0 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
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
22using namespace osgWidget;
23
24PdfReader::PdfReader(const std::string& filename, const GeometryHints& hints)
25{
26    open(filename, hints);
27}
28
29bool 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            // no need to adjust aspect ratio
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
76bool 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
82bool PdfReader::page(int pageNum)
83{
84    if (!_pdfImage) return false;
85
86    return _pdfImage->page(pageNum);
87}
88
89bool PdfReader::previous()
90{
91    if (!_pdfImage) return false;
92
93    return _pdfImage->previous();
94}
95
96bool PdfReader::next()
97{
98    if (!_pdfImage) return false;
99
100    return _pdfImage->next();
101}
Note: See TracBrowser for help on using the browser.