Back to index

lightning-sunbird  0.9+nobinonly
nsISVGPathFlatten.h
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 project.
00016  *
00017  * The Initial Developer of the Original Code is IBM Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2005
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either of the GNU General Public License Version 2 or later (the "GPL"),
00025  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #ifndef __NS_ISVGPATHFLATTEN_H__
00038 #define __NS_ISVGPATHFLATTEN_H__
00039 
00040 #include "nsISupports.h"
00041 #include <stdlib.h>
00042 #include <math.h>
00043 
00045 // nsISVGPathFlatten
00046 
00047 #define NS_ISVGPATHFLATTEN_IID \
00048 { 0x8bcaff8f, 0xb85e, 0x4138, { 0xaf, 0x29, 0x08, 0x9c, 0x96, 0x95, 0x8b, 0x5c } }
00049 
00050 #define NS_SVGPATHFLATTEN_LINE 0
00051 #define NS_SVGPATHFLATTEN_MOVE 1
00052 
00053 class nsSVGPathData
00054 {
00055 private:
00056   PRUint32 arraysize;
00057 
00058 public:
00059   PRUint32 count;
00060   float *x;
00061   float *y;
00062   PRUint8 *type;
00063   
00064   nsSVGPathData() : arraysize(0), count(0), x(nsnull), y(nsnull), type(nsnull) {}
00065   ~nsSVGPathData() {
00066     if (x) free(x);
00067     if (y) free(y);
00068     if (type) free(type);
00069   }
00070 
00071   void AddPoint(float aX, float aY, PRUint8 aType) {
00072     if (count + 1 > arraysize) {
00073       if (!arraysize)
00074         arraysize = 16;
00075       x = (float *) realloc(x, 2*arraysize*sizeof(float));
00076       y = (float *) realloc(y, 2*arraysize*sizeof(float));
00077       type = (PRUint8 *) realloc(type, 2*arraysize*sizeof(PRUint8));
00078       arraysize *= 2;
00079     }
00080     x[count] = aX;
00081     y[count] = aY;
00082     type[count] = aType;
00083     count++;
00084   }
00085 
00086   float Length() {
00087     float length = 0;
00088     if (count > 0) {
00089       // Must initialize xx and yy before they're used in the loop below
00090       float xx = x[0];
00091       float yy = y[0];
00092       // Loop from i=1 because dx and dy would always be zero for i==0 since xx
00093       // and yy would then *be* x[0] and y[0]. Also the first segment *must*
00094       // be a moveto (M) command, which also results in a dx and dy of zero.
00095       for (PRUint32 i = 1; i < count; i++) {
00096         if (type[i] == NS_SVGPATHFLATTEN_LINE) {
00097           float dx = x[i] - xx;
00098           float dy = y[i] - yy;
00099           length += sqrt(dx*dx + dy*dy);
00100         }
00101         xx = x[i];
00102         yy = y[i];
00103       }
00104     }
00105     return length;
00106   }
00107 };
00108 
00109 class nsISVGPathFlatten : public nsISupports
00110 {
00111 public:
00112   static const nsIID& GetIID() { static nsIID iid = NS_ISVGPATHFLATTEN_IID; return iid; }
00113 
00114   NS_IMETHOD GetFlattenedPath(nsSVGPathData **_retval, PRBool useLocalTransfrom = PR_FALSE)=0;
00115 };
00116 
00117 #endif // __NS_ISVGPATHFLATTEN_H__