/// <summary> /// Function to create a GDI+ linear gradient brush from a gorgon linear gradient font brush. /// </summary> /// <param name="region">The region to paint.</param> /// <param name="brush">Brush to convert.</param> /// <returns>The GDI+ brush.</returns> internal static Brush CreateLinearGradBrush(Rectangle region, GorgonGlyphBrush brush) { var linearBrush = (GorgonGlyphLinearGradientBrush)brush; var result = new LinearGradientBrush(region, linearBrush.StartColor, linearBrush.EndColor, linearBrush.Angle, linearBrush.ScaleAngle) { GammaCorrection = linearBrush.GammaCorrection }; if (linearBrush.Interpolation.Count == 0) { return(result); } var interpColors = new ColorBlend(linearBrush.Interpolation.Count.Max(1)); for (int i = 0; i < linearBrush.Interpolation.Count; i++) { interpColors.Colors[i] = linearBrush.Interpolation[i].Color; interpColors.Positions[i] = linearBrush.Interpolation[i].Weight; } result.InterpolationColors = interpColors; return(result); }
/// <summary> /// Function to generate the Gorgon bitmap fonts. /// </summary> /// <param name="fontFamilies">The list of TrueType font families to use.</param> /// <param name="window">The window that contains the loading message.</param> private static void GenerateGorgonFonts(IReadOnlyList <Drawing.FontFamily> fontFamilies, FormMain window) { // Pick a font to use with outlines. int fontWithOutlineIndex = GorgonRandom.RandomInt32(1, 5); _glowIndex = GorgonRandom.RandomInt32(fontWithOutlineIndex + 1, fontWithOutlineIndex + 5); int fontWithGradient = GorgonRandom.RandomInt32(_glowIndex + 1, _glowIndex + 5); int fontWithTexture = GorgonRandom.RandomInt32(fontWithGradient + 1, fontWithGradient + 5).Min(_fontFamilies.Count - 1); var pngCodec = new GorgonCodecPng(); using (IGorgonImage texture = pngCodec.LoadFromFile(Path.Combine(GorgonExample.GetResourcePath(@"Textures\Fonts\").FullName, "Gradient.png"))) { for (int i = 0; i < _fontFamilies.Count; ++i) { string fontFamily = _fontFamilies[i]; // Use this to determine if the font is avaiable. if (fontFamilies.All(item => !string.Equals(item.Name, fontFamily, StringComparison.InvariantCultureIgnoreCase))) { // Can't locate this one, move on... continue; } bool isExternal = Drawing.FontFamily.Families.All(item => !string.Equals(item.Name, fontFamily, StringComparison.InvariantCultureIgnoreCase)); string fontName; int outlineSize = 0; GorgonColor outlineColor1 = GorgonColor.BlackTransparent; GorgonColor outlineColor2 = GorgonColor.BlackTransparent; GorgonGlyphBrush brush = null; if (i == fontWithOutlineIndex) { fontName = $"{fontFamily} 32px Outlined{(isExternal ? " External TTF" : string.Empty)}"; outlineColor1 = GorgonColor.Black; outlineColor2 = GorgonColor.Black; outlineSize = 3; } else if (i == _glowIndex) { fontName = $"{fontFamily} 32px Outline as Glow{(isExternal ? " External TTF" : string.Empty)}"; outlineColor1 = new GorgonColor(GorgonColor.YellowPure, 1.0f); outlineColor2 = new GorgonColor(GorgonColor.DarkRed, 0.0f); outlineSize = 16; } else if (i == fontWithGradient) { fontName = $"{fontFamily} 32px Gradient{(isExternal ? " External TTF" : string.Empty)}"; brush = new GorgonGlyphLinearGradientBrush { StartColor = GorgonColor.White, EndColor = GorgonColor.Black, Angle = 45.0f }; } else if (i == fontWithTexture) { fontName = $"{fontFamily} 32px Textured{(isExternal ? " External TTF" : string.Empty)}"; brush = new GorgonGlyphTextureBrush(texture); } else { fontName = $"{fontFamily} 32px{(isExternal ? " External TTF" : string.Empty)}"; } window.UpdateStatus($"Generating Font: {fontFamily}".Ellipses(50)); var fontInfo = new GorgonFontInfo(fontFamily, 30.25f, name: fontName) { AntiAliasingMode = FontAntiAliasMode.AntiAlias, OutlineSize = outlineSize, OutlineColor1 = outlineColor1, OutlineColor2 = outlineColor2, UsePremultipliedTextures = false, Brush = brush }; _font.Add(_fontFactory.GetFont(fontInfo)); // Texture brushes have to be disposed when we're done with them. var disposableBrush = brush as IDisposable; disposableBrush?.Dispose(); } } }
/// <summary> /// Edits the specified object's value using the editor style indicated by the <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"/> method. /// </summary> /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that can be used to gain additional context information.</param> /// <param name="provider">An <see cref="T:System.IServiceProvider"/> that this editor can use to obtain services.</param> /// <param name="value">The object to edit.</param> /// <returns> /// The new value of the object. If the value of the object has not changed, this should return the same object it was passed. /// </returns> public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { var descriptor = (ContentTypeDescriptor)context.Instance; var document = (GorgonFontContent)descriptor.Content; FormBrushEditor brushEditor = null; try { EditorFile textureBrushFile = null; if (document.Brush.BrushType == GlyphBrushType.Texture) { Dependency dependency = document.Dependencies.SingleOrDefault(item => string.Equals(item.Type, GorgonFontContent.TextureBrushTextureType)); if (dependency != null) { textureBrushFile = dependency.EditorFile; } } brushEditor = new FormBrushEditor(document) { BrushType = document.Brush.BrushType }; switch (document.Brush.BrushType) { case GlyphBrushType.Texture: brushEditor.TextureBrush = (GorgonGlyphTextureBrush)document.Brush; brushEditor.TextureBrushFile = textureBrushFile; break; case GlyphBrushType.Hatched: brushEditor.PatternBrush = (GorgonGlyphHatchBrush)document.Brush; break; case GlyphBrushType.LinearGradient: brushEditor.GradientBrush = (GorgonGlyphLinearGradientBrush)document.Brush; break; case GlyphBrushType.Solid: brushEditor.SolidBrush = (GorgonGlyphSolidBrush)document.Brush; break; } GorgonGlyphBrush brush = document.Brush; if (brushEditor.ShowDialog() != DialogResult.OK) { return(brush); } // Destroy the previous brush. if (brush.BrushType == GlyphBrushType.Texture) { var textureBrush = (GorgonGlyphTextureBrush)document.Brush; if ((brushEditor.BrushType != GlyphBrushType.Texture) && (textureBrush.Texture != null)) { textureBrush.Texture.Dispose(); } // Remove the old texture brush dependency (if it existed). if (textureBrushFile != null) { if (document.Dependencies.Contains(textureBrushFile, GorgonFontContent.TextureBrushTextureType)) { document.Dependencies.Remove(textureBrushFile, GorgonFontContent.TextureBrushTextureType); } } } switch (brushEditor.BrushType) { case GlyphBrushType.Texture: brush = brushEditor.TextureBrush; // Add the updated texture brush dependency path. if (textureBrushFile != null) { document.Dependencies[brushEditor.TextureBrushFile, GorgonFontContent.TextureBrushTextureType] = new Dependency(brushEditor.TextureBrushFile, GorgonFontContent.TextureBrushTextureType); } break; case GlyphBrushType.Solid: brush = brushEditor.SolidBrush; break; case GlyphBrushType.LinearGradient: brush = brushEditor.GradientBrush; break; case GlyphBrushType.Hatched: brush = brushEditor.PatternBrush; break; } return(brush); } finally { if (brushEditor != null) { brushEditor.Dispose(); } } }