| 1 | /* |
| 2 | * Copyright (C) 2016 Igalia S.L. 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 | #include "RenderMathMLPadded.h" |
| 28 | |
| 29 | #if ENABLE(MATHML) |
| 30 | |
| 31 | #include <cmath> |
| 32 | #include <wtf/IsoMallocInlines.h> |
| 33 | |
| 34 | namespace WebCore { |
| 35 | |
| 36 | WTF_MAKE_ISO_ALLOCATED_IMPL(RenderMathMLPadded); |
| 37 | |
| 38 | RenderMathMLPadded::RenderMathMLPadded(MathMLPaddedElement& element, RenderStyle&& style) |
| 39 | : RenderMathMLRow(element, WTFMove(style)) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | LayoutUnit RenderMathMLPadded::voffset() const |
| 44 | { |
| 45 | return toUserUnits(element().voffset(), style(), 0); |
| 46 | } |
| 47 | |
| 48 | LayoutUnit RenderMathMLPadded::lspace() const |
| 49 | { |
| 50 | LayoutUnit lspace = toUserUnits(element().lspace(), style(), 0); |
| 51 | // FIXME: Negative lspace values are not supported yet (https://bugs.webkit.org/show_bug.cgi?id=85730). |
| 52 | return std::max<LayoutUnit>(0, lspace); |
| 53 | } |
| 54 | |
| 55 | LayoutUnit RenderMathMLPadded::mpaddedWidth(LayoutUnit contentWidth) const |
| 56 | { |
| 57 | return std::max<LayoutUnit>(0, toUserUnits(element().width(), style(), contentWidth)); |
| 58 | } |
| 59 | |
| 60 | LayoutUnit RenderMathMLPadded::mpaddedHeight(LayoutUnit contentHeight) const |
| 61 | { |
| 62 | return std::max<LayoutUnit>(0, toUserUnits(element().height(), style(), contentHeight)); |
| 63 | } |
| 64 | |
| 65 | LayoutUnit RenderMathMLPadded::mpaddedDepth(LayoutUnit contentDepth) const |
| 66 | { |
| 67 | return std::max<LayoutUnit>(0, toUserUnits(element().depth(), style(), contentDepth)); |
| 68 | } |
| 69 | |
| 70 | void RenderMathMLPadded::computePreferredLogicalWidths() |
| 71 | { |
| 72 | ASSERT(preferredLogicalWidthsDirty()); |
| 73 | |
| 74 | // Determine the intrinsic width of the content. |
| 75 | RenderMathMLRow::computePreferredLogicalWidths(); |
| 76 | |
| 77 | // Only the width attribute should modify the width. |
| 78 | // We parse it using the preferred width of the content as its default value. |
| 79 | m_maxPreferredLogicalWidth = mpaddedWidth(m_maxPreferredLogicalWidth); |
| 80 | m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth; |
| 81 | |
| 82 | setPreferredLogicalWidthsDirty(false); |
| 83 | } |
| 84 | |
| 85 | void RenderMathMLPadded::layoutBlock(bool relayoutChildren, LayoutUnit) |
| 86 | { |
| 87 | ASSERT(needsLayout()); |
| 88 | |
| 89 | if (!relayoutChildren && simplifiedLayout()) |
| 90 | return; |
| 91 | |
| 92 | // We first layout our children as a normal <mrow> element. |
| 93 | LayoutUnit contentWidth, contentAscent, contentDescent; |
| 94 | stretchVerticalOperatorsAndLayoutChildren(); |
| 95 | getContentBoundingBox(contentWidth, contentAscent, contentDescent); |
| 96 | layoutRowItems(contentWidth, contentAscent); |
| 97 | |
| 98 | // We parse the mpadded attributes using the content metrics as the default value. |
| 99 | LayoutUnit width = mpaddedWidth(contentWidth); |
| 100 | LayoutUnit ascent = mpaddedHeight(contentAscent); |
| 101 | LayoutUnit descent = mpaddedDepth(contentDescent); |
| 102 | |
| 103 | // Align children on the new baseline and shift them by (lspace, -voffset) |
| 104 | LayoutPoint contentLocation(lspace(), ascent - contentAscent - voffset()); |
| 105 | for (auto* child = firstChildBox(); child; child = child->nextSiblingBox()) |
| 106 | child->setLocation(child->location() + contentLocation); |
| 107 | |
| 108 | // Set the final metrics. |
| 109 | setLogicalWidth(width); |
| 110 | setLogicalHeight(ascent + descent); |
| 111 | |
| 112 | layoutPositionedObjects(relayoutChildren); |
| 113 | |
| 114 | updateScrollInfoAfterLayout(); |
| 115 | |
| 116 | clearNeedsLayout(); |
| 117 | } |
| 118 | |
| 119 | Optional<int> RenderMathMLPadded::firstLineBaseline() const |
| 120 | { |
| 121 | // We try and calculate the baseline from the position of the first child. |
| 122 | LayoutUnit ascent; |
| 123 | if (auto* baselineChild = firstChildBox()) |
| 124 | ascent = ascentForChild(*baselineChild) + baselineChild->logicalTop() + voffset(); |
| 125 | else |
| 126 | ascent = mpaddedHeight(0); |
| 127 | return Optional<int>(std::lround(static_cast<float>(ascent))); |
| 128 | } |
| 129 | |
| 130 | } |
| 131 | |
| 132 | #endif |
| 133 | |