Skip to content

Commit

Permalink
clean up the names.
Browse files Browse the repository at this point in the history
  • Loading branch information
wutipong committed Jan 21, 2024
1 parent fb2d02b commit 4ad1f97
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 58 deletions.
87 changes: 43 additions & 44 deletions font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

namespace {

const std::map<VariantAxis, hb_tag_t> axisTagMap{
{VariantAxis::Italic, HB_OT_TAG_VAR_AXIS_ITALIC},
{VariantAxis::OpticalSize, HB_OT_TAG_VAR_AXIS_OPTICAL_SIZE},
{VariantAxis::Slant, HB_OT_TAG_VAR_AXIS_SLANT},
{VariantAxis::Weight, HB_OT_TAG_VAR_AXIS_WEIGHT},
{VariantAxis::Width, HB_OT_TAG_VAR_AXIS_WIDTH},
const std::map<VariationAxis, hb_tag_t> axisTagMap{
{VariationAxis::Italic, HB_OT_TAG_VAR_AXIS_ITALIC},
{VariationAxis::OpticalSize, HB_OT_TAG_VAR_AXIS_OPTICAL_SIZE},
{VariationAxis::Slant, HB_OT_TAG_VAR_AXIS_SLANT},
{VariationAxis::Weight, HB_OT_TAG_VAR_AXIS_WEIGHT},
{VariationAxis::Width, HB_OT_TAG_VAR_AXIS_WIDTH},
};
} // namespace

Expand Down Expand Up @@ -107,6 +107,32 @@ bool Font::Initialize() {
family = face->family_name;
subFamily = face->style_name;

if (IsVariableFont()) {
auto hb_face = hb_font_get_face(hb_font);
auto count = hb_ot_var_get_axis_count(hb_face);

std::vector<hb_ot_var_axis_info_t> hbAxisInfo;
hbAxisInfo.resize(count);

hb_ot_var_get_axis_infos(hb_face, 0, &count, hbAxisInfo.data());

for (auto &info : hbAxisInfo) {
auto it = std::ranges::find_if(
axisTagMap,
[&info](const hb_tag_t &t) -> bool { return info.tag == t; },
[](const auto &e) -> auto { return e.second; });

if (it != axisTagMap.end()) {
auto tag = it->first;
axisInfo[tag] = {
.min = info.min_value,
.max = info.max_value,
.defaultValue = info.default_value,
};
}
}
}

return true;
}

Expand Down Expand Up @@ -224,65 +250,38 @@ bool Font::IsVariableFont() const {
return hb_ot_var_has_data(hb_face);
}

magic_enum::containers::array<VariantAxis, std::optional<VariantAxisLimit>>
Font::GetVariantAxisLimits() const {
magic_enum::containers::array<VariationAxis, std::optional<AxisInfo>>
Font::GetAxisInfos() const {
if (!IsVariableFont()) {
return {};
}

magic_enum::containers::array<VariantAxis, std::optional<VariantAxisLimit>>
output{};

auto hb_face = hb_font_get_face(hb_font);
auto count = hb_ot_var_get_axis_count(hb_face);

std::vector<hb_ot_var_axis_info_t> hbAxisInfo;
hbAxisInfo.resize(count);

hb_ot_var_get_axis_infos(hb_face, 0, &count, hbAxisInfo.data());

for (auto &info : hbAxisInfo) {
auto it = std::ranges::find_if(
axisTagMap,
[&info](const hb_tag_t &t) -> bool { return info.tag == t; },
[](const auto &e) -> auto { return e.second; });

if (it != axisTagMap.end()) {
auto tag = it->first;
output[tag] = {
.min = info.min_value,
.max = info.max_value,
.defaultValue = info.default_value,
};
}
}

return output;
return axisInfo;
}

void Font::SetVariant(
const magic_enum::containers::array<VariantAxis, float> &variant) {
void Font::SetVariationValues(
const magic_enum::containers::array<VariationAxis, float> &values) {

if (!IsValid())
return;

auto limits = GetVariantAxisLimits();
auto limits = GetAxisInfos();
std::vector<hb_variation_t> variations;

FT_MM_Var *amaster;
FT_Get_MM_Var(face, &amaster);

magic_enum::enum_for_each<VariantAxis>(
[&limits, &variations, &variant](const VariantAxis &axis) {
magic_enum::enum_for_each<VariationAxis>(
[&limits, &variations, &values](const VariationAxis &axis) {
if (limits[axis].has_value()) {
variations.push_back({
.tag = axisTagMap.at(axis),
.value = variant[axis],
.value = values[axis],
});
}
});

// hb_font_set_variations(hb_font, variations.data(), variations.size());
hb_font_set_variations(hb_font, variations.data(), variations.size());

std::vector<FT_Fixed> coords;
for (int i = 0; i < amaster->num_axis; i++) {
Expand All @@ -296,7 +295,7 @@ void Font::SetVariant(
if (it != axisTagMap.end()) {
auto axis = it->first;

FT_Fixed value = static_cast<FT_Fixed>(variant[axis] * 65'536.0f);
FT_Fixed value = static_cast<FT_Fixed>(values[axis] * 65'536.0f);
coords.push_back(value);
} else {
coords.push_back(amaster->axis[i].def);
Expand Down
14 changes: 9 additions & 5 deletions font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ inline float HBPosToFloat(const hb_position_t &value) {
return static_cast<float>(value) / 64.0f;
}

enum class VariantAxis {
enum class VariationAxis {
Italic,
OpticalSize,
Slant,
Weight,
Width,
};

struct VariantAxisLimit {
struct AxisInfo {
float min;
float max;
float defaultValue;
Expand Down Expand Up @@ -99,10 +99,11 @@ class Font {
const SDL_Color &color, const std::string &language,
const hb_script_t &script);

magic_enum::containers::array<VariantAxis, std::optional<VariantAxisLimit>>
GetVariantAxisLimits() const;
magic_enum::containers::array<VariationAxis, std::optional<AxisInfo>>
GetAxisInfos() const;

void SetVariant(const magic_enum::containers::array<VariantAxis, float>& variant);
void SetVariationValues(
const magic_enum::containers::array<VariationAxis, float> &values);

private:
static FT_Library library;
Expand Down Expand Up @@ -130,4 +131,7 @@ class Font {

TextRenderFunction textRenderer;
TextRenderEnum textRendererEnum{TextRenderEnum::NoShape};

magic_enum::containers::array<VariationAxis, std::optional<AxisInfo>>
axisInfo{};
};
14 changes: 7 additions & 7 deletions main_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ void MainScene::DoUI(Context &context) {
if (ImGui::CollapsingHeader("Parameters", ImGuiTreeNodeFlags_DefaultOpen)) {
ImGui::SliderInt("Font Size", &fontSize, 0, 128);

ImGui::SeparatorText("Variant");
ImGui::SeparatorText("Variations");
ImGui::BeginDisabled(!font.IsVariableFont());

bool axisChanged = false;

static constexpr magic_enum::containers::array<VariantAxis, const char *>
static constexpr magic_enum::containers::array<VariationAxis, const char *>
axisLabel = {{{
"Italic##axis",
"Optical size##axis",
Expand All @@ -175,8 +175,8 @@ void MainScene::DoUI(Context &context) {
"Width##axis",
}}};

magic_enum::enum_for_each<VariantAxis>(
[this, &axisChanged](const VariantAxis &axis) {
magic_enum::enum_for_each<VariationAxis>(
[this, &axisChanged](const VariationAxis &axis) {
if (axisLimits[axis].has_value()) {
auto [min, max, _] = *axisLimits[axis];
axisChanged |= ImGui::DragFloat(axisLabel[axis], &axisValue[axis],
Expand All @@ -187,7 +187,7 @@ void MainScene::DoUI(Context &context) {
});

if (axisChanged) {
font.SetVariant(axisValue);
font.SetVariationValues(axisValue);
}

ImGui::EndDisabled();
Expand Down Expand Up @@ -304,9 +304,9 @@ void MainScene::DoUI(Context &context) {
ImGui::OpenPopup("InvalidFont");
} else {
font = newFont;
axisLimits = font.GetVariantAxisLimits();
axisLimits = font.GetAxisInfos();

magic_enum::enum_for_each<VariantAxis>([this](const VariantAxis &axis) {
magic_enum::enum_for_each<VariationAxis>([this](const VariationAxis &axis) {
if (!axisLimits[axis].has_value())
return;

Expand Down
4 changes: 2 additions & 2 deletions main_scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class MainScene : public Scene {
int selectedDirection = 0;
int selectedLanguage = 0;

magic_enum::containers::array<VariantAxis, float> axisValue;
magic_enum::containers::array<VariantAxis, std::optional<VariantAxisLimit>>
magic_enum::containers::array<VariationAxis, float> axisValue;
magic_enum::containers::array<VariationAxis, std::optional<AxisInfo>>
axisLimits;
};

0 comments on commit 4ad1f97

Please sign in to comment.