2 // TTEDIT.CPP - The TrueType Editor
4 // (C) 2004 Underground Software
6 // JLH = James L. Hammons <jlhamm@acm.org>
9 // --- ---------- -------------------------------------------------------------
10 // JLH 08/28/2008 Created this file
11 // JLH 03/19/2009 Converted from wxWidgets to Qt
12 // JLH 03/21/2009 Fixed main screen points rendering
17 // - Render of main window points [DONE]
21 // - Fix "window disappears when tool win comes up" problem
24 #include "charwindow.h"
27 CharWindow::CharWindow(QWidget * parent/*= NULL*/): QWidget(parent, Qt::Tool), path(NULL)
29 setWindowTitle("Character: Unknown");
32 void CharWindow::MakePathFromPoints(GlyphPoints * gp)
40 path = new QPainterPath();
41 // path->setFillRule(Qt::OddEvenFill);
42 path->setFillRule(Qt::WindingFill);
44 // Draw curve formed by points
46 for(int poly=0; poly<gp->GetNumPolys(); poly++)
48 if (gp->GetNumPoints(poly) < 3)
51 // Initial move: If our start point is on curve, then go to it. Otherwise,
52 // check previous point. If it's on curve, go to it otherwise go the
53 // midpoint between start point and previous (since it's between two curve
55 IPoint pt = (gp->GetOnCurve(poly, 0)
56 ? gp->GetPoint(poly, 0) : (gp->GetPrevOnCurve(poly, 0)
57 ? gp->GetPrevPoint(poly, 0) : gp->GetMidpointToPrev(poly, 0)));
58 path->moveTo(pt.x, pt.y);
60 for(int i=0; i<gp->GetNumPoints(poly); i++)
62 // If this point and then next are both on curve, we have a line...
63 if (gp->GetOnCurve(poly, i) && gp->GetNextOnCurve(poly, i))
64 path->lineTo(gp->GetNextX(poly, i), gp->GetNextY(poly, i));
67 // Skip point if it's on curve (start of curve--it's already
68 // been plotted so we don't need to handle it...)
69 if (gp->GetOnCurve(poly, i))
72 // We are now guaranteed that we are sitting on a curve control point
73 // (off curve). Figure the extent of the curve: If the following is a
74 // curve control point, then use the midpoint to it otherwise go to
75 // the next point since it's on curve.
76 IPoint pt = (gp->GetNextOnCurve(poly, i)
77 ? gp->GetNextPoint(poly, i) : gp->GetMidpointToNext(poly, i));
79 path->quadTo(gp->GetX(poly, i), gp->GetY(poly, i), pt.x, pt.y);
87 QSize CharWindow::minimumSizeHint() const
92 QSize CharWindow::sizeHint() const
94 return QSize(200, 200);
97 void CharWindow::paintEvent(QPaintEvent * /*event*/)
104 // p.setBrush(QColor(0, 163, 200)); // Nice, aqua color...
105 p.setPen(QPen(Qt::black, 1.0, Qt::SolidLine));
106 p.setBrush(Qt::black);
108 QRectF rect = path->boundingRect();
109 QSize paintSize = size();
111 // For some reason, this code cuts off two pixels when rendering the path.
112 // Not sure why, but we compensate for that here.
113 paintSize.rwidth() -= 2;
114 paintSize.rheight() -= 2;
116 p.translate(0, paintSize.height());
117 float extraX = 0.0f, extraY = 0.0f;
118 float xConvFac = (float)paintSize.width() / rect.width();
119 float yConvFac = (float)paintSize.height() / rect.height();
121 if (xConvFac > yConvFac)
123 // height is limiting factor (smaller than width)
124 p.scale(yConvFac, -yConvFac);
125 extraX = (((float)paintSize.width() / yConvFac) - rect.width()) / 2.0f;
129 // width is limiting factor (smaller than height)
130 p.scale(xConvFac, -xConvFac);
131 //extraY = (rect.width() / (float)paintSize.width()) * (float)paintSize.height();
132 //extraY = (extraY - rect.height()) / 2.0f;
133 extraY = (((float)paintSize.height() / xConvFac) - rect.height()) / 2.0f;
136 p.translate(-rect.x() + extraX, -rect.y() + extraY);