22#include "basicelement.h"
23#include "tokenstyleelement.h"
25KFORMULA_NAMESPACE_BEGIN
28 m_mathSizeType ( NoSize ),
29 m_charStyle( anyChar ),
30 m_charFamily( anyFamily ),
31 m_mathColor(
"#000000" ),
32 m_mathBackground(
"#FFFFFF" ),
33 m_fontSizeType( NoSize ),
34 m_customMathVariant( false ),
35 m_customMathColor( false ),
36 m_customMathBackground( false ),
37 m_customFontWeight( false ),
38 m_customFontStyle( false ),
39 m_customFontFamily( false ),
40 m_customColor( false )
46 ContextStyle::IndexStyle istyle,
47 StyleAttributes& style )
49 setStyleSize( context, style );
51 setStyleColor( style );
52 setStyleBackground( style );
57void TokenStyleElement::draw( TQPainter& painter,
const LuPixelRect& r,
60 ContextStyle::IndexStyle istyle,
61 StyleAttributes& style,
62 const LuPixelPoint& parentOrigin )
64 setStyleSize( context, style );
66 setStyleColor( style );
67 setStyleBackground( style );
68 if ( style.background() != TQt::color0 ) {
69 painter.fillRect( context.layoutUnitToPixelX( parentOrigin.x() + getX() ),
70 context.layoutUnitToPixelY( parentOrigin.y() + getY() ),
71 context.layoutUnitToPixelX( getWidth() ),
72 context.layoutUnitToPixelY( getHeight() ),
75 inherited::draw( painter, r, context, tstyle, istyle, style, parentOrigin );
86 TQString variantStr = element.attribute(
"mathvariant" );
87 if ( !variantStr.isNull() ) {
88 if ( variantStr ==
"normal" ) {
89 setCharStyle( normalChar );
90 setCharFamily( normalFamily );
92 else if ( variantStr ==
"bold" ) {
93 setCharStyle( boldChar );
94 setCharFamily( normalFamily );
96 else if ( variantStr ==
"italic" ) {
97 setCharStyle( italicChar );
98 setCharFamily( normalFamily );
100 else if ( variantStr ==
"bold-italic" ) {
101 setCharStyle( boldItalicChar );
102 setCharFamily( normalFamily );
104 else if ( variantStr ==
"double-struck" ) {
105 setCharStyle( normalChar );
106 setCharFamily( doubleStruckFamily );
108 else if ( variantStr ==
"bold-fraktur" ) {
109 setCharStyle( boldChar );
110 setCharFamily( frakturFamily );
112 else if ( variantStr ==
"script" ) {
113 setCharStyle( normalChar );
114 setCharFamily( scriptFamily );
116 else if ( variantStr ==
"bold-script" ) {
117 setCharStyle( boldChar );
118 setCharFamily( scriptFamily );
120 else if ( variantStr ==
"fraktur" ) {
121 setCharStyle( boldChar );
122 setCharFamily( frakturFamily );
124 else if ( variantStr ==
"sans-serif" ) {
125 setCharStyle( normalChar );
126 setCharFamily( sansSerifFamily );
128 else if ( variantStr ==
"bold-sans-serif" ) {
129 setCharStyle( boldChar );
130 setCharFamily( sansSerifFamily );
132 else if ( variantStr ==
"sans-serif-italic" ) {
133 setCharStyle( italicChar );
134 setCharFamily( sansSerifFamily );
136 else if ( variantStr ==
"sans-serif-bold-italic" ) {
137 setCharStyle( boldItalicChar );
138 setCharFamily( sansSerifFamily );
140 else if ( variantStr ==
"monospace" ) {
141 setCharStyle( normalChar );
142 setCharFamily( monospaceFamily );
146 TQString sizeStr = element.attribute(
"mathsize" );
147 if ( !sizeStr.isNull() ) {
148 if ( sizeStr ==
"small" ) {
149 setRelativeSize( 0.8 );
151 else if ( sizeStr ==
"normal" ) {
152 setRelativeSize( 1.0 );
154 else if ( sizeStr ==
"big" ) {
155 setRelativeSize( 1.2 );
158 double s =
getSize( sizeStr, &m_mathSizeType );
159 switch ( m_mathSizeType ) {
161 setAbsoluteSize( s );
164 setRelativeSize( s );
175 TQString colorStr = element.attribute(
"mathcolor" );
176 if ( !colorStr.isNull() ) {
177 if ( colorStr[0] !=
'#' ) {
181 setMathColor( TQColor( colorStr ) );
184 TQString backgroundStr = element.attribute(
"mathbackground" );
185 if ( !backgroundStr.isNull() ) {
186 if ( backgroundStr[0] !=
'#' ) {
187 setMathBackground( TQColor(
getHtmlColor( backgroundStr ) ) );
190 setMathBackground( TQColor( backgroundStr ) );
196 sizeStr = element.attribute(
"fontsize" );
197 if ( ! sizeStr.isNull() ) {
198 if ( sizeStr ==
"small" ) {
199 setRelativeSize( 0.8,
true );
201 else if ( sizeStr ==
"normal" ) {
202 setRelativeSize( 1.0,
true );
204 else if ( sizeStr ==
"big" ) {
205 setRelativeSize( 1.2,
true );
208 double s =
getSize( sizeStr, &m_fontSizeType );
209 switch ( m_fontSizeType ) {
211 setAbsoluteSize( s,
true );
214 setRelativeSize( s,
true );
217 setPixelSize( s,
true );
225 TQString styleStr = element.attribute(
"fontstyle" );
226 if ( ! styleStr.isNull() ) {
227 if ( styleStr.lower() ==
"italic" ) {
228 setFontStyle(
true );
231 setFontStyle(
false );
235 TQString weightStr = element.attribute(
"fontweight" );
236 if ( ! weightStr.isNull() ) {
237 if ( weightStr.lower() ==
"bold" ) {
238 setFontWeight(
true );
241 setFontWeight(
false );
245 TQString familyStr = element.attribute(
"fontfamily" );
246 if ( ! familyStr.isNull() ) {
247 setFontFamily( familyStr );
250 colorStr = element.attribute(
"color" );
251 if ( ! colorStr.isNull() ) {
252 if ( colorStr[0] !=
'#' ) {
256 setColor( TQColor( colorStr ) );
263void TokenStyleElement::writeMathMLAttributes( TQDomElement& element )
const
266 if ( customMathVariant() ) {
267 if ( charFamily() == normalFamily ) {
268 if ( charStyle() == normalChar ) {
269 element.setAttribute(
"mathvariant",
"normal" );
271 else if ( charStyle() == boldChar ) {
272 element.setAttribute(
"mathvariant",
"bold" );
274 else if ( charStyle() == italicChar ) {
275 element.setAttribute(
"mathvariant",
"italic" );
277 else if ( charStyle() == boldItalicChar ) {
278 element.setAttribute(
"mathvariant",
"bold-italic" );
281 kdWarning( DEBUGID ) <<
"Mathvariant: unknown style for normal family\n";
284 else if ( charFamily() == doubleStruckFamily ) {
285 if ( charStyle() == normalChar ) {
286 element.setAttribute(
"mathvariant",
"double-struck" );
289 kdWarning( DEBUGID ) <<
"Mathvariant: unknown style for double-struck family\n";
292 else if ( charFamily() == frakturFamily ) {
293 if ( charStyle() == normalChar ) {
294 element.setAttribute(
"mathvariant",
"fraktur" );
296 else if ( charStyle() == boldChar ) {
297 element.setAttribute(
"mathvariant",
"bold-fraktur" );
300 kdWarning( DEBUGID ) <<
"Mathvariant: unknown style for fraktur family\n";
303 else if ( charFamily() == scriptFamily ) {
304 if ( charStyle() == normalChar ) {
305 element.setAttribute(
"mathvariant",
"script" );
307 else if ( charStyle() == boldChar ) {
308 element.setAttribute(
"mathvariant",
"bold-script" );
311 kdWarning( DEBUGID ) <<
"Mathvariant: unknown style for script family\n";
314 else if ( charFamily() == sansSerifFamily ) {
315 if ( charStyle() == normalChar ) {
316 element.setAttribute(
"mathvariant",
"sans-serif" );
318 else if ( charStyle() == boldChar ) {
319 element.setAttribute(
"mathvariant",
"bold-sans-serif" );
321 else if ( charStyle() == italicChar ) {
322 element.setAttribute(
"mathvariant",
"sans-serif-italic" );
324 else if ( charStyle() == boldItalicChar ) {
325 element.setAttribute(
"mathvariant",
"sans-serif-bold-italic" );
328 kdWarning( DEBUGID ) <<
"Mathvariant: unknown style for sans serif family\n";
331 else if ( charFamily() == monospaceFamily ) {
332 if ( charStyle() == normalChar ) {
333 element.setAttribute(
"mathvariant",
"monospace" );
336 kdWarning( DEBUGID ) <<
"Mathvariant: unknown style for monospace family\n";
340 kdWarning( DEBUGID ) <<
"Mathvariant: unknown family\n";
345 switch ( m_mathSizeType ) {
347 element.setAttribute(
"mathsize", TQString(
"%1pt" ).arg( m_mathSize ) );
350 element.setAttribute(
"mathsize", TQString(
"%1%" ).arg( m_mathSize * 100.0 ) );
353 element.setAttribute(
"mathsize", TQString(
"%1px" ).arg( m_mathSize ) );
360 if ( customMathColor() ) {
361 element.setAttribute(
"mathcolor", mathColor().
name() );
365 if ( customMathBackground() ) {
366 element.setAttribute(
"mathbackground", mathBackground().
name() );
371 switch ( m_fontSizeType ) {
373 element.setAttribute(
"fontsize", TQString(
"%1pt" ).arg( m_fontSize ) );
376 element.setAttribute(
"fontsize", TQString(
"%1%" ).arg( m_fontSize * 100.0 ) );
379 element.setAttribute(
"fontsize", TQString(
"%3px" ).arg( m_fontSize ) );
386 if ( customFontWeight() ) {
387 element.setAttribute(
"fontweight", fontWeight() ?
"bold" :
"normal" );
391 if ( customFontStyle() ) {
392 element.setAttribute(
"fontstyle", fontStyle() ?
"italic" :
"normal" );
396 if ( customFontFamily() ) {
397 element.setAttribute(
"fontfamily", fontFamily() );
401 if ( customColor() ) {
402 element.setAttribute(
"color", color().
name() );
406void TokenStyleElement::setAbsoluteSize(
double s,
bool fontsize )
408 kdDebug( DEBUGID) <<
"Setting absolute size: " << s << endl;
410 m_fontSizeType = AbsoluteSize;
414 m_mathSizeType = AbsoluteSize;
419void TokenStyleElement::setRelativeSize(
double f,
bool fontsize )
421 kdDebug( DEBUGID) <<
"Setting relative size: " << f << endl;
423 m_fontSizeType = RelativeSize;
427 m_mathSizeType = RelativeSize;
432void TokenStyleElement::setPixelSize(
double px,
bool fontsize )
434 kdDebug( DEBUGID) <<
"Setting pixel size: " << px << endl;
436 m_fontSizeType = PixelSize;
440 m_mathSizeType = PixelSize;
445void TokenStyleElement::setStyleSize(
const ContextStyle& context, StyleAttributes& style )
447 style.setSizeFactor( sizeFactor( context, style.sizeFactor() ) );
452 if ( customMathVariant() || style.customMathVariant() ) {
453 style.setCustomMathVariant (
true );
454 style.setCustomFontWeight(
false );
455 style.setCustomFontStyle(
false );
456 style.setCustomFont(
false );
457 if ( customMathVariant() ) {
458 style.setCharFamily ( charFamily() );
459 style.setCharStyle( charStyle() );
462 style.setCharFamily( style.charFamily() );
463 style.setCharStyle( style.charStyle() );
467 style.setCustomMathVariant(
false );
468 if ( customFontFamily() ) {
469 style.setCustomFont(
true );
470 style.setFont( TQFont(fontFamily()) );
473 bool fontweight =
false;
474 if ( customFontWeight() || style.customFontWeight() ) {
475 style.setCustomFontWeight(
true );
476 if ( customFontWeight() ) {
477 fontweight = fontWeight();
480 fontweight = style.fontWeight();
482 style.setFontWeight( fontweight );
485 style.setCustomFontWeight(
false );
488 bool fontstyle =
false;
489 if ( style.customFontStyle() ) {
490 style.setCustomFontStyle(
true );
491 fontstyle = style.fontStyle();
492 style.setFontStyle( fontstyle );
495 style.setCustomFontStyle(
false );
497 if ( customFontStyle() ) {
498 fontstyle = fontStyle();
501 if ( fontweight && fontstyle ) {
502 style.setCharStyle( boldItalicChar );
504 else if ( fontweight && ! fontstyle ) {
505 style.setCharStyle( boldChar );
507 else if ( ! fontweight && fontstyle ) {
508 style.setCharStyle( italicChar );
511 style.setCharStyle( normalChar );
516void TokenStyleElement::setStyleColor( StyleAttributes& style )
518 if ( customMathColor() ) {
519 style.setColor( mathColor() );
521 else if ( customColor() ) {
522 style.setColor( color() );
525 style.setColor( style.color() );
529void TokenStyleElement::setStyleBackground( StyleAttributes& style )
531 if ( customMathBackground() ) {
532 style.setBackground( mathBackground() );
535 style.setBackground( style.background() );
539void TokenStyleElement::resetStyle( StyleAttributes& style )
542 style.resetCharStyle();
543 style.resetCharFamily();
545 style.resetBackground();
546 style.resetFontFamily();
547 style.resetFontWeight();
548 style.resetFontStyle();
551double TokenStyleElement::sizeFactor(
const ContextStyle& context,
double factor )
553 double basesize = context.layoutUnitPtToPt( context.getBaseSize() );
554 switch ( m_mathSizeType ) {
556 return m_mathSize / basesize;
561 return context.pixelYToPt( m_mathSize ) / basesize;
563 switch ( m_fontSizeType ) {
565 return m_fontSize / basesize;
569 return context.pixelYToPt( m_fontSize ) / basesize;
576 kdWarning( DEBUGID ) << k_funcinfo <<
" Unknown SizeType\n";
585 TQString colorname = colorStr.lower();
587 if ( colorname ==
"black" )
589 if ( colorname ==
"silver" )
591 if ( colorname ==
"gray" )
593 if ( colorname ==
"white" )
595 if ( colorname ==
"maroon" )
597 if ( colorname ==
"red" )
599 if ( colorname ==
"purple" )
601 if ( colorname ==
"fuchsia" )
603 if ( colorname ==
"green" )
605 if ( colorname ==
"lime" )
607 if ( colorname ==
"olive" )
609 if ( colorname ==
"yellow" )
611 if ( colorname ==
"navy" )
613 if ( colorname ==
"blue")
615 if ( colorname ==
"teal" )
617 if ( colorname ==
"aqua" )
619 kdWarning( DEBUGID ) <<
"Invalid HTML color: " << colorname << endl;
624KFORMULA_NAMESPACE_END
Basis of every formula element.
virtual bool readAttributesFromMathMLDom(const TQDomElement &element)
Reads our attributes from the MathML element.
double getSize(const TQString &str, SizeType *st)
Utility function that sets the size type and returns the size value from a MathML attribute string wi...
Contains all the style information for the formela.
TextStyle
Textstyles like in TeX.
The element that contains a number of children.
virtual void calcSizes(const ContextStyle &cstyle, ContextStyle::TextStyle tstyle, ContextStyle::IndexStyle istyle, StyleAttributes &style)
Calculates our width and height and our children's parentPosition.
TQString getHtmlColor(const TQString &colorStr)
Return RGB string from HTML Colors.
virtual void calcSizes(const ContextStyle &context, ContextStyle::TextStyle tstyle, ContextStyle::IndexStyle istyle, StyleAttributes &style)
Calculates our width and height and our children's parentPosition.
virtual bool readAttributesFromMathMLDom(const TQDomElement &element)
Reads our attributes from the MathML element.
virtual void setStyleVariant(StyleAttributes &style)
Set the mathvariant related info in style stacks, including info for deprecated attributes.
const char * name(StdAction id)