|
15 | 15 | namespace py = pybind11;
|
16 | 16 | using namespace BPrivate;
|
17 | 17 |
|
18 |
| -/* |
19 |
| -struct text_run { |
20 |
| - int32_t offset; |
21 |
| - BFont font; |
22 |
| - rgb_color color; |
23 |
| -}; |
24 |
| -
|
25 |
| -struct text_run_array { |
26 |
| - int32_t count; |
27 |
| - std::vector<text_run> runs; |
28 |
| - text_run_array(int32_t count) : count(count), runs(count) {} |
29 |
| -}; |
30 |
| -*/ |
31 |
| - |
32 | 18 | class PyBTextView : public BTextView{
|
33 | 19 | public:
|
34 | 20 | using BTextView::BTextView;
|
@@ -140,57 +126,38 @@ py::class_<text_run>(m, "text_run")
|
140 | 126 | .def_readwrite("font", &text_run::font, "")
|
141 | 127 | .def_readwrite("color", &text_run::color, "")
|
142 | 128 | ;
|
| 129 | +//tentativo di creare text_run gestito da python, produce lo stesso errori |
| 130 | +/*m.def("create_text_run", []() { return new text_run(); // Python prende proprietà |
| 131 | +}, py::return_value_policy::take_ownership);*/ |
143 | 132 |
|
144 | 133 | py::class_<text_run_array>(m, "text_run_array")
|
145 |
| -.def(py::init<>()) |
| 134 | +//.def(py::init<>()) |
| 135 | +/*init has been commented out because it's safer creating a text_run_array with |
| 136 | +AllocRunArray static function*/ |
146 | 137 | .def_readwrite("count", &text_run_array::count, "")
|
147 | 138 | //.def_readonly("runs", &text_run_array::runs, "")
|
148 |
| -/*first attempt |
149 |
| -.def_property("runs",[](const text_run_array& self){ |
150 |
| - return std::vector<text_run>(self.runs, self.runs + self.text_run_array::count); |
151 |
| - },[](text_run_array& self, const std::vector<text_run>& new_values) { |
152 |
| - if (new_values.size() != self.count){ |
153 |
| - throw std::runtime_error("Invalid buffer size or dimensions"); |
154 |
| - } |
155 |
| - std::copy(new_values.begin(),new_values.end(),self.runs); |
156 |
| - }) |
157 |
| -;*/ |
158 |
| -/* |
159 |
| -.def_property("runs",[](const text_run_array& self){ |
160 |
| - return std::vector<text_run>(self.runs, self.runs + self.text_run_array::count); |
161 |
| - },[](text_run_array& self, const std::vector<text_run>& new_values) { |
162 |
| - if (new_values.size() != self.count){ |
163 |
| - throw std::runtime_error("Invalid buffer size or dimensions"); |
164 |
| - } |
165 |
| - std::memcpy(self.runs, new_values.data(), new_values.size() * sizeof(text_run)); |
166 |
| - //std::copy(new_values.begin(),new_values.end(),self.runs); |
167 |
| - }) |
168 |
| -;*/ |
169 |
| -.def_property("runs",[](const text_run_array& self){ |
| 139 | +//this method works but gives error on delete(rewrite draw?) |
| 140 | +/*.def_property("runs",[](const text_run_array& self){ |
170 | 141 | return std::vector<text_run>(self.runs, self.runs + self.text_run_array::count);
|
171 | 142 | },[](text_run_array& self, const py::list &new_values) {
|
172 | 143 | std::vector<text_run> newruns;
|
173 | 144 | for (auto item : new_values) {
|
174 | 145 | newruns.push_back(item.cast<text_run>());
|
175 | 146 | }
|
176 | 147 | std::memcpy(self.runs, newruns.data(), newruns.size() * sizeof(text_run));
|
177 |
| - }) |
| 148 | + })*/ |
| 149 | +//also this one works but gives errors as above |
| 150 | +.def_property("runs", [](const text_run_array& self) -> py::list { |
| 151 | + py::list result; for (int i = 0; i < self.count; ++i) { |
| 152 | + result.append(self.runs[i]); |
| 153 | + } return result; |
| 154 | +}, [](text_run_array& self, const py::list& new_values) { |
| 155 | + int i = 0; for (auto item : new_values) { |
| 156 | + if (i >= self.count) break; // Evita overflow |
| 157 | + self.runs[i] = item.cast<text_run>(); ++i; |
| 158 | + } |
| 159 | +}) |
178 | 160 | ;
|
179 |
| -/* look at this |
180 |
| -/*suggested attempt |
181 |
| -py::class_<text_run_array>(m, "text_run_array") |
182 |
| -.def(py::init<int32_t>(), py::arg("count") = 1) |
183 |
| -.def_readwrite("count", &text_run_array::count) |
184 |
| -.def_property("runs", [](const text_run_array& self) { |
185 |
| - return self.runs; |
186 |
| - }, |
187 |
| - [](text_run_array& self, const std::vector<text_run>& new_values) { |
188 |
| - if (new_values.size() != self.count) { |
189 |
| - throw std::runtime_error("Invalid buffer size or dimensions"); |
190 |
| - } |
191 |
| - self.runs = new_values; |
192 |
| - }) |
193 |
| -;*/ |
194 | 161 | /*
|
195 | 162 | py::class_<text_run_array>(m, "text_run_array")
|
196 | 163 | .def_readwrite("count", &text_run_array::count, "")
|
@@ -223,6 +190,18 @@ py::class_<BTextView, PyBTextView, BView, std::unique_ptr<BTextView,py::nodelete
|
223 | 190 | .def("SetText", py::overload_cast<const char *, const text_run_array *>(&BTextView::SetText), "", py::arg("text"), py::arg("runs")=NULL)
|
224 | 191 | .def("SetText", py::overload_cast<const char *, int32, const text_run_array *>(&BTextView::SetText), "", py::arg("text"), py::arg("length"), py::arg("runs")=NULL)
|
225 | 192 | .def("SetText", py::overload_cast<BFile *, int32, int32, const text_run_array *>(&BTextView::SetText), "", py::arg("file"), py::arg("offset"), py::arg("length"), py::arg("runs")=NULL)
|
| 193 | +.def("SetText", [](BTextView& self, const char* text, const py::list& runs){//&SetTextWrapper, "", py::arg("text"), py::arg("runs")=NULL) |
| 194 | + if (!runs.is_none()) { |
| 195 | + auto len = runs.size(); |
| 196 | + text_run_array* tra = BTextView::AllocRunArray(len); |
| 197 | + int i = 0; for (auto item : runs) { |
| 198 | + if (i >= len) break; // Evita overflow |
| 199 | + tra->runs[i] = item.cast<text_run>(); ++i; |
| 200 | + } |
| 201 | + self.SetText(text, tra); |
| 202 | + BTextView::FreeRunArray(tra); |
| 203 | + } |
| 204 | +}, "", py::arg("text"), py::arg("runs")) |
226 | 205 | .def("Insert", py::overload_cast<const char *, const text_run_array *>(&BTextView::Insert), "", py::arg("text"), py::arg("runs")=NULL)
|
227 | 206 | .def("Insert", py::overload_cast<const char *, int32, const text_run_array *>(&BTextView::Insert), "", py::arg("text"), py::arg("length"), py::arg("runs")=NULL)
|
228 | 207 | .def("Insert", py::overload_cast<int32, const char *, int32, const text_run_array *>(&BTextView::Insert), "", py::arg("offset"), py::arg("text"), py::arg("length"), py::arg("runs")=NULL)
|
|
0 commit comments