void SelectFont() { _c.SetFont(CGFont.CreateWithFontName(_lastFont.FontFamily)); _c.SetFontSize(_lastFont.FontSize); _c.SetFont(_lastFont.ToUIFont()); _c.TextMatrix = _textMatrix; }
internal AppleMathFont(string name, CGFont cgFont, float size) : this(size) { Name = name; CgFont = cgFont; var transform = CGAffineTransform.MakeIdentity(); CtFont = new CTFont(CgFont, size, transform); }
internal AppleMathFont(string name, CGFont cgFont, float size) { PointSize = size; Name = name; CgFont = cgFont; CtFont = new CTFont(CgFont, size, CGAffineTransform.MakeIdentity()); }
public IFont LoadFontFromPath(string path, float sizeInPoints, FontStyle style = FontStyle.Regular) { CGFont cgFont = _installedFonts.GetOrAdd(path, _ => { try { CGFont createdFont = CGFont.CreateFromProvider(new CGDataProvider(path)); NSError error; if (!CTFontManager.RegisterGraphicsFont(createdFont, out error)) { Debug.WriteLine($"Failed to load font from {path} (loading default font instead), error: {error.ToString()}"); return(null); } return(createdFont); } catch (ArgumentException e) { Debug.WriteLine($"Failed to load font from {path} (loading default font instead), exception: {e.ToString()}"); return(null); } }); if (cgFont == null) { return(LoadFont("Helvetica", sizeInPoints, style)); } CTFont font = new CTFont(cgFont, sizeInPoints, CGAffineTransform.MakeIdentity()); font = setFontStyle(font, sizeInPoints, style); return(new IOSFont(font, style, this)); }
public static CTFont ToCTFont(this Font font) { using (var cgFont = CGFont.CreateWithFontName(font.Name)) { return(new CTFont(cgFont, font.Size, CGAffineTransform.MakeIdentity())); } }
private static void InitTypefaceWeightRamp() { lock (InitSyncObject) { if (null == FontRamp) { CGFont[] fontRamp = new CGFont[5]; // Weight 100 fontRamp[0] = CGFont.CreateWithFontName("AvenirNext-UltraLight"); // Weight 300 fontRamp[1] = CGFont.CreateWithFontName("AvenirNext-Regular"); // Weight 400 fontRamp[2] = CGFont.CreateWithFontName("AvenirNext-Medium"); // Weight 500 fontRamp[3] = CGFont.CreateWithFontName("AvenirNext-DemiBold"); // Weight 700 fontRamp[4] = CGFont.CreateWithFontName("AvenirNext-Bold"); FontRamp = fontRamp; } } }
public (bool success, string filePath) LoadFont(EmbeddedFont font) { try { var data = NSData.FromStream(font.ResourceStream); var provider = new CGDataProvider(data); var cGFont = CGFont.CreateFromProvider(provider); var name = cGFont.PostScriptName; if (CTFontManager.RegisterGraphicsFont(cGFont, out var error)) { return(true, name); } else //Lets check if the font is already registered { var uiFont = NSFont.FromFontName(name, 10); if (uiFont != null) { return(true, name); } } Debug.WriteLine(error.Description); } catch (Exception ex) { Debug.WriteLine(ex); } return(false, null); }
internal static void loadFontIfNeeded(string fontName) { string loadedFont; MaterialFontLoader.LoadedFonts.TryGetValue(fontName, out loadedFont); if ((loadedFont == null) && (UIFont.FromName(name: fontName, size: 1) == null)) { MaterialFontLoader.LoadedFonts[fontName] = fontName; var bundle = NSBundle.FromClass(new Class(typeof(MaterialFontLoader))); var identifier = bundle.BundleIdentifier; var fontURL = bundle.GetUrlForResource(name: fontName, fileExtension: "ttf", subdirectory: "Fonts/Roboto"); if (fontURL != null) { var data = NSData.FromUrl(fontURL); var provider = new CGDataProvider(data); var font = CGFont.CreateFromProvider(provider); NSError error; if (!CoreText.CTFontManager.RegisterGraphicsFont(font, out error)) { var errerrorDescription = error.Description; throw new Exception(error.Description); //new NSException(name: NSException.con, reason: error.Description, userInfo: error.UserInfo); } } } }
static UIFont LoadAndRegisterEmbeddedFont(string resourceId, nfloat size, Assembly assembly) { using (var stream = EmbeddedResourceCache.GetStream(resourceId, assembly)) { if (stream == null) { Console.WriteLine("Could not open Embedded Resource [" + resourceId + "]"); return(null); } var data = NSData.FromStream(stream); if (data == null) { Console.WriteLine("Could not retrieve data from Embedded Resource [" + resourceId + "]."); return(null); } var provider = new CGDataProvider(data); var font = CGFont.CreateFromProvider(provider); if (!CTFontManager.RegisterGraphicsFont(font, out NSError error)) { if (error.Code != 105) { Console.WriteLine("Could load but failed to register [" + resourceId + "] font. Error messages:"); Console.WriteLine("\tdescription: " + error.Description); throw new MissingMemberException("Failed to register [" + resourceId + "] font. Error messages in console."); } } _embeddedResourceFonts.Add(resourceId, font.PostScriptName); return(UIFont.FromName(font.PostScriptName, size)); } }
private static UIFont GetFontFromFile(nfloat size, string file) { var fileName = Path.GetFileNameWithoutExtension(file); var fileExtension = Path.GetExtension(file)?.Replace(".", ""); var url = NSBundle .MainBundle .GetUrlForResource( name: fileName, fileExtension: fileExtension, subdirectory: "Fonts" ); if (url == null) { return(null); } var fontData = NSData.FromUrl(url); if (fontData == null) { return(null); } //iOS loads UIFonts based on the PostScriptName of the font file using (var fontProvider = new CGDataProvider(fontData)) { using (var font = CGFont.CreateFromProvider(fontProvider)) { return(font != null?UIFont.FromName(font.PostScriptName, size) : null); } } }
//We need to register fonts with iOS runtime //Take a look at following article for more details //https://marco.org/2012/12/21/ios-dynamic-font-loading protected override async Task InstallFonts(IEnumerable <CustomFont> fonts) { NSError error = null; foreach (var font in fonts) { //If font data is null we need to load it from a font file if (font.Data == null) { var fontFile = await FontFolder.GetFileAsync(font.File); using (var stream = await fontFile.OpenAsync(FileAccess.Read)) { font.Data = new byte[stream.Length]; await stream.ReadAsync(font.Data, 0, font.Data.Length); } } var fontData = NSData.FromArray(font.Data); using (CGDataProvider provider = new CGDataProvider(fontData)) { using (CGFont nativeFont = CGFont.CreateFromProvider(provider)) { CTFontManager.RegisterGraphicsFont(nativeFont, out error); if (error != null) { Console.WriteLine(error); } } } } }
public static bool UnregisterGraphicsFont(CGFont font, out NSError error) { if (font == null) { throw new ArgumentNullException("font"); } IntPtr h = IntPtr.Zero; bool ret; try { ret = CTFontManagerUnregisterGraphicsFont(font.Handle, out h); if (ret) { error = null; } else { error = new NSError(h); } } finally { if (h != IntPtr.Zero) { NSObject.DangerousRelease(h); } } return(ret); }
public (bool success, string?filePath) LoadFont(EmbeddedFont font) { try { if (font.ResourceStream == null) { throw new InvalidOperationException("ResourceStream was null."); } var data = NSData.FromStream(font.ResourceStream); var provider = new CGDataProvider(data); var cGFont = CGFont.CreateFromProvider(provider); var name = cGFont.PostScriptName; if (CTFontManager.RegisterGraphicsFont(cGFont, out var error)) { return(true, name); } var uiFont = UIFont.FromName(name, 10); if (uiFont != null) { return(true, name); } throw new NSErrorException(error); } catch (Exception ex) { _logger?.LogWarning(ex, "Unable register font {Font} with the system.", font.FontName); } return(false, null); }
public void GetGlyphWithGlyphName() { using (var f = CGFont.CreateWithFontName("Courier New")) { Assert.That(f.Handle, Is.Not.EqualTo(IntPtr.Zero), "CreateWithFontName"); // note: the API is marked to accept a null CFStringRef but it currently (iOS9 beta 4) crash when provided one Assert.Throws <ArgumentNullException> (() => f.GetGlyphWithGlyphName(null), "GetGlyphWithGlyphName"); } }
public void SetFont(CGFont font) { if (font == null) { ObjCRuntime.ThrowHelper.ThrowArgumentNullException(nameof(font)); } _Font = font.Handle; }
public void SetFont(CGFont font) { if (font == null) { throw new ArgumentNullException("font"); } _Font = font.Handle; }
public static XIR.Image RemoteRepresentation(this CGFont cgfont) { var atts = new CTStringAttributes(); atts.Font = new CTFont(cgfont, 12, null); var attdString = new NSAttributedString(exampleString, atts.Dictionary); return(attdString.RemoteRepresentation()); }
public void GetGlyphsForCharacters_35048() { using (var font = CGFont.CreateWithFontName("AppleColorEmoji")) using (var ctfont = font.ToCTFont((nfloat)10.0)) { ushort[] gid = new ushort [2]; Assert.True(ctfont.GetGlyphsForCharacters("\ud83d\ude00".ToCharArray(), gid), "GetGlyphsForCharacters"); Assert.That(gid [0], Is.Not.EqualTo(0), "0"); Assert.That(gid [1], Is.EqualTo(0), "1"); } }
private static CGFont _CreateLatinMathCg() { var manifestProvider = IosResourceProviders.Manifest(); byte[] buffer = manifestProvider.ManifestContents(LatinMathFontName + ".otf"); using (CGDataProvider fontDataProvider = new CGDataProvider(buffer)) { var r = CGFont.CreateFromProvider(fontDataProvider); return(r); } }
static AppleMathFont() { using var fontDataProvider = new CGDataProvider( Foundation.NSData.FromStream( new Resources.ManifestResourceProvider( System.Reflection.Assembly.GetExecutingAssembly() ).ManifestStream(LatinMathFontName + ".otf") ) ); LatinMathCG = CGFont.CreateFromProvider(fontDataProvider); }
public void CTFontCopyNameForGlyph() { TestRuntime.AssertXcodeVersion(12, 0); using (var ctfont = new CTFont("HoeflerText-Regular", 10, CTFontOptions.Default)) Assert.That(ctfont.GetGlyphName((ushort)65), Is.EqualTo("asciicircum"), "1"); using (var font = CGFont.CreateWithFontName("AppleColorEmoji")) using (var ctfont = font.ToCTFont((nfloat)10.0)) Assert.Null(ctfont.GetGlyphName('\ud83d'), "2"); }
public string?LoadFont(EmbeddedFont font) { try { CGFont?cgFont; if (font.ResourceStream == null) { if (!System.IO.File.Exists(font.FontName)) { throw new InvalidOperationException("ResourceStream was null."); } var provider = new CGDataProvider(font.FontName); cgFont = CGFont.CreateFromProvider(provider); } else { var data = NSData.FromStream(font.ResourceStream); if (data == null) { throw new InvalidOperationException("Unable to load font stream data."); } var provider = new CGDataProvider(data); cgFont = CGFont.CreateFromProvider(provider); } if (cgFont == null) { throw new InvalidOperationException("Unable to load font from the stream."); } var name = cgFont.PostScriptName; if (CTFontManager.RegisterGraphicsFont(cgFont, out var error)) { return(name); } var uiFont = UIFont.FromName(name, 10); if (uiFont != null) { return(name); } throw new NSErrorException(error); } catch (Exception ex) { _serviceProvider?.CreateLogger <EmbeddedFontLoader>()?.LogWarning(ex, "Unable register font {Font} with the system.", font.FontName); } return(null); }
public void GetBoundingRect_WithEmptyGlyphs() { CGFont cgFont = CGFont.CreateWithFontName("Arial"); ushort [] glyphs = new ushort [] {}; CTFont ctFont = new CTFont(cgFont, 14, new CTFontDescriptor("Arial", 14)); NSFont nsFont = NSFont.FromCTFont(ctFont); Assert.Throws <ArgumentException> (() => nsFont.GetBoundingRects(glyphs)); Assert.Throws <ArgumentException> (() => nsFont.GetAdvancements(glyphs)); }
public CTFont(CGFont font, float size, CTFontDescriptor descriptor) { if (font == null) { throw new ArgumentNullException("font"); } handle = CTFontCreateWithGraphicsFont2(font.Handle, size, IntPtr.Zero, descriptor == null ? IntPtr.Zero : descriptor.Handle); if (handle == IntPtr.Zero) { throw ConstructorError.Unknown(this); } }
public CTFont(CGFont font, float size, CGAffineTransform transform) { if (font == null) { throw new ArgumentNullException("font"); } handle = CTFontCreateWithGraphicsFont(font.Handle, size, ref transform, IntPtr.Zero); if (handle == IntPtr.Zero) { throw ConstructorError.Unknown(this); } }
public CGFont GetCustomFont(string name) { if (name == null) { return(CGFont.CreateWithFontName(_systemFontName)); } if (_customFonts.TryGetValue(name, out var font)) { return(font); } return(CGFont.CreateWithFontName(_systemFontName)); }
private static Tuple <NSMutableAttributedString, List <NewsCellView.Link> > CreateAttributedStringFromBlocks(UIFont font, UIColor primaryColor, IEnumerable <TextBlock> blocks) { var attributedString = new NSMutableAttributedString(); var links = new List <NewsCellView.Link>(); nint lengthCounter = 0; int i = 0; CoreText.CTFont ctFont; try { ctFont = new CoreText.CTFont(font.FamilyName, font.PointSize); } catch { ctFont = CGFont.CreateWithFontName(font.Name).ToCTFont(font.PointSize); } foreach (var b in blocks) { UIColor color = null; if (b.Tapped != null) { color = LinkColor; } color = color ?? primaryColor; var str = new NSAttributedString(b.Value, new CoreText.CTStringAttributes() { ForegroundColor = color.CGColor, Font = ctFont }); attributedString.Append(str); var strLength = str.Length; if (b.Tapped != null) { var weakTapped = new WeakReference <Action>(b.Tapped); links.Add(new NewsCellView.Link { Range = new NSRange(lengthCounter, strLength), Callback = () => weakTapped.Get()?.Invoke(), Id = i++ }); } lengthCounter += strLength; } return(new Tuple <NSMutableAttributedString, List <NewsCellView.Link> >(attributedString, links)); }
public static void LoadFont(string name, string format) { var bundle = NSBundle.FromClass(new Class(typeof(NBMaterialCircularActivityIndicator))); var fontURL = bundle.GetUrlForResource(name, format); var data = NSData.FromUrl(fontURL); var provider = new CGDataProvider(data); var font = CGFont.CreateFromProvider(provider); NSError error; if (!CTFontManager.RegisterGraphicsFont(font, out error)) { throw new NSErrorException(error); } }
public void GetBoundingRect_WithEmptyGlyphs() { if (!PlatformHelper.CheckSystemVersion(10, 13)) { return; } CGFont cgFont = CGFont.CreateWithFontName("Arial"); ushort [] glyphs = new ushort [] {}; CTFont ctFont = new CTFont(cgFont, 14, new CTFontDescriptor("Arial", 14)); NSFont nsFont = NSFont.FromCTFont(ctFont); Assert.Throws <ArgumentException> (() => nsFont.GetBoundingRects(glyphs)); Assert.Throws <ArgumentException> (() => nsFont.GetAdvancements(glyphs)); }
public override void Draw(RectangleF rect) { base.Draw(rect); using (var context = UIGraphics.GetCurrentContext()) { UIBezierPath mainAxis = UIBezierPath.FromRoundedRect(new RectangleF(xAxisMargin, 0.0f, 2.0f, rect.Height), 2.0f); UIColor.FromRGB(203, 203, 203).SetFill(); mainAxis.Fill(); float sepInterval = Convert.ToSingle(Math.Floor((rect.Width - xAxisMargin - xTextMargin) / 5)); for (int i = 1; i < 6; i++) { var separatorAxis = new UIBezierPath(); separatorAxis.MoveTo(new PointF(xAxisMargin + sepInterval * i, 0)); separatorAxis.AddLineTo(new PointF(xAxisMargin + sepInterval * i, rect.Height - yAxisMargin)); UIColor.FromRGB(203, 203, 203).SetStroke(); separatorAxis.LineWidth = 1.0f; separatorAxis.SetLineDash(new [] { 1.0f, 1.0f }, 1); separatorAxis.Stroke(); var textLayer = new CATextLayer(); textLayer.ContentsScale = UIScreen.MainScreen.Scale; CGFont font = CGFont.CreateWithFontName(LabelFont.Name); if (font != null) { textLayer.SetFont(font); font.Dispose(); } textLayer.FontSize = LabelFont.PointSize; textLayer.AnchorPoint = new PointF(0.5f, 0.0f); textLayer.AlignmentMode = CATextLayer.AlignmentCenter; textLayer.BackgroundColor = UIColor.Clear.CGColor; textLayer.ForegroundColor = LabelColor.CGColor; SizeF size = ((NSString)"0000 h").StringSize(LabelFont); textLayer.String = "00 h"; textLayer.Bounds = new RectangleF(0, 0, size.Width, size.Height); textLayer.Position = new PointF(xAxisMargin + sepInterval * i, rect.Height - yAxisMargin + 5.0f); Layer.AddSublayer(textLayer); xAxisText [i - 1] = textLayer; } } }