]> Shamusworld >> Repos - ttedit/blob - src/charwindow.cpp
Fix for missing ampersand in QApplication.
[ttedit] / src / charwindow.cpp
1 //
2 // TTEDIT.CPP - The TrueType Editor
3 // by James L. Hammons
4 // (C) 2004 Underground Software
5 //
6 // JLH = James L. Hammons <jlhamm@acm.org>
7 //
8 // Who  When        What
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
13 //
14
15 // FIXED:
16 //
17 // - Render of main window points [DONE]
18 //
19 // STILL TO BE DONE:
20 //
21 // - Fix "window disappears when tool win comes up" problem
22 //
23
24 #include "charwindow.h"
25 #include "debug.h"
26
27 CharWindow::CharWindow(QWidget * parent/*= NULL*/): QWidget(parent, Qt::Tool), path(NULL)
28 {
29         setWindowTitle("Character: Unknown");
30 }
31
32 void CharWindow::MakePathFromPoints(GlyphPoints * gp)
33 {
34         if (gp == NULL)
35                 return;
36
37         if (path != NULL)
38                 delete path;
39
40         path = new QPainterPath();
41 //      path->setFillRule(Qt::OddEvenFill);
42         path->setFillRule(Qt::WindingFill);
43
44         // Draw curve formed by points
45
46         for(int poly=0; poly<gp->GetNumPolys(); poly++)
47         {
48                 if (gp->GetNumPoints(poly) < 3)
49                         continue;
50
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
54                 // control points).
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);
59
60                 for(int i=0; i<gp->GetNumPoints(poly); i++)
61                 {
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));
65                         else
66                         {
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))
70                                         continue;
71
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));
78
79                                 path->quadTo(gp->GetX(poly, i), gp->GetY(poly, i), pt.x, pt.y);
80                         }
81                 }
82
83                 path->closeSubpath();
84         }
85 }
86
87 QSize CharWindow::minimumSizeHint() const
88 {
89         return QSize(50, 50);
90 }
91
92 QSize CharWindow::sizeHint() const
93 {
94         return QSize(200, 200);
95 }
96
97 void CharWindow::paintEvent(QPaintEvent * /*event*/)
98 {
99         if (path == NULL)
100                 return;
101
102         QPainter p(this);
103
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);
107
108         QRectF rect = path->boundingRect();
109         QSize paintSize = size();
110
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;
115
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();
120
121         if (xConvFac > yConvFac)
122         {
123                 // height is limiting factor (smaller than width)
124                 p.scale(yConvFac, -yConvFac);
125                 extraX = (((float)paintSize.width() / yConvFac) - rect.width()) / 2.0f;
126         }
127         else
128         {
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;
134         }
135
136         p.translate(-rect.x() + extraX, -rect.y() + extraY);
137
138         p.drawPath(*path);
139 }