RenderMathMLMenclose.cpp [plain text]
#include "config.h"
#include "RenderMathMLMenclose.h"
#if ENABLE(MATHML)
#include "GraphicsContext.h"
#include "MathMLNames.h"
#include "PaintInfo.h"
#include <wtf/IsoMallocInlines.h>
#include <wtf/MathExtras.h>
namespace WebCore {
using namespace MathMLNames;
WTF_MAKE_ISO_ALLOCATED_IMPL(RenderMathMLMenclose);
const unsigned short longDivLeftSpace = 10;
RenderMathMLMenclose::RenderMathMLMenclose(MathMLMencloseElement& element, RenderStyle&& style)
: RenderMathMLRow(element, WTFMove(style))
{
}
LayoutUnit RenderMathMLMenclose::ruleThickness() const
{
return 0.05f * style().fontCascade().size();
}
RenderMathMLMenclose::SpaceAroundContent RenderMathMLMenclose::spaceAroundContent(LayoutUnit contentWidth, LayoutUnit contentHeight) const
{
SpaceAroundContent space;
space.right = 0;
space.top = 0;
space.bottom = 0;
space.left = 0;
LayoutUnit thickness = ruleThickness();
if (hasNotation(MathMLMencloseElement::Left))
space.left = std::max(space.left, 5 * thickness);
if (hasNotation(MathMLMencloseElement::Right))
space.right = std::max(space.right, 5 * thickness);
if (hasNotation(MathMLMencloseElement::Left) || hasNotation(MathMLMencloseElement::Right)) {
LayoutUnit extraSpace = 4 * thickness;
space.top = std::max(space.top, extraSpace);
space.bottom = std::max(space.bottom, extraSpace);
}
if (hasNotation(MathMLMencloseElement::Top))
space.top = std::max(space.top, 5 * thickness);
if (hasNotation(MathMLMencloseElement::Bottom))
space.bottom = std::max(space.bottom, 5 * thickness);
if (hasNotation(MathMLMencloseElement::Top) || hasNotation(MathMLMencloseElement::Bottom)) {
LayoutUnit extraSpace = 4 * thickness;
space.left = std::max(space.left, extraSpace);
space.right = std::max(space.right, extraSpace);
}
if (hasNotation(MathMLMencloseElement::LongDiv)) {
space.top = std::max(space.top, 5 * thickness);
space.bottom = std::max(space.bottom, 5 * thickness);
space.left = std::max(space.left, longDivLeftSpace * thickness);
space.right = std::max(space.right, 4 * thickness);
}
if (hasNotation(MathMLMencloseElement::RoundedBox)) {
LayoutUnit extraSpace = 5 * thickness;
space.left = std::max(space.left, extraSpace);
space.right = std::max(space.right, extraSpace);
space.top = std::max(space.top, extraSpace);
space.bottom = std::max(space.bottom, extraSpace);
}
if (hasNotation(MathMLMencloseElement::UpDiagonalStrike) || hasNotation(MathMLMencloseElement::DownDiagonalStrike)) {
LayoutUnit extraSpace = thickness / 2;
space.left = std::max(space.left, extraSpace);
space.right = std::max(space.right, extraSpace);
space.top = std::max(space.top, extraSpace);
space.bottom = std::max(space.bottom, extraSpace);
}
if (hasNotation(MathMLMencloseElement::Circle)) {
LayoutUnit extraSpace = (contentWidth * (sqrtOfTwoFloat - 1) + 3 * thickness) / 2;
space.left = std::max(space.left, extraSpace);
space.right = std::max(space.right, extraSpace);
extraSpace = (contentHeight * (sqrtOfTwoFloat - 1) + 3 * thickness) / 2;
space.top = std::max(space.top, extraSpace);
space.bottom = std::max(space.bottom, extraSpace);
}
return space;
}
void RenderMathMLMenclose::computePreferredLogicalWidths()
{
ASSERT(preferredLogicalWidthsDirty());
RenderMathMLRow::computePreferredLogicalWidths();
LayoutUnit preferredWidth = m_maxPreferredLogicalWidth;
SpaceAroundContent space = spaceAroundContent(preferredWidth, 0);
m_maxPreferredLogicalWidth = space.left + preferredWidth + space.right;
m_maxPreferredLogicalWidth = m_minPreferredLogicalWidth;
setPreferredLogicalWidthsDirty(false);
}
void RenderMathMLMenclose::layoutBlock(bool relayoutChildren, LayoutUnit)
{
ASSERT(needsLayout());
if (!relayoutChildren && simplifiedLayout())
return;
LayoutUnit contentWidth, contentAscent, contentDescent;
stretchVerticalOperatorsAndLayoutChildren();
getContentBoundingBox(contentWidth, contentAscent, contentDescent);
layoutRowItems(contentWidth, contentAscent);
SpaceAroundContent space = spaceAroundContent(contentWidth, contentAscent + contentDescent);
setLogicalWidth(space.left + contentWidth + space.right);
setLogicalHeight(space.top + contentAscent + contentDescent + space.bottom);
LayoutPoint contentLocation(space.left, space.top);
for (auto* child = firstChildBox(); child; child = child->nextSiblingBox())
child->setLocation(child->location() + contentLocation);
m_contentRect = LayoutRect(space.left, space.top, contentWidth, contentAscent + contentDescent);
layoutPositionedObjects(relayoutChildren);
clearNeedsLayout();
}
static void drawLine(PaintInfo& info, const LayoutUnit& xStart, const LayoutUnit& yStart, const LayoutUnit& xEnd, const LayoutUnit& yEnd)
{
Path line;
line.moveTo(LayoutPoint(xStart, yStart));
line.addLineTo(LayoutPoint(xEnd, yEnd));
info.context().strokePath(line);
}
void RenderMathMLMenclose::paint(PaintInfo& info, const LayoutPoint& paintOffset)
{
RenderMathMLRow::paint(info, paintOffset);
if (info.context().paintingDisabled() || info.phase != PaintPhaseForeground || style().visibility() != VISIBLE)
return;
LayoutUnit thickness = ruleThickness();
PaintInfo paintInfo(info);
GraphicsContextStateSaver stateSaver(paintInfo.context());
paintInfo.context().setStrokeThickness(thickness);
paintInfo.context().setStrokeStyle(SolidStroke);
paintInfo.context().setStrokeColor(style().visitedDependentColor(CSSPropertyColor));
paintInfo.context().setFillColor(Color::transparent);
paintInfo.applyTransform(AffineTransform().translate(paintOffset + location()));
if (hasNotation(MathMLMencloseElement::Left)) {
LayoutUnit x = m_contentRect.x() - 7 * thickness / 2;
LayoutUnit yStart = m_contentRect.y() - 4 * thickness;
LayoutUnit yEnd = m_contentRect.maxY() + 4 * thickness;
drawLine(info, x, yStart, x, yEnd);
}
if (hasNotation(MathMLMencloseElement::Right)) {
LayoutUnit x = m_contentRect.maxX() + 7 * thickness / 2;
LayoutUnit yStart = m_contentRect.y() - 4 * thickness;
LayoutUnit yEnd = m_contentRect.maxY() + 4 * thickness;
drawLine(info, x, yStart, x, yEnd);
}
if (hasNotation(MathMLMencloseElement::VerticalStrike)) {
LayoutUnit x = m_contentRect.x() + (m_contentRect.width() - thickness) / 2;
LayoutUnit yStart = m_contentRect.y();
LayoutUnit yEnd = m_contentRect.maxY();
drawLine(info, x, yStart, x, yEnd);
}
if (hasNotation(MathMLMencloseElement::Top)) {
LayoutUnit y = m_contentRect.y() - 7 * thickness / 2;
LayoutUnit xStart = m_contentRect.x() - 4 * thickness;
LayoutUnit xEnd = m_contentRect.maxX() + 4 * thickness;
drawLine(info, xStart, y, xEnd, y);
}
if (hasNotation(MathMLMencloseElement::Bottom)) {
LayoutUnit y = m_contentRect.maxY() + 7 * thickness / 2;
LayoutUnit xStart = m_contentRect.x() - 4 * thickness;
LayoutUnit xEnd = m_contentRect.maxX() + 4 * thickness;
drawLine(info, xStart, y, xEnd, y);
}
if (hasNotation(MathMLMencloseElement::HorizontalStrike)) {
LayoutUnit y = m_contentRect.y() + (m_contentRect.height() - thickness) / 2;
LayoutUnit xStart = m_contentRect.x();
LayoutUnit xEnd = m_contentRect.maxX();
drawLine(info, xStart, y, xEnd, y);
}
if (hasNotation(MathMLMencloseElement::UpDiagonalStrike))
drawLine(info, m_contentRect.x(), m_contentRect.maxY(), m_contentRect.maxX(), m_contentRect.y());
if (hasNotation(MathMLMencloseElement::DownDiagonalStrike))
drawLine(info, m_contentRect.x(), m_contentRect.y(), m_contentRect.maxX(), m_contentRect.maxY());
if (hasNotation(MathMLMencloseElement::RoundedBox)) {
LayoutSize radiiSize(3 * thickness, 3 * thickness);
RoundedRect::Radii radii(radiiSize, radiiSize, radiiSize, radiiSize);
RoundedRect roundedRect(m_contentRect, radii);
roundedRect.inflate(7 * thickness / 2);
Path path;
path.addRoundedRect(roundedRect);
paintInfo.context().strokePath(path);
}
if (hasNotation(MathMLMencloseElement::LongDiv)) {
LayoutUnit top = m_contentRect.y() - 7 * thickness / 2;
LayoutUnit bottom = m_contentRect.maxY() + 7 * thickness / 2;
LayoutUnit left = m_contentRect.x() - longDivLeftSpace * thickness + thickness / 2;
LayoutUnit right = m_contentRect.maxX() + 4 * thickness;
LayoutUnit midX = left + longDivLeftSpace * thickness;
LayoutUnit midY = (top + bottom) / 2;
Path path;
path.moveTo(LayoutPoint(right, top));
path.addLineTo(LayoutPoint(left, top));
path.addQuadCurveTo(LayoutPoint(midX, midY), FloatPoint(left, bottom));
paintInfo.context().strokePath(path);
}
if (hasNotation(MathMLMencloseElement::Circle)) {
LayoutRect ellipseRect;
ellipseRect.setWidth(m_contentRect.width() * sqrtOfTwoFloat + thickness);
ellipseRect.setHeight(m_contentRect.height() * sqrtOfTwoFloat + thickness);
ellipseRect.setX(m_contentRect.x() - (ellipseRect.width() - m_contentRect.width()) / 2);
ellipseRect.setY(m_contentRect.y() - (ellipseRect.height() - m_contentRect.height()) / 2);
Path path;
path.addEllipse(ellipseRect);
paintInfo.context().strokePath(path);
}
}
}
#endif // ENABLE(MATHML)