136 lines
3.6 KiB
C++
136 lines
3.6 KiB
C++
/******************************************************************************
|
|
* File : Metafile.cpp (c) 1997-2002, Furix
|
|
*
|
|
* Author : J. Groot Lipman
|
|
* Project : BetterWMF
|
|
* Version : 4.00
|
|
*
|
|
* Function : Core file of BetterWMF. Contains all functions to convert a
|
|
* WMF-file or Clipboard object to a better one
|
|
*****************************************************************************/
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "metafile.h"
|
|
|
|
/*****************************************************************************
|
|
* Static variables for CMetafile. Only temporary used for/in static playback
|
|
* procedures
|
|
*/
|
|
|
|
// ==========================================================================
|
|
/*
|
|
|
|
FUNCTION : WriteMetaFileAsPlaceable
|
|
|
|
PARAMETERS : HMETAFILE - a handle to the metafile
|
|
LPCSTR lpstrOutFileName - filename to write to
|
|
|
|
PURPOSE : Write a metafile in placeable format
|
|
|
|
RETURNS : BOOL - TRUE is Success, FALSE is failure
|
|
|
|
COMMENTS : Fills out and writes a placeable header, followed by the
|
|
metafile bits, to the file.
|
|
|
|
HISTORY : 4/17/95 - Created
|
|
|
|
*/
|
|
BOOL CMetafile::WriteMetaFileAsPlaceable( HMETAFILE hOld, CSize Size, double Inch, LPCSTR lpstrOutFileName)
|
|
{
|
|
FILE *OutFile;
|
|
//OFSTRUCT outof;
|
|
|
|
DWORD dwSize;
|
|
LPBYTE pBits;
|
|
|
|
ALDUSMFHEADER pAldusMFHdr;
|
|
// Fill out the placeable header
|
|
pAldusMFHdr.key = ALDUS_KEY;
|
|
pAldusMFHdr.hmf = NULL;
|
|
pAldusMFHdr.inch = (unsigned short)Inch;
|
|
;
|
|
pAldusMFHdr.reserved = 0;
|
|
pAldusMFHdr.bbox.left = (short)0;
|
|
pAldusMFHdr.bbox.top = (short)0;
|
|
pAldusMFHdr.bbox.right = (short)Size.cx;
|
|
pAldusMFHdr.bbox.bottom = (short)Size.cy;
|
|
pAldusMFHdr.checksum = CalculateAPMCheckSum( pAldusMFHdr );
|
|
|
|
// Get the bits
|
|
if( (dwSize = GetMetaFileBitsEx( hOld, 0, NULL )) == 0 )
|
|
{
|
|
DeleteMetaFile( hOld );
|
|
// myErrorBox(NULL, "Failed to Get MetaFile Bits Size");
|
|
return NULL;
|
|
}
|
|
// Allocate that much memory
|
|
if( (pBits = (LPBYTE) malloc( dwSize )) == NULL )
|
|
{
|
|
DeleteMetaFile( hOld );
|
|
// myErrorBox(NULL, "Failed to Allocate Memory for Metafile Bits");
|
|
return NULL;
|
|
}
|
|
// Get the metafile bits
|
|
if( GetMetaFileBitsEx( hOld, dwSize, pBits ) == 0 )
|
|
{
|
|
free( pBits );
|
|
DeleteMetaFile( hOld );
|
|
// myErrorBox(NULL, "Failed to Get MetaFile Bits");
|
|
return NULL;
|
|
}
|
|
|
|
// TODO: Error checking
|
|
// Open the file
|
|
OutFile = fopen( lpstrOutFileName, "wb" );
|
|
|
|
// Write the header
|
|
fwrite(&pAldusMFHdr, 1, sizeof( APMFILEHEADER ), OutFile);
|
|
|
|
// Write the bits
|
|
fwrite(pBits, 1, dwSize, OutFile );
|
|
|
|
// Close the file
|
|
fclose(OutFile);
|
|
|
|
// Clean up
|
|
free( pBits );
|
|
return TRUE;
|
|
}
|
|
// End WriteMetaFileAsPlaceable
|
|
// ==========================================================================
|
|
|
|
|
|
// ==========================================================================
|
|
/*
|
|
|
|
FUNCTION : CalculateAPMCheckSum
|
|
|
|
PARAMETERS : APMFILEHEADER apmfh - A placeable header
|
|
|
|
PURPOSE : Calculates the checksum for a placeable header
|
|
|
|
RETURNS : WORD - the checksum
|
|
|
|
COMMENTS : The checksum is derived by XORing each word.
|
|
|
|
HISTORY : 4/17/95 - Created
|
|
|
|
*/
|
|
WORD CMetafile::CalculateAPMCheckSum( ALDUSMFHEADER pAldusMFHdr )
|
|
{
|
|
LPWORD lpWord;
|
|
WORD wResult, i;
|
|
|
|
// Start with the first word
|
|
wResult = *(lpWord = (LPWORD)(&pAldusMFHdr));
|
|
// XOR in each of the other 9 words
|
|
for(i=1;i<=9;i++)
|
|
{
|
|
wResult ^= lpWord[i];
|
|
}
|
|
return wResult;
|
|
}
|
|
// End CalculateAPMCheckSum
|
|
// ==========================================================================
|