Remove Flatness and deduce it from font size

This commit is contained in:
Filip Sykala 2021-10-27 15:47:51 +02:00
parent b33de9a9d9
commit d2dda18753
6 changed files with 86 additions and 46 deletions

View File

@ -8,9 +8,11 @@
#include "imgui/imstb_truetype.h" // stbtt_fontinfo
#include <Triangulation.hpp> // CGAL project
#include "libslic3r.h"
using namespace Slic3r;
double Emboss::SHAPE_SCALE = 0.001;//SCALING_FACTOR;
// do not expose out of this file stbtt_ data types
class Private
{
@ -18,7 +20,7 @@ public:
Private() = delete;
static std::optional<stbtt_fontinfo> load_font_info(const Emboss::Font &font);
static std::optional<Emboss::Glyph> get_glyph(stbtt_fontinfo &font_info, int unicode_letter, float flatness = 2.f);
static std::optional<Emboss::Glyph> get_glyph(stbtt_fontinfo &font_info, int unicode_letter, float flatness);
static std::optional<Emboss::Glyph> get_glyph(int unicode, const Emboss::Font &font, const FontProp &font_prop,
Emboss::Glyphs &cache, std::optional<stbtt_fontinfo> &font_info_opt);
@ -31,6 +33,9 @@ public:
/// <param name="expolygons">Shape which can contain same point, will be extended by dilatation rects</param>
/// <returns>ExPolygons with only unique points</returns>
static ExPolygons dilate_to_unique_points(ExPolygons &expolygons);
// scale and convert flota to int coordinate
static Point to_point(const stbtt__point &point);
};
std::optional<stbtt_fontinfo> Private::load_font_info(const Emboss::Font &font)
@ -86,12 +91,11 @@ std::optional<Emboss::Glyph> Private::get_glyph(stbtt_fontinfo &font_info, int u
--length;
Points pts;
pts.reserve(length);
for (int i = 0; i < length; ++i) {
const stbtt__point &point = points[pi++];
pts.emplace_back(point.x, point.y);
}
// last point is first point
assert(pts.front() == Point(points[pi].x, points[pi].y));
for (int i = 0; i < length; ++i)
pts.emplace_back(to_point(points[pi++]));
// last point is first point --> closed contour
assert(pts.front() == to_point(points[pi]));
++pi;
// change outer cw to ccw and inner ccw to cw order
@ -115,15 +119,17 @@ std::optional<Emboss::Glyph> Private::get_glyph(
auto glyph_item = cache.find(unicode);
if (glyph_item != cache.end())
return glyph_item->second;
if (!font_info_opt.has_value()) {
font_info_opt = Private::load_font_info(font);
// can load font info?
if (!font_info_opt.has_value()) return {};
}
float flatness = static_cast<float>(
font.ascent * RESOLUTION / font_prop.size_in_mm);
std::optional<Emboss::Glyph> glyph_opt =
Private::get_glyph(*font_info_opt, unicode, font_prop.flatness);
Private::get_glyph(*font_info_opt, unicode, flatness);
// IMPROVE: multiple loadig glyph without data
// has definition inside of font?
@ -204,6 +210,11 @@ ExPolygons Private::dilate_to_unique_points(ExPolygons &expolygons)
return result;
}
Point Private::to_point(const stbtt__point &point) {
return Point(static_cast<int>(std::round(point.x / Emboss::SHAPE_SCALE)),
static_cast<int>(std::round(point.y / Emboss::SHAPE_SCALE)));
}
#ifdef _WIN32
#include <windows.h>
#include <wingdi.h>
@ -527,7 +538,7 @@ ExPolygons Emboss::text2shapes(Font & font,
for (wchar_t wc: ws){
if (wc == '\n') {
cursor.x() = 0;
cursor.y() -= font.ascent - font.descent + font.linegap + font_prop.line_gap;
cursor.y() -= (font.ascent - font.descent + font.linegap + font_prop.line_gap) / SHAPE_SCALE;
continue;
}
if (wc == '\t') {
@ -535,7 +546,7 @@ ExPolygons Emboss::text2shapes(Font & font,
const int count_spaces = 4;
std::optional<Glyph> space_opt = Private::get_glyph(int(' '), font, font_prop, font.cache, font_info_opt);
if (!space_opt.has_value()) continue;
cursor.x() += count_spaces *(space_opt->advance_width + font_prop.char_gap);
cursor.x() += (count_spaces *(space_opt->advance_width + font_prop.char_gap)) / SHAPE_SCALE;
continue;
}
@ -547,7 +558,7 @@ ExPolygons Emboss::text2shapes(Font & font,
ExPolygons expolygons = glyph_opt->shape; // copy
for (ExPolygon &expolygon : expolygons)
expolygon.translate(cursor);
cursor.x() += glyph_opt->advance_width + font_prop.char_gap;
cursor.x() += (glyph_opt->advance_width + font_prop.char_gap) / SHAPE_SCALE;
expolygons_append(result, expolygons);
}
result = Slic3r::union_ex(result);
@ -620,7 +631,10 @@ indexed_triangle_set Emboss::polygons2model(const ExPolygons &shape2d,
std::pair<Vec3f, Vec3f> Emboss::ProjectZ::project(const Point &p) const
{
Vec3f front(p.x(),p.y(),0.f);
Vec3f front(
static_cast<float>(p.x() * SHAPE_SCALE),
static_cast<float>(p.y() * SHAPE_SCALE),
0.f);
Vec3f back = front; // copy
back.z() = m_depth;
return std::make_pair(front, back);

View File

@ -20,6 +20,9 @@ class Emboss
{
public:
Emboss() = delete;
// every glyph's shape point is divided by SHAPE_SCALE - increase precission of fixed point value
static double SHAPE_SCALE;
/// <summary>
/// Collect fonts registred inside OS

View File

@ -153,7 +153,6 @@ static constexpr const char *FONT_DESCRIPTOR_TYPE_ATTR = "font_descriptor_type";
// TextConfiguration::FontProperty
static constexpr const char *CHAR_GAP_ATTR = "char_gap";
static constexpr const char *LINE_GAP_ATTR = "line_gap";
static constexpr const char *FLATNESS_ATTR = "flatness";
static constexpr const char *LINE_HEIGHT_ATTR = "line_height";
static constexpr const char *DEPTH_ATTR = "depth";
@ -3272,7 +3271,6 @@ void TextConfigurationSerialization::to_xml(std::stringstream &stream, const Tex
const FontProp &fp = tc.font_prop;
stream << CHAR_GAP_ATTR << "=\"" << fp.char_gap << "\" ";
stream << LINE_GAP_ATTR << "=\"" << fp.line_gap << "\" ";
stream << FLATNESS_ATTR << "=\"" << fp.flatness << "\" ";
stream << LINE_HEIGHT_ATTR << "=\"" << fp.size_in_mm << "\" ";
stream << DEPTH_ATTR << "=\"" << fp.emboss << "\" ";
@ -3291,7 +3289,6 @@ std::optional<TextConfiguration> TextConfigurationSerialization::read(const char
FontProp fp;
fp.char_gap = get_attribute_value_int(attributes, num_attributes, CHAR_GAP_ATTR);
fp.line_gap = get_attribute_value_int(attributes, num_attributes, LINE_GAP_ATTR);
fp.flatness = get_attribute_value_float(attributes, num_attributes, FLATNESS_ATTR);
fp.size_in_mm = get_attribute_value_float(attributes, num_attributes, LINE_HEIGHT_ATTR);
fp.emboss = get_attribute_value_float(attributes, num_attributes, DEPTH_ATTR);

View File

@ -6,15 +6,6 @@
#include <optional>
namespace Slic3r {
// additional information stored/load to/from .3mf file
// to be platform independent
struct Font3mfDesc
{
std::string family;
Font3mfDesc() = default;
};
// represent selected font
// Name must be human readable is visible in gui
@ -26,8 +17,6 @@ struct FontItem
enum class Type;
Type type;
std::optional<Font3mfDesc> font3mf; // description from 3mf
FontItem() : type(Type::undefined){} // set undefined type
FontItem(const std::string &name, const std::string &path, Type type)
: name(name), path(path), type(type)
@ -52,13 +41,12 @@ struct FontProp
int char_gap = 0;
// define extra space between lines, negative mean closer lines
int line_gap = 0;
// Precision of lettter outline curve in conversion to lines
float flatness = 2.0;
// Z depth of text [in mm]
float emboss = 5;
//////
// Duplicit data to wxFontDescriptor
// used for store/load .3mf file
//////
// Height of letter [in mm],
@ -66,7 +54,10 @@ struct FontProp
float size_in_mm = 10;
// Define type of font
// duplicit to wxFont::Family
std::string family = "";
std::optional<std::string> family;
std::optional<std::string> style;
int weight;
std::optional<std::string> encoding;
// TODO: add enum class Align: center/left/right

View File

@ -515,7 +515,8 @@ void GLGizmoEmboss::draw_font_list()
} else {
process();
}
}
} else if (ImGui::IsItemHovered())
ImGui::SetTooltip(f.name.c_str());
// draw buttons rename and delete
ImGui::SameLine();
@ -593,13 +594,10 @@ void GLGizmoEmboss::draw_advanced() {
fi.path = WxFontUtils::store_wxFont(font);
}
load_imgui_font();
process();
}
if (ImGui::InputFloat(_u8L("Emboss[in mm]").c_str(), &m_font_prop.emboss)) process();
if (ImGui::InputFloat(_u8L("Flatness").c_str(), &m_font_prop.flatness)) {
if (m_font.has_value()) m_font->cache.clear();
process();
}
if (ImGui::InputFloat(_u8L("Emboss[in mm]").c_str(), &m_font_prop.emboss)) process();
if (ImGui::InputInt(_u8L("CharGap[in font points]").c_str(), &m_font_prop.char_gap))
process();
if (ImGui::InputInt(_u8L("LineGap[in font points]").c_str(), &m_font_prop.line_gap))
@ -623,7 +621,9 @@ void GLGizmoEmboss::draw_advanced() {
}
}
ImGui::Text("Font family = %s", m_font_prop.family);
ImGui::Text("Font style = %s", (m_font_prop.style.has_value()?m_font_prop.style->c_str() : " --- "));
ImGui::Text("Font weight = %d", m_font_prop.weight);
ImGui::Text("Font family = %s", (m_font_prop.family.has_value()?m_font_prop.family->c_str() : " --- "));
// ImGui::InputFloat3("Origin", m_orientation.origin.data());
// if (ImGui::InputFloat3("Normal", m_normal.data())) m_normal.normalize();
@ -693,12 +693,19 @@ bool GLGizmoEmboss::load_font(size_t font_index)
bool GLGizmoEmboss::load_font() {
if (m_font_selected >= m_font_list.size()) return false;
auto font = WxFontUtils::load_font(m_font_list[m_font_selected]);
if (!font.has_value()) return false;
m_font = font;
auto font_opt = WxFontUtils::load_font(m_font_list[m_font_selected]);
if (!font_opt.has_value()) return false;
m_font = font_opt;
load_imgui_font();
return true;
}
bool GLGizmoEmboss::load_font(const wxFont& font)
{
auto font_opt = WxFontUtils::load_font(font);
if (!font_opt.has_value()) return false;
m_font = font_opt;
load_imgui_font();
return true;
}
@ -747,9 +754,15 @@ void GLGizmoEmboss::load_imgui_font() {
builder.AddText(m_text.c_str());
m_imgui_font_ranges.clear();
builder.BuildRanges(&m_imgui_font_ranges);
int font_size = static_cast<int>(
std::round(std::abs(m_font_prop.size_in_mm / 0.3528)));
if (font_size < m_gui_cfg->min_imgui_font_size)
font_size = m_gui_cfg->min_imgui_font_size;
if (font_size > m_gui_cfg->max_imgui_font_size)
font_size = m_gui_cfg->max_imgui_font_size;
ImFontConfig font_config;
font_config.FontDataOwnedByAtlas = false;
m_imgui_font_atlas.Flags |= ImFontAtlasFlags_NoMouseCursors |
@ -809,17 +822,34 @@ bool GLGizmoEmboss::choose_font_by_wxdialog()
// decide use point size as mm for emboss
m_font_prop.size_in_mm = font.GetPointSize(); // *0.3528f;
wxString faceName = font.GetFaceName();
m_font_prop.family = std::string((const char *) faceName.ToUTF8());
wxString wx_face_name = font.GetFaceName();
std::string family((const char *) wx_face_name.ToUTF8());
if (!family.empty()) m_font_prop.family = family;
wxFontStyle wx_style = font.GetStyle();
std::map<wxFontStyle, std::string> from_style(
{{wxFONTSTYLE_ITALIC, "italic"},
{wxFONTSTYLE_SLANT, "slant"},
{wxFONTSTYLE_MAX, "max"},
{wxFONTSTYLE_NORMAL, "normal"}});
if (wx_style != wxFONTSTYLE_NORMAL)
m_font_prop.style = from_style[wx_style];
m_font_prop.weight = font.GetNumericWeight();
m_font_prop.emboss = m_font_prop.size_in_mm / 2.f;
m_font_prop.flatness = m_font_prop.size_in_mm / 5.f;
m_font_prop.emboss = m_font_prop.size_in_mm / 2.f;
// set activ index of font
std::swap(font_index, m_font_selected);
// Check that deserialization NOT influence font
// false - use direct selected font in dialog
// true - use font item (serialize and deserialize wxFont)
bool use_deserialized_font = false;
// Try load and use new added font
if (!load_font() || !process()) {
if ((!use_deserialized_font && !load_font(font)) ||
(use_deserialized_font && !load_font()) ||
!process()) {
// reverse index for font_selected
std::swap(font_index, m_font_selected);
// remove form font list

View File

@ -59,6 +59,7 @@ private:
bool load_font();
// try to set font_index
bool load_font(size_t font_index);
bool load_font(const wxFont &font);
void load_imgui_font();
void check_imgui_font_range();
@ -83,6 +84,10 @@ private:
{
size_t max_count_char_in_volume_name = 20;
int count_line_of_text = 6;
// limits for font size inside gizmo
// When out of limits no change in size will appear in text input
int min_imgui_font_size = 18;
int max_imgui_font_size = 60;
// Zero means it is calculated in init function
float combo_font_width = 0.f;