|
| 1 | +// all rights reserved |
| 2 | + |
| 3 | +#include "DataAsset_FontTags.h" |
| 4 | + |
| 5 | +TArray<FUnicodeCharacterTags>& UDataAsset_FontTags::GetCharactersMerged() const |
| 6 | +{ |
| 7 | + if (CharactersMerged.IsEmpty()) |
| 8 | + { |
| 9 | + CharactersMerged = Characters; |
| 10 | + // create the codepoint cache for quicker lookup of existing entries |
| 11 | + CacheCodepoints(); |
| 12 | + |
| 13 | + if (Parent && Parent != this) // there's a chance of infinite recursion here, this still doesn't catch recursion traps like A => B => A |
| 14 | + { |
| 15 | + for (FUnicodeCharacterTags& ParentCharacter : Parent->GetCharactersMerged()) |
| 16 | + { |
| 17 | + // character exists, append tags |
| 18 | + if (int32 const* CharacterIndex = CodepointLookup.Find(ParentCharacter.Character)) |
| 19 | + { |
| 20 | + for (FString& ParentCharacterTag : ParentCharacter.Tags) |
| 21 | + { |
| 22 | + CharactersMerged[*CharacterIndex].Tags.AddUnique(ParentCharacterTag); |
| 23 | + } |
| 24 | + } |
| 25 | + // character doesn't exist, add it |
| 26 | + else |
| 27 | + { |
| 28 | + CharactersMerged.Add(ParentCharacter); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // recreate the codepoint cache if the parent has any characters |
| 33 | + if (Parent->GetCharactersMerged().Num() > 0) |
| 34 | + { |
| 35 | + CacheCodepoints(); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + return CharactersMerged; |
| 40 | +} |
| 41 | + |
| 42 | +TArray<int32> UDataAsset_FontTags::GetCharactersByNeedle(FString NeedleIn) const |
| 43 | +{ |
| 44 | + TArray<int32> Result; |
| 45 | + TArray<FString> Needles; |
| 46 | + |
| 47 | + // explode only if the length is >1 since "," is a valid character search term |
| 48 | + if (NeedleIn.Len() > 1) |
| 49 | + { |
| 50 | + NeedleIn.ParseIntoArray(Needles, TEXT(",")); |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + Needles = {NeedleIn}; |
| 55 | + } |
| 56 | + |
| 57 | + // trim, as the user may type stuff like "Phone, Calculator" |
| 58 | + for (FString& Needle : Needles) |
| 59 | + { |
| 60 | + Needle.TrimStartAndEndInline(); |
| 61 | + } |
| 62 | + |
| 63 | + Result.Reserve(GetCharactersMerged().Num()); |
| 64 | + for (FUnicodeCharacterTags const& Entry : GetCharactersMerged()) |
| 65 | + { |
| 66 | + for (FString const& Needle : Needles) |
| 67 | + { |
| 68 | + if (Entry.ContainsNeedle(Needle)) |
| 69 | + { |
| 70 | + Result.Add(Entry.Character); |
| 71 | + break; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return Result; |
| 77 | +} |
| 78 | + |
| 79 | +bool UDataAsset_FontTags::SupportsFont(FSlateFontInfo const& FontInfo) const |
| 80 | +{ |
| 81 | + // allow presets to apply to all fonts if they don't contain any specific fonts |
| 82 | + return Fonts.IsEmpty() || Fonts.Contains(Cast<UFont>(FontInfo.FontObject)); |
| 83 | +} |
| 84 | + |
| 85 | +void UDataAsset_FontTags::CacheCodepoints() const |
| 86 | +{ |
| 87 | + CodepointLookup.Reset(); |
| 88 | + CodepointLookup.Reserve(CharactersMerged.Num()); |
| 89 | + |
| 90 | + for (int Idx = 0; Idx < CharactersMerged.Num(); Idx++) |
| 91 | + { |
| 92 | + CodepointLookup.Add(CharactersMerged[Idx].Character, Idx); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +TArray<FString> UDataAsset_FontTags::GetCodepointTags(int32 Codepoint) const |
| 97 | +{ |
| 98 | + if (CodepointLookup.IsEmpty()) |
| 99 | + { |
| 100 | + // this creates the cache |
| 101 | + // ReSharper disable once CppExpressionWithoutSideEffects |
| 102 | + GetCharactersMerged(); |
| 103 | + } |
| 104 | + |
| 105 | + if (int32 const* Index = CodepointLookup.Find(Codepoint)) |
| 106 | + { |
| 107 | + return GetCharactersMerged()[*Index].Tags; |
| 108 | + } |
| 109 | + |
| 110 | + return {}; |
| 111 | +} |
| 112 | + |
| 113 | +bool UDataAsset_FontTags::ImportFromJson(FString Filename) |
| 114 | +{ |
| 115 | + FString JsonString; |
| 116 | + if (!FFileHelper::LoadFileToString(JsonString, *Filename)) |
| 117 | + return false; |
| 118 | + |
| 119 | + SourceFile = Filename; |
| 120 | + |
| 121 | + FString CodePointFieldDecimal = ""; |
| 122 | + FString CodePointFieldHexadecimal = ""; |
| 123 | + TArray<FString> TagFields; |
| 124 | + |
| 125 | + TSharedPtr<FJsonObject> JsonObject = MakeShared<FJsonObject>(); |
| 126 | + const auto Reader = TJsonReaderFactory<>::Create(JsonString); |
| 127 | + FJsonSerializer::Deserialize(Reader, JsonObject); |
| 128 | + |
| 129 | + if (!JsonObject->HasTypedField<EJson::Array>(TEXT("tagFields"))) |
| 130 | + { |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + JsonObject->TryGetStringField(TEXT("codepointFieldDecimal"), CodePointFieldDecimal); |
| 135 | + JsonObject->TryGetStringField(TEXT("codepointFieldHexadecimal"), CodePointFieldHexadecimal); |
| 136 | + |
| 137 | + if (CodePointFieldDecimal.IsEmpty() && CodePointFieldHexadecimal.IsEmpty()) |
| 138 | + return false; |
| 139 | + |
| 140 | + JsonObject->TryGetStringArrayField(TEXT("tagFields"), TagFields); |
| 141 | + if (TagFields.IsEmpty()) |
| 142 | + return false; |
| 143 | + |
| 144 | + if (!JsonObject->HasTypedField<EJson::Array>(TEXT("glyphs"))) |
| 145 | + return false; |
| 146 | + |
| 147 | + TArray<TSharedPtr<FJsonValue>> Glyphs = JsonObject->GetArrayField(TEXT("glyphs")); |
| 148 | + if (Glyphs.IsEmpty()) |
| 149 | + return false; |
| 150 | + |
| 151 | + for (TSharedPtr<FJsonValue>& GlyphValue : Glyphs) |
| 152 | + { |
| 153 | + TSharedPtr<FJsonObject> Glyph = GlyphValue->AsObject(); |
| 154 | + FUnicodeCharacterTags Data; |
| 155 | + |
| 156 | + if (CodePointFieldDecimal.Len() > 0) |
| 157 | + { |
| 158 | + if (Glyph->HasTypedField<EJson::Number>(*CodePointFieldDecimal)) |
| 159 | + { |
| 160 | + Data.Character = Glyph->GetNumberField(*CodePointFieldDecimal); |
| 161 | + } |
| 162 | + else if (Glyph->HasTypedField<EJson::String>(*CodePointFieldDecimal)) |
| 163 | + { |
| 164 | + FString DecimalString = Glyph->GetStringField(*CodePointFieldDecimal); |
| 165 | + Data.Character = FCString::Strtoi(*DecimalString, nullptr, 10); |
| 166 | + } |
| 167 | + } |
| 168 | + else if (CodePointFieldHexadecimal.Len() > 0) |
| 169 | + { |
| 170 | + if (!Glyph->HasTypedField<EJson::String>(*CodePointFieldHexadecimal)) |
| 171 | + continue; |
| 172 | + |
| 173 | + FString HexString = Glyph->GetStringField(*CodePointFieldHexadecimal); |
| 174 | + if (!HexString.StartsWith("0x", ESearchCase::IgnoreCase)) |
| 175 | + continue; |
| 176 | + |
| 177 | + Data.Character = FCString::Strtoi(*HexString, nullptr, 16); |
| 178 | + } |
| 179 | + |
| 180 | + for (FString& TagField : TagFields) |
| 181 | + { |
| 182 | + FString Tag = ""; |
| 183 | + Glyph->TryGetStringField(*TagField, Tag); |
| 184 | + |
| 185 | + if (Tag.Len() > 0) |
| 186 | + { |
| 187 | + Data.Tags.Add(Tag); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + Characters.Add(Data); |
| 192 | + } |
| 193 | + |
| 194 | + return true; |
| 195 | +} |
0 commit comments