fix a leak, add some table features

This commit is contained in:
Alex Fabijanic 2020-01-15 22:06:45 +01:00 committed by Günter Obiltschnig
parent 854bf4d79f
commit b435db6e8e
7 changed files with 94 additions and 15 deletions

24
PDF/include/Poco/PDF/Cell.h Normal file → Executable file
View File

@ -35,7 +35,7 @@ public:
}; };
Cell(const AttributedString& content = "", const std::string& name = "", FontMapPtr pFontMap = 0); Cell(const AttributedString& content = "", const std::string& name = "", FontMapPtr pFontMap = 0);
Cell(const AttributedString& content, FontMapPtr pFontMap, const std::string& encoding = "UTF-8" , bool trueType = true); Cell(const AttributedString& content, FontMapPtr pFontMap, const std::string& encoding = "UTF-8" , bool trueType = true, int widthAsPct=-1);
~Cell(); ~Cell();
const std::string& getName() const; const std::string& getName() const;
@ -56,6 +56,9 @@ public:
void setFonts(FontMapPtr pFontMap); void setFonts(FontMapPtr pFontMap);
FontMapPtr getFonts() const { return _pFontMap; } FontMapPtr getFonts() const { return _pFontMap; }
void draw(Page& page, float x, float y, float width, float height); void draw(Page& page, float x, float y, float width, float height);
int getWidthAsPct() const;
void setWidthAsPct(int width);
bool hasWidth() const;
private: private:
AttributedString _content; AttributedString _content;
@ -65,6 +68,7 @@ private:
FontMapPtr _pFontMap; FontMapPtr _pFontMap;
std::string _encoding; std::string _encoding;
bool _trueType; bool _trueType;
int _widthAsPct;
}; };
@ -148,6 +152,24 @@ inline void Cell::setLineWidth(float width)
} }
inline int Cell::getWidthAsPct() const
{
return _widthAsPct;
}
inline void Cell::setWidthAsPct(int width)
{
_widthAsPct = width;
}
inline bool Cell::hasWidth() const
{
return _widthAsPct > 0;
}
} } // namespace Poco::PDF } } // namespace Poco::PDF

2
PDF/include/Poco/PDF/Font.h Normal file → Executable file
View File

@ -30,7 +30,7 @@ class PDF_API Font: public Resource<HPDF_Font>
/// Font class represents font resource. /// Font class represents font resource.
{ {
public: public:
Font(HPDF_Doc* pPDF, const HPDF_Font& resource); Font(HPDF_Doc* pPDF, HPDF_Font resource);
/// Creates the font. /// Creates the font.
~Font(); ~Font();

12
PDF/src/Cell.cpp Normal file → Executable file
View File

@ -15,19 +15,20 @@ Cell::Cell(const AttributedString& content, const std::string& name, FontMapPtr
_outline(OUTLINE_NONE), _outline(OUTLINE_NONE),
_lineWidth(1.0f), _lineWidth(1.0f),
_encoding("UTF-8"), _encoding("UTF-8"),
_trueType(true) _trueType(true),
_widthAsPct(-1)
{ {
setFonts(pFontMap); setFonts(pFontMap);
} }
Cell::Cell(const AttributedString& content, FontMapPtr pFontMap, const std::string& encoding, bool trueType): Cell::Cell(const AttributedString& content, FontMapPtr pFontMap, const std::string& encoding, bool trueType, int widthAsPct):
_content(content), _content(content),
_outline(OUTLINE_NONE), _outline(OUTLINE_NONE),
_lineWidth(1.0f), _lineWidth(1.0f),
_encoding(encoding), _encoding(encoding),
_trueType(trueType) _trueType(trueType),
_widthAsPct(widthAsPct)
{ {
setFonts(pFontMap); setFonts(pFontMap);
} }
@ -78,8 +79,11 @@ void Cell::draw(Page& page, float x, float y, float width, float height)
page.setLineWidth(_lineWidth); page.setLineWidth(_lineWidth);
page.moveTo(x, y); page.moveTo(x, y);
if (_outline & OUTLINE_LEFT ) page.lineTo(x, y+height); if (_outline & OUTLINE_LEFT ) page.lineTo(x, y+height);
page.moveTo(x, y+height);
if (_outline & OUTLINE_TOP ) page.lineTo(x+width, y+height); if (_outline & OUTLINE_TOP ) page.lineTo(x+width, y+height);
page.moveTo(x+width, y+height);
if (_outline & OUTLINE_RIGHT ) page.lineTo(x+width, y ); if (_outline & OUTLINE_RIGHT ) page.lineTo(x+width, y );
page.moveTo(x+width, y);
if (_outline & OUTLINE_BOTTOM) page.lineTo(x, y ); if (_outline & OUTLINE_BOTTOM) page.lineTo(x, y );
page.stroke(); page.stroke();
} }

2
PDF/src/Font.cpp Normal file → Executable file
View File

@ -19,7 +19,7 @@ namespace Poco {
namespace PDF { namespace PDF {
Font::Font(HPDF_Doc* pPDF, const HPDF_Font& font): Font::Font(HPDF_Doc* pPDF, HPDF_Font font):
Resource<HPDF_Font>(pPDF, font, HPDF_Font_GetFontName(font)) Resource<HPDF_Font>(pPDF, font, HPDF_Font_GetFontName(font))
{ {
} }

8
PDF/src/Page.cpp Normal file → Executable file
View File

@ -49,6 +49,7 @@ Page::Page(const Page& other):
Page::~Page() Page::~Page()
{ {
delete _pCurrentFont;
} }
@ -120,8 +121,11 @@ void Page::setTTFont(const std::string& name, float size, const std::string& enc
const Font& Page::getFont() const const Font& Page::getFont() const
{ {
delete _pCurrentFont; delete _pCurrentFont; _pCurrentFont = 0;
return *(_pCurrentFont = new Font(&_pDocument->handle(), HPDF_Page_GetCurrentFont(_page))); HPDF_Font pCurFont = HPDF_Page_GetCurrentFont(_page);
if (!pCurFont) throw Poco::NullPointerException("PDF::Page has no font set.");
_pCurrentFont = new Font(&_pDocument->handle(), pCurFont);
return *_pCurrentFont;
} }

16
PDF/src/Table.cpp Normal file → Executable file
View File

@ -64,19 +64,31 @@ void Table::draw(Page& page, float x, float y, float width, float height)
{ {
if (_cells.size()) if (_cells.size())
{ {
int rows = _cells.size(); int rows = static_cast<int>(_cells.size());
int cols = _cells[0].size(); int cols = static_cast<int>(_cells[0].size());
int r = 0; int r = 0;
for (Cells::iterator it = _cells.begin(); it != _cells.end(); ++it) for (Cells::iterator it = _cells.begin(); it != _cells.end(); ++it)
{ {
TableRow& row(*it); TableRow& row(*it);
float h = height / rows; float h = height / rows;
int c = 0; int c = 0;
float lastX = x;
for (TableRow::iterator itr = row.begin(); itr != row.end(); ++itr) for (TableRow::iterator itr = row.begin(); itr != row.end(); ++itr)
{ {
Cell& cell(*itr); Cell& cell(*itr);
float w = width / cols; float w = width / cols;
if (!cell.hasWidth())
{
cell.draw(page, x + (w * c), y - (h * r), w, h); cell.draw(page, x + (w * c), y - (h * r), w, h);
lastX += (w * c);
}
else
{
w = width * cell.getWidthAsPct() / 100.0f;
cell.draw(page, lastX, y - (h * r), w, h);
lastX += w;
}
++c; ++c;
} }
++r; ++r;

41
PDF/src/XMLTemplate.cpp Normal file → Executable file
View File

@ -279,6 +279,16 @@ public:
_pPage->setLineWidth(0.2f); _pPage->setLineWidth(0.2f);
RGBColor black = {0, 0, 0}; RGBColor black = {0, 0, 0};
_pPage->setRGBStroke(black); _pPage->setRGBStroke(black);
// read or force font, page must have default
std::string fontFamily = _styles.getString("font-family", "helvetica");
float fontSize = _styles.getFloat("font-size", 10.0);
std::string fontStyle = _styles.getString("font-style", "normal");
std::string fontWeight = _styles.getString("font-weight", "normal");
Font font = loadFont(fontFamily, fontStyle, fontWeight);
_pPage->setFont(font, fontSize);
_boxes.push_back(Box(0, 0, _pPage->getWidth(), _pPage->getHeight())); _boxes.push_back(Box(0, 0, _pPage->getWidth(), _pPage->getHeight()));
float margin = _styles.getFloat("margin", 0); float margin = _styles.getFloat("margin", 0);
@ -323,7 +333,6 @@ public:
_text = transcode(_text); _text = transcode(_text);
Font font = loadFont(fontFamily, fontStyle, fontWeight); Font font = loadFont(fontFamily, fontStyle, fontWeight);
_pPage->setFont(font, fontSize); _pPage->setFont(font, fontSize);
float width = static_cast<float>(font.textWidth(_text).width*fontSize / 1000); float width = static_cast<float>(font.textWidth(_text).width*fontSize / 1000);
@ -448,12 +457,20 @@ public:
AttributedString::Alignment align = AttributedString::ALIGN_LEFT; AttributedString::Alignment align = AttributedString::ALIGN_LEFT;
int style = AttributedString::STYLE_PLAIN; int style = AttributedString::STYLE_PLAIN;
std::string fontFamily = _styles.getString("font-family"); std::string fontFamily = _styles.getString("font-family");
float fontSize = _styles.getFloat("font-size"); float fontSize = _styles.getFloat("font-size");
std::string textAlign = _styles.getString("text-align", "left"); std::string textAlign = _styles.getString("text-align", "left");
std::string fontStyle = _styles.getString("font-style", "normal"); std::string fontStyle = _styles.getString("font-style", "normal");
std::string fontWeight = _styles.getString("font-weight", "normal"); std::string fontWeight = _styles.getString("font-weight", "normal");
std::string textTransform = _styles.getString("text-transform", "none"); std::string textTransform = _styles.getString("text-transform", "none");
std::string widthPct = _styles.getString("width", "");
// solid only supported at this time
bool borderAll = _styles.getString("border-style", "") == "solid";
bool borderLeft = _styles.getString("border-left", "") == "solid";
bool borderTop = _styles.getString("border-top", "") == "solid";
bool borderRight = _styles.getString("border-right", "") == "solid";
bool borderBottom = _styles.getString("border-bottom", "") == "solid";
_text = transform(_text, textTransform); _text = transform(_text, textTransform);
_text = transcode(_text); _text = transcode(_text);
@ -462,6 +479,8 @@ public:
align = AttributedString::ALIGN_RIGHT; align = AttributedString::ALIGN_RIGHT;
else if (textAlign == "left") else if (textAlign == "left")
align = AttributedString::ALIGN_LEFT; align = AttributedString::ALIGN_LEFT;
else if (textAlign == "center")
align = AttributedString::ALIGN_CENTER;
if (fontStyle == "italic" || fontStyle == "oblique") if (fontStyle == "italic" || fontStyle == "oblique")
style |= AttributedString::STYLE_ITALIC; style |= AttributedString::STYLE_ITALIC;
@ -479,7 +498,25 @@ public:
(*pFontMap)[AttributedString::STYLE_ITALIC] = italicFontName(normalizedFontFamily); (*pFontMap)[AttributedString::STYLE_ITALIC] = italicFontName(normalizedFontFamily);
(*pFontMap)[AttributedString::STYLE_BOLD | AttributedString::STYLE_ITALIC] = boldItalicFontName(normalizedFontFamily); (*pFontMap)[AttributedString::STYLE_BOLD | AttributedString::STYLE_ITALIC] = boldItalicFontName(normalizedFontFamily);
_row.push_back(Cell(content, pFontMap, _encoding, false)); int width = -1;
if (!widthPct.empty())
{
if (*widthPct.rbegin() != '%')
throw Poco::InvalidArgumentException("Only percentage widths supported for table cells.");
else
{
widthPct.erase(widthPct.length() - 1);
width = NumberParser::parse(widthPct);
}
}
Cell cell(content, pFontMap, _encoding, false, width);
if (borderAll) cell.borderAll(true);
if (borderLeft) cell.borderLeft(true);
if (borderTop) cell.borderTop(true);
if (borderRight) cell.borderRight(true);
if (borderBottom) cell.borderBottom(true);
_row.push_back(cell);
popStyle(); popStyle();
} }