mirror of
https://git.mirrors.martin98.com/https://github.com/prusa3d/PrusaSlicer.git
synced 2025-08-14 12:55:53 +08:00
update font after bold click
This commit is contained in:
parent
1078fe55ec
commit
057abce935
@ -468,12 +468,7 @@ std::optional<std::wstring> Emboss::get_font_path(const std::wstring &font_face_
|
|||||||
std::unique_ptr<Emboss::FontFile> Emboss::load_font(
|
std::unique_ptr<Emboss::FontFile> Emboss::load_font(
|
||||||
std::vector<unsigned char> &&data)
|
std::vector<unsigned char> &&data)
|
||||||
{
|
{
|
||||||
unsigned int collection_size = 0;
|
int collection_size = stbtt_GetNumberOfFonts(data.data());
|
||||||
int font_offset = 0;
|
|
||||||
while (font_offset >= 0) {
|
|
||||||
font_offset = stbtt_GetFontOffsetForIndex(data.data(), collection_size++);
|
|
||||||
}
|
|
||||||
--collection_size; // last one is bad
|
|
||||||
// at least one font must be inside collection
|
// at least one font must be inside collection
|
||||||
if (collection_size < 1) {
|
if (collection_size < 1) {
|
||||||
std::cerr << "There is no font collection inside data." << std::endl;
|
std::cerr << "There is no font collection inside data." << std::endl;
|
||||||
|
@ -810,7 +810,10 @@ void GLGizmoEmboss::draw_font_list()
|
|||||||
ImGui::SameLine();
|
ImGui::SameLine();
|
||||||
// select font file by file browser
|
// select font file by file browser
|
||||||
if (draw_button(IconType::open_file)) {
|
if (draw_button(IconType::open_file)) {
|
||||||
choose_true_type_file();
|
if (choose_true_type_file()) {
|
||||||
|
m_font_manager.free_style_images();
|
||||||
|
process();
|
||||||
|
}
|
||||||
} else if (ImGui::IsItemHovered())
|
} else if (ImGui::IsItemHovered())
|
||||||
ImGui::SetTooltip("%s", _u8L("add file with font(.ttf, .ttc)").c_str());
|
ImGui::SetTooltip("%s", _u8L("add file with font(.ttf, .ttc)").c_str());
|
||||||
#endif // ALLOW_ADD_FONT_BY_FILE
|
#endif // ALLOW_ADD_FONT_BY_FILE
|
||||||
@ -818,7 +821,10 @@ void GLGizmoEmboss::draw_font_list()
|
|||||||
#ifdef ALLOW_ADD_FONT_BY_OS_SELECTOR
|
#ifdef ALLOW_ADD_FONT_BY_OS_SELECTOR
|
||||||
ImGui::SameLine();
|
ImGui::SameLine();
|
||||||
if (draw_button(IconType::system_selector)) {
|
if (draw_button(IconType::system_selector)) {
|
||||||
choose_font_by_wxdialog();
|
if (choose_font_by_wxdialog()) {
|
||||||
|
m_font_manager.free_style_images();
|
||||||
|
process();
|
||||||
|
}
|
||||||
} else if (ImGui::IsItemHovered())
|
} else if (ImGui::IsItemHovered())
|
||||||
ImGui::SetTooltip("%s", _u8L("Open dialog for choose from fonts.").c_str());
|
ImGui::SetTooltip("%s", _u8L("Open dialog for choose from fonts.").c_str());
|
||||||
#endif // ALLOW_ADD_FONT_BY_OS_SELECTOR
|
#endif // ALLOW_ADD_FONT_BY_OS_SELECTOR
|
||||||
@ -1075,7 +1081,7 @@ bool GLGizmoEmboss::italic_button()
|
|||||||
skew.reset();
|
skew.reset();
|
||||||
if (wx_font->GetStyle() != wxFontStyle::wxFONTSTYLE_NORMAL) {
|
if (wx_font->GetStyle() != wxFontStyle::wxFONTSTYLE_NORMAL) {
|
||||||
wx_font->SetStyle(wxFontStyle::wxFONTSTYLE_NORMAL);
|
wx_font->SetStyle(wxFontStyle::wxFONTSTYLE_NORMAL);
|
||||||
font_file = WxFontUtils::create_font_file(*wx_font);
|
m_font_manager.wx_font_changed();
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -1084,8 +1090,8 @@ bool GLGizmoEmboss::italic_button()
|
|||||||
} else {
|
} else {
|
||||||
if (draw_button(IconType::italic)) {
|
if (draw_button(IconType::italic)) {
|
||||||
auto new_ff = WxFontUtils::set_italic(*wx_font, *font_file);
|
auto new_ff = WxFontUtils::set_italic(*wx_font, *font_file);
|
||||||
if (new_ff) {
|
if (new_ff != nullptr) {
|
||||||
font_file = std::move(new_ff);
|
m_font_manager.wx_font_changed(std::move(new_ff));
|
||||||
} else {
|
} else {
|
||||||
// italic font doesn't exist
|
// italic font doesn't exist
|
||||||
// add skew when wxFont can't set it
|
// add skew when wxFont can't set it
|
||||||
@ -1114,7 +1120,7 @@ bool GLGizmoEmboss::bold_button() {
|
|||||||
boldness.reset();
|
boldness.reset();
|
||||||
if (wx_font->GetWeight() != wxFontWeight::wxFONTWEIGHT_NORMAL) {
|
if (wx_font->GetWeight() != wxFontWeight::wxFONTWEIGHT_NORMAL) {
|
||||||
wx_font->SetWeight(wxFontWeight::wxFONTWEIGHT_NORMAL);
|
wx_font->SetWeight(wxFontWeight::wxFONTWEIGHT_NORMAL);
|
||||||
font_file = WxFontUtils::create_font_file(*wx_font);
|
m_font_manager.wx_font_changed();
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -1123,12 +1129,14 @@ bool GLGizmoEmboss::bold_button() {
|
|||||||
} else {
|
} else {
|
||||||
if (draw_button(IconType::bold)) {
|
if (draw_button(IconType::bold)) {
|
||||||
auto new_ff = WxFontUtils::set_bold(*wx_font, *font_file);
|
auto new_ff = WxFontUtils::set_bold(*wx_font, *font_file);
|
||||||
if (new_ff) {
|
if (new_ff != nullptr) {
|
||||||
font_file = std::move(new_ff);
|
m_font_manager.wx_font_changed(std::move(new_ff));
|
||||||
} else {
|
} else {
|
||||||
// bold font can't be loaded
|
// bold font can't be loaded
|
||||||
// set up boldness
|
// set up boldness
|
||||||
boldness = 20.f;
|
boldness = 20.f;
|
||||||
|
m_font_manager.free_style_images();
|
||||||
|
//font_file->cache.empty();
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -1140,6 +1148,8 @@ bool GLGizmoEmboss::bold_button() {
|
|||||||
|
|
||||||
void GLGizmoEmboss::draw_style_edit() {
|
void GLGizmoEmboss::draw_style_edit() {
|
||||||
const GuiCfg::Translations &tr = m_gui_cfg->translations;
|
const GuiCfg::Translations &tr = m_gui_cfg->translations;
|
||||||
|
m_imgui->text_colored(ImGuiWrapper::COL_GREY_DARK, m_font_manager.get_font_item().path);
|
||||||
|
|
||||||
ImGui::Text("%s", tr.font.c_str());
|
ImGui::Text("%s", tr.font.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);
|
||||||
@ -1171,19 +1181,39 @@ void GLGizmoEmboss::draw_style_edit() {
|
|||||||
// store font size into path
|
// store font size into path
|
||||||
if (fi.type == WxFontUtils::get_actual_type()) {
|
if (fi.type == WxFontUtils::get_actual_type()) {
|
||||||
if (wx_font.has_value()) {
|
if (wx_font.has_value()) {
|
||||||
// TODO: check difference wx_font->Scale(float x)
|
|
||||||
wx_font->SetPointSize(font_prop.size_in_mm);
|
wx_font->SetPointSize(font_prop.size_in_mm);
|
||||||
fi.path = WxFontUtils::store_wxFont(*wx_font);
|
m_font_manager.wx_font_changed();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exist_change = true;
|
exist_change = true;
|
||||||
}
|
}
|
||||||
if (exist_change) {
|
|
||||||
m_font_manager.clear_imgui_font();
|
if (wx_font.has_value()) {
|
||||||
m_font_manager.free_style_images();
|
ImGui::Text("%s", "weight");
|
||||||
process();
|
ImGui::SameLine(m_gui_cfg->style_edit_text_width);
|
||||||
|
ImGui::SetNextItemWidth(m_gui_cfg->combo_font_width);
|
||||||
|
int weight = wx_font->GetNumericWeight();
|
||||||
|
int min_weight = 1, max_weight = 1000;
|
||||||
|
if (ImGui::SliderInt("##weight", &weight, min_weight, max_weight))
|
||||||
|
{
|
||||||
|
wx_font->SetNumericWeight(weight);
|
||||||
|
m_font_manager.wx_font_changed();
|
||||||
|
exist_change = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
wxFont f = wx_font->Bold();
|
||||||
|
bool disable = f == *wx_font;
|
||||||
|
ImGui::SameLine();
|
||||||
|
if (draw_button(IconType::bold, disable)) {
|
||||||
|
*wx_font = f;
|
||||||
|
m_font_manager.wx_font_changed();
|
||||||
|
exist_change = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
@ -1326,8 +1356,7 @@ bool GLGizmoEmboss::choose_font_by_wxdialog()
|
|||||||
|
|
||||||
// Try load and use new added font
|
// Try load and use new added font
|
||||||
if ((use_deserialized_font && !m_font_manager.load_font(font_index)) ||
|
if ((use_deserialized_font && !m_font_manager.load_font(font_index)) ||
|
||||||
(!use_deserialized_font && !m_font_manager.load_font(font_index, wx_font)) ||
|
(!use_deserialized_font && !m_font_manager.load_font(font_index, wx_font))) {
|
||||||
!process()) {
|
|
||||||
m_font_manager.erase(font_index);
|
m_font_manager.erase(font_index);
|
||||||
wxString message = GUI::format_wxstr(
|
wxString message = GUI::format_wxstr(
|
||||||
_L("Font '%1%' can't be used. Please select another."),
|
_L("Font '%1%' can't be used. Please select another."),
|
||||||
@ -1343,7 +1372,6 @@ bool GLGizmoEmboss::choose_font_by_wxdialog()
|
|||||||
!Emboss::is_italic(*m_font_manager.get_font_file())) {
|
!Emboss::is_italic(*m_font_manager.get_font_file())) {
|
||||||
m_font_manager.get_font_item().prop.skew = 0.2;
|
m_font_manager.get_font_item().prop.skew = 0.2;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
#endif // ALLOW_ADD_FONT_BY_OS_SELECTOR
|
#endif // ALLOW_ADD_FONT_BY_OS_SELECTOR
|
||||||
@ -1356,28 +1384,23 @@ bool GLGizmoEmboss::choose_true_type_file()
|
|||||||
wxString selectedFile = wxEmptyString;
|
wxString selectedFile = wxEmptyString;
|
||||||
wxFileDialog dialog(nullptr, _L("Choose one or more files (TTF, TTC):"),
|
wxFileDialog dialog(nullptr, _L("Choose one or more files (TTF, TTC):"),
|
||||||
fontDir, selectedFile, file_wildcards(FT_FONTS),
|
fontDir, selectedFile, file_wildcards(FT_FONTS),
|
||||||
wxFD_OPEN | wxFD_MULTIPLE | wxFD_FILE_MUST_EXIST);
|
wxFD_OPEN | wxFD_FILE_MUST_EXIST);
|
||||||
if (dialog.ShowModal() == wxID_OK) dialog.GetPaths(input_files);
|
if (dialog.ShowModal() == wxID_OK) dialog.GetPaths(input_files);
|
||||||
if (input_files.IsEmpty()) return false;
|
if (input_files.IsEmpty()) return false;
|
||||||
bool font_loaded = false;
|
size_t index = m_font_manager.get_fonts().size();
|
||||||
|
// use first valid font
|
||||||
for (auto &input_file : input_files) {
|
for (auto &input_file : input_files) {
|
||||||
std::string path = std::string(input_file.c_str());
|
std::string path = std::string(input_file.c_str());
|
||||||
std::string name = get_file_name(path);
|
std::string name = get_file_name(path);
|
||||||
//make_unique_name(name, m_font_list);
|
//make_unique_name(name, m_font_list);
|
||||||
FontItem fi(name, path, FontItem::Type::file_path, FontProp());
|
const FontProp& prop = m_font_manager.get_font_prop();
|
||||||
|
FontItem fi(name, path, FontItem::Type::file_path, prop);
|
||||||
m_font_manager.add_font(fi);
|
m_font_manager.add_font(fi);
|
||||||
|
|
||||||
// set first valid added font as active
|
// set first valid added font as active
|
||||||
if (!font_loaded) {
|
if (m_font_manager.load_font(index)) return true;
|
||||||
size_t index = m_font_manager.get_fonts().size() - 1;
|
m_font_manager.erase(index);
|
||||||
if (!m_font_manager.load_font(index))
|
|
||||||
m_font_manager.erase(index);
|
|
||||||
else
|
|
||||||
font_loaded = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (font_loaded) process();
|
return false;
|
||||||
return font_loaded;
|
|
||||||
}
|
}
|
||||||
#endif // ALLOW_ADD_FONT_BY_FILE
|
#endif // ALLOW_ADD_FONT_BY_FILE
|
||||||
|
|
||||||
|
@ -51,16 +51,37 @@ void FontManager::duplicate(size_t index) {
|
|||||||
|
|
||||||
void FontManager::erase(size_t index) {
|
void FontManager::erase(size_t index) {
|
||||||
if (index >= m_font_list.size()) return;
|
if (index >= m_font_list.size()) return;
|
||||||
|
//ImFont *imgui_font = get_imgui_font(index);
|
||||||
ImFont *imgui_font = get_imgui_font(index);
|
//if (imgui_font != nullptr)
|
||||||
if (imgui_font != nullptr)
|
// IM_DELETE(imgui_font);
|
||||||
IM_DELETE(imgui_font);
|
|
||||||
|
|
||||||
m_font_list.erase(m_font_list.begin() + index);
|
|
||||||
|
|
||||||
// fix selected index
|
// fix selected index
|
||||||
if (!is_activ_font()) return;
|
if (is_activ_font() && index < m_font_selected)
|
||||||
if (index < m_font_selected) --m_font_selected;
|
--m_font_selected;
|
||||||
|
|
||||||
|
m_font_list.erase(m_font_list.begin() + index);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FontManager::wx_font_changed(std::unique_ptr<Emboss::FontFile> font_file)
|
||||||
|
{
|
||||||
|
if (!is_activ_font()) return false;
|
||||||
|
auto &wx_font = get_wx_font();
|
||||||
|
if (!wx_font.has_value()) return false;
|
||||||
|
|
||||||
|
if (font_file == nullptr) {
|
||||||
|
auto new_font_file = WxFontUtils::create_font_file(*wx_font);
|
||||||
|
if (new_font_file == nullptr) return false;
|
||||||
|
get_font_file() = std::move(new_font_file);
|
||||||
|
} else {
|
||||||
|
get_font_file() = std::move(font_file);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto &fi = get_font_item();
|
||||||
|
fi.type = WxFontUtils::get_actual_type();
|
||||||
|
fi.path = WxFontUtils::store_wxFont(*wx_font);
|
||||||
|
clear_imgui_font();
|
||||||
|
free_style_images();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FontManager::load_font(size_t font_index)
|
bool FontManager::load_font(size_t font_index)
|
||||||
|
@ -41,6 +41,13 @@ public:
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="index">Index of style to be removed</param>
|
/// <param name="index">Index of style to be removed</param>
|
||||||
void erase(size_t index);
|
void erase(size_t index);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Actual wx font was changed
|
||||||
|
/// Clear caches
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="font_file">font file created by WxFontUtils::create_font_file(wx_font)</param>
|
||||||
|
bool wx_font_changed(std::unique_ptr<Emboss::FontFile> font_file = nullptr);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Change active font
|
/// Change active font
|
||||||
|
@ -245,7 +245,7 @@ std::unique_ptr<Emboss::FontFile> WxFontUtils::set_italic(wxFont &font, const Em
|
|||||||
wxFontStyle::wxFONTSTYLE_ITALIC,
|
wxFontStyle::wxFONTSTYLE_ITALIC,
|
||||||
wxFontStyle::wxFONTSTYLE_SLANT
|
wxFontStyle::wxFONTSTYLE_SLANT
|
||||||
};
|
};
|
||||||
|
wxFontStyle orig_style = font.GetStyle();
|
||||||
for (wxFontStyle style : italic_styles) {
|
for (wxFontStyle style : italic_styles) {
|
||||||
font.SetStyle(style);
|
font.SetStyle(style);
|
||||||
std::unique_ptr<Emboss::FontFile> new_font_file =
|
std::unique_ptr<Emboss::FontFile> new_font_file =
|
||||||
@ -259,6 +259,8 @@ std::unique_ptr<Emboss::FontFile> WxFontUtils::set_italic(wxFont &font, const Em
|
|||||||
|
|
||||||
return new_font_file;
|
return new_font_file;
|
||||||
}
|
}
|
||||||
|
// There is NO italic font by wx
|
||||||
|
font.SetStyle(orig_style);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -270,7 +272,7 @@ std::unique_ptr<Emboss::FontFile> WxFontUtils::set_bold(wxFont &font, const Embo
|
|||||||
wxFontWeight::wxFONTWEIGHT_EXTRABOLD,
|
wxFontWeight::wxFONTWEIGHT_EXTRABOLD,
|
||||||
wxFontWeight::wxFONTWEIGHT_EXTRAHEAVY
|
wxFontWeight::wxFONTWEIGHT_EXTRAHEAVY
|
||||||
};
|
};
|
||||||
|
wxFontWeight orig_weight = font.GetWeight();
|
||||||
for (wxFontWeight weight : bold_weight) {
|
for (wxFontWeight weight : bold_weight) {
|
||||||
font.SetWeight(weight);
|
font.SetWeight(weight);
|
||||||
std::unique_ptr<Emboss::FontFile> new_font_file =
|
std::unique_ptr<Emboss::FontFile> new_font_file =
|
||||||
@ -284,5 +286,7 @@ std::unique_ptr<Emboss::FontFile> WxFontUtils::set_bold(wxFont &font, const Embo
|
|||||||
|
|
||||||
return new_font_file;
|
return new_font_file;
|
||||||
}
|
}
|
||||||
|
// There is NO bold font by wx
|
||||||
|
font.SetWeight(orig_weight);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user