Fix font size

Fix swap style
This commit is contained in:
Filip Sykala 2022-02-15 09:14:16 +01:00
parent faa33901f2
commit 6dd5e21f59
3 changed files with 67 additions and 29 deletions

View File

@ -1014,6 +1014,7 @@ void GLGizmoEmboss::draw_style_list() {
if (ImGui::BeginCombo("##style_selector", trunc_name.c_str())) { if (ImGui::BeginCombo("##style_selector", trunc_name.c_str())) {
m_font_manager.init_style_images(m_gui_cfg->max_style_image_width); m_font_manager.init_style_images(m_gui_cfg->max_style_image_width);
const auto &fonts = m_font_manager.get_fonts(); const auto &fonts = m_font_manager.get_fonts();
std::optional<std::pair<size_t,size_t>> swap_indexes;
for (const auto &item : fonts) { for (const auto &item : fonts) {
size_t index = &item - &fonts.front(); size_t index = &item - &fonts.front();
const FontItem & fi = item.font_item; const FontItem & fi = item.font_item;
@ -1038,16 +1039,13 @@ void GLGizmoEmboss::draw_style_list() {
// reorder items // reorder items
if (ImGui::IsItemActive() && !ImGui::IsItemHovered()) { if (ImGui::IsItemActive() && !ImGui::IsItemHovered()) {
std::optional<size_t> other_index;
if (ImGui::GetMouseDragDelta(0).y < 0.f) { if (ImGui::GetMouseDragDelta(0).y < 0.f) {
if (index > 0) other_index = index - 1; if (index > 0)
swap_indexes = {index, index - 1};
} else if ((index + 1) < fonts.size()) } else if ((index + 1) < fonts.size())
other_index = index + 1; swap_indexes = {index, index + 1};
if (swap_indexes.has_value())
if (other_index.has_value()) {
m_font_manager.swap(index, *other_index);
ImGui::ResetMouseDragDelta(); ImGui::ResetMouseDragDelta();
}
} }
// draw style name // draw style name
@ -1066,6 +1064,11 @@ void GLGizmoEmboss::draw_style_list() {
} }
ImGui::PopID(); ImGui::PopID();
} }
if (swap_indexes.has_value())
m_font_manager.swap(swap_indexes->first,
swap_indexes->second);
ImGui::EndCombo(); ImGui::EndCombo();
} }
@ -1228,6 +1231,10 @@ void GLGizmoEmboss::draw_style_edit() {
exist_change |= italic_button(); exist_change |= italic_button();
ImGui::SameLine(); ImGui::SameLine();
exist_change |= bold_button(); exist_change |= bold_button();
if (exist_change) {
m_font_manager.free_style_images();
process();
}
FontItem &fi = m_font_manager.get_font_item(); FontItem &fi = m_font_manager.get_font_item();
std::optional<wxFont> &wx_font = m_font_manager.get_wx_font(); std::optional<wxFont> &wx_font = m_font_manager.get_wx_font();
@ -1254,7 +1261,7 @@ void GLGizmoEmboss::draw_style_edit() {
m_font_manager.wx_font_changed(); m_font_manager.wx_font_changed();
} }
} }
exist_change = true; process();
} }
#ifdef SHOW_WX_WEIGHT_INPUT #ifdef SHOW_WX_WEIGHT_INPUT
@ -1267,7 +1274,7 @@ void GLGizmoEmboss::draw_style_edit() {
if (ImGui::SliderInt("##weight", &weight, min_weight, max_weight)) { if (ImGui::SliderInt("##weight", &weight, min_weight, max_weight)) {
wx_font->SetNumericWeight(weight); wx_font->SetNumericWeight(weight);
m_font_manager.wx_font_changed(); m_font_manager.wx_font_changed();
exist_change = true; process();
} }
wxFont f = wx_font->Bold(); wxFont f = wx_font->Bold();
@ -1276,16 +1283,13 @@ void GLGizmoEmboss::draw_style_edit() {
if (draw_button(IconType::bold, disable)) { if (draw_button(IconType::bold, disable)) {
*wx_font = f; *wx_font = f;
m_font_manager.wx_font_changed(); m_font_manager.wx_font_changed();
exist_change = true; process();
} }
if (ImGui::IsItemHovered()) if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", _u8L("wx Make bold").c_str()); ImGui::SetTooltip("%s", _u8L("wx Make bold").c_str());
} }
#endif // SHOW_WX_WEIGHT_INPUT #endif // SHOW_WX_WEIGHT_INPUT
if (exist_change)
process();
ImGui::Text("%s", tr.depth.c_str()); ImGui::Text("%s", tr.depth.c_str());
ImGui::SameLine(m_gui_cfg->style_edit_text_width); ImGui::SameLine(m_gui_cfg->style_edit_text_width);
ImGui::SetNextItemWidth(m_gui_cfg->combo_font_width); ImGui::SetNextItemWidth(m_gui_cfg->combo_font_width);
@ -1704,7 +1708,7 @@ bool GLGizmoEmboss::init_icons()
// state order has to match the enum IconState // state order has to match the enum IconState
std::vector<std::pair<int, bool>> states; std::vector<std::pair<int, bool>> states;
states.push_back(std::make_pair(1, false)); // Activable states.push_back(std::make_pair(1, false)); // Activable
states.push_back(std::make_pair(0, true)); // Hovered states.push_back(std::make_pair(0, false)); // Hovered
states.push_back(std::make_pair(2, false)); // Disabled states.push_back(std::make_pair(2, false)); // Disabled
unsigned int sprite_size_px = std::ceil(m_gui_cfg->icon_width); unsigned int sprite_size_px = std::ceil(m_gui_cfg->icon_width);

View File

@ -21,15 +21,26 @@ FontManager::~FontManager() {
} }
void FontManager::swap(size_t i1, size_t i2) { void FontManager::swap(size_t i1, size_t i2) {
if (i1 >= m_font_list.size() && if (i1 >= m_font_list.size() ||
i2 >= m_font_list.size()) return; i2 >= m_font_list.size()) return;
std::swap(m_font_list[i1], m_font_list[i2]); std::swap(m_font_list[i1], m_font_list[i2]);
// fix selected index // fix selected index
if (!is_activ_font()) return; if (!is_activ_font()) return;
if (m_font_selected == i1) bool change_selected = false;
if (m_font_selected == i1) {
m_font_selected = i2; m_font_selected = i2;
else if (m_font_selected == i2) change_selected = true;
} else if (m_font_selected == i2) {
m_font_selected = i1; m_font_selected = i1;
change_selected = true;
}
// something went wrong with imgui font when swap
// Hot fix is regenerate imgui font
if (change_selected) {
m_font_list[m_font_selected].imgui_font_index.reset();
}
} }
void FontManager::duplicate() { duplicate(m_font_selected); } void FontManager::duplicate() { duplicate(m_font_selected); }
@ -71,9 +82,9 @@ bool FontManager::wx_font_changed(std::unique_ptr<Emboss::FontFile> font_file)
if (font_file == nullptr) { if (font_file == nullptr) {
auto new_font_file = WxFontUtils::create_font_file(*wx_font); auto new_font_file = WxFontUtils::create_font_file(*wx_font);
if (new_font_file == nullptr) return false; if (new_font_file == nullptr) return false;
get_font_file() = std::move(new_font_file); m_font_list[m_font_selected].font_file = std::move(new_font_file);
} else { } else {
get_font_file() = std::move(font_file); m_font_list[m_font_selected].font_file = std::move(font_file);
} }
auto &fi = get_font_item(); auto &fi = get_font_item();
@ -396,12 +407,17 @@ void FontManager::create_texture(size_t index, const std::string &text, GLuint&
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
} }
// for get DPI
#include "slic3r/GUI/GUI_App.hpp"
#include "slic3r/GUI/MainFrame.hpp"
void FontManager::init_style_images(int max_width) { void FontManager::init_style_images(int max_width) {
// check already initialized // check already initialized
if (m_exist_style_images) return; if (m_exist_style_images) return;
// create shapes and calc size (bounding boxes) // create shapes and calc size (bounding boxes)
std::vector<ExPolygons> name_shapes(m_font_list.size()); std::vector<ExPolygons> name_shapes(m_font_list.size());
std::vector<double> scales(m_font_list.size());
for (Item &item : m_font_list) { for (Item &item : m_font_list) {
FontItem & font_item = item.font_item; FontItem & font_item = item.font_item;
const FontProp & font_prop = font_item.prop; const FontProp & font_prop = font_item.prop;
@ -412,6 +428,7 @@ void FontManager::init_style_images(int max_width) {
ExPolygons &shapes = name_shapes[index]; ExPolygons &shapes = name_shapes[index];
shapes = Emboss::text2shapes(*font_file, font_item.name.c_str(), font_prop); shapes = Emboss::text2shapes(*font_file, font_item.name.c_str(), font_prop);
// create image description // create image description
item.image = StyleImage(); item.image = StyleImage();
StyleImage &image = *item.image; StyleImage &image = *item.image;
@ -420,9 +437,18 @@ void FontManager::init_style_images(int max_width) {
for (ExPolygon &shape : shapes) for (ExPolygon &shape : shapes)
bounding_box.merge(BoundingBox(shape.contour.points)); bounding_box.merge(BoundingBox(shape.contour.points));
for (ExPolygon &shape : shapes) shape.translate(-bounding_box.min); for (ExPolygon &shape : shapes) shape.translate(-bounding_box.min);
// calculate conversion from FontPoint to screen pixels by size of font
auto mf = wxGetApp().mainframe;
// dot per inch for monitor
int dpi = get_dpi_for_window(mf);
double ppm = dpi / 25.4; // pixel per milimeter
double scale = font_prop.size_in_mm / font_file->ascent * Emboss::SHAPE_SCALE * ppm;
scales[index] = scale;
double scale = font_prop.size_in_mm; //double scale = font_prop.size_in_mm * SCALING_FACTOR;
BoundingBoxf bb2 = unscaled(bounding_box); BoundingBoxf bb2(bounding_box.min.cast<double>(),
bounding_box.max.cast<double>());
bb2.scale(scale); bb2.scale(scale);
image.tex_size.x = bb2.max.x() - bb2.min.x()+1; image.tex_size.x = bb2.max.x() - bb2.min.x()+1;
image.tex_size.y = bb2.max.y() - bb2.min.y()+1; image.tex_size.y = bb2.max.y() - bb2.min.y()+1;
@ -469,14 +495,18 @@ void FontManager::init_style_images(int max_width) {
for (Item &item : m_font_list) item.image->texture_id = texture_id; for (Item &item : m_font_list) item.image->texture_id = texture_id;
// upload sub textures // upload sub textures
for (Item &item : m_font_list) { for (Item &item : m_font_list) {
double scale = item.font_item.prop.size_in_mm;
StyleImage &image = *item.image; StyleImage &image = *item.image;
sla::Resolution resolution(image.tex_size.x, image.tex_size.y); sla::Resolution resolution(image.tex_size.x, image.tex_size.y);
sla::PixelDim dim(1 / scale, 1 / scale);
size_t index = &item - &m_font_list.front();
//double scale = item.font_item.prop.size_in_mm / SCALING_FACTOR / item.font_file->ascent;
//double scale = item.font_item.prop.size_in_mm;
//sla::PixelDim dim(1 / scale, 1 / scale);
double pixel_dim = SCALING_FACTOR / scales[index];
sla::PixelDim dim(pixel_dim, pixel_dim);
double gamma = 1.; double gamma = 1.;
std::unique_ptr<sla::RasterBase> r = sla::create_raster_grayscale_aa(resolution, dim, gamma); std::unique_ptr<sla::RasterBase> r = sla::create_raster_grayscale_aa(resolution, dim, gamma);
size_t index = &item - &m_font_list.front();
for (const ExPolygon &shape : name_shapes[index]) r->draw(shape); for (const ExPolygon &shape : name_shapes[index]) r->draw(shape);
const Point& offset = image.offset; const Point& offset = image.offset;
sla::RasterEncoder encoder = sla::RasterEncoder encoder =
@ -542,8 +572,12 @@ ImFont * FontManager::load_imgui_font(size_t index, const std::string &text)
ImFontAtlasFlags_NoPowerOfTwoHeight; ImFontAtlasFlags_NoPowerOfTwoHeight;
const FontProp &font_prop = item.font_item.prop; const FontProp &font_prop = item.font_item.prop;
int font_size = static_cast<int>(
std::round(std::abs(font_prop.size_in_mm / 0.3528))); float c1 = (font_file.ascent - font_file.descent + font_file.linegap) / (float)font_file.ascent;
// The point size is defined as 1/72 of the Anglo-Saxon inch (25.4 mm):
// it is approximately 0.0139 inch or 352.8 um.
// But it is too small, so I decide use point size as mm for emboss
float font_size = c1 * std::abs(font_prop.size_in_mm) / 0.3528f;
if (font_size < m_cfg.min_imgui_font_size) if (font_size < m_cfg.min_imgui_font_size)
font_size = m_cfg.min_imgui_font_size; font_size = m_cfg.min_imgui_font_size;
if (font_size > m_cfg.max_imgui_font_size) if (font_size > m_cfg.max_imgui_font_size)

View File

@ -185,8 +185,8 @@ private:
{ {
// limits for imgui loaded font // limits for imgui loaded font
// Value out of limits is crop // Value out of limits is crop
int min_imgui_font_size = 18; float min_imgui_font_size = 18.f;
int max_imgui_font_size = 60; float max_imgui_font_size = 60.f;
} m_cfg; } m_cfg;
bool set_up_font_file(size_t item_index); bool set_up_font_file(size_t item_index);