/* libwpg * Copyright (C) 2006 Ariya Hidayat (ariya@kde.org) * Copyright (C) 2005 Fridrich Strba (fridrich.strba@bluewin.ch) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * For further information visit http://libwpg.sourceforge.net */ /* "This product is not manufactured, approved, or supported by * Corel Corporation or Corel Corporation Limited." */ #include "WPGSVGGenerator.h" libwpg::WPGSVGGenerator::WPGSVGGenerator(std::ostream & output_sink): m_pen(libwpg::WPGPen()), m_brush(libwpg::WPGBrush()), m_fillRule(AlternatingFill), m_gradientIndex(1), m_outputSink(output_sink), m_oldLocale(output_sink.getloc()) { // set the stream's locale to "C" when printing floats std::locale c_locale("C"); m_outputSink.imbue(c_locale); } libwpg::WPGSVGGenerator::~WPGSVGGenerator() { m_outputSink.imbue(m_oldLocale); } void libwpg::WPGSVGGenerator::startDocument(double width, double height) { m_outputSink << "\n"; m_outputSink << "\n"; // m_outputSink << "\n"; m_outputSink << "\n"; m_gradientIndex = 1; } void libwpg::WPGSVGGenerator::endDocument() { m_outputSink << "\n"; } void libwpg::WPGSVGGenerator::setPen(const libwpg::WPGPen& pen) { m_pen = pen; } void libwpg::WPGSVGGenerator::setBrush(const libwpg::WPGBrush& brush) { m_brush = brush; if(m_brush.style == libwpg::WPGBrush::Gradient) { double angle = m_brush.gradient.angle(); m_outputSink << "\n"; m_outputSink << " \n"; for(unsigned c = 0; c < m_brush.gradient.count(); c++) { // round to nearest percentage int ofs = (int)((100.0*m_brush.gradient.stopOffset(c))+0.5); libwpg::WPGColor color = m_brush.gradient.stopColor(c); m_outputSink << " \n"; // reset stream formatting m_outputSink << std::dec; m_outputSink.width(old_stream_size); } m_outputSink << " \n"; // not a simple horizontal gradient if(angle != -90.0) { m_outputSink << " \n"; m_outputSink << " \n"; } m_outputSink << "\n"; } } void libwpg::WPGSVGGenerator::setFillRule(FillRule rule) { m_fillRule = rule; } void libwpg::WPGSVGGenerator::startLayer(unsigned int id) { m_outputSink << "\n"; } void libwpg::WPGSVGGenerator::endLayer(unsigned int) { m_outputSink << "\n"; } void libwpg::WPGSVGGenerator::drawRectangle(const libwpg::WPGRect& rect, double rx, double ry) { m_outputSink << "\n"; } void libwpg::WPGSVGGenerator::drawEllipse(const libwpg::WPGPoint& center, double rx, double ry) { m_outputSink << "\n"; } void libwpg::WPGSVGGenerator::drawPolygon(const libwpg::WPGPointArray& vertices) { if(vertices.count() < 2) return; if(vertices.count() == 2) { const libwpg::WPGPoint& p1 = vertices[0]; const libwpg::WPGPoint& p2 = vertices[1]; m_outputSink << "\n"; } else { m_outputSink << "\n"; } } void libwpg::WPGSVGGenerator::drawPath(const libwpg::WPGPath& path) { m_outputSink << "\n"; } // create "style" attribute based on current pen and brush void libwpg::WPGSVGGenerator::writeStyle() { m_outputSink << "style=\""; const libwpg::WPGColor& color = m_pen.foreColor; m_outputSink << "stroke-width: " << 72*m_pen.width << "; "; if(m_pen.width > 0.0) { m_outputSink << "stroke: rgb(" << color.red << "," << color.green << "," << color.blue << "); "; if(color.alpha != 0) // alpha = 0 means opacity = 1.0, alpha = 256 means opacity = 0 m_outputSink << "stroke-opacity: " << 1.0-(color.alpha/256.0) << "; "; } if(!m_pen.solid) { m_outputSink << "stroke-dasharray: "; for(unsigned i = 0; i < m_pen.dashArray.count(); i++) { m_outputSink << 72*m_pen.dashArray.at(i)*m_pen.width; if(i < m_pen.dashArray.count()-1) m_outputSink << ", "; } m_outputSink << "; "; } if(m_brush.style == libwpg::WPGBrush::NoBrush) m_outputSink << "fill: none; "; if(m_fillRule == WPGSVGGenerator::WindingFill) m_outputSink << "fill-rule: nonzero; "; else if(m_fillRule == WPGSVGGenerator::AlternatingFill) m_outputSink << "fill-rule: evenodd; "; if(m_brush.style == libwpg::WPGBrush::Gradient) m_outputSink << "fill: url(#grad" << m_gradientIndex-1 << "); "; if(m_brush.style == libwpg::WPGBrush::Solid) m_outputSink << "fill: rgb(" << m_brush.foreColor.red << "," << m_brush.foreColor.green << "," << m_brush.foreColor.blue << "); "; m_outputSink << "\""; // style }