Add <span> tag and limited support for CSS styles to wxHTML.

Parse "style" attributes of the HTML tags and honour those of them that can be
mapped to the HTML 3 attributes.

Also add a handler for <span> tag.

Closes #10631.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@64443 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2010-05-30 23:12:25 +00:00
parent c9554803fa
commit f68e16c5fc
26 changed files with 590 additions and 3 deletions

View File

@@ -23,6 +23,8 @@
#endif
#include "wx/html/htmlpars.h"
#include "wx/html/styleparams.h"
#include "wx/vector.h"
#include <stdio.h> // for vsscanf
@@ -430,6 +432,32 @@ wxHtmlTag::wxHtmlTag(wxHtmlTag *parent,
#if WXWIN_COMPATIBILITY_2_8
m_sourceStart = source->begin();
#endif
// Try to parse any style parameters that can be handled simply by
// converting them to the equivalent HTML 3 attributes: this is a far cry
// from perfect but better than nothing.
static const struct EquivAttr
{
const char *style;
const char *attr;
} equivAttrs[] =
{
{ "text-align", "ALIGN" },
{ "width", "WIDTH" },
{ "vertical-align", "VALIGN" },
{ "background", "BGCOLOR" },
};
wxHtmlStyleParams styleParams(*this);
for ( unsigned n = 0; n < WXSIZEOF(equivAttrs); n++ )
{
const EquivAttr& ea = equivAttrs[n];
if ( styleParams.HasParam(ea.style) && !HasParam(ea.attr) )
{
m_ParamNames.Add(ea.attr);
m_ParamValues.Add(styleParams.GetParam(ea.style));
}
}
}
wxHtmlTag::~wxHtmlTag()
@@ -481,12 +509,11 @@ int wxHtmlTag::ScanParam(const wxString& par,
return wxSscanf(parval, format, param);
}
bool wxHtmlTag::GetParamAsColour(const wxString& par, wxColour *clr) const
/* static */
bool wxHtmlTag::ParseAsColour(const wxString& str, wxColour *clr)
{
wxCHECK_MSG( clr, false, wxT("invalid colour argument") );
wxString str = GetParam(par);
// handle colours defined in HTML 4.0 first:
if (str.length() > 1 && str[0] != wxT('#'))
{
@@ -522,6 +549,12 @@ bool wxHtmlTag::GetParamAsColour(const wxString& par, wxColour *clr) const
return false;
}
bool wxHtmlTag::GetParamAsColour(const wxString& par, wxColour *clr) const
{
const wxString str = GetParam(par);
return !str.empty() && ParseAsColour(str, clr);
}
bool wxHtmlTag::GetParamAsInt(const wxString& par, int *clr) const
{
if ( !HasParam(par) )

125
src/html/m_span.cpp Normal file
View File

@@ -0,0 +1,125 @@
/////////////////////////////////////////////////////////////////////////////
// Name: src/html/m_span.cpp
// Purpose: wxHtml module for span handling
// Author: Nigel Paton
// RCS-ID: $Id$
// Copyright: wxWidgets team
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#if wxUSE_HTML
#include "wx/html/forcelnk.h"
#include "wx/html/m_templ.h"
#include "wx/fontenum.h"
#include "wx/tokenzr.h"
#include "wx/html/styleparams.h"
FORCE_LINK_ME(m_span)
TAG_HANDLER_BEGIN(SPAN, "SPAN" )
TAG_HANDLER_VARS
wxArrayString m_Faces;
TAG_HANDLER_CONSTR(SPAN) { }
TAG_HANDLER_PROC(tag)
{
wxColour oldclr = m_WParser->GetActualColor();
int oldsize = m_WParser->GetFontSize();
int oldbold = m_WParser->GetFontBold();
// Load any style parameters
wxHtmlStyleParams styleParams(tag);
wxString str;
str = styleParams.GetParam(wxS("color"));
if ( !str.empty() )
{
wxColour clr;
if ( wxHtmlTag::ParseAsColour(str, &clr) )
{
m_WParser->SetActualColor(clr);
m_WParser->GetContainer()->InsertCell(new wxHtmlColourCell(clr));
}
}
str = styleParams.GetParam(wxS("font-size"));
if ( !str.empty() )
{
// Point size
int foundIndex = str.Find(wxS("pt"));
if (foundIndex != wxNOT_FOUND)
{
str.Truncate(foundIndex);
long sizeValue;
if (str.ToLong(&sizeValue) == true)
{
// Set point size
m_WParser->SetFontPointSize(sizeValue);
m_WParser->GetContainer()->InsertCell(
new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
}
}
// else: check for other ways of specifying size (TODO)
}
str = styleParams.GetParam(wxS("font-weight"));
if ( !str.empty() )
{
// Only bold and normal supported just now
if ( str == wxS("bold") )
{
m_WParser->SetFontBold(true);
m_WParser->GetContainer()->InsertCell(
new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
}
else if ( str == wxS("normal") )
{
m_WParser->SetFontBold(false);
m_WParser->GetContainer()->InsertCell(
new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
}
}
ParseInner(tag);
if (oldbold != m_WParser->GetFontBold())
{
m_WParser->SetFontBold(oldbold);
m_WParser->GetContainer()->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
}
if (oldsize != m_WParser->GetFontSize())
{
m_WParser->SetFontSize(oldsize);
m_WParser->GetContainer()->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
}
if (oldclr != m_WParser->GetActualColor())
{
m_WParser->SetActualColor(oldclr);
m_WParser->GetContainer()->InsertCell(new wxHtmlColourCell(oldclr));
}
return true;
}
TAG_HANDLER_END(SPAN)
TAGS_MODULE_BEGIN(Spans)
TAGS_MODULE_ADD(SPAN)
TAGS_MODULE_END(Spans)
#endif // wxUSE_HTML

78
src/html/styleparams.cpp Normal file
View File

@@ -0,0 +1,78 @@
/////////////////////////////////////////////////////////////////////////////
// Name: src/html/styleparams.cpp
// Purpose: wxHtml helper code for extracting style parameters
// Author: Nigel Paton
// RCS-ID: $Id$
// Copyright: wxWidgets team
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#include "wx/wxprec.h"
#include "wx/tokenzr.h"
#include "wx/html/htmltag.h"
#include "wx/html/styleparams.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#if wxUSE_HTML
wxHtmlStyleParams::wxHtmlStyleParams(const wxHtmlTag& tag)
{
wxString wd = tag.GetParam(wxT("STYLE"));
// Make sure no whitespace
wd.Trim(true).Trim(false);
if ( wd.empty() )
return;
// Check for bracketed entries
// Only support element properties and not pseudo-element or pseudo-classes
if (wd.Find('{') == 0)
{
// Extract string up to end bracket
int endBracket = wd.Find('}');
if (endBracket != wxNOT_FOUND)
{
// Replace original string with bracketed options
wd = wd.SubString(1, endBracket - 1);
// Make sure no whitespace
wd.Trim(true).Trim(false);
}
else
{
// Syntax problem change to blank string
wd = "";
}
}
// Should now have a semi-colon delimited list of options
// Each option is a name and a value separated by a colon
// Split the list into names and values
wxStringTokenizer tkz(wd, wxT(";"), wxTOKEN_STRTOK);
while ( tkz.HasMoreTokens() )
{
wxString token = tkz.GetNextToken();
// Split into name and value
int colonIndex = token.Find(':');
if ((colonIndex != wxNOT_FOUND) && // Not a name value pair
(colonIndex != 0)) // No name
{
wxString tempString;
// Extract and trim name
tempString = token.SubString(0, colonIndex - 1);
tempString.Trim(true).Trim(false);
// Add to name list
m_names.Add(tempString);
// Extract and trim values
tempString = token.SubString(colonIndex + 1, token.Length() - 1);
tempString.Trim(true).Trim(false);
// Add to values list
m_values.Add(tempString);
}
}
}
#endif // wxUSE_HTML

View File

@@ -559,6 +559,31 @@ void wxHtmlWinParser::SetDC(wxDC *dc, double pixel_scale, double font_scale)
m_FontScale = font_scale;
}
void wxHtmlWinParser::SetFontPointSize(int pt)
{
if (pt <= m_FontsSizes[0])
m_FontSize = 1;
else if (pt >= m_FontsSizes[6])
m_FontSize = 7;
else
{
// Find the font closest to the given value with a simple linear search
// (binary search is not worth it here for so small number of elements)
for ( int n = 0; n < 6; n++ )
{
if ( (pt > m_FontsSizes[n]) && (pt <= m_FontsSizes[n + 1]) )
{
// In this range, find out which entry it is closest to
if ( (pt - m_FontsSizes[n]) < (m_FontsSizes[n + 1] - pt) )
m_FontSize = n;
else
m_FontSize = n + 1;
break;
}
}
}
}
wxFont* wxHtmlWinParser::CreateCurrentFont()
{