Back to index

lightning-sunbird  0.9+nobinonly
nsSVGPreserveAspectRatio.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 Mozilla SVG Project code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Jonathan Watt.
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Jonathan Watt <jonathan.watt@strath.ac.uk> (original author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsSVGPreserveAspectRatio.h"
00040 #include "nsSVGValue.h"
00041 #include "nsCRT.h"
00042 #include "nsContentUtils.h"
00043 
00045 // nsSVGPreserveAspectRatio class
00046 
00047 class nsSVGPreserveAspectRatio : public nsIDOMSVGPreserveAspectRatio,
00048                                  public nsSVGValue
00049 {
00050 protected:
00051   friend nsresult NS_NewSVGPreserveAspectRatio(
00052                                         nsIDOMSVGPreserveAspectRatio** result,
00053                                         PRUint16 aAlign,
00054                                         PRUint16 aMeetOrSlice);
00055 
00056   nsSVGPreserveAspectRatio(PRUint16 aAlign, PRUint16 aMeetOrSlice);
00057   ~nsSVGPreserveAspectRatio();
00058 
00059 public:
00060   // nsISupports interface:
00061   NS_DECL_ISUPPORTS
00062 
00063   // nsIDOMSVGPreserveAspectRatio interface:
00064   NS_DECL_NSIDOMSVGPRESERVEASPECTRATIO
00065 
00066   // nsISVGValue interface:
00067   NS_IMETHOD SetValueString(const nsAString& aValue);
00068   NS_IMETHOD GetValueString(nsAString& aValue);
00069 
00070 protected:
00071   PRUint16 mAlign, mMeetOrSlice;
00072 };
00073 
00074 //----------------------------------------------------------------------
00075 // implementation:
00076 
00077 
00078 nsSVGPreserveAspectRatio::nsSVGPreserveAspectRatio(PRUint16 aAlign,
00079                                                    PRUint16 aMeetOrSlice)
00080     : mAlign(aAlign), mMeetOrSlice(aMeetOrSlice)
00081 {
00082 }
00083 
00084 nsSVGPreserveAspectRatio::~nsSVGPreserveAspectRatio()
00085 {
00086 }
00087 
00088 //----------------------------------------------------------------------
00089 // nsISupports methods:
00090 
00091 NS_IMPL_ADDREF(nsSVGPreserveAspectRatio)
00092 NS_IMPL_RELEASE(nsSVGPreserveAspectRatio)
00093 
00094 NS_INTERFACE_MAP_BEGIN(nsSVGPreserveAspectRatio)
00095   NS_INTERFACE_MAP_ENTRY(nsISVGValue)
00096   NS_INTERFACE_MAP_ENTRY(nsIDOMSVGPreserveAspectRatio)
00097   NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(SVGPreserveAspectRatio)
00098   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsISVGValue)
00099 NS_INTERFACE_MAP_END
00100 
00101 //----------------------------------------------------------------------
00102 // nsISVGValue methods:
00103 
00104 NS_IMETHODIMP
00105 nsSVGPreserveAspectRatio::SetValueString(const nsAString& aValue)
00106 {
00107   char* str = ToNewCString(aValue);
00108   if (!str) return NS_ERROR_OUT_OF_MEMORY;
00109 
00110   nsresult rv = NS_OK;
00111 
00112   char* rest = str;
00113   char* token;
00114   const char* delimiters = "\x20\x9\xD\xA";
00115   PRUint16 align, meetOrSlice;
00116 
00117   token = nsCRT::strtok(rest, delimiters, &rest);
00118 
00119   if (token && !strcmp(token, "defer"))
00120     // Ignore: only applicable for preserveAspectRatio on 'image' elements
00121     token = nsCRT::strtok(rest, delimiters, &rest);
00122 
00123   if (token) {
00124     if (!strcmp(token, "none"))
00125       align = SVG_PRESERVEASPECTRATIO_NONE;
00126     else if (!strcmp(token, "xMinYMin"))
00127       align = SVG_PRESERVEASPECTRATIO_XMINYMIN;
00128     else if (!strcmp(token, "xMidYMin"))
00129       align = SVG_PRESERVEASPECTRATIO_XMIDYMIN;
00130     else if (!strcmp(token, "xMaxYMin"))
00131       align = SVG_PRESERVEASPECTRATIO_XMAXYMIN;
00132     else if (!strcmp(token, "xMinYMid"))
00133       align = SVG_PRESERVEASPECTRATIO_XMINYMID;
00134     else if (!strcmp(token, "xMidYMid"))
00135       align = SVG_PRESERVEASPECTRATIO_XMIDYMID;
00136     else if (!strcmp(token, "xMaxYMid"))
00137       align = SVG_PRESERVEASPECTRATIO_XMAXYMID;
00138     else if (!strcmp(token, "xMinYMax"))
00139       align = SVG_PRESERVEASPECTRATIO_XMINYMAX;
00140     else if (!strcmp(token, "xMidYMax"))
00141       align = SVG_PRESERVEASPECTRATIO_XMIDYMAX;
00142     else if (!strcmp(token, "xMaxYMax"))
00143       align = SVG_PRESERVEASPECTRATIO_XMAXYMAX;
00144     else
00145       rv = NS_ERROR_FAILURE;
00146 
00147     if (NS_SUCCEEDED(rv)) {
00148       token = nsCRT::strtok(rest, delimiters, &rest);
00149       if (token) {
00150         if (!strcmp(token, "meet"))
00151           meetOrSlice = SVG_MEETORSLICE_MEET;
00152         else if (!strcmp(token, "slice"))
00153           meetOrSlice = SVG_MEETORSLICE_SLICE;
00154         else
00155           rv = NS_ERROR_FAILURE;
00156       }
00157       else
00158         meetOrSlice = SVG_MEETORSLICE_MEET;
00159     }
00160   }
00161   else  // align not specified
00162     rv = NS_ERROR_FAILURE;
00163 
00164   if (nsCRT::strtok(rest, delimiters, &rest))  // there's more
00165     rv = NS_ERROR_FAILURE;
00166 
00167   if (NS_SUCCEEDED(rv)) {
00168     WillModify();
00169     mAlign = align;
00170     mMeetOrSlice = meetOrSlice;
00171     DidModify();
00172   }
00173 
00174   nsMemory::Free(str);
00175 
00176   return rv;
00177 }
00178 
00179 NS_IMETHODIMP
00180 nsSVGPreserveAspectRatio::GetValueString(nsAString& aValue)
00181 {
00182   // XXX: defer isn't stored
00183 
00184   switch (mAlign) {
00185     case SVG_PRESERVEASPECTRATIO_NONE:
00186       aValue.AssignLiteral("none");
00187       break;
00188     case SVG_PRESERVEASPECTRATIO_XMINYMIN:
00189       aValue.AssignLiteral("xMinYMin");
00190       break;
00191     case SVG_PRESERVEASPECTRATIO_XMIDYMIN:
00192       aValue.AssignLiteral("xMidYMin");
00193       break;
00194     case SVG_PRESERVEASPECTRATIO_XMAXYMIN:
00195       aValue.AssignLiteral("xMaxYMin");
00196       break;
00197     case SVG_PRESERVEASPECTRATIO_XMINYMID:
00198       aValue.AssignLiteral("xMinYMid");
00199       break;
00200     case SVG_PRESERVEASPECTRATIO_XMIDYMID:
00201       aValue.AssignLiteral("xMidYMid");
00202       break;
00203     case SVG_PRESERVEASPECTRATIO_XMAXYMID:
00204       aValue.AssignLiteral("xMaxYMid");
00205       break;
00206     case SVG_PRESERVEASPECTRATIO_XMINYMAX:
00207       aValue.AssignLiteral("xMinYMax");
00208       break;
00209     case SVG_PRESERVEASPECTRATIO_XMIDYMAX:
00210       aValue.AssignLiteral("xMidYMax");
00211       break;
00212     case SVG_PRESERVEASPECTRATIO_XMAXYMAX:
00213       aValue.AssignLiteral("xMaxYMax");
00214       break;
00215     default:
00216       NS_NOTREACHED("Unknown value for mAlign");
00217   }
00218 
00219   // XXX: meetOrSlice may not have been specified in the attribute
00220 
00221   if (mAlign != SVG_PRESERVEASPECTRATIO_NONE) {
00222     switch (mMeetOrSlice) {
00223       case SVG_MEETORSLICE_MEET:
00224         aValue.AppendLiteral(" meet");
00225         break;
00226       case SVG_MEETORSLICE_SLICE:
00227         aValue.AppendLiteral(" slice");
00228         break;
00229       default:
00230         NS_NOTREACHED("Unknown value for mMeetOrSlice");
00231     }
00232   }
00233 
00234   return NS_OK;
00235 }
00236 
00237 //----------------------------------------------------------------------
00238 // nsIDOMSVGPreserveAspectRatio methods:
00239 
00240 /* attribute unsigned short align; */
00241 NS_IMETHODIMP nsSVGPreserveAspectRatio::GetAlign(PRUint16 *aAlign)
00242 {
00243   *aAlign = mAlign;
00244   return NS_OK;
00245 }
00246 NS_IMETHODIMP nsSVGPreserveAspectRatio::SetAlign(PRUint16 aAlign)
00247 {
00248   if (aAlign < SVG_PRESERVEASPECTRATIO_NONE ||
00249       aAlign > SVG_PRESERVEASPECTRATIO_XMAXYMAX)
00250     return NS_ERROR_FAILURE;
00251 
00252   WillModify();
00253   mAlign = aAlign;
00254   DidModify();
00255 
00256   return NS_OK;
00257 }
00258 
00259 /* attribute unsigned short meetOrSlice; */
00260 NS_IMETHODIMP nsSVGPreserveAspectRatio::GetMeetOrSlice(PRUint16 *aMeetOrSlice)
00261 {
00262   *aMeetOrSlice = mMeetOrSlice;
00263   return NS_OK;
00264 }
00265 NS_IMETHODIMP nsSVGPreserveAspectRatio::SetMeetOrSlice(PRUint16 aMeetOrSlice)
00266 {
00267   if (aMeetOrSlice < SVG_MEETORSLICE_MEET ||
00268       aMeetOrSlice > SVG_MEETORSLICE_SLICE)
00269     return NS_ERROR_FAILURE;
00270 
00271   WillModify();
00272   mMeetOrSlice = aMeetOrSlice;
00273   DidModify();
00274 
00275   return NS_OK;
00276 }
00277 
00278 
00280 // Exported creation functions:
00281 
00282 nsresult
00283 NS_NewSVGPreserveAspectRatio(nsIDOMSVGPreserveAspectRatio** result,
00284                              PRUint16 aAlign, PRUint16 aMeetOrSlice)
00285 {
00286   *result = (nsIDOMSVGPreserveAspectRatio*) new nsSVGPreserveAspectRatio(aAlign, aMeetOrSlice);
00287   if (!*result) return NS_ERROR_OUT_OF_MEMORY;
00288 
00289   NS_ADDREF(*result);
00290   return NS_OK;
00291 }