OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::GUI::Emboss::CreateFontStyleImagesJob Class Reference

Create texture with name of styles written by its style NOTE: Access to glyph cache is possible only from job. More...

#include </home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/GUI/Jobs/CreateFontStyleImagesJob.hpp>

Inheritance diagram for Slic3r::GUI::Emboss::CreateFontStyleImagesJob:
Collaboration diagram for Slic3r::GUI::Emboss::CreateFontStyleImagesJob:

Public Types

enum  JobPrepareState { PREPARE_STATE_DEFAULT = 0 , PREPARE_STATE_MENU = 1 }

Public Member Functions

 CreateFontStyleImagesJob (StyleManager::StyleImagesData &&input)
void process (Ctl &ctl) override
void finalize (bool canceled, std::exception_ptr &) override

Private Attributes

StyleManager::StyleImagesData m_input
int m_width
int m_height
std::vector< unsigned char > m_pixels
std::vector< StyleManager::StyleImage > m_images

Detailed Description

Create texture with name of styles written by its style NOTE: Access to glyph cache is possible only from job.

Member Enumeration Documentation

◆ JobPrepareState

Enumerator
PREPARE_STATE_DEFAULT 
PREPARE_STATE_MENU 
22 {
25 };
@ PREPARE_STATE_MENU
Definition Job.hpp:24
@ PREPARE_STATE_DEFAULT
Definition Job.hpp:23

Constructor & Destructor Documentation

◆ CreateFontStyleImagesJob()

CreateFontStyleImagesJob::CreateFontStyleImagesJob ( StyleManager::StyleImagesData && input)
30 : m_input(std::move(input)), m_width(0), m_height(0)
31{
32 assert(m_input.result != nullptr);
33 assert(!m_input.styles.empty());
34 assert(!m_input.text.empty());
35 assert(m_input.max_size.x() > 1);
36 assert(m_input.max_size.y() > 1);
37 assert(m_input.ppm > 1e-5);
38}
StyleManager::StyleImagesData m_input
Definition CreateFontStyleImagesJob.hpp:18
int m_height
Definition CreateFontStyleImagesJob.hpp:22
int m_width
Definition CreateFontStyleImagesJob.hpp:22

References m_height, m_input, and m_width.

Member Function Documentation

◆ finalize()

void CreateFontStyleImagesJob::finalize ( bool canceled,
std::exception_ptr &  )
overridevirtual

Reimplemented from Slic3r::GUI::Job.

137{
138 if (canceled) return;
139 // upload texture on GPU
140 GLuint tex_id;
142 GLint level = 0, border = 0;
143 glsafe(::glGenTextures(1, &tex_id));
144 glsafe(::glBindTexture(target, tex_id));
147 GLint w = m_width, h = m_height;
148 glsafe(::glTexImage2D(target, level, GL_RGBA, w, h, border, format, type,
149 (const void *) m_pixels.data()));
150
151 // set up texture id
152 void *texture_id = (void *) (intptr_t) tex_id;
153 for (StyleManager::StyleImage &image : m_images)
154 image.texture_id = texture_id;
155
156 // move to result
157 m_input.result->styles = std::move(m_input.styles);
158 m_input.result->images = std::move(m_images);
159
160 // bind default texture
161 GLuint no_texture_id = 0;
162 glsafe(::glBindTexture(target, no_texture_id));
163
164 // show rendered texture
165 wxGetApp().plater()->canvas3D()->schedule_extra_frame(0);
166}
#define glsafe(cmd)
Definition OpenGLUtils.hpp:25
std::vector< StyleManager::StyleImage > m_images
Definition CreateFontStyleImagesJob.hpp:26
std::vector< unsigned char > m_pixels
Definition CreateFontStyleImagesJob.hpp:24
#define GL_TEXTURE_MIN_FILTER
Definition gl.h:1828
#define glTexParameteri
Definition gl.h:4911
int GLint
Definition gl.h:2062
#define glTexImage2D
Definition gl.h:4895
#define GL_NEAREST
Definition gl.h:1200
unsigned int GLenum
Definition gl.h:2054
#define GL_UNSIGNED_BYTE
Definition gl.h:1937
#define glGenTextures
Definition gl.h:3787
#define glBindTexture
Definition gl.h:3299
#define GL_RGBA
Definition gl.h:1493
unsigned int GLuint
Definition gl.h:2063
#define GL_TEXTURE_MAG_FILTER
Definition gl.h:1822
#define GL_TEXTURE_2D
Definition gl.h:1739
std::string format(const char *fmt, TArgs &&... args)
Definition format.hpp:36

References Slic3r::GUI::format(), GL_NEAREST, GL_RGBA, GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_TEXTURE_MIN_FILTER, GL_UNSIGNED_BYTE, glBindTexture, glGenTextures, glsafe, glTexImage2D, glTexParameteri, m_height, m_images, m_input, m_pixels, and m_width.

Here is the call graph for this function:

◆ process()

void CreateFontStyleImagesJob::process ( Ctl & ctl)
overridevirtual

Implements Slic3r::GUI::Job.

41{
42 // create shapes and calc size (bounding boxes)
43 std::vector<ExPolygons> name_shapes(m_input.styles.size());
44 std::vector<double> scales(m_input.styles.size());
45 m_images = std::vector<StyleManager::StyleImage>(m_input.styles.size());
46
47 auto was_canceled = []() { return false; };
48 for (auto &item : m_input.styles) {
49 size_t index = &item - &m_input.styles.front();
50 ExPolygons &shapes = name_shapes[index];
51 shapes = text2shapes(item.font, m_input.text.c_str(), item.prop, was_canceled);
52
53 // create image description
54 StyleManager::StyleImage &image = m_images[index];
55 BoundingBox &bounding_box = image.bounding_box;
56 for (const ExPolygon &shape : shapes)
57 bounding_box.merge(BoundingBox(shape.contour.points));
58 for (ExPolygon &shape : shapes) shape.translate(-bounding_box.min);
59
60 if (bounding_box.size().x() < 1 || bounding_box.size().y() < 1)
61 continue; // or however the font job's degenerate-box case is handled
62
63 // Normalize to fit max_size, exactly like CreateFontImageJob does against m_input.size.
64 // Fit by height (matches row height), then clamp width if needed.
65 constexpr float preview_padding_px = 2.f; // margin for AA sampling, tune to your AA kernel radius
66
67 double scale = m_input.max_size.y() / (double) bounding_box.size().y();
68 BoundingBoxf bb2(bounding_box.min.cast<double>(), bounding_box.max.cast<double>());
69 bb2.scale(scale);
70
71 // crop width only if the (now height-normalized) text is too wide
72 image.tex_size.x = std::ceil(bb2.max.x() - bb2.min.x()) + 2 * preview_padding_px;
73 image.tex_size.y = std::ceil(bb2.max.y() - bb2.min.y()) + 2 * preview_padding_px;
74
75 if (image.tex_size.x > m_input.max_size.x())
76 image.tex_size.x = m_input.max_size.x();
77
78 scales[index] = scale;
79 }
80
81 // arrange bounding boxes
82 int offset_y = 0;
83 m_width = 0;
84 for (StyleManager::StyleImage &image : m_images) {
85 image.offset.y() = offset_y;
86 offset_y += image.tex_size.y+1;
87 if (m_width < image.tex_size.x)
88 m_width = image.tex_size.x;
89 }
90 m_height = offset_y;
91 for (StyleManager::StyleImage &image : m_images) {
92 const Point &o = image.offset;
93 const ImVec2 &s = image.tex_size;
94 image.uv0 = ImVec2(o.x() / (double) m_width,
95 o.y() / (double) m_height);
96 image.uv1 = ImVec2((o.x() + s.x) / (double) m_width,
97 (o.y() + s.y) / (double) m_height);
98 }
99
100 // Set up result
101 m_pixels = std::vector<unsigned char>(4 * m_width * m_height, {255});
102
103 // upload sub textures
104 for (StyleManager::StyleImage &image : m_images) {
105 sla::Resolution resolution(image.tex_size.x, image.tex_size.y);
106 size_t index = &image - &m_images.front();
107 double pixel_dim = SCALING_FACTOR / scales[index];
108 sla::PixelDim dim(pixel_dim, pixel_dim);
109 double gamma = 1.;
110 std::unique_ptr<sla::RasterBase> r =
112 for (const ExPolygon &shape : name_shapes[index]) r->draw(shape);
113
114 // copy rastered data to pixels
115 sla::RasterEncoder encoder = [&offset = image.offset, &pix = m_pixels, w=m_width,h=m_height]
116 (const void *ptr, size_t width, size_t height, size_t num_components) {
117 // bigger value create darker image
118 unsigned char gray_level = 1;
119 size_t size {static_cast<size_t>(w*h)};
120 assert((offset.x() + width) <= (size_t)w);
121 assert((offset.y() + height) <= (size_t)h);
122 const unsigned char *ptr2 = (const unsigned char *) ptr;
123 for (size_t x = 0; x < width; ++x)
124 for (size_t y = 0; y < height; ++y) {
125 size_t index = (offset.y() + y)*w + offset.x() + x;
126 assert(index < size);
127 if (index >= size) continue;
128 pix[4*index+3] = ptr2[y * width + x] / gray_level;
129 }
130 return sla::EncodedRaster();
131 };
132 r->encode(encoder);
133 }
134}
int scale(const int val)
Definition WipeTowerDialog.cpp:58
PointType size() const
Definition BoundingBox.cpp:187
void merge(const PointType &point)
Definition BoundingBox.cpp:139
ImVec2 tex_size
Definition EmbossStyleManager.hpp:186
ImVec2 uv0
Definition EmbossStyleManager.hpp:187
Point offset
Definition EmbossStyleManager.hpp:189
ImVec2 uv1
Definition EmbossStyleManager.hpp:188
BoundingBox bounding_box
Definition EmbossStyleManager.hpp:185
double SCALING_FACTOR
Definition libslic3r.cpp:3
HealedExPolygons text2shapes(FontFileWithCache &font, const char *text, const FontProp &font_prop, const std::function< bool()> &was_canceled=[]() {return false;})
Convert text into polygons.
Definition Emboss.cpp:1388
Kernel::Point_3 Point
Definition MeshRepair.cpp:81
coord_t width(const BoundingBox &box)
Definition Arrange.cpp:1184
coord_t height(const BoundingBox &box)
Definition Arrange.cpp:1185
std::function< EncodedRaster(const void *ptr, size_t w, size_t h, size_t num_components)> RasterEncoder
Definition RasterBase.hpp:59
std::unique_ptr< RasterBase > create_raster_grayscale_aa(const Resolution &res, const PixelDim &pxdim, double gamma, const RasterBase::Trafo &tr)
Definition RasterBase.cpp:77
std::vector< ExPolygon > ExPolygons
Definition ExPolygon.hpp:20
BoundingBoxf3 bounding_box(const TriangleMesh &m)
Definition TriangleMesh.hpp:391
zaa_minimize_perimeter_height resolution((ConfigOptionFloats, retraction_minimum_travel))((ConfigOptionBools
Slic3r::Polygons offset(const Slic3r::Polygon &polygon, const float delta, JoinType joinType, double miterLimit)
Definition ClipperUtils.cpp:582
auto was_canceled(const Job::Ctl &ctl, const DataBase &base)
Definition EmbossJob.cpp:292

References Slic3r::bounding_box(), Slic3r::GUI::Emboss::StyleManager::StyleImage::bounding_box, Slic3r::sla::create_raster_grayscale_aa(), m_height, m_images, m_input, m_pixels, m_width, Slic3r::BoundingBoxBase< PointType, APointsType >::max, Slic3r::BoundingBoxBase< PointType, APointsType >::min, Slic3r::offset(), Slic3r::resolution(), scale(), Slic3r::BoundingBoxBase< PointType, APointsType >::scale(), SCALING_FACTOR, and Slic3r::GUI::Emboss::StyleManager::StyleImage::tex_size.

Here is the call graph for this function:

Member Data Documentation

◆ m_height

int Slic3r::GUI::Emboss::CreateFontStyleImagesJob::m_height
private

◆ m_images

std::vector<StyleManager::StyleImage> Slic3r::GUI::Emboss::CreateFontStyleImagesJob::m_images
private

Referenced by finalize(), and process().

◆ m_input

StyleManager::StyleImagesData Slic3r::GUI::Emboss::CreateFontStyleImagesJob::m_input
private

◆ m_pixels

std::vector<unsigned char> Slic3r::GUI::Emboss::CreateFontStyleImagesJob::m_pixels
private

Referenced by finalize(), and process().

◆ m_width

int Slic3r::GUI::Emboss::CreateFontStyleImagesJob::m_width
private

The documentation for this class was generated from the following files: