예제 #1
0
        public FontManager(string languageName, StringComparer fontNameComparer)
        {
            var assembly = Assembly.GetExecutingAssembly();

            // For the json file, I think it's better to leave it on the disk
            // (instead of embedding it) so users can edit it
            using var assemblyFileSystem = new FileSystem(Path.GetDirectoryName(assembly.Location));
            using var fileSystem         = new FileSystem(Environment.CurrentDirectory, assemblyFileSystem);

            var fontFallbackSettingsJson  = "{}";
            var fontFallbackSettingsEntry = fileSystem.GetFile("Content/Fonts/FontFallbackSettings.json");

            if (fontFallbackSettingsEntry != null || true)
            {
                Logger.Info($"FontFallback Settings loaded from {fontFallbackSettingsEntry.FullFilePath}");
                using var stream         = fontFallbackSettingsEntry.Open();
                using var reader         = new StreamReader(stream, Encoding.UTF8);
                fontFallbackSettingsJson = reader.ReadToEnd();
            }

            _cachedFonts          = new Dictionary <FontKey, Font>();
            _fontFallbackSettings = FontFallbackSettings
                                    .LoadFromJson(fontFallbackSettingsJson)
                                    .LoadLanguageSpecificFallbackSettings(languageName, fontNameComparer);
            _fallbackFonts = new FontCollection();


            var fontStream = assembly.GetManifestResourceStream($"OpenSage.Content.Fonts.{FallbackEmbeddedFont}-Regular.ttf");

            _fallbackFonts.Install(fontStream);
            fontStream = assembly.GetManifestResourceStream($"OpenSage.Content.Fonts.{FallbackEmbeddedFont}-Bold.ttf");
            _fallbackFonts.Install(fontStream);
        }
예제 #2
0
        /// <summary>
        /// Make <see cref="SpecificFontFallbacks"/> case insensitive,
        /// also load default fallbacks from parent settings.
        /// </summary>
        /// <param name="comparer">
        /// The case insensitive <see cref="StringComparer"/> to be used with
        /// <see cref="SpecificFontFallbacks"/>.
        /// </param>
        /// <param name="defaultSettings">
        /// The <see cref="LanguageSpecificFontFallbackSettings"/> of default language.
        /// </param>
        public void Normalize(StringComparer comparer, LanguageSpecificFontFallbackSettings?defaultSettings)
        {
            if (Normalized)
            {
                // already normalized
                throw new InvalidOperationException();
            }

            if (defaultSettings?.Normalized == false)
            {
                throw new InvalidOperationException();
            }

            SpecificFontFallbacks = FontFallbackSettings.NormalizeDictionary(SpecificFontFallbacks, comparer);
            DefaultFallbacks ??= new string[0];
            _defaultSettings = this != defaultSettings ? defaultSettings : null;

            Normalized = true;
        }