• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KHTML

Path.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2003, 2006 Apple Computer, Inc.  All rights reserved.
00003  *                     2006 Rob Buis <buis@kde.org>
00004  * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
00016  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00018  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
00019  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00020  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00021  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00022  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00023  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00026  */
00027 
00028 
00029 #include "config.h"
00030 #include "wtf/Platform.h"
00031 #include "Path.h"
00032 
00033 #include "FloatPoint.h"
00034 #include "FloatRect.h"
00035 #include "PathTraversalState.h"
00036 #include <math.h>
00037 #include <wtf/MathExtras.h>
00038 
00039 const float QUARTER = 0.552f; // approximation of control point positions on a bezier
00040                               // to simulate a quarter of a circle.
00041 namespace WebCore {
00042 
00043 static void pathLengthApplierFunction(void* info, const PathElement* element)
00044 {
00045     PathTraversalState& traversalState = *static_cast<PathTraversalState*>(info);
00046     if (traversalState.m_success)
00047         return;
00048     traversalState.m_previous = traversalState.m_current;
00049     FloatPoint* points = element->points;
00050     float segmentLength = 0.0f;
00051     switch (element->type) {
00052         case PathElementMoveToPoint:
00053             segmentLength = traversalState.moveTo(points[0]);
00054             break;
00055         case PathElementAddLineToPoint:
00056             segmentLength = traversalState.lineTo(points[0]);
00057             break;
00058         case PathElementAddQuadCurveToPoint:
00059             segmentLength = traversalState.quadraticBezierTo(points[0], points[1]);
00060             break;
00061         case PathElementAddCurveToPoint:
00062             segmentLength = traversalState.cubicBezierTo(points[0], points[1], points[2]);
00063             break;
00064         case PathElementCloseSubpath:
00065             segmentLength = traversalState.closeSubpath();
00066             break;
00067     }
00068     traversalState.m_totalLength += segmentLength; 
00069     if ((traversalState.m_action == PathTraversalState::TraversalPointAtLength || 
00070          traversalState.m_action == PathTraversalState::TraversalNormalAngleAtLength) &&
00071         (traversalState.m_totalLength >= traversalState.m_desiredLength)) {
00072         FloatSize change = traversalState.m_current - traversalState.m_previous;
00073         float slope = atan2f(change.height(), change.width());
00074 
00075         if (traversalState.m_action == PathTraversalState::TraversalPointAtLength) {
00076             float offset = traversalState.m_desiredLength - traversalState.m_totalLength;
00077             traversalState.m_current.move(offset * cosf(slope), offset * sinf(slope));
00078         } else {
00079             static const float rad2deg = 180.0f / piFloat;
00080             traversalState.m_normalAngle = slope * rad2deg;
00081         }
00082 
00083         traversalState.m_success = true;
00084     }
00085 }
00086 
00087 float Path::length()
00088 {
00089     PathTraversalState traversalState(PathTraversalState::TraversalTotalLength);
00090     apply(&traversalState, pathLengthApplierFunction);
00091     return traversalState.m_totalLength;
00092 }
00093 
00094 FloatPoint Path::pointAtLength(float length, bool& ok)
00095 {
00096     PathTraversalState traversalState(PathTraversalState::TraversalPointAtLength);
00097     traversalState.m_desiredLength = length;
00098     apply(&traversalState, pathLengthApplierFunction);
00099     ok = traversalState.m_success;
00100     return traversalState.m_current;
00101 }
00102 
00103 float Path::normalAngleAtLength(float length, bool& ok)
00104 {
00105     PathTraversalState traversalState(PathTraversalState::TraversalNormalAngleAtLength);
00106     traversalState.m_desiredLength = length;
00107     apply(&traversalState, pathLengthApplierFunction);
00108     ok = traversalState.m_success;
00109     return traversalState.m_normalAngle;
00110 }
00111 
00112 Path Path::createRoundedRectangle(const FloatRect& rectangle, const FloatSize& roundingRadii)
00113 {
00114     Path path;
00115     float x = rectangle.x();
00116     float y = rectangle.y();
00117     float width = rectangle.width();
00118     float height = rectangle.height();
00119     float rx = roundingRadii.width();
00120     float ry = roundingRadii.height();
00121     if (width <= 0.0f || height <= 0.0f)
00122         return path;
00123 
00124     float dx = rx, dy = ry;
00125     // If rx is greater than half of the width of the rectangle
00126     // then set rx to half of the width (required in SVG spec)
00127     if (dx > width * 0.5f)
00128         dx = width * 0.5f;
00129 
00130     // If ry is greater than half of the height of the rectangle
00131     // then set ry to half of the height (required in SVG spec)
00132     if (dy > height * 0.5f)
00133         dy = height * 0.5f;
00134 
00135     path.moveTo(FloatPoint(x + dx, y));
00136 
00137     if (dx < width * 0.5f)
00138         path.addLineTo(FloatPoint(x + width - rx, y));
00139 
00140     path.addBezierCurveTo(FloatPoint(x + width - dx * (1 - QUARTER), y), FloatPoint(x + width, y + dy * (1 - QUARTER)), FloatPoint(x + width, y + dy));
00141 
00142     if (dy < height * 0.5)
00143         path.addLineTo(FloatPoint(x + width, y + height - dy));
00144 
00145     path.addBezierCurveTo(FloatPoint(x + width, y + height - dy * (1 - QUARTER)), FloatPoint(x + width - dx * (1 - QUARTER), y + height), FloatPoint(x + width - dx, y + height));
00146 
00147     if (dx < width * 0.5)
00148         path.addLineTo(FloatPoint(x + dx, y + height));
00149 
00150     path.addBezierCurveTo(FloatPoint(x + dx * (1 - QUARTER), y + height), FloatPoint(x, y + height - dy * (1 - QUARTER)), FloatPoint(x, y + height - dy));
00151 
00152     if (dy < height * 0.5)
00153         path.addLineTo(FloatPoint(x, y + dy));
00154 
00155     path.addBezierCurveTo(FloatPoint(x, y + dy * (1 - QUARTER)), FloatPoint(x + dx * (1 - QUARTER), y), FloatPoint(x + dx, y));
00156 
00157     path.closeSubpath();
00158 
00159     return path;
00160 }
00161 
00162 Path Path::createRoundedRectangle(const FloatRect& rectangle, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius)
00163 {
00164     Path path;
00165 
00166     float width = rectangle.width();
00167     float height = rectangle.height();
00168     if (width <= 0.0 || height <= 0.0)
00169         return path;
00170 
00171     if (width < topLeftRadius.width() + topRightRadius.width()
00172             || width < bottomLeftRadius.width() + bottomRightRadius.width()
00173             || height < topLeftRadius.height() + bottomLeftRadius.height()
00174             || height < topRightRadius.height() + bottomRightRadius.height())
00175         // If all the radii cannot be accommodated, return a rect.
00176         return createRectangle(rectangle);
00177 
00178     float x = rectangle.x();
00179     float y = rectangle.y();
00180 
00181     path.moveTo(FloatPoint(x + topLeftRadius.width(), y));
00182 
00183     path.addLineTo(FloatPoint(x + width - topRightRadius.width(), y));
00184 
00185     path.addBezierCurveTo(FloatPoint(x + width - topRightRadius.width() * (1 - QUARTER), y), FloatPoint(x + width, y + topRightRadius.height() * (1 - QUARTER)), FloatPoint(x + width, y + topRightRadius.height()));
00186 
00187     path.addLineTo(FloatPoint(x + width, y + height - bottomRightRadius.height()));
00188 
00189     path.addBezierCurveTo(FloatPoint(x + width, y + height - bottomRightRadius.height() * (1 - QUARTER)), FloatPoint(x + width - bottomRightRadius.width() * (1 - QUARTER), y + height), FloatPoint(x + width - bottomRightRadius.width(), y + height));
00190 
00191     path.addLineTo(FloatPoint(x + bottomLeftRadius.width(), y + height));
00192 
00193     path.addBezierCurveTo(FloatPoint(x + bottomLeftRadius.width() * (1 - QUARTER), y + height), FloatPoint(x, y + height - bottomLeftRadius.height() * (1 - QUARTER)), FloatPoint(x, y + height - bottomLeftRadius.height()));
00194 
00195     path.addLineTo(FloatPoint(x, y + topLeftRadius.height()));
00196 
00197     path.addBezierCurveTo(FloatPoint(x, y + topLeftRadius.height() * (1 - QUARTER)), FloatPoint(x + topLeftRadius.width() * (1 - QUARTER), y), FloatPoint(x + topLeftRadius.width(), y));
00198 
00199     path.closeSubpath();
00200 
00201     return path;
00202 }
00203 
00204 Path Path::createRectangle(const FloatRect& rectangle)
00205 {
00206     Path path;
00207     float x = rectangle.x();
00208     float y = rectangle.y();
00209     float width = rectangle.width();
00210     float height = rectangle.height();
00211     if (width <= 0.0f || height <= 0.0f)
00212         return path;
00213     
00214     path.moveTo(FloatPoint(x, y));
00215     path.addLineTo(FloatPoint(x + width, y));
00216     path.addLineTo(FloatPoint(x + width, y + height));
00217     path.addLineTo(FloatPoint(x, y + height));
00218     path.closeSubpath();
00219 
00220     return path;
00221 }
00222 
00223 Path Path::createEllipse(const FloatPoint& center, float rx, float ry)
00224 {
00225     float cx = center.x();
00226     float cy = center.y();
00227     Path path;
00228     if (rx <= 0.0f || ry <= 0.0f)
00229         return path;
00230 
00231     float x = cx;
00232     float y = cy;
00233 
00234     unsigned step = 0, num = 100;
00235     bool running = true;
00236     while (running)
00237     {
00238         if (step == num)
00239         {
00240             running = false;
00241             break;
00242         }
00243 
00244         float angle = static_cast<float>(step) / static_cast<float>(num) * 2.0f * piFloat;
00245         x = cx + cosf(angle) * rx;
00246         y = cy + sinf(angle) * ry;
00247 
00248         step++;
00249         if (step == 1)
00250             path.moveTo(FloatPoint(x, y));
00251         else
00252             path.addLineTo(FloatPoint(x, y));
00253     }
00254 
00255     path.closeSubpath();
00256 
00257     return path;
00258 }
00259 
00260 Path Path::createCircle(const FloatPoint& center, float r)
00261 {
00262     return createEllipse(center, r, r);
00263 }
00264 
00265 Path Path::createLine(const FloatPoint& start, const FloatPoint& end)
00266 {
00267     Path path;
00268     if (start.x() == end.x() && start.y() == end.y())
00269         return path;
00270 
00271     path.moveTo(start);
00272     path.addLineTo(end);
00273 
00274     return path;
00275 }
00276 
00277 }

KHTML

Skip menu "KHTML"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal