Commit ab5fd168 authored by Enrico Ros's avatar Enrico Ros
Browse files

Leakfix! Option to disable KPDFActiveRects generation. Optimize.

@style: kebase/kicker/HACKING.

svn path=/branches/kpdf_experiments/kdegraphics/kpdf/; revision=361181
parent a0dfb620
......@@ -32,62 +32,50 @@
// NOTE: XPDF/Splash implementation dependant code will be marked with '###'
// BEGIN KPDFOutputDev
KPDFOutputDev::KPDFOutputDev(SplashColor paperColor)
: SplashOutputDev(splashModeRGB8, false, paperColor),
m_pixmapWidth( -1 ), m_pixmapHeight( -1 ), m_pixmap( 0 ), m_text( 0 )
KPDFOutputDev::KPDFOutputDev( SplashColor paperColor )
: SplashOutputDev( splashModeRGB8, false, paperColor ), m_pixmap( 0 ), m_text( 0 )
{
}
KPDFOutputDev::~KPDFOutputDev()
{
QValueList< KPDFLink * >::iterator itL = m_links.begin(), endL = m_links.end();
for ( ; itL != endL; ++itL )
delete *itL;
QValueList< KPDFActiveRect * >::iterator itR = m_rects.begin(), endR = m_rects.end();
for ( ; itR != endR; ++itR )
delete *itR;
delete m_pixmap;
delete m_text;
clear();
}
void KPDFOutputDev::setParams( int width, int height, bool genText, bool /*genLinks*/ )
void KPDFOutputDev::setParams( int width, int height, bool genT, bool genL, bool genAR )
{
m_pixmapWidth = width;
m_pixmapHeight = height;
if ( m_pixmap )
{
delete m_pixmap;
m_pixmap = 0;
}
clear();
m_pixmapWidth = width;
m_pixmapHeight = height;
delete m_text;
m_text = genText ? new TextPage( gFalse ) : 0;
m_generateText = genT;
m_generateLinks = genL;
m_generateActiveRects = genAR;
QValueList< KPDFLink * >::iterator it = m_links.begin(), end = m_links.end();
for ( ; it != end; ++it )
delete *it;
m_links.clear();
if ( m_generateText )
m_text = new TextPage( gFalse );
}
QPixmap * KPDFOutputDev::takePixmap()
{
QPixmap * pix = m_pixmap;
m_pixmap = 0;
return pix;
QPixmap * pix = m_pixmap;
m_pixmap = 0;
return pix;
}
TextPage * KPDFOutputDev::takeTextPage()
{
TextPage * text = m_text;
m_text = 0;
return text;
TextPage * text = m_text;
m_text = 0;
return text;
}
QValueList< KPDFLink * > KPDFOutputDev::takeLinks()
{
QValueList< KPDFLink * > linksCopy( m_links );
m_links.clear();
return linksCopy;
QValueList< KPDFLink * > linksCopy( m_links );
m_links.clear();
return linksCopy;
}
QValueList< KPDFActiveRect * > KPDFOutputDev::takeActiveRects()
......@@ -97,114 +85,142 @@ QValueList< KPDFActiveRect * > KPDFOutputDev::takeActiveRects()
return rectsCopy;
}
void KPDFOutputDev::startPage(int pageNum, GfxState *state)
void KPDFOutputDev::startPage( int pageNum, GfxState *state )
{
m_pageNum = pageNum;
SplashOutputDev::startPage(pageNum, state);
if ( m_text )
m_text->startPage(state);
if ( m_generateText )
m_text->startPage( state );
SplashOutputDev::startPage( pageNum, state );
}
void KPDFOutputDev::endPage()
{
SplashOutputDev::endPage();
if ( m_text )
m_text->coalesce(gTrue);
SplashOutputDev::endPage();
if ( m_generateText )
m_text->coalesce( gTrue );
// create a QPixmap from page data
delete m_pixmap;
int bh = getBitmap()->getHeight(),
bw = getBitmap()->getWidth();
SplashColorPtr dataPtr = getBitmap()->getDataPtr();
QImage * img = new QImage((uchar*)dataPtr.rgb8, bw, bh, 32, 0, 0, QImage::IgnoreEndian);
if ( bw != m_pixmapWidth || bh != m_pixmapHeight )
{
// it may happen (in fact it doesn't) that we need rescaling
kdWarning() << "Pixmap at page '" << m_pageNum << "' needed rescale." << endl;
m_pixmap = new QPixmap( img->smoothScale( m_pixmapWidth, m_pixmapHeight ) );
}
else
m_pixmap = new QPixmap( *img );
delete img;
// ### hack: unload memory used by bitmap
SplashOutputDev::startPage(0, NULL);
// create a QPixmap from page data
delete m_pixmap;
int bh = getBitmap()->getHeight(),
bw = getBitmap()->getWidth();
SplashColorPtr dataPtr = getBitmap()->getDataPtr();
QImage * img = new QImage( (uchar*)dataPtr.rgb8, bw, bh, 32, 0, 0, QImage::IgnoreEndian );
// it may happen (in fact it doesn't) that we need rescaling
if ( bw != m_pixmapWidth || bh != m_pixmapHeight )
m_pixmap = new QPixmap( img->smoothScale( m_pixmapWidth, m_pixmapHeight ) );
else
m_pixmap = new QPixmap( *img );
delete img;
// ### hack: unload memory used by internal SplashOutputDev's bitmap
SplashOutputDev::startPage( 0, NULL );
}
void KPDFOutputDev::drawLink(Link * link, Catalog * catalog)
void KPDFOutputDev::drawLink( Link * link, Catalog * catalog )
{
if ( !link->isOk() )
return;
if ( !link->isOk() )
return;
// create the new KPDFLink ...
KPDFLink * l = new KPDFLink( link->getAction() );
double x1, y1, x2, y2;
link->getRect( &x1, &y1, &x2, &y2 );
int left, top, right, bottom;
cvtUserToDev( x1, y1, &left, &top );
cvtUserToDev( x2, y2, &right, &bottom );
// ... and assign its coords withing current page geometry
l->setGeometry( left, top, right, bottom );
// add the link to the vector container
m_links.push_back( l );
if ( m_generateLinks )
{
// create the new KPDFLink ...
KPDFLink * l = new KPDFLink( link->getAction() );
double x1, y1, x2, y2;
link->getRect( &x1, &y1, &x2, &y2 );
int left, top, right, bottom;
cvtUserToDev( x1, y1, &left, &top );
cvtUserToDev( x2, y2, &right, &bottom );
// ... and assign its coords withing current page geometry
l->setGeometry( left, top, right, bottom );
// add the link to the vector container
m_links.push_back( l );
}
SplashOutputDev::drawLink( link, catalog );
}
// call parent's link handler
SplashOutputDev::drawLink(link, catalog);
void KPDFOutputDev::updateFont( GfxState *state )
{
if ( m_generateText )
m_text->updateFont( state );
SplashOutputDev::updateFont( state );
}
void KPDFOutputDev::updateFont(GfxState *state)
void KPDFOutputDev::drawChar( GfxState *state, double x, double y, double dx, double dy, double originX, double originY, CharCode code, Unicode *u, int uLen )
{
SplashOutputDev::updateFont(state);
if ( m_text )
m_text->updateFont(state);
if ( m_generateText )
m_text->addChar( state, x, y, dx, dy, code, u, uLen );
SplashOutputDev::drawChar( state, x, y, dx, dy, originX, originY, code, u, uLen );
}
void KPDFOutputDev::drawChar(GfxState *state, double x, double y, double dx, double dy, double originX, double originY, CharCode code, Unicode *u, int uLen)
GBool KPDFOutputDev::beginType3Char( GfxState *state, double x, double y, double dx, double dy, CharCode code, Unicode *u, int uLen )
{
if ( m_text )
m_text->addChar(state, x, y, dx, dy, code, u, uLen);
SplashOutputDev::drawChar(state, x, y, dx, dy, originX, originY, code, u, uLen);
if ( m_generateText )
m_text->addChar( state, x, y, dx, dy, code, u, uLen );
return SplashOutputDev::beginType3Char( state, x, y, dx, dy, code, u, uLen );
}
GBool KPDFOutputDev::beginType3Char(GfxState *state, double x, double y, double dx, double dy, CharCode code, Unicode *u, int uLen)
void KPDFOutputDev::drawImage( GfxState *state, Object *ref, Stream *str,
int _width, int _height, GfxImageColorMap *colorMap, int *maskColors, GBool inlineImg )
{
if ( m_text )
m_text->addChar(state, x, y, dx, dy, code, u, uLen);
return SplashOutputDev::beginType3Char(state, x, y, dx, dy, code, u, uLen);
if ( m_generateActiveRects )
{
// find out image rect from the Coord Transform Matrix
double * ctm = state->getCTM();
int left = (int)ctm[4],
top = (int)ctm[5],
width = (int)ctm[0],
height = (int)ctm[3];
// normalize width
if ( width < 0 )
{
width = -width;
left -= width;
}
// normalize height
if ( height < 0 )
{
height = -height;
top -= height;
}
if ( width > 10 && height > 10 )
{
KPDFActiveRect * r = new KPDFActiveRect( left, top, width, height );
// add the rect to the vector container
m_rects.push_back( r );
}
}
SplashOutputDev::drawImage( state, ref, str, _width, _height, colorMap, maskColors, inlineImg );
}
void KPDFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
int _width, int _height, GfxImageColorMap *colorMap,
int *maskColors, GBool inlineImg)
void KPDFOutputDev::clear()
{
// find out image rect from the Coord Transform Matrix
double * ctm = state->getCTM();
int left = (int)ctm[4],
top = (int)ctm[5],
width = (int)ctm[0],
height = (int)ctm[3];
// normalize width
if ( width < 0 )
// delete links
if ( m_links.count() )
{
width = -width;
left -= width;
QValueList< KPDFLink * >::iterator it = m_links.begin(), end = m_links.end();
for ( ; it != end; ++it )
delete *it;
m_links.clear();
}
// normalize height
if ( height < 0 )
// delete activerects
if ( m_rects.count() )
{
height = -height;
top -= height;
QValueList< KPDFActiveRect * >::iterator it = m_rects.begin(), end = m_rects.end();
for ( ; it != end; ++it )
delete *it;
m_rects.clear();
}
if ( width > 10 && height > 10 )
// delete pixmap
if ( m_pixmap )
{
KPDFActiveRect * r = new KPDFActiveRect(left, top, width, height);
// add the rect to the vector container
m_rects.push_back( r );
delete m_pixmap;
m_pixmap = 0;
}
// delete text
if ( m_text )
{
delete m_text;
m_text = 0;
}
// call parent's image handler
SplashOutputDev::drawImage(state, ref, str, _width, _height, colorMap, maskColors, inlineImg);
}
// END KPDFOutputDev
......@@ -212,41 +228,41 @@ void KPDFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
// BEGIN KPDFTextDev
KPDFTextDev::KPDFTextDev()
{
m_text = new TextPage( gFalse );
m_text = new TextPage( gFalse );
}
KPDFTextDev::~KPDFTextDev()
{
delete m_text;
delete m_text;
}
TextPage * KPDFTextDev::takeTextPage()
{
TextPage * t = m_text;
m_text = 0;
return t;
TextPage * t = m_text;
m_text = 0;
return t;
}
void KPDFTextDev::startPage(int, GfxState *state)
void KPDFTextDev::startPage( int, GfxState *state )
{
if ( !m_text )
m_text = new TextPage( gFalse );
m_text->startPage(state);
if ( !m_text )
m_text = new TextPage( gFalse );
m_text->startPage( state );
}
void KPDFTextDev::endPage()
{
m_text->endPage();
m_text->coalesce(gTrue);
m_text->endPage();
m_text->coalesce( gTrue );
}
void KPDFTextDev::updateFont(GfxState *state)
void KPDFTextDev::updateFont( GfxState *state )
{
m_text->updateFont(state);
m_text->updateFont( state );
}
void KPDFTextDev::drawChar(GfxState *state, double x, double y, double dx, double dy, double /*originX*/, double /*originY*/, CharCode code, Unicode *u, int uLen)
void KPDFTextDev::drawChar( GfxState *state, double x, double y, double dx, double dy, double /*originX*/, double /*originY*/, CharCode code, Unicode *u, int uLen )
{
m_text->addChar(state, x, y, dx, dy, code, u, uLen);
m_text->addChar( state, x, y, dx, dy, code, u, uLen );
}
// END KPDFTextDev
......@@ -29,58 +29,64 @@ class KPDFActiveRect;
/**
* @short A SplashOutputDev renderer that grabs text and links.
*
* This output device:
* This output device:
* - renders the page using SplashOutputDev (its parent)
* - harvests text into a textPage (for searching text)
* - harvests links and collect them
* - collects images and collect them as generic 'activerects'
*/
class KPDFOutputDev : public SplashOutputDev
{
public:
KPDFOutputDev( SplashColor paperColor );
virtual ~KPDFOutputDev();
// to be called before PDFDoc->displayPage( thisclass, .. )
void setParams( int pixmapWidth, int pixmapHeight, bool generateTextpage, bool generateLinks );
// takes pointers out of the class (so deletion it's up to others)
QPixmap * takePixmap();
TextPage * takeTextPage();
QValueList< KPDFLink * > takeLinks();
QValueList< KPDFActiveRect * > takeActiveRects();
/** inherited from OutputDev */
// Start a page.
virtual void startPage(int pageNum, GfxState *state);
// End a page.
virtual void endPage();
//----- link borders
virtual void drawLink(Link *link, Catalog *catalog);
//----- update text state
virtual void updateFont(GfxState *state);
//----- text drawing
virtual void drawChar(GfxState *state, double x, double y, double dx, double dy, double originX, double originY, CharCode code, Unicode *u, int uLen);
virtual GBool beginType3Char(GfxState *state, double x, double y, double dx, double dy, CharCode code, Unicode *u, int uLen);
//----- image drawing
virtual void drawImage(GfxState *state, Object *ref, Stream *str,
int width, int height, GfxImageColorMap *colorMap,
int *maskColors, GBool inlineImg);
private:
// the pixmap where the page is drawn (generated on every execution)
int m_pageNum;
int m_pixmapWidth;
int m_pixmapHeight;
QPixmap * m_pixmap;
// text page generated on demand
TextPage * m_text;
// links generated on demand
QValueList< KPDFLink * > m_links;
// active areas on page
QValueList< KPDFActiveRect * > m_rects;
public:
KPDFOutputDev( SplashColor paperColor );
virtual ~KPDFOutputDev();
// to be called before PDFDoc->displayPage( thisclass, .. )
void setParams( int pixmapWidth, int pixmapHeight, bool generateTextpage,
bool generateLinks, bool generateActiveRects );
// takes pointers out of the class (so deletion it's up to others)
QPixmap * takePixmap();
TextPage * takeTextPage();
QValueList< KPDFLink * > takeLinks();
QValueList< KPDFActiveRect * > takeActiveRects();
/** inherited from OutputDev */
// Start a page.
virtual void startPage(int pageNum, GfxState *state);
// End a page.
virtual void endPage();
//----- link borders
virtual void drawLink(Link *link, Catalog *catalog);
//----- update text state
virtual void updateFont(GfxState *state);
//----- text drawing
virtual void drawChar(GfxState *state, double x, double y, double dx, double dy, double originX, double originY, CharCode code, Unicode *u, int uLen);
virtual GBool beginType3Char(GfxState *state, double x, double y, double dx, double dy, CharCode code, Unicode *u, int uLen);
//----- image drawing
virtual void drawImage(GfxState *state, Object *ref, Stream *str, int width, int height,
GfxImageColorMap *colorMap, int *maskColors, GBool inlineImg);
private:
// delete all interal objects and data
void clear();
// generator switches and parameters
bool m_generateText;
bool m_generateLinks;
bool m_generateActiveRects;
int m_pixmapWidth;
int m_pixmapHeight;
QPixmap * m_pixmap;
// text page generated on demand
TextPage * m_text;
// links generated on demand
QValueList< KPDFLink * > m_links;
// active areas on page
QValueList< KPDFActiveRect * > m_rects;
};
......@@ -94,35 +100,35 @@ private:
*/
class KPDFTextDev : public OutputDev
{
public:
KPDFTextDev();
virtual ~KPDFTextDev();
// takes pointers out of the class (so deletion it's up to others)
TextPage * takeTextPage();
/** inherited from OutputDev */
// top left corner is (0,0)
virtual GBool upsideDown() { return gTrue; }
// use draw char to get text data
virtual GBool useDrawChar() { return gTrue; }
// use drawChar even for Type3 chars
virtual GBool interpretType3Chars() { return gFalse; }
// do not pass non-text to this device
virtual GBool needNonText() { return gFalse; }
// Start a page.
virtual void startPage(int, GfxState *state);
// End a page.
virtual void endPage();
//----- update text state
virtual void updateFont(GfxState *state);
//----- text drawing
virtual void drawChar(GfxState *state, double x, double y, double dx, double dy, double originX, double originY, CharCode code, Unicode *u, int uLen);
private:
// text page generated by execution
TextPage * m_text;
public:
KPDFTextDev();
virtual ~KPDFTextDev();
// takes pointers out of the class (so deletion it's up to others)
TextPage * takeTextPage();
/** inherited from OutputDev */
// top left corner is (0,0)
virtual GBool upsideDown() { return gTrue; }
// use draw char to get text data
virtual GBool useDrawChar() { return gTrue; }
// use drawChar even for Type3 chars
virtual GBool interpretType3Chars() { return gFalse; }
// do not pass non-text to this device
virtual GBool needNonText() { return gFalse; }
// Start a page.
virtual void startPage(int, GfxState *state);
// End a page.
virtual void endPage();
//----- update text state
virtual void updateFont(GfxState *state);
//----- text drawing
virtual void drawChar(GfxState *state, double x, double y, double dx, double dy, double originX, double originY, CharCode code, Unicode *u, int uLen);
private:
// text page generated by execution
TextPage * m_text;
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment