/*
//
//   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:   - Font enumeration, font embedding
// 
// Steps:
// 
// * Call PDEnumSysFonts() to find the font in question
// * To embed an entire font, pass the flag kPDEFontCreateEmbedded when
//     creating the font with PDEFontCreateFromSysFont.
// * To embed and subset a font, pass both the flags kPDEFontCreateEmbedded and
//     kPDEFontWillSubset (or them together) with PDEFontCreateFromSysFont.
// 
// This sample demonstrates also demonstrates use of the enumeration methods
// in the Acrobat API. The most important thing to know about the enumerators
// is that you should not depend on anything getting done in them. If the 
// Library cannot find the fonts *** The enumeration Callback will not get
// called*** Notice how the PDEFont is set as a global in this sample code 
// and then we test to see if it was set in the callback and if not we bail.
// 
*/

#ifndef MAC_ENV
#include <sys/types.h>
#include <sys/stat.h>
#endif

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

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

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

#define FONT_NAME "CourierStd"

PDEFont                   gfont  =  NULL;
Uns8**                    encoding;
char                         fontName[100],  fontType[100],
                                                fontCharSet[100],fontEncoding[100];

ACCB1  ASBool   ACCB2   fontEnumProc(PDSysFont  sysFont,  void  *clientData);
ACCB1  ASBool   ACCB2   fontEnumProc(PDSysFont  sysFont,  void  *clientData)
{
                char  buf[256];
                PDEFontInfoRec  info;
                ASAtom  encname;
                PDEFontAttrs  attrs;
               
                strcpy_safe(buf,  sizeof (buf),  ASAtomGetString(PDSysFontGetName(sysFont)));
                if  (!strcmp(buf, ( char  *)clientData))
               {
                                /* Various methods to obtain information about the system font */
                                PDSysFontGetAttrs(sysFont, &attrs,  sizeof (PDEFontAttrs));

                                /* Checking font embedding policy */
                                if  (attrs.cantEmbed  != 0) {
                                               printf( "This font cannot be embedded" );
                                                return   false ;
                               }

                                encoding  =  PDSysFontGetEncoding(sysFont, &encname);
                                if  (encoding  ==  NULL){
                                                strcpy_safe(fontEncoding,  sizeof (fontEncoding),  "Default" );
                               }
                                else {
                                                strcpy_safe(fontEncoding,  sizeof (fontEncoding),  ASAtomGetString(encname));
                               }
                                               
                                PDSysFontGetInfo(sysFont, &info,  sizeof (PDEFontInfoRec));
                               
                                strcpy_safe(fontName,  sizeof (fontName),  ASAtomGetString(info.name));
                                strcpy_safe(fontType,  sizeof (fontType),  ASAtomGetString(info.type));
                                strcpy_safe(fontCharSet,  sizeof (fontCharSet),  ASAtomGetString(info.charSet));
                               
                                /* We want to both embed the font and subset it, so pass those flags in.
                                 * The font will not actually be subset until PDEFontSubsetNow is called
                                 * after the text is added (so we know what characters to subset).
                                 */
                                gfont  =  PDEFontCreateFromSysFont(sysFont, (kPDEFontCreateEmbedded|kPDEFontWillSubset));
                               
                                /* We found our font - stop the enum */
                                return   false ; 
               }
                /* We didn't find our font - continue enum */
                return   true ; 
}


void   MainProc( void );
void   MainProc( void )
{
                PDDoc  pdDoc =  NULL;
                ASBool  b;
                ASFixedRect  mediaBox = {0,  Int16ToFixed(5*72),  Int16ToFixed(8*72), 0};
                PDPage  pdPage;
                PDEContent  pdeContent =  NULL;
                PDEText  pdeText =  NULL;
                PDEGraphicState  gState;
                PDEColorSpace  pdeColorSpace =  NULL;          /* ColorSpace */
                ASDoubleMatrix  txtMatrix;
                Int32  err = 0;
                ASPathName  outPath =  NULL;

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

                DURING
               {
                                DURING
                               {
                                                /* enumerate the fonts, looking for the font in question 
                                               * See the top of this file for information on the correct use
                                               * of enumerators*/
                                                PDEnumSysFonts(fontEnumProc, ( void *)  FONT_NAME);

                                                /***********************************************************/
                                                if  (gfont)
                                               {
                                                               pdDoc =  PDDocCreate();  /* create new document   */
                                                               pdPage =  PDDocCreatePage(pdDoc,  PDDocGetNumPages(pdDoc) - 1, mediaBox);
                                                               
                                                                /* Acquire PDEContent container for page */
                                                               pdeContent =  PDPageAcquirePDEContent(pdPage,  NULL);         

                                                                /* Fill out the text matrix for a 14 point font */
                                                               memset(&txtMatrix, 0,  sizeof (txtMatrix));
                                                               txtMatrix.a  = 14;
                                                               txtMatrix.d  = 14;

                                                                /* The following code sets up the default Graphics state.   We do this so that
                                                                 * we can free the PDEColorSpace objects.   The code below to set up the default
                                                                 * gState is exactly the same as calling PDEDefaultGState()
                                                                 */
                                                               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;

                                                                /* Create a PDEText object to store the text strings we want to add to the page */
                                                               pdeText =  PDETextCreate();

                                                                /* Specify the horizontal and verical position of the text */
                                                               txtMatrix.h  = 0.25 * 72;
                                                               txtMatrix.v  = 4 * 72;
                                                                PDETextAddEx(pdeText,                                              /* add text: font name */
                                                                                kPDETextRun,
                                                                               0,
                                                                               (Uns8  *)  fontName,
                                                                               strlen(fontName),
                                                                                gfont,
                                                                               &gState,  sizeof (gState),
                                                                               0, 0,
                                                                               &txtMatrix,
                                                                                NULL);

                                                                /* Specify the horizontal and verical position of the text */
                                                               txtMatrix.h  = 0.25 * 72;
                                                               txtMatrix.v  = 3 * 72;
                                                                PDETextAddEx(pdeText,                                              /* add text: font type */
                                                                                kPDETextRun,
                                                                               0, 
                                                                               (Uns8  *)  fontType,
                                                                               strlen(fontType),
                                                                                gfont,
                                                                               &gState,  sizeof (gState),
                                                                               0, 0,
                                                                               &txtMatrix,
                                                                                NULL);

                                                                /* Specify the horizontal and verical position of the text */
                                                               txtMatrix.h  = 0.25 * 72;
                                                               txtMatrix.v  = 2 * 72;
               
                                                               printf ( "fontCharSet = %s, len = %d\n" ,  fontCharSet, strlen(fontCharSet));
                                                                /* Check if fontCharSet in charstring dictionary is NULL. If not, add char set text */
                                                                if  (strlen(fontCharSet) != 0){
                                                                    PDETextAddEx(pdeText,                          /* add text: font char set */
                                                                                kPDETextRun,
                                                                               0, 
                                                                               (Uns8  *)  fontCharSet,
                                                                               strlen(fontCharSet),
                                                                                gfont,
                                                                               &gState,  sizeof (gState),
                                                                               0, 0,
                                                                               &txtMatrix,
                                                                                NULL);
                                                               }
                                                                /* Specify the horizontal and verical position of the text */
                                                               txtMatrix.h  = 0.25 * 72;
                                                               txtMatrix.v  = 1 * 72;
               
                                                                PDETextAddEx(pdeText,                                              /* add text: font encoding */
                                                                                kPDETextRun,
                                                                               0, 
                                                                               (Uns8  *)  fontEncoding,
                                                                               strlen(fontEncoding),
                                                                                gfont,
                                                                               &gState,  sizeof (gState),
                                                                               0, 0,
                                                                               &txtMatrix,
                                                                                NULL);

                                                                /* Add the text elements, including each of the text runs, to the PDEContent
                                                                 * for the page
                                                                 */
                                                                PDEContentAddElem(pdeContent,  kPDEAfterLast, (PDEElement) pdeText);

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

                                                                /* Tell the toolkit to subset the font.   This must be done
                                                                 * after the text block has been created so that we know what characters
                                                                 * to subset.   In addition, subsetting the font must occur after the page
                                                                 * contents have been set.
                                                                 */
                                                                PDEFontSubsetNow(gfont,  PDDocGetCosDoc(pdDoc));
                                               }
                                                else
                                               {
                                                                ASRaise(ASRegisterErrorString(ErrAlways, "Sample Application Error \n Font not found in PDEnumSysFonts() : No Output Produced" ));
                                               }
                               }
                                HANDLER
                               {
                                                RERAISE();
                               }
                                END_HANDLER


                                /*==================================================================*\
                                                                                                                                                 Save Document
                               \*==================================================================*/

                                /* 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
               {
                               err =  ERRORCODE;
               }
                END_HANDLER

                if (err)
                                DisplayError(err);

                /* Release all of the objects we acquired or created */
                if (gfont)              PDERelease((PDEObject)gfont);
                if (pdeText)          PDERelease((PDEObject) pdeText);
                if (pdeColorSpace)              PDERelease((PDEObject) pdeColorSpace);
                if (pdPage)
               {
                                PDPageReleasePDEContent(pdPage,  NULL);
                                PDPageRelease(pdPage);
               }
                if (outPath)          ASFileSysReleasePathNULL, outPath);
                if (pdDoc)              PDDocClose(pdDoc);
}


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