]> Shamusworld >> Repos - ttedit/blob - src/charwindow.cpp
Separated out app object, added character window.
[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 //
13
14 // FIXED:
15 //
16 // STILL TO BE DONE:
17 //
18 // - Fix "window disappears when tool win comes up" problem
19 // - Render of main window points
20 //
21
22 #include "charwindow.h"
23 #include "debug.h"
24
25 CharWindow::CharWindow(QWidget * parent/*= NULL*/): QWidget(parent, Qt::Tool), path(NULL)
26 {
27         setWindowTitle("Character: Unknown");
28 }
29
30 void CharWindow::MakePathFromPoints(GlyphPoints * gp)
31 {
32         if (gp == NULL)
33                 return;
34
35         if (path != NULL)
36                 delete path;
37
38         path = new QPainterPath();
39
40 #if 0
41         try
42         {
43                 path->moveTo(gp->GetX(0), gp->GetY(0));
44         }
45         catch (int e)
46         {
47                 if (e == GP_OUT_OF_RANGE)
48                         WriteLogMsg("CharWindow: Caught GP_OUT_OF_RANGE exception.\n");
49         }
50
51         for(int i=1; i<gp->GetNumPoints(); i++)
52         {
53         }
54 #endif
55
56         // Draw curve formed by points
57
58         for(int poly=0; poly<gp->GetNumPolys(); poly++)
59         {
60                 if (gp->GetNumPoints(poly) > 2)
61                 {
62                         // Initial move...
63                         // If it's not on curve, then move to it, otherwise move to last point...
64
65                         int x, y;
66
67                         if (gp->GetOnCurve(poly, gp->GetNumPoints(poly) - 1))
68                                 x = (int)gp->GetX(poly, gp->GetNumPoints(poly) - 1), y = (int)gp->GetY(poly, gp->GetNumPoints(poly) - 1);
69                         else
70                                 x = (int)gp->GetX(poly, 0), y = (int)gp->GetY(poly, 0);
71
72                         path->moveTo(x, y);
73
74                         for(int i=0; i<gp->GetNumPoints(poly); i++)
75                         {
76                                 if (gp->GetOnCurve(poly, i))
77                                 {
78 //                                      p.drawLine(x, y, gp->GetX(poly, i), gp->GetY(poly, i));
79                                         x = (int)gp->GetX(poly, i), y = (int)gp->GetY(poly, i);
80                                         path->lineTo(x, y);
81                                 }
82                                 else
83                                 {
84                                         uint32 prev = gp->GetPrev(poly, i), next = gp->GetNext(poly, i);
85                                         float px = gp->GetX(poly, prev), py = gp->GetY(poly, prev),
86                                                 nx = gp->GetX(poly, next), ny = gp->GetY(poly, next);
87
88                                         if (!gp->GetOnCurve(poly, prev))
89                                                 px = (px + gp->GetX(poly, i)) / 2.0f,
90                                                 py = (py + gp->GetY(poly, i)) / 2.0f;
91
92                                         if (!gp->GetOnCurve(poly, next))
93                                                 nx = (nx + gp->GetX(poly, i)) / 2.0f,
94                                                 ny = (ny + gp->GetY(poly, i)) / 2.0f;
95
96 //                                      Bezier(p, point(px, py), point(gp->GetX(poly, i), gp->GetY(poly, i)), point(nx, ny));
97 //                                      path->moveTo(px, py);
98                                         path->quadTo(gp->GetX(poly, i), gp->GetY(poly, i), nx, ny);
99                                         x = (int)nx, y = (int)ny;
100
101                                         if (gp->GetOnCurve(poly, next))
102                                                 i++;                                    // Following point is on curve, so move past it
103                                 }
104                         }
105
106                         path->closeSubpath();
107                 }
108         }
109
110
111 #if 0
112         // Draw curve formed by points
113
114         for(int poly=0; poly<pts.GetNumPolys(); poly++)
115         {
116                 if (pts.GetNumPoints(poly) > 2)
117                 {
118                         // Initial move...
119                         // If it's not on curve, then move to it, otherwise move to last point...
120
121                         int x, y;
122
123                         if (pts.GetOnCurve(poly, pts.GetNumPoints(poly) - 1))
124                                 x = (int)pts.GetX(poly, pts.GetNumPoints(poly) - 1), y = (int)pts.GetY(poly, pts.GetNumPoints(poly) - 1);
125                         else
126                                 x = (int)pts.GetX(poly, 0), y = (int)pts.GetY(poly, 0);
127
128                         for(int i=0; i<pts.GetNumPoints(poly); i++)
129                         {
130                                 if (pts.GetOnCurve(poly, i))
131 //                                      LineTo(hdc, pts.GetX(poly, i), pts.GetY(poly, i));
132                                 {
133                                         p.drawLine(x, y, pts.GetX(poly, i), pts.GetY(poly, i));
134                                         x = (int)pts.GetX(poly, i), y = (int)pts.GetY(poly, i);
135                                 }
136                                 else
137                                 {
138                                         uint32 prev = pts.GetPrev(poly, i), next = pts.GetNext(poly, i);
139                                         float px = pts.GetX(poly, prev), py = pts.GetY(poly, prev),
140                                                 nx = pts.GetX(poly, next), ny = pts.GetY(poly, next);
141
142                                         if (!pts.GetOnCurve(poly, prev))
143                                                 px = (px + pts.GetX(poly, i)) / 2.0f,
144                                                 py = (py + pts.GetY(poly, i)) / 2.0f;
145
146                                         if (!pts.GetOnCurve(poly, next))
147                                                 nx = (nx + pts.GetX(poly, i)) / 2.0f,
148                                                 ny = (ny + pts.GetY(poly, i)) / 2.0f;
149
150                                         Bezier(p, point(px, py), point(pts.GetX(poly, i), pts.GetY(poly, i)), point(nx, ny));
151                                         x = (int)nx, y = (int)ny;
152
153                                         if (pts.GetOnCurve(poly, next))
154                                                 i++;                                    // Following point is on curve, so move past it
155                                 }
156                         }
157                 }
158         }
159 #endif
160 }
161
162 QSize CharWindow::minimumSizeHint() const
163 {
164         return QSize(50, 50);
165 }
166
167 QSize CharWindow::sizeHint() const
168 {
169         return QSize(200, 200);
170 }
171
172 void CharWindow::paintEvent(QPaintEvent * /*event*/)
173 {
174         if (path == NULL)
175                 return;
176
177         QPainter p(this);
178
179 // Need to translate as well...
180 //      p.scale(1.0, -1.0);
181         p.drawPath(*path);
182 }
183
184
185 #if 0
186 BEGIN_EVENT_TABLE(CharWindow, wxMiniFrame)
187         EVT_PAINT(CharWindow::OnPaint)
188 //      EVT_MOUSE_EVENTS(CharWindow::OnMouseEvent)
189 END_EVENT_TABLE()
190
191 CharWindow::CharWindow(wxFrame * parent, const wxString &title, const wxPoint &pos,
192         const wxSize &size, long style): wxMiniFrame(parent, -1, title, pos, size, style)
193 {
194         Show(false);
195 }
196
197 CharWindow::~CharWindow()
198 {
199 }
200
201 void CharWindow::OnPaint(wxPaintEvent &e)
202 {
203         wxPaintDC dc(this);
204 //doesnothing   dc.SetBackground(*wxWHITE_BRUSH);
205
206 //      wxMemoryDC memDC;
207 //      memDC.SelectObject(*bmp);
208 //      dc.Blit(0, 0, sizeTPBM.x, sizeTPBM.y, &memDC, 0, 0, wxCOPY);
209
210 //      if (prevTool != -1)
211 //      {
212 //          //need ul corner of bitmap, ul corner of dest, width/height
213 //              wxPoint pt(sizeStamp.x * (prevTool & 0x03), sizeStamp.y * (prevTool >> 2));
214 //              dc.Blit(pt.x, pt.y, sizeStamp.x, sizeStamp.y, &memDC, pt.x, pt.y, wxSRC_INVERT);
215 //      }
216
217 //      memDC.SelectObject(wxNullBitmap);
218 }
219 #endif