WebCore/mathml/RenderMathMLSquareRoot.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2009 Alex Milowski (alex@milowski.com). All rights reserved.
       
     3  * Copyright (C) 2010 François Sausset (sausset@gmail.com). All rights reserved.
       
     4  *
       
     5  * Redistribution and use in source and binary forms, with or without
       
     6  * modification, are permitted provided that the following conditions
       
     7  * are met:
       
     8  * 1. Redistributions of source code must retain the above copyright
       
     9  *    notice, this list of conditions and the following disclaimer.
       
    10  * 2. Redistributions in binary form must reproduce the above copyright
       
    11  *    notice, this list of conditions and the following disclaimer in the
       
    12  *    documentation and/or other materials provided with the distribution.
       
    13  *
       
    14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    15  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    16  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    17  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    18  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    19  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    20  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    25  */
       
    26 
       
    27 #include "config.h"
       
    28 
       
    29 #if ENABLE(MATHML)
       
    30 
       
    31 #include "RenderMathMLSquareRoot.h"
       
    32 
       
    33 #include "GraphicsContext.h"
       
    34 #include "MathMLNames.h"
       
    35 #include "Path.h"
       
    36 
       
    37 namespace WebCore {
       
    38     
       
    39 using namespace MathMLNames;
       
    40 
       
    41 // Bottom padding of the radical (px)
       
    42 const int gRadicalBasePad = 3;
       
    43 // Threshold above which the radical shape is modified to look nice with big bases (%)
       
    44 const float gThresholdBaseHeight = 1.5;
       
    45 // Radical width (%)
       
    46 const float gRadicalWidth = 0.75;
       
    47 // Horizontal position of the bottom point of the radical (%)
       
    48 const float gRadicalBottomPointXPos= 0.5;
       
    49 // Horizontal position of the top left point of the radical (%)
       
    50 const float gRadicalTopLeftPointXPos = 0.2;
       
    51 // Vertical position of the top left point of the radical (%)
       
    52 const float gRadicalTopLeftPointYPos = 0.5; 
       
    53 // Vertical shift of the left end point of the radical (%)
       
    54 const float gRadicalLeftEndYShift = 0.05;
       
    55 // Additional bottom root padding (%)
       
    56 const float gRootBottomPadding = 0.2;
       
    57 
       
    58 // Radical line thickness (%)
       
    59 const float gRadicalLineThickness = 0.02;
       
    60 // Radical thick line thickness (%)
       
    61 const float gRadicalThickLineThickness = 0.1;
       
    62     
       
    63 RenderMathMLSquareRoot::RenderMathMLSquareRoot(Node *expression) 
       
    64     : RenderMathMLBlock(expression) 
       
    65 {
       
    66 }
       
    67 
       
    68 void RenderMathMLSquareRoot::paint(PaintInfo& info, int tx, int ty)
       
    69 {
       
    70     RenderMathMLBlock::paint(info, tx, ty);
       
    71    
       
    72     if (info.context->paintingDisabled())
       
    73         return;
       
    74     
       
    75     tx += x();
       
    76     ty += y();
       
    77 
       
    78     int maxHeight = 0;
       
    79     int width = 0;
       
    80     RenderObject* current = firstChild();
       
    81     while (current) {
       
    82         if (current->isBoxModelObject()) {
       
    83             
       
    84             RenderBoxModelObject* box = toRenderBoxModelObject(current);
       
    85             
       
    86             // Check to see if this box has a larger height
       
    87             if (box->offsetHeight() > maxHeight)
       
    88                 maxHeight = box->offsetHeight();
       
    89             width += box->offsetWidth();
       
    90         }
       
    91         current = current->nextSibling();
       
    92     }
       
    93     // default to the font size in pixels if we're empty
       
    94     if (!maxHeight)
       
    95         maxHeight = style()->fontSize();
       
    96     
       
    97     int frontWidth = static_cast<int>(style()->fontSize() * gRadicalWidth);
       
    98     int topStartShift = 0;
       
    99     // Base height above which the shape of the root changes
       
   100     int thresholdHeight = static_cast<int>(gThresholdBaseHeight * style()->fontSize());
       
   101     
       
   102     if (maxHeight > thresholdHeight && thresholdHeight) {
       
   103         float shift = (maxHeight - thresholdHeight) / static_cast<float>(thresholdHeight);
       
   104         if (shift > 1.)
       
   105             shift = 1.;
       
   106         topStartShift = static_cast<int>(gRadicalBottomPointXPos * frontWidth * shift);
       
   107     }
       
   108     
       
   109     width += topStartShift;
       
   110     
       
   111     FloatPoint topStart(tx + frontWidth - topStartShift, ty);
       
   112     FloatPoint bottomLeft(tx + frontWidth * gRadicalBottomPointXPos , ty + maxHeight + gRadicalBasePad);
       
   113     FloatPoint topLeft(tx + frontWidth * gRadicalTopLeftPointXPos , ty + gRadicalTopLeftPointYPos * maxHeight);
       
   114     FloatPoint leftEnd(tx , topLeft.y() + gRadicalLeftEndYShift * style()->fontSize());
       
   115     
       
   116     info.context->save();
       
   117     
       
   118     info.context->setStrokeThickness(gRadicalLineThickness * style()->fontSize());
       
   119     info.context->setStrokeStyle(SolidStroke);
       
   120     info.context->setStrokeColor(style()->visitedDependentColor(CSSPropertyColor), DeviceColorSpace);
       
   121     info.context->setLineJoin(MiterJoin);
       
   122     info.context->setMiterLimit(style()->fontSize());
       
   123     
       
   124     Path root;
       
   125     
       
   126     root.moveTo(FloatPoint(topStart.x() + width , ty));
       
   127     // draw top
       
   128     root.addLineTo(topStart);
       
   129     // draw from top left corner to bottom point of radical
       
   130     root.addLineTo(bottomLeft);
       
   131     // draw from bottom point to top of left part of radical base "pocket"
       
   132     root.addLineTo(topLeft);
       
   133     // draw to end
       
   134     root.addLineTo(leftEnd);
       
   135     
       
   136     info.context->beginPath();
       
   137     info.context->addPath(root);
       
   138     info.context->strokePath();
       
   139     
       
   140     info.context->save();
       
   141     
       
   142     // Build a mask to draw the thick part of the root.
       
   143     Path mask;
       
   144     
       
   145     mask.moveTo(topStart);
       
   146     mask.addLineTo(bottomLeft);
       
   147     mask.addLineTo(topLeft);
       
   148     mask.addLineTo(FloatPoint(2 * topLeft.x() - leftEnd.x(), 2 * topLeft.y() - leftEnd.y()));
       
   149     
       
   150     info.context->beginPath();
       
   151     info.context->addPath(mask);
       
   152     info.context->clip(mask);
       
   153     
       
   154     // Draw the thick part of the root.
       
   155     info.context->setStrokeThickness(gRadicalThickLineThickness * style()->fontSize());
       
   156     info.context->setLineCap(SquareCap);
       
   157     
       
   158     Path line;
       
   159     
       
   160     line = line.createLine(bottomLeft, topLeft);
       
   161     
       
   162     info.context->beginPath();
       
   163     info.context->addPath(line);
       
   164     info.context->strokePath();
       
   165     
       
   166     info.context->restore();
       
   167     
       
   168     info.context->restore();
       
   169 }
       
   170 
       
   171 void RenderMathMLSquareRoot::layout()
       
   172 {
       
   173     int maxHeight = 0;
       
   174     
       
   175     RenderObject* current = firstChild();
       
   176     while (current) {
       
   177         if (current->isBoxModelObject()) {
       
   178             RenderBoxModelObject* box = toRenderBoxModelObject(current);
       
   179             
       
   180             if (box->offsetHeight() > maxHeight)
       
   181                 maxHeight = box->offsetHeight();
       
   182             
       
   183             box->style()->setVerticalAlign(BASELINE);
       
   184         }
       
   185         current = current->nextSibling();
       
   186     }
       
   187     
       
   188     if (!maxHeight)
       
   189         maxHeight = style()->fontSize();
       
   190 
       
   191     
       
   192     if (maxHeight > static_cast<int>(gThresholdBaseHeight * style()->fontSize()))
       
   193         style()->setPaddingBottom(Length(static_cast<int>(gRootBottomPadding * style()->fontSize()), Fixed));
       
   194 
       
   195     
       
   196     RenderBlock::layout();
       
   197 }
       
   198     
       
   199 }
       
   200 
       
   201 #endif // ENABLE(MATHML)
       
   202 
       
   203