(empty log message)
@@ -0,0 +1,516 @@ | ||
1 | +/* | |
2 | + * Misc.cs | |
3 | + * Copyright (c) 2008-2009 kbinani | |
4 | + * | |
5 | + * This file is part of Boare.Lib.AppUtil. | |
6 | + * | |
7 | + * Boare.Lib.AppUtil is free software; you can redistribute it and/or | |
8 | + * modify it under the terms of the BSD License. | |
9 | + * | |
10 | + * Boare.Lib.AppUtil is distributed in the hope that it will be useful, | |
11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
13 | + */ | |
14 | +#if JAVA | |
15 | +package org.kbinani.apputil; | |
16 | + | |
17 | +import java.awt.*; | |
18 | +import javax.swing.*; | |
19 | +import java.awt.image.*; | |
20 | +#else | |
21 | +using System; | |
22 | +using System.Collections.Generic; | |
23 | +using System.Drawing; | |
24 | +using System.Drawing.Imaging; | |
25 | +using System.IO; | |
26 | +using System.Reflection; | |
27 | +using System.Windows.Forms; | |
28 | +using bocoree; | |
29 | + | |
30 | +namespace Boare.Lib.AppUtil { | |
31 | + using java = bocoree.java; | |
32 | + using boolean = System.Boolean; | |
33 | +#endif | |
34 | + | |
35 | +#if JAVA | |
36 | + public class Util{ | |
37 | +#else | |
38 | + public static partial class Util { | |
39 | +#endif | |
40 | +#if JAVA | |
41 | + public static void applyContextMenuFontRecurse( MenuElement item, Font font ){ | |
42 | + applyToolStripFontRecurse( item, font ); | |
43 | + } | |
44 | +#else | |
45 | + public static void applyContextMenuFontRecurse( ContextMenuStrip item, bocoree.java.awt.Font font ) { | |
46 | + item.Font = font.font; | |
47 | + foreach ( ToolStripItem tsi in item.Items ) { | |
48 | + applyToolStripFontRecurse( tsi, font ); | |
49 | + } | |
50 | + } | |
51 | +#endif | |
52 | + | |
53 | +#if JAVA | |
54 | + public static void applyToolStripFontRecurse( MenuElement item, Font font ){ | |
55 | + if( item instanceof Component ){ | |
56 | + ((Component)item).setFont( font ); | |
57 | + } | |
58 | + for( MenuElement element : item.getSubElements() ){ | |
59 | + applyToolStripFontRecurse( element, font ); | |
60 | + } | |
61 | + } | |
62 | +#else | |
63 | + public static void applyToolStripFontRecurse( ToolStripItem item, bocoree.java.awt.Font font ) { | |
64 | + item.Font = font.font; | |
65 | + if ( item is ToolStripMenuItem ) { | |
66 | + ToolStripMenuItem tsmi = (ToolStripMenuItem)item; | |
67 | + foreach ( ToolStripItem tsi in tsmi.DropDownItems ) { | |
68 | + applyToolStripFontRecurse( tsi, font ); | |
69 | + } | |
70 | + } else if ( item is ToolStripDropDownItem ) { | |
71 | + ToolStripDropDownItem tsdd = (ToolStripDropDownItem)item; | |
72 | + foreach ( ToolStripItem tsi in tsdd.DropDownItems ) { | |
73 | + applyToolStripFontRecurse( tsi, font ); | |
74 | + } | |
75 | + } | |
76 | + } | |
77 | +#endif | |
78 | + | |
79 | + /// <summary> | |
80 | + /// 指定したフォントを描画するとき、描画指定したy座標と、描かれる文字の中心線のズレを調べます | |
81 | + /// </summary> | |
82 | + /// <param name="font"></param> | |
83 | + /// <returns></returns> | |
84 | + public static int getStringDrawOffset( java.awt.Font font ) { | |
85 | + int ret = 0; | |
86 | + String pangram = "cozy lummox gives smart squid who asks for job pen. 01234567890 THE QUICK BROWN FOX JUMPED OVER THE LAZY DOGS."; | |
87 | + java.awt.Dimension size = measureString( pangram, font ); | |
88 | + if ( size.height <= 0 ) { | |
89 | + return 0; | |
90 | + } | |
91 | + java.awt.image.BufferedImage b = null; | |
92 | + java.awt.Graphics2D g = null; | |
93 | +#if JAVA | |
94 | + java.awt.image.BufferedImage b2 = null; | |
95 | +#else | |
96 | + BitmapEx b2 = null; | |
97 | +#endif | |
98 | + try { | |
99 | + int w = size.width * 3; | |
100 | + int h = size.height * 3; | |
101 | + b = new java.awt.image.BufferedImage( w, h, java.awt.image.BufferedImage.TYPE_INT_BGR ); | |
102 | + g = b.createGraphics(); | |
103 | + g.setColor( java.awt.Color.white ); | |
104 | + g.fillRect( 0, 0, w, h ); | |
105 | + g.setFont( font ); | |
106 | + g.setColor( java.awt.Color.black ); | |
107 | + g.drawString( pangram, size.width, size.height ); | |
108 | + | |
109 | +#if JAVA | |
110 | + Graphics2D g2 = b.createGraphics(); | |
111 | + g2.drawImage( b, 0, 0, null ); | |
112 | +#else | |
113 | + b2 = new BitmapEx( b.m_image ); | |
114 | +#endif | |
115 | + // 上端に最初に現れる色つきピクセルを探す | |
116 | + int firsty = 0; | |
117 | + boolean found = false; | |
118 | + for ( int y = 0; y < h; y++ ) { | |
119 | + for ( int x = 0; x < w; x++ ) { | |
120 | +#if JAVA | |
121 | + int ic = b2.getRGB( x, y ); | |
122 | + Color c = new Color( ic ); | |
123 | +#else | |
124 | + java.awt.Color c = new bocoree.java.awt.Color( b2.GetPixel( x, y ) ); | |
125 | +#endif | |
126 | + if ( c.getRed() != 255 || c.getGreen() != 255 || c.getBlue() != 255 ) { | |
127 | + found = true; | |
128 | + firsty = y; | |
129 | + break; | |
130 | + } | |
131 | + } | |
132 | + if ( found ) { | |
133 | + break; | |
134 | + } | |
135 | + } | |
136 | + | |
137 | + // 下端 | |
138 | + int endy = h - 1; | |
139 | + found = false; | |
140 | + for ( int y = h - 1; y >= 0; y-- ) { | |
141 | + for ( int x = 0; x < w; x++ ) { | |
142 | +#if JAVA | |
143 | + int ic = b2.getRGB( x, y ); | |
144 | + Color c = new Color( ic ); | |
145 | +#else | |
146 | + java.awt.Color c = new bocoree.java.awt.Color( b2.GetPixel( x, y ) ); | |
147 | +#endif | |
148 | + if ( c.getRed() != 255 || c.getGreen() != 255 || c.getBlue() != 255 ) { | |
149 | + found = true; | |
150 | + endy = y; | |
151 | + break; | |
152 | + } | |
153 | + } | |
154 | + if ( found ) { | |
155 | + break; | |
156 | + } | |
157 | + } | |
158 | + | |
159 | + int center = (firsty + endy) / 2; | |
160 | + ret = center - (int)size.height; | |
161 | + } catch ( Exception ex ) { | |
162 | + } finally { | |
163 | +#if JAVA | |
164 | +#else | |
165 | + if ( b != null && b.m_image != null ) { | |
166 | + b.m_image.Dispose(); | |
167 | + } | |
168 | + if ( g != null ) { | |
169 | + g.nativeGraphics.Dispose(); | |
170 | + } | |
171 | + if ( b2 != null && b2 != null ) { | |
172 | + b2.Dispose(); | |
173 | + } | |
174 | +#endif | |
175 | + } | |
176 | + return ret; | |
177 | + } | |
178 | + | |
179 | + /// <summary> | |
180 | + /// 指定した言語コードの表す言語が、右から左へ記述する言語かどうかを調べます | |
181 | + /// </summary> | |
182 | + /// <param name="language_code"></param> | |
183 | + /// <returns></returns> | |
184 | + public static boolean isRightToLeftLanguage( String language_code ) { | |
185 | + language_code = language_code.ToLower(); | |
186 | + if ( language_code.Equals( "ar" ) || | |
187 | + language_code.Equals( "he" ) || | |
188 | + language_code.Equals( "iw" ) || | |
189 | + language_code.Equals( "fa" ) || | |
190 | + language_code.Equals( "ur" ) ) { | |
191 | + return true; | |
192 | + } else { | |
193 | + return false; | |
194 | + } | |
195 | + } | |
196 | + | |
197 | +#if !JAVA | |
198 | + /// <summary> | |
199 | + /// 指定したディレクトリに作成可能な、一時ファイル名を取得します | |
200 | + /// </summary> | |
201 | + /// <param name="directory">ディレクトリ</param> | |
202 | + /// <returns></returns> | |
203 | + public static string GetTempFileNameIn( string directory ) { | |
204 | + for ( uint i = uint.MinValue; i <= uint.MaxValue; i++ ) { | |
205 | + string file = Path.Combine( directory, "temp" + i ); | |
206 | + if ( !File.Exists( file ) ) { | |
207 | + return file; | |
208 | + } | |
209 | + } | |
210 | + return ""; | |
211 | + } | |
212 | +#endif | |
213 | + | |
214 | +#if !JAVA | |
215 | + /// <summary> | |
216 | + /// 指定したディレクトリに作成可能な、一時ファイル名を取得します | |
217 | + /// </summary> | |
218 | + /// <param name="directory">ディレクトリ</param> | |
219 | + /// <param name="extention">拡張子(ex. ".txt")</param> | |
220 | + /// <returns></returns> | |
221 | + public static string GetTempFileNameIn( string directory, string extention ){ | |
222 | + for ( uint i = uint.MinValue; i <= uint.MaxValue; i++ ) { | |
223 | + string file = Path.Combine( directory, "temp" + i + extention ); | |
224 | + if ( !File.Exists( file ) ) { | |
225 | + return file; | |
226 | + } | |
227 | + } | |
228 | + return ""; | |
229 | + } | |
230 | +#endif | |
231 | + | |
232 | +#if !JAVA | |
233 | + /// <summary> | |
234 | + /// 指定した画像ファイルから新しいBitmapオブジェクトを作成します | |
235 | + /// </summary> | |
236 | + /// <param name="file"></param> | |
237 | + /// <returns></returns> | |
238 | + public static Bitmap BitmapFromStream( string file ) { | |
239 | + if ( !File.Exists( file ) ) { | |
240 | + return null; | |
241 | + } | |
242 | + FileStream fs = new FileStream( file, FileMode.Open ); | |
243 | + Bitmap ret = new Bitmap( fs ); | |
244 | + fs.Close(); | |
245 | + return ret; | |
246 | + } | |
247 | +#endif | |
248 | + | |
249 | +#if !JAVA | |
250 | + /// <summary> | |
251 | + /// 指定した画像ファイルから新しいImageオブジェクトを作成します | |
252 | + /// </summary> | |
253 | + /// <param name="file"></param> | |
254 | + /// <returns></returns> | |
255 | + public static Image ImageFromStream( string file ) { | |
256 | + if ( !File.Exists( file ) ) { | |
257 | + return null; | |
258 | + } | |
259 | + FileStream fs = new FileStream( file, FileMode.Open ); | |
260 | + Image ret = Image.FromStream( fs ); | |
261 | + fs.Close(); | |
262 | + return ret; | |
263 | + } | |
264 | +#endif | |
265 | + | |
266 | +#if !JAVA | |
267 | + /// <summary> | |
268 | + /// ImageFormatから,デフォルトの拡張子を取得します | |
269 | + /// </summary> | |
270 | + /// <param name="format"></param> | |
271 | + /// <returns></returns> | |
272 | + public static string GetExtensionFromImageFormat( ImageFormat format ) { | |
273 | + switch ( format.ToString().ToLower() ) { | |
274 | + case "bmp": | |
275 | + return ".bmp"; | |
276 | + case "emf": | |
277 | + return ".emf"; | |
278 | + case "gif": | |
279 | + return ".gif"; | |
280 | + case "jpeg": | |
281 | + return ".jpg"; | |
282 | + case "png": | |
283 | + return ".png"; | |
284 | + case "tiff": | |
285 | + return ".tiff"; | |
286 | + case "wmf": | |
287 | + return ".wmf"; | |
288 | + default: | |
289 | + return ""; | |
290 | + | |
291 | + } | |
292 | + } | |
293 | +#endif | |
294 | + | |
295 | +#if !JAVA | |
296 | + /// <summary> | |
297 | + /// System.Drawimg.Imaging.ImageFormatで使用可能なフォーマットの一覧を取得します | |
298 | + /// </summary> | |
299 | + /// <returns></returns> | |
300 | + public static ImageFormat[] GetImageFormats() { | |
301 | +#if DEBUG | |
302 | + Console.WriteLine( "GetImageFormats()" ); | |
303 | +#endif | |
304 | + PropertyInfo[] properties = typeof( System.Drawing.Imaging.ImageFormat ).GetProperties(); | |
305 | + List<ImageFormat> ret = new List<ImageFormat>(); | |
306 | + foreach ( PropertyInfo pi in properties ) { | |
307 | + if ( pi.PropertyType.Equals( typeof( System.Drawing.Imaging.ImageFormat ) ) ) { | |
308 | + ImageFormat ifmt = (System.Drawing.Imaging.ImageFormat)pi.GetValue( null, null ); | |
309 | +#if DEBUG | |
310 | + Console.WriteLine( ifmt.ToString() ); | |
311 | +#endif | |
312 | + ret.Add( ifmt ); | |
313 | + } | |
314 | + } | |
315 | + return ret.ToArray(); | |
316 | + } | |
317 | +#endif | |
318 | + | |
319 | +#if !JAVA | |
320 | + public static void RgbToHsv( int r, int g, int b, out double h, out double s, out double v ) { | |
321 | + RgbToHsv( r / 255.0, g / 255.0, b / 255.0, out h, out s, out v ); | |
322 | + } | |
323 | + | |
324 | + public static void RgbToHsv( double r, double g, double b, out double h, out double s, out double v ) { | |
325 | + double tmph, imax, imin; | |
326 | + const double sqrt3 = 1.7320508075688772935274463415059; | |
327 | + imax = Math.Max( r, Math.Max( g, b ) ); | |
328 | + imin = Math.Min( r, Math.Min( g, b ) ); | |
329 | + if ( imax == 0.0 ) { | |
330 | + h = 0; | |
331 | + s = 0; | |
332 | + v = 0; | |
333 | + return; | |
334 | + } else if ( imax == imin ) { | |
335 | + tmph = 0; | |
336 | + } else { | |
337 | + if ( r == imax ) { | |
338 | + tmph = 60.0 * (g - b) / (imax - imin); | |
339 | + } else if ( g == imax ) { | |
340 | + tmph = 60.0 * (b - r) / (imax - imin) + 120.0; | |
341 | + } else { | |
342 | + tmph = 60.0 * (r - g) / (imax - imin) + 240.0; | |
343 | + } | |
344 | + } | |
345 | + while ( tmph < 0.0 ) { | |
346 | + tmph = tmph + 360.0; | |
347 | + } | |
348 | + while ( tmph >= 360.0 ) { | |
349 | + tmph = tmph - 360.0; | |
350 | + } | |
351 | + h = tmph / 360.0; | |
352 | + s = (imax - imin) / imax; | |
353 | + v = imax; | |
354 | + } | |
355 | + | |
356 | + public static Color HsvToColor( double h, double s, double v ) { | |
357 | + double dr, dg, db; | |
358 | + HsvToRgb( h, s, v, out dr, out dg, out db ); | |
359 | + return Color.FromArgb( (int)(dr * 255), (int)(dg * 255), (int)(db * 255) ); | |
360 | + } | |
361 | + | |
362 | + public static void HsvToRgb( double h, double s, double v, out byte r, out byte g, out byte b ) { | |
363 | + double dr, dg, db; | |
364 | + HsvToRgb( h, s, v, out dr, out dg, out db ); | |
365 | + r = (byte)(dr * 255); | |
366 | + g = (byte)(dg * 255); | |
367 | + b = (byte)(db * 255); | |
368 | + } | |
369 | + | |
370 | + public static void HsvToRgb( double h, double s, double v, out double r, out double g, out double b ) { | |
371 | + double f, p, q, t, hh; | |
372 | + int hi; | |
373 | + r = g = b = 0.0; | |
374 | + if ( s == 0 ) { | |
375 | + r = v; | |
376 | + g = v; | |
377 | + b = v; | |
378 | + } else { | |
379 | + hh = h * 360.0; | |
380 | + hi = (int)(hh / 60.0) % 6; | |
381 | + f = hh / 60.0 - (double)(hi); | |
382 | + p = v * (1.0 - s); | |
383 | + q = v * (1.0 - f * s); | |
384 | + t = v * (1.0 - (1.0 - f) * s); | |
385 | + switch ( hi ) { | |
386 | + case 0: | |
387 | + r = v; | |
388 | + g = t; | |
389 | + b = p; | |
390 | + break; | |
391 | + case 1: | |
392 | + r = q; | |
393 | + g = v; | |
394 | + b = p; | |
395 | + break; | |
396 | + case 2: | |
397 | + r = p; | |
398 | + g = v; | |
399 | + b = t; | |
400 | + break; | |
401 | + case 3: | |
402 | + r = p; | |
403 | + g = q; | |
404 | + b = v; | |
405 | + break; | |
406 | + case 4: | |
407 | + r = t; | |
408 | + g = p; | |
409 | + b = v; | |
410 | + break; | |
411 | + case 5: | |
412 | + r = v; | |
413 | + g = p; | |
414 | + b = q; | |
415 | + break; | |
416 | + } | |
417 | + } | |
418 | + } | |
419 | +#endif | |
420 | + | |
421 | + /// <summary> | |
422 | + /// 指定された文字列を指定されたフォントで描画したときのサイズを計測します。 | |
423 | + /// </summary> | |
424 | + /// <param name="text"></param> | |
425 | + /// <param name="font"></param> | |
426 | + /// <returns></returns> | |
427 | +#if JAVA | |
428 | + public static Dimension measureString( String text, Font font ){ | |
429 | + BufferedImage dumy = new BufferedImage( 1, 1, BufferedImage.TYPE_INT_BGR ); | |
430 | + Graphics2D g = dumy.createGraphics(); | |
431 | + g.setFont( font ); | |
432 | + FontMetrics fm = g.getFontMetrics(); | |
433 | + Dimension ret = new Dimension( fm.stringWidth( text ), fm.getHeight() ); | |
434 | + g = null; | |
435 | + dumy = null; | |
436 | + return ret; | |
437 | + } | |
438 | +#else | |
439 | + public static java.awt.Dimension measureString( string text, java.awt.Font font ) { | |
440 | + Size s = measureString( text, font.font ); | |
441 | + return new bocoree.java.awt.Dimension( s.Width, s.Height ); | |
442 | + } | |
443 | + | |
444 | + public static Size measureString( string text, Font font ) { | |
445 | + using ( Bitmap dumy = new Bitmap( 1, 1 ) ) | |
446 | + using ( Graphics g = Graphics.FromImage( dumy ) ) { | |
447 | + SizeF tmp = g.MeasureString( text, font ); | |
448 | + return new Size( (int)tmp.Width, (int)tmp.Height ); | |
449 | + } | |
450 | + } | |
451 | +#endif | |
452 | + | |
453 | + /// <summary> | |
454 | + /// 指定したコントロールと、その子コントロールのフォントを再帰的に変更します | |
455 | + /// </summary> | |
456 | + /// <param name="c"></param> | |
457 | + /// <param name="font"></param> | |
458 | +#if JAVA | |
459 | + public static void applyFontRecurse( Component c, Font font ){ | |
460 | +#else | |
461 | + public static void applyFontRecurse( Control c, java.awt.Font font ) { | |
462 | +#endif | |
463 | +#if JAVA | |
464 | + c.setFont( font ); | |
465 | + if( c instanceof Container ){ | |
466 | + Container container = (Container)c; | |
467 | + int count = container.getComponentCount(); | |
468 | + for( int i = 0; i < count; i++ ){ | |
469 | + Component component = container.getComponent( i ); | |
470 | + applyFontRecurse( component, font ); | |
471 | + } | |
472 | + } | |
473 | +#else | |
474 | + applyFontRecurse( c, font.font ); | |
475 | +#endif | |
476 | + } | |
477 | + | |
478 | +#if !JAVA | |
479 | + public static void applyFontRecurse( Control c, System.Drawing.Font font ) { | |
480 | + c.Font = font; | |
481 | + for ( int i = 0; i < c.Controls.Count; i++ ) { | |
482 | + applyFontRecurse( c.Controls[i], font ); | |
483 | + } | |
484 | + } | |
485 | +#endif | |
486 | + | |
487 | +#if !JAVA | |
488 | + /// <summary> | |
489 | + /// | |
490 | + /// </summary> | |
491 | + /// <param name="start1"></param> | |
492 | + /// <param name="end1"></param> | |
493 | + /// <param name="start2"></param> | |
494 | + /// <param name="end2"></param> | |
495 | + /// <returns></returns> | |
496 | + public static bool IsOverwrapped( double start1, double end1, double start2, double end2 ) { | |
497 | + if ( start2 <= start1 && start1 < end2 ) { | |
498 | + return true; | |
499 | + } else if ( start2 < end1 && end1 < end2 ) { | |
500 | + return true; | |
501 | + } else { | |
502 | + if ( start1 <= start2 && start2 < end1 ) { | |
503 | + return true; | |
504 | + } else if ( start1 < end2 && end2 < end1 ) { | |
505 | + return true; | |
506 | + } else { | |
507 | + return false; | |
508 | + } | |
509 | + } | |
510 | + } | |
511 | +#endif | |
512 | + } | |
513 | + | |
514 | +#if !JAVA | |
515 | +} | |
516 | +#endif |