]> Shamusworld >> Repos - ttedit/blobdiff - src/charwindow.cpp
Added preview window to file loading dialog. :-)
[ttedit] / src / charwindow.cpp
old mode 100755 (executable)
new mode 100644 (file)
index a6da0f9..06b273d
@@ -6,7 +6,7 @@
 // JLH = James L. Hammons <jlhamm@acm.org>
 //
 // Who  When        What
-// ---  ----------  -------------------------------------------------------------
+// ---  ----------  -----------------------------------------------------------
 // JLH  08/28/2008  Created this file
 // JLH  03/19/2009  Converted from wxWidgets to Qt
 // JLH  03/21/2009  Fixed main screen points rendering
 #include "charwindow.h"
 #include "debug.h"
 
-CharWindow::CharWindow(QWidget * parent/*= NULL*/): QWidget(parent, Qt::Tool), path(NULL)
+
+CharWindow::CharWindow(QWidget * parent/*= NULL*/): QWidget(parent, Qt::Window), path(NULL)
 {
        setWindowTitle("Character: Unknown");
 }
 
-void CharWindow::MakePathFromPoints(GlyphPoints * gp)
+
+QPainterPath * CharWindow::MakePathFromPoints(GlyphPoints * gp)
 {
        if (gp == NULL)
-               return;
+               return NULL;
 
        if (path != NULL)
                delete path;
@@ -45,65 +47,103 @@ void CharWindow::MakePathFromPoints(GlyphPoints * gp)
 
        for(int poly=0; poly<gp->GetNumPolys(); poly++)
        {
-               if (gp->GetNumPoints(poly) > 2)
+               if (gp->GetNumPoints(poly) < 3)
+                       continue;
+
+               // Initial move: If our start point is on curve, then go to it.
+               // Otherwise, check previous point. If it's on curve, go to it
+               // otherwise go the midpoint between start point and previous (since
+               // it's between two curve control points).
+               IPoint pt = (gp->GetOnCurve(poly, 0)
+                       ? gp->GetPoint(poly, 0) : (gp->GetPrevOnCurve(poly, 0)
+                               ? gp->GetPrevPoint(poly, 0) : gp->GetMidpointToPrev(poly, 0)));
+               path->moveTo(pt.x, pt.y);
+
+               for(int i=0; i<gp->GetNumPoints(poly); i++)
                {
-                       // Initial move...
-                       // If last point is on curve then move to it, otherwise move to first point...
-
-                       int x, y;
-
-                       if (gp->GetOnCurve(poly, gp->GetPrev(poly, 0)))
-                               x = gp->GetX(poly, gp->GetPrev(poly, 0)),
-                               y = gp->GetY(poly, gp->GetPrev(poly, 0));
+                       // If this point and then next are both on curve, we have a line...
+                       if (gp->GetOnCurve(poly, i) && gp->GetNextOnCurve(poly, i))
+                               path->lineTo(gp->GetNextX(poly, i), gp->GetNextY(poly, i));
                        else
                        {
-                               x = (int)gp->GetX(poly, 0), y = (int)gp->GetY(poly, 0);
+                               // Skip point if it's on curve (start of curve--it's already
+                               // been plotted so we don't need to handle it...)
+                               if (gp->GetOnCurve(poly, i))
+                                       continue;
 
-                               if (!gp->GetOnCurve(poly, 0))
-                               {
-                                       IPoint pt = gp->GetMidpointToPrev(poly, 0);
-                                       x = pt.x, y = pt.y;
-                               }
+                               // We are now guaranteed that we are sitting on a curve control
+                               // point (off curve). Figure the extent of the curve: If the
+                               // following is a curve control point, then use the midpoint to
+                               // it otherwise go to the next point since it's on curve.
+                               IPoint pt = (gp->GetNextOnCurve(poly, i)
+                                       ? gp->GetNextPoint(poly, i) : gp->GetMidpointToNext(poly, i));
+
+                               path->quadTo(gp->GetX(poly, i), gp->GetY(poly, i), pt.x, pt.y);
                        }
+               }
 
-                       path->moveTo(x, y);
+               path->closeSubpath();
+       }
 
-                       for(int i=0; i<gp->GetNumPoints(poly); i++)
-                       {
-                               if (gp->GetOnCurve(poly, i))
-                               {
-//                                     p.drawLine(x, y, gp->GetX(poly, i), gp->GetY(poly, i));
-                                       x = (int)gp->GetX(poly, i), y = (int)gp->GetY(poly, i);
-                                       path->lineTo(x, y);
-                               }
-                               else
-                               {
-                                       IPoint pt = (gp->GetOnCurve(poly, gp->GetNext(poly, i)) ? gp->GetNextPoint(poly, i) : gp->GetMidpointToNext(poly, i));
-
-                                       path->quadTo(gp->GetX(poly, i), gp->GetY(poly, i), pt.x, pt.y);
-                                       x = pt.x, y = pt.y;
-
-                                       // If following point is on curve, move past it
-                                       if (gp->GetOnCurve(poly, gp->GetNext(poly, i)))
-                                               i++;
-                               }
-                       }
+       return path;
+}
 
-                       path->closeSubpath();
-               }
+
+void CharWindow::RenderPathInImage(QPainterPath * path, QImage * img)
+{
+       if ((path == NULL) || (img == NULL))
+               return;
+
+       QPainter p(img);
+
+       p.setPen(QPen(Qt::black, 1.0, Qt::SolidLine));
+       p.setBrush(Qt::black);
+
+       QRectF rect = path->boundingRect();
+       QSize paintSize = img->size();
+
+#if 0
+       // For some reason, this code cuts off two pixels when rendering the path.
+       // Not sure why, but we compensate for that here.
+       paintSize.rwidth() -= 2;
+       paintSize.rheight() -= 2;
+#endif
+
+       p.translate(0, paintSize.height());
+       float extraX = 0.0f, extraY = 0.0f;
+       float xConvFac = (float)paintSize.width() / rect.width();
+       float yConvFac = (float)paintSize.height() / rect.height();
+
+       if (xConvFac > yConvFac)
+       {
+               // height is limiting factor (smaller than width)
+               p.scale(yConvFac, -yConvFac);
+               extraX = (((float)paintSize.width() / yConvFac) - rect.width()) / 2.0f;
        }
+       else
+       {
+               // width is limiting factor (smaller than height)
+               p.scale(xConvFac, -xConvFac);
+               extraY = (((float)paintSize.height() / xConvFac) - rect.height()) / 2.0f;
+       }
+
+       p.translate(-rect.x() + extraX, -rect.y() + extraY);
+       p.drawPath(*path);
 }
 
+
 QSize CharWindow::minimumSizeHint() const
 {
        return QSize(50, 50);
 }
 
+
 QSize CharWindow::sizeHint() const
 {
        return QSize(200, 200);
 }
 
+
 void CharWindow::paintEvent(QPaintEvent * /*event*/)
 {
        if (path == NULL)
@@ -111,53 +151,38 @@ void CharWindow::paintEvent(QPaintEvent * /*event*/)
 
        QPainter p(this);
 
-       p.setBrush(QColor(122, 163, 39));
-// Need to translate as well...
-//     p.scale(2.0, 2.0);
-
-/*
-1.0 -> 3.0, height = 400
-r.h / ps.h = 2/400 <-- do it the other way!
+//     p.setBrush(QColor(0, 163, 200));        // Nice, aqua color...
+       p.setPen(QPen(Qt::black, 1.0, Qt::SolidLine));
+       p.setBrush(Qt::black);
 
-height works, width does not
-
-2-step process:
-compare aspect ratios
-
-ps.w - ((r.h / ps.h) * ps.w)
-
-0.5 -> where in the 400? -> 100
-0.5/r.h(2.0) = 0.25 * ps.h(400) = 100
-conv.fac. -> (ps.h / r.h)
-*/
        QRectF rect = path->boundingRect();
        QSize paintSize = size();
 
+       // For some reason, this code cuts off two pixels when rendering the path.
+       // Not sure why, but we compensate for that here.
+       paintSize.rwidth() -= 2;
+       paintSize.rheight() -= 2;
+
        p.translate(0, paintSize.height());
-float extraX = 0.0f, extraY = 0.0f;
-float xConvFac = (float)paintSize.width() / rect.width();
-float yConvFac = (float)paintSize.height() / rect.height();
+       float extraX = 0.0f, extraY = 0.0f;
+       float xConvFac = (float)paintSize.width() / rect.width();
+       float yConvFac = (float)paintSize.height() / rect.height();
 
-//     if (((float)paintSize.width() / rect.width()) > ((float)paintSize.height() / rect.height()))
        if (xConvFac > yConvFac)
        {
-               // height is limiting factor
+               // height is limiting factor (smaller than width)
                p.scale(yConvFac, -yConvFac);
-//extraX = (rect.height() / (float)paintSize.height()) * (float)paintSize.width();
-//extraX = (extraX - rect.width()) / 2.0f;
                extraX = (((float)paintSize.width() / yConvFac) - rect.width()) / 2.0f;
        }
        else
        {
-               // width is limiting factor
-//             p.scale((float)paintSize.width() / rect.width(), -(float)paintSize.width() / rect.height());
-               p.scale((float)paintSize.width() / rect.width(), -(float)paintSize.width() / rect.width());
-extraY = (rect.width() / (float)paintSize.width()) * (float)paintSize.height();// / 2.0f;
-extraY = extraY - rect.height();
-extraY /= 2.0f;
+               // width is limiting factor (smaller than height)
+               p.scale(xConvFac, -xConvFac);
+               extraY = (((float)paintSize.height() / xConvFac) - rect.height()) / 2.0f;
        }
 
        p.translate(-rect.x() + extraX, -rect.y() + extraY);
 
        p.drawPath(*path);
 }
+