Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ namespace mfl
{
if (std::holds_alternative<glue_spec>(n))
{
const auto& glue = std::get<glue_spec>(n);
if (glue_scale == 0.0) return glue.size;
const auto& [size, stretch, shrink] = std::get<glue_spec>(n);
if (glue_scale == 0.0) return size;

const auto w = (glue_scale < 0.0) ? glue.shrink.value : glue.stretch.value;
const auto w = (glue_scale < 0.0) ? shrink.value : stretch.value;
return static_cast<dist_t>(glue_scale * static_cast<double>(w));
}

Expand Down
6 changes: 3 additions & 3 deletions src/noad/math_char.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ namespace mfl
if (variants.empty()) return face.glyph_index_from_code_point(char_code, false);

auto result = variants.front().glyph_index;
for (const auto v : variants)
for (const auto [glyph_index, size] : variants)
{
if (v.size > requested_size) return result;
if (size > requested_size) return result;

result = v.glyph_index;
result = glyph_index;
}

return result;
Expand Down
19 changes: 9 additions & 10 deletions src/noad/noad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,20 @@ namespace mfl
if (!iterms.empty())
{
const auto& term = iterms.front();
auto tail = iterms.subspan(1);
item_kind kind = item_kind::none;
const auto tail = iterms.subspan(1);
auto kind = item_kind::none;
if (std::holds_alternative<inoad>(term))
{
const auto& n = std::get<inoad>(term);
kind = change_kind(prev_kind, n.kind, tail);
const auto space = make_space(s, math_spacing(prev_kind, n.kind));
result.nodes.insert(result.nodes.end(), space.nodes.begin(), space.nodes.end());
result.nodes.insert(result.nodes.end(), n.translated_noad.nodes.begin(),
n.translated_noad.nodes.end());
const auto& [translated_noad, inoad_kind] = std::get<inoad>(term);
kind = change_kind(prev_kind, inoad_kind, tail);
const auto [space_nodes] = make_space(s, math_spacing(prev_kind, inoad_kind));
result.nodes.insert(result.nodes.end(), space_nodes.begin(), space_nodes.end());
result.nodes.insert(result.nodes.end(), translated_noad.nodes.begin(), translated_noad.nodes.end());
}
else if (std::holds_alternative<ispace>(term))
{
const auto space = make_space(s, std::get<ispace>(term));
result.nodes.insert(result.nodes.end(), space.nodes.begin(), space.nodes.end());
const auto [space_nodes] = make_space(s, std::get<ispace>(term));
result.nodes.insert(result.nodes.end(), space_nodes.begin(), space_nodes.end());
}

intermediate_terms_to_hlist(s, has_penalties, kind, tail, result);
Expand Down
2 changes: 1 addition & 1 deletion src/noad/radical.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace mfl
auto radical_symbol = make_auto_height_glyph(s, font_family::roman, radical_char_code, requested_height).first;
const auto shift =
height(radical_symbol) - (content_box.dims.height + vertical_gap + radical_rule_thickness(s));
constexpr auto percent_divisor = dist_t(100);
constexpr auto percent_divisor = dist_t{100};
degree_box.shift = -degree_box.dims.depth
- (height(radical_symbol) * radical_degree_bottom_raise_percent(s)) / percent_divisor;
auto radical_box = make_hbox({.nodes = {kern{.size = radical_kern_before_degree(s)}, std::move(degree_box),
Expand Down
10 changes: 5 additions & 5 deletions src/node/box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ namespace mfl

hlist hlist_with_unit_glue(box&& box)
{
const auto unit_glue_spec = glue_spec{.size = 0,
.stretch = {.value = unit_distance, .order = infinity_order::fil},
.shrink = {.value = unit_distance, .order = infinity_order::fil}};
constexpr auto unit_glue_spec = glue_spec{.size = 0,
.stretch = {.value = unit_distance, .order = infinity_order::fil},
.shrink = {.value = unit_distance, .order = infinity_order::fil}};
return make_hlist(unit_glue_spec, std::move(box), unit_glue_spec);
}
}
Expand Down Expand Up @@ -60,7 +60,7 @@ namespace mfl

box make_vbox(const dist_t width, node_variant&& ref_node, vlist&& up_list, vlist&& down_list)
{
auto shift = dist_t(0);
auto shift = dist_t{0};
if (std::holds_alternative<wrapped_box>(ref_node))
{
auto& b = static_cast<box&>(std::get<wrapped_box>(ref_node));
Expand All @@ -75,7 +75,7 @@ namespace mfl
nodes.reserve(up_list.nodes.size() + 1 + down_list.nodes.size());
std::move(up_list.nodes.rbegin(), up_list.nodes.rend(), std::back_inserter(nodes));
nodes.emplace_back(std::move(ref_node));
std::move(down_list.nodes.begin(), down_list.nodes.end(), std::back_inserter(nodes));
std::ranges::move(down_list.nodes, std::back_inserter(nodes));

return box{.kind = box_kind::vbox, .dims = dims, .shift = shift, .nodes = std::move(nodes)};
}
Expand Down
12 changes: 6 additions & 6 deletions src/node/glue.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// if the width_diff is positive
// find highest order for which the sum of all stretch glues in the node list is non-zero
// find the highest order for which the sum of all stretch glues in the node list is non-zero
// return the sum and the corresponding order
// result is glue_param{.scale = width_diff / sum, .order = order}
//
Expand All @@ -20,8 +20,8 @@ namespace mfl
for (const auto& n : nodes)
{
std::visit(overload{[&](const glue_spec& glue) {
const auto& s = glue.*scale;
if (s.order == order) glue_sum += s.value;
if (const auto& [value, scale_order] = glue.*scale; scale_order == order)
glue_sum += value;
},
[](const auto&) {}},
n);
Expand All @@ -35,11 +35,11 @@ namespace mfl
using enum infinity_order;
for (const auto order : {filll, fill, fil, normal})
{
const auto glue_sum = sum_of_glue_by_order(order, scale, nodes);
if (glue_sum != 0) return std::pair(order, glue_sum);
if (const auto glue_sum = sum_of_glue_by_order(order, scale, nodes); glue_sum != 0)
return std::pair(order, glue_sum);
}

return std::pair(infinity_order::normal, dist_t(0));
return std::pair(normal, dist_t{0});
}

glue_param calculate_glue_param(const dist_t width_diff, const std::vector<node_variant>& nodes,
Expand Down
8 changes: 4 additions & 4 deletions src/node/hlist.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "node/hlist.hpp"

#include <algorithm>
#include <numeric>
#include <ranges>

namespace mfl
Expand All @@ -16,14 +15,15 @@ namespace mfl
hlist concat(hlist&& l0, hlist&& l1)
{
auto result = std::move(l0);
std::move(l1.nodes.begin(), l1.nodes.end(), std::back_inserter(result.nodes));
std::ranges::move(l1.nodes, std::back_inserter(result.nodes));
return result;
}

dist_t hlist_width(const hlist& l)
{
return std::accumulate(l.nodes.begin(), l.nodes.end(), dist_t(0),
[&](const dist_t acc, const node_variant& n) { return acc + width(n); });
return std::ranges::fold_left_first(
l.nodes | std::views::transform([](const node_variant& n) { return width(n); }), std::plus{})
.value_or({});
}

dist_t hlist_depth(const hlist& l)
Expand Down
12 changes: 6 additions & 6 deletions src/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ namespace mfl
dist_t depth(const node_variant& n)
{
return std::visit(overload{[](const box& b) { return b.dims.depth + b.shift; },
[](const glue_spec&) { return dist_t(0); }, [](const glyph& g) { return g.depth; },
[](const kern&) { return dist_t(0); }, [](const rule& r) { return r.depth; }},
[](const glue_spec&) { return dist_t{0}; }, [](const glyph& g) { return g.depth; },
[](const kern&) { return dist_t{0}; }, [](const rule& r) { return r.depth; }},
n);
}

dist_t height(const node_variant& n)
{
return std::visit(overload{[](const box& b) { return b.dims.height - b.shift; },
[](const glue_spec&) { return dist_t(0); }, [](const glyph& g) { return g.height; },
[](const kern&) { return dist_t(0); }, [](const rule& r) { return r.height; }},
[](const glue_spec&) { return dist_t{0}; }, [](const glyph& g) { return g.height; },
[](const kern&) { return dist_t{0}; }, [](const rule& r) { return r.height; }},
n);
}

Expand All @@ -42,8 +42,8 @@ namespace mfl
dist_t vwidth(const node_variant& n)
{
return std::visit(overload{[](const box& b) { return b.shift + b.dims.width; },
[](const glue_spec&) { return dist_t(0); }, [](const glyph& g) { return g.width; },
[](const kern&) { return dist_t(0); }, [](const rule& r) { return r.width; }},
[](const glue_spec&) { return dist_t{0}; }, [](const glyph& g) { return g.width; },
[](const kern&) { return dist_t{0}; }, [](const rule& r) { return r.width; }},
n);
}

Expand Down
8 changes: 5 additions & 3 deletions src/node/vlist.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "node/vlist.hpp"

#include <numeric>
#include <algorithm>
#include <ranges>

namespace mfl
{
dist_t vlist_size(const vlist& l)
{
return std::accumulate(l.nodes.begin(), l.nodes.end(), dist_t(0),
[&](const dist_t acc, const node_variant& n) { return acc + vsize(n); });
return std::ranges::fold_left_first(
l.nodes | std::views::transform([](const node_variant& n) { return vsize(n); }), std::plus{})
.value_or({});
}
}
4 changes: 2 additions & 2 deletions src/parser/big_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ namespace mfl::parser

if (sup0 && sup1) state.set_error("multiple superscripts");

optional_noads sub = sub0 ? sub0 : sub1;
optional_noads sup = sup0 ? sup0 : sup1;
const optional_noads sub = sub0 ? sub0 : sub1;
const optional_noads sup = sup0 ? sup0 : sup1;
return {.limits = limits,
.nucleus = {math_char{.kind = item_kind::op, .char_code = unicode_index("\\" + op_name, state)}},
.sub = sub,
Expand Down
2 changes: 0 additions & 2 deletions src/parser/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
#include "parser/fraction.hpp"
#include "parser/function.hpp"
#include "parser/left_right.hpp"
#include "parser/lexer.hpp"
#include "parser/line.hpp"
#include "parser/math_char.hpp"
#include "parser/math_space.hpp"
#include "parser/parser_state.hpp"
#include "parser/parser_utilities.hpp"
#include "parser/radical.hpp"

namespace mfl::parser
Expand Down
2 changes: 1 addition & 1 deletion src/parser/font_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace mfl::parser
{
using namespace std::string_view_literals;

const auto font_commands = std::array<std::pair<std::string_view, font_choice>, 9>{{
constexpr auto font_commands = std::array<std::pair<std::string_view, font_choice>, 9>{{
{"mathcal"sv, font_choice::calligraphic},
{"mathnormal"sv, font_choice::normal},
{"mathbf"sv, font_choice::bold},
Expand Down
4 changes: 2 additions & 2 deletions src/parser/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ namespace mfl::parser
{
using namespace std::string_view_literals;

const auto function_names =
constexpr auto function_names =
std::array{"arccos"sv, "csc"sv, "ker"sv, "min"sv, "arcsin"sv, "deg"sv, "lg"sv, "Pr"sv,
"arctan"sv, "det"sv, "lim"sv, "sec"sv, "arg"sv, "dim"sv, "liminf"sv, "sin"sv,
"cos"sv, "exp"sv, "limsup"sv, "sinh"sv, "cosh"sv, "gcd"sv, "ln"sv, "sup"sv,
"cot"sv, "hom"sv, "log"sv, "tan"sv, "coth"sv, "inf"sv, "max"sv, "tanh"sv};

const auto sub_function_names =
constexpr auto sub_function_names =
std::array{"det"sv, "gcd"sv, "inf"sv, "lim"sv, "liminf"sv, "limsup"sv, "max"sv, "min"sv, "Pr"sv, "sup"sv};

bool is_sub_function(const std::string& name) { return std::ranges::contains(sub_function_names, name); }
Expand Down
13 changes: 5 additions & 8 deletions src/parser/left_right.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace mfl::parser
symbol
};

const auto command_delimiter_names =
constexpr auto command_delimiter_names =
std::array{"|"sv, "lfloor"sv, "rfloor"sv, "lceil"sv, "rceil"sv,
"langle"sv, "rangle"sv, "uparrow"sv, "Uparrow"sv, "downarrow"sv,
"Downarrow"sv, "updownarrow"sv, "Updownarrow"sv, "vert"sv, "Vert"sv};
Expand All @@ -49,20 +49,17 @@ namespace mfl::parser
state.consume_token(tokens::open_brace);
}

auto result = code_point(0);
auto result = code_point{0};

const auto tok = state.lexer_token();
const auto is_empty_braces = (is_closing_brace_expected and (tok == tokens::close_brace));

if (!is_empty_braces)
if (const auto is_empty_braces = (is_closing_brace_expected and (tok == tokens::close_brace)); !is_empty_braces)
{
const auto name = state.consume_lexer_value();
if (!((tok == tokens::symbol) and (name == ".")))
if (const auto name = state.consume_lexer_value(); !((tok == tokens::symbol) and (name == ".")))
{
const auto is_valid_token = (tok == tokens::symbol) or (tok == tokens::command);
const auto full_name = ((tok == tokens::command) ? "\\"s : ""s) + name;
const auto type = get_delimiter_type(full_name);
if (!is_valid_token or (type == delimiter_type::none))
if (const auto type = get_delimiter_type(full_name); !is_valid_token or (type == delimiter_type::none))
{
state.set_error(fmt::format("'{}' is not a valid delimiter.", full_name));
return 0;
Expand Down
1 change: 0 additions & 1 deletion src/parser/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "parser/utf8.hpp"

#include <array>
#include <istream>
#include <string_view>

Expand Down
6 changes: 3 additions & 3 deletions src/parser/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace mfl
}
};

struct istring_view_stream : virtual string_view_buf, std::istream
struct istring_view_stream final : virtual string_view_buf, std::istream
{
explicit istring_view_stream(const std::string_view s)
: string_view_buf(s), std::istream(static_cast<std::streambuf*>(this))
Expand All @@ -36,7 +36,7 @@ namespace mfl
istring_view_stream is(input);
parser::lexer lx(is);
parser::parser_state state(lx);
const auto result = parser::parse_until_token(state, parser::tokens::eof);
return {result.noads, state.error()};
const auto [noads] = parser::parse_until_token(state, parser::tokens::eof);
return {noads, state.error()};
}
}
2 changes: 1 addition & 1 deletion src/parser/parser_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace mfl::parser

std::string parser_state::lexer_value() const { return lexer_.value(); }

std::string parser_state::consume_lexer_value()
std::string parser_state::consume_lexer_value() const
{
auto movable_result = lexer_.value();
lexer_.move_to_next_token();
Expand Down
2 changes: 1 addition & 1 deletion src/parser/parser_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace mfl::parser
[[nodiscard]] tokens lexer_token() const;
void consume_token(const tokens tok);
[[nodiscard]] std::string lexer_value() const;
[[nodiscard]] std::string consume_lexer_value();
[[nodiscard]] std::string consume_lexer_value() const;
[[nodiscard]] std::pair<tokens, std::string> lexer_state() const;

void set_font_choice(const font_choice choice);
Expand Down
15 changes: 5 additions & 10 deletions src/parser/parser_utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ namespace mfl::parser
return {};
}

auto noads = create_script(state);
std::move(noads.begin(), noads.end(), std::back_inserter(result));
std::ranges::move(create_script(state), std::back_inserter(result));
}

state.consume_token(end_token);
Expand All @@ -58,11 +57,11 @@ namespace mfl::parser
state.consume_token(tokens::open_brace);
scoped_state s(state, {.font = state.get_font_choice()});

const auto noad_list = parse_until_token(state, tokens::close_brace);
const auto [noads] = parse_until_token(state, tokens::close_brace);

state.consume_token(tokens::close_brace);

return noad_list.noads;
return noads;
}

std::pair<optional_noads, optional_noads> parse_sub_sup(parser_state& state)
Expand All @@ -85,14 +84,10 @@ namespace mfl::parser
std::vector<noad> parse_item(parser_state& state)
{
std::vector<noad> result;
auto tok = state.lexer_token();
if (tok == tokens::symbol)
if (const auto tok = state.lexer_token(); tok == tokens::symbol)
result.emplace_back(create_math_char(state));
else if (tok == tokens::command)
{
auto noads = create_command(state);
std::move(noads.begin(), noads.end(), std::back_inserter(result));
}
std::ranges::move(create_command(state), std::back_inserter(result));
else
state.set_error("unexpected token.");

Expand Down
Loading