Skip to content

Commit

Permalink
Fix site maps being incorrectly scaled. Optimize SVGs to combine colu…
Browse files Browse the repository at this point in the history
…mns that are the same height.
  • Loading branch information
BenLubar committed Mar 19, 2018
1 parent bb73f12 commit e4f3118
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
12 changes: 9 additions & 3 deletions helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1229,18 +1229,24 @@ void born_died(std::ostream & s, df::historical_figure *hf)
s << ")";
}

void render_map_coords(std::ostream &s, const df::coord2d_path &coords)
void render_map_coords(std::ostream &s, const df::coord2d_path &coords, int32_t mul)
{
s << "<svg width=\"100%\" class=\"map\" viewBox=\"0 0 " << world->world_data->world_width << " " << world->world_data->world_height << "\">";
s << "<svg width=\"100%\" class=\"map\" viewBox=\"0 0 " << (world->world_data->world_width * mul) << " " << (world->world_data->world_height * mul) << "\">";
for (size_t i = 0; i < coords.size(); i++)
{
int width = 1;
int height = 1;
while (i + 1 < coords.size() && coords.x.at(i) == coords.x.at(i + 1) && coords.y.at(i) + 1 == coords.y.at(i + 1))
{
height++;
i++;
}
s << "<rect width=\"1\" height=\"" << height << "\" x=\"" << coords.x.at(i) << "\" y=\"" << (coords.y.at(i) - height + 1) << "\"></rect>";
while (i + height < coords.size() && coords.x.at(i) + 1 == coords.x.at(i + height) && coords.y.at(i) == coords.y.at(i + height))
{
width++;
i += height;
}
s << "<rect width=\"" << width << "\" height=\"" << height << "\" x=\"" << (coords.x.at(i) - width + 1) << "\" y=\"" << (coords.y.at(i) - height + 1) << "\"></rect>";
}
s << "</svg>";
}
2 changes: 1 addition & 1 deletion helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void spheres(std::ostream & s, df::historical_figure *hf);
void year(std::ostream & s, int32_t year, int32_t tick);
void born_died(std::ostream & s, df::historical_figure *hf);

void render_map_coords(std::ostream &s, const df::coord2d_path &coords);
void render_map_coords(std::ostream &s, const df::coord2d_path &coords, int32_t mul = 1);

// https://stackoverflow.com/a/24315631/2664560
static inline void replace_all(std::string & str, const std::string & from, const std::string & to)
Expand Down
2 changes: 1 addition & 1 deletion render_site.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ bool WebLegends::render_site(std::ostream & s, int32_t id, int32_t page)
coords.push_back(df::coord2d(uint16_t(x), uint16_t(y)));
}
}
render_map_coords(s, coords);
render_map_coords(s, coords, 16);

s << "<p>";
categorize(s, site);
Expand Down

0 comments on commit e4f3118

Please sign in to comment.