1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
/*
* tikz.cc -- ePiX::tikz output format
*
* This file is part of ePiX, a C++ library for creating high-quality
* figures in LaTeX
*
* Version 1.1.15
* Last Change: September 07, 2007
*
* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
* Andrew D. Hwang <ahwang -at- holycross -dot- edu>
* Department of Mathematics and Computer Science
* College of the Holy Cross
* Worcester, MA, 01610-2395, USA
*
*
* ePiX is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* ePiX 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 General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ePiX; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <list>
#include <string>
#include <sstream>
#include <set>
#include "constants.h"
#include "utils.h"
#include "pairs.h"
#include "edge_data.h"
#include "Color.h"
#include "path_style.h"
#include "pen_data.h"
// need to get units from the_picture
#include "picture_data.h"
#include "picture.h"
#include "format.h"
#include "tikz.h"
namespace ePiX {
tikz::tikz()
: m_fill(Neutral()), m_stroke(Black()), m_lwidth(PLAIN_WIDTH),
m_units(the_picture().the_unitlength.units()) { }
tikz* tikz::clone() const
{
return new tikz(*this);
}
// Filled region with specified Cartesian edges, offset, and color
std::string tikz::print_fill(const std::list<edge2d>& edges,
const pair& offset,
const Color& fc,
const pen_data& line,
const std::string& len) const
{
std::stringstream obuf;
obuf << set_fill_state(fc) << set_pen_state(line);
// compute attribute string
std::stringstream attribs;
if ( !(fc.is_unset()) )
{
attribs << "[fill";
if (fc.alpha() < 1)
attribs << ",opacity=" << fc.alpha();
attribs << "]";
}
obuf << print_paths(edges, offset, attribs.str(), len);
return obuf.str();
}
// Unfilled region, specified Cartesian edges
std::string tikz::print_line(const std::list<edge2d>& edges,
const pair& offset,
const pen_data& line,
const pen_data& base,
const path_state& style,
const std::string& len) const
{
std::string value;
// draw *solid* base first
if (line.width() < base.width() && !base.color().is_unset())
{
std::stringstream battr;
battr << "[color=" << format::print(base.color()) << ","
<< "line width=" << format::print(base.width()) << "]";
value += format::print_line(edges, offset, base, path_state(),
battr.str(), len);
}
value += set_pen_state(line);
return value += format::print_line(edges, offset, line, style, "", len);
}
// Print color declaration strings: model, name, densities
std::string tikz::print_color(const std::string& model,
const std::string& name,
double d1, double d2,
double d3) const
{
return format::xdefinecolor(model, name, d1, d2, d3);
}
std::string tikz::print_color(const std::string& model,
const std::string& name,
double d1, double d2,
double d3, double d4) const
{
return format::xdefinecolor(model, name, d1, d2, d3, d4);
}
// One-line comment
std::string tikz::print_comment(const std::string& msg) const
{
std::stringstream obuf;
obuf << "%% " << msg << std::endl;
return obuf.str();
}
// verbatim string, newline protected
std::string tikz::print_verbatim(const std::string& msg) const
{
std::stringstream obuf;
obuf << msg << "%" << std::endl;
return obuf.str();
}
// start/end a picture-like environment, set unit length
std::string tikz::start_picture(const pair& sz, const pair& offset) const
{
std::stringstream obuf;
obuf << "\\begin{tikzpicture}" << std::endl
<< "\\pgfsetlinewidth{" << format::print(PLAIN_WIDTH) << "}"
<< std::endl
<< "\\useasboundingbox " << print(offset)
<< " rectangle " << print(sz + offset) << ";"
<< std::endl;
return obuf.str();
}
std::string tikz::end_picture() const
{
std::stringstream obuf;
obuf << "\\end{tikzpicture}" << std::endl;
return obuf.str();
}
// Actual work done in start_picture
std::string tikz::set_unitlength(const std::string& len) const
{
return "";
/*
std::stringstream obuf;
obuf << "\\setlength{\\unitlength}{1" << len << "}%" << std::endl;
return obuf.str();
*/
}
void tikz::reset_state() const
{
m_fill = Neutral();
m_stroke = Black();
m_lwidth = PLAIN_WIDTH;
}
//// private member functions ////
std::string tikz::path_connector() const
{
return "--";
}
std::string tikz::usepackages() const
{
return "usepackages tikz";
}
// string argument for passing attributes local to this path/loop
std::string tikz::start_open_path(const std::string& attribs) const
{
return "\\draw " + attribs;
}
std::string tikz::end_open_path(const std::string&) const
{
std::stringstream obuf;
obuf << ";" << std::endl;
return obuf.str();
}
std::string tikz::start_closed_path(const std::string& attribs) const
{
return "\\draw " + attribs;
}
std::string tikz::end_closed_path(const std::string&) const
{
std::stringstream obuf;
obuf << "--cycle;" << std::endl;
return obuf.str();
}
// print declarations to set state of output format
std::string tikz::set_fill_state(const Color& col) const
{
std::stringstream buf;
if (col != m_fill && !col.is_unset())
buf << "\\pgfsetfillcolor{" << format::print(col) << "}" << std::endl;
m_fill = col;
return buf.str();
}
std::string tikz::set_pen_state(const pen_data& pen) const
{
std::stringstream buf;
if (pen.color() != m_stroke)
{
buf << "\\pgfsetstrokecolor{" << format::print(pen.color()) << "}"
<< std::endl;
m_stroke = pen.color();
}
if (pen.width() != m_lwidth)
{
buf << "\\pgfsetlinewidth{" << format::print(pen.width()) << "}"
<< std::endl;
m_lwidth = pen.width();
}
return buf.str();
}
// place a LaTeX box of width zero (containing string) at location (pair)
std::string tikz::put_box(const pair& loc, const std::string& msg) const
{
std::stringstream obuf;
obuf << "\\pgftext[at={\\pgfpoint{"
<< truncate(loc.x1()) << m_units << "}{"
<< truncate(loc.x2()) << m_units
<< "}}] {" << msg << "}" << std::endl;
return obuf.str();
}
std::string tikz::print_circle_marker(const pair& here, double diam,
bool fill, const Color& color,
const std::string& len) const
{
std::stringstream obuf;
obuf << "\\";
if (fill)
obuf << "fill";
obuf << "draw[color=" << format::print(color) << "] "
<< print(here) << " circle(" << 0.5*diam << len << ");" << std::endl;
return obuf.str();
}
std::string tikz::print(const pair& arg) const
{
std::stringstream o;
o << "(" << truncate(arg.x1()) << m_units
<< "," << truncate(arg.x2()) << m_units << ")";
return o.str();
}
} // end of namespace
|