* Add support for style families (text looks much more consistent now)

* Add support for images
 * Add support for spaces and tabs

svn path=/trunk/playground/graphics/okular/; revision=585220
This commit is contained in:
Tobias Koenig 2006-09-16 15:52:00 +00:00
parent 2202f9cf3e
commit dd5b91ca40
8 changed files with 152 additions and 32 deletions

View file

@ -7,6 +7,7 @@
* (at your option) any later version. *
***************************************************************************/
#include <QtCore/QUrl>
#include <QtGui/QTextCursor>
#include <QtGui/QTextDocument>
#include <QtGui/QTextFrame>
@ -61,6 +62,15 @@ bool Converter::convert()
if ( !styleParser.parse() )
return false;
// add images to resource framework
const QMap<QString, QByteArray> images = mDocument->images();
QMapIterator<QString, QByteArray> it( images );
while ( it.hasNext() ) {
it.next();
mTextDocument->addResource( QTextDocument::ImageResource, QUrl( it.key() ), QImage::fromData( it.value() ) );
}
const QString masterLayout = mStyleInformation->masterLayout( "Standard" );
const PageFormatProperty property = mStyleInformation->pageProperty( masterLayout );
mTextDocument->setPageSize( QSize( qRound( property.width() ), qRound( property.height() ) ) );
@ -187,6 +197,15 @@ bool Converter::convertParagraph( QTextCursor *cursor, const QDomElement &elemen
if ( childElement.tagName() == QLatin1String( "span" ) ) {
if ( !convertSpan( cursor, childElement, textFormat ) )
return false;
} else if ( childElement.tagName() == QLatin1String( "tab" ) ) {
mCursor->insertText( " " );
} else if ( childElement.tagName() == QLatin1String( "s" ) ) {
QString spaces;
spaces.fill( ' ', childElement.attribute( "c" ).toInt() );
mCursor->insertText( spaces );
} else if ( childElement.tagName() == QLatin1String( "frame" ) ) {
if ( !convertFrame( childElement ) )
return false;
}
} else if ( child.isText() ) {
const QDomText childText = child.toText();
@ -272,6 +291,25 @@ bool Converter::convertTable( const QDomElement &element )
return true;
}
bool Converter::convertFrame( const QDomElement &element )
{
QDomElement child = element.firstChildElement();
while ( !child.isNull() ) {
if ( child.tagName() == QLatin1String( "image" ) ) {
const QString href = child.attribute( "href" );
QTextImageFormat format;
format.setWidth( StyleParser::convertUnit( element.attribute( "width" ) ) );
format.setHeight( StyleParser::convertUnit( element.attribute( "height" ) ) );
format.setName( href );
mCursor->insertImage( format );
}
child = child.nextSiblingElement();
}
return true;
}
QTextDocument *Converter::textDocument() const
{

View file

@ -55,6 +55,7 @@ class Converter
bool convertSpan( QTextCursor *cursor, const QDomElement &element, const QTextCharFormat &format );
bool convertList( const QDomElement &element );
bool convertTable( const QDomElement &element );
bool convertFrame( const QDomElement &element );
private:
const Document *mDocument;

View file

@ -55,6 +55,18 @@ bool Document::open()
file = static_cast<const KArchiveFile*>( directory->entry( "meta.xml" ) );
mMeta = file->data();
if ( !entries.contains( "Pictures" ) ) {
return false;
}
const KArchiveDirectory *imagesDirectory = static_cast<const KArchiveDirectory*>( directory->entry( "Pictures" ) );
const QStringList imagesEntries = imagesDirectory->entries();
for ( int i = 0; i < imagesEntries.count(); ++i ) {
file = static_cast<const KArchiveFile*>( imagesDirectory->entry( imagesEntries[ i ] ) );
mImages.insert( QString( "Pictures/%1" ).arg( imagesEntries[ i ] ), file->data() );
}
zip.close();
return true;
@ -74,3 +86,8 @@ QByteArray Document::styles() const
{
return mStyles;
}
QMap<QString, QByteArray> Document::images() const
{
return mImages;
}

View file

@ -11,6 +11,7 @@
#define OOO_DOCUMENT_H
#include <QtCore/QByteArray>
#include <QtCore/QMap>
#include <QtCore/QString>
namespace OOO {
@ -25,12 +26,14 @@ class Document
QByteArray content() const;
QByteArray meta() const;
QByteArray styles() const;
QMap<QString, QByteArray> images() const;
private:
QString mFileName;
QByteArray mContent;
QByteArray mMeta;
QByteArray mStyles;
QMap<QString, QByteArray> mImages;
};
}

View file

@ -48,6 +48,9 @@ void ParagraphFormatProperty::apply( QTextFormat *format ) const
}
format->setProperty( QTextFormat::FrameWidth, 595 );
if ( mBackgroundColor.isValid() )
format->setBackground( mBackgroundColor );
}
void ParagraphFormatProperty::setPageNumber( int number )
@ -66,27 +69,25 @@ void ParagraphFormatProperty::setTextAlignment( Qt::Alignment alignment )
mAlignment = alignment;
}
void ParagraphFormatProperty::setBackgroundColor( const QColor &color )
{
mBackgroundColor = color;
}
TextFormatProperty::TextFormatProperty()
: mStyleInformation( 0 ), mHasFontSize( false ), mTextPosition( 0 )
: mStyleInformation( 0 ), mHasFontSize( false ),
mFontWeight( -1 ), mTextPosition( 0 )
{
}
TextFormatProperty::TextFormatProperty( const StyleInformation *information )
: mStyleInformation( information ), mHasFontSize( false ), mTextPosition( 0 )
: mStyleInformation( information ), mHasFontSize( false ),
mFontWeight( -1 ), mTextPosition( 0 )
{
}
void TextFormatProperty::apply( QTextFormat *format ) const
void TextFormatProperty::apply( QTextCharFormat *format ) const
{
if ( mHasFontSize )
format->setProperty( QTextFormat::FontPointSize, mFontSize );
if ( mColor.isValid() )
format->setForeground( mColor );
if ( mBackgroundColor.isValid() )
format->setBackground( mBackgroundColor );
if ( !mFontName.isEmpty() ) {
if ( mStyleInformation ) {
const FontFormatProperty property = mStyleInformation->fontProperty( mFontName );
@ -94,9 +95,26 @@ void TextFormatProperty::apply( QTextFormat *format ) const
}
}
if ( mFontWeight != -1 ) {
QFont font = format->font();
font.setWeight( mFontWeight );
format->setFont( font );
}
if ( mHasFontSize ) {
QFont font = format->font();
font.setPointSize( mFontSize );
format->setFont( font );
}
if ( mColor.isValid() )
format->setForeground( mColor );
if ( mBackgroundColor.isValid() )
format->setBackground( mBackgroundColor );
// TODO: get FontFormatProperty and apply it
// TODO: how to set the base line?!?
}
void TextFormatProperty::setFontSize( int size )
@ -110,6 +128,11 @@ void TextFormatProperty::setFontName( const QString &name )
mFontName = name;
}
void TextFormatProperty::setFontWeight( int weight )
{
mFontWeight = weight;
}
void TextFormatProperty::setTextPosition( int position )
{
mTextPosition = position;
@ -126,17 +149,22 @@ void TextFormatProperty::setBackgroundColor( const QColor &color )
}
StyleFormatProperty::StyleFormatProperty()
: mStyleInformation( 0 )
: mStyleInformation( 0 ), mDefaultStyle( false )
{
}
StyleFormatProperty::StyleFormatProperty( const StyleInformation *information )
: mStyleInformation( information )
: mStyleInformation( information ), mDefaultStyle( false )
{
}
void StyleFormatProperty::apply( QTextBlockFormat *blockFormat, QTextCharFormat *textFormat ) const
{
if ( !mDefaultStyle && !mFamily.isEmpty() && mStyleInformation ) {
const StyleFormatProperty property = mStyleInformation->styleProperty( mFamily );
property.apply( blockFormat, textFormat );
}
if ( !mParentStyleName.isEmpty() && mStyleInformation ) {
const StyleFormatProperty property = mStyleInformation->styleProperty( mParentStyleName );
property.apply( blockFormat, textFormat );
@ -161,6 +189,11 @@ void StyleFormatProperty::setFamily( const QString &family )
mFamily = family;
}
void StyleFormatProperty::setDefaultStyle( bool defaultStyle )
{
mDefaultStyle = defaultStyle;
}
void StyleFormatProperty::setMasterPageName( const QString &masterPageName )
{
mMasterPageName = masterPageName;

View file

@ -67,25 +67,28 @@ class ParagraphFormatProperty : public FormatProperty
void setPageNumber( int number );
void setWritingMode( WritingMode mode );
void setTextAlignment( Qt::Alignment alignment );
void setBackgroundColor( const QColor &color );
private:
int mPageNumber;
WritingMode mWritingMode;
Qt::Alignment mAlignment;
bool mHasAlignment;
QColor mBackgroundColor;
};
class TextFormatProperty : public FormatProperty
class TextFormatProperty
{
public:
TextFormatProperty();
TextFormatProperty( const StyleInformation *information );
virtual ~TextFormatProperty() {}
virtual void apply( QTextFormat *format ) const;
virtual void apply( QTextCharFormat *format ) const;
void setFontSize( int size );
void setFontName( const QString &name );
void setFontWeight( int weight );
void setTextPosition( int position );
void setColor( const QColor &color );
void setBackgroundColor( const QColor &color );
@ -94,6 +97,7 @@ class TextFormatProperty : public FormatProperty
const StyleInformation *mStyleInformation;
int mFontSize;
bool mHasFontSize;
int mFontWeight;
QString mFontName;
int mTextPosition;
QColor mColor;
@ -113,6 +117,8 @@ class StyleFormatProperty
QString parentStyleName() const;
void setFamily( const QString &family );
void setDefaultStyle( bool defaultStyle );
void setMasterPageName( const QString &masterPageName );
void setParagraphFormat( const ParagraphFormatProperty &format );
@ -125,6 +131,7 @@ class StyleFormatProperty
ParagraphFormatProperty mParagraphFormat;
TextFormatProperty mTextFormat;
const StyleInformation *mStyleInformation;
bool mDefaultStyle;
};
class PageFormatProperty : public FormatProperty

View file

@ -8,6 +8,7 @@
***************************************************************************/
#include <QtCore/QDateTime>
#include <QtGui/QFont>
#include <QtXml/QDomDocument>
#include <QtXml/QDomElement>
#include <QtXml/QXmlSimpleReader>
@ -162,6 +163,7 @@ bool StyleParser::parseMetaFile()
return true;
}
bool StyleParser::parseDocumentCommonAttrs( QDomElement& )
{
return true;
@ -227,6 +229,10 @@ bool StyleParser::parseAutomaticStyles( QDomElement &parent )
} else if ( element.tagName() == QLatin1String( "list-style" ) ) {
const ListFormatProperty property = parseListProperty( element );
mStyleInformation->addListProperty( element.attribute( "name" ), property );
} else if ( element.tagName() == QLatin1String( "default-style" ) ) {
StyleFormatProperty property = parseStyleProperty( element );
property.setDefaultStyle( true );
mStyleInformation->addStyleProperty( element.attribute( "family" ), property );
} else {
qDebug( "unknown tag %s", qPrintable( element.tagName() ) );
}
@ -292,6 +298,10 @@ ParagraphFormatProperty StyleParser::parseParagraphProperty( QDomElement &parent
property.setTextAlignment( alignMap[ parent.attribute( "text-align", "left" ) ] );
}
if ( parent.hasAttribute( "background-color" ) ) {
property.setBackgroundColor( parent.attribute( "background-color" ) );
}
return property;
}
@ -299,11 +309,21 @@ TextFormatProperty StyleParser::parseTextProperty( QDomElement &parent )
{
TextFormatProperty property;
QString fontSize = parent.attribute( "font-size" );
const QString fontSize = parent.attribute( "font-size" );
if ( !fontSize.isEmpty() )
property.setFontSize( qRound( convertUnit( fontSize ) ) );
QColor color( parent.attribute( "color" ) );
static QMap<QString, QFont::Weight> weightMap;
if ( weightMap.isEmpty() ) {
weightMap.insert( "normal", QFont::Normal );
weightMap.insert( "bold", QFont::Bold );
}
const QString fontWeight = parent.attribute( "font-weight" );
if ( !fontWeight.isEmpty() )
property.setFontWeight( weightMap[ fontWeight ] );
const QColor color( parent.attribute( "color" ) );
if ( color.isValid() ) {
property.setColor( color );
}
@ -355,7 +375,7 @@ ListFormatProperty StyleParser::parseListProperty( QDomElement &parent )
return property;
}
double StyleParser::convertUnit( const QString &data ) const
double StyleParser::convertUnit( const QString &data )
{
#define MM_TO_POINT(mm) ((mm)*2.83465058)
#define CM_TO_POINT(cm) ((cm)*28.3465058)
@ -365,35 +385,36 @@ double StyleParser::convertUnit( const QString &data ) const
#define DD_TO_POINT(dd) ((dd)*154.08124)
#define CC_TO_POINT(cc) ((cc)*12.840103)
double points = 0;
if ( data.endsWith( "pt" ) ) {
return data.left( data.length() - 2 ).toDouble();
points = data.left( data.length() - 2 ).toDouble();
} else if ( data.endsWith( "cm" ) ) {
double value = data.left( data.length() - 2 ).toDouble();
return CM_TO_POINT( value );
points = CM_TO_POINT( value );
} else if ( data.endsWith( "mm" ) ) {
double value = data.left( data.length() - 2 ).toDouble();
return MM_TO_POINT( value );
points = MM_TO_POINT( value );
} else if ( data.endsWith( "dm" ) ) {
double value = data.left( data.length() - 2 ).toDouble();
return DM_TO_POINT( value );
points = DM_TO_POINT( value );
} else if ( data.endsWith( "in" ) ) {
double value = data.left( data.length() - 2 ).toDouble();
return INCH_TO_POINT( value );
points = INCH_TO_POINT( value );
} else if ( data.endsWith( "inch" ) ) {
double value = data.left( data.length() - 4 ).toDouble();
return INCH_TO_POINT( value );
points = INCH_TO_POINT( value );
} else if ( data.endsWith( "pi" ) ) {
double value = data.left( data.length() - 4 ).toDouble();
return PI_TO_POINT( value );
points = PI_TO_POINT( value );
} else if ( data.endsWith( "dd" ) ) {
double value = data.left( data.length() - 4 ).toDouble();
return DD_TO_POINT( value );
points = DD_TO_POINT( value );
} else if ( data.endsWith( "cc" ) ) {
double value = data.left( data.length() - 4 ).toDouble();
return CC_TO_POINT( value );
points = CC_TO_POINT( value );
} else {
qDebug( "unknown unit %s", qPrintable( data ) );
}
return 0;
return points;
}

View file

@ -24,6 +24,8 @@ class StyleParser
bool parse();
static double convertUnit( const QString& );
private:
bool parseContentFile();
bool parseStyleFile();
@ -41,8 +43,6 @@ class StyleParser
PageFormatProperty parsePageProperty( QDomElement& );
ListFormatProperty parseListProperty( QDomElement& );
double convertUnit( const QString& ) const;
const Document *mDocument;
StyleInformation *mStyleInformation;
};