]> git.lyx.org Git - lyx.git/blobdiff - src/insets/ExternalTransforms.C
* src/LyXAction.C: mark goto-clear-bookmark as working without buffer
[lyx.git] / src / insets / ExternalTransforms.C
index 3a82d937eb9c996b40135ef850d1138b334eff14..16d6406881abb38747c5288a0c96f52a121e1794 100644 (file)
 
 #include "support/lstrings.h"
 #include "support/lyxlib.h" // float_equal
+#include "support/convert.h"
 #include "support/translator.h"
 
 #include <boost/regex.hpp>
-#include "support/std_sstream.h"
-#include <cmath> // std::abs
-
-using lyx::support::float_equal;
 
-using std::string;
+#include <cmath> // std::abs
+#include <sstream>
 
 
 namespace lyx {
 namespace external {
 
+using support::float_equal;
+using std::string;
+
+
 string const ExtraData::get(string const & id) const
 {
        std::map<string, string>::const_iterator it = data_.find(id);
@@ -45,22 +47,31 @@ void ExtraData::set(string const & id, string const & data)
 
 bool ResizeData::no_resize() const
 {
-       return float_equal(scale, 0.0, 0.05) &&
-               width.zero() && height.zero();
+       return !usingScale() && width.zero() && height.zero();
+}
+
+
+bool ResizeData::usingScale() const
+{
+       return (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05));
 }
 
 
 bool RotationData::no_rotation() const
 {
-       return (std::abs(angle()) < 0.1);
+       return (angle.empty() || std::abs(convert<double>(angle)) < 0.1);
 }
 
 
-void RotationData::angle(double a)
+string const RotationData::adjAngle() const
 {
-       // Ensure that angle_ lies in the range -360 < angle_ < 360.
-       int const multiples = int(a) / 360;
-       angle_ = a - (multiples * 360);
+       // Ensure that angle lies in the range -360 < angle < 360
+       double rotAngle = convert<double>(angle);
+       if (std::abs(rotAngle) > 360.0) {
+               rotAngle -= 360.0 * floor(rotAngle / 360.0);
+               return convert<string>(rotAngle);
+       }
+       return angle;
 }
 
 
@@ -89,16 +100,28 @@ string const ResizeLatexCommand::front_impl() const
        if (data.no_resize())
                return string();
 
-        std::ostringstream os;
-       if (!float_equal(data.scale, 0.0, 0.05)) {
-               double const scl = data.scale / 100.0;
-               os << "\\scalebox{" << scl << "}{" << scl << "}{";
+       std::ostringstream os;
+       if (data.usingScale()) {
+               double const scl = convert<double>(data.scale) / 100.0;
+               os << "\\scalebox{" << scl << "}[" << scl << "]{";
        } else {
+               string width  = "!";
+               string height = "!";
+               if (data.keepAspectRatio) {
+                       if (data.width.inPixels(10) > data.height.inPixels(10))
+                               width = data.width.asLatexString();
+                       else
+                               height = data.height.asLatexString();
+               } else {
+                       if (!data.width.zero())
+                               width = data.width.asLatexString();
+                       if (!data.height.zero())
+                               height = data.height.asLatexString();
+               }
+
                os << "\\resizebox{"
-                  << (data.width.zero()  ?
-                      "!" : data.width.asLatexString()) << "}{"
-                  << (data.height.zero() ?
-                      "!" : data.height.asLatexString()) << "}{";
+                  << width << "}{"
+                  << height << "}{";
        }
        return os.str();
 }
@@ -170,14 +193,14 @@ string const RotationLatexCommand::front_impl() const
        if (data.no_rotation())
                return string();
 
-        std::ostringstream os;
-        os << "\\rotatebox";
+       std::ostringstream os;
+       os << "\\rotatebox";
 
        if (data.origin() != RotationData::DEFAULT)
                os << "[origin=" << data.origin() << ']';
 
-       os << '{' << data.angle() << "}{";
-        return os.str();
+       os << '{' << data.angle << "}{";
+       return os.str();
 }
 
 
@@ -209,10 +232,11 @@ string const ResizeLatexOption::option_impl() const
        if (data.no_resize())
                return string();
 
-        std::ostringstream os;
-       if (!float_equal(data.scale, 0.0, 0.05)) {
-               if (!float_equal(data.scale, 100.0, 0.05))
-                       os << "scale=" << data.scale / 100.0 << ',';
+       std::ostringstream os;
+       if (data.usingScale()) {
+               double const scl = convert<double>(data.scale);
+               if (!float_equal(scl, 100.0, 0.05))
+                       os << "scale=" << scl / 100.0 << ',';
                return os.str();
        }
 
@@ -233,7 +257,7 @@ string const RotationLatexOption ::option_impl() const
                return string();
 
        std::ostringstream os;
-       os << "angle=" << data.angle() << ',';
+       os << "angle=" << data.angle << ',';
 
        if (data.origin() != RotationData::DEFAULT)
                os << "origin=" << data.origin() << ',';
@@ -255,15 +279,15 @@ string const sanitizeLatexOption(string const & input)
        string::const_iterator it = begin;
 
        // Strip any leading commas
-       // "[,,,,foo..." -> "foo..."
+       // "[,,,,foo..." -> "foo..." ("foo..." may be empty)
        string output;
        boost::smatch what;
-       boost::regex const front("^( *[[],*)(.*)$");
+       static boost::regex const front("^( *[[],*)(.*)$");
 
        regex_match(it, end, what, front, boost::match_partial);
        if (!what[0].matched) {
                lyxerr << "Unable to sanitize LaTeX \"Option\": "
-                      << output << '\n';
+                      << input << '\n';
                return string();
        }
        it =  what[1].second;
@@ -271,7 +295,7 @@ string const sanitizeLatexOption(string const & input)
        // Replace any consecutive commas with a single one
        // "foo,,,,bar" -> "foo,bar"
        // with iterator now pointing to 'b'
-       boost::regex const commas("([^,]*)(,,*)(.*)$");
+       static boost::regex const commas("([^,]*)(,,*)(.*)$");
        for (; it != end;) {
                regex_match(it, end, what, commas, boost::match_partial);
                if (!what[0].matched) {
@@ -280,11 +304,11 @@ string const sanitizeLatexOption(string const & input)
                }
                output += what.str(1) + ",";
                it = what[3].first;
-       }
+       }
 
        // Strip any trailing commas
-       // "...foo,,,]" -> "...foo"
-       boost::regex const back("^(.*[^,])(,*[]] *)$");
+       // "...foo,,,]" -> "...foo" ("...foo,,," may be empty)
+       static boost::regex const back("^(.*[^,])?,*[]] *$");
        regex_match(output, what, back);
        if (!what[0].matched) {
                lyxerr << "Unable to sanitize LaTeX \"Option\": "
@@ -294,19 +318,13 @@ string const sanitizeLatexOption(string const & input)
        output = what.str(1);
 
        // Remove any surrounding whitespace
-       output = lyx::support::trim(output);
+       output = support::trim(output);
 
        // If the thing is empty, leave it so, else wrap it in square brackets.
        return output.empty() ? output : "[" + output + "]";
 }
 
 
-string const sanitizeLinuxDocOption(string const & input)
-{
-       return input;
-}
-
-
 namespace {
 
 template <typename Factory, typename Data, typename Transformer>