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

14
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),
_lineWidth(1.0f),
_encoding("UTF-8"),
_trueType(true)
_trueType(true),
_widthAsPct(-1)
{
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),
_outline(OUTLINE_NONE),
_lineWidth(1.0f),
_encoding(encoding),
_trueType(trueType)
_trueType(trueType),
_widthAsPct(widthAsPct)
{
setFonts(pFontMap);
}
@@ -71,15 +72,18 @@ void Cell::borderAll(bool show)
void Cell::draw(Page& page, float x, float y, float width, float height)
{
// uncomment to force showing of the cell outline regardless of settings
// _outline = 15;
//_outline = 15;
if (_outline != OUTLINE_NONE)
{
page.setLineWidth(_lineWidth);
page.moveTo(x, y);
if (_outline & OUTLINE_LEFT ) page.lineTo(x, y+height);
page.moveTo(x, 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 );
page.moveTo(x+width, y);
if (_outline & OUTLINE_BOTTOM) page.lineTo(x, y );
page.stroke();
}

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

@@ -19,7 +19,7 @@ namespace Poco {
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))
{
}

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

@@ -49,6 +49,7 @@ Page::Page(const Page& other):
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
{
delete _pCurrentFont;
return *(_pCurrentFont = new Font(&_pDocument->handle(), HPDF_Page_GetCurrentFont(_page)));
delete _pCurrentFont; _pCurrentFont = 0;
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;
}

18
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())
{
int rows = _cells.size();
int cols = _cells[0].size();
int rows = static_cast<int>(_cells.size());
int cols = static_cast<int>(_cells[0].size());
int r = 0;
for (Cells::iterator it = _cells.begin(); it != _cells.end(); ++it)
{
TableRow& row(*it);
float h = height / rows;
int c = 0;
float lastX = x;
for (TableRow::iterator itr = row.begin(); itr != row.end(); ++itr)
{
Cell& cell(*itr);
float w = width / cols;
cell.draw(page, x + (w * c), y - (h * r), w, h);
if (!cell.hasWidth())
{
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;
}
++r;

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

@@ -279,6 +279,16 @@ public:
_pPage->setLineWidth(0.2f);
RGBColor black = {0, 0, 0};
_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()));
float margin = _styles.getFloat("margin", 0);
@@ -323,7 +333,6 @@ public:
_text = transcode(_text);
Font font = loadFont(fontFamily, fontStyle, fontWeight);
_pPage->setFont(font, fontSize);
float width = static_cast<float>(font.textWidth(_text).width*fontSize / 1000);
@@ -448,12 +457,20 @@ public:
AttributedString::Alignment align = AttributedString::ALIGN_LEFT;
int style = AttributedString::STYLE_PLAIN;
std::string fontFamily = _styles.getString("font-family");
float fontSize = _styles.getFloat("font-size");
std::string textAlign = _styles.getString("text-align", "left");
std::string fontStyle = _styles.getString("font-style", "normal");
std::string fontWeight = _styles.getString("font-weight", "normal");
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 = transcode(_text);
@@ -462,6 +479,8 @@ public:
align = AttributedString::ALIGN_RIGHT;
else if (textAlign == "left")
align = AttributedString::ALIGN_LEFT;
else if (textAlign == "center")
align = AttributedString::ALIGN_CENTER;
if (fontStyle == "italic" || fontStyle == "oblique")
style |= AttributedString::STYLE_ITALIC;
@@ -479,7 +498,25 @@ public:
(*pFontMap)[AttributedString::STYLE_ITALIC] = italicFontName(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();
}