initial import
[vuplus_webkit] / Source / WebCore / rendering / mathml / RenderMathMLFenced.cpp
1 /*
2  * Copyright (C) 2009 Alex Milowski (alex@milowski.com). All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
16  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
17  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27
28 #if ENABLE(MATHML)
29
30 #include "RenderMathMLFenced.h"
31
32 #include "FontSelector.h"
33 #include "MathMLNames.h"
34 #include "RenderInline.h"
35 #include "RenderMathMLOperator.h"
36 #include "RenderText.h"
37 #include <wtf/text/StringBuilder.h>
38
39 namespace WebCore {
40     
41 using namespace MathMLNames;
42     
43 enum Braces { OpeningBraceChar = 0x28, ClosingBraceChar = 0x29 };
44     
45 static const float gOperatorPadding = 0.1f;
46
47 RenderMathMLFenced::RenderMathMLFenced(Node* fenced) 
48     : RenderMathMLRow(fenced)
49     , m_open(OpeningBraceChar)
50     , m_close(ClosingBraceChar)
51 {
52 }
53
54 void RenderMathMLFenced::updateFromElement()
55 {
56     Element* fenced = static_cast<Element*>(node());
57  
58     // FIXME: Handle open/close values with more than one character (they should be treated like text).
59     AtomicString openValue = fenced->getAttribute(MathMLNames::openAttr);
60     if (openValue.length() > 0)
61         m_open = openValue[0];
62     AtomicString closeValue = fenced->getAttribute(MathMLNames::closeAttr);
63     if (closeValue.length() > 0)
64         m_close = closeValue[0];
65     
66     AtomicString separators = static_cast<Element*>(fenced)->getAttribute(MathMLNames::separatorsAttr);
67     if (!separators.isNull()) {
68         StringBuilder characters;
69         for (unsigned int i = 0; i < separators.length(); i++) {
70             if (!isSpaceOrNewline(separators[i]))
71                 characters.append(separators[i]);
72         }
73         m_separators = !characters.length() ? 0 : characters.toString().impl();
74     } else {
75         // The separator defaults to a single comma.
76         m_separators = StringImpl::create(",");
77     }
78     
79     if (isEmpty())
80         makeFences();
81 }
82
83 RefPtr<RenderStyle> RenderMathMLFenced::makeOperatorStyle() 
84 {
85     RefPtr<RenderStyle> newStyle = RenderStyle::create();
86     newStyle->inheritFrom(style());
87     newStyle->setDisplay(INLINE_BLOCK);
88     newStyle->setPaddingRight(Length(static_cast<int>(gOperatorPadding * style()->fontSize()), Fixed));
89     return newStyle;
90 }
91
92 void RenderMathMLFenced::makeFences()
93 {
94     RenderObject* openFence = new (renderArena()) RenderMathMLOperator(node(), m_open);
95     openFence->setStyle(makeOperatorStyle().release());
96     RenderBlock::addChild(openFence, firstChild());
97     RenderObject* closeFence = new (renderArena()) RenderMathMLOperator(node(), m_close);
98     closeFence->setStyle(makeOperatorStyle().release());
99     RenderBlock::addChild(closeFence);
100 }
101
102 void RenderMathMLFenced::addChild(RenderObject* child, RenderObject*)
103 {
104     // make the fences if the render object is empty
105     if (isEmpty())
106         updateFromElement();
107     
108     if (m_separators.get()) {
109         unsigned int count = 0;
110         for (Node* position = child->node(); position; position = position->previousSibling()) {
111             if (position->nodeType() == Node::ELEMENT_NODE)
112                 count++;
113         }
114                 
115         if (count > 1) {
116             UChar separator;
117             
118             // Use the last separator if we've run out of specified separators.
119             if ((count - 1) >= m_separators.get()->length())
120                 separator = (*m_separators.get())[m_separators.get()->length() - 1];
121             else
122                 separator = (*m_separators.get())[count - 1];
123                 
124             RenderObject* separatorObj = new (renderArena()) RenderMathMLOperator(node(), separator);
125             separatorObj->setStyle(makeOperatorStyle().release());
126             RenderBlock::addChild(separatorObj, lastChild());
127         }
128     }
129     
130     // If we have a block, we'll wrap it in an inline-block.
131     if (child->isBlockFlow() && child->style()->display() != INLINE_BLOCK) {
132         // Block objects wrapper.
133
134         RenderBlock* block = new (renderArena()) RenderBlock(node());
135         RefPtr<RenderStyle> newStyle = RenderStyle::create();
136         newStyle->inheritFrom(style());
137         newStyle->setDisplay(INLINE_BLOCK);
138         block->setStyle(newStyle.release());
139         
140         RenderBlock::addChild(block, lastChild());
141         block->addChild(child);    
142     } else
143         RenderBlock::addChild(child, lastChild());
144 }
145
146 }    
147
148 #endif