1 // Copyright (C) 2000-2007, Luca Padovani <padovani@sti.uniurb.it>.
2 //
3 // This file is part of GtkMathView, a flexible, high-quality rendering
4 // engine for MathML documents.
5 //
6 // GtkMathView is free software; you can redistribute it and/or modify it
7 // under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GtkMathView is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 
19 #include <config.h>
20 
21 #include <cassert>
22 #include <iomanip>
23 
24 #include "AbstractLogger.hh"
25 #include "SVG_RenderingContext.hh"
26 #include "scaledAux.hh"
27 #include "BoundingBoxAux.hh"
28 #include "RGBColorAux.hh"
29 #include "TFM.hh"
30 #include "TFMFont.hh"
31 
SVG_RenderingContext(const SmartPtr<AbstractLogger> & l)32 SVG_RenderingContext::SVG_RenderingContext(const SmartPtr<AbstractLogger>& l)
33   : logger(l)
34 {
35   assert(logger);
36 }
37 
~SVG_RenderingContext()38 SVG_RenderingContext::~SVG_RenderingContext()
39 { }
40 
41 String
toSVGLength(const scaled & s) const42 SVG_RenderingContext::toSVGLength(const scaled& s) const
43 {
44   std::ostringstream buffer;
45   buffer << std::fixed << std::setprecision(2) << s.toFloat() << "pt";
46   return buffer.str();
47 }
48 
49 String
toSVGColor(const RGBColor & c) const50 SVG_RenderingContext::toSVGColor(const RGBColor& c) const
51 {
52   return toString(RGBColor(c.red, c.green, c.blue));
53 }
54 
55 String
toSVGOpacity(const RGBColor & c) const56 SVG_RenderingContext::toSVGOpacity(const RGBColor& c) const
57 {
58   std::ostringstream buffer;
59   buffer << static_cast<float>(c.alpha) / 255;
60   return buffer.str();
61 }
62 
63 void
documentStart(const BoundingBox & bbox)64 SVG_RenderingContext::documentStart(const BoundingBox& bbox)
65 {
66   beginDocument(bbox);
67   metadata("Created by " PACKAGE " version " VERSION);
68 }
69 
70 void
documentEnd()71 SVG_RenderingContext::documentEnd()
72 {
73   endDocument();
74 }
75 
76 void
fill(const scaled & x,const scaled & y,const BoundingBox & box)77 SVG_RenderingContext::fill(const scaled& x, const scaled& y, const BoundingBox& box)
78 {
79   rect(toSVGX(x), toSVGY(y + box.height), box.horizontalExtent(), box.verticalExtent(),
80        getForegroundColor(), getForegroundColor(), scaled::zero());
81 }
82 
83 void
draw(const scaled & x,const scaled & y,const SmartPtr<TFMFont> & font,Char8 index)84 SVG_RenderingContext::draw(const scaled& x, const scaled& y, const SmartPtr<TFMFont>& font, Char8 index)
85 {
86   SmartPtr<TFM> tfm = font->getTFM();
87   assert(tfm);
88   const int mappedIndex = (index < 32) ? 256 + index : index;
89 
90   std::ostringstream familyS;
91   familyS << tfm->getFamily() << tfm->getDesignSize().toInt();
92   std::ostringstream contentS;
93   contentS << "&#" << mappedIndex << ";";
94   const String family = familyS.str();
95   const String content = contentS.str();
96   text(toSVGX(x), toSVGY(y), family, font->getSize(),
97        getForegroundColor(), getForegroundColor(), scaled::zero(), content);
98 }
99 
100 void
wrapperStart(const scaled &,const scaled &,const BoundingBox &,const SmartPtr<Element> &)101 SVG_RenderingContext::wrapperStart(const scaled&, const scaled&, const BoundingBox&,
102 				   const SmartPtr<Element>&)
103 { }
104 
105 void
wrapperEnd()106 SVG_RenderingContext::wrapperEnd()
107 { }
108 
109 void
beginDocument(const BoundingBox &)110 SVG_RenderingContext::beginDocument(const BoundingBox&)
111 { }
112 
113 void
endDocument()114 SVG_RenderingContext::endDocument()
115 { }
116 
117 void
metadata(const String &)118 SVG_RenderingContext::metadata(const String&)
119 { }
120 
121 void
text(const scaled &,const scaled &,const String &,const scaled &,const RGBColor &,const RGBColor &,const scaled &,const String &)122 SVG_RenderingContext::text(const scaled&, const scaled&, const String&, const scaled&,
123 			   const RGBColor&, const RGBColor&, const scaled&, const String&)
124 { }
125 
126 void
rect(const scaled &,const scaled &,const scaled &,const scaled &,const RGBColor &,const RGBColor &,const scaled &)127 SVG_RenderingContext::rect(const scaled&, const scaled&, const scaled&, const scaled&,
128 			   const RGBColor&, const RGBColor&, const scaled&)
129 { }
130 
131 void
line(const scaled &,const scaled &,const scaled &,const scaled &,const RGBColor &,const scaled &)132 SVG_RenderingContext::line(const scaled&, const scaled&, const scaled&, const scaled&,
133 			   const RGBColor&, const scaled&)
134 { }
135