Bladeren bron

experimental font overriding support

Scrublord1336 6 jaren geleden
bovenliggende
commit
a242fb74e0

+ 28 - 0
src/XUnity.AutoTranslator.Plugin.Core/AutoTranslationPlugin.cs

@@ -107,6 +107,8 @@ namespace XUnity.AutoTranslator.Plugin.Core
       private int _secondForQueuedTranslation = -1;
       private int _consecutiveSecondsTranslated = 0;
 
+      private bool _changeFont = false;
+
       public void Initialize()
       {
          Current = this;
@@ -140,6 +142,20 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
          _symbolCheck = TextHelper.GetSymbolCheck( Settings.FromLanguage );
 
+         if( !string.IsNullOrEmpty( Settings.OverrideFont ) )
+         {
+            var available = Font.GetOSInstalledFontNames();
+            if( !available.Contains( Settings.OverrideFont ) )
+            {
+               Logger.Current.Error( $"The specified override font is not available. Available fonts: " + string.Join( ", ", available ) );
+               Settings.OverrideFont = null;
+            }
+            else
+            {
+               _changeFont = true;
+            }
+         }
+
          LoadTranslations();
          LoadStaticTranslations();
 
@@ -646,6 +662,18 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
                ui.SetText( text );
 
+               if( _changeFont )
+               {
+                  if( isTranslated )
+                  {
+                     info?.ChangeFont( ui );
+                  }
+                  else
+                  {
+                     info?.UnchangeFont( ui );
+                  }
+               }
+
                if( Settings.EnableUIResizing )
                {
                   if( isTranslated )

+ 2 - 0
src/XUnity.AutoTranslator.Plugin.Core/Configuration/Settings.cs

@@ -67,6 +67,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
       public static bool EnableUIResizing;
       public static string GoogleAPIKey;
       public static bool UseStaticTranslations;
+      public static string OverrideFont;
 
       public static bool CopyToClipboard;
       public static int MaxClipboardCopyCharacters;
@@ -115,6 +116,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
          EnableBatching = Config.Current.Preferences[ "Behaviour" ][ "EnableBatching" ].GetOrDefault( true );
          TrimAllText = Config.Current.Preferences[ "Behaviour" ][ "TrimAllText" ].GetOrDefault( Types.AdvEngine == null );
          UseStaticTranslations = Config.Current.Preferences[ "Behaviour" ][ "UseStaticTranslations" ].GetOrDefault( true );
+         OverrideFont = Config.Current.Preferences[ "Behaviour" ][ "OverrideFont" ].GetOrDefault( string.Empty );
 
          GoogleAPIKey = Config.Current.Preferences[ "GoogleLegitimate" ][ "GoogleAPIKey" ].GetOrDefault( "" );
 

+ 25 - 0
src/XUnity.AutoTranslator.Plugin.Core/Fonts/FontCache.cs

@@ -0,0 +1,25 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using UnityEngine;
+using XUnity.AutoTranslator.Plugin.Core.Configuration;
+
+namespace XUnity.AutoTranslator.Plugin.Core.Fonts
+{
+   public static class FontCache
+   {
+      private static readonly Dictionary<int, Font> CachedFonts = new Dictionary<int, Font>();
+
+      public static Font GetOrCreate( int size )
+      {
+         if( !CachedFonts.TryGetValue( size, out Font font ) )
+         {
+            font = Font.CreateDynamicFontFromOSFont( Settings.OverrideFont, size );
+            GameObject.DontDestroyOnLoad( font );
+            CachedFonts.Add( size, font );
+         }
+         return font;
+      }
+   }
+}

+ 49 - 10
src/XUnity.AutoTranslator.Plugin.Core/TranslationInfo.cs

@@ -4,16 +4,22 @@ using System.Linq;
 using System.Text;
 using UnityEngine;
 using UnityEngine.UI;
+using XUnity.AutoTranslator.Plugin.Core.Configuration;
+using XUnity.AutoTranslator.Plugin.Core.Fonts;
 
 namespace XUnity.AutoTranslator.Plugin.Core
 {
    public class TranslationInfo
    {
+      private static readonly string FontSizePropertyName = "fontSize";
+      private static readonly string TrueTypeFontPropertyName = "trueTypeFont";
+      private static readonly string BitmapFontPropertyName = "bitmapFont";
       private static readonly string MultiLinePropertyName = "multiLine";
       private static readonly string OverflowMethodPropertyName = "overflowMethod";
       private static readonly string UILabelClassName = "UILabel";
 
-      private Action<object> _reset;
+      private Action<object> _unresize;
+      private Action<object> _unfont;
 
       public TranslationInfo()
       {
@@ -29,6 +35,36 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
       public bool IsCurrentlySettingText { get; set; }
 
+      public void ChangeFont( object graphic )
+      {
+         if( graphic == null ) return;
+
+         if( graphic is Text )
+         {
+            var ui = graphic as Text;
+
+            var previousFont = ui.font;
+            var newFont = FontCache.GetOrCreate( previousFont.fontSize );
+
+            ui.font = newFont;
+            if( _unfont == null )
+            {
+               _unfont = obj =>
+               {
+                  ( (Text)obj ).font = previousFont;
+               };
+            }
+         }
+      }
+
+      public void UnchangeFont( object graphic )
+      {
+         if( graphic == null ) return;
+
+         _unfont?.Invoke( graphic );
+         _unfont = null;
+      }
+
       public void ResizeUI( object graphic )
       {
          // do not resize if there is no object of ir it is already resized
@@ -54,9 +90,9 @@ namespace XUnity.AutoTranslator.Plugin.Core
                ui.horizontalOverflow = HorizontalWrapMode.Wrap;
                ui.verticalOverflow = VerticalWrapMode.Overflow;
 
-               if( _reset == null )
+               if( _unresize == null )
                {
-                  _reset = g =>
+                  _unresize = g =>
                   {
                      var gui = (Text)g;
                      gui.horizontalOverflow = originalHorizontalOverflow;
@@ -78,12 +114,15 @@ namespace XUnity.AutoTranslator.Plugin.Core
                type.GetProperty( MultiLinePropertyName )?.GetSetMethod()?.Invoke( graphic, new object[] { true } );
                type.GetProperty( OverflowMethodPropertyName )?.GetSetMethod()?.Invoke( graphic, new object[] { 0 } );
 
-               _reset = g =>
+               if( _unresize == null )
                {
-                  var gtype = g.GetType();
-                  gtype.GetProperty( MultiLinePropertyName )?.GetSetMethod()?.Invoke( g, new object[] { originalMultiLine } );
-                  gtype.GetProperty( OverflowMethodPropertyName )?.GetSetMethod()?.Invoke( g, new object[] { originalOverflowMethod } );
-               };
+                  _unresize = g =>
+                  {
+                     var gtype = g.GetType();
+                     gtype.GetProperty( MultiLinePropertyName )?.GetSetMethod()?.Invoke( g, new object[] { originalMultiLine } );
+                     gtype.GetProperty( OverflowMethodPropertyName )?.GetSetMethod()?.Invoke( g, new object[] { originalOverflowMethod } );
+                  };
+               }
             }
          }
       }
@@ -92,8 +131,8 @@ namespace XUnity.AutoTranslator.Plugin.Core
       {
          if( graphic == null ) return;
 
-         _reset?.Invoke( graphic );
-         _reset = null;
+         _unresize?.Invoke( graphic );
+         _unresize = null;
       }
 
       public TranslationInfo Reset( string newText )