]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetgraphicsParams.C
the convert patch
[lyx.git] / src / insets / insetgraphicsParams.C
index b790ce1c5f3bb861b432c2aa41a60bc315e140b2..fd9678471c48f428560de8c41701984cb1875c8b 100644 (file)
@@ -1,77 +1,43 @@
-/* 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.
  *
  * \author Baruch Even
- * \author Herbert Voss <voss@lyx.org>
+ * \author Herbert Voß
  *
- * ================================================= */
+ * Full author contact details are available in file CREDITS.
+ */
 
-#include <config.h> 
-
-#ifdef __GNUG__
-#pragma implementation
-#endif 
+#include <config.h>
 
 #include "insetgraphicsParams.h"
 
-#include "support/translator.h"
+#include "debug.h"
+#include "lyxlex.h"
+#include "lyxrc.h"
+
+#include "frontends/lyx_gui.h"
+
+#include "graphics/GraphicsParams.h"
+
+#include "support/convert.h"
 #include "support/filetools.h"
 #include "support/lyxlib.h"
-#include "support/LOstream.h"
-#include "lyxrc.h"
+#include "support/lstrings.h"
+#include "support/translator.h"
 
-#include "support/LAssert.h"
-
-namespace {
-
-/// This variable keeps a tab on whether the translator was set with the
-/// translations.
-bool translatorsSet = false;
-
-/// This is the translator between the Display enum and corresponding lyx
-/// file strings.
-Translator< InsetGraphicsParams::DisplayType, string >
-displayTranslator(InsetGraphicsParams::MONOCHROME, "monochrome");
-
-// this is only compatibility stuff for the first 1.2 version
-// it is obselete until 1.3
-LyXLength convertResizeValue(string const token, LyXLex & lex) {
-    lex.next();
-    string value = lex.getString();    // "width" or "height"  
-    lex.next();                                // anyway not interesting
-    value = lex.getString();
-    if (token == "default")
-       return (LyXLength(value+"pt"));
-    else if (token == "cm")
-       return (LyXLength(value+"cm"));
-    else if (token == "inch")
-       return (LyXLength(value+"in"));
-    else if (token == "percentOfColumn")
-       return (LyXLength(value+"c%"));
-    else if (token == "percentOfPage")
-       return (LyXLength(value+"p%"));
-    else return LyXLength("0pt");      // nothing with figinset
-}
+using lyx::support::float_equal;
+using lyx::support::readBB_from_PSFile;
+using lyx::support::token;
 
-} // namespace anon
+using std::string;
+using std::ostream;
 
 
 InsetGraphicsParams::InsetGraphicsParams()
 {
        init();
-       // Set translators
-       if (! translatorsSet) {
-               translatorsSet = true;
-               // Fill the display translator
-               displayTranslator.addPair(MONOCHROME, "monochrome");
-               displayTranslator.addPair(GRAYSCALE, "grayscale");
-               displayTranslator.addPair(COLOR, "color");
-               displayTranslator.addPair(NONE, "none");
-       }
 }
 
 
@@ -83,247 +49,280 @@ 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();
-       bb = string();                  // bounding box
+       filename.erase();
+       lyxscale = 100;                 // lyx scaling in percentage
+       display = lyx::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
-       if (lyxrc.display_graphics == "mono") 
-           display = MONOCHROME;
-       else if (lyxrc.display_graphics == "gray") 
-           display = GRAYSCALE;
-       else if (lyxrc.display_graphics == "color") 
-           display = COLOR;
-       else
-           display = NONE;
+
+       rotateAngle = "0";              // angle of rotation in degrees
+       rotateOrigin.erase();           // Origin of rotation
        subcaption = false;             // subfigure
-       width = LyXLength();            // set to 0pt
-       height = LyXLength();
-       lyxwidth = LyXLength();         // for the view in lyx
-       lyxheight = LyXLength();
-       scale = 0;
-       size_type = DEFAULT_SIZE;       // do nothing
-       keepAspectRatio = false;        //
-       rotateOrigin = "center";        // 
-       rotateAngle = 0.0;              // in degrees
-       special = string();             // userdefined stuff
-
-       testInvariant();
+       subcaptionText.erase();         // subfigure caption
+       special.erase();                // additional userdefined stuff
 }
 
+
 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
 {
        filename = igp.filename;
-       bb = igp.bb;
-       draft = igp.draft;
-       clip = igp.clip;
+       lyxscale = igp.lyxscale;
        display = igp.display;
-       subcaption = igp.subcaption;
-       subcaptionText = igp.subcaptionText;
-       keepAspectRatio = igp.keepAspectRatio;
+       scale = igp.scale;
        width = igp.width;
        height = igp.height;
-       scale = igp.scale;
-       size_type = igp.size_type;
-       lyxwidth = igp.lyxwidth;
-       lyxheight = igp.lyxheight;
-       rotateOrigin = igp.rotateOrigin;
+       keepAspectRatio = igp.keepAspectRatio;
+       draft = igp.draft;
+       noUnzip = igp.noUnzip;
+
+       bb = igp.bb;
+       clip = igp.clip;
+
        rotateAngle = igp.rotateAngle;
+       rotateOrigin = igp.rotateOrigin;
+       subcaption = igp.subcaption;
+       subcaptionText = igp.subcaptionText;
        special = igp.special;
-
-       testInvariant();
 }
 
-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
-             );
-       // 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);
-
-}
 
 bool operator==(InsetGraphicsParams const & left,
-                InsetGraphicsParams const & right)
+               InsetGraphicsParams const & right)
 {
        if (left.filename == right.filename &&
-               left.bb == right.bb &&
-               left.draft == right.draft &&
-               left.clip == right.clip &&
-               left.display == right.display &&
-               left.subcaption == right.subcaption &&
-               left.subcaptionText == right.subcaptionText &&
-               left.keepAspectRatio == right.keepAspectRatio &&
-               left.width == right.width &&
-               left.height == right.height &&
-               left.scale == right.scale &&
-               left.size_type == right.size_type &&
-               left.lyxwidth == right.lyxwidth &&
-               left.lyxheight == right.lyxheight &&
-               left.rotateOrigin == right.rotateOrigin &&
-               lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
-               left.special == right.special) 
+           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);
 }
 
 
-void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
+void InsetGraphicsParams::Write(ostream & os, string const & bufpath) const
 {
-       // If there is no filename, write nothing for it.
-       if (! filename.empty()) {
-               os << "\tfilename "
-               << MakeRelPath(filename, buf->filePath())
-               << '\n';
+       // Do not write the default values
+
+       if (!filename.empty()) {
+               os << "\tfilename " << filename.outputFilename(bufpath) << '\n';
        }
+       if (lyxscale != 100)
+               os << "\tlyxscale " << lyxscale << '\n';
+       if (display != lyx::graphics::DefaultDisplay)
+               os << "\tdisplay " << lyx::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';
+       }
+
+       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 (draft)                      // draft mode
-               os << "\tdraft\n";
-       // Save the display type
-       os << "\tdisplay " << displayTranslator.find(display) << '\n';
-       // Save the subcaption status
-       if (subcaption)
-           os << "\tsubcaption\n";
-       if (!subcaptionText.empty())
-           os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
-    // we always need the size type
-    // 0: no special
-    // 1: width/height combination
-    // 2: scale
-       os << "\tsize_type " <<  size_type << '\n';
-       if (!width.zero())
-           os << "\twidth " << width.asString() << '\n';
-       if (!height.zero())
-           os << "\theight " << height.asString() << '\n';
-       if (scale != 0)
-           os << "\tscale " << scale << '\n';
-       if (keepAspectRatio)
-               os << "\tkeepAspectRatio\n";
-       if (!lyx::float_equal(rotateAngle, 0.0, 0.001))
+
+       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';
-       if (!lyxwidth.zero())           // the lyx-viewsize
-           os << "\tlyxwidth " << lyxwidth.asString() << '\n';
-       if (!lyxheight.zero())
-           os << "\tlyxheight " << lyxheight.asString();
 }
 
 
-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, buf->filePath());
-               }
-       } else if (token == "BoundingBox") {
-               for (int i=0; i<4 ;i++) {
-                   lex.next();
-                   bb += (lex.getString()+" ");
-               }
-       } else if (token == "clip") {
-               clip = true;
-       } else if (token == "draft") {
-               draft = true;
+               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") {
-               if (lex.next()) {
-                   string const token = lex.getString();
-                   if (token == "scale") {
-                       lex.next();
-                       scale = lex.getInteger();
-                       size_type = SCALE;
-                   }
-                   else {
-                       width = convertResizeValue(token, lex);
-                       size_type = WH;
-                   }
-               }
-       } else if (token == "size_type") {
+               display = lyx::graphics::displayTranslator().find(type);
+       } else if (token == "scale") {
                lex.next();
-               switch (lex.getInteger()) {
-                   case 0 : size_type = DEFAULT_SIZE;
-                       break;
-                   case 1 : size_type = WH;
-                       break;
-                   case 2 : size_type = SCALE;
-               }
+               scale = lex.getString();
        } else if (token == "width") {
                lex.next();
                width = LyXLength(lex.getString());
-               size_type = WH;
-       } else if (token == "heightResize") {
-               if (lex.next())
-                       height = convertResizeValue(lex.getString(), lex);
+               scale = string();
        } else if (token == "height") {
                lex.next();
                height = LyXLength(lex.getString());
-               size_type = WH;
+               scale = string();
        } else if (token == "keepAspectRatio") {
                keepAspectRatio = true;
-       } else if (token == "scale") {
-               lex.next();
-               scale = lex.getInteger();
+       } 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.getFloat();
+               rotateAngle = lex.getString();
        } else if (token == "rotateOrigin") {
                lex.next();
                rotateOrigin=lex.getString();
-       } else if (token == "lyxwidth") {
-               lex.next();
-               lyxwidth = LyXLength(lex.getString());
-       } else if (token == "lyxheight") {
+       } 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();
-               lyxheight = LyXLength(lex.getString());
+               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;
 }
+
+
+lyx::graphics::Params InsetGraphicsParams::as_grfxParams() const
+{
+       lyx::graphics::Params pars;
+       pars.filename = filename.absFilename();
+       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.absFilename());
+               lyxerr[Debug::GRAPHICS] << "BB_from_File: " << tmp << std::endl;
+               if (!tmp.empty()) {
+#warning why not convert to unsigned int? (Lgb)
+                       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 == lyx::graphics::DefaultDisplay) {
+               pars.display = lyxrc.display_graphics;
+       } else {
+               pars.display = display;
+       }
+
+       // Override the above if we're not using a gui
+       if (!lyx_gui::use_gui) {
+               pars.display = lyx::graphics::NoDisplay;
+       }
+
+       return pars;
+}