]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetgraphicsParams.C
move inset related stuff from src/graphics to src/inset/
[lyx.git] / src / insets / insetgraphicsParams.C
index 405368d2fff93711a48d207ddd486cf875bf461a..41c61fdb7851eb9e8a4c8b045e1da5d8246feecd 100644 (file)
@@ -1,6 +1,6 @@
 /* This file is part of
  * =================================================
- * 
+ *
  *          LyX, The Document Processor
  *          Copyright 1995 Matthias Ettrich.
  *          Copyright 1995-2001 The LyX Team.
  *
  * ================================================= */
 
-#include <config.h> 
+#include <config.h>
 
 #ifdef __GNUG__
 #pragma implementation
-#endif 
+#endif
 
 #include "insetgraphicsParams.h"
 
@@ -23,6 +23,9 @@
 #include "support/lyxlib.h"
 #include "support/LOstream.h"
 #include "support/LAssert.h"
+#include "support/lstrings.h"
+#include "lyxrc.h"
+#include "debug.h"
 
 
 using std::ostream;
@@ -39,26 +42,6 @@ bool translatorsSet = false;
 Translator< InsetGraphicsParams::DisplayType, string >
 displayTranslator(InsetGraphicsParams::DEFAULT, "default");
 
-// 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
-}
-
 } // namespace anon
 
 
@@ -114,7 +97,7 @@ void InsetGraphicsParams::init()
        size_type = DEFAULT_SIZE;       // do nothing
        lyxsize_type = DEFAULT_SIZE;    // do nothing
        keepAspectRatio = false;        // only for latex
-       rotate = false;                 // Rotating 
+       rotate = false;                 // Rotating
        rotateOrigin = "center";        // Origin
        rotateAngle = 0.0;              // in degrees
        special = string();             // userdefined stuff
@@ -146,49 +129,47 @@ void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
 }
 
 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.noUnzip == right.noUnzip &&
-               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.lyxsize_type == right.lyxsize_type &&
-               left.lyxwidth == right.lyxwidth &&
-               left.lyxheight == right.lyxheight &&
-               left.lyxscale == right.lyxscale &&
-               left.rotate == right.rotate &&
-               left.rotateOrigin == right.rotateOrigin &&
-               lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
-               left.special == right.special) 
-         )
+           left.bb == right.bb &&
+           left.draft == right.draft &&
+           left.clip == right.clip &&
+           left.display == right.display &&
+           left.subcaption == right.subcaption &&
+           left.noUnzip == right.noUnzip &&
+           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.lyxsize_type == right.lyxsize_type &&
+           left.lyxwidth == right.lyxwidth &&
+           left.lyxheight == right.lyxheight &&
+           left.lyxscale == right.lyxscale &&
+           left.rotate == right.rotate &&
+           left.rotateOrigin == right.rotateOrigin &&
+           lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
+                            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) const
 {
        // If there is no filename, write nothing for it.
-       if (! filename.empty()) {
-               os << "\tfilename "
-               << MakeRelPath(filename, buf->filePath())
-               << '\n';
+       if (!filename.empty()) {
+               os << "\tfilename " << filename << '\n';
        }
        if (!bb.empty())                // bounding box
                os << "\tBoundingBox " << bb << '\n';
@@ -200,27 +181,27 @@ void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
        os << "\tdisplay " << displayTranslator.find(display) << '\n';
        // Save the subcaption status
        if (subcaption)
-           os << "\tsubcaption\n";
+               os << "\tsubcaption\n";
        if (!subcaptionText.empty())
-           os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
+               os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
        if (noUnzip)
-           os << "\tnoUnzip\n";
-    // we always need the size type
-    // 0: no special
-    // 1: width/height combination
-    // 2: scale
+               os << "\tnoUnzip\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';
+               os << "\twidth " << width.asString() << '\n';
        if (!height.zero())
-           os << "\theight " << height.asString() << '\n';
+               os << "\theight " << height.asString() << '\n';
        if (scale != 0)
-           os << "\tscale " << scale << '\n';
+               os << "\tscale " << scale << '\n';
        if (keepAspectRatio)
                os << "\tkeepAspectRatio\n";
        if (rotate)
                os << "\trotate\n";
-       if (!lyx::float_equal(rotateAngle, 0.0, 0.001))
+       if (rotateAngle != 0.0)
                os << "\trotateAngle " << rotateAngle << '\n';
        if (!rotateOrigin.empty())
                os << "\trotateOrigin " << rotateOrigin << '\n';
@@ -229,28 +210,23 @@ void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
        // the values for the view in lyx
        os << "\tlyxsize_type " <<  lyxsize_type << '\n';
        if (!lyxwidth.zero())           // the lyx-viewsize
-           os << "\tlyxwidth " << lyxwidth.asString() << '\n';
+               os << "\tlyxwidth " << lyxwidth.asString() << '\n';
        if (!lyxheight.zero())
-           os << "\tlyxheight " << lyxheight.asString();
+               os << "\tlyxheight " << lyxheight.asString();
        if (lyxscale != 0)
-           os << "\tlyxscale " << lyxscale << '\n';
+               os << "\tlyxscale " << lyxscale << '\n';
 }
 
 
-bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
-                               string const& token)
+bool InsetGraphicsParams::Read(LyXLex & lex, string const & token)
 {
        if (token == "filename") {
                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()+" ");
+                       lex.next();
+                       bb += (lex.getString()+" ");
                }
        } else if (token == "clip") {
                clip = true;
@@ -270,20 +246,22 @@ bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
        } else if (token == "size_type") {
                lex.next();
                switch (lex.getInteger()) {
-                   case 0 : size_type = DEFAULT_SIZE;
+               case 0:
+                       size_type = DEFAULT_SIZE;
                        break;
-                   case 1 : size_type = WH;
+               case 1:
+                       size_type = WH;
+                       break;
+               case 2:
+                       size_type = SCALE;
                        break;
-                   case 2 : size_type = SCALE;
                }
        } else if (token == "width") {
                lex.next();
                width = LyXLength(lex.getString());
-               size_type = WH;
        } else if (token == "height") {
                lex.next();
                height = LyXLength(lex.getString());
-               size_type = WH;
        } else if (token == "keepAspectRatio") {
                keepAspectRatio = true;
        } else if (token == "scale") {
@@ -300,11 +278,15 @@ bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
        } else if (token == "lyxsize_type") {
                lex.next();
                switch (lex.getInteger()) {
-                   case 0 : lyxsize_type = DEFAULT_SIZE;
+               case 0:
+                       lyxsize_type = DEFAULT_SIZE;
+                       break;
+               case 1:
+                       lyxsize_type = WH;
                        break;
-                   case 1 : lyxsize_type = WH;
+               case 2:
+                       lyxsize_type = SCALE;
                        break;
-                   case 2 : lyxsize_type = SCALE;
                }
        } else if (token == "lyxwidth") {
                lex.next();
@@ -315,28 +297,108 @@ bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
        } else if (token == "lyxscale") {
                lex.next();
                lyxscale = lex.getInteger();
-       // now the compytibility stuff for "old" 1.2.0 files which uses
-       // the first try of the new graphic inset. Can be deleted, when
-       // 1.3 comes out
-       } 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 == "heightResize") {
-               if (lex.next())
-                       height = convertResizeValue(lex.getString(), lex);
-       // end compytibility stuff
+       } else if (token == "special") {
+               lex.eatLine();
+               special = lex.getString();
        } else {        // If it's none of the above, its not ours.
                return false;
        }
        return true;
 }
+
+
+grfx::GParams InsetGraphicsParams::asGParams(string const & filepath) const
+{
+       grfx::GParams pars;
+       pars.width    = 0;
+       pars.height   = 0;
+       pars.scale    = 0;
+       pars.angle    = 0;
+       pars.filename = filename;
+
+       if (!filepath.empty()) {
+               pars.filename = MakeAbsPath(pars.filename, filepath);
+       }
+
+       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()) {
+                       int const bb_orig_xl = strToInt(token(tmp, ' ', 0));
+                       int const bb_orig_yb = strToInt(token(tmp, ' ', 1));
+
+                       pars.bb.xl -= bb_orig_xl;
+                       pars.bb.xr -= bb_orig_xl;
+                       pars.bb.yb -= bb_orig_yb;
+                       pars.bb.yt -= bb_orig_yb;
+               }
+
+               pars.bb.xl = std::max(0, pars.bb.xl);
+               pars.bb.xr = std::max(0, pars.bb.xr);
+               pars.bb.yb = std::max(0, pars.bb.yb);
+               pars.bb.yt = std::max(0, pars.bb.yt);
+
+               // 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 (rotate)
+               pars.angle = int(rotateAngle);
+
+       if (display == InsetGraphicsParams::DEFAULT) {
+
+               if (lyxrc.display_graphics == "mono")
+                       pars.display = grfx::GParams::MONOCHROME;
+               else if (lyxrc.display_graphics == "gray")
+                       pars.display = grfx::GParams::GRAYSCALE;
+               else if (lyxrc.display_graphics == "color")
+                       pars.display = grfx::GParams::COLOR;
+               else
+                       pars.display = grfx::GParams::NONE;
+
+       } else if (display == InsetGraphicsParams::NONE) {
+               pars.display = grfx::GParams::NONE;
+
+       } else if (display == InsetGraphicsParams::MONOCHROME) {
+               pars.display = grfx::GParams::MONOCHROME;
+
+       } else if (display == InsetGraphicsParams::GRAYSCALE) {
+               pars.display = grfx::GParams::GRAYSCALE;
+
+       } else if (display == InsetGraphicsParams::COLOR) {
+               pars.display = grfx::GParams::COLOR;
+       }
+
+       // Override the above if we're not using a gui
+       if (!lyxrc.use_gui) {
+               pars.display = grfx::GParams::NONE;
+       }
+
+       if (lyxsize_type == InsetGraphicsParams::SCALE) {
+               pars.scale = lyxscale;
+
+       } else if (lyxsize_type == InsetGraphicsParams::WH) {
+               if (!lyxwidth.zero())
+                       pars.width  = lyxwidth.inPixels(1, 1);
+               if (!lyxheight.zero())
+                       pars.height = lyxheight.inPixels(1, 1);
+
+               // inPixels returns a value scaled by lyxrc.zoom.
+               // We want, therefore, to undo this.
+               double const scaling_factor = 100.0 / double(lyxrc.zoom);
+               pars.width  = uint(scaling_factor * pars.width);
+               pars.height = uint(scaling_factor * pars.height);
+       }
+       return pars;
+}