Back to index

lightning-sunbird  0.9+nobinonly
nsEPSObjectPS.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ex: set tabstop=8 softtabstop=4 shiftwidth=4 expandtab: */
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is developed for mozilla.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Kenneth Herron <kherron@fastmail.us>.
00020  * Portions created by the Initial Developer are Copyright (C) 2004
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Roland Mainz <roland.mainz@nrubsig.org>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * 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 "nsEPSObjectPS.h"
00041 #include "prprf.h"
00042 
00043 
00044 /* For details on the EPSF spec, see Adobe specification #5002,
00045  * "Encapsulated PostScript File Format Specification". The document
00046  * structuring conventions are described in Specificion #5001, 
00047  * "PostScript Language Document Structuring Conventions Specification".
00048  */
00049 
00050 
00055 nsEPSObjectPS::nsEPSObjectPS(FILE *aFile) :
00056   mStatus(NS_ERROR_INVALID_ARG),
00057   mEPSF(aFile),
00058   mBBllx(0.0),
00059   mBBlly(0.0),
00060   mBBurx(0.0),
00061   mBBury(0.0)
00062 {
00063   NS_PRECONDITION(aFile != nsnull,   "aFile == nsnull");
00064   NS_PRECONDITION(0 == fseek(aFile, 0L, SEEK_SET), "File isn't seekable");    
00065   Parse();
00066 }
00067 
00085 PRBool
00086 nsEPSObjectPS::EPSFFgets(nsACString& aBuffer)
00087 {
00088   aBuffer.Truncate();
00089   while (1) {
00090     int ch = getc(mEPSF);
00091     if ('\n' == ch) {
00092       /* Eat any following carriage return */
00093       ch = getc(mEPSF);
00094       if ((EOF != ch) && ('\r' != ch))
00095         ungetc(ch, mEPSF);
00096       return PR_TRUE;
00097     }
00098     else if ('\r' == ch) {
00099       /* Eat any following line feed */
00100       ch = getc(mEPSF);
00101       if ((EOF != ch) && ('\n' != ch))
00102         ungetc(ch, mEPSF);
00103       return PR_TRUE;
00104     }
00105     else if (EOF == ch) {
00106       /* If we read any text before the EOF, return true. */
00107       return !aBuffer.IsEmpty();
00108     }
00109 
00110     /* Normal case */
00111     aBuffer.Append((char)ch);
00112   }
00113 }
00114 
00115 
00120 void
00121 nsEPSObjectPS::Parse()
00122 {
00123   nsCAutoString line;
00124 
00125   NS_PRECONDITION(nsnull != mEPSF, "No file");
00126 
00127   rewind(mEPSF);
00128   while (EPSFFgets(line)) {
00129     if (PR_sscanf(line.get(), "%%%%BoundingBox: %lf %lf %lf %lf",
00130                   &mBBllx, &mBBlly, &mBBurx, &mBBury) == 4) {
00131       mStatus = NS_OK;
00132       return;
00133     }
00134   }
00135   mStatus = NS_ERROR_INVALID_ARG;
00136 }
00137 
00138 
00144 nsresult
00145 nsEPSObjectPS::WriteTo(FILE *aDest)
00146 {
00147   NS_PRECONDITION(NS_SUCCEEDED(mStatus), "Bad status");
00148 
00149   nsCAutoString line;
00150   PRBool        inPreview = PR_FALSE;
00151 
00152   rewind(mEPSF);
00153   while (EPSFFgets(line)) {
00154     if (inPreview) {
00155       /* filter out the print-preview section */
00156       if (StringBeginsWith(line, NS_LITERAL_CSTRING("%%EndPreview")))
00157           inPreview = PR_FALSE;
00158       continue;
00159     }
00160     else if (StringBeginsWith(line, NS_LITERAL_CSTRING("%%BeginPreview:"))){
00161       inPreview = PR_TRUE;
00162       continue;
00163     }
00164 
00165     /* Output the EPSF with this platform's line terminator */
00166     fwrite(line.get(), line.Length(), 1, aDest);
00167     putc('\n', aDest);
00168   }
00169   return NS_OK;
00170 }
00171 
00172