]> git.lyx.org Git - lyx.git/commitdiff
#9376 prepare use of Length in lyxrc - move the class Length to support
authorStephan Witt <switt@lyx.org>
Mon, 3 Aug 2020 12:15:09 +0000 (14:15 +0200)
committerStephan Witt <switt@lyx.org>
Fri, 7 Aug 2020 07:24:31 +0000 (09:24 +0200)
44 files changed:
src/BufferParams.cpp
src/BufferView.cpp
src/Length.cpp [deleted file]
src/Length.h [deleted file]
src/LyXRC.h
src/Makefile.am
src/Paragraph.cpp
src/ParagraphParameters.h
src/Text.cpp
src/VSpace.cpp
src/VSpace.h
src/frontends/qt/GuiBox.cpp
src/frontends/qt/GuiExternal.cpp
src/frontends/qt/GuiGraphics.cpp
src/frontends/qt/LengthCombo.h
src/frontends/qt/Validator.h
src/frontends/qt/qt_helpers.cpp
src/frontends/qt/qt_helpers.h
src/graphics/GraphicsParams.h
src/insets/ExternalTransforms.h
src/insets/InsetBox.h
src/insets/InsetGraphics.cpp
src/insets/InsetInfo.cpp
src/insets/InsetLine.cpp
src/insets/InsetListingsParams.cpp
src/insets/InsetNomencl.cpp
src/insets/InsetSpace.cpp
src/insets/InsetSpace.h
src/insets/InsetTabular.h
src/insets/InsetWrap.h
src/lengthcommon.cpp [deleted file]
src/mathed/InsetMathGrid.h
src/mathed/InsetMathKern.h
src/mathed/InsetMathSpace.h
src/mathed/InsetMathXYMatrix.h
src/mathed/MathSupport.cpp
src/support/Length.cpp [new file with mode: 0644]
src/support/Length.h [new file with mode: 0644]
src/support/Makefile.am
src/support/lengthcommon.cpp [new file with mode: 0644]
src/tests/CMakeLists.txt
src/tex2lyx/CMakeLists.txt
src/tex2lyx/Makefile.am
src/tex2lyx/text.cpp

index f678e73a7facbc5c0732d20c70cdc2c36df8f502..6e0d9f91c6aacb37a2b188cb218c1bb427c83b7d 100644 (file)
@@ -32,7 +32,6 @@
 #include "Language.h"
 #include "LaTeXFeatures.h"
 #include "LaTeXFonts.h"
-#include "Length.h"
 #include "ModuleList.h"
 #include "Font.h"
 #include "Lexer.h"
@@ -54,6 +53,7 @@
 #include "support/FileName.h"
 #include "support/filetools.h"
 #include "support/gettext.h"
+#include "support/Length.h"
 #include "support/Messages.h"
 #include "support/mutex.h"
 #include "support/Package.h"
index fe02be5019a3b8565fd98c7d1ce8ec8ecbc1f6c7..e316aab02e2d38319d2d5dd6df5d7cabb106a90d 100644 (file)
@@ -35,7 +35,6 @@
 #include "Language.h"
 #include "LaTeXFeatures.h"
 #include "LayoutFile.h"
-#include "Length.h"
 #include "Lexer.h"
 #include "LyX.h"
 #include "LyXAction.h"
@@ -81,6 +80,7 @@
 #include "support/filetools.h"
 #include "support/gettext.h"
 #include "support/lassert.h"
+#include "support/Length.h"
 #include "support/lstrings.h"
 #include "support/lyxlib.h"
 #include "support/Package.h"
diff --git a/src/Length.cpp b/src/Length.cpp
deleted file mode 100644 (file)
index d286882..0000000
+++ /dev/null
@@ -1,463 +0,0 @@
-/**
- * \file Length.cpp
- * This file is part of LyX, the document processor.
- * Licence details can be found in the file COPYING.
- *
- * \author Matthias Ettrich
- * \author Lars Gullik Bjønnes
- * \author Jean-Marc Lasgouttes
- * \author Angus Leeming
- * \author John Levon
- * \author Dekel Tsur
- *
- * Full author contact details are available in file CREDITS.
- */
-
-#include <config.h>
-
-#include "Length.h"
-#include "LyXRC.h"
-
-#include "support/debug.h"
-#include "support/docstream.h"
-#include "support/lstrings.h"
-#include "support/lyxlib.h"
-
-#include <sstream>
-#include <iomanip>
-
-using namespace std;
-using namespace lyx::support;
-
-namespace lyx {
-
-
-/////////////////////////////////////////////////////////////////////
-//
-// Length
-//
-/////////////////////////////////////////////////////////////////////
-
-Length::Length()
-       : val_(0), unit_(Length::UNIT_NONE)
-{}
-
-
-Length::Length(double v, Length::UNIT u)
-       : val_(v), unit_(u)
-{}
-
-
-Length::Length(string const & data)
-       : val_(0), unit_(Length::UNIT_NONE)
-{
-       Length tmp;
-
-       if (!isValidLength(data, &tmp))
-               return; // should raise an exception
-
-       val_  = tmp.val_;
-       unit_ = tmp.unit_;
-}
-
-
-string const Length::asString() const
-{
-       ostringstream os;
-       if (unit_ != UNIT_NONE)
-               os << formatFPNumber(val_) << unit_name[unit_]; // setw?
-       return os.str();
-}
-
-
-docstring const Length::asDocstring() const
-{
-       odocstringstream os;
-       if (unit_ != UNIT_NONE)
-               os << from_ascii(formatFPNumber(val_))
-                  << from_ascii(unit_name[unit_]); // setw?
-       return os.str();
-}
-
-
-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 << formatFPNumber(val_ / 100.0) << "\\textwidth";
-               break;
-       case PCW:
-               os << formatFPNumber(val_ / 100.0) << "\\columnwidth";
-               break;
-       case PPW:
-               os << formatFPNumber(val_ / 100.0) << "\\paperwidth";
-               break;
-       case PLW:
-               os << formatFPNumber(val_ / 100.0) << "\\linewidth";
-               break;
-       case PTH:
-               os << formatFPNumber(val_ / 100.0) << "\\textheight";
-               break;
-       case PPH:
-               os << formatFPNumber(val_ / 100.0) << "\\paperheight";
-               break;
-       case BLS:
-               os << formatFPNumber(val_ / 100.0) << "\\baselineskip";
-               break;
-       case UNIT_NONE:
-               break;
-       default:
-               os << 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 << formatFPNumber(val_) << "pt";
-               break;
-       case MM:
-       case CM:
-       case PC:
-       case IN:
-       case EX:
-       case EM:
-               os << formatFPNumber(val_) << unit_name[unit_];
-               break;
-       case CC:
-               os << formatFPNumber(val_ / 12.0) << "pt";
-               break;
-       case MU:
-               os << formatFPNumber(val_ / 18.0) << "em";
-               break;
-       case PTW:
-       case PPW:
-       case PLW:
-       case PCW:
-       case PTH:
-       case PPH:
-       case BLS:
-               // what it's a percentage of probably won't make sense for HTML,
-               // so we'll assume people have chosen these appropriately
-               os << formatFPNumber(val_) << '%';
-               break;
-       case SP:
-       case UNIT_NONE:
-               break;
-       }
-       return os.str();
-}
-
-
-double Length::value() const
-{
-       return val_;
-}
-
-
-Length::UNIT Length::unit() const
-{
-       return unit_;
-}
-
-
-void Length::value(double v)
-{
-       val_ = v;
-}
-
-
-void Length::unit(Length::UNIT u)
-{
-       unit_ = u;
-}
-
-
-bool Length::zero() const
-{
-       return val_ == 0.0;
-}
-
-
-bool Length::empty() const
-{
-       return unit_ == Length::UNIT_NONE;
-}
-
-
-int Length::inPixels(int text_width, int em_width_base) const
-{
-       // Zoom factor specified by user in percent
-       double const zoom = lyxrc.currentZoom / 100.0; // [percent]
-
-       // DPI setting for monitor: pixels/inch
-       double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
-
-       double const em_width_in = (em_width_base > 0)
-               ? em_width_base / (zoom * dpi)
-               : 10.0/72.27;
-       // A different estimate for em_width is
-       // theFontMetrics(FontInfo(sane_font)).em()
-       // but this estimate might not be more accurate as the screen font
-       // is different then the latex font.
-
-       // Pixel values are scaled so that the ratio
-       // between lengths and font sizes on the screen
-       // is the same as on paper.
-
-       double const text_width_in = text_width / (zoom * dpi);
-       double const result = zoom * dpi * inInch(text_width_in, em_width_in);
-       return support::iround(result);
-}
-
-
-double Length::inInch(double text_width, double em_width) const
-{
-       double result = 0.0;
-
-       switch (unit_) {
-       case Length::SP:
-               // Scaled point: sp = 1/65536 pt
-               result = val_ / (72.27 * 65536); // 4736286.7
-               break;
-       case Length::PT:
-               // Point: 1 pt = 1/72.27 inch
-               result = val_ / 72.27; // 72.27
-               break;
-       case Length::BP:
-               // Big point: 1 bp = 1/72 inch
-               result = val_ / 72; // 72
-               break;
-       case Length::DD:
-               // Didot: 1157dd = 1238 pt?
-               result = val_ / (72.27 / (0.376 * 2.845)); // 67.559735
-               break;
-       case Length::MM:
-               // Millimeter: 1 mm = 1/25.4 inch
-               result = val_ / 25.4; // 25.4
-               break;
-       case Length::PC:
-               // Pica: 1 pc = 12 pt
-               result = val_ / (72.27 / 12); // 6.0225
-               break;
-       case Length::CC:
-               // Cicero: 1 cc = 12 dd
-               result = val_
-                       / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
-               break;
-       case Length::CM:
-               // Centimeter: 1 cm = 1/2.54 inch
-               result = val_ / 2.54; // 2.54
-               break;
-       case Length::IN:
-               // Inch
-               result = val_;
-               break;
-       case Length::EX:
-               // Ex: The height of an "x"
-               // 0.4305 is the ration between 1ex and 1em in cmr10
-               result = val_ * em_width * 0.4305;
-               break;
-       case Length::EM:
-               // Em: The width of an "m"
-               result = val_ * em_width;
-               break;
-       case Length::MU:
-               // math unit = 1/18em
-               result = val_ * em_width / 18;
-               break;
-       case Length::PCW: // Always % of workarea
-       case Length::PTW:
-       case Length::PLW:
-               result = val_ * text_width / 100;
-               break;
-       case Length::PPW:
-               // paperwidth/textwidth is 1.7 for A4 paper with default margins
-               result = val_ * text_width * 1.7 / 100;
-               break;
-       case Length::PTH:
-               result = val_ * text_width * 1.787 / 100;
-               break;
-       case Length::PPH:
-               result = val_ * text_width * 2.2 / 100;
-               break;
-       case Length::BLS:
-               // baselineskip is approximately 1.2 times the font size for the cmr fonts
-               // The value actually depends on the current paragraph (see TextMetrics::setRowHeight),
-               // but we do not have this information here.
-               result = val_ * em_width * 1.2 / 100;
-               break;
-       case Length::UNIT_NONE:
-               result = 0;  // this cannot happen
-               break;
-       }
-       return result;
-}
-
-
-int Length::inBP() const
-{
-       // return any Length value as a one with
-       // the PostScript point, called bp (big points)
-
-       double const text_width_in = 210.0 / 2.54; // assume A4
-       double const em_width_in = 10.0 / 72.27;
-       double const result = 72.0 * inInch(text_width_in, em_width_in);
-       return support::iround(result);
-}
-
-
-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();
-}
-
-
-bool operator!=(Length const & l1, Length const & l2)
-{
-       return !(l1 == l2);
-}
-
-
-/////////////////////////////////////////////////////////////////////
-//
-// GlueLength
-//
-/////////////////////////////////////////////////////////////////////
-
-
-GlueLength::GlueLength(Length const & len)
-       : len_(len)
-{}
-
-
-GlueLength::GlueLength(Length const & len, Length const & plus,
-               Length const & minus)
-       : len_(len), plus_(plus), minus_(minus)
-{}
-
-
-GlueLength::GlueLength(string const & data)
-{
-       if (!isValidGlueLength(data, this))
-               LYXERR0("Invalid glue length " + data);
-}
-
-
-string const GlueLength::asString() const
-{
-       if (len_.empty())
-               return string();
-
-       ostringstream buffer;
-
-       buffer << formatFPNumber(len_.value());
-
-       if (plus_.zero() && minus_.zero()) {
-               buffer << unit_name[len_.unit()];
-               return buffer.str();
-       }
-
-       // just len and plus
-       if (minus_.zero()) {
-               if (len_.unit() != plus_.unit())
-                       buffer << unit_name[len_.unit()];
-               buffer << '+' << formatFPNumber(plus_.value());
-               buffer << unit_name[plus_.unit()];
-               return buffer.str();
-       }
-
-       // just len and minus
-       if (plus_.zero()) {
-               if (len_.unit() != minus_.unit())
-                       buffer << unit_name[len_.unit()];
-               buffer << '-' << formatFPNumber(minus_.value());
-               buffer << unit_name[minus_.unit()];
-               return buffer.str();
-       }
-
-       // ok, len, plus AND minus
-
-       // len+-
-       if (minus_ == plus_) {
-               if (len_.unit() != minus_.unit())
-                       buffer << unit_name[len_.unit()];
-               buffer << "+-" << formatFPNumber(minus_.value());
-               buffer << unit_name[minus_.unit()];
-               return buffer.str();
-       }
-
-       // this is so rare a case, why bother minimising units ?
-
-       buffer << unit_name[len_.unit()];
-       buffer << '+' << formatFPNumber(plus_.value()) << unit_name[plus_.unit()];
-       buffer << '-' << formatFPNumber(minus_.value()) << unit_name[minus_.unit()];
-
-       return buffer.str();
-}
-
-
-string const GlueLength::asLatexString() const
-{
-       ostringstream buffer;
-       // use Length::asLatexString() to handle also the percent lengths
-       buffer << len_.Length::asLatexString();
-       if (!plus_.zero())
-               buffer << " plus " << plus_.Length::asLatexString();
-       if (!minus_.zero())
-               buffer << " minus " << minus_.Length::asLatexString();
-       return buffer.str();
-}
-
-
-Length const & GlueLength::len() const
-{
-       return len_;
-}
-
-
-Length const & GlueLength::plus() const
-{
-       return plus_;
-}
-
-
-Length const & GlueLength::minus() const
-{
-       return minus_;
-}
-
-
-bool operator==(GlueLength const & l1, GlueLength const & l2)
-{
-       return l1.len() == l2.len()
-                && l1.plus() == l2.plus()
-                && l1.minus() == l2.minus();
-}
-
-
-bool operator!=(GlueLength const & l1, GlueLength const & l2)
-{
-       return !(l1 == l2);
-}
-
-
-} // namespace lyx
diff --git a/src/Length.h b/src/Length.h
deleted file mode 100644 (file)
index aaf1923..0000000
+++ /dev/null
@@ -1,211 +0,0 @@
-// -*- C++ -*-
-/**
- * \file Length.h
- * This file is part of LyX, the document processor.
- * Licence details can be found in the file COPYING.
- *
- * \author Matthias Ettrich
- * \author Lars Gullik Bjønnes
- * \author Jean-Marc Lasgouttes
- * \author John Levon
- *
- * Full author contact details are available in file CREDITS.
- */
-
-#ifndef LENGTH_H
-#define LENGTH_H
-
-#include "support/strfwd.h"
-
-
-namespace lyx {
-
-// Solaris/x86 version 9 and earlier define these
-#undef PC
-#undef SP
-
-/////////////////////////////////////////////////////////////////////
-//
-// Length
-//
-/////////////////////////////////////////////////////////////////////
-
-
-/**
- * Length - Represents latex length measurement
- */
-class Length {
-public:
-       /// length units
-       enum UNIT {
-               BP, ///< Big point (72bp = 1in), also PostScript point
-               CC, ///< Cicero = 12dd = 4.531mm
-               CM, ///< Centimeter = 10mm = 2.371pc
-               DD, ///< Didot point = 1/72 of a French inch, = 0.376mm
-               EM, ///< Width of capital "M" in current font.
-               EX, ///< Height of a small "x" for the current font.
-               IN, ///< Inch = 25.4mm = 72.27pt = 6.022pc
-               MM, ///< Millimeter = 2.845pt
-               MU, ///< Math unit (18mu = 1em) for positioning in math mode
-               PC, ///< Pica = 12pt = 4.218mm
-               PT, ///< Point = 1/72.27in = 0.351mm
-               SP, ///< Scaled point (65536sp = 1pt) TeX's smallest unit.
-               PTW, //< Percent of TextWidth
-               PCW, //< Percent of ColumnWidth
-               PPW, //< Percent of PageWidth
-               PLW, //< Percent of LineWidth
-               PTH, //< Percent of TextHeight          // Herbert 2002-05-16
-               PPH, //< Percent of PaperHeight         // Herbert 2002-05-16
-               BLS, //< Percent of BaselineSkip        // uwestoehr 2017-04-01
-               UNIT_NONE ///< no unit
-       };
-
-       ///
-       Length();
-       ///
-       Length(double v, Length::UNIT u);
-
-       /// "data" must be a decimal number, followed by a unit
-       explicit Length(std::string const & data);
-
-       ///
-       double value() const;
-       ///
-       Length::UNIT unit() const;
-       ///
-       void value(double);
-       ///
-       void unit(Length::UNIT unit);
-       ///
-       bool zero() const;
-       ///
-       bool empty() const;
-       /// return string representation
-       std::string const asString() const;
-       /// return string representation
-       docstring const asDocstring() const;
-       /// return string representation for LaTeX
-       std::string const asLatexString() const;
-       /// return string representation for HTML
-       std::string const asHTMLString() const;
-       /** return the on-screen size of this length.
-        *
-        *      If the second argument is not provided, then the unit EM will
-        *      only be approximated. It is better if possible to use
-        *      FontMetrics::em() to get this value.
-        */
-       int inPixels(int text_width, int em_width = 0) const;
-
-       /// return the value in Big Postscript points.
-       /// Caution: Inaccurate for em, ex, mu and percent units.
-       int inBP() const;
-
-       /// return the default unit (centimeter or inch)
-       static UNIT defaultUnit();
-
-       friend bool isValidLength(std::string const & data, Length * result);
-
-private:
-       /// Convert value to inch for text width and em width given in inch
-       double inInch(double text_width, double em_width) const;
-       ///
-       double val_;
-       ///
-       Length::UNIT unit_;
-};
-
-///
-bool operator==(Length const & l1, Length const & l2);
-///
-bool operator!=(Length const & l1, Length const & l2);
-/** Test whether \p data represents a valid length.
- *
- * \returns whether \p data is a valid length
- * \param data Length in LyX format. Since the only difference between LyX
- * and LaTeX format is the representation of length variables as units (e.g.
- * \c text% vs. \c \\textwidth) you can actually use this function as well
- * for testing LaTeX lengths as long as they only contain real units like pt.
- * \param result Pointer to a Length variable. If \p result is not 0 and
- * \p data is valid, the length represented by it is stored into \p result.
- */
-bool isValidLength(std::string const & data, Length * result = 0);
-/// return the LyX name of the given unit number
-char const * stringFromUnit(int unit);
-
-
-/////////////////////////////////////////////////////////////////////
-//
-// GlueLength
-//
-/////////////////////////////////////////////////////////////////////
-
-class GlueLength {
-public:
-       ///
-       GlueLength() {}
-       ///
-       explicit GlueLength(Length const & len);
-       ///
-       GlueLength(Length const & len,
-                     Length const & plus,
-                     Length const & minus);
-
-       /** "data" must be a decimal number, followed by a unit, and
-         optional "glue" indicated by "+" and "-".  You may abbreviate
-         reasonably.  Examples:
-         1.2 cm  //  4mm +2pt  //  2cm -4mm +2mm  //  4+0.1-0.2cm
-         The traditional Latex format is also accepted, like
-         4cm plus 10pt minus 10pt */
-       explicit GlueLength(std::string const & data);
-
-       ///
-       Length const & len() const;
-       ///
-       Length const & plus() const;
-       ///
-       Length const & minus() const;
-
-
-       /// conversion
-       std::string const asString() const;
-       ///
-       std::string const asLatexString() const;
-
-       friend bool isValidGlueLength(std::string const & data,
-                                     GlueLength* result);
-
-private:
-       /// the normal vlaue
-       Length len_;
-       /// extra stretch
-       Length plus_;
-       /// extra shrink
-       Length minus_;
-};
-
-///
-bool operator==(GlueLength const & l1, GlueLength const & l2);
-///
-bool operator!=(GlueLength const & l1, GlueLength const & l2);
-/** If "data" is valid, the length represented by it is
-    stored into "result", if that is not 0. */
-bool isValidGlueLength(std::string const & data, GlueLength * result = 0);
-
-/// the number of units possible
-extern int const num_units;
-
-/**
- * array of unit names
- *
- * FIXME: I am not sure if "mu" should be possible to select (Lgb)
- */
-extern char const * const unit_name[];
-extern char const * const unit_name_gui[];
-
-/// return the unit given a string representation such as "cm"
-Length::UNIT unitFromString(std::string const & data);
-
-
-} // namespace lyx
-
-#endif // LENGTH_H
index ed7511a6d11a9d454fdff3e0f079d9a79a6ec85a..955826bcf6c57034210d3c879d3217ff18e4dbd4 100644 (file)
@@ -18,9 +18,9 @@
 #ifndef LYXRC_H
 #define LYXRC_H
 
-#include "Length.h"
 #include "LyX.h"
 
+#include "support/Length.h"
 #include "support/strfwd.h"
 #include "support/userinfo.h"
 
index 4a74c6eef46619b2482c78ea1664b3addd00e844..be8e478007e4a70e75aabc13f90ed37e3323bcbe 100644 (file)
@@ -147,8 +147,6 @@ SOURCEFILESCORE = \
        LaTeXPackages.cpp \
        LayoutFile.cpp \
        LayoutModuleList.cpp \
-       Length.cpp \
-       lengthcommon.cpp \
        Lexer.cpp \
        LyX.cpp \
        LyXAction.cpp \
@@ -251,7 +249,6 @@ HEADERFILESCORE = \
        LayoutEnums.h \
        LayoutFile.h \
        LayoutModuleList.h \
-       Length.h \
        Lexer.h \
        LyXAction.h \
        lyxfind.h \
@@ -733,20 +730,15 @@ check_ExternalTransforms_SOURCES = \
        tests/boost.cpp
 check_ExternalTransforms_LYX_OBJS = \
        graphics/GraphicsParams.o \
-       insets/ExternalTransforms.o \
-       Length.o \
-       lengthcommon.o
+       insets/ExternalTransforms.o
 
 check_Length_CPPFLAGS = $(AM_CPPFLAGS)
-check_Length_LDADD = $(check_Length_LYX_OBJS) $(TESTS_LIBS)
+check_Length_LDADD = $(TESTS_LIBS)
 check_Length_LDFLAGS = $(QT_LDFLAGS) $(ADD_FRAMEWORKS)
 check_Length_SOURCES = \
        tests/check_Length.cpp \
        tests/dummy_functions.cpp \
        tests/boost.cpp
-check_Length_LYX_OBJS = \
-       Length.o \
-       lengthcommon.o
 
 check_ListingsCaption_CPPFLAGS = $(AM_CPPFLAGS)
 check_ListingsCaption_LDADD = $(check_ListingsCaption_LYX_OBJS) $(TESTS_LIBS)
index 1c8f03057ae9f2331488edcf5569f22dfb4a222f..133ddd15b8be95008949d7b5e2223a718297eac0 100644 (file)
@@ -30,7 +30,6 @@
 #include "Language.h"
 #include "LaTeXFeatures.h"
 #include "Layout.h"
-#include "Length.h"
 #include "Font.h"
 #include "FontList.h"
 #include "LyXRC.h"
@@ -62,6 +61,7 @@
 #include "support/ExceptionMessage.h"
 #include "support/gettext.h"
 #include "support/lassert.h"
+#include "support/Length.h"
 #include "support/lstrings.h"
 #include "support/textutils.h"
 #include "output_docbook.h"
index ccb131c9c6558cd89f0b6701a13c5fa425aac4f1..b4608e0b3bf62936e6882da6c05fe00700ca8448 100644 (file)
 #define PARAGRAPHPARAMETERS_H
 
 #include "LayoutEnums.h"
-#include "Length.h"
 #include "Spacing.h"
 
 #include "support/debug.h"
 #include "support/types.h"
 #include "support/docstring.h"
+#include "support/Length.h"
 
 
 namespace lyx {
index 60705c0b3470025d2b8be59eaaecfedac8fb4009..e03c2cb57c630edcafd2288c21c4a7ea07394a86 100644 (file)
@@ -37,7 +37,6 @@
 #include "InsetList.h"
 #include "Language.h"
 #include "Layout.h"
-#include "Length.h"
 #include "Lexer.h"
 #include "lyxfind.h"
 #include "LyXRC.h"
@@ -65,6 +64,7 @@
 #include "support/docstream.h"
 #include "support/gettext.h"
 #include "support/lassert.h"
+#include "support/Length.h"
 #include "support/lstrings.h"
 #include "support/lyxalgo.h"
 #include "support/lyxtime.h"
index a2d4a3d398e408d3c106ae9d06c7c0b68930a4db..d176850c876d48fd64e261deab215c1f9ae7fb29 100644 (file)
 #include "BufferParams.h"
 #include "BufferView.h"
 #include "support/gettext.h"
-#include "Length.h"
 #include "Text.h"
 #include "TextMetrics.h" // for defaultRowHeight()
 
 #include "support/convert.h"
+#include "support/Length.h"
 #include "support/lstrings.h"
 
 #include "support/lassert.h"
index 2bce154e33314ee77fa08031a39b767b8d47b892..7cd1a2b131e51bca386728a2918f2a33983faf57 100644 (file)
@@ -12,7 +12,7 @@
 #ifndef VSPACE_H
 #define VSPACE_H
 
-#include "Length.h"
+#include "support/Length.h"
 
 
 namespace lyx {
index 67a1b0c52b1d6e86a54e3ee6316ac4ace039bd89..5b504ddbda55283790a0af159b5b8c0c7696a736 100644 (file)
 #include "ColorCache.h"
 #include "ColorSet.h"
 #include "LengthCombo.h"
-#include "Length.h"
 #include "qt_helpers.h"
 #include "Validator.h"
 
 #include "insets/InsetBox.h"
 
 #include "support/gettext.h"
+#include "support/Length.h"
 #include "support/lstrings.h"
 
 #include <QComboBox>
index d850c2d8a5b8d9f1e4a3029086ca9b49be1740a1..eb71aad4d1d9df0dd9ac03c47e24facbb6ca1cfb 100644 (file)
@@ -17,7 +17,6 @@
 #include "Buffer.h"
 #include "FuncRequest.h"
 #include "support/gettext.h"
-#include "Length.h"
 #include "LyXRC.h"
 
 #include "insets/ExternalSupport.h"
@@ -31,6 +30,7 @@
 
 #include "support/convert.h"
 #include "support/filetools.h"
+#include "support/Length.h"
 #include "support/lstrings.h"
 #include "support/lyxlib.h"
 #include "support/os.h"
index e0a1b20b322080ff5a6797323b02a33c266afd3a..142f433759bce2a660be0b8149b57b559f1b53b7 100644 (file)
@@ -22,7 +22,6 @@
 #include "Buffer.h"
 #include "FuncRequest.h"
 #include "LengthCombo.h"
-#include "Length.h"
 #include "LyXRC.h"
 
 #include "graphics/epstools.h"
@@ -36,6 +35,7 @@
 #include "support/debug.h"
 #include "support/filetools.h"
 #include "support/gettext.h"
+#include "support/Length.h"
 #include "support/lstrings.h"
 #include "support/os.h"
 #include "support/Package.h"
index 2545a9660634a9baa792110229c7a2cf38210691..eb1a38769b083735b222546a39751cfb3e408372 100644 (file)
@@ -14,7 +14,7 @@
 
 #include <QComboBox>
 
-#include "Length.h"
+#include "support/Length.h"
 
 
 namespace lyx {
index bf77795918b637a8f4c6b24b049102cfedb2533b..cce7cf5f75a49541d778e80688c7c961becaaba7 100644 (file)
 #ifndef VALIDATOR_H
 #define VALIDATOR_H
 
-#include "Length.h"
 #include "Dialog.h" // KernelDocType
 
+#include "support/Length.h"
+
 #include <QValidator>
 
 class QWidget;
index 26c0df0ffcc5b6a6d5dea06339521b4822bdd448..d3da0ee7dea70816cc277eda00b6d019f2025c44 100644 (file)
 #include "BufferParams.h"
 #include "FloatList.h"
 #include "Language.h"
-#include "Length.h"
 #include "TextClass.h"
 
 #include "support/convert.h"
 #include "support/debug.h"
 #include "support/gettext.h"
+#include "support/Length.h"
 #include "support/lstrings.h"
 #include "support/lyxalgo.h"
 #include "support/os.h"
index 754b6bf9138fae5329c2c8342b51825c11ef8bbe..92872e08087cbda02b56f87f70a819adada52ebd 100644 (file)
@@ -14,7 +14,7 @@
 #define QTHELPERS_H
 
 #include "ColorSet.h"
-#include "Length.h"
+#include "support/Length.h"
 #include "support/qstring_helpers.h"
 #include "support/filetools.h"
 #include "qt_i18n.h"
index 052b006bff719aa42c1977904be4d2115ec3ea29..e05bb67c5a4c4e12f7a97959b14bfcc1a2e8ae70 100644 (file)
@@ -14,9 +14,8 @@
 #ifndef GRAPHICSPARAMS_H
 #define GRAPHICSPARAMS_H
 
-#include "Length.h"
-
 #include "support/FileName.h"
+#include "support/Length.h"
 
 #include <string>
 #include <iosfwd>
index d6735b60b5cbf1a7165155ad6eb520ea8e7f97d4..6d0f9afedbb2a2bf22daab637f2bdd5654675261 100644 (file)
 #ifndef EXTERNALTRANSFORMS_H
 #define EXTERNALTRANSFORMS_H
 
-#include "Length.h"
-
 #include "graphics/GraphicsParams.h"
 
+#include "support/Length.h"
 #include "support/unique_ptr.h"
 
 #include <boost/any.hpp>
index 418a24277200f67a479ed11a383780a73b2f36be..3398a52ed600962a172805ad8760b68a0817c867 100644 (file)
@@ -15,7 +15,8 @@
 #define INSETBOX_H
 
 #include "InsetCollapsible.h"
-#include "Length.h"
+
+#include "support/Length.h"
 
 
 namespace lyx {
index 62efa84dc02978e1be91fcb6873c47ca05526bad..99fcf3d6fc56d2d3369506d316687acccb6ac430 100644 (file)
@@ -62,7 +62,6 @@ TODO
 #include "FuncStatus.h"
 #include "InsetIterator.h"
 #include "LaTeXFeatures.h"
-#include "Length.h"
 #include "Lexer.h"
 #include "MetricsInfo.h"
 #include "Mover.h"
@@ -86,6 +85,7 @@ TODO
 #include "support/ExceptionMessage.h"
 #include "support/filetools.h"
 #include "support/gettext.h"
+#include "support/Length.h"
 #include "support/lyxlib.h"
 #include "support/lstrings.h"
 #include "support/os.h"
index 699d878bb147924cf2cd88baea639755ac60282b..4edf0eb1e08daae05ce7f0c7bfa5a86ede70de80 100644 (file)
@@ -25,7 +25,6 @@
 #include "LaTeXFeatures.h"
 #include "Language.h"
 #include "LayoutFile.h"
-#include "Length.h"
 #include "LyXAction.h"
 #include "LyXRC.h"
 #include "LyXVC.h"
@@ -45,6 +44,7 @@
 #include "support/FileName.h"
 #include "support/filetools.h"
 #include "support/gettext.h"
+#include "support/Length.h"
 #include "support/Messages.h"
 #include "support/lstrings.h"
 #include "support/qstring_helpers.h"
index e6964aad791cf486e1a950e55de2d543eeeb651b..8d6a4ada2f5f4b6a16c466c23d743c24ca17e71d 100644 (file)
@@ -21,7 +21,6 @@
 #include "FuncRequest.h"
 #include "FuncStatus.h"
 #include "LaTeXFeatures.h"
-#include "Length.h"
 #include "MetricsInfo.h"
 #include "OutputParams.h"
 #include "output_docbook.h"
@@ -35,6 +34,7 @@
 #include "support/debug.h"
 #include "support/docstream.h"
 #include "support/gettext.h"
+#include "support/Length.h"
 #include "support/lstrings.h"
 
 #include <cstdlib>
index 2e2f4c6e535062c07f3ec2411f518bdd730802d7..9c87a58c77cab75a2ebb39fe50af904331ffac8d 100644 (file)
@@ -13,7 +13,7 @@
 
 #include "InsetListingsParams.h"
 
-#include "Length.h"
+#include "support/Length.h"
 #include "Lexer.h"
 
 #include "support/convert.h"
index e7fc8a7d98935ddfc3274e64b556faf83dec64c9..c762e33d67e60586e694187e7cf6ee8f5524782f 100644 (file)
@@ -26,7 +26,6 @@
 #include "InsetList.h"
 #include "Language.h"
 #include "LaTeXFeatures.h"
-#include "Length.h"
 #include "LyX.h"
 #include "OutputParams.h"
 #include "xml.h"
@@ -38,6 +37,7 @@
 #include "support/debug.h"
 #include "support/docstream.h"
 #include "support/gettext.h"
+#include "support/Length.h"
 #include "support/lstrings.h"
 
 using namespace std;
index 04b515cdccf5d088e188598d3385ce2c50d061c2..5a4f46801af73c89eac00a2085716cf82db3518f 100644 (file)
@@ -22,7 +22,6 @@
 #include "FuncStatus.h"
 #include "Language.h"
 #include "LaTeXFeatures.h"
-#include "Length.h"
 #include "Lexer.h"
 #include "MetricsInfo.h"
 #include "OutputParams.h"
@@ -33,6 +32,7 @@
 #include "support/docstream.h"
 #include "support/gettext.h"
 #include "support/lassert.h"
+#include "support/Length.h"
 #include "support/lstrings.h"
 
 #include "frontends/Application.h"
index 1111aa1e828d7215d56104e429930ad511f1a08f..0ebcec1f5480fa1ebf737882118be5353c8d59d2 100644 (file)
@@ -16,7 +16,8 @@
 #define INSET_SPACE_H
 
 #include "Inset.h"
-#include "Length.h"
+
+#include "support/Length.h"
 
 
 namespace lyx {
index d917ab8370d085fe799a7379b595a13368f30436..028da979694400dacf9ba5bf24e235d823f534ed 100644 (file)
@@ -25,7 +25,8 @@
 #define INSET_TABULAR_H
 
 #include "InsetText.h"
-#include "Length.h"
+
+#include "support/Length.h"
 
 #include <climits>
 #include <iosfwd>
index 934e3bd101e3a1e03440b218ae1c7bee24f3752c..5619dc042b73f491accce98d5c5d986ad1a3dcbc 100644 (file)
@@ -13,7 +13,8 @@
 #define INSETWRAP_H
 
 #include "InsetCaptionable.h"
-#include "Length.h"
+
+#include "support/Length.h"
 
 
 namespace lyx {
diff --git a/src/lengthcommon.cpp b/src/lengthcommon.cpp
deleted file mode 100644 (file)
index 4478d84..0000000
+++ /dev/null
@@ -1,367 +0,0 @@
-/**
- * \file lengthcommon.cpp
- * This file is part of LyX, the document processor.
- * Licence details can be found in the file COPYING.
- *
- * \author Lars Gullik Bjønnes
- * \author Matthias Ettrich
- * \author John Levon
- *
- * Full author contact details are available in file CREDITS.
- */
-
-#include <config.h>
-
-#include "Length.h"
-
-#include "support/convert.h"
-#include "support/gettext.h"
-#include "support/lassert.h"
-#include "support/lstrings.h"
-
-using namespace std;
-using namespace lyx::support;
-
-
-namespace lyx {
-
-// I am not sure if "mu" should be possible to select (Lgb)
-
-// the latex units
-char const * const unit_name[] = {
-       "bp", "cc", "cm", "dd", "em", "ex", "in", "mm", "mu",
-       "pc", "pt", "sp",
-       "text%",  "col%", "page%", "line%",
-       "theight%", "pheight%", "baselineskip%", "" };
-
-int const num_units = int(sizeof(unit_name) / sizeof(unit_name[0]) - 1);
-
-// the LyX gui units
-char const * const unit_name_gui[] = {
-       N_("bp"), N_("cc[[unit of measure]]"), N_("cm"), N_("dd"), N_("em"),
-       N_("ex"), N_("in[[unit of measure]]"), N_("mm"), N_("mu[[unit of measure]]"), N_("pc"),
-       N_("pt"), N_("sp"), N_("Text Width %"),
-       N_("Column Width %"), N_("Page Width %"), N_("Line Width %"),
-       N_("Text Height %"), N_("Page Height %"), N_("Line Distance %"), "" };
-
-
-Length::UNIT unitFromString(string const & data)
-{
-       int i = 0;
-       while (i < num_units && data != unit_name[i])
-               ++i;
-       return static_cast<Length::UNIT>(i);
-}
-
-
-namespace {
-
-/// skip n characters of input
-inline void lyx_advance(string & data, size_t n)
-{
-       data.erase(0, n);
-}
-
-
-/// return true when the input is at the end
-inline bool isEndOfData(string const & data)
-{
-       return ltrim(data).empty();
-}
-
-
-/**
- * nextToken -  return the next token in the input
- * @param data input string
- * @param number_index the current position in the number array
- * @param unit_index the current position in the unit array
- * @return a char representing the type of token returned
- *
- * The possible return values are :
- *     +       stretch indicator for glue length
- *     -       shrink indicator for glue length
- *     n       a numeric value (stored in number array)
- *     u       a unit type (stored in unit array)
- *     E       parse error
- */
-char nextToken(string & data, double * number, int & number_index,
-               Length::UNIT * unit, int & unit_index)
-{
-       data = ltrim(data);
-
-       if (data.empty())
-               return '\0';
-
-       if (data[0] == '+') {
-               lyx_advance(data, 1);
-               return '+';
-       }
-
-       if (prefixIs(data, "plus")) {
-               lyx_advance(data, 4);
-               return '+';
-       }
-
-       if (data[0] == '-') {
-               lyx_advance(data, 1);
-               return '-';
-       }
-
-       if (prefixIs(data, "minus")) {
-               lyx_advance(data, 5);
-               return '-';
-       }
-
-       size_t i = data.find_first_not_of("0123456789.");
-
-       if (i != 0) {
-               if (number_index > 3)
-                       return 'E';
-
-               string buffer;
-
-               // we have found some number
-               if (i == string::npos) {
-                       buffer = data;
-                       i = data.size() + 1;
-               } else {
-                       buffer = data.substr(0, i);
-               }
-
-               lyx_advance(data, i);
-
-               if (isStrDbl(buffer)) {
-                       number[number_index] = convert<double>(buffer);
-                       ++number_index;
-                       return 'n';
-               }
-               return 'E';
-       }
-
-       i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
-       if (i != 0) {
-               if (unit_index > 3)
-                       return 'E';
-
-               string buffer;
-
-               // we have found some alphabetical string
-               if (i == string::npos) {
-                       buffer = data;
-                       i = data.size() + 1;
-               } else {
-                       buffer = data.substr(0, i);
-               }
-
-               // possibly we have "mmplus" string or similar
-               if (buffer.size() > 5 &&
-                               (buffer.substr(2, 4) == string("plus") ||
-                                buffer.substr(2, 5) == string("minus")))
-               {
-                       lyx_advance(data, 2);
-                       unit[unit_index] = unitFromString(buffer.substr(0, 2));
-               } else {
-                       lyx_advance(data, i);
-                       unit[unit_index] = unitFromString(buffer);
-               }
-
-               if (unit[unit_index] != Length::UNIT_NONE) {
-                       ++unit_index;
-                       return 'u';
-               }
-               return 'E';  // Error
-       }
-       return 'E';  // Error
-}
-
-
-/// latex representation of a vspace
-struct LaTeXLength {
-       char const * pattern;
-       int  plus_val_index;
-       int  minus_val_index;
-       int  plus_uni_index;
-       int  minus_uni_index;
-};
-
-
-/// the possible formats for a vspace string
-LaTeXLength table[] = {
-       { "nu",       0, 0, 0, 0 },
-       { "nu+nu",    2, 0, 2, 0 },
-       { "nu+nu-nu", 2, 3, 2, 3 },
-       { "nu+-nu",   2, 2, 2, 2 },
-       { "nu-nu",    0, 2, 0, 2 },
-       { "nu-nu+nu", 3, 2, 3, 2 },
-       { "nu-+nu",   2, 2, 2, 2 },
-       { "n+nu",     2, 0, 1, 0 },
-       { "n+n-nu",   2, 3, 1, 1 },
-       { "n+-nu",    2, 2, 1, 1 },
-       { "n-nu",     0, 2, 0, 1 },
-       { "n-n+nu",   3, 2, 1, 1 },
-       { "n-+nu",    2, 2, 1, 1 },
-       { "",         0, 0, 0, 0 }   // sentinel, must be empty
-};
-
-
-} // namespace
-
-
-const char * stringFromUnit(int unit)
-{
-       if (unit < 0 || unit > num_units)
-               return nullptr;
-       return unit_name[unit];
-}
-
-
-bool isValidGlueLength(string const & data, GlueLength * result)
-{
-       // This parser is table-driven.  First, it constructs a "pattern"
-       // that describes the sequence of tokens in "data".  For example,
-       // "n-nu" means: number, minus sign, number, unit.  As we go along,
-       // numbers and units are stored into static arrays.  Then, "pattern"
-       // is searched in the "table".  If it is found, the associated
-       // table entries tell us which number and unit should go where
-       // in the Length structure.  Example: if "data" has the "pattern"
-       // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
-       // That means, "plus_val" is the second number that was seen
-       // in the input, "minus_val" is the third number, and "plus_uni"
-       // and "minus_uni" are the second and third units, respectively.
-       // ("val" and "uni" are always the first items seen in "data".)
-       // This is the most elegant solution I could find -- a straight-
-       // forward approach leads to very long, tedious code that would be
-       // much harder to understand and maintain. (AS)
-
-       if (data.empty()) {
-               if (result)
-                       *result = GlueLength();
-               return true;
-       }
-       string buffer = ltrim(data);
-
-       // To make isValidGlueLength recognize negative values as
-       // the first number this little hack is needed:
-       int val_sign = 1; // positive as default
-       switch (buffer[0]) {
-       case '-':
-               lyx_advance(buffer, 1);
-               val_sign = -1;
-               break;
-       case '+':
-               lyx_advance(buffer, 1);
-               break;
-       default:
-               break;
-       }
-       // end of hack
-
-       // used to return numeric values in parsing vspace
-       double number[4] = { 0, 0, 0, 0 };
-       // used to return unit types in parsing vspace
-       Length::UNIT unit[4] = {Length::UNIT_NONE, Length::UNIT_NONE,
-                               Length::UNIT_NONE, Length::UNIT_NONE};
-       int number_index = 1; // entries at index 0 are sentinels
-       int unit_index = 1;   // entries at index 0 are sentinels
-
-       // construct "pattern" from "data"
-       size_t const pattern_max_size = 20;
-       string pattern;
-       while (!isEndOfData(buffer)) {
-               if (pattern.size() > pattern_max_size)
-                       return false;
-               char const c = nextToken(buffer, number, number_index, unit,
-                               unit_index);
-               if (c == 'E')
-                       return false;
-               pattern.push_back(c);
-       }
-
-       // search "pattern" in "table"
-       size_t table_index = 0;
-       while (pattern != table[table_index].pattern) {
-               ++table_index;
-               if (!*table[table_index].pattern)
-                       return false;
-       }
-
-       // Get the values from the appropriate places.  If an index
-       // is zero, the corresponding array value is zero or UNIT_NONE,
-       // so we needn't check this.
-       if (result) {
-               result->len_.value  (number[1] * val_sign);
-               result->len_.unit   (unit[1]);
-               result->plus_.value (number[table[table_index].plus_val_index]);
-               result->plus_.unit  (unit  [table[table_index].plus_uni_index]);
-               result->minus_.value(number[table[table_index].minus_val_index]);
-               result->minus_.unit (unit  [table[table_index].minus_uni_index]);
-       }
-       return true;
-}
-
-
-bool isValidLength(string const & data, Length * result)
-{
-       // This is a trimmed down version of isValidGlueLength.
-       // The parser may seem overkill for lengths without
-       // glue, but since we already have it, using it is
-       // easier than writing something from scratch.
-       if (data.empty()) {
-               if (result)
-                       *result = Length();
-               return true;
-       }
-
-       string buffer = data;
-
-       // To make isValidLength recognize negative values
-       // this little hack is needed:
-       int val_sign = 1; // positive as default
-       switch (buffer[0]) {
-       case '-':
-               lyx_advance(buffer, 1);
-               val_sign = -1;
-               break;
-       case '+':
-               lyx_advance(buffer, 1);
-               // fall through
-       default:
-               // no action
-               break;
-       }
-       // end of hack
-
-       // used to return numeric values in parsing vspace
-       double number[4] = { 0, 0, 0, 0 };
-       // used to return unit types in parsing vspace
-       Length::UNIT unit[4] = {Length::UNIT_NONE, Length::UNIT_NONE,
-                               Length::UNIT_NONE, Length::UNIT_NONE};
-       int number_index = 1; // entries at index 0 are sentinels
-       int unit_index = 1;   // entries at index 0 are sentinels
-
-       // construct "pattern" from "data"
-       string pattern;
-       while (!isEndOfData(buffer)) {
-               if (pattern.size() > 2)
-                       return false;
-               char const token = nextToken(buffer, number,
-                               number_index, unit, unit_index);
-               if (token == 'E')
-                       return false;
-               pattern += token;
-       }
-
-       // only the most basic pattern is accepted here
-       if (pattern != "nu")
-               return false;
-
-       // It _was_ a correct length string.
-       // Store away the values we found.
-       if (result) {
-               result->val_  = number[1] * val_sign;
-               result->unit_ = unit[1];
-       }
-       return true;
-}
-
-} // namespace lyx
index 1c8efd1814fe3272e5af0410fb493a740349c694..fa7c2f2691636d3b96da1a6ce93c16ac6645c7a5 100644 (file)
@@ -13,7 +13,8 @@
 #define MATH_GRID_H
 
 #include "InsetMathNest.h"
-#include "Length.h"
+
+#include "support/Length.h"
 
 #include <map>
 
index e985ad4dc3bc1e2515fbca76dfaa6ec1ce7b8101..58d540d1fcfb27c2172af646f8b0e14fdda9cd3a 100644 (file)
@@ -13,7 +13,8 @@
 #define MATH_CHEATINSET_H
 
 #include "InsetMath.h"
-#include "Length.h"
+
+#include "support/Length.h"
 
 
 namespace lyx {
index 4f16f1640bf5b38b949c2a6c5c4f3e2d294e4c88..32147dbe2549bce617dfd281cc7d2d6b6eb63e4f 100644 (file)
@@ -13,7 +13,8 @@
 #define MATH_SPACEINSET_H
 
 #include "InsetMath.h"
-#include "Length.h"
+
+#include "support/Length.h"
 
 
 namespace lyx {
index a5be7c0af31b29182b567c08ac798a107277287d..68de02813ae9a0fcb6070f535f32fefe10dd720f 100644 (file)
 #ifndef MATH_XYMATRIX_H
 #define MATH_XYMATRIX_H
 
-#include "Length.h"
 #include "InsetMathGrid.h"
 
+#include "support/Length.h"
+
 
 namespace lyx {
 
index 45ca4296816d4161b34c2284fa595c1073df39b0..ef93cb0bf1c82187da33b1bb5e11f9d9cbebfb1c 100644 (file)
@@ -15,7 +15,6 @@
 
 #include "InsetMathFont.h"
 #include "InsetMathSymbol.h"
-#include "Length.h"
 #include "MathData.h"
 #include "MathFactory.h"
 #include "MathParser.h"
@@ -31,6 +30,7 @@
 #include "support/debug.h"
 #include "support/docstream.h"
 #include "support/lassert.h"
+#include "support/Length.h"
 #include "support/lyxlib.h"
 
 #include <map>
diff --git a/src/support/Length.cpp b/src/support/Length.cpp
new file mode 100644 (file)
index 0000000..d23ea9b
--- /dev/null
@@ -0,0 +1,463 @@
+/**
+ * \file Length.cpp
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Matthias Ettrich
+ * \author Lars Gullik Bjønnes
+ * \author Jean-Marc Lasgouttes
+ * \author Angus Leeming
+ * \author John Levon
+ * \author Dekel Tsur
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#include <config.h>
+
+#include "LyXRC.h"
+
+#include "support/debug.h"
+#include "support/docstream.h"
+#include "support/Length.h"
+#include "support/lstrings.h"
+#include "support/lyxlib.h"
+
+#include <sstream>
+#include <iomanip>
+
+using namespace std;
+using namespace lyx::support;
+
+namespace lyx {
+
+
+/////////////////////////////////////////////////////////////////////
+//
+// Length
+//
+/////////////////////////////////////////////////////////////////////
+
+Length::Length()
+       : val_(0), unit_(Length::UNIT_NONE)
+{}
+
+
+Length::Length(double v, Length::UNIT u)
+       : val_(v), unit_(u)
+{}
+
+
+Length::Length(string const & data)
+       : val_(0), unit_(Length::UNIT_NONE)
+{
+       Length tmp;
+
+       if (!isValidLength(data, &tmp))
+               return; // should raise an exception
+
+       val_  = tmp.val_;
+       unit_ = tmp.unit_;
+}
+
+
+string const Length::asString() const
+{
+       ostringstream os;
+       if (unit_ != UNIT_NONE)
+               os << formatFPNumber(val_) << unit_name[unit_]; // setw?
+       return os.str();
+}
+
+
+docstring const Length::asDocstring() const
+{
+       odocstringstream os;
+       if (unit_ != UNIT_NONE)
+               os << from_ascii(formatFPNumber(val_))
+                  << from_ascii(unit_name[unit_]); // setw?
+       return os.str();
+}
+
+
+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 << formatFPNumber(val_ / 100.0) << "\\textwidth";
+               break;
+       case PCW:
+               os << formatFPNumber(val_ / 100.0) << "\\columnwidth";
+               break;
+       case PPW:
+               os << formatFPNumber(val_ / 100.0) << "\\paperwidth";
+               break;
+       case PLW:
+               os << formatFPNumber(val_ / 100.0) << "\\linewidth";
+               break;
+       case PTH:
+               os << formatFPNumber(val_ / 100.0) << "\\textheight";
+               break;
+       case PPH:
+               os << formatFPNumber(val_ / 100.0) << "\\paperheight";
+               break;
+       case BLS:
+               os << formatFPNumber(val_ / 100.0) << "\\baselineskip";
+               break;
+       case UNIT_NONE:
+               break;
+       default:
+               os << 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 << formatFPNumber(val_) << "pt";
+               break;
+       case MM:
+       case CM:
+       case PC:
+       case IN:
+       case EX:
+       case EM:
+               os << formatFPNumber(val_) << unit_name[unit_];
+               break;
+       case CC:
+               os << formatFPNumber(val_ / 12.0) << "pt";
+               break;
+       case MU:
+               os << formatFPNumber(val_ / 18.0) << "em";
+               break;
+       case PTW:
+       case PPW:
+       case PLW:
+       case PCW:
+       case PTH:
+       case PPH:
+       case BLS:
+               // what it's a percentage of probably won't make sense for HTML,
+               // so we'll assume people have chosen these appropriately
+               os << formatFPNumber(val_) << '%';
+               break;
+       case SP:
+       case UNIT_NONE:
+               break;
+       }
+       return os.str();
+}
+
+
+double Length::value() const
+{
+       return val_;
+}
+
+
+Length::UNIT Length::unit() const
+{
+       return unit_;
+}
+
+
+void Length::value(double v)
+{
+       val_ = v;
+}
+
+
+void Length::unit(Length::UNIT u)
+{
+       unit_ = u;
+}
+
+
+bool Length::zero() const
+{
+       return val_ == 0.0;
+}
+
+
+bool Length::empty() const
+{
+       return unit_ == Length::UNIT_NONE;
+}
+
+
+int Length::inPixels(int text_width, int em_width_base) const
+{
+       // Zoom factor specified by user in percent
+       double const zoom = lyxrc.currentZoom / 100.0; // [percent]
+
+       // DPI setting for monitor: pixels/inch
+       double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
+
+       double const em_width_in = (em_width_base > 0)
+               ? em_width_base / (zoom * dpi)
+               : 10.0/72.27;
+       // A different estimate for em_width is
+       // theFontMetrics(FontInfo(sane_font)).em()
+       // but this estimate might not be more accurate as the screen font
+       // is different then the latex font.
+
+       // Pixel values are scaled so that the ratio
+       // between lengths and font sizes on the screen
+       // is the same as on paper.
+
+       double const text_width_in = text_width / (zoom * dpi);
+       double const result = zoom * dpi * inInch(text_width_in, em_width_in);
+       return support::iround(result);
+}
+
+
+double Length::inInch(double text_width, double em_width) const
+{
+       double result = 0.0;
+
+       switch (unit_) {
+       case Length::SP:
+               // Scaled point: sp = 1/65536 pt
+               result = val_ / (72.27 * 65536); // 4736286.7
+               break;
+       case Length::PT:
+               // Point: 1 pt = 1/72.27 inch
+               result = val_ / 72.27; // 72.27
+               break;
+       case Length::BP:
+               // Big point: 1 bp = 1/72 inch
+               result = val_ / 72; // 72
+               break;
+       case Length::DD:
+               // Didot: 1157dd = 1238 pt?
+               result = val_ / (72.27 / (0.376 * 2.845)); // 67.559735
+               break;
+       case Length::MM:
+               // Millimeter: 1 mm = 1/25.4 inch
+               result = val_ / 25.4; // 25.4
+               break;
+       case Length::PC:
+               // Pica: 1 pc = 12 pt
+               result = val_ / (72.27 / 12); // 6.0225
+               break;
+       case Length::CC:
+               // Cicero: 1 cc = 12 dd
+               result = val_
+                       / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
+               break;
+       case Length::CM:
+               // Centimeter: 1 cm = 1/2.54 inch
+               result = val_ / 2.54; // 2.54
+               break;
+       case Length::IN:
+               // Inch
+               result = val_;
+               break;
+       case Length::EX:
+               // Ex: The height of an "x"
+               // 0.4305 is the ration between 1ex and 1em in cmr10
+               result = val_ * em_width * 0.4305;
+               break;
+       case Length::EM:
+               // Em: The width of an "m"
+               result = val_ * em_width;
+               break;
+       case Length::MU:
+               // math unit = 1/18em
+               result = val_ * em_width / 18;
+               break;
+       case Length::PCW: // Always % of workarea
+       case Length::PTW:
+       case Length::PLW:
+               result = val_ * text_width / 100;
+               break;
+       case Length::PPW:
+               // paperwidth/textwidth is 1.7 for A4 paper with default margins
+               result = val_ * text_width * 1.7 / 100;
+               break;
+       case Length::PTH:
+               result = val_ * text_width * 1.787 / 100;
+               break;
+       case Length::PPH:
+               result = val_ * text_width * 2.2 / 100;
+               break;
+       case Length::BLS:
+               // baselineskip is approximately 1.2 times the font size for the cmr fonts
+               // The value actually depends on the current paragraph (see TextMetrics::setRowHeight),
+               // but we do not have this information here.
+               result = val_ * em_width * 1.2 / 100;
+               break;
+       case Length::UNIT_NONE:
+               result = 0;  // this cannot happen
+               break;
+       }
+       return result;
+}
+
+
+int Length::inBP() const
+{
+       // return any Length value as a one with
+       // the PostScript point, called bp (big points)
+
+       double const text_width_in = 210.0 / 2.54; // assume A4
+       double const em_width_in = 10.0 / 72.27;
+       double const result = 72.0 * inInch(text_width_in, em_width_in);
+       return support::iround(result);
+}
+
+
+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();
+}
+
+
+bool operator!=(Length const & l1, Length const & l2)
+{
+       return !(l1 == l2);
+}
+
+
+/////////////////////////////////////////////////////////////////////
+//
+// GlueLength
+//
+/////////////////////////////////////////////////////////////////////
+
+
+GlueLength::GlueLength(Length const & len)
+       : len_(len)
+{}
+
+
+GlueLength::GlueLength(Length const & len, Length const & plus,
+               Length const & minus)
+       : len_(len), plus_(plus), minus_(minus)
+{}
+
+
+GlueLength::GlueLength(string const & data)
+{
+       if (!isValidGlueLength(data, this))
+               LYXERR0("Invalid glue length " + data);
+}
+
+
+string const GlueLength::asString() const
+{
+       if (len_.empty())
+               return string();
+
+       ostringstream buffer;
+
+       buffer << formatFPNumber(len_.value());
+
+       if (plus_.zero() && minus_.zero()) {
+               buffer << unit_name[len_.unit()];
+               return buffer.str();
+       }
+
+       // just len and plus
+       if (minus_.zero()) {
+               if (len_.unit() != plus_.unit())
+                       buffer << unit_name[len_.unit()];
+               buffer << '+' << formatFPNumber(plus_.value());
+               buffer << unit_name[plus_.unit()];
+               return buffer.str();
+       }
+
+       // just len and minus
+       if (plus_.zero()) {
+               if (len_.unit() != minus_.unit())
+                       buffer << unit_name[len_.unit()];
+               buffer << '-' << formatFPNumber(minus_.value());
+               buffer << unit_name[minus_.unit()];
+               return buffer.str();
+       }
+
+       // ok, len, plus AND minus
+
+       // len+-
+       if (minus_ == plus_) {
+               if (len_.unit() != minus_.unit())
+                       buffer << unit_name[len_.unit()];
+               buffer << "+-" << formatFPNumber(minus_.value());
+               buffer << unit_name[minus_.unit()];
+               return buffer.str();
+       }
+
+       // this is so rare a case, why bother minimising units ?
+
+       buffer << unit_name[len_.unit()];
+       buffer << '+' << formatFPNumber(plus_.value()) << unit_name[plus_.unit()];
+       buffer << '-' << formatFPNumber(minus_.value()) << unit_name[minus_.unit()];
+
+       return buffer.str();
+}
+
+
+string const GlueLength::asLatexString() const
+{
+       ostringstream buffer;
+       // use Length::asLatexString() to handle also the percent lengths
+       buffer << len_.Length::asLatexString();
+       if (!plus_.zero())
+               buffer << " plus " << plus_.Length::asLatexString();
+       if (!minus_.zero())
+               buffer << " minus " << minus_.Length::asLatexString();
+       return buffer.str();
+}
+
+
+Length const & GlueLength::len() const
+{
+       return len_;
+}
+
+
+Length const & GlueLength::plus() const
+{
+       return plus_;
+}
+
+
+Length const & GlueLength::minus() const
+{
+       return minus_;
+}
+
+
+bool operator==(GlueLength const & l1, GlueLength const & l2)
+{
+       return l1.len() == l2.len()
+                && l1.plus() == l2.plus()
+                && l1.minus() == l2.minus();
+}
+
+
+bool operator!=(GlueLength const & l1, GlueLength const & l2)
+{
+       return !(l1 == l2);
+}
+
+
+} // namespace lyx
diff --git a/src/support/Length.h b/src/support/Length.h
new file mode 100644 (file)
index 0000000..aaf1923
--- /dev/null
@@ -0,0 +1,211 @@
+// -*- C++ -*-
+/**
+ * \file Length.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Matthias Ettrich
+ * \author Lars Gullik Bjønnes
+ * \author Jean-Marc Lasgouttes
+ * \author John Levon
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#ifndef LENGTH_H
+#define LENGTH_H
+
+#include "support/strfwd.h"
+
+
+namespace lyx {
+
+// Solaris/x86 version 9 and earlier define these
+#undef PC
+#undef SP
+
+/////////////////////////////////////////////////////////////////////
+//
+// Length
+//
+/////////////////////////////////////////////////////////////////////
+
+
+/**
+ * Length - Represents latex length measurement
+ */
+class Length {
+public:
+       /// length units
+       enum UNIT {
+               BP, ///< Big point (72bp = 1in), also PostScript point
+               CC, ///< Cicero = 12dd = 4.531mm
+               CM, ///< Centimeter = 10mm = 2.371pc
+               DD, ///< Didot point = 1/72 of a French inch, = 0.376mm
+               EM, ///< Width of capital "M" in current font.
+               EX, ///< Height of a small "x" for the current font.
+               IN, ///< Inch = 25.4mm = 72.27pt = 6.022pc
+               MM, ///< Millimeter = 2.845pt
+               MU, ///< Math unit (18mu = 1em) for positioning in math mode
+               PC, ///< Pica = 12pt = 4.218mm
+               PT, ///< Point = 1/72.27in = 0.351mm
+               SP, ///< Scaled point (65536sp = 1pt) TeX's smallest unit.
+               PTW, //< Percent of TextWidth
+               PCW, //< Percent of ColumnWidth
+               PPW, //< Percent of PageWidth
+               PLW, //< Percent of LineWidth
+               PTH, //< Percent of TextHeight          // Herbert 2002-05-16
+               PPH, //< Percent of PaperHeight         // Herbert 2002-05-16
+               BLS, //< Percent of BaselineSkip        // uwestoehr 2017-04-01
+               UNIT_NONE ///< no unit
+       };
+
+       ///
+       Length();
+       ///
+       Length(double v, Length::UNIT u);
+
+       /// "data" must be a decimal number, followed by a unit
+       explicit Length(std::string const & data);
+
+       ///
+       double value() const;
+       ///
+       Length::UNIT unit() const;
+       ///
+       void value(double);
+       ///
+       void unit(Length::UNIT unit);
+       ///
+       bool zero() const;
+       ///
+       bool empty() const;
+       /// return string representation
+       std::string const asString() const;
+       /// return string representation
+       docstring const asDocstring() const;
+       /// return string representation for LaTeX
+       std::string const asLatexString() const;
+       /// return string representation for HTML
+       std::string const asHTMLString() const;
+       /** return the on-screen size of this length.
+        *
+        *      If the second argument is not provided, then the unit EM will
+        *      only be approximated. It is better if possible to use
+        *      FontMetrics::em() to get this value.
+        */
+       int inPixels(int text_width, int em_width = 0) const;
+
+       /// return the value in Big Postscript points.
+       /// Caution: Inaccurate for em, ex, mu and percent units.
+       int inBP() const;
+
+       /// return the default unit (centimeter or inch)
+       static UNIT defaultUnit();
+
+       friend bool isValidLength(std::string const & data, Length * result);
+
+private:
+       /// Convert value to inch for text width and em width given in inch
+       double inInch(double text_width, double em_width) const;
+       ///
+       double val_;
+       ///
+       Length::UNIT unit_;
+};
+
+///
+bool operator==(Length const & l1, Length const & l2);
+///
+bool operator!=(Length const & l1, Length const & l2);
+/** Test whether \p data represents a valid length.
+ *
+ * \returns whether \p data is a valid length
+ * \param data Length in LyX format. Since the only difference between LyX
+ * and LaTeX format is the representation of length variables as units (e.g.
+ * \c text% vs. \c \\textwidth) you can actually use this function as well
+ * for testing LaTeX lengths as long as they only contain real units like pt.
+ * \param result Pointer to a Length variable. If \p result is not 0 and
+ * \p data is valid, the length represented by it is stored into \p result.
+ */
+bool isValidLength(std::string const & data, Length * result = 0);
+/// return the LyX name of the given unit number
+char const * stringFromUnit(int unit);
+
+
+/////////////////////////////////////////////////////////////////////
+//
+// GlueLength
+//
+/////////////////////////////////////////////////////////////////////
+
+class GlueLength {
+public:
+       ///
+       GlueLength() {}
+       ///
+       explicit GlueLength(Length const & len);
+       ///
+       GlueLength(Length const & len,
+                     Length const & plus,
+                     Length const & minus);
+
+       /** "data" must be a decimal number, followed by a unit, and
+         optional "glue" indicated by "+" and "-".  You may abbreviate
+         reasonably.  Examples:
+         1.2 cm  //  4mm +2pt  //  2cm -4mm +2mm  //  4+0.1-0.2cm
+         The traditional Latex format is also accepted, like
+         4cm plus 10pt minus 10pt */
+       explicit GlueLength(std::string const & data);
+
+       ///
+       Length const & len() const;
+       ///
+       Length const & plus() const;
+       ///
+       Length const & minus() const;
+
+
+       /// conversion
+       std::string const asString() const;
+       ///
+       std::string const asLatexString() const;
+
+       friend bool isValidGlueLength(std::string const & data,
+                                     GlueLength* result);
+
+private:
+       /// the normal vlaue
+       Length len_;
+       /// extra stretch
+       Length plus_;
+       /// extra shrink
+       Length minus_;
+};
+
+///
+bool operator==(GlueLength const & l1, GlueLength const & l2);
+///
+bool operator!=(GlueLength const & l1, GlueLength const & l2);
+/** If "data" is valid, the length represented by it is
+    stored into "result", if that is not 0. */
+bool isValidGlueLength(std::string const & data, GlueLength * result = 0);
+
+/// the number of units possible
+extern int const num_units;
+
+/**
+ * array of unit names
+ *
+ * FIXME: I am not sure if "mu" should be possible to select (Lgb)
+ */
+extern char const * const unit_name[];
+extern char const * const unit_name_gui[];
+
+/// return the unit given a string representation such as "cm"
+Length::UNIT unitFromString(std::string const & data);
+
+
+} // namespace lyx
+
+#endif // LENGTH_H
index 3d92b62db0b584e8fda26e9a0332fc4d95a94547..8b7d211fa0a95594219653b375d5d917a13f6f96 100644 (file)
@@ -71,6 +71,9 @@ liblyxsupport_a_SOURCES = \
        kill.cpp \
        lassert.h \
        lassert.cpp \
+       Length.cpp \
+       Length.h \
+       lengthcommon.cpp \
        limited_stack.h \
        lstrings.cpp \
        lstrings.h \
diff --git a/src/support/lengthcommon.cpp b/src/support/lengthcommon.cpp
new file mode 100644 (file)
index 0000000..303dbcb
--- /dev/null
@@ -0,0 +1,366 @@
+/**
+ * \file lengthcommon.cpp
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Lars Gullik Bjønnes
+ * \author Matthias Ettrich
+ * \author John Levon
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#include <config.h>
+
+#include "support/convert.h"
+#include "support/gettext.h"
+#include "support/lassert.h"
+#include "support/Length.h"
+#include "support/lstrings.h"
+
+using namespace std;
+using namespace lyx::support;
+
+
+namespace lyx {
+
+// I am not sure if "mu" should be possible to select (Lgb)
+
+// the latex units
+char const * const unit_name[] = {
+       "bp", "cc", "cm", "dd", "em", "ex", "in", "mm", "mu",
+       "pc", "pt", "sp",
+       "text%",  "col%", "page%", "line%",
+       "theight%", "pheight%", "baselineskip%", "" };
+
+int const num_units = int(sizeof(unit_name) / sizeof(unit_name[0]) - 1);
+
+// the LyX gui units
+char const * const unit_name_gui[] = {
+       N_("bp"), N_("cc[[unit of measure]]"), N_("cm"), N_("dd"), N_("em"),
+       N_("ex"), N_("in[[unit of measure]]"), N_("mm"), N_("mu[[unit of measure]]"), N_("pc"),
+       N_("pt"), N_("sp"), N_("Text Width %"),
+       N_("Column Width %"), N_("Page Width %"), N_("Line Width %"),
+       N_("Text Height %"), N_("Page Height %"), N_("Line Distance %"), "" };
+
+
+Length::UNIT unitFromString(string const & data)
+{
+       int i = 0;
+       while (i < num_units && data != unit_name[i])
+               ++i;
+       return static_cast<Length::UNIT>(i);
+}
+
+
+namespace {
+
+/// skip n characters of input
+inline void lyx_advance(string & data, size_t n)
+{
+       data.erase(0, n);
+}
+
+
+/// return true when the input is at the end
+inline bool isEndOfData(string const & data)
+{
+       return ltrim(data).empty();
+}
+
+
+/**
+ * nextToken -  return the next token in the input
+ * @param data input string
+ * @param number_index the current position in the number array
+ * @param unit_index the current position in the unit array
+ * @return a char representing the type of token returned
+ *
+ * The possible return values are :
+ *     +       stretch indicator for glue length
+ *     -       shrink indicator for glue length
+ *     n       a numeric value (stored in number array)
+ *     u       a unit type (stored in unit array)
+ *     E       parse error
+ */
+char nextToken(string & data, double * number, int & number_index,
+               Length::UNIT * unit, int & unit_index)
+{
+       data = ltrim(data);
+
+       if (data.empty())
+               return '\0';
+
+       if (data[0] == '+') {
+               lyx_advance(data, 1);
+               return '+';
+       }
+
+       if (prefixIs(data, "plus")) {
+               lyx_advance(data, 4);
+               return '+';
+       }
+
+       if (data[0] == '-') {
+               lyx_advance(data, 1);
+               return '-';
+       }
+
+       if (prefixIs(data, "minus")) {
+               lyx_advance(data, 5);
+               return '-';
+       }
+
+       size_t i = data.find_first_not_of("0123456789.");
+
+       if (i != 0) {
+               if (number_index > 3)
+                       return 'E';
+
+               string buffer;
+
+               // we have found some number
+               if (i == string::npos) {
+                       buffer = data;
+                       i = data.size() + 1;
+               } else {
+                       buffer = data.substr(0, i);
+               }
+
+               lyx_advance(data, i);
+
+               if (isStrDbl(buffer)) {
+                       number[number_index] = convert<double>(buffer);
+                       ++number_index;
+                       return 'n';
+               }
+               return 'E';
+       }
+
+       i = data.find_first_not_of("abcdefghijklmnopqrstuvwxyz%");
+       if (i != 0) {
+               if (unit_index > 3)
+                       return 'E';
+
+               string buffer;
+
+               // we have found some alphabetical string
+               if (i == string::npos) {
+                       buffer = data;
+                       i = data.size() + 1;
+               } else {
+                       buffer = data.substr(0, i);
+               }
+
+               // possibly we have "mmplus" string or similar
+               if (buffer.size() > 5 &&
+                               (buffer.substr(2, 4) == string("plus") ||
+                                buffer.substr(2, 5) == string("minus")))
+               {
+                       lyx_advance(data, 2);
+                       unit[unit_index] = unitFromString(buffer.substr(0, 2));
+               } else {
+                       lyx_advance(data, i);
+                       unit[unit_index] = unitFromString(buffer);
+               }
+
+               if (unit[unit_index] != Length::UNIT_NONE) {
+                       ++unit_index;
+                       return 'u';
+               }
+               return 'E';  // Error
+       }
+       return 'E';  // Error
+}
+
+
+/// latex representation of a vspace
+struct LaTeXLength {
+       char const * pattern;
+       int  plus_val_index;
+       int  minus_val_index;
+       int  plus_uni_index;
+       int  minus_uni_index;
+};
+
+
+/// the possible formats for a vspace string
+LaTeXLength table[] = {
+       { "nu",       0, 0, 0, 0 },
+       { "nu+nu",    2, 0, 2, 0 },
+       { "nu+nu-nu", 2, 3, 2, 3 },
+       { "nu+-nu",   2, 2, 2, 2 },
+       { "nu-nu",    0, 2, 0, 2 },
+       { "nu-nu+nu", 3, 2, 3, 2 },
+       { "nu-+nu",   2, 2, 2, 2 },
+       { "n+nu",     2, 0, 1, 0 },
+       { "n+n-nu",   2, 3, 1, 1 },
+       { "n+-nu",    2, 2, 1, 1 },
+       { "n-nu",     0, 2, 0, 1 },
+       { "n-n+nu",   3, 2, 1, 1 },
+       { "n-+nu",    2, 2, 1, 1 },
+       { "",         0, 0, 0, 0 }   // sentinel, must be empty
+};
+
+
+} // namespace
+
+
+const char * stringFromUnit(int unit)
+{
+       if (unit < 0 || unit > num_units)
+               return nullptr;
+       return unit_name[unit];
+}
+
+
+bool isValidGlueLength(string const & data, GlueLength * result)
+{
+       // This parser is table-driven.  First, it constructs a "pattern"
+       // that describes the sequence of tokens in "data".  For example,
+       // "n-nu" means: number, minus sign, number, unit.  As we go along,
+       // numbers and units are stored into static arrays.  Then, "pattern"
+       // is searched in the "table".  If it is found, the associated
+       // table entries tell us which number and unit should go where
+       // in the Length structure.  Example: if "data" has the "pattern"
+       // "nu+nu-nu", the associated table entries are "2, 3, 2, 3".
+       // That means, "plus_val" is the second number that was seen
+       // in the input, "minus_val" is the third number, and "plus_uni"
+       // and "minus_uni" are the second and third units, respectively.
+       // ("val" and "uni" are always the first items seen in "data".)
+       // This is the most elegant solution I could find -- a straight-
+       // forward approach leads to very long, tedious code that would be
+       // much harder to understand and maintain. (AS)
+
+       if (data.empty()) {
+               if (result)
+                       *result = GlueLength();
+               return true;
+       }
+       string buffer = ltrim(data);
+
+       // To make isValidGlueLength recognize negative values as
+       // the first number this little hack is needed:
+       int val_sign = 1; // positive as default
+       switch (buffer[0]) {
+       case '-':
+               lyx_advance(buffer, 1);
+               val_sign = -1;
+               break;
+       case '+':
+               lyx_advance(buffer, 1);
+               break;
+       default:
+               break;
+       }
+       // end of hack
+
+       // used to return numeric values in parsing vspace
+       double number[4] = { 0, 0, 0, 0 };
+       // used to return unit types in parsing vspace
+       Length::UNIT unit[4] = {Length::UNIT_NONE, Length::UNIT_NONE,
+                               Length::UNIT_NONE, Length::UNIT_NONE};
+       int number_index = 1; // entries at index 0 are sentinels
+       int unit_index = 1;   // entries at index 0 are sentinels
+
+       // construct "pattern" from "data"
+       size_t const pattern_max_size = 20;
+       string pattern;
+       while (!isEndOfData(buffer)) {
+               if (pattern.size() > pattern_max_size)
+                       return false;
+               char const c = nextToken(buffer, number, number_index, unit,
+                               unit_index);
+               if (c == 'E')
+                       return false;
+               pattern.push_back(c);
+       }
+
+       // search "pattern" in "table"
+       size_t table_index = 0;
+       while (pattern != table[table_index].pattern) {
+               ++table_index;
+               if (!*table[table_index].pattern)
+                       return false;
+       }
+
+       // Get the values from the appropriate places.  If an index
+       // is zero, the corresponding array value is zero or UNIT_NONE,
+       // so we needn't check this.
+       if (result) {
+               result->len_.value  (number[1] * val_sign);
+               result->len_.unit   (unit[1]);
+               result->plus_.value (number[table[table_index].plus_val_index]);
+               result->plus_.unit  (unit  [table[table_index].plus_uni_index]);
+               result->minus_.value(number[table[table_index].minus_val_index]);
+               result->minus_.unit (unit  [table[table_index].minus_uni_index]);
+       }
+       return true;
+}
+
+
+bool isValidLength(string const & data, Length * result)
+{
+       // This is a trimmed down version of isValidGlueLength.
+       // The parser may seem overkill for lengths without
+       // glue, but since we already have it, using it is
+       // easier than writing something from scratch.
+       if (data.empty()) {
+               if (result)
+                       *result = Length();
+               return true;
+       }
+
+       string buffer = data;
+
+       // To make isValidLength recognize negative values
+       // this little hack is needed:
+       int val_sign = 1; // positive as default
+       switch (buffer[0]) {
+       case '-':
+               lyx_advance(buffer, 1);
+               val_sign = -1;
+               break;
+       case '+':
+               lyx_advance(buffer, 1);
+               // fall through
+       default:
+               // no action
+               break;
+       }
+       // end of hack
+
+       // used to return numeric values in parsing vspace
+       double number[4] = { 0, 0, 0, 0 };
+       // used to return unit types in parsing vspace
+       Length::UNIT unit[4] = {Length::UNIT_NONE, Length::UNIT_NONE,
+                               Length::UNIT_NONE, Length::UNIT_NONE};
+       int number_index = 1; // entries at index 0 are sentinels
+       int unit_index = 1;   // entries at index 0 are sentinels
+
+       // construct "pattern" from "data"
+       string pattern;
+       while (!isEndOfData(buffer)) {
+               if (pattern.size() > 2)
+                       return false;
+               char const token = nextToken(buffer, number,
+                               number_index, unit, unit_index);
+               if (token == 'E')
+                       return false;
+               pattern += token;
+       }
+
+       // only the most basic pattern is accepted here
+       if (pattern != "nu")
+               return false;
+
+       // It _was_ a correct length string.
+       // Store away the values we found.
+       if (result) {
+               result->val_  = number[1] * val_sign;
+               result->unit_ = unit[1];
+       }
+       return true;
+}
+
+} // namespace lyx
index df83d0441be4eea3fdbca7f7d7ef1ddb38c7177f..957e4c0cd77edef8c0b046a67d9047eefe42b8b9 100644 (file)
@@ -66,7 +66,7 @@ endforeach()
 
 set(check_ExternalTransforms_SOURCES)
 foreach(_f graphics/GraphicsParams.cpp insets/ExternalTransforms.cpp
-       Length.cpp lengthcommon.cpp tests/check_ExternalTransforms.cpp
+       tests/check_ExternalTransforms.cpp
        tests/boost.cpp tests/dummy_functions.cpp)
     list(APPEND check_ExternalTransforms_SOURCES ${TOP_SRC_DIR}/src/${_f})
 endforeach()
@@ -90,7 +90,7 @@ add_test(NAME "check_ExternalTransforms"
 add_dependencies(lyx_run_tests check_ExternalTransforms)
 
 set(check_Length_SOURCES)
-foreach(_f Length.cpp lengthcommon.cpp tests/check_Length.cpp tests/boost.cpp tests/dummy_functions.cpp)
+foreach(_f tests/check_Length.cpp tests/boost.cpp tests/dummy_functions.cpp)
   list(APPEND check_Length_SOURCES ${TOP_SRC_DIR}/src/${_f})
 endforeach()
 add_executable(check_Length ${check_Length_SOURCES})
index cb129af1585054d0c0774a899971916c5bdac5ed..2a68e7115090d938517dd509bfffe63788942892 100644 (file)
@@ -7,14 +7,13 @@
 
 project(${_tex2lyx})
 
-# There is no header file lengthcommon.h
-set(LINKED_sources ${TOP_SRC_DIR}/src/lengthcommon.cpp)
+set(LINKED_sources)
 set(LINKED_headers)
 
 foreach(_src graphics/GraphicsParams insets/ExternalTemplate
        insets/ExternalTransforms insets/InsetLayout Author CiteEnginesList Color Counters
        Encoding FloatList Floating FontInfo LaTeXPackages Layout
-       LayoutFile LayoutModuleList Length Lexer ModuleList TextClass
+       LayoutFile LayoutModuleList Lexer ModuleList TextClass
        Spacing version)
        list(APPEND LINKED_sources ${TOP_SRC_DIR}/src/${_src}.cpp)
        list(APPEND LINKED_headers ${TOP_SRC_DIR}/src/${_src}.h)
index 87bb3355e0fce42db7b0b1fd641ed86199b8e7f0..05a3efc55a467010522a2d69e6dd3868458b8b6f 100644 (file)
@@ -102,8 +102,6 @@ LYX_OBJS = \
        ../Layout.o \
        ../LayoutFile.o \
        ../LayoutModuleList.o \
-       ../Length.o \
-       ../lengthcommon.o \
        ../Lexer.o \
        ../ModuleList.o \
        ../Spacing.o \
index fdcab6333fee81fa5577d00012cffdc0fb0f7484..6744d78157db4a99f154e7a485a1b0989a870bab 100644 (file)
@@ -21,7 +21,6 @@
 #include "FloatList.h"
 #include "LaTeXPackages.h"
 #include "Layout.h"
-#include "Length.h"
 #include "Preamble.h"
 
 #include "insets/ExternalTemplate.h"
@@ -30,6 +29,7 @@
 #include "support/convert.h"
 #include "support/FileName.h"
 #include "support/filetools.h"
+#include "support/Length.h"
 #include "support/lstrings.h"
 #include "support/lyxtime.h"