You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
297 lines
10 KiB
297 lines
10 KiB
15 years ago
|
/* This file is part of the KDE project
|
||
|
Copyright (C) 2006 Alfredo Beaumont Sainz <alfredo.beaumont@gmail.com>
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License as published by the Free Software Foundation; either
|
||
|
version 2 of the License, or (at your option) any later version.
|
||
|
|
||
|
This library is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
* Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
|
#include "elementtype.h"
|
||
|
#include "paddedelement.h"
|
||
|
|
||
|
KFORMULA_NAMESPACE_BEGIN
|
||
|
|
||
13 years ago
|
PaddedElement::PaddedElement( BasicElement* parent ) : SequenceElement( parent ),
|
||
15 years ago
|
m_widthType( NoSize ),
|
||
|
m_lspaceType( NoSize ),
|
||
|
m_heightType( NoSize ),
|
||
|
m_depthType( NoSize ),
|
||
|
m_widthRelative( false ),
|
||
|
m_lspaceRelative( false ),
|
||
|
m_heightRelative( false ),
|
||
|
m_depthRelative( false )
|
||
|
{
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Calculates our width and height and
|
||
13 years ago
|
* our children's parentPosition.
|
||
15 years ago
|
*/
|
||
|
void PaddedElement::calcSizes( const ContextStyle& context,
|
||
|
ContextStyle::TextStyle tstyle,
|
||
|
ContextStyle::IndexStyle istyle,
|
||
|
StyleAttributes& style )
|
||
|
{
|
||
|
double factor = style.sizeFactor();
|
||
|
|
||
|
luPixel width = 0;
|
||
|
luPixel height = 0;
|
||
|
luPixel depth = 0;
|
||
|
|
||
|
if ( !isEmpty() ) {
|
||
|
// First, get content height and width
|
||
|
for ( iterator it = begin(); it != end(); ++it ) {
|
||
|
luPixel spaceBefore = 0;
|
||
|
if ( it == begin() ) {
|
||
|
spaceBefore = context.ptToPixelX( getSpaceBefore( context, tstyle, factor ) );
|
||
|
}
|
||
|
it->calcSizes( context, tstyle, istyle, style );
|
||
|
width += it->getWidth() + spaceBefore;
|
||
|
luPixel baseline = it->getBaseline();
|
||
|
if ( baseline > -1 ) {
|
||
14 years ago
|
height = TQMAX( height, baseline );
|
||
|
depth = TQMAX( depth, it->getHeight() - baseline );
|
||
15 years ago
|
}
|
||
|
else {
|
||
|
luPixel bl = it->getHeight()/2 + context.axisHeight( tstyle, factor );
|
||
14 years ago
|
height = TQMAX( height, bl );
|
||
|
depth = TQMAX( depth, it->getHeight() - bl );
|
||
15 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
width = context.getEmptyRectWidth( factor );
|
||
|
height = context.getEmptyRectHeight( factor );
|
||
|
depth = 0;
|
||
|
}
|
||
|
|
||
|
luPixel left = calcSize( context, m_lspaceType, m_lspaceRelative, m_lspace, width, height, 0 );
|
||
|
luPixel right = calcSize( context, m_widthType, m_widthRelative, m_width, width, height, width ) + left;
|
||
|
luPixel down = calcSize( context, m_depthType, m_depthRelative, m_depth, width, height, depth );
|
||
|
luPixel up = calcSize( context, m_heightType, m_heightRelative, m_height, width, height, height );
|
||
|
|
||
|
// Check borders
|
||
|
if ( right < 0 ) right = 0;
|
||
|
if ( up + down < 0 ) up = down = 0;
|
||
|
|
||
|
if ( ! isEmpty() ) {
|
||
|
width = left;
|
||
|
// Let's do all normal elements that have a base line.
|
||
|
for ( iterator it = begin(); it != end(); ++it ) {
|
||
|
luPixel spaceBefore = 0;
|
||
|
if ( it == begin() ) {
|
||
|
spaceBefore = context.ptToPixelX( getSpaceBefore( context, tstyle, factor ) );
|
||
|
}
|
||
|
it->calcSizes( context, tstyle, istyle, style );
|
||
|
it->setX( width + spaceBefore );
|
||
|
width += it->getWidth() + spaceBefore;
|
||
|
}
|
||
|
|
||
|
setWidth( right );
|
||
|
setHeight( up + down );
|
||
|
setBaseline( up );
|
||
|
setChildrenPositions();
|
||
|
}
|
||
|
else {
|
||
|
setWidth( right );
|
||
|
setHeight( up + down );
|
||
|
setBaseline( up );
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
bool PaddedElement::readAttributesFromMathMLDom(const TQDomElement& element)
|
||
15 years ago
|
{
|
||
|
if ( ! BasicElement::readAttributesFromMathMLDom( element ) ) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString widthStr = element.attribute( "width" ).stripWhiteSpace().lower();
|
||
15 years ago
|
if ( ! widthStr.isNull() ) {
|
||
|
m_width = readSizeAttribute( widthStr, &m_widthType, &m_widthRelative );
|
||
|
}
|
||
14 years ago
|
TQString lspaceStr = element.attribute( "lspace" ).stripWhiteSpace().lower();
|
||
15 years ago
|
if ( ! lspaceStr.isNull() ) {
|
||
|
m_lspace = readSizeAttribute( lspaceStr, &m_lspaceType, &m_lspaceRelative );
|
||
|
}
|
||
14 years ago
|
TQString heightStr = element.attribute( "height" ).stripWhiteSpace().lower();
|
||
15 years ago
|
if ( ! heightStr.isNull() ) {
|
||
|
m_height = readSizeAttribute( heightStr, &m_heightType, &m_heightRelative );
|
||
|
}
|
||
14 years ago
|
TQString depthStr = element.attribute( "depth" ).stripWhiteSpace().lower();
|
||
15 years ago
|
if ( ! depthStr.isNull() ) {
|
||
|
m_depth = readSizeAttribute( depthStr, &m_depthType, &m_depthRelative );
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
14 years ago
|
void PaddedElement::writeMathMLAttributes( TQDomElement& element ) const
|
||
15 years ago
|
{
|
||
|
writeSizeAttribute( element, "width", m_widthType, m_width, m_widthRelative );
|
||
|
writeSizeAttribute( element, "lspace", m_lspaceType, m_lspace, m_lspaceRelative );
|
||
|
writeSizeAttribute( element, "height", m_heightType, m_height, m_heightRelative );
|
||
|
writeSizeAttribute( element, "depth", m_depthType, m_depth, m_depthRelative );
|
||
|
}
|
||
|
|
||
14 years ago
|
double PaddedElement::readSizeAttribute( const TQString& str, SizeType* st, bool* relative )
|
||
15 years ago
|
{
|
||
|
if ( st == 0 ){
|
||
|
return -1;
|
||
|
}
|
||
|
if ( str[0] == '+' || str[0] == '-' ) {
|
||
|
*relative = true;
|
||
|
}
|
||
13 years ago
|
int index = str.find( "width" );
|
||
15 years ago
|
if ( index != -1 ) {
|
||
13 years ago
|
int index2 = str.find( "%" );
|
||
15 years ago
|
if ( index2 != -1 ) {
|
||
|
return str2size( str.left( index2 ).stripWhiteSpace(), st, WidthRelativeSize ) / 100.0;
|
||
|
}
|
||
|
return str2size( str.left( index ).stripWhiteSpace(), st, WidthRelativeSize );
|
||
|
}
|
||
13 years ago
|
index = str.find( "height" );
|
||
15 years ago
|
if ( index != -1 ) {
|
||
13 years ago
|
int index2 = str.find( "%" );
|
||
15 years ago
|
if ( index2 != -1 ) {
|
||
|
return str2size( str.left( index2 ).stripWhiteSpace(), st, HeightRelativeSize ) / 100.0;
|
||
|
}
|
||
|
return str2size( str.left( index ).stripWhiteSpace(), st, HeightRelativeSize );
|
||
|
}
|
||
13 years ago
|
index = str.find( "%" );
|
||
15 years ago
|
if ( index != -1 ) {
|
||
|
return str2size( str.left( index ).stripWhiteSpace(), st, RelativeSize ) / 100.0;
|
||
|
}
|
||
13 years ago
|
index = str.find( "pt", 0, false );
|
||
15 years ago
|
if ( index != -1 ) {
|
||
|
return str2size( str.left( index ).stripWhiteSpace(), st, AbsoluteSize );
|
||
|
}
|
||
13 years ago
|
index = str.find( "mm", 0, false );
|
||
15 years ago
|
if ( index != -1 ) {
|
||
|
return str2size( str.left( index ).stripWhiteSpace(), st, AbsoluteSize ) * 72.0 / 20.54;
|
||
|
}
|
||
13 years ago
|
index = str.find( "cm", 0, false );
|
||
15 years ago
|
if ( index != -1 ) {
|
||
|
return str2size( str.left( index ).stripWhiteSpace(), st, AbsoluteSize ) * 72.0 / 2.54;
|
||
|
}
|
||
13 years ago
|
index = str.find( "in", 0, false );
|
||
15 years ago
|
if ( index != -1 ) {
|
||
|
return str2size( str.left( index ).stripWhiteSpace(), st, AbsoluteSize ) * 72.0;
|
||
|
}
|
||
13 years ago
|
index = str.find( "em", 0, false );
|
||
15 years ago
|
if ( index != -1 ) {
|
||
|
return str2size( str.left( index ).stripWhiteSpace(), st, RelativeSize );
|
||
|
}
|
||
13 years ago
|
index = str.find( "ex", 0, false );
|
||
15 years ago
|
if ( index != -1 ) {
|
||
|
return str2size( str.left( index ).stripWhiteSpace(), st, RelativeSize );
|
||
|
}
|
||
13 years ago
|
index = str.find( "pc", 0, false );
|
||
15 years ago
|
if ( index != -1 ) {
|
||
|
return str2size( str.left( index ).stripWhiteSpace(), st, AbsoluteSize ) * 12.0;
|
||
|
}
|
||
13 years ago
|
index = str.find( "px", 0, false );
|
||
15 years ago
|
if ( index != -1 ) {
|
||
|
return str2size( str.left( index ).stripWhiteSpace(), st, PixelSize );
|
||
|
}
|
||
|
// If there's no unit, assume 'pt'
|
||
|
return str2size( str, st, AbsoluteSize );
|
||
|
}
|
||
|
|
||
14 years ago
|
double PaddedElement::str2size( const TQString& str, SizeType *st, SizeType type )
|
||
15 years ago
|
{
|
||
|
bool ok;
|
||
|
double size = str.toDouble( &ok );
|
||
|
if ( ok ) {
|
||
|
if ( st ) {
|
||
|
*st = type;
|
||
|
}
|
||
|
return size;
|
||
|
}
|
||
|
if ( st ) {
|
||
|
*st = NoSize;
|
||
|
}
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
14 years ago
|
void PaddedElement::writeSizeAttribute( TQDomElement element, const TQString& str,
|
||
15 years ago
|
SizeType st, bool relative, double s ) const
|
||
|
{
|
||
14 years ago
|
TQString prefix;
|
||
15 years ago
|
if ( relative ) {
|
||
|
s < 0 ? prefix = "-" : prefix = "+" ;
|
||
|
}
|
||
|
switch ( st ) {
|
||
|
case WidthRelativeSize:
|
||
13 years ago
|
element.setAttribute( str, prefix + TQString( "%1 width" ).arg( s ) );
|
||
15 years ago
|
break;
|
||
|
case HeightRelativeSize:
|
||
13 years ago
|
element.setAttribute( str, prefix + TQString( "%1 height" ).arg( s ) );
|
||
15 years ago
|
case AbsoluteSize:
|
||
13 years ago
|
element.setAttribute( str, prefix + TQString( "%1pt" ).arg( s ) );
|
||
15 years ago
|
break;
|
||
|
case RelativeSize:
|
||
13 years ago
|
element.setAttribute( str, prefix + TQString( "%1%" ).arg( s * 100.0 ) );
|
||
15 years ago
|
break;
|
||
|
case PixelSize:
|
||
13 years ago
|
element.setAttribute( str, prefix + TQString( "%1px" ).arg( s ) );
|
||
15 years ago
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
luPixel PaddedElement::calcSize( const ContextStyle& context, SizeType type,
|
||
|
bool relative, double length, luPixel width,
|
||
|
luPixel height, luPixel defvalue )
|
||
|
{
|
||
|
luPixel value = defvalue;
|
||
|
switch ( type ) {
|
||
|
case AbsoluteSize:
|
||
|
if ( relative )
|
||
|
value += context.ptToLayoutUnitPt ( length );
|
||
|
else
|
||
|
value = context.ptToLayoutUnitPt( length );
|
||
|
break;
|
||
|
case RelativeSize:
|
||
|
if ( relative )
|
||
|
value += length * value;
|
||
|
else
|
||
|
value *= length;
|
||
|
break;
|
||
|
case WidthRelativeSize:
|
||
|
if ( relative )
|
||
|
value += length * width;
|
||
|
else
|
||
|
value = length * width;
|
||
|
break;
|
||
|
case HeightRelativeSize:
|
||
|
if ( relative )
|
||
|
value += length * height;
|
||
|
else
|
||
|
value = length * height;
|
||
|
break;
|
||
|
case PixelSize:
|
||
|
if ( relative )
|
||
|
value += context.pixelToLayoutUnitX( length );
|
||
|
else
|
||
|
value = context.pixelToLayoutUnitX( length );
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
return value;
|
||
|
}
|
||
|
|
||
|
KFORMULA_NAMESPACE_END
|