/// <summary> /// Modify rendering settings (antialiasing, grid fitting) to improve appearance. /// </summary> void SetTextRenderingOptions(System.Drawing.Graphics gfx, Font font, TextQuality quality) { switch (quality) { case TextQuality.Default: gfx.TextRenderingHint = TextRenderingHint.SystemDefault; break; case TextQuality.High: gfx.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; break; case TextQuality.Medium: if (font.Size <= 18.0f) { gfx.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; } else { gfx.TextRenderingHint = TextRenderingHint.AntiAlias; } break; case TextQuality.Low: if (font.Size <= 18.0f) { gfx.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit; } else { gfx.TextRenderingHint = TextRenderingHint.SingleBitPerPixel; } break; } }
public Bitmap Rasterize(Glyph glyph, TextQuality quality) { EnsureSurfaceSize(ref glyph_surface, ref glyph_renderer, glyph.Font); SetTextRenderingOptions(glyph_renderer, glyph.Font, quality); RectangleF r2 = new RectangleF(); glyph_renderer.Clear(Color.Transparent); glyph_renderer.DrawString(glyph.Character.ToString(), glyph.Font, Brushes.White, Point.Empty, //new Point(glyph_surface.Width, 0), glyph.Font.Style == FontStyle.Italic ? load_glyph_string_format : load_glyph_string_format_tight); r2 = FindEdges(glyph_surface, true); //if ((default_string_format.FormatFlags & StringFormatFlags.DirectionRightToLeft) != 0) //{ // glyph_renderer.DrawString(glyph.Character.ToString(), glyph.Font, Brushes.White, Point.Empty, //new Point(glyph_surface.Width, 0), // load_glyph_string_format);//glyph.Font.Style == FontStyle.Italic ? load_glyph_string_format : default_string_format); // r2 = FindEdges(glyph_surface, true); //} //else //{ // glyph_renderer.DrawString(glyph.Character.ToString(), glyph.Font, Brushes.White, Point.Empty, // load_glyph_string_format_tight); //glyph.Font.Style == FontStyle.Italic ? load_glyph_string_format : default_string_format); // r2 = FindEdges(glyph_surface, false); //} return glyph_surface.Clone(r2, System.Drawing.Imaging.PixelFormat.Format32bppArgb); }
public Bitmap Rasterize(Glyph glyph, TextQuality quality) { EnsureSurfaceSize(ref glyph_surface, ref glyph_renderer, glyph.Font); SetTextRenderingOptions(glyph_renderer, glyph.Font, quality); RectangleF r2 = new RectangleF(); glyph_renderer.Clear(Color.Transparent); glyph_renderer.DrawString(glyph.Character.ToString(), glyph.Font, Brushes.White, Point.Empty, //new Point(glyph_surface.Width, 0), glyph.Font.Style == FontStyle.Italic ? load_glyph_string_format : load_glyph_string_format_tight); r2 = FindEdges(glyph_surface, true); //if ((default_string_format.FormatFlags & StringFormatFlags.DirectionRightToLeft) != 0) //{ // glyph_renderer.DrawString(glyph.Character.ToString(), glyph.Font, Brushes.White, Point.Empty, //new Point(glyph_surface.Width, 0), // load_glyph_string_format);//glyph.Font.Style == FontStyle.Italic ? load_glyph_string_format : default_string_format); // r2 = FindEdges(glyph_surface, true); //} //else //{ // glyph_renderer.DrawString(glyph.Character.ToString(), glyph.Font, Brushes.White, Point.Empty, // load_glyph_string_format_tight); //glyph.Font.Style == FontStyle.Italic ? load_glyph_string_format : default_string_format); // r2 = FindEdges(glyph_surface, false); //} return(glyph_surface.Clone(r2, System.Drawing.Imaging.PixelFormat.Format32bppArgb)); }
public GL11TextOutputProvider(TextQuality quality) { if (quality == TextQuality.High || quality == TextQuality.Default) this.quality = TextQuality.Medium; else this.quality = quality; }
public static GL1TextOutputProvider Create(TextQuality quality) { if (!GL.SupportsExtension("Version12") || !GL.SupportsFunction("BlendColor") || quality == TextQuality.Low || quality == TextQuality.Medium) { return(new GL11TextOutputProvider(quality)); } else { return(new GL12TextOutputProvider(quality)); } }
public GL11TextOutputProvider(TextQuality quality) { if (quality == TextQuality.High || quality == TextQuality.Default) { this.quality = TextQuality.Medium; } else { this.quality = quality; } }
public static GL1TextOutputProvider Create(TextQuality quality) { var extensions = GL.GetString(StringName.Extensions).Split(' '); var version = GL.GetInteger(GetPName.MajorVersion) + GL.GetInteger(GetPName.MinorVersion) / 10.0; if ((version < 1.2) || !extensions.Contains("GL_EXT_blend_color") || quality == TextQuality.Low || quality == TextQuality.Medium) { return(new GL11TextOutputProvider(quality)); } else { return(new GL12TextOutputProvider(quality)); } }
private TextRenderingHint GetTextQuality(TextQuality quality) { switch (quality) { case TextQuality.Regular: return(TextRenderingHint.AntiAliasGridFit); case TextQuality.ClearType: return(TextRenderingHint.ClearTypeGridFit); case TextQuality.AntiAlias: return(TextRenderingHint.AntiAlias); } return(TextRenderingHint.SystemDefault); }
public TextExtents MeasureText(ref TextBlock block, TextQuality quality) { // First, check if we have cached this text block. Do not use block_cache.TryGetValue, to avoid thrashing // the user's TextBlockExtents struct. int hashcode = block.GetHashCode(); if (block_cache.ContainsKey(hashcode)) { return(block_cache[hashcode]); } // If this block is not cached, we have to measure it and (potentially) place it in the cache. TextExtents extents = MeasureTextExtents(ref block, quality); if ((block.Options & TextPrinterOptions.NoCache) == 0) { block_cache.Add(hashcode, extents); } return(extents); }
TextExtents MeasureTextExtents(ref TextBlock block, TextQuality quality) { // TODO: Parse layout options: var format = block.Font.Italic ? measure_string_format : measure_string_format_tight; //StringFormat format = measure_string_format_tight; if (block.Direction == TextDirection.Vertical) { format.FormatFlags |= StringFormatFlags.DirectionVertical; } else { format.FormatFlags &= ~StringFormatFlags.DirectionVertical; } if (block.Direction == TextDirection.RightToLeft) { format.FormatFlags |= StringFormatFlags.DirectionRightToLeft; } else { format.FormatFlags &= ~StringFormatFlags.DirectionRightToLeft; } if (block.Alignment == TextAlignment.Near) { format.Alignment = StringAlignment.Near; } else if (block.Alignment == TextAlignment.Center) { format.Alignment = StringAlignment.Center; } else { format.Alignment = StringAlignment.Far; } TextExtents extents = text_extents_pool.Acquire(); RectangleF rect = block.Bounds; // Work around Mono/GDI+ bug, which causes incorrect // text wraping when block.Bounds == SizeF.Empty. if (block.Bounds.Size == SizeF.Empty) { rect.Size = MaximumGraphicsClipSize; } SetTextRenderingOptions(graphics, block.Font, quality); IntPtr native_graphics = GdiPlus.GetNativeGraphics(graphics); IntPtr native_font = GdiPlus.GetNativeFont(block.Font); IntPtr native_string_format = GdiPlus.GetNativeStringFormat(format); float max_width = 0, max_height = 0; // It seems that the mere presence of \n and \r characters // is enough for Mono to botch the layout (even if these // characters are not processed.) We'll need to find a // different way to perform layout on Mono, probably // through Pango. // TODO: This workaround allocates memory. //if (Configuration.RunningOnMono) { string[] lines = block.Text.Replace("\r", String.Empty).Split('\n'); foreach (string s in lines) { float width, height; extents.AddRange(MeasureGlyphExtents( ref block, s, native_graphics, native_font, native_string_format, ref rect, out width, out height)); if ((block.Direction & TextDirection.Vertical) == 0) { rect.Y += block.Font.Height; } else { rect.X += block.Font.Height; } if (width > max_width) { max_width = width; } if (height > max_height) { max_height = height; } } } if (extents.Count > 0) { extents.BoundingBox = new RectangleF(extents[0].X, extents[0].Y, max_width, max_height); } else { extents.BoundingBox = RectangleF.Empty; } return(extents); }
public abstract void Add(Glyph glyph, IGlyphRasterizer rasterizer, TextQuality quality);
/// <summary> /// Constructs a new TextPrinter instance with the specified TextQuality level. /// </summary> /// <param name="quality">The desired TextQuality of this TextPrinter.</param> public TextPrinter(TextQuality quality) : this(null, null, quality) { }
TextPrinter(IGlyphRasterizer rasterizer, ITextOutputProvider output, TextQuality quality) { glyph_rasterizer = rasterizer; text_output = output; text_quality = quality; }
public GL12TextOutputProvider(TextQuality quality) { this.quality = quality; cache = new GlyphCache<Texture2D>(); }
public static void DrawText(Graphics graphics, Font font, string text, Rectangle boundingRect, StringAlignment alignment, TextQuality textQuality, Brush textBrush) { if (graphics == null) { throw new ArgumentNullException("graphics"); } if (font == null) { throw new ArgumentNullException("font"); } if (text == null) { throw new ArgumentNullException("text"); } if (textBrush == null) { throw new ArgumentNullException("textBrush"); } if (!boundingRect.IsEmpty) { StringFormat format = new StringFormat { Alignment = alignment, LineAlignment = StringAlignment.Center, Trimming = StringTrimming.EllipsisCharacter, FormatFlags = StringFormatFlags.NoClip | StringFormatFlags.FitBlackBox }; TextRenderingHint textRenderingHint = graphics.TextRenderingHint; graphics.TextRenderingHint = (textQuality == TextQuality.AntiAliased) ? TextRenderingHint.AntiAlias : TextRenderingHint.SystemDefault; graphics.DrawString(text, font, textBrush, boundingRect, format); graphics.TextRenderingHint = textRenderingHint; } }
TextExtents MeasureTextExtents(ref TextBlock block, TextQuality quality) { // TODO: Parse layout options: var format = block.Font.Italic ? measure_string_format : measure_string_format_tight; //StringFormat format = measure_string_format_tight; if (block.Direction == TextDirection.Vertical) format.FormatFlags |= StringFormatFlags.DirectionVertical; else format.FormatFlags &= ~StringFormatFlags.DirectionVertical; if (block.Direction == TextDirection.RightToLeft) format.FormatFlags |= StringFormatFlags.DirectionRightToLeft; else format.FormatFlags &= ~StringFormatFlags.DirectionRightToLeft; if (block.Alignment == TextAlignment.Near) format.Alignment = StringAlignment.Near; else if (block.Alignment == TextAlignment.Center) format.Alignment = StringAlignment.Center; else format.Alignment = StringAlignment.Far; TextExtents extents = text_extents_pool.Acquire(); RectangleF rect = block.Bounds; // Work around Mono/GDI+ bug, which causes incorrect // text wraping when block.Bounds == SizeF.Empty. if (block.Bounds.Size == SizeF.Empty) rect.Size = MaximumGraphicsClipSize; SetTextRenderingOptions(graphics, block.Font, quality); IntPtr native_graphics = GdiPlus.GetNativeGraphics(graphics); IntPtr native_font = GdiPlus.GetNativeFont(block.Font); IntPtr native_string_format = GdiPlus.GetNativeStringFormat(format); float max_width = 0, max_height = 0; // It seems that the mere presence of \n and \r characters // is enough for Mono to botch the layout (even if these // characters are not processed.) We'll need to find a // different way to perform layout on Mono, probably // through Pango. // TODO: This workaround allocates memory. //if (Configuration.RunningOnMono) { string[] lines = block.Text.Replace("\r", String.Empty).Split('\n'); foreach (string s in lines) { float width, height; extents.AddRange(MeasureGlyphExtents( ref block, s, native_graphics, native_font, native_string_format, ref rect, out width, out height)); if ((block.Direction & TextDirection.Vertical) == 0) rect.Y += block.Font.Height; else rect.X += block.Font.Height; if (width > max_width) max_width = width; if (height > max_height) max_height = height; } } if (extents.Count > 0) extents.BoundingBox = new RectangleF(extents[0].X, extents[0].Y, max_width, max_height); else extents.BoundingBox = RectangleF.Empty; return extents; }
public static GL1TextOutputProvider Create(TextQuality quality) { var extensions = GL.GetString(StringName.Extensions).Split(' '); var version = GL.GetInteger(GetPName.MajorVersion) + GL.GetInteger(GetPName.MinorVersion) / 10.0; if ((version < 1.2) || !extensions.Contains("GL_EXT_blend_color") || quality == TextQuality.Low || quality == TextQuality.Medium) return new GL11TextOutputProvider(quality); else return new GL12TextOutputProvider(quality); }
public TextExtents MeasureText(ref TextBlock block, TextQuality quality) { // First, check if we have cached this text block. Do not use block_cache.TryGetValue, to avoid thrashing // the user's TextBlockExtents struct. int hashcode = block.GetHashCode(); if (block_cache.ContainsKey(hashcode)) return block_cache[hashcode]; // If this block is not cached, we have to measure it and (potentially) place it in the cache. TextExtents extents = MeasureTextExtents(ref block, quality); if ((block.Options & TextPrinterOptions.NoCache) == 0) block_cache.Add(hashcode, extents); return extents; }
/// <summary> /// Modify rendering settings (antialiasing, grid fitting) to improve appearance. /// </summary> void SetTextRenderingOptions(System.Drawing.Graphics gfx, Font font, TextQuality quality) { switch (quality) { case TextQuality.Default: gfx.TextRenderingHint = TextRenderingHint.SystemDefault; break; case TextQuality.High: gfx.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; break; case TextQuality.Medium: if (font.Size <= 18.0f) gfx.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; else gfx.TextRenderingHint = TextRenderingHint.AntiAlias; break; case TextQuality.Low: if (font.Size <= 18.0f) gfx.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit; else gfx.TextRenderingHint = TextRenderingHint.SingleBitPerPixel; break; } }
public GL12TextOutputProvider(TextQuality quality) { this.quality = quality; cache = new GlyphCache <RgbaTexture2D>(); }
public static GL1TextOutputProvider Create(TextQuality quality) { if (!GL.SupportsExtension("Version12") || !GL.SupportsFunction("BlendColor") || quality == TextQuality.Low || quality == TextQuality.Medium) return new GL11TextOutputProvider(quality); else return new GL12TextOutputProvider(quality); }