Skip to content

Commit 90889c7

Browse files
committed
Attempt to fix text_run_array style
1 parent f0f01ae commit 90889c7

File tree

2 files changed

+64
-13
lines changed

2 files changed

+64
-13
lines changed

bindings/interface/TextView.cpp

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@
1515
namespace py = pybind11;
1616
using namespace BPrivate;
1717

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+
1832
class PyBTextView : public BTextView{
1933
public:
2034
using BTextView::BTextView;
@@ -131,6 +145,7 @@ py::class_<text_run_array>(m, "text_run_array")
131145
.def(py::init<>())
132146
.def_readwrite("count", &text_run_array::count, "")
133147
//.def_readonly("runs", &text_run_array::runs, "")
148+
/*first attempt
134149
.def_property("runs",[](const text_run_array& self){
135150
return std::vector<text_run>(self.runs, self.runs + self.text_run_array::count);
136151
},[](text_run_array& self, const std::vector<text_run>& new_values) {
@@ -139,8 +154,43 @@ py::class_<text_run_array>(m, "text_run_array")
139154
}
140155
std::copy(new_values.begin(),new_values.end(),self.runs);
141156
})
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){
170+
return std::vector<text_run>(self.runs, self.runs + self.text_run_array::count);
171+
},[](text_run_array& self, const py::list &new_values) {
172+
std::vector<text_run> newruns;
173+
for (auto item : new_values) {
174+
newruns.push_back(item.cast<text_run>());
175+
}
176+
std::memcpy(self.runs, newruns.data(), newruns.size() * sizeof(text_run));
177+
})
142178
;
143-
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+
;*/
144194
/*
145195
py::class_<text_run_array>(m, "text_run_array")
146196
.def_readwrite("count", &text_run_array::count, "")

tmtest.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
from Be import BEntry
1515
from Be.Entry import entry_ref
1616
from Be.Entry import get_ref_for_path
17-
17+
from Be.TextView import text_run,text_run_array
18+
from Be.View import set_font_mask
19+
B_FONT_ALL = set_font_mask.B_FONT_ALL
1820

1921
class PBox(BBox):
2022
def __init__(self,frame,name,immagine):
@@ -317,9 +319,6 @@ def __init__(self):
317319
self.typtap.SetText(stuff,None)#, [(0, be_plain_font, (0, 0, 0, 0)), (n, be_bold_font, (0, 150, 0, 0)), (n + 14, be_plain_font, (0, 0, 0, 0)),(m,be_plain_font,(100,150,0,0))])
318320
self.AddChild(self.bckgnd,None)
319321
tra=self.typtap.RunArray(0,len(self.typtap.Text()))
320-
#print("runarray count",tra.count)
321-
#print("tra.runs è\n",tra.runs)
322-
#print(tra.runs[0].offset,tra.runs[0].font,tra.runs[0].color)
323322
self.panel.AddChild(self.list.topview(),None)
324323
pittura=rgb_color()
325324
pittura.red=255
@@ -331,21 +330,23 @@ def __init__(self):
331330
pictura.green=255
332331
pictura.blue=0
333332
pictura.alpha=255
334-
from Be.TextView import text_run
333+
335334
tr1=text_run()
336335
tr1.offset=0
337-
tr1.font=be_plain_font
336+
tr1.font=BFont(be_plain_font)
338337
tr1.color=pittura
339338
tr2=text_run()
340-
tr2.offset=10
341-
tr2.font=be_bold_font
339+
tr2.offset=20
340+
tr2.font=BFont(be_bold_font)
342341
tr2.color=pictura
343342
mytralist=[tr1,tr2]
344-
#this is the way to write text_run_arrays
345-
tra.count=2 #increase the count
346-
#print("ora tra.count è 2")
347-
#print("e ora tra.runs è\n",tra.runs) #now you have 2 text_runs
343+
tra.count=2 #now you have 2 text_runs
348344
tra.runs[1] = tr2 #assign the second one
345+
self.typtap.SetRunArray(0,len(self.typtap.Text()),tra) #this doesn't work
346+
trb=text_run_array()
347+
trb.count=2
348+
trb.runs=mytralist
349+
self.typtap.SetRunArray(0,len(self.typtap.Text()),trb) #this works, why error exiting and why does trb need tra (or it won't work)
349350
#print(tra.runs[1].color.green)
350351
###################### add other text_runs
351352
###### Example handling refs / BEntry #####

0 commit comments

Comments
 (0)