📄 htmlparse.cpp
字号:
case pHeight:
nHeight = GetNumberParameter( strParam, nHeight );
break;
case pAlign:
alg = GetAlignmentFromString( strParam, alg );
break;
case pSrc:
strFilename = strParam;
break;
case pBorder:
nBorder = GetNumberParameter( strParam, nBorder );
break;
}
}
CHTMLParagraph *pPara = m_pDocument->CurrentParagraph();
// Before loading the image, see if it already exists in the cache.
StringClass fname( strFilename.GetData(), strFilename.GetLength() );
CImage** ppLoadedImage = m_pMasterDocument->m_mapImages.Lookup(fname);
CImage* pImage;
if (!ppLoadedImage)
{
TRACENL( _T("Image %s not in cache. Attempting Load\n"), (LPCTSTR)fname);
pImage = OnLoadImage( fname );
if( !pImage )
{
TRACENL( _T("Image %s could not be loaded.\n"), (LPCTSTR)fname);
// Consider making this image a global instance.
pImage = new CImage;
VERIFY( pImage->Load( g_hResourceInstance, g_uNoImageBitmapID ) );
}
m_pMasterDocument->m_mapImages.SetAt(fname, pImage);
}
else
{
TRACENL( _T("Image %s loaded from cache.\n"), (LPCTSTR)fname);
pImage = *ppLoadedImage;
}
CHTMLImage *pHTMLImage = new CHTMLImage( nWidth, nHeight, nBorder, fname, alg, pImage );
UpdateItemLinkStatus( pHTMLImage );
pPara->AddItem( pHTMLImage );
}
CImage *CHTMLParse::OnLoadImage( LPCTSTR pcszFilename )
//
// Called when we need an image to be loaded.
{
return AquireImage( m_hInstLoadedFrom, m_pcszFilePath, pcszFilename );
}
void CHTMLParse::UpdateItemLinkStatus( CHTMLParagraphObject *pItem )
{
// Assign the current link pointer to the object
pItem->m_pAnchor = m_pLastAnchor;
}
void CHTMLParse::CreateNewTextObject()
//
// Create a new text block and add it to the current docuemnt-paragraph
{
if( m_strToken.GetSize() )
{
CHTMLParagraph *pPara = m_pDocument->CurrentParagraph();
HTMLFontDef def( m_pProp->szFaceName, m_pProp->nSize, m_pProp->bBold, m_pProp->bItalic, m_pProp->bUnderline, m_pProp->bStrikeThrough, m_pProp->m_nSup, m_pProp->m_nSub );
CStaticString str( m_strToken.GetData(), m_strToken.GetSize() );
CHTMLTextBlock *pText = new CHTMLTextBlock( str, m_pDocument->GetFontDefIndex( def ), m_pProp->m_crFore, IsPreformatted() );
if( m_pLastAnchor )
UpdateItemLinkStatus( pText );
pPara->AddItem( pText );
m_strToken.SetSize( 0 );
}
}
void CHTMLParse::CreateNewProperties()
//
// Create a new properties set and add it to the stack.
{
m_pProp = new Properties( *m_pProp );
m_stkProperties.Push( m_pProp );
}
void CHTMLParse::PopPreviousProperties()
{
if( m_stkProperties.GetSize() > 1 )
{
delete m_stkProperties.Pop();
m_pProp = m_stkProperties.Top();
}
}
void CHTMLParse::CreateNewParagraph( int nLinesAbove, int nLinesBelow, Align alg )
{
if( m_pDocument->CurrentParagraph() && m_pDocument->CurrentParagraph()->IsEmpty() )
{
m_pDocument->CurrentParagraph()->Reset( nLinesAbove, nLinesBelow, alg );
}
else
{
m_pDocument->AddParagraph( new CHTMLParagraph( nLinesAbove, nLinesBelow, alg ) );
}
}
void CHTMLParse::OnGotHR( const CParameters &pList )
{
CreateNewTextObject();
if( !m_pDocument->CurrentParagraph() || !m_pDocument->CurrentParagraph()->IsEmpty() )
{
CreateNewParagraph( m_pDefaults->m_nParagraphLinesAbove, m_pDefaults->m_nParagraphLinesBelow, m_pProp->nAlignment );
}
CHTMLParse::Align alg = knDefaultHRAlignment;
int nSize = 2;
int nWidth = -100;
bool bNoShade = false;
COLORREF crColor = 0;
const UINT uParamSize = pList.GetSize();
for( UINT n = 0; n < uParamSize; n++ )
{
const CStaticString &strParam = pList[n].m_strValue;
switch( pList[n].m_param )
{
case pWidth:
nWidth = GetNumberParameterPercent( strParam, nWidth );
if( nWidth == 0 )
nWidth = -100;
break;
case pSize:
// REVIEW - russf - could allow dan to use point/font sizes here too.
nSize = GetNumberParameter(strParam, nSize);
if( nSize <= 0 )
nSize = 3;
break;
case pNoShade:
bNoShade = true;
break;
case pAlign:
alg = GetAlignmentFromString( strParam, alg );
break;
case pColor:
crColor = GetColourFromString( strParam, crColor );
break;
}
}
CHTMLParagraph *pPara = m_pDocument->CurrentParagraph();
CHTMLHorizontalRule *pHR = new CHTMLHorizontalRule( alg, nSize, nWidth, bNoShade, crColor );
UpdateItemLinkStatus( pHR );
pPara->AddItem( pHR );
CreateNewParagraph( m_pDefaults->m_nParagraphLinesAbove, m_pDefaults->m_nParagraphLinesBelow, m_pProp->nAlignment );
}
void CHTMLParse::OnGotParagraph( const CParameters &pList )
{
CreateNewTextObject();
CHTMLParse::Align alg = m_pProp->nAlignment;
const UINT uParamSize = pList.GetSize();
for( UINT n = 0; n < uParamSize; n++ )
{
const CStaticString &strParam = pList[n].m_strValue;
switch( pList[n].m_param )
{
case pAlign:
alg = GetAlignmentFromString( strParam, alg );
break;
}
}
m_pProp->nAlignment;
CreateNewParagraph( m_pDefaults->m_nParagraphLinesAbove, m_pDefaults->m_nParagraphLinesBelow, alg );
}
void CHTMLParse::OnGotFont( const CParameters &pList )
{
CreateNewTextObject();
CreateNewProperties();
const UINT uParamSize = pList.GetSize();
for( UINT n = 0; n < uParamSize; n++ )
{
const CStaticString &strParam = pList[n].m_strValue;
switch( pList[n].m_param )
{
case pColor:
m_pProp->m_crFore = GetColourFromString( strParam, m_pProp->m_crFore );
break;
case pFace:
GetFontName( m_pProp->szFaceName, countof( m_pProp->szFaceName ), strParam );
break;
case pSize:
{
//
// REVIEW - russf - Could add the ability to have +n and -n font and point sizes here.
m_pProp->nSize = GetFontSize( strParam, m_pProp->nSize );
}
break;
}
}
}
void CHTMLParse::OnGotHeading( const Token token, const CParameters &pList )
{
int nLinesAbove = 1, nLinesBelow = 1;
Align alg = m_pProp->nAlignment;
CreateNewTextObject();
CreateNewProperties();
switch( token )
{
case tokH1:
m_pProp->nSize = 6;
m_pProp->bBold = true;
break;
case tokH2:
m_pProp->nSize = 5;
m_pProp->bBold = true;
break;
case tokH3:
m_pProp->nSize = 4;
m_pProp->bBold = true;
break;
case tokH4:
m_pProp->nSize = 3;
m_pProp->bBold = true;
break;
case tokH5:
m_pProp->nSize = 3;
m_pProp->bBold = true;
break;
case tokH6:
nLinesAbove = 2;
m_pProp->nSize = 1;
m_pProp->bBold = true;
break;
}
const UINT uParamSize = pList.GetSize();
for( UINT n = 0; n < uParamSize; n++ )
{
const CStaticString &strParam = pList[n].m_strValue;
switch( pList[n].m_param )
{
case pAlign:
alg = GetAlignmentFromString( strParam, alg );
break;
}
}
CreateNewParagraph( nLinesAbove, nLinesBelow, alg );
}
void CHTMLParse::OnGotAnchor( const CParameters &pList )
{
// We will no longer store link attributes in the properties.
// They will instead be stored in an inline ParagraphObject
// The sectionCreator will be responsible for keeping the list
// of links and map of named sections
// Since the coloring will change, we will still create a new
// text object. This also ensures that we have a paragraph into which
// we store the HTMLAnchor object.
CreateNewTextObject();
// Anchor information is no longer stored in the properties stack,
// So we don't need new properties.
// CreateNewProperties();
// Create a new Anchor object, then store some data in it.
// This one has a default ctor, which provides empty members.
// We need to specify colors, though.
CHTMLAnchor* pAnchor = new CHTMLAnchor();
const UINT uParamSize = pList.GetSize();
for( UINT n = 0; n < uParamSize; n++ )
{
const CStaticString &strParam = pList[n].m_strValue;
switch( pList[n].m_param )
{
case pName:
pAnchor->m_strLinkName.Set( strParam.GetData(), strParam.GetLength() );
break;
case pHref:
pAnchor->m_strLinkTarget.Set( strParam.GetData(), strParam.GetLength() );
break;
case pTitle:
pAnchor->m_strLinkTitle.Set( strParam.GetData(), strParam.GetLength() );
break;
}
}
// Get the colors from the master document, which is the only place they should be set
pAnchor->m_crLink = m_pMasterDocument->m_crLink;
pAnchor->m_crHover = m_pMasterDocument->m_crLinkHover;
// Signifies to the parser that we are in a anchor tag, and this is it!
// This remains complicated by named tags. i.e. what should happen
// in this case:
// <a href="someTarget">Some text<a name="here">More Text</a> Other Text</a>
// is the Other Text in a link?
// In IE4, the second tag cancels out the first. We will use that behavior.
if (pAnchor->m_strLinkTarget.GetLength())
m_pLastAnchor = pAnchor;
else
m_pLastAnchor = NULL;
CHTMLParagraph *pPara = m_pDocument->CurrentParagraph();
pPara->AddItem( pAnchor );
}
void CHTMLParse::OnGotEndAnchor()
{
// Signify to the parser that we are no longer in an anchor tag.
// If we are truly anding a link, make a new text object.
// Otherwise, it's ignored.
if (m_pLastAnchor)
CreateNewTextObject();
m_pLastAnchor = NULL;
}
void CHTMLParse::OnGotTable( const CParameters &pList )
{
CreateNewTextObject();
int nWidth = 0; // Default width - unspec, -100; // For 100 percent
int nBorder = 0;
int nCellPadding = m_pDefaults->m_nCellPadding;
int nCellSpacing = m_pDefaults->m_nCellSpacing;
bool bTransparent = true;
COLORREF crBorder = RGB(0, 0, 0);
COLORREF crBgColor = RGB( 255, 255, 255 ); // Not really used.
COLORREF crBorderLight = m_pDefaults->m_crBorderLight;
COLORREF crBorderDark = m_pDefaults->m_crBorderDark;
bool bBorderDarkSet = false;
bool bBorderLightSet = false;
CHTMLParse::Align alg = knDefaultTableAlignment;
CHTMLParse::Align valg = knDefaultTableAlignment;
const UINT uParamSize = pList.GetSize();
for( UINT n = 0; n < uParamSize; n++ )
{
const CStaticString &strParam = pList[n].m_strValue;
switch( pList[n].m_param )
{
case pWidth:
nWidth = GetNumberParameterPercent( strParam, nWidth );
break;
case pAlign:
alg = GetAlignmentFromString( strParam, alg );
break;
case pVAlign:
valg = GetAlignmentFromString( strParam, valg );
break;
case pBorder:
nBorder = GetNumberParameter( strParam, nBorder );
break;
case pBColor:
crBgColor = GetColourFromString( strParam, crBgColor );
bTransparent = false;
break;
case pCellSpacing:
nCellSpacing = GetNumberParameter( strParam, nCellSpacing );
break;
case pCellPadding:
nCellPadding = GetNumberParameter( strParam, nCellPadding );
break;
case pBorderColor:
crBorder = GetColourFromString( strParam, crBorder );
if ( !bBorderDarkSet )
crBorderDark = crBorder;
if ( !bBorderLightSet )
crBorderLight = crBorder;
break;
case pBorderColorLight:
crBorderLight = GetColourFromString( strParam, crBorderLight );
bBorderLightSet = true;
break;
case pBorderColorDark:
crBorderDark = GetColourFromString( strParam, crBorderDark );
bBorderDarkSet = true;
break;
}
}
switch( alg )
{
case algLeft: case algRight:
break;
default:
CreateNewParagraph( 0, 0, m_pProp->nAlignment );
}
CHTMLTable *ptab = new CHTMLTable( nWidth, nBorder, alg, valg, nCellSpacing, nCellPadding, bTransparent, crBgColor, crBorderDark, crBorderLight);
m_stkTable.Push( ptab );
m_pDocument->CurrentParagraph()->AddItem( m_stkTable.Top() );
// Push onto the inTableCell stack
m_stkInTableCell.Push(false); // Not in a cell yet!
}
void CHTMLParse::OnGotTableRow( const CParameters &pList )
{
// Close an open cell first.
if (m_stkInTableCell.GetSize() && m_stkInTableCell.Top())
OnGotEndTableCell();
CHTMLParse::Align valg = knDefaultTableAlignment;
if( m_stkTable.GetSize() )
{
valg = m_stkTable.Top()->m_valg;
}
const UINT uParamSize = pList.GetSize();
for( UINT n = 0; n < uParamSize; n++ )
{
const CStaticString &strParam = pList[n].m_strValue;
switch( pList[n].m_param )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -