]> git.lyx.org Git - lyx.git/blobdiff - src/Length.cpp
Clarify "Save compressed by default" feature (bug 7822)
[lyx.git] / src / Length.cpp
index a47052b506757b53083842dd7c33c7b1ad4a4bd2..89061e104b2470ef85741c125bc6bf8d52f95500 100644 (file)
@@ -4,7 +4,7 @@
  * Licence details can be found in the file COPYING.
  *
  * \author Matthias Ettrich
- * \author Lars Gullik Bjønnes
+ * \author Lars Gullik Bjønnes
  * \author Jean-Marc Lasgouttes
  * \author Angus Leeming
  * \author John Levon
 #include <config.h>
 
 #include "Length.h"
-#include "lengthcommon.h"
 #include "LyXRC.h"
+#include "MetricsInfo.h"
+
+#include "frontends/FontMetrics.h"
 
 #include "support/docstream.h"
+#include "support/lstrings.h"
+
 #include <sstream>
 #include <iomanip>
 
+using namespace std;
 
 namespace lyx {
 
-using std::ostringstream;
-using std::string;
-
 
 /////////////////////////////////////////////////////////////////////
 //
@@ -62,7 +64,8 @@ Length::Length(string const & data)
 string const Length::asString() const
 {
        ostringstream os;
-       os << val_ << unit_name[unit_]; // setw?
+       if (unit_ != UNIT_NONE)
+               os << val_ << unit_name[unit_]; // setw?
        return os.str();
 }
 
@@ -70,7 +73,8 @@ string const Length::asString() const
 docstring const Length::asDocstring() const
 {
        odocstringstream os;
-       os << val_ << unit_name[unit_]; // setw?
+       if (unit_ != UNIT_NONE)
+               os << val_ << unit_name[unit_]; // setw?
        return os.str();
 }
 
@@ -78,33 +82,79 @@ docstring const Length::asDocstring() const
 string const Length::asLatexString() const
 {
        ostringstream os;
+       // Do not allow scientific notation (e.g. 1.2e+03), since this is not valid
+       // LaTeX (bug 9416)
        switch (unit_) {
        case PTW:
-               os << val_ / 100.0 << "\\textwidth";
+               os << support::formatFPNumber(val_ / 100.0) << "\\textwidth";
                break;
        case PCW:
-               os << val_ / 100.0 << "\\columnwidth";
+               os << support::formatFPNumber(val_ / 100.0) << "\\columnwidth";
                break;
        case PPW:
-               os << val_ / 100.0 << "\\paperwidth";
+               os << support::formatFPNumber(val_ / 100.0) << "\\paperwidth";
                break;
        case PLW:
-               os << val_ / 100.0 << "\\linewidth";
+               os << support::formatFPNumber(val_ / 100.0) << "\\linewidth";
+               break;
+       case PTH:
+               os << support::formatFPNumber(val_ / 100.0) << "\\textheight";
                break;
        case PPH:
-               os << val_ / 100.0 << "\\paperheight";
+               os << support::formatFPNumber(val_ / 100.0) << "\\paperheight";
                break;
-       case PTH:
-               os << val_ / 100.0 << "\\textheight";
+       case UNIT_NONE:
                break;
        default:
-               os << val_ << unit_name[unit_];
+               os << support::formatFPNumber(val_) << unit_name[unit_];
          break;
        }
        return os.str();
 }
 
 
+string const Length::asHTMLString() const
+{
+       ostringstream os;
+       switch (unit_) {
+       case PT:
+       case BP:
+       case DD:
+               // close enough
+               os << val_ << "pt";
+               break;
+       case MM:
+       case CM:
+       case PC:
+       case IN:
+       case EX:
+       case EM:
+               os << val_ << unit_name[unit_];
+               break;
+       case CC:
+               os << val_/12.0 << "pt";
+               break;
+       case MU:
+               os << val_/18.0 << "em";
+               break;
+       case PTW:
+       case PPW:
+       case PLW:
+       case PCW:
+       case PTH:
+       case PPH:
+               // what it's a percentage of probably won't make sense for HTML,
+               // so we'll assume people have chosen these appropriately
+               os << val_ << '%';
+               break;
+       case SP:
+       case UNIT_NONE:
+               break;
+       }
+       return os.str();
+}
+
+
 double Length::value() const
 {
        return val_;
@@ -153,7 +203,7 @@ int Length::inPixels(int text_width, int em_width_base) const
                ? em_width_base
                : 10*(dpi/72.27)*zoom;
        // A different estimate for em_width is
-       // theFontMetrics(Font(Font::ALL_SANE)).width('M')
+       // theFontMetrics(FontInfo(sane_font)).em()
        // but this estimate might not be more accurate as the screen font
        // is different then the latex font.
 
@@ -244,6 +294,12 @@ int Length::inPixels(int text_width, int em_width_base) const
 }
 
 
+int Length::inPixels(MetricsBase const & base) const
+{
+       return inPixels(base.textwidth, theFontMetrics(base.font).em());
+}
+
+
 int Length::inBP() const
 {
        // return any Length value as a one with
@@ -271,6 +327,13 @@ int Length::inBP() const
 }
 
 
+Length::UNIT Length::defaultUnit()
+{
+       return lyxrc.default_length_unit;
+}
+
+
+
 bool operator==(Length const & l1, Length const & l2)
 {
        return l1.value() == l2.value() && l1.unit() == l2.unit();
@@ -309,6 +372,9 @@ GlueLength::GlueLength(string const & data)
 
 string const GlueLength::asString() const
 {
+       if (len_.empty())
+               return string();
+
        ostringstream buffer;
 
        buffer << len_.value();
@@ -360,13 +426,12 @@ string const GlueLength::asString() const
 string const GlueLength::asLatexString() const
 {
        ostringstream buffer;
-
-       buffer << len_.value() << unit_name[len_.unit()];
-
+       // use Length::asLatexString() to handle also the percent lengths
+       buffer << len_.Length::asLatexString();
        if (!plus_.zero())
-               buffer << " plus " << plus_.value() << unit_name[plus_.unit()];
+               buffer << " plus " << plus_.Length::asLatexString();
        if (!minus_.zero())
-               buffer << " minus " << minus_.value() << unit_name[minus_.unit()];
+               buffer << " minus " << minus_.Length::asLatexString();
        return buffer.str();
 }