]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetgraphicsParams.C
* src/LyXAction.C: mark goto-clear-bookmark as working without buffer
[lyx.git] / src / insets / insetgraphicsParams.C
index 846f28d84eb15595de1801e3293d9f580a2640cc..c0cdd5fae8826669aa6d68d3e2c1ae08c78acc69 100644 (file)
@@ -1,91 +1,45 @@
-/* This file is part of
- * =================================================
- * 
- *          LyX, The Document Processor
- *          Copyright 1995 Matthias Ettrich.
- *          Copyright 1995-2001 The LyX Team.
+/**
+ * \file insetgraphicsParams.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- *          This file Copyright 2000 Baruch Even
- * ================================================= */
-
-#include <config.h> 
+ * \author Baruch Even
+ * \author Herbert Voß
+ *
+ * Full author contact details are available in file CREDITS.
+ */
 
-#ifdef __GNUG__
-#pragma implementation
-#endif 
+#include <config.h>
 
 #include "insetgraphicsParams.h"
 
-#include "support/translator.h"
-#include "support/filetools.h"
-#include "support/lyxlib.h"
-#include "support/LOstream.h"
-
-#include "support/LAssert.h"
+#include "debug.h"
+#include "lyx_main.h" // for use_gui
+#include "lyxlex.h"
+#include "lyxrc.h"
 
-namespace {
+#include "graphics/GraphicsParams.h"
 
-/// This variable keeps a tab on whether the translator was set with the
-/// translations.
-bool translatorsSet = false;
+#include "support/convert.h"
+#include "support/filetools.h"
+#include "support/lyxlib.h"
+#include "support/lstrings.h"
+#include "support/translator.h"
 
-/// This is the translator between the Resize enum and corresponding lyx
-/// file strings.
-Translator< InsetGraphicsParams::Resize, string >
-resizeTranslator(InsetGraphicsParams::DEFAULT_SIZE, "default");
 
-/// This is the translator between the Origin enum and corresponding lyx
-/// file strings.
-Translator< InsetGraphicsParams::Origin, string >
-originTranslator(InsetGraphicsParams::DEFAULT, "default");
+namespace lyx {
 
-/// This is the translator between the Display enum and corresponding lyx
-/// file strings.
-Translator< InsetGraphicsParams::DisplayType, string >
-displayTranslator(InsetGraphicsParams::MONOCHROME, "monochrome");
+using support::float_equal;
+using support::readBB_from_PSFile;
+using support::token;
 
-} // namespace anon
+using std::string;
+using std::ostream;
 
 
 InsetGraphicsParams::InsetGraphicsParams()
 {
        init();
-
-       // Set translators
-       if (! translatorsSet) {
-               translatorsSet = true;
-
-               // Fill the resize translator
-               resizeTranslator.addPair(DEFAULT_SIZE, "default");
-               resizeTranslator.addPair(CM, "cm");
-               resizeTranslator.addPair(INCH, "inch");
-               resizeTranslator.addPair(PERCENT_PAGE, "percentOfPage");
-               resizeTranslator.addPair(PERCENT_COLUMN, "percentOfColumn");
-               resizeTranslator.addPair(SCALE, "scale");
-
-               // Fill the origin translator
-               originTranslator.addPair(DEFAULT, "default");
-               originTranslator.addPair(LEFTTOP, "leftTop");
-               originTranslator.addPair(LEFTCENTER, "leftCenter");
-               originTranslator.addPair(LEFTBASELINE, "leftBaseLine");
-               originTranslator.addPair(LEFTBOTTOM, "leftBottom");
-               originTranslator.addPair(CENTERTOP, "centerTop");
-               originTranslator.addPair(CENTER, "center");
-               originTranslator.addPair(CENTERBASELINE, "centerBaseLine");
-               originTranslator.addPair(CENTERBOTTOM, "centerBottom");
-               originTranslator.addPair(RIGHTTOP, "rightTop");
-               originTranslator.addPair(RIGHTCENTER, "rightCenter");
-               originTranslator.addPair(RIGHTBASELINE, "rightBaseLine");
-               originTranslator.addPair(RIGHTBOTTOM, "rightBottom");
-               originTranslator.addPair(REFERENCE_POINT, "referencePoint");
-
-               // Fill the display translator
-               displayTranslator.addPair(MONOCHROME, "monochrome");
-               displayTranslator.addPair(GRAYSCALE, "grayscale");
-               displayTranslator.addPair(COLOR, "color");
-               displayTranslator.addPair(NONE, "none");
-       }
-
 }
 
 
@@ -97,240 +51,285 @@ InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
        copy(igp);
 }
 
+
 InsetGraphicsParams &
 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
 {
        // Are we assigning the object into itself?
        if (this == &params)
-               return * this;
-
+               return *this;
        copy(params);
        return *this;
 }
 
+
 void InsetGraphicsParams::init()
 {
-       subcaptionText = filename = string();
-       display = MONOCHROME;
-       subcaption = false;
-       keepAspectRatio = true;
-       widthResize = DEFAULT_SIZE;
-       widthSize = 0.0;
-       heightResize = DEFAULT_SIZE;
-       heightSize = 0.0;
-       rotateOrigin = DEFAULT;
-       rotateAngle = 0.0;
-
-       testInvariant();
+       filename.erase();
+       lyxscale = 100;                 // lyx scaling in percentage
+       display = graphics::DefaultDisplay; // display mode; see preferences
+       scale = string();                       // output scaling in percentage
+       width = LyXLength();
+       height = LyXLength();
+       keepAspectRatio = false;        // for LaTeX output
+       draft = false;                  // draft mode
+       noUnzip = false;                // unzip files
+
+       bb = string();                  // bounding box
+       clip = false;                   // clip image
+
+       rotateAngle = "0";              // angle of rotation in degrees
+       rotateOrigin.erase();           // Origin of rotation
+       subcaption = false;             // subfigure
+       subcaptionText.erase();         // subfigure caption
+       special.erase();                // additional userdefined stuff
 }
 
+
 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
 {
        filename = igp.filename;
+       lyxscale = igp.lyxscale;
        display = igp.display;
-       subcaption = igp.subcaption;
-       subcaptionText = igp.subcaptionText;
+       scale = igp.scale;
+       width = igp.width;
+       height = igp.height;
        keepAspectRatio = igp.keepAspectRatio;
-       widthResize = igp.widthResize;
-       widthSize = igp.widthSize;
-       heightResize = igp.heightResize;
-       heightSize = igp.heightSize;
-       rotateOrigin = igp.rotateOrigin;
-       rotateAngle = igp.rotateAngle;
-
-       testInvariant();
-}
+       draft = igp.draft;
+       noUnzip = igp.noUnzip;
 
-void InsetGraphicsParams::testInvariant() const
-{
-       // Filename might be empty (when the dialog is first created).
-       // Assert(!filename.empty());
-
-       lyx::Assert(display == COLOR ||
-              display == MONOCHROME ||
-              display == GRAYSCALE ||
-              display == NONE
-             );
-
-       lyx::Assert(widthResize == DEFAULT_SIZE ||
-              widthResize == CM ||
-              widthResize == INCH ||
-              widthResize == PERCENT_PAGE ||
-              widthResize == PERCENT_COLUMN ||
-                  widthResize == SCALE
-             );
-
-       lyx::Assert(heightResize == DEFAULT_SIZE ||
-              heightResize == CM ||
-              heightResize == INCH ||
-              heightResize == PERCENT_PAGE ||
-                  heightResize == SCALE
-             );
-
-       // For SCALE these can be negative.
-       //lyx::Assert(widthSize >= 0.0);
-       //lyx::Assert(heightSize >= 0.0);
-
-       // Angle is in degrees and ranges -360 < angle < 360
-       // The reason for this is that in latex there is a meaning for the
-       // different angles and they are not necessarliy interchangeable,
-       // it depends on the rotation origin.
-       lyx::Assert(rotateAngle < 360.0);
-       lyx::Assert(rotateAngle > -360.0);
+       bb = igp.bb;
+       clip = igp.clip;
 
+       rotateAngle = igp.rotateAngle;
+       rotateOrigin = igp.rotateOrigin;
+       subcaption = igp.subcaption;
+       subcaptionText = igp.subcaptionText;
+       special = igp.special;
 }
 
+
 bool operator==(InsetGraphicsParams const & left,
-                InsetGraphicsParams const & right)
+               InsetGraphicsParams const & right)
 {
        if (left.filename == right.filename &&
-               left.display == right.display &&
-               left.subcaption == right.subcaption &&
-               left.subcaptionText == right.subcaptionText &&
-               left.keepAspectRatio == right.keepAspectRatio &&
-               left.widthResize == right.widthResize &&
-               left.widthSize == right.widthSize &&
-               left.heightResize == right.heightResize &&
-               left.heightSize == right.heightSize &&
-               left.rotateOrigin == right.rotateOrigin &&
-               lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001)
+           left.lyxscale == right.lyxscale &&
+           left.display == right.display &&
+           left.scale == right.scale &&
+           left.width == right.width &&
+           left.height == right.height &&
+           left.keepAspectRatio == right.keepAspectRatio &&
+           left.draft == right.draft &&
+           left.noUnzip == right.noUnzip &&
+
+
+           left.bb == right.bb &&
+           left.clip == right.clip &&
+
+           left.rotateAngle == right.rotateAngle &&
+           left.rotateOrigin == right.rotateOrigin &&
+           left.subcaption == right.subcaption &&
+           left.subcaptionText == right.subcaptionText &&
+           left.special == right.special
           )
                return true;
 
        return false;
 }
 
+
 bool operator!=(InsetGraphicsParams const & left,
-                InsetGraphicsParams const & right)
+               InsetGraphicsParams const & right)
 {
-       return  !(left == right);
+       return  !(left == right);
 }
 
 
-namespace {
-
-void writeResize(ostream & os, string const & key,
-                        InsetGraphicsParams::Resize resize, double size)
+void InsetGraphicsParams::Write(ostream & os, string const & bufpath) const
 {
-       os << ' ' << key << "Resize ";
-
-       os << resizeTranslator.find(resize);
-       os << ' ' << key << ' ' << size << '\n';
-}
-
-void writeOrigin(ostream & os,
-                        InsetGraphicsParams::Origin origin)
-{
-       os << " rotateOrigin " << originTranslator.find(origin);
-       os << '\n';
-}
-
-} // namespace anon
+       // Do not write the default values
 
-
-void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
-{
-       // If there is no filename, write nothing for it.
-       if (! filename.empty()) {
-               os << "filename "
-               << MakeRelPath(filename, OnlyPath(buf->fileName()))
-               << '\n';
+       if (!filename.empty()) {
+               os << "\tfilename " << filename.outputFilename(bufpath) << '\n';
        }
-
-       // Save the display type
-       os << " display " << displayTranslator.find(display) << '\n';
-
-       // Save the subcaption status
-       if (subcaption)
-               os << " subcaption";
-
-       if (! subcaptionText.empty())
-               os << " subcaptionText \"" << subcaptionText << '\"' << '\n';
-
-       writeResize(os, "width", widthResize, widthSize);
-       writeResize(os, "height", heightResize, heightSize);
-
-       writeOrigin(os, rotateOrigin);
-       if (!lyx::float_equal(rotateAngle, 0.0, 0.001)) {
-               os << " rotateAngle " << rotateAngle << '\n';
+       if (lyxscale != 100)
+               os << "\tlyxscale " << lyxscale << '\n';
+       if (display != graphics::DefaultDisplay)
+               os << "\tdisplay " << graphics::displayTranslator().find(display) << '\n';
+       if (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
+               if (!float_equal(convert<double>(scale), 100.0, 0.05))
+                       os << "\tscale " << scale << '\n';
+       } else {
+               if (!width.zero())
+                       os << "\twidth " << width.asString() << '\n';
+               if (!height.zero())
+                       os << "\theight " << height.asString() << '\n';
        }
-}
-
 
-namespace {
-
-void readResize(InsetGraphicsParams * igp, bool height,
-                       string const & token)
-{
-       InsetGraphicsParams::Resize resize = InsetGraphicsParams::DEFAULT_SIZE;
-
-       resize = resizeTranslator.find(token);
-
-       if (height)
-               igp->heightResize = resize;
-       else
-               igp->widthResize = resize;
-}
-
-
-void readOrigin(InsetGraphicsParams * igp, string const & token)
-{
-       // TODO: complete this function.
-       igp->rotateOrigin = originTranslator.find(token);
+       if (keepAspectRatio)
+               os << "\tkeepAspectRatio\n";
+       if (draft)                      // draft mode
+               os << "\tdraft\n";
+       if (noUnzip)
+               os << "\tnoUnzip\n";
+
+       if (!bb.empty())                // bounding box
+               os << "\tBoundingBox " << bb << '\n';
+       if (clip)                       // clip image
+               os << "\tclip\n";
+
+       if (!rotateAngle.empty()
+               && !float_equal(convert<double>(rotateAngle), 0.0, 0.001))
+               os << "\trotateAngle " << rotateAngle << '\n';
+       if (!rotateOrigin.empty())
+               os << "\trotateOrigin " << rotateOrigin << '\n';
+       if (subcaption)
+               os << "\tsubcaption\n";
+       if (!subcaptionText.empty())
+               os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
+       if (!special.empty())
+               os << "\tspecial " << special << '\n';
 }
 
-} // namespace anon
 
-
-bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
-                               string const& token)
+bool InsetGraphicsParams::Read(LyXLex & lex, string const & token, string const & bufpath)
 {
        if (token == "filename") {
+               lex.eatLine();
+               filename.set(lex.getString(), bufpath);
+       } else if (token == "lyxscale") {
                lex.next();
-               filename = lex.getString();
-
-               if (!filename.empty()) {
-                       // Make the filename with absolute directory.
-                       filename = MakeAbsPath(filename, OnlyPath(buf->fileName()));
-               }
+               lyxscale = lex.getInteger();
        } else if (token == "display") {
                lex.next();
                string const type = lex.getString();
-
-               display = displayTranslator.find(type);
-       } else if (token == "subcaption") {
-               subcaption = true;
-       } else if (token == "subcaptionText") {
-               lex.next();
-               subcaptionText = lex.getString();
-       } else if (token == "widthResize") {
+               display = graphics::displayTranslator().find(type);
+       } else if (token == "scale") {
                lex.next();
-               string const token = lex.getString();
-
-               readResize(this, false, token);
+               scale = lex.getString();
        } else if (token == "width") {
                lex.next();
-               widthSize = lex.getFloat();
-       } else if (token == "heightResize") {
-               lex.next();
-               string const token = lex.getString();
-
-               readResize(this, true, token);
+               width = LyXLength(lex.getString());
+               scale = string();
        } else if (token == "height") {
                lex.next();
-               heightSize = lex.getFloat();
+               height = LyXLength(lex.getString());
+               scale = string();
+       } else if (token == "keepAspectRatio") {
+               keepAspectRatio = true;
+       } else if (token == "draft") {
+               draft = true;
+       } else if (token == "noUnzip") {
+               noUnzip = true;
+       } else if (token == "BoundingBox") {
+               bb.erase();
+               for (int i = 0; i < 4; ++i) {
+                       if (i != 0)
+                               bb += ' ';
+                       lex.next();
+                       bb += lex.getString();
+               }
+       } else if (token == "clip") {
+               clip = true;
+       } else if (token == "rotateAngle") {
+               lex.next();
+               rotateAngle = lex.getString();
        } else if (token == "rotateOrigin") {
                lex.next();
-               string const token = lex.getString();
-
-               readOrigin(this, token);
-       } else if (token == "rotateAngle") {
+               rotateOrigin=lex.getString();
+       } else if (token == "subcaption") {
+               subcaption = true;
+       } else if (token == "subcaptionText") {
+               lex.eatLine();
+               string sub = lex.getString();
+               // strip surrounding " "
+               subcaptionText = sub.substr(1, sub.length() - 2);
+       } else if (token == "special") {
+               lex.eatLine();
+               special = lex.getString();
+
+       // catch and ignore following two old-format tokens and their arguments.
+       // e.g. "size_kind scale" clashes with the setting of the
+       // "scale <value>" keyword.
+       } else if (token == "size_kind" || token == "lyxsize_kind") {
                lex.next();
-               rotateAngle = lex.getFloat();
+               lex.getString();
+
        } else {
-               // If it's none of the above, its not ours.
+               // If it's none of the above, it's not ours.
                return false;
        }
-
        return true;
 }
+
+
+graphics::Params InsetGraphicsParams::as_grfxParams() const
+{
+       graphics::Params pars;
+       pars.filename = filename;
+       pars.scale = lyxscale;
+       pars.angle = convert<double>(rotateAngle);
+
+       if (clip) {
+               pars.bb = bb;
+
+               // Get the original Bounding Box from the file
+               string const tmp = readBB_from_PSFile(filename);
+               lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
+               if (!tmp.empty()) {
+#ifdef WITH_WARNINGS
+# warning why not convert to unsigned int? (Lgb)
+#endif
+                       unsigned int const bb_orig_xl = convert<int>(token(tmp, ' ', 0));
+                       unsigned int const bb_orig_yb = convert<int>(token(tmp, ' ', 1));
+
+                       // new pars.bb values must be >= zero
+                       if  (pars.bb.xl > bb_orig_xl)
+                               pars.bb.xl -= bb_orig_xl;
+                       else
+                               pars.bb.xl = 0;
+
+                       if (pars.bb.xr > bb_orig_xl)
+                               pars.bb.xr -= bb_orig_xl;
+                       else
+                               pars.bb.xr = 0;
+
+                       if (pars.bb.yb > bb_orig_yb)
+                               pars.bb.yb -= bb_orig_yb;
+                       else
+                               pars.bb.yb = 0;
+
+                       if (pars.bb.yt > bb_orig_yb)
+                               pars.bb.yt -= bb_orig_yb;
+                       else
+                               pars.bb.yt = 0;
+               }
+
+               // Paranoia check.
+               int const width  = pars.bb.xr - pars.bb.xl;
+               int const height = pars.bb.yt - pars.bb.yb;
+
+               if (width  < 0 || height < 0) {
+                       pars.bb.xl = 0;
+                       pars.bb.xr = 0;
+                       pars.bb.yb = 0;
+                       pars.bb.yt = 0;
+               }
+       }
+
+       if (display == graphics::DefaultDisplay) {
+               pars.display = lyxrc.display_graphics;
+       } else {
+               pars.display = display;
+       }
+
+       // Override the above if we're not using a gui
+       if (!use_gui) {
+               pars.display = graphics::NoDisplay;
+       }
+
+       return pars;
+}
+
+
+} // namespace lyx