⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scemfdoc.cpp

📁 Source code for EMFexplorer 1.0
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		return;
	}

	ASSERT(m_Properties.bSizeAllPages);
	int iMinX = INT_MAX;
	int iMinY = INT_MAX;
	int iMaxX = INT_MIN;
	int iMaxY = INT_MIN;

	for (UINT uiPage=0; (uiPage<m_uiNbFiles); uiPage++)
	{
		PSCEMFDocPage pDocPage = m_vDocPages[uiPage];
		ASSERT(pDocPage);
		HENHMETAFILE hEMF = pDocPage->SCGetPageEMF();
		if (hEMF)
		{
			CRect& ElemsRect = pDocPage->m_rectSize;
			// deflate
			CRect& rectI = pDocPage->m_rectInflate;
			ElemsRect.InflateRect(-rectI.left, -rectI.top, -rectI.right, -rectI.bottom);
			//
			
			if (ElemsRect.left<iMinX)
				iMinX = ElemsRect.left;
			if (ElemsRect.top<iMinY)
				iMinY = ElemsRect.top;

			if (ElemsRect.right>iMaxX)
				iMaxX = ElemsRect.right;
			if (ElemsRect.bottom>iMaxY)
				iMaxY = ElemsRect.bottom;
		}
	}

	ASSERT(iMinX != INT_MAX);
	ASSERT(iMinY != INT_MAX);
	ASSERT(iMaxX != INT_MIN);
	ASSERT(iMaxY != INT_MIN);

	for (uiPage=0; (uiPage<m_uiNbFiles); uiPage++)
	{
		m_vDocPages[uiPage]->m_rectSize.SetRect(iMinX, iMinY, iMaxX, iMaxY);
	}
	m_Properties.rectDocument.SetRect(iMinX, iMinY, iMaxX, iMaxY);
}

void SCEMFDoc::SCInflateElemsRect(UINT uiPage, int iLeft, int iTop, int iRight, int iBottom)
{
	ASSERT(uiPage<m_uiNbFiles);
	if (uiPage>=m_uiNbFiles)
		return;
	m_vDocPages[uiPage]->SCInflateElemsRect(iLeft, iTop, iRight, iBottom);
}

///
/// Install all fonts found in the 'Fonts' subdirectory (of the document's directory)
///
void SCEMFDoc::SCInstallFonts()
{
	SCDeleteLockedFontFiles(); // delayed clean up

	// For now, just install what is found in the Fonts subdirectory if any
	if (m_strUniDocName.IsEmpty())
		return;

	TCHAR drive[_MAX_DRIVE];	// drive of the playlist
	TCHAR dir[_MAX_DIR];		// dir of the playlist
	_tsplitpath(LPCTSTR(m_strUniDocName), drive, dir, NULL, NULL);

	// Look for TT fonts
	CString strFile;
	strFile.Format(_T("%s%s%s"), drive, dir, _T("Fonts\\*.*"));

	CFileFind finder;
	BOOL bFound = finder.FindFile((LPCTSTR)strFile);
	BOOL bTemp = FALSE;
	CString strTempPath;
	if (bFound)
	{
		bTemp = (!SCIsWriteableMedia(m_strUniDocName));
		if (bTemp)
		{// create temp directory
			if (!::SCCreateTempDir(strTempPath, SC_EMFX_TEMPSUBDIR))
			{
				ASSERT(0);
				finder.Close();
				return;
			}
		}
	}
	
	while (bFound)
	{
		bFound = finder.FindNextFile();
		if (!finder.IsDirectory())
		{
			CString strFilePath = finder.GetFilePath();
			if (!SCIsTrueTypeFontFile(strFilePath))
				continue;

			// TODO: Check that font is not already installed
			// if (m_pSysFontsHolder && m_pSysFontsHolder->SCIsFontInstalled(strFilePath))
			// continue;

			if (bTemp)
			{// copy to temp directory
				TCHAR szFname[_MAX_FNAME];
				SCSplitPath((LPTSTR)LPCTSTR(strFilePath), NULL, NULL, szFname, NULL);
				
				CString strFilename;
				strFilename.Format(_T("%s%s%s"), strTempPath, szFname, SC_EMFX_TMPFONT_EXT);
#ifdef _DEBUG
				ASSERT(!SCExistFile(strFilename));
#endif
				BOOL bOk = CopyFile(strFilePath, strFilename, TRUE);
				ASSERT(bOk);
				strFilePath = strFilename;
			}
			
			SCInstallFontFile(strFilePath, bTemp);
		}
	}	
	finder.Close();
#ifdef _DEBUG
	int iCount = m_FontRes.GetCount();
#endif
}

///
/// Uninstall fonts, and delete temporary files.
///
void SCEMFDoc::SCUnInstallFonts()
{
	CString strTempPath;
	::SCGetFullTempDirName(strTempPath, SC_EMFX_TEMPSUBDIR);
	BOOL bTempCheck = FALSE;
	BOOL bTempFiles = FALSE;

	POSITION pos = m_FontRes.GetHeadPosition();
	CString strFilename;
	BOOL bOK;
	while(pos)
	{
		strFilename = m_FontRes.GetNext(pos);

		ASSERT(!strFilename.IsEmpty());
		bOK = RemoveFontResource(strFilename);

		ASSERT(bOK);
		if (!bOK)
			continue;
		
		// delete the scalable resource file
		bOK = DeleteFile(strFilename);
		ASSERT(bOK);
		
		if (!bTempCheck)
		{// check if TT files are in temp directory
			TCHAR szDrive[SC_MAX_UNC_DRIVE];
			TCHAR szDir[_MAX_DIR];
			SCSplitPath(strFilename, szDrive, szDir, NULL, NULL);
			CString strFilesDir;
			strFilesDir.Format(_T("%s%s"), szDrive, szDir);
			
			if (0==strTempPath.Compare(strFilesDir))
			{
				int iPos = strFilename.ReverseFind(_T('.'));
				if (iPos>0)
				{
					CString strTTFName = strFilename.Left(iPos) + SC_EMFX_TMPFONT_EXT;
					bTempFiles = SCExistFile(strTTFName);
				}
			}
			
			bTempCheck = TRUE;
		}

		// delete temporary font file
		if (bTempFiles)
		{
			int iPos = strFilename.ReverseFind(_T('.'));
			if (iPos>0)
			{
				CString strTTFName = strFilename.Left(iPos) + SC_EMFX_TMPFONT_EXT;
				bOK = SCRemoveFileAttributes(strTTFName, FILE_ATTRIBUTE_READONLY);
				ASSERT(bOK);

				bOK = DeleteFile(strTTFName);
				
				// Deceptively, Windows locks some fonts AFTER they have passed through
				// metafile playing, though they are OUR private fonts.
				// TODO: find reason.
				if (!bOK)
					m_LockedFontFiles.AddTail(strTTFName); // font to delete later
			}
		}
	}
	m_FontRes.RemoveAll();
}

///
/// Delete fonts that were locked in a previous attempt to delete them.
///
void SCEMFDoc::SCDeleteLockedFontFiles(BOOL bClosing/*=FALSE*/)
{
	POSITION pos = m_LockedFontFiles.GetHeadPosition();
	CString strFilename;

	if (bClosing)
	{// Just delete the files. They should be deleteable at this point.
		while(pos)
		{
			strFilename = m_LockedFontFiles.GetNext(pos);
			ASSERT(!strFilename.IsEmpty());

			BOOL bOK = SCRemoveFileAttributes(strFilename, FILE_ATTRIBUTE_READONLY);
			ASSERT(bOK);

			bOK = DeleteFile(strFilename);
			ASSERT(bOK);
		}
		return;
	}

	// Attemp to delete. Keep those files that are still locked.
	CStringList RemainList;
	while(pos)
	{
		strFilename = m_LockedFontFiles.GetNext(pos);
		ASSERT(!strFilename.IsEmpty());
		if (!DeleteFile(strFilename))
			RemainList.AddTail(strFilename);
	}
	m_LockedFontFiles.RemoveAll();
	pos = RemainList.GetHeadPosition();
	while(pos)
	{
		m_LockedFontFiles.AddTail(RemainList.GetNext(pos));
	}
}


///
/// Install a TrueType read-only embedded font from memory data
///
BOOL SCEMFDoc::SCInstallMemoryFont(CString strFaceName, BYTE* lpData, long lDataSize)
{
	// 1. check if its a TrueType

	// 2. avoid installing fonts on the system (copy their files
	// into temp directory)
	CString strTempPath;
	if (!::SCCreateTempDir(strTempPath, SC_EMFX_TEMPSUBDIR))
	{
		ASSERT(0);
		return FALSE;
	}

	int iPos = strFaceName.ReverseFind(_T('.'));
	if (-1==iPos)
		strFaceName += SC_EMFX_TMPFONT_EXT;
	else
		strFaceName = strFaceName.Left(iPos) + SC_EMFX_TMPFONT_EXT;

	CString strFilename = strTempPath + strFaceName;
#ifdef _DEBUG
	ASSERT(!SCExistFile(strFilename)); // it should not exist
#endif

	CFile file;
	CFileException fe;
	// create new file, 'Open' won't throw exception
	if (file.Open(strFilename, CFile::modeCreate |
	  CFile::modeWrite | CFile::shareExclusive, &fe))		
	{
		// save the data
		try
		{
			file.Write(lpData, lDataSize);
		}
		catch(CFileException* pEx)
		{
			pEx->Delete();
			ASSERT(0);
			file.Close();
			return FALSE;
		}

		file.Close();
	} else
	{
		ASSERT(0);
		return FALSE;
	}

	// 3. install and store filename
	return SCInstallFontFile(strFilename, TRUE);
}

///
/// Install a TrueType read-only embedded font from a file
///
BOOL SCEMFDoc::SCInstallFontFile(CString strFilename, BOOL bTemporary)
{
	CString strFontRes;
	int iPos = strFilename.ReverseFind(_T('.'));
	if (iPos>0)
	{
		strFontRes = strFilename.Left(iPos) + SC_EMFX_TMPFRES_EXT;
	} else
		strFontRes = strFilename + SC_EMFX_TMPFRES_EXT;
	
	DeleteFile(strFontRes);
	BOOL bCreated = CreateScalableFontResource(
		1,						// 0/1=flag for read-only embedded font
		LPCTSTR(strFontRes),	// pointer to filename for font resource
		LPCTSTR(strFilename),	// pointer to filename for scalable font
		LPCTSTR(NULL)    // pointer to path to font file
		);
	DWORD dwError = GetLastError();

	//ASSERT(bCreated); // Font file must be TrueType

	if (!bCreated)
	{
		if (bTemporary)
			DeleteFile(strFilename);
		return FALSE;
	}
	int iInstalled = AddFontResource(strFontRes);
	ASSERT(iInstalled);

	if (!iInstalled)
	{
		BOOL bOK = RemoveFontResource(strFontRes);
		ASSERT(bOK);
		if (bTemporary)
			DeleteFile(strFilename);
		return FALSE;
	}
	m_FontRes.AddTail(strFontRes);

	return TRUE;
}

void SCEMFDoc::SCNewDocument()
{
	SCBeginAddFiles(0);
	SCEndAddFiles();
}

UINT SCEMFDoc::SCOpenDocument(LPCTSTR lpszPathName)
{
	UINT uiFileType = SCEMFDoc::SCGetFileType(lpszPathName);
	switch(uiFileType & SC_FTYPE_MASK)
	{
	case SC_FTYPE_UKN :
		return uiFileType;

	case SC_FTYPE_BGP :
		SCLoadMaster(lpszPathName);
		break;
		
	case SC_FTYPE_EMF:
	case SC_FTYPE_WMF:
	case SC_FTYPE_EMZ:
	case SC_FTYPE_WMZ:
	case SC_FTYPE_IMG:
		SCLoadSinglePage(lpszPathName, uiFileType);
		break;

	case SC_FTYPE_TXT:
		if (!SCLoadRTFPages(lpszPathName, FALSE,
			(SC_SUBTYPE_TXT_RTF==(uiFileType & SC_SUBTYPE_MASK))))
			return SC_FTYPE_UKN;
		break;

	default:
		ASSERT(0);
	}

	return uiFileType;
}

BOOL SCEMFDoc::SCInsertFile(LPCTSTR lpszPathName, UINT* puiType/*=NULL*/)
{
	UINT uiFileType = SCEMFDoc::SCGetFileType(lpszPathName);
	if (puiType)
		*puiType = uiFileType;
	BOOL bInserted = FALSE;
	switch(uiFileType & SC_FTYPE_MASK)
	{
	case SC_FTYPE_UKN :
		return FALSE;

	case SC_FTYPE_BGP :
		// Merge isn't supported in this version
		//return SCLoadMaster(lpszPathName, TRUE);
		return FALSE;
		
	case SC_FTYPE_EMF:
	case SC_FTYPE_WMF:
	case SC_FTYPE_EMZ:
	case SC_FTYPE_WMZ:
	case SC_FTYPE_IMG:
		bInserted = SCLoadSinglePage(lpszPathName, uiFileType, TRUE);
		break;

	case SC_FTYPE_TXT:
		bInserted = SCLoadRTFPages(lpszPathName, TRUE,
			(SC_SUBTYPE_TXT_RTF==(uiFileType & SC_SUBTYPE_MASK)));
		break;

	default:
		ASSERT(0);
	}

	if (bInserted)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -