2006-06-27 22:17:35 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2006 by Luigi Toscano <luigi.toscano@tiscali.it> *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "generator_dvi.h"
|
|
|
|
#include "core/page.h"
|
|
|
|
#include "core/link.h"
|
2006-06-30 22:52:54 +00:00
|
|
|
#include "core/utils.h"
|
2006-06-27 22:17:35 +00:00
|
|
|
#include "dviFile.h"
|
|
|
|
#include "dviPageInfo.h"
|
|
|
|
#include "dviRenderer.h"
|
|
|
|
#include "pageSize.h"
|
|
|
|
|
|
|
|
#include <qapplication.h>
|
|
|
|
#include <qstring.h>
|
|
|
|
#include <qurl.h>
|
2006-07-01 21:17:42 +00:00
|
|
|
#include <qvector.h>
|
2006-06-30 22:35:48 +00:00
|
|
|
#include <qstack.h>
|
2006-06-27 22:17:35 +00:00
|
|
|
|
|
|
|
#include <kdebug.h>
|
|
|
|
#include <kimageeffect.h>
|
|
|
|
#include <klocale.h>
|
|
|
|
|
|
|
|
OKULAR_EXPORT_PLUGIN(DviGenerator)
|
|
|
|
|
2006-10-22 11:25:08 +00:00
|
|
|
DviGenerator::DviGenerator() : Okular::Generator(),
|
2006-06-30 22:35:48 +00:00
|
|
|
m_docInfo( 0 ), m_docSynopsis( 0 ), ready( false ), m_dviRenderer( 0 )
|
2006-06-27 22:17:35 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-09-21 08:45:36 +00:00
|
|
|
bool DviGenerator::loadDocument( const QString & fileName, QVector< Okular::Page * > &pagesVector )
|
2006-06-27 22:17:35 +00:00
|
|
|
{
|
|
|
|
//kDebug() << "file: " << qPrintable( fileName ) << endl;
|
|
|
|
KUrl base( fileName );
|
|
|
|
|
|
|
|
m_dviRenderer = new dviRenderer();
|
|
|
|
if ( ! m_dviRenderer->setFile( fileName, base ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
kDebug() << "# of pages: " << m_dviRenderer->dviFile->total_pages << endl;
|
|
|
|
|
2006-10-29 15:08:18 +00:00
|
|
|
m_resolution = Okular::Utils::getDpiY();
|
2006-06-27 22:17:35 +00:00
|
|
|
loadPages( pagesVector, 0 );
|
|
|
|
|
|
|
|
ready = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-07-14 10:54:29 +00:00
|
|
|
bool DviGenerator::closeDocument()
|
|
|
|
{
|
|
|
|
delete m_docInfo;
|
|
|
|
m_docInfo = 0;
|
|
|
|
delete m_docSynopsis;
|
|
|
|
m_docSynopsis = 0;
|
|
|
|
delete m_dviRenderer;
|
|
|
|
m_dviRenderer = 0;
|
|
|
|
|
|
|
|
ready = false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-10-20 16:51:46 +00:00
|
|
|
bool DviGenerator::canGeneratePixmap ( bool /*async*/ ) const
|
2006-06-27 22:17:35 +00:00
|
|
|
{
|
|
|
|
return ready;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rotateCoordinates( const int iWidth, const int iHeight,
|
|
|
|
int &fWidth, int &fHeight, const int orientation)
|
|
|
|
{
|
|
|
|
if ( orientation % 2 == 0 )
|
|
|
|
{
|
|
|
|
/* portrait */
|
|
|
|
fWidth = iWidth;
|
|
|
|
fHeight = iHeight;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* landscape */
|
|
|
|
fWidth = iHeight;
|
|
|
|
fHeight = iWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-06-28 20:40:47 +00:00
|
|
|
static void rotateCoordinates( const double iWidth, const double iHeight,
|
|
|
|
double &fWidth, double &fHeight, const int orientation)
|
|
|
|
{
|
|
|
|
if ( orientation % 2 == 0 )
|
|
|
|
{
|
|
|
|
/* portrait */
|
|
|
|
fWidth = iWidth;
|
|
|
|
fHeight = iHeight;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* landscape */
|
|
|
|
fWidth = iHeight;
|
|
|
|
fHeight = iWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-09-21 08:45:36 +00:00
|
|
|
void DviGenerator::fillViewportFromAnchor( Okular::DocumentViewport &vp,
|
2006-06-30 22:35:48 +00:00
|
|
|
const Anchor &anch, int pW, int pH,
|
|
|
|
int orientation )
|
|
|
|
{
|
|
|
|
vp.pageNumber = anch.page - 1;
|
|
|
|
|
|
|
|
double vp_x = 0.0, vp_y = 0.0;
|
|
|
|
|
|
|
|
SimplePageSize ps = m_dviRenderer->sizeOfPage( vp.pageNumber );
|
|
|
|
double resolution = 0;
|
|
|
|
|
|
|
|
if ( orientation % 2 == 0 )
|
|
|
|
resolution = (double)(pW)/ps.width().getLength_in_inch();
|
|
|
|
else
|
|
|
|
resolution = (double)(pH)/ps.height().getLength_in_inch();
|
|
|
|
|
|
|
|
double py = (double)anch.distance_from_top.getLength_in_inch()*resolution + 0.5;
|
|
|
|
|
|
|
|
rotateCoordinates( 0.5, py / (double)pH,
|
|
|
|
vp_x, vp_y, orientation );
|
|
|
|
vp.rePos.normalizedX = vp_x;
|
|
|
|
vp.rePos.normalizedY = vp_y;
|
|
|
|
vp.rePos.enabled = true;
|
2006-09-21 08:45:36 +00:00
|
|
|
vp.rePos.pos = Okular::DocumentViewport::Center;
|
2006-06-30 22:35:48 +00:00
|
|
|
}
|
2006-06-27 22:17:35 +00:00
|
|
|
|
2006-09-21 08:45:36 +00:00
|
|
|
QLinkedList<Okular::ObjectRect*> DviGenerator::generateDviLinks( const dviPageInfo *pageInfo,
|
2006-06-27 22:17:35 +00:00
|
|
|
int orientation )
|
|
|
|
{
|
2006-09-21 08:45:36 +00:00
|
|
|
QLinkedList<Okular::ObjectRect*> dviLinks;
|
2006-06-27 22:17:35 +00:00
|
|
|
|
|
|
|
int pageWidth = 0, pageHeight = 0;
|
|
|
|
|
|
|
|
rotateCoordinates( pageInfo->width, pageInfo->height,
|
|
|
|
pageWidth, pageHeight, orientation );
|
|
|
|
|
|
|
|
foreach( const Hyperlink dviLink, pageInfo->hyperLinkList )
|
|
|
|
{
|
2006-09-21 08:45:36 +00:00
|
|
|
QRect boxArea = Okular::Utils::rotateRect( dviLink.box, pageWidth, pageHeight,
|
2006-06-30 22:52:54 +00:00
|
|
|
orientation );
|
2006-06-27 22:17:35 +00:00
|
|
|
double nl = (double)boxArea.left() / pageWidth,
|
|
|
|
nt = (double)boxArea.top() / pageHeight,
|
|
|
|
nr = (double)boxArea.right() / pageWidth,
|
|
|
|
nb = (double)boxArea.bottom() / pageHeight;
|
|
|
|
|
|
|
|
Anchor anch = m_dviRenderer->findAnchor(dviLink.linkText);
|
|
|
|
|
2006-09-21 08:45:36 +00:00
|
|
|
Okular::Link *okuLink = 0;
|
2006-06-28 20:40:47 +00:00
|
|
|
|
2006-06-30 22:35:48 +00:00
|
|
|
/* distinguish between local (-> anchor) and remote links */
|
|
|
|
if (anch.isValid())
|
2006-06-27 22:17:35 +00:00
|
|
|
{
|
2006-06-30 22:35:48 +00:00
|
|
|
/* internal link */
|
2006-09-21 08:45:36 +00:00
|
|
|
Okular::DocumentViewport vp;
|
2006-06-30 22:35:48 +00:00
|
|
|
fillViewportFromAnchor( vp, anch, pageWidth, pageHeight,
|
|
|
|
orientation );
|
2006-09-21 08:45:36 +00:00
|
|
|
okuLink = new Okular::LinkGoto( "", vp );
|
2006-06-27 22:17:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-09-21 08:45:36 +00:00
|
|
|
okuLink = new Okular::LinkBrowse( dviLink.linkText );
|
2006-06-27 22:17:35 +00:00
|
|
|
}
|
2006-06-28 20:40:47 +00:00
|
|
|
if ( okuLink )
|
|
|
|
{
|
2006-09-21 08:45:36 +00:00
|
|
|
Okular::ObjectRect *orlink = new Okular::ObjectRect( nl, nt, nr, nb, false, Okular::ObjectRect::Link,
|
2006-06-28 20:40:47 +00:00
|
|
|
okuLink );
|
|
|
|
dviLinks.push_front( orlink );
|
|
|
|
}
|
|
|
|
|
2006-06-27 22:17:35 +00:00
|
|
|
}
|
|
|
|
return dviLinks;
|
|
|
|
}
|
|
|
|
|
2006-09-21 08:45:36 +00:00
|
|
|
void DviGenerator::generatePixmap( Okular::PixmapRequest *request )
|
2006-06-27 22:17:35 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
dviPageInfo *pageInfo = new dviPageInfo();
|
|
|
|
pageSize ps;
|
|
|
|
|
2006-10-25 15:35:53 +00:00
|
|
|
rotateCoordinates( request->width(), request->height(),
|
2006-10-28 16:24:16 +00:00
|
|
|
pageInfo->width, pageInfo->height, request->page()->rotation() );
|
2006-06-27 22:17:35 +00:00
|
|
|
|
2006-10-25 15:35:53 +00:00
|
|
|
pageInfo->pageNumber = request->pageNumber() + 1;
|
2006-06-27 22:17:35 +00:00
|
|
|
|
|
|
|
// pageInfo->resolution = m_resolution;
|
|
|
|
|
|
|
|
SimplePageSize s = m_dviRenderer->sizeOfPage( pageInfo->pageNumber );
|
|
|
|
|
|
|
|
/* if ( s.width() != pageInfo->width) */
|
|
|
|
// if (!useDocumentSpecifiedSize)
|
|
|
|
// s = userPreferredSize;
|
|
|
|
|
|
|
|
if (s.isValid())
|
|
|
|
{
|
|
|
|
ps = s; /* it should be the user specified size, if any, instead */
|
|
|
|
}
|
|
|
|
|
|
|
|
pageInfo->resolution = (double)(pageInfo->width)/ps.width().getLength_in_inch();
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
kDebug() << request
|
|
|
|
<< ", res:" << pageInfo->resolution << " - (" << pageInfo->width << ","
|
|
|
|
<< ps.width().getLength_in_inch() << ")," << ps.width().getLength_in_mm()
|
|
|
|
<< endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ( m_dviRenderer )
|
|
|
|
{
|
|
|
|
m_dviRenderer->drawPage( pageInfo );
|
|
|
|
|
|
|
|
if ( ! pageInfo->img.isNull() )
|
|
|
|
{
|
|
|
|
kDebug() << "Image OK" << endl;
|
|
|
|
|
2006-10-25 17:43:15 +00:00
|
|
|
request->page()->setImage( request->id(), pageInfo->img );
|
2006-06-27 22:17:35 +00:00
|
|
|
|
2006-10-25 15:35:53 +00:00
|
|
|
request->page()->setObjectRects(
|
|
|
|
generateDviLinks( pageInfo, 0 ) );
|
2006-06-27 22:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ready = true;
|
|
|
|
|
|
|
|
delete pageInfo;
|
|
|
|
|
|
|
|
signalRequestDone( request );
|
|
|
|
}
|
|
|
|
|
2006-10-20 16:51:46 +00:00
|
|
|
bool DviGenerator::canGenerateTextPage() const
|
2006-06-27 22:17:35 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-09-21 08:45:36 +00:00
|
|
|
void DviGenerator::generateSyncTextPage( Okular::Page* page )
|
2006-06-27 22:17:35 +00:00
|
|
|
{
|
2006-09-21 08:45:36 +00:00
|
|
|
kDebug() << "DviGenerator::generateSyncTextPage( Okular::Page * page )" << endl;
|
2006-06-27 22:17:35 +00:00
|
|
|
dviPageInfo *pageInfo = new dviPageInfo();
|
|
|
|
pageSize ps;
|
|
|
|
|
|
|
|
rotateCoordinates( (int)(page->width()), (int)(page->height()),
|
2006-10-08 15:07:23 +00:00
|
|
|
pageInfo->width, pageInfo->height, page->totalOrientation() );
|
2006-06-27 22:17:35 +00:00
|
|
|
|
|
|
|
pageInfo->pageNumber = page->number() + 1;
|
|
|
|
|
|
|
|
pageInfo->resolution = m_resolution;
|
|
|
|
SimplePageSize s = m_dviRenderer->sizeOfPage( pageInfo->pageNumber );
|
|
|
|
pageInfo->resolution = (double)(pageInfo->width)/ps.width().getLength_in_inch();
|
|
|
|
|
|
|
|
// get page text from m_dviRenderer
|
|
|
|
if ( m_dviRenderer )
|
|
|
|
{
|
|
|
|
m_dviRenderer->getText( pageInfo );
|
|
|
|
|
2006-10-08 15:07:23 +00:00
|
|
|
Okular::TextPage *ktp = extractTextFromPage( pageInfo, page->totalOrientation() );
|
2006-06-27 22:17:35 +00:00
|
|
|
page->setSearchPage( ktp );
|
|
|
|
}
|
|
|
|
delete pageInfo;
|
|
|
|
}
|
|
|
|
|
2006-09-21 08:45:36 +00:00
|
|
|
Okular::TextPage *DviGenerator::extractTextFromPage( dviPageInfo *pageInfo, int orientation )
|
2006-06-27 22:17:35 +00:00
|
|
|
{
|
2006-09-21 08:45:36 +00:00
|
|
|
QList<Okular::TextEntity*> textOfThePage;
|
2006-06-27 22:17:35 +00:00
|
|
|
|
2006-07-01 21:17:42 +00:00
|
|
|
QVector<TextBox>::ConstIterator it = pageInfo->textBoxList.constBegin();
|
|
|
|
QVector<TextBox>::ConstIterator itEnd = pageInfo->textBoxList.constEnd();
|
2006-06-27 22:17:35 +00:00
|
|
|
QRect tmpRect;
|
|
|
|
|
|
|
|
int pageWidth = 0, pageHeight = 0;
|
|
|
|
|
|
|
|
rotateCoordinates( pageInfo->width, pageInfo->height,
|
|
|
|
pageWidth, pageHeight, orientation );
|
|
|
|
|
|
|
|
for ( ; it != itEnd ; ++it )
|
|
|
|
{
|
|
|
|
TextBox curTB = *it;
|
2006-06-30 22:35:48 +00:00
|
|
|
|
2006-09-21 08:45:36 +00:00
|
|
|
tmpRect = Okular::Utils::rotateRect( curTB.box, pageWidth, pageHeight, orientation );
|
2006-06-27 22:17:35 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
kDebug() << "orientation: " << orientation
|
|
|
|
<< ", curTB.box: " << curTB.box
|
|
|
|
<< ", tmpRect: " << tmpRect
|
|
|
|
<< ", ( " << pageWidth << "," << pageHeight << " )"
|
|
|
|
<<endl;
|
|
|
|
#endif
|
2006-09-21 08:45:36 +00:00
|
|
|
textOfThePage.push_back( new Okular::TextEntity( curTB.text,
|
|
|
|
new Okular::NormalizedRect( tmpRect, pageWidth, pageHeight ) ) );
|
2006-06-27 22:17:35 +00:00
|
|
|
}
|
|
|
|
|
2006-09-21 08:45:36 +00:00
|
|
|
Okular::TextPage* ktp = new Okular::TextPage( textOfThePage );
|
2006-06-27 22:17:35 +00:00
|
|
|
|
|
|
|
return ktp;
|
|
|
|
}
|
|
|
|
|
2006-09-21 08:45:36 +00:00
|
|
|
const Okular::DocumentInfo *DviGenerator::generateDocumentInfo()
|
2006-06-27 22:17:35 +00:00
|
|
|
{
|
|
|
|
if ( m_docInfo )
|
|
|
|
return m_docInfo;
|
|
|
|
|
2006-09-21 08:45:36 +00:00
|
|
|
m_docInfo = new Okular::DocumentInfo();
|
2006-06-27 22:17:35 +00:00
|
|
|
|
|
|
|
m_docInfo->set( "mimeType", "application/x-dvi" );
|
|
|
|
|
|
|
|
if ( m_dviRenderer && m_dviRenderer->dviFile )
|
|
|
|
{
|
|
|
|
dvifile *dvif = m_dviRenderer->dviFile;
|
|
|
|
|
|
|
|
// read properties from dvif
|
|
|
|
//m_docInfo->set( "filename", dvif->filename, i18n("Filename") );
|
|
|
|
m_docInfo->set( "generatorDate", dvif->generatorString,
|
|
|
|
i18n("Generator/Date") );
|
|
|
|
m_docInfo->set( "pages", QString::number( dvif->total_pages ),
|
|
|
|
i18n("Pages") );
|
|
|
|
}
|
|
|
|
return m_docInfo;
|
|
|
|
}
|
|
|
|
|
2006-09-21 08:45:36 +00:00
|
|
|
const Okular::DocumentSynopsis *DviGenerator::generateDocumentSynopsis()
|
2006-06-30 22:35:48 +00:00
|
|
|
{
|
|
|
|
if ( m_docSynopsis )
|
|
|
|
return m_docSynopsis;
|
|
|
|
|
2006-09-21 08:45:36 +00:00
|
|
|
m_docSynopsis = new Okular::DocumentSynopsis();
|
2006-06-30 22:35:48 +00:00
|
|
|
|
|
|
|
QStack<QDomElement*> stack;
|
|
|
|
|
2006-07-01 21:17:42 +00:00
|
|
|
QVector<PreBookmark> prebookmarks = m_dviRenderer->getPrebookmarks();
|
2006-06-30 22:35:48 +00:00
|
|
|
|
|
|
|
if ( prebookmarks.isEmpty() )
|
|
|
|
return m_docSynopsis;
|
|
|
|
|
2006-07-01 21:17:42 +00:00
|
|
|
QVector<PreBookmark>::ConstIterator it = prebookmarks.begin();
|
|
|
|
QVector<PreBookmark>::ConstIterator itEnd = prebookmarks.end();
|
2006-06-30 22:35:48 +00:00
|
|
|
for( ; it != itEnd; ++it )
|
|
|
|
{
|
|
|
|
QDomElement *domel = new QDomElement;
|
|
|
|
*domel= m_docSynopsis->createElement( (*it).title );
|
|
|
|
|
|
|
|
Anchor a = m_dviRenderer->findAnchor((*it).anchorName);
|
|
|
|
if ( a.isValid() )
|
|
|
|
{
|
2006-09-21 08:45:36 +00:00
|
|
|
Okular::DocumentViewport vp;
|
2006-06-30 22:35:48 +00:00
|
|
|
|
2006-10-22 11:25:08 +00:00
|
|
|
const Okular::Page *p = document()->page( a.page - 1 );
|
2006-06-30 22:35:48 +00:00
|
|
|
/* Don't care about rotations... */
|
|
|
|
fillViewportFromAnchor( vp, a, (int)p->width(), (int)p->height(), 0 );
|
|
|
|
domel->setAttribute( "Viewport", vp.toString() );
|
|
|
|
}
|
|
|
|
if ( stack.isEmpty() )
|
|
|
|
m_docSynopsis->appendChild( *domel );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stack.top()->appendChild( *domel );
|
|
|
|
stack.pop();
|
|
|
|
}
|
|
|
|
for ( int i = 0; i < (*it).noOfChildren; ++i )
|
|
|
|
stack.push( domel );
|
2006-09-04 14:55:59 +00:00
|
|
|
if (!(*it).noOfChildren)
|
|
|
|
delete domel;
|
2006-06-30 22:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_docSynopsis;
|
|
|
|
}
|
|
|
|
|
2006-09-21 08:45:36 +00:00
|
|
|
void DviGenerator::loadPages( QVector< Okular::Page * > &pagesVector, int orientation )
|
2006-06-27 22:17:35 +00:00
|
|
|
{
|
|
|
|
QSize pageRequiredSize;
|
|
|
|
|
|
|
|
int numofpages = m_dviRenderer->dviFile->total_pages;
|
|
|
|
pagesVector.resize( numofpages );
|
|
|
|
|
|
|
|
//kDebug() << "resolution: " << m_resolution << ", dviFile->preferred? " << endl;
|
|
|
|
|
|
|
|
/* get the suggested size */
|
|
|
|
if ( m_dviRenderer->dviFile->suggestedPageSize )
|
|
|
|
{
|
|
|
|
pageRequiredSize = m_dviRenderer->dviFile->suggestedPageSize->sizeInPixel(
|
|
|
|
m_resolution );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pageSize ps;
|
|
|
|
pageRequiredSize = ps.sizeInPixel( m_resolution );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ( orientation % 2 != 0 )
|
|
|
|
{
|
|
|
|
/* landscape */
|
|
|
|
pageRequiredSize = QSize ( pageRequiredSize.height(),
|
|
|
|
pageRequiredSize.width() );
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( int i = 0; i < numofpages; ++i )
|
|
|
|
{
|
|
|
|
//kDebug() << "getting status of page " << i << ":" << endl;
|
|
|
|
|
|
|
|
if ( pagesVector[i] )
|
|
|
|
{
|
|
|
|
delete pagesVector[i];
|
|
|
|
}
|
|
|
|
|
2006-09-21 08:45:36 +00:00
|
|
|
Okular::Page * page = new Okular::Page( i,
|
2006-06-27 22:17:35 +00:00
|
|
|
pageRequiredSize.width(),
|
|
|
|
pageRequiredSize.height(),
|
|
|
|
orientation );
|
|
|
|
pagesVector[i] = page;
|
|
|
|
}
|
|
|
|
kDebug() << "pagesVector successfully inizialized ! " << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "generator_dvi.moc"
|