FontFlow.cpp (4603B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #include "pch.h" 4 #include "FontFlow.h" 5 #include "TableOutput.h" 6 #include <winget/Fonts.h> 7 #include <AppInstallerRuntime.h> 8 9 namespace AppInstaller::CLI::Workflow 10 { 11 using namespace AppInstaller::CLI::Execution; 12 13 namespace 14 { 15 struct InstalledFontFamiliesTableLine 16 { 17 InstalledFontFamiliesTableLine(Utility::LocIndString familyName, int faceCount) 18 : FamilyName(familyName), FaceCount(faceCount) {} 19 20 Utility::LocIndString FamilyName; 21 int FaceCount; 22 }; 23 24 struct InstalledFontFacesTableLine 25 { 26 InstalledFontFacesTableLine(Utility::LocIndString familyName, Utility::LocIndString faceName, Utility::LocIndString faceVersion, std::filesystem::path filePath) 27 : FamilyName(familyName), FaceName(faceName), FaceVersion(faceVersion), FilePath(filePath) {} 28 29 Utility::LocIndString FamilyName; 30 Utility::LocIndString FaceName; 31 Utility::LocIndString FaceVersion; 32 std::filesystem::path FilePath; 33 }; 34 35 void OutputInstalledFontFamiliesTable(Execution::Context& context, const std::vector<InstalledFontFamiliesTableLine>& lines) 36 { 37 Execution::TableOutput<2> table(context.Reporter, { Resource::String::FontFamily, Resource::String::FontFaces }); 38 39 for (auto line : lines) 40 { 41 table.OutputLine({ line.FamilyName, std::to_string(line.FaceCount) }); 42 } 43 44 table.Complete(); 45 } 46 47 void OutputInstalledFontFacesTable(Execution::Context& context, const std::vector<InstalledFontFacesTableLine>& lines) 48 { 49 Execution::TableOutput<4> table(context.Reporter, { Resource::String::FontFamily, Resource::String::FontFace, Resource::String::FontVersion, Resource::String::FontFilePaths }); 50 51 bool anonymizePath = Settings::User().Get<Settings::Setting::AnonymizePathForDisplay>(); 52 53 for (auto line : lines) 54 { 55 if (anonymizePath) 56 { 57 AppInstaller::Runtime::ReplaceProfilePathsWithEnvironmentVariable(line.FilePath); 58 } 59 60 table.OutputLine({ line.FamilyName, line.FaceName, line.FaceVersion, line.FilePath.u8string() }); 61 } 62 63 table.Complete(); 64 } 65 } 66 67 void ReportInstalledFonts(Execution::Context& context) 68 { 69 Fonts::FontCatalog fontCatalog; 70 71 if (context.Args.Contains(Args::Type::Family)) 72 { 73 // TODO: Create custom source and search mechanism for fonts. 74 const auto& familyNameArg = AppInstaller::Utility::ConvertToUTF16(context.Args.GetArg(Args::Type::Family)); 75 const auto& fontFamilies = fontCatalog.GetInstalledFontFamilies(familyNameArg); 76 77 if (fontFamilies.empty()) 78 { 79 context.Reporter.Info() << Resource::String::NoInstalledFontFound << std::endl; 80 return; 81 } 82 83 std::vector<InstalledFontFacesTableLine> lines; 84 85 for (const auto& fontFamily : fontFamilies) 86 { 87 const auto& familyName = Utility::LocIndString(Utility::ConvertToUTF8(fontFamily.Name)); 88 89 for (const auto& fontFace : fontFamily.Faces) 90 { 91 for (const auto& filePath : fontFace.FilePaths) 92 { 93 InstalledFontFacesTableLine line( 94 familyName, 95 Utility::LocIndString(Utility::ToLower(Utility::ConvertToUTF8(fontFace.Name))), 96 Utility::LocIndString(fontFace.Version.ToString()), 97 filePath.u8string()); 98 99 lines.push_back(std::move(line)); 100 } 101 } 102 } 103 104 OutputInstalledFontFacesTable(context, lines); 105 } 106 else 107 { 108 const auto& fontFamilies = fontCatalog.GetInstalledFontFamilies(); 109 std::vector<InstalledFontFamiliesTableLine> lines; 110 111 for (const auto& fontFamily : fontFamilies) 112 { 113 InstalledFontFamiliesTableLine line( 114 Utility::LocIndString(Utility::ConvertToUTF8(fontFamily.Name)), 115 static_cast<int>(fontFamily.Faces.size()) 116 ); 117 118 lines.push_back(std::move(line)); 119 } 120 121 OutputInstalledFontFamiliesTable(context, lines); 122 } 123 } 124 }