]> Shamusworld >> Repos - ttedit/blob - src/charwindow.cpp
Fixed character window to render the main window's points properly,
[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) > 2)
49                 {
50                         // Initial move...
51                         // If last point is on curve then move to it, otherwise move to first point...
52
53                         int x, y;
54
55                         if (gp->GetOnCurve(poly, gp->GetPrev(poly, 0)))
56                                 x = gp->GetX(poly, gp->GetPrev(poly, 0)),
57                                 y = gp->GetY(poly, gp->GetPrev(poly, 0));
58                         else
59                         {
60                                 x = (int)gp->GetX(poly, 0), y = (int)gp->GetY(poly, 0);
61
62                                 if (!gp->GetOnCurve(poly, 0))
63                                 {
64                                         IPoint pt = gp->GetMidpointToPrev(poly, 0);
65                                         x = pt.x, y = pt.y;
66                                 }
67                         }
68
69                         path->moveTo(x, y);
70
71                         for(int i=0; i<gp->GetNumPoints(poly); i++)
72                         {
73                                 if (gp->GetOnCurve(poly, i))
74                                 {
75 //                                      p.drawLine(x, y, gp->GetX(poly, i), gp->GetY(poly, i));
76                                         x = (int)gp->GetX(poly, i), y = (int)gp->GetY(poly, i);
77                                         path->lineTo(x, y);
78                                 }
79                                 else
80                                 {
81                                         IPoint pt = (gp->GetOnCurve(poly, gp->GetNext(poly, i)) ? gp->GetNextPoint(poly, i) : gp->GetMidpointToNext(poly, i));
82
83                                         path->quadTo(gp->GetX(poly, i), gp->GetY(poly, i), pt.x, pt.y);
84                                         x = pt.x, y = pt.y;
85
86                                         // If following point is on curve, move past it
87                                         if (gp->GetOnCurve(poly, gp->GetNext(poly, i)))
88                                                 i++;
89                                 }
90                         }
91
92                         path->closeSubpath();
93                 }
94         }
95 }
96
97 QSize CharWindow::minimumSizeHint() const
98 {
99         return QSize(50, 50);
100 }
101
102 QSize CharWindow::sizeHint() const
103 {
104         return QSize(200, 200);
105 }
106
107 void CharWindow::paintEvent(QPaintEvent * /*event*/)
108 {
109         if (path == NULL)
110                 return;
111
112         QPainter p(this);
113
114         p.setBrush(QColor(122, 163, 39));
115 // Need to translate as well...
116 //      p.scale(2.0, 2.0);
117
118 /*
119 1.0 -> 3.0, height = 400
120 r.h / ps.h = 2/400 <-- do it the other way!
121
122 height works, width does not
123
124 2-step process:
125 compare aspect ratios
126
127 ps.w - ((r.h / ps.h) * ps.w)
128
129 0.5 -> where in the 400? -> 100
130 0.5/r.h(2.0) = 0.25 * ps.h(400) = 100
131 conv.fac. -> (ps.h / r.h)
132 */
133         QRectF rect = path->boundingRect();
134         QSize paintSize = size();
135
136         p.translate(0, paintSize.height());
137 float extraX = 0.0f, extraY = 0.0f;
138 float xConvFac = (float)paintSize.width() / rect.width();
139 float yConvFac = (float)paintSize.height() / rect.height();
140
141 //      if (((float)paintSize.width() / rect.width()) > ((float)paintSize.height() / rect.height()))
142         if (xConvFac > yConvFac)
143         {
144                 // height is limiting factor
145                 p.scale(yConvFac, -yConvFac);
146 //extraX = (rect.height() / (float)paintSize.height()) * (float)paintSize.width();
147 //extraX = (extraX - rect.width()) / 2.0f;
148                 extraX = (((float)paintSize.width() / yConvFac) - rect.width()) / 2.0f;
149         }
150         else
151         {
152                 // width is limiting factor
153 //              p.scale((float)paintSize.width() / rect.width(), -(float)paintSize.width() / rect.height());
154                 p.scale((float)paintSize.width() / rect.width(), -(float)paintSize.width() / rect.width());
155 extraY = (rect.width() / (float)paintSize.width()) * (float)paintSize.height();// / 2.0f;
156 extraY = extraY - rect.height();
157 extraY /= 2.0f;
158         }
159
160         p.translate(-rect.x() + extraX, -rect.y() + extraY);
161
162         p.drawPath(*path);
163 }