KaiClavier

STMRTLSupport.cs

Mar 25th, 2020 (edited)
1,482
0
Never
7
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.25 KB | None | 0 0
  1. //Copyright (c) 2016-2021 Kai Clavier [kaiclavier.com] Do Not Distribute
  2. //Made to work with Arabic Letters Support for Unity by Abdullah Konash [https://github.com/Konash/arabic-support-unity]
  3. using UnityEngine;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using ArabicSupport;
  7. using System.Text; //for stringbuilder
  8. using System.Linq;
  9.  
  10. /*
  11. This will also fix ordering for other RTL languages, not just Arabic!
  12. */
  13.  
  14. [ExecuteInEditMode]
  15. [RequireComponent(typeof(SuperTextMesh))]
  16. [DefaultExecutionOrder(-9999)] //to work on enable, this needs to run before Super Text Mesh.
  17. public class STMRTLSupport : MonoBehaviour
  18. {
  19.  
  20.     public SuperTextMesh superTextMesh;
  21.     [Tooltip("Sending other-language strings through the fixer can cause issues, so this is here so things can be quickly disabled in localized games.")]
  22.     public bool isEnabled = true;
  23.     [Tooltip("Makes sure that tags are formatted properly for RTL.")]
  24.     public bool fixTags = true;
  25.     [Tooltip("Flips curly braces. Temporary until added by Arabic Support script")]
  26.     public bool flipCurlyBraces = true;
  27.     [Tooltip("Flips specified punctuation on numbers.")]
  28.     public char[] reversePunctuationOnNumbers = new char[]{'.', ',', '!', '?', ')', '('};
  29.     [Tooltip("Helps prevent order of words being out of place by extending spaces between letters before adjusting")]
  30.     public bool extendSpaces = true;
  31.     [Tooltip("Assumes that text is typed up in an RTL format")]
  32.     public bool rtl = true;
  33.  
  34.     public bool showTashkeel = false;
  35.     public bool combineTashkeel = false;
  36.     public bool useHinduNumbers = false;
  37.    
  38.     public static bool disableAll = false;
  39.  
  40.     void Reset()
  41.     {
  42.         superTextMesh = GetComponent<SuperTextMesh>();
  43.     }
  44.  
  45.     void OnEnable()
  46.     {
  47.         superTextMesh.OnPreParse += FixText;
  48.     }
  49.     void OnDisable()
  50.     {
  51.         superTextMesh.OnPreParse -= FixText;
  52.     }
  53.    
  54.     void FixText(STMTextContainer container)
  55.     {
  56.         if(isEnabled && !disableAll)
  57.         {
  58.             if(fixTags)
  59.             {
  60.                 container.text = container.text.Replace("<", " |||<"); //replace with an unusual string
  61.                 container.text = container.text.Replace(">", ">||| "); //all thats really needed is that space, so order stays
  62.  
  63.                
  64.             }
  65.            
  66.             if(flipCurlyBraces)
  67.             {
  68.                 //flip curly braces, since arabic support script doesn't handle this yet
  69.                 //remove this code when curly braces are added to list in Arabic Support
  70.                 container.text = container.text.Replace("{", "|||{|||");
  71.                 container.text = container.text.Replace("}", "{");
  72.                 container.text = container.text.Replace("|||{|||", "}");
  73.             }
  74.  
  75.             for(int i=0; i<reversePunctuationOnNumbers.Length; i++)
  76.             {
  77.                 for(int j=0; j<10; j++) //this makes SURE it only happens on numbers, so arabic letters don't render wrong.
  78.                 {
  79.                     //Also for now, this tricks punctuation into being placed on the right side of numbers.
  80.                     container.text = container.text.Replace(j.ToString() + reversePunctuationOnNumbers[i].ToString(), j.ToString() + "ששש" + reversePunctuationOnNumbers[i].ToString());
  81.                 }
  82.             }
  83.  
  84.             if(extendSpaces)
  85.             {
  86.                 container.text = container.text.Replace(" ", "  |  ");
  87.             }
  88.  
  89.             if(rtl)
  90.             {
  91.                 List<string> paragraphs = container.text.Split('\n').ToList();
  92.  
  93.                 for(int i=0; i<paragraphs.Count; i++)
  94.                 {
  95.                     paragraphs[i] = string.Join(" ", paragraphs[i].Split(new char[]{' '}).Reverse().ToArray());
  96.                 }
  97.  
  98.                 container.text = string.Join("\n", paragraphs.ToArray());
  99.             }
  100.  
  101.             string fixedString = ArabicFixer.Fix(container.text, showTashkeel, combineTashkeel, useHinduNumbers);
  102.  
  103.             if(extendSpaces)
  104.             {
  105.                 fixedString = fixedString.Replace("  |  ", " ");
  106.             }
  107.             for(int i=0; i<reversePunctuationOnNumbers.Length; i++)
  108.             {
  109.                 //for(int j=0; j<10; j++)
  110.                 //{
  111.                     //Also for now, this tricks punctuation into being placed on the right side of numbers.
  112.                     fixedString = fixedString.Replace(reversePunctuationOnNumbers[i].ToString() + "ששש", reversePunctuationOnNumbers[i].ToString());
  113.                 //}
  114.             }
  115.             if(fixTags)
  116.             {
  117.                 fixedString = fixedString.Replace(".ששש", ".");
  118.  
  119.                 fixedString = fixedString.Replace("<||| ", ">");
  120.                 fixedString = fixedString.Replace(" |||>", "<");
  121.  
  122.                 //also...
  123.                 fixedString = fixedString.Replace("<|||", "<");
  124.                 fixedString = fixedString.Replace("|||>", ">");
  125.             }
  126.            
  127.             container.text = fixedString;
  128.         }
  129.     }
  130. }
  131.  
Advertisement
Comments
  • RahimDev
    1 year
    # text 0.17 KB | 0 0
    1. Hello, its great but when i keep enabling and disabling the STMs gameobjects ... the RTLSTM doesnt work probably on all texts in the scene ! did you face that problem ?
    2.  
    • KaiClavier
      1 year
      # text 0.21 KB | 1 0
      1. Hm, On Enable & Disable, this code will subscribe to the "OnPreParse" event STM has, so it will be called when text rebuilds. Does anything different happen if you call superTextMesh.Rebuild() when enabling the object?
      • RahimDev
        1 year
        # text 0.10 KB | 0 0
        1. thanks for the fast response, thought it will take weeks, i tried ReBuild() still the same problem
        • KaiClavier
          1 year
          # text 0.30 KB | 1 0
          1. Ok, I think I fixed it, but let me know if it works for your use-case. I added the line "[DefaultExecutionOrder(-9999)]" to the start of the class to make sure that this code's OnEnable() call runs before Super Text Mesh's. Hopefully that fixes it, but please let me know if you're still seeing the issue!
          • RahimDev
            1 year
            # text 0.16 KB | 0 0
            1. now its perfect, it works 🥳🎉! it even solve double flipping RTL words when i change languages! (that issue was happen rarely but now its gone)
            2. Thank you
  • RahimDev
    1 year
    Comment was deleted
    • KaiClavier
      1 year
      # text 0.47 KB | 1 0
      1. Hm, I think this may be the "rtl" option under the "beta" tab in Super Text Mesh, rather than the "rtl" option in this script? Try unchecking (or checking?) it for a different result! It's been a while since I looked into it, but it changes how the text is interpreted, so hopefully the right configuration is there. Keep in mind, any changes made to this script won't change until Super Text Mesh rebuilds, so make sure to refresh some setting on Super Text Mesh to see the change.
Add Comment
Please, Sign In to add comment