diff --git a/PDF/include/Poco/PDF/Cell.h b/PDF/include/Poco/PDF/Cell.h old mode 100644 new mode 100755 index 82c7eaa55..6797e9079 --- a/PDF/include/Poco/PDF/Cell.h +++ b/PDF/include/Poco/PDF/Cell.h @@ -35,7 +35,7 @@ public: }; 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(); const std::string& getName() const; @@ -56,6 +56,9 @@ public: void setFonts(FontMapPtr pFontMap); FontMapPtr getFonts() const { return _pFontMap; } void draw(Page& page, float x, float y, float width, float height); + int getWidthAsPct() const; + void setWidthAsPct(int width); + bool hasWidth() const; private: AttributedString _content; @@ -65,6 +68,7 @@ private: FontMapPtr _pFontMap; std::string _encoding; 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 diff --git a/PDF/include/Poco/PDF/Font.h b/PDF/include/Poco/PDF/Font.h old mode 100644 new mode 100755 index 796024032..023b7ec39 --- a/PDF/include/Poco/PDF/Font.h +++ b/PDF/include/Poco/PDF/Font.h @@ -30,7 +30,7 @@ class PDF_API Font: public Resource /// Font class represents font resource. { public: - Font(HPDF_Doc* pPDF, const HPDF_Font& resource); + Font(HPDF_Doc* pPDF, HPDF_Font resource); /// Creates the font. ~Font(); diff --git a/PDF/src/Cell.cpp b/PDF/src/Cell.cpp old mode 100644 new mode 100755 index b466c7f4b..e2b0acff8 --- a/PDF/src/Cell.cpp +++ b/PDF/src/Cell.cpp @@ -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(); } diff --git a/PDF/src/Font.cpp b/PDF/src/Font.cpp old mode 100644 new mode 100755 index 27d3131b3..fcbbc4769 --- a/PDF/src/Font.cpp +++ b/PDF/src/Font.cpp @@ -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(pPDF, font, HPDF_Font_GetFontName(font)) { } diff --git a/PDF/src/Page.cpp b/PDF/src/Page.cpp old mode 100644 new mode 100755 index a02d717ff..6f7fd49c1 --- a/PDF/src/Page.cpp +++ b/PDF/src/Page.cpp @@ -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; } diff --git a/PDF/src/Table.cpp b/PDF/src/Table.cpp old mode 100644 new mode 100755 index af12d18a8..94d8a9281 --- a/PDF/src/Table.cpp +++ b/PDF/src/Table.cpp @@ -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(_cells.size()); + int cols = static_cast(_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; diff --git a/PDF/src/XMLTemplate.cpp b/PDF/src/XMLTemplate.cpp old mode 100644 new mode 100755 index 4f5850de4..871f19a02 --- a/PDF/src/XMLTemplate.cpp +++ b/PDF/src/XMLTemplate.cpp @@ -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(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(); }