/// <summary> /// Parse a complex font family css property to check if it contains multiple fonts and if the font exists.<br/> /// returns the font family name to use or 'inherit' if failed. /// </summary> /// <param name="propValue">the value of the property to parse</param> /// <returns>parsed font-family value</returns> private static string ParseFontFamilyProperty(string propValue) { int start = 0; while (start > -1 && start < propValue.Length) { while (char.IsWhiteSpace(propValue[start]) || propValue[start] == ',' || propValue[start] == '\'' || propValue[start] == '"') { start++; } int end = propValue.IndexOf(',', start); if (end < 0) { end = propValue.Length; } int adjEnd = end - 1; while (char.IsWhiteSpace(propValue[adjEnd]) || propValue[adjEnd] == '\'' || propValue[adjEnd] == '"') { adjEnd--; } string font = propValue.Substring(start, adjEnd - start + 1); if (FontsUtils.IsFontExists(font)) { return(font); } start = end; } return(CssConstants.Inherit); }
/// <summary> /// Adds a font mapping from <paramref name="fromFamily"/> to <paramref name="toFamily"/> iff the <paramref name="fromFamily"/> is not found.<br/> /// When the <paramref name="fromFamily"/> font is used in rendered html and is not found in existing /// fonts (installed or added) it will be replaced by <paramref name="toFamily"/>.<br/> /// </summary> /// <remarks> /// This fonts mapping can be used as a fallback in case the requested font is not installed in the client system. /// </remarks> /// <param name="fromFamily">the font family to replace</param> /// <param name="toFamily">the font family to replace with</param> public static void AddFontFamilyMapping(string fromFamily, string toFamily) { ArgChecker.AssertArgNotNullOrEmpty(fromFamily, "fromFamily"); ArgChecker.AssertArgNotNullOrEmpty(toFamily, "toFamily"); FontsUtils.AddFontFamilyMapping(fromFamily, toFamily); }
/// <summary> /// Special draw logic to draw transparent text using GDI.<br/> /// 1. Create in-memory DC<br/> /// 2. Copy background to in-memory DC<br/> /// 3. Draw the text to in-memory DC<br/> /// 4. Copy the in-memory DC to the proper location with alpha blend<br/> /// </summary> private static void DrawTransparentText(IntPtr hdc, string str, Font font, Point point, Size size, Color color) { IntPtr dib; var memoryHdc = Win32Utils.CreateMemoryHdc(hdc, size.Width, size.Height, out dib); try { // copy target background to memory HDC so when copied back it will have the proper background Win32Utils.BitBlt(memoryHdc, 0, 0, size.Width, size.Height, hdc, point.X, point.Y, Win32Utils.BitBltCopy); // Create and select font Win32Utils.SelectObject(memoryHdc, FontsUtils.GetCachedHFont(font)); Win32Utils.SetTextColor(memoryHdc, (color.B & 0xFF) << 16 | (color.G & 0xFF) << 8 | color.R); // Draw text to memory HDC Win32Utils.TextOut(memoryHdc, 0, 0, str, str.Length); // copy from memory HDC to normal HDC with alpha blend so achieve the transparent text Win32Utils.AlphaBlend(hdc, point.X, point.Y, size.Width, size.Height, memoryHdc, 0, 0, size.Width, size.Height, new BlendFunction(color.A)); } finally { Win32Utils.ReleaseMemoryHdc(memoryHdc, dib); } }
/// <summary> /// Draw the given string using the given font and foreground color at given location. /// </summary> /// <param name="str">the string to draw</param> /// <param name="font">the font to use to draw the string</param> /// <param name="color">the text color to set</param> /// <param name="point">the location to start string draw (top-left)</param> /// <param name="size">used to know the size of the rendered text for transparent text support</param> public void DrawString(String str, Font font, Color color, PointF point, SizeF size) { if (_useGdiPlusTextRendering) { ReleaseHdc(); _g.DrawString(str, font, RenderUtils.GetSolidBrush(color), point.X - FontsUtils.GetFontLeftPadding(font) * .8f, point.Y); } else { if (color.A == 255) { SetFont(font); SetTextColor(color); Win32Utils.TextOut(_hdc, (int)Math.Round(point.X), (int)Math.Round(point.Y), str, str.Length); } else { InitHdc(); DrawTransparentText(_hdc, str, font, new Point((int)Math.Round(point.X), (int)Math.Round(point.Y)), Size.Round(size), color); } } }
/// <summary> /// Draw video title on top of the iframe if found. /// </summary> private void DrawTitle(IGraphics g, Rectangle rect) { if (_videoTitle != null && _imageWord.Width > 40 && _imageWord.Height > 40) { var font = FontsUtils.GetCachedFont("Arial", 9f, System.Drawing.FontStyle.Regular); g.FillRectangle(RenderUtils.GetSolidBrush(System.Drawing.Color.FromArgb(160, 0, 0, 0)), rect.Left, rect.Top, rect.Width, FontsUtils.GetFontHeight(font) + 7); using (var sf = new StringFormat(StringFormat.GenericTypographic)) { sf.FormatFlags = StringFormatFlags.NoWrap; sf.Trimming = StringTrimming.EllipsisCharacter; var titleRect = new RectangleF(rect.Left + 3, rect.Top + 3, rect.Width - 6, rect.Height - 6); g.DrawString(_videoTitle, font, System.Drawing.Color.WhiteSmoke, titleRect.Location, SizeF.Empty); } } }
/// <summary> /// Adds a font family to be used in html rendering.<br/> /// The added font will be used by all rendering function including <see cref="HtmlContainer"/> and all WinForms controls. /// </summary> /// <remarks> /// The given font family instance must be remain alive while the renderer is in use.<br/> /// If loaded to <see cref="PrivateFontCollection"/> then the collection must be alive.<br/> /// If loaded from file then the file must not be deleted. /// </remarks> /// <param name="fontFamily">The font family to add.</param> public static void AddFontFamily(FontFamily fontFamily) { ArgChecker.AssertArgNotNull(fontFamily, "fontFamily"); FontsUtils.AddFontFamily(fontFamily); }
/// <summary> /// Gets the height of the font in the specified units /// </summary> /// <returns></returns> public float GetEmHeight() { return(FontsUtils.GetFontHeight(ActualFont)); }
/// <summary> /// Set a resource (e.g. a font) for the specified device context. /// WARNING: Calling Font.ToHfont() many times without releasing the font handle crashes the app. /// </summary> private void SetFont(Font font) { InitHdc(); Win32Utils.SelectObject(_hdc, FontsUtils.GetCachedHFont(font)); }