Back to index

lightning-sunbird  0.9+nobinonly
nsSVGFill.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 project.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Crocodile Clips Ltd..
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Alex Fritze <alex.fritze@crocodile-clips.com> (original author)
00024  *   Bradley Baetz <bbaetz@cs.mcgill.ca>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include <math.h>
00041 
00042 #include "nsSVGFill.h"
00043 #include "nsISVGPathGeometrySource.h"
00044 
00045 // static helper functions 
00046 
00047 #define EPSILON 1e-6
00048 
00049 static PRBool ContainsOpenPath(ArtVpath* src)
00050 {
00051   int pos;
00052   for(pos=0;src[pos].code != ART_END;++pos) {
00053     if (src[pos].code==ART_MOVETO_OPEN)
00054       return PR_TRUE;
00055   }
00056   return PR_FALSE;
00057 }
00058 
00059 // Closes an open path, returning a new one
00060 static ArtVpath* CloseOpenPath(ArtVpath* src)
00061 {
00062   // We need to insert something extra for each open subpath
00063   // So we realloc stuff as needed
00064   int currSize = 0;
00065   int maxSize = 4;
00066   ArtVpath *ret = art_new(ArtVpath, maxSize);
00067 
00068   PRBool isOpenSubpath = PR_FALSE;
00069   double startX=0;
00070   double startY=0;
00071 
00072   int srcPos;
00073   for (srcPos=0;src[srcPos].code != ART_END;++srcPos) {
00074     if (currSize==maxSize)
00075       art_expand(ret, ArtVpath, maxSize);
00076     if (src[srcPos].code == ART_MOVETO_OPEN)
00077       ret[currSize].code = ART_MOVETO; // close it
00078     else
00079       ret[currSize].code = src[srcPos].code;
00080     ret[currSize].x = src[srcPos].x;
00081     ret[currSize].y = src[srcPos].y;
00082     ++currSize;
00083 
00084     // OK, it was open
00085     if (src[srcPos].code == ART_MOVETO_OPEN) {
00086       startX = src[srcPos].x;
00087       startY = src[srcPos].y;
00088       isOpenSubpath = PR_TRUE;
00089     } else if (src[srcPos+1].code != ART_LINETO) {
00090       if (isOpenSubpath &&
00091           ((fabs(startX - src[srcPos].x) > EPSILON) ||
00092            (fabs(startY - src[srcPos].y) > EPSILON))) {
00093         // The next one isn't a line, so lets close this
00094         if (currSize == maxSize)
00095           art_expand(ret, ArtVpath, maxSize);
00096         ret[currSize].code = ART_LINETO;
00097         ret[currSize].x = startX;
00098         ret[currSize].y = startY;
00099         ++currSize;
00100       }
00101       isOpenSubpath = PR_FALSE;
00102     }
00103   }
00104   if (currSize == maxSize)
00105     art_expand(ret, ArtVpath, maxSize);
00106   ret[currSize].code = ART_END;
00107   ret[currSize].x = 0;
00108   ret[currSize].y = 0;
00109   return ret;
00110 }
00111 
00112 // nsSVGFill members
00113   
00114 void
00115 nsSVGFill::Build(ArtVpath* path, nsISVGPathGeometrySource* source)
00116 {
00117   if (mSvp) {
00118     art_svp_free(mSvp);
00119     mSvp = nsnull;
00120   }
00121 
00122   NS_ASSERTION(path, "null vpath");
00123   
00124   // clip
00125   ArtVpathArrayIterator src_iter;
00126 //  ArtVpathPolyRectClipFilter clip_filter;
00127   
00128   art_vpath_array_iterator_init((ArtVpath*)path, &src_iter);
00129 //  art_vpath_poly_rect_clip_filter_init((ArtVpathIterator*)&src_iter,
00130 //                                       canvasSpecs->GetClipRect(),
00131 //                                       &clip_filter);
00132   
00133 //  path = art_vpath_new_vpath_array((ArtVpathIterator*)&clip_filter);
00134   path = art_vpath_new_vpath_array((ArtVpathIterator*)&src_iter);
00135 
00136   
00137   if (ContainsOpenPath(path)) {
00138     ArtVpath* tmp_path = path;
00139     path = CloseOpenPath(tmp_path);
00140     art_free(tmp_path);
00141   }
00142   
00143   ArtVpath* perturbedVP = art_vpath_perturb(path);
00144   art_free(path);
00145   
00146   ArtSVP* svp = art_svp_from_vpath(perturbedVP);
00147   art_free(perturbedVP);
00148   
00149   ArtSVP* uncrossedSVP = art_svp_uncross(svp);
00150   art_svp_free(svp);
00151   
00152   PRUint16 fillrule;
00153   source->GetFillRule(&fillrule);
00154   ArtWindRule wind;
00155   switch (fillrule) {
00156     case nsISVGGeometrySource::FILL_RULE_NONZERO:
00157       wind = ART_WIND_RULE_NONZERO;
00158       break;
00159     case nsISVGGeometrySource::FILL_RULE_EVENODD:
00160       wind = ART_WIND_RULE_ODDEVEN;
00161       break;
00162     default:
00163       NS_ERROR("not reached");
00164   }
00165   
00166   mSvp = art_svp_rewind_uncrossed (uncrossedSVP, wind);
00167   art_svp_free (uncrossedSVP);
00168 
00169 }
00170