#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;
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)));
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));
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)
{
// 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;
}
p.drawPath(*path);
}
+