srctree

Robin Linden parent 271beeba e7181ac3
style: Remove the to_px-helper

It's been completely replaced by UnresolvedValue now.
style/styled_node.cpp added: 15, removed: 33, total 0
@@ -721,10 +721,12 @@ std::optional<WhiteSpace> StyledNode::get_white_space_property() const {
std::pair<int, int> StyledNode::get_border_radius_property(css::PropertyId id) const {
auto raw = get_raw_property(id);
auto [horizontal, vertical] = raw.contains('/') ? util::split_once(raw, "/") : std::pair{raw, raw};
auto horizontal_prop = UnresolvedValue{horizontal};
auto vertical_prop = UnresolvedValue{vertical};
 
int font_size = get_property<css::PropertyId::FontSize>();
int root_font_size = get_root_font_size(*this);
return {to_px(horizontal, font_size, root_font_size), to_px(vertical, font_size, root_font_size)};
return {horizontal_prop.resolve(font_size, root_font_size), vertical_prop.resolve(font_size, root_font_size)};
}
 
} // namespace style
 
style/unresolved_value.cpp added: 15, removed: 33, total 0
@@ -16,40 +16,33 @@
namespace style {
 
int UnresolvedValue::resolve(int font_size, int root_font_size, std::optional<int> percent_relative_to) const {
return to_px(raw, font_size, root_font_size, percent_relative_to);
return try_resolve(font_size, root_font_size, percent_relative_to).value_or(0);
}
 
std::optional<int> UnresolvedValue::try_resolve(
int font_size, int root_font_size, std::optional<int> percent_relative_to) const {
return try_to_px(raw, font_size, root_font_size, percent_relative_to);
}
 
std::optional<int> try_to_px(std::string_view property,
int const font_size,
int const root_font_size,
std::optional<int> parent_property_value) {
// Special case for 0 since it won't ever have a unit that needs to be handled.
if (property == "0") {
if (raw == "0") {
return 0;
}
 
float res{};
auto parse_result = util::from_chars(property.data(), property.data() + property.size(), res);
auto parse_result = util::from_chars(raw.data(), raw.data() + raw.size(), res);
if (parse_result.ec != std::errc{}) {
spdlog::warn("Unable to parse property '{}' in to_px", property);
spdlog::warn("Unable to parse property '{}' in to_px", raw);
return std::nullopt;
}
 
auto const parsed_length = std::distance(property.data(), parse_result.ptr);
auto const unit = property.substr(parsed_length);
auto const parsed_length = std::distance(raw.data(), parse_result.ptr);
auto const unit = raw.substr(parsed_length);
 
if (unit == "%") {
if (!parent_property_value.has_value()) {
if (!percent_relative_to.has_value()) {
spdlog::warn("Missing parent-value for property w/ '%' unit");
return std::nullopt;
}
 
return static_cast<int>(res / 100.f * (*parent_property_value));
return static_cast<int>(res / 100.f * (*percent_relative_to));
}
 
if (unit == "px") {
@@ -76,7 +69,7 @@ std::optional<int> try_to_px(std::string_view property,
return static_cast<int>(res * kExToEmRatio * font_size);
}
 
spdlog::warn("Bad property '{}' w/ unit '{}' in to_px", property, unit);
spdlog::warn("Bad property '{}' w/ unit '{}' in to_px", raw, unit);
return std::nullopt;
}
 
 
style/unresolved_value.h added: 15, removed: 33, total 0
@@ -21,19 +21,6 @@ struct UnresolvedValue {
int font_size, int root_font_size, std::optional<int> percent_relative_to = std::nullopt) const;
};
 
// TODO(robinlinden): This should be internal.
std::optional<int> try_to_px(std::string_view property,
int font_size,
int root_font_size,
std::optional<int> parent_property_value = std::nullopt);
 
inline int to_px(std::string_view property,
int font_size,
int root_font_size,
std::optional<int> parent_property_value = std::nullopt) {
return try_to_px(property, font_size, root_font_size, parent_property_value).value_or(0);
}
 
} // namespace style
 
#endif