/*
//
//   ADOBE SYSTEMS INCORPORATED
//   Copyright (C) 2000-2003 Adobe Systems Incorporated
//   All rights reserved.
//
//   NOTICE: Adobe permits you to use, modify, and distribute this file
//   in accordance with the terms of the Adobe license agreement
//   accompanying it. If you have received this file from a source other
//   than Adobe, then your use, modification, or distribution of it
//   requires the prior written permission of Adobe.
//
*/
/*
// 
// Project: - "Hello World" Example of Simple Text Display
// 
// Demonstrates inserting text into PDF document.
// 
// Things To Know:
// 
// Different font styles such as italic or bold are actually different type-
// faces.   For instance, to use an italicized Helvetica font, acquire the
// font "Helvetica,Italic"
// 
// 
// Steps:
// 
// * Initialize PDFEdit
// 
// * Create new doc
// * Set page size and insert new page into doc
// * Create container for page content
// 
// * Acquire/create font
// * Set text attribute
// * Add text to text element
// * Insert text element into page content
// 
// * Convert content into resource & content objects
// * Put resource & content objects into page
// 
// * Save document
// 
// * Release all objects acquired/created
// 
// * Terminate PDFEdit
// 
*/

#ifdef MAC_ENV
#include <Carbon/Carbon.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

#include "PDFInit.h"
#include "CosCalls.h"
#include "CorCalls.h"
#include "ASCalls.h"
#include "PDCalls.h"
#include "PSFCalls.h"
#include "PERCalls.h"
#include "PEWCalls.h"
#include "PIExcept.h"
#include "PagePDECntCalls.h"
#include "MyPDFLibUtils.h"

#ifdef MAC_ENV                                                                   
#include "macUtils.h"
#endif

void   MainProc( void );
void   MainProc( void )
{
                ASBool  b;
                PDDoc  pdDoc =  NULL;                                                          /* reference to a PDF document   */
                PDPage  pdPage =  NULL;                                      /* reference to a page in doc */
                PDEContent  pdeContent;                                    /* container for page content */
                ASFixedRect  mediaBox;                                      /* dimensions of page */
                PDSysFont  sysFont;                                                            /* used by PDEFont creation */
                PDEFont  pdeFont =  NULL;                                  /* reference to a font used on a page */
                PDEFontAttrs  pdeFontAttrs;                            /* font attributes       */
                PDEText  pdeText =  NULL;                                  /* container for text */
                ASDoubleMatrix  textMatrix;                            /* transformation matrix for text */
                PDEColorSpace  pdeColorSpace =  NULL;          /* ColorSpace */
                PDEGraphicState  gState;                                  /* graphic state to apply to operation */
                const   char  *HelloWorldStr =  "Hello World!" ;
                ASPathName  outPath =  NULL;
                ASErrorCode  errCode = 0;



/*==================================================================*\
                                   Create Doc, Page, Content Container                                 
\*==================================================================*/

DURING

               pdDoc =  PDDocCreate();                                        /* create new document   */

               mediaBox.left      =  fixedZero;                            /* dimensions of page     */
               mediaBox.top        =  Int16ToFixed(4*72);          /* in this case 5" x 4" */
               mediaBox.right    =  Int16ToFixed(5*72);
               mediaBox.bottom  =  fixedZero;

                /* create page with those dimensions and insert as first page         */
               pdPage =  PDDocCreatePage(pdDoc,  PDBeforeFirstPage, mediaBox);

                /* Acquire PDEContent container for page */
               pdeContent =  PDPageAcquirePDEContent(pdPage,  NULL);         

/*==================================================================*\
           Acquire Font, Add Text, Insert Into Page Content Container 
\*==================================================================*/    

               memset(&pdeFontAttrs, 0,  sizeof (pdeFontAttrs));
               pdeFontAttrs.name  =  ASAtomFromString( "CourierStd" );
               pdeFontAttrs.type  =  ASAtomFromString( "Type1" );

               sysFont =  PDFindSysFont(&pdeFontAttrs,  sizeof (PDEFontAttrs), 0);
               pdeFont =  PDEFontCreateFromSysFont(sysFont,  kPDEFontDoNotEmbed);

                /* The following code sets up the default Graphics state.   We do this so that
                 * we can free the PDEColorSpace objects
                 */
               pdeColorSpace =  PDEColorSpaceCreateFromName(ASAtomFromString( "DeviceGray" ));
               memset(&gState, 0,  sizeof (PDEGraphicState));
               gState.strokeColorSpec.space  = gState.fillColorSpec.space  = pdeColorSpace;
               gState.miterLimit  =  fixedTen;
               gState.flatness  =  fixedOne;
               gState.lineWidth  =  fixedOne;

               memset(&textMatrix, 0,  sizeof (textMatrix));        /* clear structure */
               textMatrix.a  = 24;          /* set font width and height */
               textMatrix.d  = 24;          /* to 24 point size             */
               textMatrix.h  = 1*72;      /* x,y coordinate on page */
               textMatrix.v  = 2*72;      /* in this case, 1" x 2"   */
               pdeText =  PDETextCreate();                      /* create new text run       */
                PDETextAddEx(pdeText,                  /* text container to add to   */
                                kPDETextRun,                            /* kPDETextRun, kPDETextChar */
                               0,                                                /* index */
                               (Uns8  *)HelloWorldStr,        /* text to add       */
                               strlen(HelloWorldStr),        /* length of text */
                               pdeFont,                                    /* font to apply to text */
                               &gState,  sizeof (gState),    /* graphic state to apply to text   */
                                NULL, 0,                                    /* text state and size of structure*/
                               &textMatrix,                            /* transformation matrix for text   */
                                NULL);                                        /* stroke matrix   */

    /* insert text into page content */
                PDEContentAddElem(pdeContent,  kPDEAfterLast, (PDEElement) pdeText);


/*==================================================================*\
                   Convert To Objects, Add To Page, Release Resources
\*==================================================================*/

                /* Set the PDEContent for the page */
               b =  PDPageSetPDEContent(pdPage,  NULL);

                /* save document to a file */
#if !MAC_ENV
               outPath =  ASFileSysCreatePathFromDIPathNULL,  "out.pdf" ,  NULL  );
#else
               outPath =  GetMacPath( "out.pdf" );
#endif
                PDDocSave(pdDoc,  PDSaveFull  |  PDSaveLinearized, outPath,  ASGetDefaultFileSys(),  NULLNULL);
HANDLER
               errCode =  ERRORCODE;
END_HANDLER

                if (errCode)
                                DisplayError(errCode);

                /* remember to release all objects that were created   */
                if (outPath)          ASFileSysReleasePathNULL, outPath);
                if (pdeFont)          PDERelease((PDEObject) pdeFont);
                if (pdeText)          PDERelease((PDEObject) pdeText);
                if (pdeColorSpace)              PDERelease((PDEObject)pdeColorSpace);
                if (pdPage)
               {             
                                PDPageReleasePDEContent(pdPage,  NULL);
                                PDPageRelease(pdPage);
               }
                if (pdDoc)              PDDocRelease(pdDoc);
}


#define INCLUDE_MYPDFLIBAPP_CPP 1
#include "MyPDFLibApp.cpp"
#undef INCLUDE_MYPDFLIBAPP_CPP