< Summary

Class:DCL.Components.DCLFont
Assembly:DCL.Components.Font
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Font/DCLFont.cs
Covered lines:28
Uncovered lines:17
Coverable lines:45
Total lines:133
Line coverage:62.2% (28 of 45)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DCLFont()0%110100%
GetDataFromJSON(...)0%110100%
GetClassId()0%2100%
IsFontLoaded(...)0%5.024060%
WaitUntilFontIsReady()0%42600%
SetFontFromComponent(...)0%330100%
ApplyChanges()0%6.246081.25%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Font/DCLFont.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using DCL.Controllers;
 4using DCL.Helpers;
 5using DCL.Models;
 6using TMPro;
 7using UnityEngine;
 8
 9namespace DCL.Components
 10{
 11    public class DCLFont : BaseDisposable
 12    {
 13        const string RESOURCE_FONT_FOLDER = "Fonts & Materials";
 14
 15        private const string DEFAULT_SANS_SERIF_HEAVY = "Inter-Heavy SDF";
 16        private const string DEFAULT_SANS_SERIF_BOLD = "Inter-Bold SDF";
 17        private const string DEFAULT_SANS_SERIF_SEMIBOLD = "Inter-SemiBold SDF";
 18        private const string DEFAULT_SANS_SERIF = "Inter-Regular SDF";
 19
 320        private readonly Dictionary<string, string> fontsMapping = new Dictionary<string, string>()
 21        {
 22            { "builtin:SF-UI-Text-Regular SDF", DEFAULT_SANS_SERIF },
 23            { "builtin:SF-UI-Text-Heavy SDF", DEFAULT_SANS_SERIF_HEAVY },
 24            { "builtin:SF-UI-Text-Semibold SDF", DEFAULT_SANS_SERIF_SEMIBOLD },
 25            { "builtin:LiberationSans SDF", "LiberationSans SDF" },
 26            { "SansSerif", DEFAULT_SANS_SERIF },
 27            { "SansSerif_Heavy", DEFAULT_SANS_SERIF_HEAVY },
 28            { "SansSerif_Bold", DEFAULT_SANS_SERIF_BOLD },
 29            { "SansSerif_SemiBold", DEFAULT_SANS_SERIF_SEMIBOLD },
 30        };
 31
 32        [System.Serializable]
 33        public class Model : BaseModel
 34        {
 35            public string src;
 36
 337            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 38        }
 39
 040        public bool loaded { private set; get; } = false;
 041        public bool error { private set; get; } = false;
 42
 043        public TMP_FontAsset fontAsset { private set; get; }
 44
 945        public DCLFont() { model = new Model(); }
 46
 047        public override int GetClassId() { return (int) CLASS_ID.FONT; }
 48
 49        public static bool IsFontLoaded(IParcelScene scene, string componentId)
 50        {
 3451            if ( string.IsNullOrEmpty(componentId))
 3152                return true;
 53
 354            if (!scene.disposableComponents.ContainsKey(componentId))
 55            {
 056                Debug.Log($"couldn't fetch font, the DCLFont component with id {componentId} doesn't exist");
 057                return false;
 58            }
 59
 360            DCLFont fontComponent = scene.disposableComponents[componentId] as DCLFont;
 61
 362            if (fontComponent == null)
 63            {
 064                Debug.Log($"couldn't fetch font, the shared component with id {componentId} is NOT a DCLFont");
 065                return false;
 66            }
 67
 368            return true;
 69        }
 70
 71        public static IEnumerator WaitUntilFontIsReady(IParcelScene scene, string componentId)
 72        {
 073            if ( string.IsNullOrEmpty(componentId))
 074                yield break;
 75
 076            DCLFont fontComponent = scene.disposableComponents[componentId] as DCLFont;
 77
 078            while (!fontComponent.loaded && !fontComponent.error)
 79            {
 080                yield return null;
 81            }
 082        }
 83
 84        public static void SetFontFromComponent(IParcelScene scene, string componentId, TMP_Text text)
 85        {
 3486            if ( string.IsNullOrEmpty(componentId))
 3187                return;
 88
 389            DCLFont fontComponent = scene.disposableComponents[componentId] as DCLFont;
 90
 391            if (!fontComponent.error)
 92            {
 293                text.font = fontComponent.fontAsset;
 94            }
 395        }
 96
 97        public override IEnumerator ApplyChanges(BaseModel newModel)
 98        {
 399            Model model = (Model) newModel;
 100
 3101            if (string.IsNullOrEmpty(model.src))
 102            {
 0103                error = true;
 0104                yield break;
 105            }
 106
 3107            if (fontsMapping.TryGetValue(model.src, out string fontResourceName))
 108            {
 2109                ResourceRequest request = Resources.LoadAsync($"{RESOURCE_FONT_FOLDER}/{fontResourceName}",
 110                    typeof(TMP_FontAsset));
 111
 2112                yield return request;
 113
 2114                if (request.asset != null)
 115                {
 2116                    fontAsset = request.asset as TMP_FontAsset;
 2117                }
 118                else
 119                {
 0120                    Debug.Log($"couldn't fetch font from resources {fontResourceName}");
 121                }
 122
 2123                loaded = true;
 2124                error = fontAsset == null;
 2125            }
 126            else
 127            {
 128                // NOTE: only support fonts in resources
 1129                error = true;
 130            }
 3131        }
 132    }
 133}