--- Download/firefox-1.5.0.1-source/mozilla/layout/printing/nsPrintEngine.cpp 2006-01-11 17:28:14.000000000 +0100 +++ workspace/mozilla/layout/printing/nsPrintEngine.cpp 2006-04-07 00:38:34.000000000 +0200 @@ -60,6 +60,8 @@ #include "nsXPCOM.h" #include "nsISupportsPrimitives.h" +#include + // PrintOptions is now implemented by PrintSettingsService static const char sPrintSettingsServiceContractID[] = "@mozilla.org/gfx/printsettings-service;1"; static const char sPrintOptionsContractID[] = "@mozilla.org/gfx/printsettings-service;1"; @@ -545,6 +547,130 @@ nsIDocShell * aDocShell, FILE* aFD); #endif +////////////////////////////////////////////////////////////// +// PATCH TO ALLOW SILENT PRINTING TO POSTSCRIPT FILE FOR YAHP +// original idea can be found here... don't understand the +// language but I got the idea +// http://groups.google.be/group/at.linux/browse_thread/ +// thread/c3372b731efb5de3/e51ce6724523f031?lnk=st&q= +// batch+print+mozilla&rnum=5&hl=fr#e51ce6724523f031 +// added method : +// nsPrintEngine::GetEnvVar +// nsPrintEngine::ConvertToDouble +// nsPrintEngine::SetPrintSettingsForYaHP +// SetPrintSettingsForYaHP is called from nsPrintEngine::Print +////////////////////////////////////////////////////////////// +/** + * This method retrieve the value of the environment variable s + * @param env - The mozilla environment object + * @param s - a string containing the name of the variable to get + * If the variable is set, the value is assigned to the string s + */ +void nsPrintEngine::GetEnvVar(nsIEnvironment* env,nsString & s) { + PRBool result; + // check if the variable is set + env->Exists(s,&result); + if (result) { + // get the value + env->Get(s,s); + // YaHP set environment variables in UTF-8 format + // convert it to the correct encoding + // get the char buffer + char * val = ToNewCString(s); + nsCString cs(val); + // convert from UTF-8 to UTF-16 PRUnichar + s.Assign(NS_ConvertUTF8toUTF16(cs).get()); + // release C string memory + delete val; + } else { + // replace by empty string + s.Assign(NS_LITERAL_STRING("").get()); + } +} + +/** + * Convert a string to a double + * @param s - the String to convert + * @param defaultValue - the default value if value is 0.0 + * @return the converted value or defaultValue. + */ +double nsPrintEngine::ConvertToDouble(nsString & s,double defaultValue) { + // convert to a C string + char * val = ToNewCString(s); + // use stdlib.h atof function. + double ret = atof(val); + // release C string memory + delete val; + if (ret == 0.0) { + return defaultValue; + } + return ret; +} + +/** + * Set the printing environment with value set by YaHPConverter. + * @param mPrt - The mozilla print data object + */ +void nsPrintEngine::SetPrintSettingsForYaHP(nsPrintData* mPrt) { + nsresult rv; + nsCOMPtr env = + do_CreateInstance("@mozilla.org/process/environment;1", &rv); + if (NS_SUCCEEDED(rv)) { + // environment variable set by YaHPConverter + nsString mozPsFilename(NS_LITERAL_STRING("MOZ_PS_FILENAME").get()); + nsString marginTop(NS_LITERAL_STRING("MOZ_MARGIN_TOP").get()); + nsString marginLeft(NS_LITERAL_STRING("MOZ_MARGIN_LEFT").get()); + nsString marginBottom(NS_LITERAL_STRING("MOZ_MARGIN_BOTTOM").get()); + nsString marginRight(NS_LITERAL_STRING("MOZ_MARGIN_RIGHT").get()); + nsString paperWidth(NS_LITERAL_STRING("MOZ_PAPER_WIDTH").get()); + nsString paperHeight(NS_LITERAL_STRING("MOZ_PAPER_HEIGHT").get()); + nsString headerLeft(NS_LITERAL_STRING("MOZ_HEADER_LEFT").get()); + nsString headerCentre(NS_LITERAL_STRING("MOZ_HEADER_CENTRE").get()); + nsString headerRight(NS_LITERAL_STRING("MOZ_HEADER_RIGHT").get()); + nsString footerLeft(NS_LITERAL_STRING("MOZ_FOOTER_LEFT").get()); + nsString footerCentre(NS_LITERAL_STRING("MOZ_FOOTER_CENTRE").get()); + nsString footerRight(NS_LITERAL_STRING("MOZ_FOOTER_RIGHT").get()); + // get the actual value + GetEnvVar(env,mozPsFilename); + GetEnvVar(env,marginTop); + GetEnvVar(env,marginLeft); + GetEnvVar(env,marginBottom); + GetEnvVar(env,marginRight); + GetEnvVar(env,paperWidth); + GetEnvVar(env,paperHeight); + GetEnvVar(env,headerLeft); + GetEnvVar(env,headerCentre); + GetEnvVar(env,headerRight); + GetEnvVar(env,footerLeft); + GetEnvVar(env,footerCentre); + GetEnvVar(env,footerRight); + // Set print properties + // ==> To a file + // ==> Silent (no selection popup) + mPrt->mPrintSettings->SetPrintToFile( PR_TRUE ); + mPrt->mPrintSettings->SetPrintSilent( PR_TRUE ); + mPrt->mPrintSettings->SetPrintInColor( PR_TRUE ); + mPrt->mPrintSettings->SetPrintBGColors( PR_TRUE ); + mPrt->mPrintSettings->SetToFileName(mozPsFilename.get()); + // Set header/footer + mPrt->mPrintSettings->SetHeaderStrLeft(headerLeft.get()); + mPrt->mPrintSettings->SetHeaderStrCenter(headerCentre.get()); + mPrt->mPrintSettings->SetHeaderStrRight(headerRight.get()); + mPrt->mPrintSettings->SetFooterStrLeft(footerLeft.get()); + mPrt->mPrintSettings->SetFooterStrCenter(footerCentre.get()); + mPrt->mPrintSettings->SetFooterStrRight(footerRight.get()); + // Set margin + mPrt->mPrintSettings->SetMarginTop(ConvertToDouble(marginTop,0.39)); + mPrt->mPrintSettings->SetMarginLeft(ConvertToDouble(marginLeft,0.39)); + mPrt->mPrintSettings->SetMarginBottom(ConvertToDouble(marginBottom,0.39)); + mPrt->mPrintSettings->SetMarginRight(ConvertToDouble(marginRight,0.39)); + // Set paper size + mPrt->mPrintSettings->SetPaperWidth(ConvertToDouble(paperWidth,210.0)); + mPrt->mPrintSettings->SetPaperHeight(ConvertToDouble(paperHeight,290.0)); + mPrt->mPrintSettings->SetPaperSizeUnit(nsIPrintSettings::kPaperSizeMillimeters); + } +} + //--------------------------------------------------------------------------------- NS_IMETHODIMP nsPrintEngine::Print(nsIPrintSettings* aPrintSettings, @@ -577,6 +703,8 @@ if (!mPrt->mPrintSettings) { GetGlobalPrintSettings(getter_AddRefs(mPrt->mPrintSettings)); } + // Set YaHPConverter properties in the print object + SetPrintSettingsForYaHP(mPrt); mPrt->mPrintOptions = do_GetService(sPrintOptionsContractID, &rv); if (NS_SUCCEEDED(rv) && mPrt->mPrintOptions && mPrt->mPrintSettings) {