On 03/19/2014 12:45 PM, Richard Heck wrote:
On 03/19/2014 02:41 AM, Liviu Andronic wrote:
On Wed, Mar 19, 2014 at 5:09 AM, aparsloe <apars...@clear.net.nz> wrote:
On 18/03/2014 8:48 p.m., Vincent van Ravesteijn wrote:
On Tue, Mar 18, 2014 at 2:33 AM, Richard Heck <rgh...@lyx.org> wrote:
On 03/17/2014 06:51 PM, aparsloe wrote:

On 18/03/2014 11:07 a.m., Liviu Andronic wrote:
On Mon, Mar 17, 2014 at 10:30 PM, aparsloe <apars...@clear.net.nz>
wrote:
If an instant preview inset is wrapped around something that produces
empty
LaTeX output, the preview inset becomes invisible in the LyX work-area
window, and can sometimes be quite hard to re-locate.

Is this related to: http://www.lyx.org/trac/ticket/8636 ?

Liviu
Yes. Thanks for pointing me to this ticket Liviu, which is essentially
the
same problem as http://www.lyx.org/trac/ticket/7522, although from a
quick
scan neither ticket makes any mention of possible malicious use of this "feature", as suggested below. A \gobble command really does "eat" into
the
pdf, and with more elaborate coding could do real damage while being
invisible in the LyX document.

I agree this is a problem. It seems to me that if we have problems
compiling, then we should simply fail and not try to display a preview image. Of course, that does not deal with the invisible image. I don't
know
what to do there other than to test its size and, again, refuse to
display
the preview if it is too small.

Richard

It is then adviced to make the background of the previews slightly
different than the background of the text area.

For the empty images (with size 0x0), we should indeed paint something
else (on my long, long todo list).

Vincent
I'm not a developer and so have no idea of the feasibility of this. I wonder if it is possible to have alternative views of a preview inset? One would be
the current one, trying to give a faithful rendering of the inset's
contents; the other would be in the style of Note insets with a label and
different background. Even for empty insets the label would be there to
alert the reader, so there would be no need for code to test the inset's
contents.

I agree. Instead of issuing a warning or whatever if the preview image
is less than 5px, we could simply add label (or red frame) to any
preview less than 5px. And no label for any preview bigger than that.
This should take care of making small or inexistent previews
immediately visible.

Here are a couple patches that do this sort of thing for math, at least. There are two parts: (i) If compilation fails, then we return an error and so not attempt to display a preview image (that seems right not matter what else we do); (ii) If the image is too small, we draw a 2px frame around it in the error color.

Thoughts?

Whoops.


>From b50d12a07b35d3044a149001013020b6655b37d4 Mon Sep 17 00:00:00 2001
From: Richard Heck <rgh...@lyx.org>
Date: Wed, 19 Mar 2014 12:42:57 -0400
Subject: [PATCH 1/2] Return an error if LaTeX is unable to compile the file.

---
 lib/scripts/legacy_lyxpreview2ppm.py | 2 ++
 lib/scripts/lyxpreview2bitmap.py     | 2 ++
 2 files changed, 4 insertions(+)

diff --git a/lib/scripts/legacy_lyxpreview2ppm.py b/lib/scripts/legacy_lyxpreview2ppm.py
index fdc5a8e..d16e867 100644
--- a/lib/scripts/legacy_lyxpreview2ppm.py
+++ b/lib/scripts/legacy_lyxpreview2ppm.py
@@ -293,6 +293,8 @@ def legacy_conversion_step1(latex_file, dpi, output_format, fg_color, bg_color,
 
     # Compile the latex file.
     latex_status, latex_stdout = run_latex(latex, latex_file)
+    if latex_status:
+      return (latex_status, [])
 
     if pdf_output:
         return legacy_conversion_step3(latex_file, dpi, output_format, True, skipMetrics)
diff --git a/lib/scripts/lyxpreview2bitmap.py b/lib/scripts/lyxpreview2bitmap.py
index 4a2cddb..ace46d2 100755
--- a/lib/scripts/lyxpreview2bitmap.py
+++ b/lib/scripts/lyxpreview2bitmap.py
@@ -409,6 +409,8 @@ def main(argv):
 
     # Compile the latex file.
     latex_status, latex_stdout = run_latex(latex, latex_file, bibtex)
+    if latex_status:
+      return (latex_status, [])
 
     # The dvi output file name
     dvi_file = latex_file_re.sub(".dvi", latex_file)
-- 
1.7.11.7

>From 9af33a4575ade5a61298da653d9df727f8a4d4f6 Mon Sep 17 00:00:00 2001
From: Richard Heck <rgh...@lyx.org>
Date: Wed, 19 Mar 2014 12:43:21 -0400
Subject: [PATCH 2/2] Draw a small red frame around preview images that are
 too small.

---
 src/mathed/InsetMathHull.cpp | 57 +++++++++++++++++++++++++++++++++++---------
 src/mathed/InsetMathHull.h   |  4 +++-
 2 files changed, 49 insertions(+), 12 deletions(-)

diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp
index e994d44..4c20625 100644
--- a/src/mathed/InsetMathHull.cpp
+++ b/src/mathed/InsetMathHull.cpp
@@ -44,6 +44,7 @@
 #include "insets/InsetRef.h"
 #include "insets/RenderPreview.h"
 
+#include "graphics/GraphicsImage.h"
 #include "graphics/PreviewImage.h"
 #include "graphics/PreviewLoader.h"
 
@@ -410,7 +411,7 @@ ColorCode InsetMathHull::standardColor() const
 }
 
 
-bool InsetMathHull::previewState(BufferView * bv) const
+bool InsetMathHull::previewState(const BufferView *const bv) const
 {
 	if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON
 		&& type_ != hullRegexp)
@@ -423,14 +424,24 @@ bool InsetMathHull::previewState(BufferView * bv) const
 }
 
 
+namespace {
+static const int ERROR_FRAME_WIDTH = 2;
+}
+
 void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
 {
 	if (previewState(mi.base.bv)) {
 		preview_->metrics(mi, dim);
-		// insert a one pixel gap in front of the formula
-		dim.wid += 1;
-		if (display())
-			dim.des += displayMargin();
+		if (previewTooSmall(dim)) {
+			// preview image is too small
+			dim.wid += 2 * ERROR_FRAME_WIDTH;
+			dim.asc += 2 * ERROR_FRAME_WIDTH;
+		} else {
+			// insert a one pixel gap in front of the formula
+			dim.wid += 1;
+			if (display())
+				dim.des += displayMargin();
+		}
 		// Cache the inset dimension.
 		setDimCache(mi, dim);
 		return;
@@ -471,10 +482,21 @@ void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
 }
 
 
+bool InsetMathHull::previewTooSmall(Dimension const & dim) const
+{
+	return dim.width() <= 10 && dim.height() <= 10;
+}
+
+
 ColorCode InsetMathHull::backgroundColor(PainterInfo const & pi) const
 {
-	if (previewState(pi.base.bv))
+	BufferView const * const bv = pi.base.bv;
+	if (previewState(bv)) {
+		Dimension const dim = dimension(*pi.base.bv);
+		if (previewTooSmall(dim))
+			return Color_error;
 		return graphics::PreviewLoader::backgroundColor();
+	}
 	return Color_mathbg;
 }
 
@@ -482,23 +504,36 @@ ColorCode InsetMathHull::backgroundColor(PainterInfo const & pi) const
 void InsetMathHull::drawBackground(PainterInfo & pi, int x, int y) const
 {
 	Dimension const dim = dimension(*pi.base.bv);
+	if (previewTooSmall(dim)) {
+		pi.pain.fillRectangle(x, y - 2 * ERROR_FRAME_WIDTH, 
+		    dim.wid, dim.asc + dim.des, backgroundColor(pi));
+		return;
+	} 
 	pi.pain.fillRectangle(x + 1, y - dim.asc + 1, dim.wid - 2,
-		dim.asc + dim.des - 1, pi.backgroundColor(this));
+			dim.asc + dim.des - 1, backgroundColor(pi));
 }
 
 
 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
 {
-	use_preview_ = previewState(pi.base.bv);
+	BufferView const * const bv = pi.base.bv;
+	use_preview_ = previewState(bv);
 
 	if (type_ == hullRegexp) {
-		Dimension const dim = dimension(*pi.base.bv);
+		Dimension const dim = dimension(*bv);
 		pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
 			dim.width() - 2, dim.height() - 2, Color_regexpframe);
 	}
+
 	if (use_preview_) {
-		// one pixel gap in front
-		preview_->draw(pi, x + 1, y);
+		Dimension const dim = dimension(*bv);
+		if (previewTooSmall(dim)) {
+			// we have an extra frame
+			preview_->draw(pi, x + ERROR_FRAME_WIDTH, y);
+		} else {
+			// one pixel gap in front
+			preview_->draw(pi, x + 1, y);
+		}
 		setPosCache(pi, x, y);
 		return;
 	}
diff --git a/src/mathed/InsetMathHull.h b/src/mathed/InsetMathHull.h
index af00eaf..0cffffd 100644
--- a/src/mathed/InsetMathHull.h
+++ b/src/mathed/InsetMathHull.h
@@ -271,7 +271,9 @@ protected:
 	///
 	void handleFont2(Cursor & cur, docstring const & arg);
 	///
-	bool previewState(BufferView * bv) const;
+	bool previewState(BufferView const * const bv) const;
+	///
+	bool previewTooSmall(Dimension const & dim) const;
 };
 
 
-- 
1.7.11.7

Reply via email to