]> git.lyx.org Git - lyx.git/blobdiff - src/lyxlength.C
some more changes
[lyx.git] / src / lyxlength.C
index 7eb622220cdda516c5da01a5fefd733cd726543b..c21bd86766df57f5f95e02f5fef55aa5ad354195 100644 (file)
@@ -2,7 +2,7 @@
  * ======================================================
  *
  *           LyX, The Document Processor
- *     
+ *
  *           Copyright 1995 Matthias Ettrich
  *           Copyright 1995-2001 The LyX Team.
  *
 #include "lengthcommon.h"
 #include "lyxrc.h"
 
+#include "support/lstrings.h"
+
 #include "Lsstream.h"
 
 #include <cstdlib>
 
+using std::abs;
 
 LyXLength::LyXLength()
        : val_(0), unit_(LyXLength::PT)
@@ -33,12 +36,48 @@ LyXLength::LyXLength(double v, LyXLength::UNIT u)
 {}
 
 
+#ifndef NO_PEXTRA_REALLY
+// compatibility stuff < version 1.2.0pre and for 
+// "old" 1.2.0 files before the pre
+namespace {
+string const convertOldRelLength(string const & oldLength) 
+{
+       // we can have only one or none of the following
+       if (oldLength.find("c%") != string::npos) {
+               return subst(oldLength,"c%","col%");
+                   
+       } else if (oldLength.find("t%") != string::npos) {
+               if (oldLength.find("text%") != string::npos)
+                   return oldLength;
+               else
+                   return subst(oldLength,"t%","text%");
+
+       } else if (oldLength.find("l%") != string::npos) {
+               if (oldLength.find("col%") != string::npos)
+                   return oldLength;
+               else
+                   return subst(oldLength,"l%","line%");
+
+       } else if (oldLength.find("p%") != string::npos)
+               return subst(oldLength,"p%","page%");
+
+       return oldLength;
+}
+} // end anon
+#endif
+
 LyXLength::LyXLength(string const & data)
        : val_(0), unit_(LyXLength::PT)
 {
        LyXLength tmp;
-       
+
+#ifndef NO_PEXTRA_REALLY
+       // this is needed for 1.1.x minipages with width like %t
+       if (!isValidLength (convertOldRelLength(data), &tmp))
+#else
        if (!isValidLength (data, &tmp))
+#endif
+               if (!isValidLength (convertOldRelLength(data), &tmp))
                return; // should raise an exception
 
        val_  = tmp.val_;
@@ -106,7 +145,7 @@ void LyXLength::unit(LyXLength::UNIT u)
 }
 
 
-bool LyXLength::zero() const 
+bool LyXLength::zero() const
 {
        return val_ == 0.0;
 }
@@ -126,9 +165,13 @@ int LyXLength::inPixels(int default_width, int default_height) const
 
        // we don't care about sign of value, we
        // display negative space with text too
+#ifdef WITH_WARNINGS
+#warning if you don't care than either call this function differently or let it return negative values and call abs() explicitly when needed (Andre')
+#endif
+
        double result = 0.0;
        int val_sign = val_ < 0.0 ? -1 : 1;
-               
+
        switch (unit_) {
        case LyXLength::SP:
                // Scaled point: sp = 1/65536 pt
@@ -200,6 +243,33 @@ int LyXLength::inPixels(int default_width, int default_height) const
 }
 
 
+int LyXLength::inBP() const
+{
+       // return any LyXLength value as a one with
+       // the PostScript point, called bp (big points)
+       double result = 0.0;
+       switch (unit_) {
+       case LyXLength::CM:
+               // 1bp = 0.2835cm
+               result = val_ * 28.346;
+               break;
+       case LyXLength::MM:
+               // 1bp = 0.02835mm
+               result = val_ * 2.8346;
+               break;
+       case LyXLength::IN:
+               // 1pt = 1/72in
+               result = val_ * 72.0;
+               break;
+       default:
+               // no other than bp possible
+               result = val_;
+               break;
+       }
+       return static_cast<int>(result + 0.5);
+}
+
+
 bool operator==(LyXLength const & l1, LyXLength const & l2)
 {
        return l1.value() == l2.value() && l1.unit() == l2.unit();
@@ -210,4 +280,3 @@ bool operator!=(LyXLength const & l1, LyXLength const & l2)
 {
        return !(l1 == l2);
 }
-