X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ba804ab2d429c03bf5b20989d207d2b1ebe049f4..9cd722e33761671e3a03a30313ef6486a55d2730:/src/html/winpars.cpp diff --git a/src/html/winpars.cpp b/src/html/winpars.cpp index 5446f917d6..3a1aff2aab 100644 --- a/src/html/winpars.cpp +++ b/src/html/winpars.cpp @@ -15,7 +15,7 @@ #if wxUSE_HTML && wxUSE_STREAMS -#ifndef WXPRECOMP +#ifndef WX_PRECOMP #include "wx/intl.h" #include "wx/dc.h" #include "wx/log.h" @@ -47,6 +47,7 @@ wxHtmlWinParser::wxHtmlWinParser(wxHtmlWindowInterface *wndIface) m_CharHeight = m_CharWidth = 0; m_UseLink = false; #if !wxUSE_UNICODE + m_nbsp = 0; m_EncConv = NULL; m_InputEnc = wxFONTENCODING_ISO8859_1; m_OutputEnc = wxFONTENCODING_DEFAULT; @@ -116,7 +117,7 @@ static void wxBuildFontSizes(int *sizes, int size) // using a fixed factor (1.2, from CSS2) is a bad idea as explained at // http://www.w3.org/TR/CSS21/fonts.html#font-size-props but this is by far // simplest thing to do so still do it like this for now - sizes[0] = int(size * 0.69); + sizes[0] = int(size * 0.75); // exception to 1.2 rule, otherwise too small sizes[1] = int(size * 0.83); sizes[2] = size; sizes[3] = int(size * 1.2); @@ -125,6 +126,17 @@ static void wxBuildFontSizes(int *sizes, int size) sizes[6] = int(size * 2); } +static int wxGetDefaultHTMLFontSize() +{ + // base the default font size on the size of the default system font but + // also ensure that we have a font of reasonable size, otherwise small HTML + // fonts are unreadable + int size = wxNORMAL_FONT->GetPointSize(); + if ( size < 10 ) + size = 10; + return size; +} + void wxHtmlWinParser::SetFonts(const wxString& normal_face, const wxString& fixed_face, const int *sizes) @@ -133,7 +145,7 @@ void wxHtmlWinParser::SetFonts(const wxString& normal_face, if ( !sizes ) { if ( !default_sizes[0] ) - wxBuildFontSizes(default_sizes, wxNORMAL_FONT->GetPointSize()); + wxBuildFontSizes(default_sizes, wxGetDefaultHTMLFontSize()); sizes = default_sizes; } @@ -168,7 +180,7 @@ void wxHtmlWinParser::SetStandardFonts(int size, const wxString& fixed_face) { if (size == -1) - size = wxNORMAL_FONT->GetPointSize(); + size = wxGetDefaultHTMLFontSize(); int f_sizes[7]; wxBuildFontSizes(f_sizes, size); @@ -188,10 +200,13 @@ void wxHtmlWinParser::InitParser(const wxString& source) m_FontBold = m_FontItalic = m_FontUnderlined = m_FontFixed = FALSE; m_FontSize = 3; //default one CreateCurrentFont(); // we're selecting default font into - m_DC->GetTextExtent( wxT("H"), &m_CharWidth, &m_CharHeight); - /* NOTE : we're not using GetCharWidth/Height() because - of differences under X and win - */ + + // we're not using GetCharWidth/Height() because of + // differences under X and win + wxCoord w,h; + m_DC->GetTextExtent( wxT("H"), &w, &h); + m_CharWidth = w; + m_CharHeight = h; m_UseLink = false; m_Link = wxHtmlLinkInfo( wxEmptyString ); @@ -321,18 +336,19 @@ wxFSFile *wxHtmlWinParser::OpenURL(wxHtmlURLType type, if ( status == wxHTML_BLOCK ) return NULL; - return GetFS()->OpenFile(myurl); + int flags = wxFS_READ; + if (type == wxHTML_URL_IMAGE) + flags |= wxFS_SEEKABLE; + + return GetFS()->OpenFile(myurl, flags); } -void wxHtmlWinParser::AddText(const wxChar* txt) +void wxHtmlWinParser::AddText(const wxString& txt) { - size_t i = 0, - x, - lng = wxStrlen(txt); register wxChar d; int templen = 0; - wxChar nbsp = GetEntitiesParser()->GetCharForCode(160 /* nbsp */); + size_t lng = txt.length(); if (lng+1 > m_tmpStrBufSize) { delete[] m_tmpStrBuf; @@ -341,42 +357,63 @@ void wxHtmlWinParser::AddText(const wxChar* txt) } wxChar *temp = m_tmpStrBuf; + wxString::const_iterator i = txt.begin(); + wxString::const_iterator end = txt.end(); + if (m_tmpLastWasSpace) { - while ((i < lng) && - ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) || (txt[i] == wxT(' ')) || - (txt[i] == wxT('\t')))) i++; + while ( (i < end) && + (*i == wxT('\n') || *i == wxT('\r') || *i == wxT(' ') || + *i == wxT('\t')) ) + { + ++i; + } } - while (i < lng) + while (i < end) { - x = 0; - d = temp[templen++] = txt[i]; + size_t x = 0; + d = temp[templen++] = *i; if ((d == wxT('\n')) || (d == wxT('\r')) || (d == wxT(' ')) || (d == wxT('\t'))) { - i++, x++; - while ((i < lng) && ((txt[i] == wxT('\n')) || (txt[i] == wxT('\r')) || - (txt[i] == wxT(' ')) || (txt[i] == wxT('\t')))) i++, x++; + ++i, ++x; + while ( (i < end) && + (*i == wxT('\n') || *i == wxT('\r') || + *i == wxT(' ') || *i == wxT('\t')) ) + { + ++i; + ++x; + } } - else i++; + else + ++i; if (x) { temp[templen-1] = wxT(' '); - DoAddText(temp, templen, nbsp); + DoAddText(temp, templen); m_tmpLastWasSpace = true; } } if (templen && (templen > 1 || temp[0] != wxT(' '))) { - DoAddText(temp, templen, nbsp); + DoAddText(temp, templen); m_tmpLastWasSpace = false; } } -void wxHtmlWinParser::DoAddText(wxChar *temp, int& templen, wxChar nbsp) +void wxHtmlWinParser::DoAddText(wxChar *temp, int& templen) { + #define NBSP_UNICODE_VALUE 160 +#if !wxUSE_UNICODE + if ( m_nbsp == 0 ) + m_nbsp = GetEntitiesParser()->GetCharForCode(NBSP_UNICODE_VALUE); + #define CUR_NBSP_VALUE m_nbsp +#else + #define CUR_NBSP_VALUE NBSP_UNICODE_VALUE +#endif + temp[templen] = 0; templen = 0; #if !wxUSE_UNICODE @@ -386,17 +423,17 @@ void wxHtmlWinParser::DoAddText(wxChar *temp, int& templen, wxChar nbsp) size_t len = wxStrlen(temp); for (size_t j = 0; j < len; j++) { - if (temp[j] == nbsp) + if (temp[j] == CUR_NBSP_VALUE) temp[j] = wxT(' '); } - wxHtmlCell *c = new wxHtmlWordCell(temp, *(GetDC())); + wxHtmlWordCell *c = new wxHtmlWordCell(temp, *(GetDC())); ApplyStateToCell(c); m_Container->InsertCell(c); - ((wxHtmlWordCell*)c)->SetPreviousWord(m_lastWordCell); - m_lastWordCell = (wxHtmlWordCell*)c; + c->SetPreviousWord(m_lastWordCell); + m_lastWordCell = c; } @@ -430,8 +467,10 @@ wxHtmlContainerCell* wxHtmlWinParser::CloseContainer() void wxHtmlWinParser::SetFontSize(int s) { - if (s < 1) s = 1; - else if (s > 7) s = 7; + if (s < 1) + s = 1; + else if (s > 7) + s = 7; m_FontSize = s; } @@ -492,8 +531,10 @@ void wxHtmlWinParser::SetLink(const wxHtmlLinkInfo& link) void wxHtmlWinParser::SetFontFace(const wxString& face) { - if (GetFontFixed()) m_FontFaceFixed = face; - else m_FontFaceNormal = face; + if (GetFontFixed()) + m_FontFaceFixed = face; + else + m_FontFaceNormal = face; #if !wxUSE_UNICODE if (m_InputEnc != wxFONTENCODING_DEFAULT) @@ -515,6 +556,9 @@ void wxHtmlWinParser::ApplyStateToCell(wxHtmlCell *cell) #if !wxUSE_UNICODE void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc) { + // the character used for non-breakable space may change: + m_nbsp = 0; + m_InputEnc = m_OutputEnc = wxFONTENCODING_DEFAULT; if (m_EncConv) { @@ -522,29 +566,37 @@ void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc) m_EncConv = NULL; } - if (enc == wxFONTENCODING_DEFAULT) return; + if (enc == wxFONTENCODING_DEFAULT) + return; wxFontEncoding altfix, altnorm; bool availfix, availnorm; - // exact match? availnorm = wxFontMapper::Get()->IsEncodingAvailable(enc, m_FontFaceNormal); availfix = wxFontMapper::Get()->IsEncodingAvailable(enc, m_FontFaceFixed); + if (availnorm && availfix) + { + // exact match? m_OutputEnc = enc; + } - // alternatives? else if (wxFontMapper::Get()->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, false) && wxFontMapper::Get()->GetAltForEncoding(enc, &altfix, m_FontFaceFixed, false) && altnorm == altfix) + { + // alternatives? m_OutputEnc = altnorm; - - // at least normal face? + } else if (availnorm) + { + // at least normal face? m_OutputEnc = enc; + } else if (wxFontMapper::Get()->GetAltForEncoding(enc, &altnorm, m_FontFaceNormal, false)) + { m_OutputEnc = altnorm; - + } else { #ifndef __WXMAC__ @@ -557,11 +609,16 @@ void wxHtmlWinParser::SetInputEncoding(wxFontEncoding enc) m_InputEnc = enc; if (m_OutputEnc == wxFONTENCODING_DEFAULT) + { GetEntitiesParser()->SetEncoding(wxFONTENCODING_SYSTEM); + } else + { GetEntitiesParser()->SetEncoding(m_OutputEnc); + } - if (m_InputEnc == m_OutputEnc) return; + if (m_InputEnc == m_OutputEnc) + return; m_EncConv = new wxEncodingConverter(); if (!m_EncConv->Init(m_InputEnc,