
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* ___________________
*
*  Copyright 2008 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any.  The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
*
* $DateTime: 2008/04/10 13:35:00 $
*
* Adobe Output Module 2.0
*
* MediaGallery Originally Written by Quality Process Incorporated, Enhanced by
* Adobe System China. Contact Sheet written by Adobe System China.
* 
*
**************************************************************************/

/*
	
	Class AOM.PDFGeneratorExternal:
	
	Public Methods:
	
		setPageLayout(dimension, arrange, margins, spacings)
			dimension: Page size in the form of [width, height].
			arrange:   Define how many images placed in each row and column, in
			           the form of [imagePerRow, imagePerColumn].
			margins:   Page margine in horizontal and vertical, in the form of
			           [vertical, horizontal].
			spacings:  Space interval between adjecent images, in the form of
			           [vertical, horizontal].
					   
		addImage(filename, dimension, bits, annotation, colorMode)
			Add an image to the contact sheet document.
			filename:   Filename.
			dimension:  Size of image in pixel, in the form of [width, height].
			bits:       Bits per color component per pixel.
			annotation: Description appearing beneath the image.
			colorMode:	The color mode of the image to be used.
			
		exportPDF(filename)
			Generate PDF disk file.
	
 */


AOM.PDFGeneratorExternal = function(location)
{
	this.images = new Array();
	this.pageFooters = new Array();
	this.pageHeaders = new Array();
	this.footerLines = new Array();
	this.headerLines = new Array();
	
	this.externalFilename = "PDFGenerator";
	this.externalLocation = location;
	this.highQuality = true;
}



AOM.PDFGeneratorExternal.prototype.getFontInformation = function()
{
	this.initPDFL();
	var pdfGenerator = new ExternalObject("lib:" + this.externalLocation
	                                             + this.externalFilename);
	var fontCount;
	var i;
	var result = new Array();

	pdfGenerator.buildFontInformationList();
	fontCount = pdfGenerator.getFontInformationCount();
	
	for(i = 0; i < fontCount; ++i) {
		result.push(pdfGenerator.getFontName(i));
	}

	pdfGenerator.unload();
	return result;
}



AOM.PDFGeneratorExternal.prototype.addImage = function(filename, dimension, bits, annotation, colorMode)
{
	this.images.push([filename, dimension, bits, annotation, colorMode]);
}


AOM.PDFGeneratorExternal.prototype.addFooterHeader = function(text, fontFamily, fontFace, fontSize,
															  color, x, y, isFooter, alignment)
{
	var item = {};
	item.text = text;
	item.fontFamily = fontFamily;
	item.fontFace = fontFace;
	item.fontSize = fontSize;
	item.color = color;
	item.x = x;
	item.y = y;
	item.alignment = alignment;
	
	if(isFooter)
		this.pageFooters.push(item);
	else
		this.pageHeaders.push(item);
}


AOM.PDFGeneratorExternal.prototype.addLine = function(x1, y1, x2, y2, width, color, atFooter)
{
	var item = {};
	item.x1 = x1;
	item.y1 = y1;
	item.x2 = x2;
	item.y2 = y2;
	item.width = width;
	item.color = color;
	
	if(atFooter)
		this.footerLines.push(item);
	else
		this.headerLines.push(item);
}



AOM.PDFGeneratorExternal.prototype.setQuality = function(high)
{
	this.highQuality = true;
}


AOM.PDFGeneratorExternal.prototype.setPageLayout = function(dimension, arrange, margins, spacings,
															footerHeight, headerHeight)
{
	this.pageLayout = new Object();
	this.pageLayout.dimension = dimension;
	this.pageLayout.margins = margins;
	this.pageLayout.spacings = spacings;
	this.pageLayout.arrange = arrange;
	this.pageLayout.footerHeight = footerHeight;
	this.pageLayout.headerHeight = headerHeight;
}



AOM.PDFGeneratorExternal.prototype.setAnnotationRatio = function(ratio)
{
	this.annotationRatio = ratio;
}



AOM.PDFGeneratorExternal.prototype.setFullScreenMode = function(b)
{
	this.fullScreenMode = b;
}



AOM.PDFGeneratorExternal.prototype.setLoop = function(b)
{
    this.loop = b;
}



AOM.PDFGeneratorExternal.prototype.setTransitionMode = function(mode, duration)
{
	this.transitionMode = mode;
	this.transitionDuration = duration;
}



AOM.PDFGeneratorExternal.prototype.setUserPassword = function(userPw)
{
	this.userPw = userPw;
}




AOM.PDFGeneratorExternal.prototype.setOwnerPassword = function(ownerPw)
{
	this.ownerPw = ownerPw;
}




AOM.PDFGeneratorExternal.prototype.setPrintPermission = function(b)
{
	this.printable = b;
}




AOM.PDFGeneratorExternal.prototype.exportPDF = function(filename, majorVer, minorVer)
{
	var i;
	var pdfGenerator;
	
	this.initPDFL();
	pdfGenerator = new ExternalObject("lib:" + this.externalLocation
	                                         + this.externalFilename);
	
	for(i = 0; i < this.images.length; ++i) {
		var annotation = this.images[i][3];
		
		if(this.displayFileExtension != undefined &&
		   this.displayFileExtension == false) {
			annotation = this.stripExtension(annotation);
		}
	
		if(this.displayFilename != undefined &&
		   this.displayFilename == false) {
			annotation = "";
		}
		
		pdfGenerator.addImage(
			this.images[i][0],     // Filename
			this.images[i][1][0],  // Image Width
			this.images[i][1][1],  // Image Height
			this.images[i][2],
			annotation,
			this.images[i][4]);
	}

	pdfGenerator.setPageLayout(
		this.pageLayout.dimension[0],
		this.pageLayout.dimension[1],
		this.pageLayout.arrange[0],
		this.pageLayout.arrange[1],
		this.pageLayout.margins[0],
		this.pageLayout.margins[1],
		this.pageLayout.margins[2],
		this.pageLayout.margins[3],
		this.pageLayout.spacings[0],
		this.pageLayout.spacings[1]);
	
	if(this.pageLayout.footerHeight)
		pdfGenerator.setFooterHeight(this.pageLayout.footerHeight);
	if(this.pageLayout.headerHeight)
		pdfGenerator.setHeaderHeight(this.pageLayout.headerHeight);
	
	for(i = 0; i < this.pageFooters.length; ++i) {
		var item = this.pageFooters[i];
		pdfGenerator.addFooterText(item.text, item.fontFamily, item.fontFace,
		                           item.fontSize, item.color, item.x, item.y, item.alignment);
	}
	for(i = 0; i < this.pageHeaders.length; ++i) {
		var item = this.pageHeaders[i];
		pdfGenerator.addHeaderText(item.text, item.fontFamily, item.fontFace,
		                           item.fontSize, item.color, item.x, item.y, item.alignment);
	}
	for(i = 0; i < this.footerLines.length; ++i) {
		var item = this.footerLines[i];
		pdfGenerator.addFooterLine(item.x1, item.y1, item.x2, item.y2, item.color, item.width);
	}
	for(i = 0; i < this.headerLines.length; ++i) {
		var item = this.headerLines[i];
		pdfGenerator.addHeaderLine(item.x1, item.y1, item.x2, item.y2, item.color, item.width);
	}
	
	if(this.annotationRatio != undefined)
		pdfGenerator.setAnnotationRatio(this.annotationRatio * 100);
	if(this.fullScreenMode != undefined)
		pdfGenerator.setFullScreen(this.fullScreenMode);
	if(this.loop != undefined)
	    pdfGenerator.setLoop(this.loop);
	if(this.transitionMode != undefined)
		pdfGenerator.setTransMode(this.transitionMode, this.transitionDuration);
	if(this.pageDuration != undefined)
		pdfGenerator.setPageDuration(this.pageDuration);
	if(this.background != undefined)
		pdfGenerator.setBackground(this.background[0], this.background[1], this.background[2]);
	if(this.fontColor != undefined)
		pdfGenerator.setFontColor(this.fontColor[0], this.fontColor[1], this.fontColor[2]);
	if(this.userPw != undefined)
		pdfGenerator.setUserPassword(this.userPw);
	if(this.ownerPw != undefined)
		pdfGenerator.setOwnerPassword(this.ownerPw);
	if(this.printable != undefined)
		pdfGenerator.setPrintPermission(this.printable);
		

    if(this.fontFamily != undefined && this.fontFace != undefined)
		pdfGenerator.setFontStyle(this.fontFamily, this.fontFace);

	if(this.placeByColumn != undefined)
		pdfGenerator.setPlaceByColumn(this.placeByColumn);
	
	if(this.watermark != undefined) {
		var offset = this.watermark.offset;
		if(offset == undefined)
			offset = {x:0, y:0};

		if(this.watermark.insertText) {
			pdfGenerator.setWatermarkText(this.watermark.text,
										  this.watermark.fontFamily,
										  this.watermark.fontFace,
										  this.watermark.fontSize,
										  this.watermark.fontColor,
										  this.watermark.opacity,
										  this.watermark.isBackground,
										  this.watermark.rotation * Math.PI / 180.0,
										  this.watermark.placeOnEachImage,
										  Number(offset.x) / 100.0,
										  Number(offset.y) / 100.0);
		} else if(this.watermark.insertImage) {
			// Currently Bridge DOM supports exporting 8-bit images only, so we don't
			// bother to retrieve the bit-per-component from the meta-data but hard
			// code it as 8.
			pdfGenerator.setWatermarkImage(this.watermark.imagePath,
										   this.watermark.intermediaFileName,
										   this.watermark.imageDimension[0],
										   this.watermark.imageDimension[1],
										   this.watermark.imageColorMode,
										   8,
										   this.watermark.imageScale / 100.0,
										   this.watermark.opacity,
										   this.watermark.isBackground,
										   this.watermark.rotation * Math.PI / 180.0,
										   this.watermark.placeOnEachImage,
										   Number(offset.x) / 100.0,
										   Number(offset.y) / 100.0);
		}
	}
	
	var result = pdfGenerator.exportPDF(filename, majorVer, minorVer);
	
	pdfGenerator.reset();
	pdfGenerator.unload();
	
	return result;
}



AOM.PDFGeneratorExternal.prototype.stripExtension = function(filename)
{
	var index;
	index = filename.lastIndexOf(".");
	
	if(index < 0)
		return filename;
	
	return filename.substring(0, index);
}



AOM.PDFGeneratorExternal.prototype.getSystemFontFamilies = function()

{
	this.initPDFL();
	var pdfGenerator = new ExternalObject("lib:" + this.externalLocation
											     + this.externalFilename);
	var result = new Array();
	var fontFamilyCount = pdfGenerator.getSystemFontFamilyCount();
	for(var i = 0; i < fontFamilyCount; ++i) {
		result.push(pdfGenerator.getSystemFontFamily(i));
	}
	
	var failingFontCount = pdfGenerator.getFailingSystemFontFamilyCount();
	result.errors = new Array();
	for(i = 0; i < failingFontCount; ++i) {
		result.errors.push(pdfGenerator.getFailingSystemFontFamily(i));
	}
		
	pdfGenerator.unload();
	return result;
}

AOM.PDFGeneratorExternal.prototype.getSystemFontFaces = function(fontFamilyName)

{
	this.initPDFL();
	var pdfGenerator = new ExternalObject("lib:" + this.externalLocation
												 + this.externalFilename);
	var result = new Array();
	try {
		var fontFamilyCount = pdfGenerator.getSystemFontFaceCount(fontFamilyName);

		for(var i = 0; i < fontFamilyCount; ++i) {
			result.push(pdfGenerator.getSystemFontFace(fontFamilyName, i));
		}
	} catch(e) {
		//
	}
	
	pdfGenerator.unload();
	return result;
}

AOM.PDFGeneratorExternal.prototype.combineDiacriticMarks = function(text)
{
	this.initPDFL();
	var pdfGenerator = new ExternalObject("lib:" + this.externalLocation
												 + this.externalFilename);
	var result = pdfGenerator.combineDiacriticMarks(text);
	
	pdfGenerator.unload();
	return result;
}

/**
 *  Trigger Bridge initializing PDFL. We find no way to initialize
 *  PDFL in PDF Generator without conflict with Bridge in use of
 *  dependent libraries, so we don't do it by ourselves but enforce
 *  Bridge to do it.
 *
 *  This is pretty time-consuming when it is performed first time
 *  during a Bridge session. So it should be invoked while a progress
 *  bar being presented to user.
 */
AOM.PDFGeneratorExternal.prototype.initPDFL = function()
{
	if(AOM.PDFGeneratorExternal.pdflInitialized == undefined)
		AOM.PDFGeneratorExternal.pdflInitialized = new BitmapData(this.externalLocation + "Dummy.pdf");
}
    
    	
