]> Shamusworld >> Repos - ttedit/blob - src/charwindow.cpp
Minor fixes to polygon adding, added Tau constants to math package.
[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 CharWindow::CharWindow(QWidget * parent/*= NULL*/): QWidget(parent, Qt::Window), path(NULL)
29 {
30         setWindowTitle("Character: Unknown");
31 }
32
33 void CharWindow::MakePathFromPoints(GlyphPoints * gp)
34 {
35         if (gp == NULL)
36                 return;
37
38         if (path != NULL)
39                 delete path;
40
41         path = new QPainterPath();
42 //      path->setFillRule(Qt::OddEvenFill);
43         path->setFillRule(Qt::WindingFill);
44
45         // Draw curve formed by points
46
47         for(int poly=0; poly<gp->GetNumPolys(); poly++)
48         {
49                 if (gp->GetNumPoints(poly) < 3)
50                         continue;
51
52                 // Initial move: If our start point is on curve, then go to it. Otherwise,
53                 // check previous point. If it's on curve, go to it otherwise go the
54                 // midpoint between start point and previous (since it's between two curve
55                 // control points).
56                 IPoint pt = (gp->GetOnCurve(poly, 0)
57                         ? gp->GetPoint(poly, 0) : (gp->GetPrevOnCurve(poly, 0)
58                                 ? gp->GetPrevPoint(poly, 0) : gp->GetMidpointToPrev(poly, 0)));
59                 path->moveTo(pt.x, pt.y);
60
61                 for(int i=0; i<gp->GetNumPoints(poly); i++)
62                 {
63                         // If this point and then next are both on curve, we have a line...
64                         if (gp->GetOnCurve(poly, i) && gp->GetNextOnCurve(poly, i))
65                                 path->lineTo(gp->GetNextX(poly, i), gp->GetNextY(poly, i));
66                         else
67                         {
68                                 // Skip point if it's on curve (start of curve--it's already
69                                 // been plotted so we don't need to handle it...)
70                                 if (gp->GetOnCurve(poly, i))
71                                         continue;
72
73                                 // We are now guaranteed that we are sitting on a curve control point
74                                 // (off curve). Figure the extent of the curve: If the following is a
75                                 // curve control point, then use the midpoint to it otherwise go to
76                                 // the next point since it's on curve.
77                                 IPoint pt = (gp->GetNextOnCurve(poly, i)
78                                         ? gp->GetNextPoint(poly, i) : gp->GetMidpointToNext(poly, i));
79
80                                 path->quadTo(gp->GetX(poly, i), gp->GetY(poly, i), pt.x, pt.y);
81                         }
82                 }
83
84                 path->closeSubpath();
85         }
86 }
87
88 QSize CharWindow::minimumSizeHint() const
89 {
90         return QSize(50, 50);
91 }
92
93 QSize CharWindow::sizeHint() const
94 {
95         return QSize(200, 200);
96 }
97
98 void CharWindow::paintEvent(QPaintEvent * /*event*/)
99 {
100         if (path == NULL)
101                 return;
102
103         QPainter p(this);
104
105 //      p.setBrush(QColor(0, 163, 200));        // Nice, aqua color...
106         p.setPen(QPen(Qt::black, 1.0, Qt::SolidLine));
107         p.setBrush(Qt::black);
108
109         QRectF rect = path->boundingRect();
110         QSize paintSize = size();
111
112         // For some reason, this code cuts off two pixels when rendering the path.
113         // Not sure why, but we compensate for that here.
114         paintSize.rwidth() -= 2;
115         paintSize.rheight() -= 2;
116
117         p.translate(0, paintSize.height());
118         float extraX = 0.0f, extraY = 0.0f;
119         float xConvFac = (float)paintSize.width() / rect.width();
120         float yConvFac = (float)paintSize.height() / rect.height();
121
122         if (xConvFac > yConvFac)
123         {
124                 // height is limiting factor (smaller than width)
125                 p.scale(yConvFac, -yConvFac);
126                 extraX = (((float)paintSize.width() / yConvFac) - rect.width()) / 2.0f;
127         }
128         else
129         {
130                 // width is limiting factor (smaller than height)
131                 p.scale(xConvFac, -xConvFac);
132 //extraY = (rect.width() / (float)paintSize.width()) * (float)paintSize.height();
133 //extraY = (extraY - rect.height()) / 2.0f;
134                 extraY = (((float)paintSize.height() / xConvFac) - rect.height()) / 2.0f;
135         }
136
137         p.translate(-rect.x() + extraX, -rect.y() + extraY);
138
139         p.drawPath(*path);
140 }