CraftSynth.ImageEditor – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Drawing; |
||
3 | using System.Drawing.Drawing2D; |
||
4 | using System.Globalization; |
||
5 | using System.Runtime.Serialization; |
||
6 | using System.Windows.Forms; |
||
7 | |||
8 | namespace CraftSynth.ImageEditor |
||
9 | { |
||
10 | /// <summary> |
||
11 | /// Rectangle graphic object |
||
12 | /// </summary> |
||
13 | //[Serializable] |
||
14 | public class DrawText : DrawObject |
||
15 | { |
||
16 | private Rectangle rectangle; |
||
17 | private string _theText; |
||
18 | private Font _font; |
||
19 | private bool _disposed; |
||
20 | |||
21 | |||
22 | protected string TheText |
||
23 | { |
||
24 | get { return _theText; } |
||
25 | set |
||
26 | { |
||
27 | _theText = value; |
||
28 | TipText = value; |
||
29 | } |
||
30 | } |
||
31 | |||
32 | public Font TheFont |
||
33 | { |
||
34 | get { return _font; } |
||
35 | set { _font = value; } |
||
36 | } |
||
37 | |||
38 | private const string entryRectangle = "Rect"; |
||
39 | private const string entryText = "Text"; |
||
40 | private const string entryFontName = "FontName"; |
||
41 | private const string entryFontBold = "FontBold"; |
||
42 | private const string entryFontItalic = "FontItalic"; |
||
43 | private const string entryFontSize = "FontSize"; |
||
44 | private const string entryFontStrikeout = "FontStrikeout"; |
||
45 | private const string entryFontUnderline = "FontUnderline"; |
||
46 | |||
47 | |||
48 | protected Rectangle Rectangle |
||
49 | { |
||
50 | get { return rectangle; } |
||
51 | set { rectangle = value; } |
||
52 | } |
||
53 | |||
54 | public DrawText() |
||
55 | { |
||
56 | //SetRectangle(0, 0, 1,1); |
||
57 | _theText = ""; |
||
58 | Initialize(); |
||
59 | } |
||
60 | |||
61 | /// <summary> |
||
62 | /// Clone this instance |
||
63 | /// </summary> |
||
64 | public override DrawObject Clone() |
||
65 | { |
||
66 | DrawText drawText = new DrawText(); |
||
67 | |||
68 | drawText._font = _font; |
||
69 | drawText._theText = _theText; |
||
70 | drawText.rectangle = rectangle; |
||
71 | |||
72 | FillDrawObjectFields(drawText); |
||
73 | return drawText; |
||
74 | } |
||
75 | |||
76 | #region Destruction |
||
77 | protected override void Dispose(bool disposing) |
||
78 | { |
||
79 | if (!this._disposed) |
||
80 | { |
||
81 | if (disposing) |
||
82 | { |
||
83 | // Free any managed objects here. |
||
84 | if (this._font != null) |
||
85 | { |
||
86 | this._font.Dispose(); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | // Free any unmanaged objects here. |
||
91 | |||
92 | this._disposed = true; |
||
93 | } |
||
94 | base.Dispose(disposing); |
||
95 | } |
||
96 | |||
97 | ~DrawText() |
||
98 | { |
||
99 | this.Dispose(false); |
||
100 | } |
||
101 | #endregion |
||
102 | |||
103 | public DrawText(int x, int y, string textToDraw, Font textFont, Color textColor) |
||
104 | { |
||
105 | rectangle.X = x; |
||
106 | rectangle.Y = y; |
||
107 | _theText = textToDraw; |
||
108 | _font = textFont; |
||
109 | Color = textColor; |
||
110 | Initialize(); |
||
111 | } |
||
112 | |||
113 | |||
114 | /// <summary> |
||
115 | /// Draw rectangle |
||
116 | /// </summary> |
||
117 | /// <param name="g"></param> |
||
118 | public override void Draw(Graphics g) |
||
119 | { |
||
120 | |||
121 | using (Font biggerFont = new Font(_font.FontFamily, _font.Size + 7, _font.Style))//bad workaround: we want font size to corespont to one showed in text dialog |
||
122 | { |
||
123 | Pen pen = new Pen(Color); |
||
124 | //Brush b = new SolidBrush(Color); |
||
125 | //g.DrawString(_theText, _font, b, new PointF(Rectangle.X, Rectangle.Y)); |
||
126 | GraphicsPath gp = new GraphicsPath(); |
||
127 | StringFormat format = StringFormat.GenericDefault; |
||
128 | gp.AddString(_theText, biggerFont.FontFamily, (int) _font.Style, biggerFont.Size, |
||
129 | new PointF(Rectangle.X, Rectangle.Y), format); |
||
130 | // Rotate the path about it's center if necessary |
||
131 | if (Rotation != 0) |
||
132 | { |
||
133 | RectangleF pathBounds = gp.GetBounds(); |
||
134 | Matrix m = new Matrix(); |
||
135 | m.RotateAt(Rotation, new PointF(pathBounds.Left + (pathBounds.Width/2), pathBounds.Top + (pathBounds.Height/2)), |
||
136 | MatrixOrder.Append); |
||
137 | gp.Transform(m); |
||
138 | } |
||
139 | g.DrawPath(pen, gp); |
||
140 | g.FillPath(new SolidBrush(pen.Color), gp); |
||
141 | |||
142 | rectangle.Size = g.MeasureString(_theText, biggerFont).ToSize(); |
||
143 | pen.Dispose(); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | /// <summary> |
||
148 | /// Get number of handles |
||
149 | /// </summary> |
||
150 | public override int HandleCount |
||
151 | { |
||
152 | get { return 8; } |
||
153 | } |
||
154 | |||
155 | |||
156 | /// <summary> |
||
157 | /// Get handle point by 1-based number |
||
158 | /// </summary> |
||
159 | /// <param name="handleNumber"></param> |
||
160 | /// <returns></returns> |
||
161 | public override Point GetHandle(int handleNumber) |
||
162 | { |
||
163 | int x, y, xCenter, yCenter; |
||
164 | |||
165 | xCenter = rectangle.X + rectangle.Width / 2; |
||
166 | yCenter = rectangle.Y + rectangle.Height / 2; |
||
167 | x = rectangle.X; |
||
168 | y = rectangle.Y; |
||
169 | |||
170 | switch (handleNumber) |
||
171 | { |
||
172 | case 1: |
||
173 | x = rectangle.X; |
||
174 | y = rectangle.Y; |
||
175 | break; |
||
176 | case 2: |
||
177 | x = xCenter; |
||
178 | y = rectangle.Y; |
||
179 | break; |
||
180 | case 3: |
||
181 | x = rectangle.Right; |
||
182 | y = rectangle.Y; |
||
183 | break; |
||
184 | case 4: |
||
185 | x = rectangle.Right; |
||
186 | y = yCenter; |
||
187 | break; |
||
188 | case 5: |
||
189 | x = rectangle.Right; |
||
190 | y = rectangle.Bottom; |
||
191 | break; |
||
192 | case 6: |
||
193 | x = xCenter; |
||
194 | y = rectangle.Bottom; |
||
195 | break; |
||
196 | case 7: |
||
197 | x = rectangle.X; |
||
198 | y = rectangle.Bottom; |
||
199 | break; |
||
200 | case 8: |
||
201 | x = rectangle.X; |
||
202 | y = yCenter; |
||
203 | break; |
||
204 | } |
||
205 | |||
206 | return new Point(x, y); |
||
207 | } |
||
208 | |||
209 | /// <summary> |
||
210 | /// Hit test. |
||
211 | /// Return value: -1 - no hit |
||
212 | /// 0 - hit anywhere |
||
213 | /// > 1 - handle number |
||
214 | /// </summary> |
||
215 | /// <param name="point"></param> |
||
216 | /// <returns></returns> |
||
217 | public override int HitTest(Point point) |
||
218 | { |
||
219 | if (Selected) |
||
220 | { |
||
221 | for (int i = 1; i <= HandleCount; i++) |
||
222 | { |
||
223 | if (GetHandleRectangle(i).Contains(point)) |
||
224 | return i; |
||
225 | } |
||
226 | } |
||
227 | |||
228 | if (PointInObject(point)) |
||
229 | return 0; |
||
230 | |||
231 | return -1; |
||
232 | } |
||
233 | |||
234 | |||
235 | protected override bool PointInObject(Point point) |
||
236 | { |
||
237 | return rectangle.Contains(point); |
||
238 | } |
||
239 | |||
240 | public override Rectangle GetBounds(Graphics g) |
||
241 | { |
||
242 | return rectangle; |
||
243 | } |
||
244 | |||
245 | |||
246 | /// <summary> |
||
247 | /// Get cursor for the handle |
||
248 | /// </summary> |
||
249 | /// <param name="handleNumber"></param> |
||
250 | /// <returns></returns> |
||
251 | public override Cursor GetHandleCursor(int handleNumber) |
||
252 | { |
||
253 | //switch ( handleNumber ) |
||
254 | //{ |
||
255 | // case 1: |
||
256 | // return Cursors.SizeNWSE; |
||
257 | // case 2: |
||
258 | // return Cursors.SizeNS; |
||
259 | // case 3: |
||
260 | // return Cursors.SizeNESW; |
||
261 | // case 4: |
||
262 | // return Cursors.SizeWE; |
||
263 | // case 5: |
||
264 | // return Cursors.SizeNWSE; |
||
265 | // case 6: |
||
266 | // return Cursors.SizeNS; |
||
267 | // case 7: |
||
268 | // return Cursors.SizeNESW; |
||
269 | // case 8: |
||
270 | // return Cursors.SizeWE; |
||
271 | // default: |
||
272 | return Cursors.Default; |
||
273 | //} |
||
274 | } |
||
275 | |||
276 | /// <summary> |
||
277 | /// Move handle to new point (resizing) |
||
278 | /// </summary> |
||
279 | /// <param name="point"></param> |
||
280 | /// <param name="handleNumber"></param> |
||
281 | public override void MoveHandleTo(Point point, int handleNumber) |
||
282 | { |
||
283 | //int left = Rectangle.Left; |
||
284 | //int top = Rectangle.Top; |
||
285 | //int right = Rectangle.Right; |
||
286 | //int bottom = Rectangle.Bottom; |
||
287 | |||
288 | //switch ( handleNumber ) |
||
289 | //{ |
||
290 | // case 1: |
||
291 | // left = point.X; |
||
292 | // top = point.Y; |
||
293 | // break; |
||
294 | // case 2: |
||
295 | // top = point.Y; |
||
296 | // break; |
||
297 | // case 3: |
||
298 | // right = point.X; |
||
299 | // top = point.Y; |
||
300 | // break; |
||
301 | // case 4: |
||
302 | // right = point.X; |
||
303 | // break; |
||
304 | // case 5: |
||
305 | // right = point.X; |
||
306 | // bottom = point.Y; |
||
307 | // break; |
||
308 | // case 6: |
||
309 | // bottom = point.Y; |
||
310 | // break; |
||
311 | // case 7: |
||
312 | // left = point.X; |
||
313 | // bottom = point.Y; |
||
314 | // break; |
||
315 | // case 8: |
||
316 | // left = point.X; |
||
317 | // break; |
||
318 | //} |
||
319 | |||
320 | //SetRectangle(left, top, right - left, bottom - top); |
||
321 | } |
||
322 | |||
323 | |||
324 | public override bool IntersectsWith(Rectangle rectangle) |
||
325 | { |
||
326 | return Rectangle.IntersectsWith(rectangle); |
||
327 | } |
||
328 | |||
329 | /// <summary> |
||
330 | /// Move object |
||
331 | /// </summary> |
||
332 | /// <param name="deltaX"></param> |
||
333 | /// <param name="deltaY"></param> |
||
334 | public override void Move(int deltaX, int deltaY) |
||
335 | { |
||
336 | rectangle.X += deltaX; |
||
337 | rectangle.Y += deltaY; |
||
338 | Dirty = true; |
||
339 | } |
||
340 | |||
341 | public override void Dump() |
||
342 | { |
||
343 | //base.Dump (); |
||
344 | |||
345 | //Trace.WriteLine("rectangle.X = " + rectangle.X.ToString(CultureInfo.InvariantCulture)); |
||
346 | //Trace.WriteLine("rectangle.Y = " + rectangle.Y.ToString(CultureInfo.InvariantCulture)); |
||
347 | //Trace.WriteLine("rectangle.Width = " + rectangle.Width.ToString(CultureInfo.InvariantCulture)); |
||
348 | //Trace.WriteLine("rectangle.Height = " + rectangle.Height.ToString(CultureInfo.InvariantCulture)); |
||
349 | } |
||
350 | |||
351 | /// <summary> |
||
352 | /// Normalize rectangle |
||
353 | /// </summary> |
||
354 | public override void Normalize() |
||
355 | { |
||
356 | //rectangle = DrawRectangle.GetNormalizedRectangle(rectangle); |
||
357 | } |
||
358 | |||
359 | /// <summary> |
||
360 | /// Save objevt to serialization stream |
||
361 | /// </summary> |
||
362 | /// <param name="info"></param> |
||
363 | /// <param name="orderNumber">Index of the Layer being saved</param> |
||
364 | /// <param name="objectIndex">Index of this object in the Layer</param> |
||
365 | public override void SaveToStream(SerializationInfo info, int orderNumber, int objectIndex) |
||
366 | { |
||
367 | info.AddValue( |
||
368 | String.Format(CultureInfo.InvariantCulture, |
||
369 | "{0}{1}-{2}", |
||
370 | entryRectangle, orderNumber, objectIndex), |
||
371 | rectangle); |
||
372 | info.AddValue( |
||
373 | String.Format(CultureInfo.InvariantCulture, |
||
374 | "{0}{1}-{2}", |
||
375 | entryText, orderNumber, objectIndex), |
||
376 | _theText); |
||
377 | info.AddValue( |
||
378 | String.Format(CultureInfo.InvariantCulture, |
||
379 | "{0}{1}-{2}", |
||
380 | entryFontName, orderNumber, objectIndex), |
||
381 | _font.Name); |
||
382 | info.AddValue( |
||
383 | String.Format(CultureInfo.InvariantCulture, |
||
384 | "{0}{1}-{2}", |
||
385 | entryFontBold, orderNumber, objectIndex), |
||
386 | _font.Bold); |
||
387 | info.AddValue( |
||
388 | String.Format(CultureInfo.InvariantCulture, |
||
389 | "{0}{1}-{2}", |
||
390 | entryFontItalic, orderNumber, objectIndex), |
||
391 | _font.Italic); |
||
392 | info.AddValue( |
||
393 | String.Format(CultureInfo.InvariantCulture, |
||
394 | "{0}{1}-{2}", |
||
395 | entryFontSize, orderNumber, objectIndex), |
||
396 | _font.Size); |
||
397 | info.AddValue( |
||
398 | String.Format(CultureInfo.InvariantCulture, |
||
399 | "{0}{1}-{2}", |
||
400 | entryFontStrikeout, orderNumber, objectIndex), |
||
401 | _font.Strikeout); |
||
402 | info.AddValue( |
||
403 | String.Format(CultureInfo.InvariantCulture, |
||
404 | "{0}{1}-{2}", |
||
405 | entryFontUnderline, orderNumber, objectIndex), |
||
406 | _font.Underline); |
||
407 | |||
408 | base.SaveToStream(info, orderNumber, objectIndex); |
||
409 | } |
||
410 | |||
411 | /// <summary> |
||
412 | /// LOad object from serialization stream |
||
413 | /// </summary> |
||
414 | /// <param name="info"></param> |
||
415 | /// <param name="orderNumber"></param> |
||
416 | /// <param name="objectIndex"></param> |
||
417 | public override void LoadFromStream(SerializationInfo info, int orderNumber, int objectIndex) |
||
418 | { |
||
419 | rectangle = (Rectangle)info.GetValue( |
||
420 | String.Format(CultureInfo.InvariantCulture, |
||
421 | "{0}{1}-{2}", |
||
422 | entryRectangle, orderNumber, objectIndex), |
||
423 | typeof(Rectangle)); |
||
424 | _theText = info.GetString( |
||
425 | String.Format(CultureInfo.InvariantCulture, |
||
426 | "{0}{1}-{2}", |
||
427 | entryText, orderNumber, objectIndex)); |
||
428 | string name = info.GetString( |
||
429 | String.Format(CultureInfo.InvariantCulture, |
||
430 | "{0}{1}-{2}", |
||
431 | entryFontName, orderNumber, objectIndex)); |
||
432 | bool bold = info.GetBoolean( |
||
433 | String.Format(CultureInfo.InvariantCulture, |
||
434 | "{0}{1}-{2}", |
||
435 | entryFontBold, orderNumber, objectIndex)); |
||
436 | bool italic = info.GetBoolean( |
||
437 | String.Format(CultureInfo.InvariantCulture, |
||
438 | "{0}{1}-{2}", |
||
439 | entryFontItalic, orderNumber, objectIndex)); |
||
440 | float size = (float)info.GetValue( |
||
441 | String.Format(CultureInfo.InvariantCulture, |
||
442 | "{0}{1}-{2}", |
||
443 | entryFontSize, orderNumber, objectIndex), |
||
444 | typeof(float)); |
||
445 | bool strikeout = info.GetBoolean( |
||
446 | String.Format(CultureInfo.InvariantCulture, |
||
447 | "{0}{1}-{2}", |
||
448 | entryFontStrikeout, orderNumber, objectIndex)); |
||
449 | bool underline = info.GetBoolean( |
||
450 | String.Format(CultureInfo.InvariantCulture, |
||
451 | "{0}{1}-{2}", |
||
452 | entryFontUnderline, orderNumber, objectIndex)); |
||
453 | FontStyle fs = FontStyle.Regular; |
||
454 | if (bold) |
||
455 | fs |= FontStyle.Bold; |
||
456 | if (italic) |
||
457 | fs |= FontStyle.Italic; |
||
458 | if (strikeout) |
||
459 | fs |= FontStyle.Strikeout; |
||
460 | if (underline) |
||
461 | fs |= FontStyle.Underline; |
||
462 | _font = new Font(name, size, fs); |
||
463 | |||
464 | base.LoadFromStream(info, orderNumber, objectIndex); |
||
465 | } |
||
466 | |||
467 | #region Helper Functions |
||
468 | //public static Rectangle GetNormalizedRectangle(int x1, int y1, int x2, int y2) |
||
469 | //{ |
||
470 | //if ( x2 < x1 ) |
||
471 | //{ |
||
472 | // int tmp = x2; |
||
473 | // x2 = x1; |
||
474 | // x1 = tmp; |
||
475 | //} |
||
476 | |||
477 | //if ( y2 < y1 ) |
||
478 | //{ |
||
479 | // int tmp = y2; |
||
480 | // y2 = y1; |
||
481 | // y1 = tmp; |
||
482 | //} |
||
483 | |||
484 | //return new Rectangle(x1, y1, x2 - x1, y2 - y1); |
||
485 | //} |
||
486 | |||
487 | //public static Rectangle GetNormalizedRectangle(Point p1, Point p2) |
||
488 | //{ |
||
489 | //return GetNormalizedRectangle(p1.X, p1.Y, p2.X, p2.Y); |
||
490 | //} |
||
491 | |||
492 | //public static Rectangle GetNormalizedRectangle(Rectangle r) |
||
493 | //{ |
||
494 | //return GetNormalizedRectangle(r.X, r.Y, r.X + r.Width, r.Y + r.Height); |
||
495 | //} |
||
496 | #endregion |
||
497 | } |
||
498 | } |