Back to index

lightning-sunbird  0.9+nobinonly
nsSVGCairoPathBuilder.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Mozilla SVG Cairo Renderer project.
00016  *
00017  * The Initial Developer of the Original Code is IBM Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2004
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Parts of this file contain code derived from the following files(s)
00022  * of the Mozilla SVG project (these parts are Copyright (C) by their
00023  * respective copyright-holders):
00024  *    layout/svg/renderer/src/libart/nsSVGLibartBPathBuilder.cpp
00025  *
00026  * Contributor(s):
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either of the GNU General Public License Version 2 or later (the "GPL"),
00030  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 
00042 #include "nsCOMPtr.h"
00043 #include "nsISVGRendererPathBuilder.h"
00044 #include "nsSVGCairoPathBuilder.h"
00045 #include <math.h>
00046 #include "cairo.h"
00047 
00052 
00053 
00056 class nsSVGCairoPathBuilder : public nsISVGRendererPathBuilder
00057 {
00058 protected:
00059   friend nsresult NS_NewSVGCairoPathBuilder(nsISVGRendererPathBuilder **result,
00060                                             cairo_t *ctx);
00061 
00062   nsSVGCairoPathBuilder(cairo_t *ctx);
00063 
00064 public:
00065   // nsISupports interface:
00066   NS_DECL_ISUPPORTS
00067 
00068   // nsISVGRendererPathBuilder interface:
00069   NS_DECL_NSISVGRENDERERPATHBUILDER
00070 
00071 private:
00072 
00073   cairo_t *mCR;
00074 };
00075 
00078 //----------------------------------------------------------------------
00079 // implementation:
00080 
00081 nsSVGCairoPathBuilder::nsSVGCairoPathBuilder(cairo_t *ctx)
00082   : mCR(ctx)
00083 {
00084 }
00085 
00086 nsresult
00087 NS_NewSVGCairoPathBuilder(nsISVGRendererPathBuilder **result,
00088                           cairo_t *ctx)
00089 {
00090   *result = new nsSVGCairoPathBuilder(ctx);
00091   if (!*result)
00092     return NS_ERROR_OUT_OF_MEMORY;
00093 
00094   NS_ADDREF(*result);
00095     
00096   return NS_OK;
00097 }
00098 
00099 //----------------------------------------------------------------------
00100 // nsISupports methods:
00101 
00102 NS_IMPL_ADDREF(nsSVGCairoPathBuilder)
00103 NS_IMPL_RELEASE(nsSVGCairoPathBuilder)
00104 
00105 NS_INTERFACE_MAP_BEGIN(nsSVGCairoPathBuilder)
00106 NS_INTERFACE_MAP_ENTRY(nsISVGRendererPathBuilder)
00107 NS_INTERFACE_MAP_ENTRY(nsISupports)
00108 NS_INTERFACE_MAP_END
00109 
00110 //----------------------------------------------------------------------
00111 // nsISVGRendererPathBuilder methods:
00112 
00114 NS_IMETHODIMP
00115 nsSVGCairoPathBuilder::Moveto(float x, float y)
00116 {
00117   cairo_move_to(mCR, x, y);
00118   return NS_OK;
00119 }
00120 
00122 NS_IMETHODIMP
00123 nsSVGCairoPathBuilder::Lineto(float x, float y)
00124 {
00125   cairo_line_to(mCR, x, y);
00126   return NS_OK;
00127 }
00128 
00130 NS_IMETHODIMP
00131 nsSVGCairoPathBuilder::Curveto(float x, float y, float x1, float y1, float x2, float y2)
00132 {
00133   cairo_curve_to(mCR, x1, y1, x2, y2, x, y);
00134   return NS_OK;
00135 }
00136 
00137 /* helper for Arcto : */
00138 static inline double CalcVectorAngle(double ux, double uy, double vx, double vy)
00139 {
00140   double ta = atan2(uy, ux);
00141   double tb = atan2(vy, vx);
00142   if (tb >= ta)
00143     return tb-ta;
00144   return 6.28318530718 - (ta-tb);
00145 }
00146 
00147 
00149 NS_IMETHODIMP
00150 nsSVGCairoPathBuilder::Arcto(float x2, float y2, float rx, float ry, float angle, PRBool largeArcFlag, PRBool sweepFlag)
00151 {
00152   const double pi = 3.14159265359;
00153   const double radPerDeg = pi/180.0;
00154 
00155   double x1=0.0, y1=0.0;
00156   cairo_get_current_point(mCR, &x1, &y1);
00157 
00158   // 1. Treat out-of-range parameters as described in
00159   // http://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
00160   
00161   // If the endpoints (x1, y1) and (x2, y2) are identical, then this
00162   // is equivalent to omitting the elliptical arc segment entirely
00163   if (x1 == x2 && y1 == y2) return NS_OK;
00164 
00165   // If rX = 0 or rY = 0 then this arc is treated as a straight line
00166   // segment (a "lineto") joining the endpoints.
00167   if (rx == 0.0f || ry == 0.0f) {
00168     Lineto(x2, y2);
00169     return NS_OK;
00170   }
00171 
00172   // If rX or rY have negative signs, these are dropped; the absolute
00173   // value is used instead.
00174   if (rx<0.0) rx = -rx;
00175   if (ry<0.0) ry = -ry;
00176   
00177   // 2. convert to center parameterization as shown in
00178   // http://www.w3.org/TR/SVG/implnote.html
00179   double sinPhi = sin(angle*radPerDeg);
00180   double cosPhi = cos(angle*radPerDeg);
00181   
00182   double x1dash =  cosPhi * (x1-x2)/2.0 + sinPhi * (y1-y2)/2.0;
00183   double y1dash = -sinPhi * (x1-x2)/2.0 + cosPhi * (y1-y2)/2.0;
00184 
00185   double root;
00186   double numerator = rx*rx*ry*ry - rx*rx*y1dash*y1dash - ry*ry*x1dash*x1dash;
00187 
00188   if (numerator < 0.0) { 
00189     //  If rX , rY and are such that there is no solution (basically,
00190     //  the ellipse is not big enough to reach from (x1, y1) to (x2,
00191     //  y2)) then the ellipse is scaled up uniformly until there is
00192     //  exactly one solution (until the ellipse is just big enough).
00193 
00194     // -> find factor s, such that numerator' with rx'=s*rx and
00195     //    ry'=s*ry becomes 0 :
00196     float s = (float)sqrt(1.0 - numerator/(rx*rx*ry*ry));
00197 
00198     rx *= s;
00199     ry *= s;
00200     root = 0.0;
00201 
00202   }
00203   else {
00204     root = (largeArcFlag == sweepFlag ? -1.0 : 1.0) *
00205       sqrt( numerator/(rx*rx*y1dash*y1dash+ry*ry*x1dash*x1dash) );
00206   }
00207   
00208   double cxdash = root*rx*y1dash/ry;
00209   double cydash = -root*ry*x1dash/rx;
00210   
00211   double cx = cosPhi * cxdash - sinPhi * cydash + (x1+x2)/2.0;
00212   double cy = sinPhi * cxdash + cosPhi * cydash + (y1+y2)/2.0;
00213   double theta1 = CalcVectorAngle(1.0, 0.0, (x1dash-cxdash)/rx, (y1dash-cydash)/ry);
00214   double dtheta = CalcVectorAngle((x1dash-cxdash)/rx, (y1dash-cydash)/ry,
00215                                   (-x1dash-cxdash)/rx, (-y1dash-cydash)/ry);
00216   if (!sweepFlag && dtheta>0)
00217     dtheta -= 2.0*pi;
00218   else if (sweepFlag && dtheta<0)
00219     dtheta += 2.0*pi;
00220   
00221   // 3. convert into cubic bezier segments <= 90deg
00222   int segments = (int)ceil(fabs(dtheta/(pi/2.0)));
00223   double delta = dtheta/segments;
00224   double t = 8.0/3.0 * sin(delta/4.0) * sin(delta/4.0) / sin(delta/2.0);
00225   
00226   for (int i = 0; i < segments; ++i) {
00227     double cosTheta1 = cos(theta1);
00228     double sinTheta1 = sin(theta1);
00229     double theta2 = theta1 + delta;
00230     double cosTheta2 = cos(theta2);
00231     double sinTheta2 = sin(theta2);
00232     
00233     // a) calculate endpoint of the segment:
00234     double xe = cosPhi * rx*cosTheta2 - sinPhi * ry*sinTheta2 + cx;
00235     double ye = sinPhi * rx*cosTheta2 + cosPhi * ry*sinTheta2 + cy;
00236 
00237     // b) calculate gradients at start/end points of segment:
00238     double dx1 = t * ( - cosPhi * rx*sinTheta1 - sinPhi * ry*cosTheta1);
00239     double dy1 = t * ( - sinPhi * rx*sinTheta1 + cosPhi * ry*cosTheta1);
00240     
00241     double dxe = t * ( cosPhi * rx*sinTheta2 + sinPhi * ry*cosTheta2);
00242     double dye = t * ( sinPhi * rx*sinTheta2 - cosPhi * ry*cosTheta2);
00243 
00244     // c) draw the cubic bezier:
00245     Curveto((float)xe, (float)ye, (float)(x1+dx1), (float)(y1+dy1),
00246             (float)(xe+dxe), (float)(ye+dye));
00247 
00248     // do next segment
00249     theta1 = theta2;
00250     x1 = (float)xe;
00251     y1 = (float)ye;
00252   }
00253 
00254   return NS_OK;
00255 }
00256 
00258 NS_IMETHODIMP
00259 nsSVGCairoPathBuilder::ClosePath(float *newX, float *newY)
00260 {
00261   cairo_close_path(mCR);
00262   return NS_OK;
00263 }
00264 
00266 NS_IMETHODIMP
00267 nsSVGCairoPathBuilder::EndPath()
00268 {
00269   return NS_OK;
00270 }