Skip to content

Commit

Permalink
Support font color customization for toSvg
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardmgruber committed Feb 13, 2024
1 parent cf20b6a commit 9fa0ee7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
15 changes: 10 additions & 5 deletions include/llama/DumpMapping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# include <functional>
# include <optional>
# include <string>
# include <string_view>
# include <vector>

namespace llama
Expand Down Expand Up @@ -250,7 +251,8 @@ namespace llama
const Mapping& mapping,
std::size_t wrapByteCount = 64,
bool breakBoxes = true,
const std::vector<std::uint32_t>& palette = {}) -> std::string
const std::vector<std::uint32_t>& palette = {},
std::string_view textColor = "black") -> std::string
{
constexpr auto byteSizeInPixel = 30;
constexpr auto blobBlockWidth = 60;
Expand All @@ -276,13 +278,14 @@ namespace llama
const auto height = blobRows * byteSizeInPixel;
svg += fmt::format(
R"a(<rect x="0" y="{}" width="{}" height="{}" fill="#AAA" stroke="#000"/>
<text x="{}" y="{}" fill="#000" text-anchor="middle">{}</text>
<text x="{}" y="{}" fill="{}" text-anchor="middle">{}</text>
)a",
blobYOffset[i],
blobBlockWidth,
height,
blobBlockWidth / 2,
blobYOffset[i] + height / 2,
textColor,
name);
};
for(std::size_t i = 0; i < Mapping::blobCount; i++)
Expand Down Expand Up @@ -377,10 +380,11 @@ namespace llama
y + byteSizeInPixel);
}
svg += fmt::format(
R"(<text x="{}" y="{}" fill="#000" text-anchor="middle" class="label">{} {}</text>
R"(<text x="{}" y="{}" fill="{}" text-anchor="middle" class="label">{} {}</text>
)",
x + width / 2,
y + byteSizeInPixel * 3 / 4,
textColor,
internal::formatArrayIndex(info.arrayIndex),
internal::xmlEscape(std::string{info.recordCoordTags}));
if(cropBoxes)
Expand Down Expand Up @@ -411,9 +415,10 @@ namespace llama

LLAMA_EXPORT
template<typename Mapping>
auto toSvg(const Mapping& mapping, const std::vector<std::uint32_t>& palette) -> std::string
auto toSvg(const Mapping& mapping, const std::vector<std::uint32_t>& palette, std::string_view textColor = "#000")
-> std::string
{
return toSvg(mapping, 64, true, palette);
return toSvg(mapping, 64, true, palette, textColor);
}

/// Returns an HTML document visualizing the memory layout created by the given mapping. The visualization is
Expand Down
7 changes: 4 additions & 3 deletions tests/dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ namespace
using ArrayExtents = decltype(extents);

template<typename Mapping>
void dump(const Mapping& mapping, const std::vector<std::uint32_t>& palette = {})
void dump(const Mapping& mapping, const std::vector<std::uint32_t>& palette = {}, std::string_view font = "black")
{
const auto outputDir = std::string{"dump"};
std::filesystem::create_directory(outputDir);
// undocumented Catch feature, see: https://github.com/catchorg/Catch2/issues/510
const auto filename = outputDir + "/" + Catch::getResultCapture().getCurrentTestName();
std::ofstream{filename + ".svg"} << llama::toSvg(mapping, palette);
std::ofstream{filename + ".svg"} << llama::toSvg(mapping, palette, font);
std::ofstream{filename + ".html"} << llama::toHtml(mapping);
}
} // namespace
Expand Down Expand Up @@ -410,7 +410,8 @@ TEST_CASE("dump.AdePT.track.palette")
{
dump(
llama::mapping::AoS<llama::ArrayExtents<int, 8>, Track>{{}},
{0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF, 0xFFFFFF});
{0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF, 0xFFFFFF},
"white");
}

TEST_CASE("dump.LHCb.Custom4")
Expand Down

0 comments on commit 9fa0ee7

Please sign in to comment.