Click here to Skip to main content
15,892,161 members
Articles / Web Development / HTML

Printing Framework

Rate me:
Please Sign up or sign in to vote.
3.77/5 (10 votes)
22 Nov 20032 min read 65.1K   3.3K   28  
Simple framework for text and graphic Printing/Previewing
// FormatString.cpp: implementation of the CFormatString class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FormatString.h"

namespace printer {

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CFormatString::CFormatString(const CString& strText)
{
	m_strText=strText;
}

CFormatString::~CFormatString()
{
}

//////////////////////////////////////////////////////////////////////
// Static
//////////////////////////////////////////////////////////////////////

void
CFormatString::GetCesure(CDC* pDC, int nLon, CString& strText, int& nFirst, int& nLast)
{
	CSize size;
	nLast =0;
	
	// Avaler les blancs
	CString strTrim = strText;
	strTrim.TrimLeft();
	int nLength = strTrim.GetLength();
	nFirst = strText.GetLength()-nLength;

	for(int i=0; i< nLength; i++) {
		if(strTrim[i]==' ') {
			CString strTmp =strTrim.Left(i);
			size = pDC->GetTextExtent(strTrim.Left(i));
			if (size.cx > nLon) {
				break;
			}
			nLast = i+nFirst;
		}
	}

	// Si sortie sur fin de list
	if (i==nLength) {
		size = pDC->GetTextExtent(strTrim);
		if ( size.cx <= nLon ) {
			nLast = i+ nFirst;
		}
	}

//	TRACE ("%d %d\n", nFirst, nLast);

	// Si nLast =0 => Le mot + long que la taille
	// => c�sure arbitraitre au max
	if (nLast==0) {
		for(int i=0; i< nLength; i++) {
			size = pDC->GetTextExtent(strTrim.Left(i));
			if (size.cx > nLon) {
				break;
			}
		}
		nLast = i+nFirst;
	}
}

void
CFormatString::Initialize(CDC* pDC, int nLon)
{
	m_listFirstLast.clear();
	int nFirst;
	int nLast;
	int nOld =0;
	CString strText = m_strText;
	CString strTmp;

	do {
		GetCesure(pDC, nLon, strText, nFirst, nLast);
		strTmp  = strText.Mid(nFirst, nLast-nFirst);
		strText = strText.Mid(nLast);
		m_listFirstLast.push_back( std::pair<int,int>(nOld+nFirst, nOld+nLast) );
		nOld += nLast;
//		TRACE("'%s'\n ", strTmp);
	} while (!strText.IsEmpty()); 
}


int
CFormatString::GetNbLignes()
{
	return m_listFirstLast.size();
}

CString
CFormatString::GetSubLigne(int nIndex)
{
	ASSERT( nIndex < GetNbLignes() );
	ASSERT( nIndex >= 0);

	int nFirst =m_listFirstLast[nIndex].first;
	int nLast = m_listFirstLast[nIndex].second;
	
	return m_strText.Mid(nFirst, nLast-nFirst);
}

int 
CFormatString::GetLength(int nIndex)
{
	return GetSubLigne(nIndex).GetLength();
}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions