/// <summary>
        /// Retrieves a <see cref="FontAsset"/>, creating it if necessary.
        /// </summary>
        /// <param name="fontFamily">The font family to use for the text.</param>
        /// <param name="fontSize">The size of the desired font.</param>
        /// <returns>A <see cref="FontAsset"/> object.</returns>
        public FontAsset GetFont(string fontFamily, float fontSize)
        {
            // Get the actual font file resource for this family
            FontFamily family = FontManager.GetFontFamily(fontFamily);

            if (family == null)
            {
                ServiceRegistration.Get <ILogger>().Warn("ContentManager: Could not get FontFamily '{0}', using default", fontFamily);
                family = FontManager.GetFontFamily(FontManager.DefaultFontFamily);
                if (family == null)
                {
                    return(null);
                }
            }

            // Round down font size
            int baseSize = (int)Math.Ceiling(fontSize * SkinContext.MaxZoomHeight);

            // If this function is called before the window is openned we get 0
            if (baseSize == 0)
            {
                baseSize = (int)fontSize;
            }
            // Generate the asset key we'll use to store this font
            string key = family.Name + "::" + baseSize;

            return(GetCreateAsset(AssetType.Font, key,
                                  assetCore => new FontAsset(assetCore as FontAssetCore),
                                  () => new FontAssetCore(family, baseSize, FontManager.DefaultDPI)) as FontAsset);
        }
Example #2
0
    /// <summary>
    /// Sets the font manager up with the specified <paramref name="resourcesCollection"/>.
    /// This method will load the font defaults (family and size) and the font files from the
    /// resource collection.
    /// </summary>
    public static void Load(IResourceAccessor resourcesCollection)
    {
      Unload();
      string defaultFontFilePath = resourcesCollection.GetResourceFilePath(
          SkinResources.FONTS_DIRECTORY + Path.DirectorySeparatorChar + DEFAULT_FONT_FILE);

      XPathDocument doc = new XPathDocument(defaultFontFilePath);

      XPathNavigator nav = doc.CreateNavigator();
      nav.MoveToChild(XPathNodeType.Element);
      _defaultFontFamily = nav.GetAttribute("FontFamily", string.Empty);
      string defaultFontSize = nav.GetAttribute("FontSize", string.Empty);
      _defaultFontSize = int.Parse(defaultFontSize);

      // Iterate over font family descriptors
      foreach (string descriptorFilePath in resourcesCollection.GetResourceFilePaths(
          "^" + SkinResources.FONTS_DIRECTORY + "\\\\.*\\.desc$").Values)
      {
        doc = new XPathDocument(descriptorFilePath);
        nav = doc.CreateNavigator();
        nav.MoveToChild(XPathNodeType.Element);
        string familyName = nav.GetAttribute("Name", string.Empty);
        if (string.IsNullOrEmpty(familyName))
          throw new ArgumentException("FontManager: Failed to parse family name for font descriptor file '{0}'", descriptorFilePath);
        string ttfFile = nav.GetAttribute("Ttf", string.Empty);
        if (string.IsNullOrEmpty(ttfFile))
          throw new ArgumentException("FontManager: Failed to parse ttf name for font descriptor file '{0}'", descriptorFilePath);

        string fontFilePath = resourcesCollection.GetResourceFilePath(
            SkinResources.FONTS_DIRECTORY + Path.DirectorySeparatorChar + ttfFile);
        FontFamily family = new FontFamily(familyName, fontFilePath);
        _families[familyName] = family;
      }
    }