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/convert.h"
20 #include "support/translator.h"
22 #include <boost/regex.hpp>
24 #include <cmath> // std::abs
31 using support::float_equal;
35 string const ExtraData::get(string const & id) const
37 std::map<string, string>::const_iterator it = data_.find(id);
38 return it == data_.end() ? string() : it->second;
42 void ExtraData::set(string const & id, string const & data)
48 bool ResizeData::no_resize() const
50 return !usingScale() && width.zero() && height.zero();
54 bool ResizeData::usingScale() const
56 return (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05));
60 bool RotationData::no_rotation() const
62 return (angle.empty() || std::abs(convert<double>(angle)) < 0.1);
66 string const RotationData::adjAngle() const
68 // Ensure that angle lies in the range -360 < angle < 360
69 double rotAngle = convert<double>(angle);
70 if (std::abs(rotAngle) > 360.0) {
71 rotAngle -= 360.0 * floor(rotAngle / 360.0);
72 return convert<string>(rotAngle);
80 typedef Translator<RotationData::OriginType, string> OriginTranslator;
81 OriginTranslator const & originTranslator();
86 void RotationData::origin(string const & o)
88 origin_ = originTranslator().find(o);
92 string const RotationData::originString() const
94 return originTranslator().find(origin_);
98 string const ResizeLatexCommand::front_impl() const
100 if (data.no_resize())
103 std::ostringstream os;
104 if (data.usingScale()) {
105 double const scl = convert<double>(data.scale) / 100.0;
106 os << "\\scalebox{" << scl << "}[" << scl << "]{";
110 if (data.keepAspectRatio) {
111 if (data.width.inPixels(10) > data.height.inPixels(10))
112 width = data.width.asLatexString();
114 height = data.height.asLatexString();
116 if (!data.width.zero())
117 width = data.width.asLatexString();
118 if (!data.height.zero())
119 height = data.height.asLatexString();
130 string const ResizeLatexCommand::back_impl() const
132 if (data.no_resize())
141 std::ostream & operator<<(std::ostream & os, RotationData::OriginType type)
144 case RotationData::DEFAULT:
145 case RotationData::CENTER:
147 case RotationData::TOPLEFT:
148 case RotationData::TOPCENTER:
149 case RotationData::TOPRIGHT:
152 case RotationData::BOTTOMLEFT:
153 case RotationData::BOTTOMCENTER:
154 case RotationData::BOTTOMRIGHT:
157 case RotationData::BASELINELEFT:
158 case RotationData::BASELINECENTER:
159 case RotationData::BASELINERIGHT:
165 case RotationData::DEFAULT:
167 case RotationData::TOPLEFT:
168 case RotationData::BOTTOMLEFT:
169 case RotationData::BASELINELEFT:
172 case RotationData::CENTER:
173 case RotationData::TOPCENTER:
174 case RotationData::BOTTOMCENTER:
175 case RotationData::BASELINECENTER:
178 case RotationData::TOPRIGHT:
179 case RotationData::BOTTOMRIGHT:
180 case RotationData::BASELINERIGHT:
191 string const RotationLatexCommand::front_impl() const
193 if (data.no_rotation())
196 std::ostringstream os;
199 if (data.origin() != RotationData::DEFAULT)
200 os << "[origin=" << data.origin() << ']';
202 os << '{' << data.angle << "}{";
207 string const RotationLatexCommand::back_impl() const
209 if (data.no_rotation())
216 string const ClipLatexOption::option_impl() const
218 if (!data.clip || data.bbox.empty())
221 std::ostringstream os;
222 if (!data.bbox.empty())
223 os << "bb=" << data.bbox << ',';
230 string const ResizeLatexOption::option_impl() const
232 if (data.no_resize())
235 std::ostringstream os;
236 if (data.usingScale()) {
237 double const scl = convert<double>(data.scale);
238 if (!float_equal(scl, 100.0, 0.05))
239 os << "scale=" << scl / 100.0 << ',';
243 if (!data.width.zero())
244 os << "width=" << data.width.asLatexString() << ',';
245 if (!data.height.zero())
246 os << "height=" << data.height.asLatexString() << ',';
247 if (data.keepAspectRatio)
248 os << "keepaspectratio,";
254 string const RotationLatexOption ::option_impl() const
256 if (data.no_rotation())
259 std::ostringstream os;
260 os << "angle=" << data.angle << ',';
262 if (data.origin() != RotationData::DEFAULT)
263 os << "origin=" << data.origin() << ',';
269 string const sanitizeDocBookOption(string const & input)
275 string const sanitizeLatexOption(string const & input)
277 string::const_iterator begin = input.begin();
278 string::const_iterator end = input.end();
279 string::const_iterator it = begin;
281 // Strip any leading commas
282 // "[,,,,foo..." -> "foo..." ("foo..." may be empty)
285 static boost::regex const front("^( *[[],*)(.*)$");
287 regex_match(it, end, what, front, boost::match_partial);
288 if (!what[0].matched) {
289 lyxerr << "Unable to sanitize LaTeX \"Option\": "
295 // Replace any consecutive commas with a single one
296 // "foo,,,,bar" -> "foo,bar"
297 // with iterator now pointing to 'b'
298 static boost::regex const commas("([^,]*)(,,*)(.*)$");
300 regex_match(it, end, what, commas, boost::match_partial);
301 if (!what[0].matched) {
302 output += string(it, end);
305 output += what.str(1) + ",";
309 // Strip any trailing commas
310 // "...foo,,,]" -> "...foo" ("...foo,,," may be empty)
311 static boost::regex const back("^(.*[^,])?,*[]] *$");
312 regex_match(output, what, back);
313 if (!what[0].matched) {
314 lyxerr << "Unable to sanitize LaTeX \"Option\": "
318 output = what.str(1);
320 // Remove any surrounding whitespace
321 output = support::trim(output);
323 // If the thing is empty, leave it so, else wrap it in square brackets.
324 return output.empty() ? output : "[" + output + "]";
330 template <typename Factory, typename Data, typename Transformer>
331 void extractIt(boost::any const & any_factory,
332 Data const & data, Transformer & transformer)
334 if (any_factory.type() != typeid(Factory))
337 Factory factory = boost::any_cast<Factory>(any_factory);
338 if (!factory.empty())
339 transformer = factory(data);
345 TransformCommand::ptr_type
346 TransformStore::getCommandTransformer(RotationData const & data) const
348 TransformCommand::ptr_type ptr;
350 extractIt<RotationCommandFactory>(any_factory, data, ptr);
355 TransformCommand::ptr_type
356 TransformStore::getCommandTransformer(ResizeData const & data) const
358 TransformCommand::ptr_type ptr;
360 extractIt<ResizeCommandFactory>(any_factory, data, ptr);
365 TransformOption::ptr_type
366 TransformStore::getOptionTransformer(RotationData const & data) const
368 TransformOption::ptr_type ptr;
370 extractIt<RotationOptionFactory>(any_factory, data, ptr);
375 TransformOption::ptr_type
376 TransformStore::getOptionTransformer(ResizeData const & data) const
378 TransformOption::ptr_type ptr;
380 extractIt<ResizeOptionFactory>(any_factory, data, ptr);
385 TransformOption::ptr_type
386 TransformStore::getOptionTransformer(ClipData const & data) const
388 TransformOption::ptr_type ptr;
390 extractIt<ClipOptionFactory>(any_factory, data, ptr);
396 TransformOption::ptr_type
397 TransformStore::getOptionTransformer(string const & data) const
399 TransformOption::ptr_type ptr;
401 extractIt<ExtraOptionFactory>(any_factory, data, ptr);
408 OriginTranslator const initOriginTranslator()
410 OriginTranslator translator(RotationData::DEFAULT, "default");
411 translator.addPair(RotationData::TOPLEFT, "topleft");
412 translator.addPair(RotationData::BOTTOMLEFT, "bottomleft");
413 translator.addPair(RotationData::BASELINELEFT, "baselineleft");
414 translator.addPair(RotationData::CENTER, "center");
415 translator.addPair(RotationData::TOPCENTER, "topcenter");
416 translator.addPair(RotationData::BOTTOMCENTER, "bottomcenter");
417 translator.addPair(RotationData::BASELINECENTER, "baselinecenter");
418 translator.addPair(RotationData::TOPRIGHT, "topright");
419 translator.addPair(RotationData::BOTTOMRIGHT, "bottomright");
420 translator.addPair(RotationData::BASELINERIGHT, "baselineright");
425 OriginTranslator const & originTranslator()
427 static OriginTranslator const translator = initOriginTranslator();
433 } // namespace external