123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- using System;
- using System.Drawing;
- namespace FastReport.Utils
- {
- internal enum MonoRendering
- {
- Undefined,
- Pango,
- Cairo
- }
- public static partial class DrawUtils
- {
- private static Font FDefaultFont;
- private static Font FDefault96Font;
- private static Font FDefaultReportFont;
- private static Font FDefaultTextObjectFont;
- private static Font FFixedFont;
- private static int FScreenDpi;
- private static float FDpiFX;
- private static MonoRendering FMonoRendering = MonoRendering.Undefined;
- public static int ScreenDpi
- {
- get
- {
- if (FScreenDpi == 0)
- FScreenDpi = GetDpi();
- return FScreenDpi;
- }
- }
- public static float ScreenDpiFX
- {
- get
- {
- if (FDpiFX == 0)
- FDpiFX = 96f / DrawUtils.ScreenDpi;
- return FDpiFX;
- }
- }
- private static int GetDpi()
- {
- using (Bitmap bmp = new Bitmap(1, 1))
- using (Graphics g = Graphics.FromImage(bmp))
- {
- return (int)g.DpiX;
- }
- }
- public static Font DefaultFont
- {
- get
- {
- if (FDefaultFont == null)
- {
- switch (System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName)
- {
- case "ja":
- FDefaultFont = new Font("MS UI Gothic", 9);
- break;
- case "zh":
- FDefaultFont = new Font("SimSun", 9);
- break;
- default:
- FDefaultFont = new Font("Tahoma", 8);
- break;
- }
- }
- return FDefaultFont;
- }
- }
- public static Font DefaultReportFont
- {
- get
- {
- if (FDefaultReportFont == null)
- {
- switch (System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName)
- {
- case "ja":
- FDefaultReportFont = new Font("MS UI Gothic", 9);
- break;
- case "zh":
- FDefaultReportFont = new Font("SimSun", 9);
- break;
- default:
- FDefaultReportFont = new Font("Arial", 10);
- break;
- }
- }
- return FDefaultReportFont;
- }
- }
- public static Font DefaultTextObjectFont
- {
- get
- {
- if (FDefaultTextObjectFont == null)
- FDefaultTextObjectFont = new Font("Arial", 10);
- return FDefaultTextObjectFont;
- }
- }
- public static Font FixedFont
- {
- get
- {
- if (FFixedFont == null)
- FFixedFont = new Font("Courier New", 10);
- return FFixedFont;
- }
- }
- public static Font Default96Font
- {
- get
- {
- if (FDefault96Font == null)
- {
- float sz = 96f / ScreenDpi;
- switch (System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName)
- {
- case "ja":
- FDefault96Font = new Font("MS UI Gothic", 9 * sz);
- break;
- case "zh":
- FDefault96Font = new Font("SimSun", 9 * sz);
- break;
- default:
- FDefault96Font = new Font("Tahoma", 8 * sz);
- break;
- }
- }
- return FDefault96Font;
- }
- }
- public static SizeF MeasureString(string text)
- {
- return MeasureString(text, DefaultFont);
- }
- public static SizeF MeasureString(string text, Font font)
- {
- using (Bitmap bmp = new Bitmap(1, 1))
- using (StringFormat sf = new StringFormat())
- {
- Graphics g = Graphics.FromImage(bmp);
- return MeasureString(g, text, font, sf);
- }
- }
- public static SizeF MeasureString(Graphics g, string text, Font font, StringFormat format)
- {
- return MeasureString(g, text, font, new RectangleF(0, 0, 10000, 10000), format);
- }
- public static SizeF MeasureString(Graphics g, string text, Font font, RectangleF layoutRect, StringFormat format)
- {
- if (String.IsNullOrEmpty(text))
- return new SizeF(0, 0);
- CharacterRange[] characterRanges = { new CharacterRange(0, text.Length) };
- StringFormatFlags saveFlags = format.FormatFlags;
- format.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
- format.SetMeasurableCharacterRanges(characterRanges);
- Region[] regions = g.MeasureCharacterRanges(text, font, layoutRect, format);
- format.FormatFlags = saveFlags;
- RectangleF rect = regions[0].GetBounds(g);
- regions[0].Dispose();
- return rect.Size;
- }
- public static void FloodFill(Bitmap bmp, int x, int y, Color color, Color replacementColor)
- {
- if (x < 0 || y < 0 || x >= bmp.Width || y >= bmp.Height || bmp.GetPixel(x, y) != color)
- return;
- bmp.SetPixel(x, y, replacementColor);
- FloodFill(bmp, x - 1, y, color, replacementColor);
- FloodFill(bmp, x + 1, y, color, replacementColor);
- FloodFill(bmp, x, y - 1, color, replacementColor);
- FloodFill(bmp, x, y + 1, color, replacementColor);
- }
- internal static MonoRendering GetMonoRendering(Graphics printerGraphics)
- {
- if (FMonoRendering == MonoRendering.Undefined)
- {
- GraphicsUnit savedUnit = printerGraphics.PageUnit;
- printerGraphics.PageUnit = GraphicsUnit.Point;
- const string s = "test string test string test string test string";
- float f1 = printerGraphics.MeasureString(s, DefaultReportFont).Width;
- FMonoRendering = f1 > 200 ? MonoRendering.Pango : MonoRendering.Cairo;
- printerGraphics.PageUnit = savedUnit;
- }
- return FMonoRendering;
- }
-
- }
- }
|