#ifndef slic3r_Emboss_hpp_ #define slic3r_Emboss_hpp_ #include #include #include #include #include // indexed_triangle_set #include "Polygon.hpp" namespace Slic3r { /// /// class with only static function add ability to engraved OR raised /// text OR polygons onto model surface /// class Emboss { public: Emboss() = delete; struct FontItem { std::string name; std::string path; enum class Type; Type type; FontItem(const std::string &name, const std::string &path); FontItem(const std::string &name, const std::string &path, Type type); FontItem(const std::wstring &name, const std::wstring &path); // way of load font described in path string enum class Type { file_path, // path is file loacation on computer - no move between computers wx_font_descr // path is font descriptor generated by wxWidgets - limits for os/language move }; }; using FontList = std::vector; /// /// Collect fonts registred inside OS /// /// OS resistred TTF font files(full path) with names static FontList get_font_list(); #ifdef _WIN32 static FontList get_font_list_by_register(); static FontList get_font_list_by_enumeration(); static FontList get_font_list_by_folder(); #endif /// /// OS dependent function to get location of font by its name descriptor /// /// Unique identificator for font /// File path to font when found static std::optional get_font_path(const std::wstring &font_face_name); // user defined font property struct FontProp { // define extra space between letters, negative mean closer letter 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; // TODO: add enum class Align: center/left/right FontProp() = default; }; // description of one letter struct Glyph { Polygons polygons; int advance_width, left_side_bearing; }; // cache for glyph by unicode using Glyphs = std::map; /// /// keep information from file about font /// struct Font { // loaded data from font file std::vector buffer; unsigned int index = 0; // index of actual file info in collection unsigned int count = 0; // count of fonts in file collection // vertical position is "scale*(ascent - descent + lineGap)" int ascent = 0, descent = 0, linegap = 0; Emboss::Glyphs cache; // cache of glyphs }; /// /// Load font file into buffer /// /// Location of .ttf or .ttc font file /// Font object when loaded. static std::optional load_font(const char *file_path); // data = raw file data static std::optional load_font(std::vector data); #ifdef _WIN32 // fix for unknown pointer HFONT using HFONT = void*; static std::optional load_font(HFONT hfont); #endif // _WIN32 /// /// convert letter into polygons /// /// Define fonts /// One character defined by unicode codepoint /// Precision of lettter outline curve in conversion to lines /// inner polygon cw(outer ccw) static std::optional letter2glyph(const Font &font, int letter, float flatness); /// /// Convert text into polygons /// /// Define fonts + cache, which could extend /// Characters to convert /// User defined property of the font /// Inner polygon cw(outer ccw) static Polygons text2polygons(Font & font, const char * text, const FontProp &font_prop); /// /// Project 2d point into space /// Could be plane, sphere, cylindric, ... /// class IProject { public: virtual ~IProject() = default; /// /// convert 2d point to 3d point /// /// 2d coordinate /// /// first - front spatial point /// second - back spatial point /// virtual std::pair project(const Point &p) const = 0; }; /// /// Create triangle model for text /// /// text or image /// Define transformation from 2d to 3d(orientation, position, scale, ...) /// Projected shape into space static indexed_triangle_set polygons2model(const Polygons &shape2d, const IProject& projection); class ProjectZ : public IProject { public: ProjectZ(float depth) : m_depth(depth) {} // Inherited via IProject virtual std::pair project(const Point &p) const override; float m_depth; }; class ProjectScale : public IProject { std::unique_ptr core; public: ProjectScale(std::unique_ptr core, float scale) : m_scale(scale) , core(std::move(core)) {} // Inherited via IProject virtual std::pair project(const Point &p) const override { auto res = core->project(p); return std::make_pair(res.first * m_scale, res.second * m_scale); } float m_scale; }; }; } // namespace Slic3r #endif // slic3r_Emboss_hpp_