]> Shamusworld >> Repos - ttedit/blobdiff - src/charwindow.cpp
Added preview window to file loading dialog. :-)
[ttedit] / src / charwindow.cpp
index 76112e4f8e92c96d59fc15ba45fd0bc84b9fd9b3..06b273dc79dee5aec307d0288a709d6820007807 100644 (file)
 #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;
@@ -49,10 +50,10 @@ void CharWindow::MakePathFromPoints(GlyphPoints * gp)
                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).
+               // 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)));
@@ -70,10 +71,10 @@ void CharWindow::MakePathFromPoints(GlyphPoints * gp)
                                if (gp->GetOnCurve(poly, i))
                                        continue;
 
-                               // 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.
+                               // 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));
 
@@ -83,18 +84,66 @@ void CharWindow::MakePathFromPoints(GlyphPoints * gp)
 
                path->closeSubpath();
        }
+
+       return path;
 }
 
+
+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)
@@ -129,8 +178,6 @@ void CharWindow::paintEvent(QPaintEvent * /*event*/)
        {
                // width is limiting factor (smaller than height)
                p.scale(xConvFac, -xConvFac);
-//extraY = (rect.width() / (float)paintSize.width()) * (float)paintSize.height();
-//extraY = (extraY - rect.height()) / 2.0f;
                extraY = (((float)paintSize.height() / xConvFac) - rect.height()) / 2.0f;
        }
 
@@ -138,3 +185,4 @@ void CharWindow::paintEvent(QPaintEvent * /*event*/)
 
        p.drawPath(*path);
 }
+