/// <summary> /// See interface docs. /// </summary> /// <param name="fontFamily"></param> /// <param name="pointSize"></param> /// <param name="fontStyle"></param> /// <param name="useCache"></param> /// <returns></returns> public VrsDrawing.IFont CreateFont(VrsDrawing.IFontFamily fontFamily, float pointSize, VrsDrawing.FontStyle fontStyle, bool useCache) { if (!useCache) { return(CreateFontWrapper(fontFamily, pointSize, fontStyle, isCached: false)); } else { return(GetOrCreateCachedFont(fontFamily, pointSize, fontStyle)); } }
/// <summary> /// Static initialisation. /// </summary> static ImageWrapper() { _FontFactory = Factory.ResolveSingleton <VrsDrawing.IFontFactory>(); _PinTextFontFamily = _FontFactory.GetFontFamilyOrFallback( _PinTextFontStyle, "Droid Sans", "MS Sans Serif", "Microsoft Sans Serif", "MS Reference Sans Serif", "Verdana", "Tahoma", "Roboto", "Helvetica", "Sans Serif", "Sans" ); }
/// <summary> /// See base docs. /// </summary> /// <param name="fontFamily"></param> /// <param name="pointSize"></param> /// <param name="fontStyle"></param> /// <param name="isCached"></param> /// <returns></returns> protected override VrsDrawing.IFont CreateFontWrapper(VrsDrawing.IFontFamily fontFamily, float pointSize, VrsDrawing.FontStyle fontStyle, bool isCached) { VrsDrawing.IFont result = null; if (fontFamily is FontFamilyWrapper fontFamilyWrapper) { result = new FontWrapper( new Font( fontFamilyWrapper.NativeFontFamily, pointSize, Convert.ToSystemDrawingFontStyle(fontStyle), GraphicsUnit.Point ), isCached ); } return(result); }
/// <summary> /// Returns a font out of the cache or, if no such font exists, creates and caches a new font for the /// properties passed across. /// </summary> /// <param name="fontFamily"></param> /// <param name="pointSize"></param> /// <param name="fontStyle"></param> /// <returns></returns> private VrsDrawing.IFont GetOrCreateCachedFont(VrsDrawing.IFontFamily fontFamily, float pointSize, VrsDrawing.FontStyle fontStyle) { var key = new FontKey(fontFamily.Name, pointSize, fontStyle); var map = _FontCache; if (!map.TryGetValue(key, out var font)) { lock (_SyncLock) { if (!_FontCache.TryGetValue(key, out font)) { font = CreateFontWrapper(fontFamily, pointSize, fontStyle, isCached: true); map = CollectionHelper.ShallowCopy(_FontCache); map.Add(key, font); _FontCache = map; } } } return(font); }
/// <summary> /// See interface docs. /// </summary> /// <param name="fontStyle"></param> /// <param name="fontFamilyNames"></param> /// <returns></returns> public VrsDrawing.IFontFamily GetFontFamilyOrFallback(VrsDrawing.FontStyle fontStyle, params string[] fontFamilyNames) { VrsDrawing.IFontFamily result = null; var installedFontFamilies = GetInstalledFonts().ToArray(); try { foreach (var familyName in fontFamilyNames) { result = installedFontFamilies.FirstOrDefault(r => String.Equals(familyName, r.Name, StringComparison.OrdinalIgnoreCase) && r.AvailableStyles.Contains(fontStyle) ); if (result != null) { break; } } if (result == null) { result = installedFontFamilies.FirstOrDefault(r => r.AvailableStyles.Contains(fontStyle)); } if (result == null) { result = installedFontFamilies.FirstOrDefault(); } } finally { foreach (var unusedFontFamily in installedFontFamilies.Where(r => !Object.ReferenceEquals(r, result))) { unusedFontFamily.Dispose(); } } return(result); }
/// <summary> /// Creates a new instance of a font wrapper. /// </summary> /// <param name="fontFamily"></param> /// <param name="pointSize"></param> /// <param name="fontStyle"></param> /// <param name="isCached"></param> /// <returns></returns> protected abstract VrsDrawing.IFont CreateFontWrapper(VrsDrawing.IFontFamily fontFamily, float pointSize, VrsDrawing.FontStyle fontStyle, bool isCached);
/// <summary> /// See interface docs. /// </summary> /// <param name="drawing"></param> /// <param name="fontFamily"></param> /// <param name="fontStyle"></param> /// <param name="largestPointSize"></param> /// <param name="smallestPointSize"></param> /// <param name="constrainToWidth"></param> /// <param name="constrainToHeight"></param> /// <param name="text"></param> /// <param name="useCache"></param> /// <returns></returns> public VrsDrawing.FontAndText GetFontForRectangle(VrsDrawing.IDrawing drawing, VrsDrawing.IFontFamily fontFamily, VrsDrawing.FontStyle fontStyle, float largestPointSize, float smallestPointSize, float constrainToWidth, float constrainToHeight, string text, bool useCache) { var textBuffer = new StringBuilder(text); var pointSize = largestPointSize; bool trySmaller; VrsDrawing.IFont font; do { font = CreateFont(fontFamily, pointSize, fontStyle, useCache); MeasureText(drawing, font, textBuffer.ToString(), out var width, out var height); trySmaller = width > constrainToWidth || height > constrainToHeight; if (trySmaller) { if (pointSize > smallestPointSize) { pointSize = Math.Max(smallestPointSize, pointSize - 0.25F); } else { if (textBuffer.Length == 0 || ImageLibraryDrawTextWillTruncate) { trySmaller = false; } else { textBuffer.Remove(textBuffer.Length - 1, 1); } } } } while(trySmaller); return(new VrsDrawing.FontAndText(font, text.ToString())); }