2 * \file ExternalTransforms.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
13 #include "ExternalTransforms.h"
17 #include "support/lstrings.h"
18 #include "support/lyxlib.h" // float_equal
19 #include "support/translator.h"
21 #include <boost/regex.hpp>
22 #include "support/std_sstream.h"
23 #include <cmath> // std::abs
25 using lyx::support::float_equal;
33 string const ExtraData::get(string const & id) const
35 std::map<string, string>::const_iterator it = data_.find(id);
36 return it == data_.end() ? string() : it->second;
40 void ExtraData::set(string const & id, string const & data)
46 bool ResizeData::no_resize() const
48 return !usingScale() && width.zero() && height.zero();
52 bool ResizeData::usingScale() const
54 return !float_equal(scale, 0.0, 0.05);
58 bool RotationData::no_rotation() const
60 return (std::abs(angle()) < 0.1);
64 void RotationData::angle(double a)
66 // Ensure that angle_ lies in the range -360 < angle_ < 360.
67 int const multiples = int(a) / 360;
68 angle_ = a - (multiples * 360);
74 typedef Translator<RotationData::OriginType, string> OriginTranslator;
75 OriginTranslator const & originTranslator();
80 void RotationData::origin(string const & o)
82 origin_ = originTranslator().find(o);
86 string const RotationData::originString() const
88 return originTranslator().find(origin_);
92 string const ResizeLatexCommand::front_impl() const
97 std::ostringstream os;
98 if (data.usingScale()) {
99 double const scl = data.scale / 100.0;
100 os << "\\scalebox{" << scl << "}{" << scl << "}{";
104 if (data.keepAspectRatio) {
105 if (data.width.inPixels(10) > data.height.inPixels(10))
106 width = data.width.asLatexString();
108 height = data.height.asLatexString();
110 if (!data.width.zero())
111 width = data.width.asLatexString();
112 if (!data.height.zero())
113 height = data.height.asLatexString();
124 string const ResizeLatexCommand::back_impl() const
126 if (data.no_resize())
135 std::ostream & operator<<(std::ostream & os, RotationData::OriginType type)
138 case RotationData::DEFAULT:
139 case RotationData::CENTER:
141 case RotationData::TOPLEFT:
142 case RotationData::TOPCENTER:
143 case RotationData::TOPRIGHT:
146 case RotationData::BOTTOMLEFT:
147 case RotationData::BOTTOMCENTER:
148 case RotationData::BOTTOMRIGHT:
151 case RotationData::BASELINELEFT:
152 case RotationData::BASELINECENTER:
153 case RotationData::BASELINERIGHT:
159 case RotationData::DEFAULT:
161 case RotationData::TOPLEFT:
162 case RotationData::BOTTOMLEFT:
163 case RotationData::BASELINELEFT:
166 case RotationData::CENTER:
167 case RotationData::TOPCENTER:
168 case RotationData::BOTTOMCENTER:
169 case RotationData::BASELINECENTER:
172 case RotationData::TOPRIGHT:
173 case RotationData::BOTTOMRIGHT:
174 case RotationData::BASELINERIGHT:
185 string const RotationLatexCommand::front_impl() const
187 if (data.no_rotation())
190 std::ostringstream os;
193 if (data.origin() != RotationData::DEFAULT)
194 os << "[origin=" << data.origin() << ']';
196 os << '{' << data.angle() << "}{";
201 string const RotationLatexCommand::back_impl() const
203 if (data.no_rotation())
210 string const ClipLatexOption::option_impl() const
212 if (!data.clip || data.bbox.empty())
215 std::ostringstream os;
216 if (!data.bbox.empty())
217 os << "bb=" << data.bbox << ',';
224 string const ResizeLatexOption::option_impl() const
226 if (data.no_resize())
229 std::ostringstream os;
230 if (data.usingScale()) {
231 if (!float_equal(data.scale, 100.0, 0.05))
232 os << "scale=" << data.scale / 100.0 << ',';
236 if (!data.width.zero())
237 os << "width=" << data.width.asLatexString() << ',';
238 if (!data.height.zero())
239 os << "height=" << data.height.asLatexString() << ',';
240 if (data.keepAspectRatio)
241 os << "keepaspectratio,";
247 string const RotationLatexOption ::option_impl() const
249 if (data.no_rotation())
252 std::ostringstream os;
253 os << "angle=" << data.angle() << ',';
255 if (data.origin() != RotationData::DEFAULT)
256 os << "origin=" << data.origin() << ',';
262 string const sanitizeDocBookOption(string const & input)
268 string const sanitizeLatexOption(string const & input)
270 string::const_iterator begin = input.begin();
271 string::const_iterator end = input.end();
272 string::const_iterator it = begin;
274 // Strip any leading commas
275 // "[,,,,foo..." -> "foo..."
278 static boost::regex const front("^( *[[],*)(.*)$");
280 regex_match(it, end, what, front, boost::match_partial);
281 if (!what[0].matched) {
282 lyxerr << "Unable to sanitize LaTeX \"Option\": "
288 // Replace any consecutive commas with a single one
289 // "foo,,,,bar" -> "foo,bar"
290 // with iterator now pointing to 'b'
291 static boost::regex const commas("([^,]*)(,,*)(.*)$");
293 regex_match(it, end, what, commas, boost::match_partial);
294 if (!what[0].matched) {
295 output += string(it, end);
298 output += what.str(1) + ",";
302 // Strip any trailing commas
303 // "...foo,,,]" -> "...foo"
304 static boost::regex const back("^(.*[^,])(,*[]] *)$");
305 regex_match(output, what, back);
306 if (!what[0].matched) {
307 lyxerr << "Unable to sanitize LaTeX \"Option\": "
311 output = what.str(1);
313 // Remove any surrounding whitespace
314 output = lyx::support::trim(output);
316 // If the thing is empty, leave it so, else wrap it in square brackets.
317 return output.empty() ? output : "[" + output + "]";
321 string const sanitizeLinuxDocOption(string const & input)
329 template <typename Factory, typename Data, typename Transformer>
330 void extractIt(boost::any const & any_factory,
331 Data const & data, Transformer & transformer)
333 if (any_factory.type() != typeid(Factory))
336 Factory factory = boost::any_cast<Factory>(any_factory);
337 if (!factory.empty())
338 transformer = factory(data);
344 TransformCommand::ptr_type
345 TransformStore::getCommandTransformer(RotationData const & data) const
347 TransformCommand::ptr_type ptr;
349 extractIt<RotationCommandFactory>(any_factory, data, ptr);
354 TransformCommand::ptr_type
355 TransformStore::getCommandTransformer(ResizeData const & data) const
357 TransformCommand::ptr_type ptr;
359 extractIt<ResizeCommandFactory>(any_factory, data, ptr);
364 TransformOption::ptr_type
365 TransformStore::getOptionTransformer(RotationData const & data) const
367 TransformOption::ptr_type ptr;
369 extractIt<RotationOptionFactory>(any_factory, data, ptr);
374 TransformOption::ptr_type
375 TransformStore::getOptionTransformer(ResizeData const & data) const
377 TransformOption::ptr_type ptr;
379 extractIt<ResizeOptionFactory>(any_factory, data, ptr);
384 TransformOption::ptr_type
385 TransformStore::getOptionTransformer(ClipData const & data) const
387 TransformOption::ptr_type ptr;
389 extractIt<ClipOptionFactory>(any_factory, data, ptr);
395 TransformOption::ptr_type
396 TransformStore::getOptionTransformer(string const & data) const
398 TransformOption::ptr_type ptr;
400 extractIt<ExtraOptionFactory>(any_factory, data, ptr);
407 OriginTranslator const initOriginTranslator()
409 OriginTranslator translator(RotationData::DEFAULT, "default");
410 translator.addPair(RotationData::TOPLEFT, "topleft");
411 translator.addPair(RotationData::BOTTOMLEFT, "bottomleft");
412 translator.addPair(RotationData::BASELINELEFT, "baselineleft");
413 translator.addPair(RotationData::CENTER, "center");
414 translator.addPair(RotationData::TOPCENTER, "topcenter");
415 translator.addPair(RotationData::BOTTOMCENTER, "bottomcenter");
416 translator.addPair(RotationData::BASELINECENTER, "baselinecenter");
417 translator.addPair(RotationData::TOPRIGHT, "topright");
418 translator.addPair(RotationData::BOTTOMRIGHT, "bottomright");
419 translator.addPair(RotationData::BASELINERIGHT, "baselineright");
424 OriginTranslator const & originTranslator()
426 static OriginTranslator const translator = initOriginTranslator();
432 } // namespace external