< 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:32
Uncovered lines:19
Coverable lines:51
Total lines:150
Line coverage:62.7% (32 of 51)
Covered branches:0
Total branches:0
Covered methods:11
Total methods:14
Method coverage:78.5% (11 of 14)

Metrics

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

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;
 8using Decentraland.Sdk.Ecs6;
 9
 10namespace DCL.Components
 11{
 12    public class DCLFont : BaseDisposable
 13    {
 14        const string RESOURCE_FONT_FOLDER = "Fonts & Materials";
 15
 16        private const string DEFAULT_SANS_SERIF_HEAVY = "Inter-Heavy SDF";
 17        private const string DEFAULT_SANS_SERIF_BOLD = "Inter-Bold SDF";
 18        private const string DEFAULT_SANS_SERIF_SEMIBOLD = "Inter-SemiBold SDF";
 19        private const string DEFAULT_SANS_SERIF = "Inter-Regular SDF";
 20
 321        private readonly Dictionary<string, string> fontsMapping = new ()
 22        {
 23            { "builtin:SF-UI-Text-Regular SDF", DEFAULT_SANS_SERIF },
 24            { "builtin:SF-UI-Text-Heavy SDF", DEFAULT_SANS_SERIF_HEAVY },
 25            { "builtin:SF-UI-Text-Semibold SDF", DEFAULT_SANS_SERIF_SEMIBOLD },
 26            { "builtin:LiberationSans SDF", "LiberationSans SDF" },
 27            { "SansSerif", DEFAULT_SANS_SERIF },
 28            { "SansSerif_Heavy", DEFAULT_SANS_SERIF_HEAVY },
 29            { "SansSerif_Bold", DEFAULT_SANS_SERIF_BOLD },
 30            { "SansSerif_SemiBold", DEFAULT_SANS_SERIF_SEMIBOLD },
 31        };
 32
 33        [System.Serializable]
 34        public class Model : BaseModel
 35        {
 36            public string src;
 37
 38            public override BaseModel GetDataFromJSON(string json) =>
 339                Utils.SafeFromJson<Model>(json);
 40
 41            public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel)
 42            {
 043                if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.Font)
 044                    return Utils.SafeUnimplemented<DCLFont, Model>(expected: ComponentBodyPayload.PayloadOneofCase.Font,
 45
 046                var pb = new Model();
 047                if (pbModel.Font.HasSrc) pb.src = pbModel.Font.Src;
 48
 049                return pb;
 50            }
 51        }
 52
 453        public bool loaded { private set; get; }
 954        public bool error { private set; get; }
 55
 856        public TMP_FontAsset fontAsset { private set; get; }
 57
 358        public DCLFont()
 59        {
 360            model = new Model();
 361        }
 62
 63        public override int GetClassId() =>
 064            (int) CLASS_ID.FONT;
 65
 66        public static bool IsFontLoaded(IParcelScene scene, string componentId)
 67        {
 3568            if ( string.IsNullOrEmpty(componentId))
 3269                return true;
 70
 371            if (!scene.componentsManagerLegacy.HasSceneSharedComponent(componentId))
 72            {
 073                Debug.Log($"couldn't fetch font, the DCLFont component with id {componentId} doesn't exist");
 074                return false;
 75            }
 76
 377            DCLFont fontComponent = scene.componentsManagerLegacy.GetSceneSharedComponent(componentId) as DCLFont;
 78
 379            if (fontComponent == null)
 80            {
 081                Debug.Log($"couldn't fetch font, the shared component with id {componentId} is NOT a DCLFont");
 082                return false;
 83            }
 84
 385            return true;
 86        }
 87
 88        public static IEnumerator WaitUntilFontIsReady(IParcelScene scene, string componentId)
 89        {
 090            if ( string.IsNullOrEmpty(componentId))
 091                yield break;
 92
 093            DCLFont fontComponent = scene.componentsManagerLegacy.GetSceneSharedComponent(componentId) as DCLFont;
 94
 095            while (!fontComponent.loaded && !fontComponent.error)
 96            {
 097                yield return null;
 98            }
 099        }
 100
 101        public static void SetFontFromComponent(IParcelScene scene, string componentId, TMP_Text text)
 102        {
 35103            if ( string.IsNullOrEmpty(componentId))
 32104                return;
 105
 3106            DCLFont fontComponent = scene.componentsManagerLegacy.GetSceneSharedComponent(componentId) as DCLFont;
 107
 3108            if (!fontComponent.error)
 109            {
 2110                text.font = fontComponent.fontAsset;
 111            }
 3112        }
 113
 114        public override IEnumerator ApplyChanges(BaseModel newModel)
 115        {
 3116            Model model = (Model) newModel;
 117
 3118            if (string.IsNullOrEmpty(model.src))
 119            {
 0120                error = true;
 0121                yield break;
 122            }
 123
 3124            if (fontsMapping.TryGetValue(model.src, out string fontResourceName))
 125            {
 2126                ResourceRequest request = Resources.LoadAsync($"{RESOURCE_FONT_FOLDER}/{fontResourceName}",
 127                    typeof(TMP_FontAsset));
 128
 2129                yield return request;
 130
 2131                if (request.asset != null)
 132                {
 2133                    fontAsset = request.asset as TMP_FontAsset;
 134                }
 135                else
 136                {
 0137                    Debug.Log($"couldn't fetch font from resources {fontResourceName}");
 138                }
 139
 2140                loaded = true;
 2141                error = fontAsset == null;
 2142            }
 143            else
 144            {
 145                // NOTE: only support fonts in resources
 1146                error = true;
 147            }
 3148        }
 149    }
 150}