Split and simplify wxHtmlDCRenderer::Render()

This function was difficult to understand as it did two quite different
things depending on the value of its "dont_render" argument and using it
also made CountPages() logic more complicated than necessary.

Simplify the code by splitting Render() into FindNextPageBreak(), which
is used by CountPages(), and Render() itself, which doesn't need to deal
with pagination at all as it's either already provided the page start
and end positions or can just assume that everything fits on a single
page (the latter is the case for the headers and footers renderer).

This is a notionally backwards-incompatible change, but no code using
wxHtmlDCRenderer could be found in the wild and the new API is so much
simpler that it seems to be worth switching to it.
This commit is contained in:
Vadim Zeitlin
2018-05-24 00:13:35 +02:00
parent 0ada873c45
commit e01892c669
4 changed files with 48 additions and 78 deletions

View File

@@ -66,6 +66,9 @@ Changes in behaviour which may result in build errors
- wx/treebook.h doesn't include wx/treectrl.h (and, via it, wx/textctrl.h) any
more, include these headers explicitly from your code if necessary.
- wxHtmlDCRenderer::Render() arguments have changed, simply omit the ones not
existing in the function signature any more to update the code using it.
3.1.2: (released 2018-??-??)
----------------------------

View File

@@ -62,30 +62,19 @@ public:
const wxString& normal_face = wxEmptyString,
const wxString& fixed_face = wxEmptyString);
// Finds the next page break after the specified (vertical) position.
// Returns wxNOT_FOUND if passed in position is the last page break.
int FindNextPageBreak(const wxArrayInt& known_pagebreaks, int pos);
// [x,y] is position of upper-left corner of printing rectangle (see SetSize)
// from is y-coordinate of the very first visible cell
// to is y-coordinate of the next following page break, if any
// Returned value is y coordinate of first cell than didn't fit onto page.
// Use this value as 'from' in next call to Render in order to print multiple pages
// document
// If dont_render is TRUE then nothing is rendered into DC and it only counts
// pixels and return y coord of the next page
//
// known_pagebreaks and number_of_pages are used only when counting pages;
// otherwise, their default values should be used. Their purpose is to
// support pagebreaks using a subset of CSS2's <DIV>. The <DIV> handler
// needs to know what pagebreaks have already been set so that it doesn't
// set the same pagebreak twice.
//
// CAUTION! Render() changes DC's user scale and does NOT restore it!
int Render(int x, int y, const wxArrayInt& known_pagebreaks, int from = 0,
int dont_render = false, int to = INT_MAX);
void Render(int x, int y, int from = 0, int to = INT_MAX);
// returns total width of the html document
int GetTotalWidth() const;
// returns total height of the html document
// (compare Render's return value with this)
int GetTotalHeight() const;
private:

View File

@@ -38,10 +38,9 @@ public:
/**
Returns the height of the HTML text in pixels.
This is important if area height (see wxHtmlDCRenderer::SetSize) is
smaller that total height and thus the page cannot fit into it. In that
case you're supposed to call Render() as long as its return value is
smaller than GetTotalHeight()'s.
If the height of the area used with this renderer (see
wxHtmlDCRenderer::SetSize) is smaller that total height, the renderer
will produce more than one page of output.
@see GetTotalWidth()
*/
@@ -52,19 +51,11 @@ public:
@param x,y
position of upper-left corner of printing rectangle (see SetSize()).
@param known_pagebreaks
@todo docme
@param from
y-coordinate of the very first visible cell.
@param dont_render
if @true then this method only returns y coordinate of the next page
and does not output anything.
@param to
y-coordinate of the last visible cell.
Returned value is y coordinate of first cell than didn't fit onto page.
Use this value as from in next call to Render() in order to print
multipages document.
y-coordinate of the last visible cell or @c INT_MAX to use the full
page height.
@note
The following three methods @b must always be called before any call to
@@ -72,11 +63,8 @@ public:
- SetDC()
- SetSize()
- SetHtmlText()
@note Render() changes the DC's user scale and does NOT restore it.
*/
int Render(int x, int y, wxArrayInt& known_pagebreaks, int from = 0,
int dont_render = false, int to = INT_MAX);
void Render(int x, int y, int from = 0, int to = INT_MAX);
/**
Assign DC instance to the renderer.

View File

@@ -140,23 +140,28 @@ void wxHtmlDCRenderer::SetStandardFonts(int size,
// else: SetHtmlText() not yet called, no need for relayout
}
int wxHtmlDCRenderer::Render(int x, int y,
const wxArrayInt& known_pagebreaks,
int from, int dont_render, int to)
int wxHtmlDCRenderer::FindNextPageBreak(const wxArrayInt& known_pagebreaks,
int pos)
{
wxCHECK_MSG( m_Cells, 0, "SetHtmlText() must be called before Render()" );
wxCHECK_MSG( m_DC, 0, "SetDC() must be called before Render()" );
// Stop looking for page breaks if the previous one was already at the end
// of the last page.
//
// For an empty HTML document total height is 0, but we still must have at
// least a single page in it, so handle the case of pos == 0 specially.
if ( pos != 0 && pos >= GetTotalHeight() )
return wxNOT_FOUND;
int pbreak, hght;
pos += m_Height;
while (m_Cells->AdjustPagebreak(&pos, known_pagebreaks, m_Height)) {}
return pos;
}
pbreak = (int)(from + m_Height);
while (m_Cells->AdjustPagebreak(&pbreak, known_pagebreaks, m_Height)) {}
hght = pbreak - from;
if(to < hght)
hght = to;
void wxHtmlDCRenderer::Render(int x, int y, int from, int to)
{
wxCHECK_RET( m_DC, "SetDC() must be called before Render()" );
const int hght = to == INT_MAX ? m_Height : to - from;
if (!dont_render)
{
wxHtmlRenderingInfo rinfo;
wxDefaultHtmlRenderingStyle rstyle;
rinfo.SetStyle(&rstyle);
@@ -166,10 +171,6 @@ int wxHtmlDCRenderer::Render(int x, int y,
x, (y - from),
y, y + hght,
rinfo);
}
const int totalHeight = GetTotalHeight();
return pbreak < totalHeight ? pbreak : totalHeight;
}
int wxHtmlDCRenderer::GetTotalWidth() const
@@ -470,27 +471,16 @@ void wxHtmlPrintout::SetFooter(const wxString& footer, int pg)
void wxHtmlPrintout::CountPages()
{
wxBusyCursor wait;
int pageWidth, pageHeight, mm_w, mm_h;
float ppmm_h, ppmm_v;
GetPageSizePixels(&pageWidth, &pageHeight);
GetPageSizeMM(&mm_w, &mm_h);
ppmm_h = (float)pageWidth / mm_w;
ppmm_v = (float)pageHeight / mm_h;
int pos = 0;
m_NumPages = 0;
m_PageBreaks.Clear();
m_PageBreaks.Add( 0);
do
for ( int pos = 0; pos != wxNOT_FOUND; )
{
pos = m_Renderer.Render((int)( ppmm_h * m_MarginLeft),
(int) (ppmm_v * (m_MarginTop + (m_HeaderHeight == 0 ? 0 : m_MarginSpace)) + m_HeaderHeight),
m_PageBreaks,
pos, true, INT_MAX);
m_PageBreaks.Add( pos);
} while (pos < m_Renderer.GetTotalHeight());
pos = m_Renderer.FindNextPageBreak(m_PageBreaks, pos);
}
}
@@ -525,8 +515,8 @@ void wxHtmlPrintout::RenderPage(wxDC *dc, int page)
dc->SetBackgroundMode(wxTRANSPARENT);
m_Renderer.Render((int) (ppmm_h * m_MarginLeft),
(int) (ppmm_v * (m_MarginTop + (m_HeaderHeight == 0 ? 0 : m_MarginSpace)) + m_HeaderHeight), m_PageBreaks,
m_PageBreaks[page-1], false, m_PageBreaks[page]-m_PageBreaks[page-1]);
(int) (ppmm_v * (m_MarginTop + (m_HeaderHeight == 0 ? 0 : m_MarginSpace)) + m_HeaderHeight),
m_PageBreaks[page-1], m_PageBreaks[page]);
m_RendererHdr.SetDC(dc,
@@ -535,12 +525,12 @@ void wxHtmlPrintout::RenderPage(wxDC *dc, int page)
if (!m_Headers[page % 2].empty())
{
m_RendererHdr.SetHtmlText(TranslateHeader(m_Headers[page % 2], page));
m_RendererHdr.Render((int) (ppmm_h * m_MarginLeft), (int) (ppmm_v * m_MarginTop), m_PageBreaks);
m_RendererHdr.Render((int) (ppmm_h * m_MarginLeft), (int) (ppmm_v * m_MarginTop));
}
if (!m_Footers[page % 2].empty())
{
m_RendererHdr.SetHtmlText(TranslateHeader(m_Footers[page % 2], page));
m_RendererHdr.Render((int) (ppmm_h * m_MarginLeft), (int) (pageHeight - ppmm_v * m_MarginBottom - m_FooterHeight), m_PageBreaks);
m_RendererHdr.Render((int) (ppmm_h * m_MarginLeft), (int) (pageHeight - ppmm_v * m_MarginBottom - m_FooterHeight));
}
}