Skip to content

Commit a0a4555

Browse files
committed
Fix GetBoundingBoxesForStrings
1 parent 20e37c7 commit a0a4555

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

bindings/interface/Font.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace py = pybind11;
1515

1616

17+
1718
PYBIND11_MODULE(Font,m)
1819
{
1920
m.attr("B_CHAR_SPACING") = 0;
@@ -236,8 +237,8 @@ py::class_<BFont>(m, "BFont")
236237
.def("GetBoundingBoxesAsGlyphs", &BFont::GetBoundingBoxesAsGlyphs, "", py::arg("charArray"), py::arg("numChars"), py::arg("mode"), py::arg("boundingBoxArray"))
237238
.def("GetBoundingBoxesAsString", &BFont::GetBoundingBoxesAsString, "", py::arg("charArray"), py::arg("numChars"), py::arg("mode"), py::arg("delta"), py::arg("boundingBoxArray"))
238239
//.def("GetBoundingBoxesForStrings", &BFont::GetBoundingBoxesForStrings, "", py::arg("stringArray"), py::arg("numStrings"), py::arg("mode"), py::arg("deltas"), py::arg("boundingBoxArray"))
239-
// TODO TEST THIS, it returns empty boundingBox array DON'T WORK
240-
.def("GetBoundingBoxesForStrings", [](const BFont &self, const std::vector<std::string> &stringArray,
240+
// TODO TEST THIS, it returns empty boundingBox array COMPILES BUT DON'T WORK
241+
/*.def("GetBoundingBoxesForStrings", [](const BFont &self, const std::vector<std::string> &stringArray,
241242
int32 numStrings, font_metric_mode mode,
242243
std::vector<escapement_delta> &deltas,
243244
std::vector<BRect> &boundingBoxArray) {
@@ -250,22 +251,22 @@ py::class_<BFont>(m, "BFont")
250251
std::vector<BRect>& constBoundingBoxArray = boundingBoxArray;
251252
252253
self.GetBoundingBoxesForStrings(cStrings.data(), numStrings, mode, constDeltas.data(), constBoundingBoxArray.data());
253-
},"",py::arg("stringArray"),py::arg("numStrings"),py::arg("mode"),py::arg("deltas"),py::arg("boundingBoxArray"))
254-
/*.def("GetBoundingBoxesForStrings", [](const BFont &self, const std::vector<std::string> &stringArray,
255-
int32 numStrings, font_metric_mode mode,
256-
std::vector<escapement_delta> &deltas) {
257-
std::vector<BRect> boundingBoxArray;
258-
std::vector<const char*> cStrings;
259-
for (const auto &str : stringArray) {
260-
cStrings.push_back(str.c_str());
254+
},"",py::arg("stringArray"),py::arg("numStrings"),py::arg("mode"),py::arg("deltas"),py::arg("boundingBoxArray"))*/
255+
.def("GetBoundingBoxesForStrings", [](const BFont &self, const std::vector<std::string> &stringArray, font_metric_mode mode) {
256+
std::vector<escapement_delta> deltas(stringArray.size());
257+
std::vector<BRect> boundingBoxArray(stringArray.size());
258+
259+
// Converte i vettori Python in array C
260+
const char** cStringArray = new const char*[stringArray.size()];
261+
for (size_t i = 0; i < stringArray.size(); ++i) {
262+
cStringArray[i] = stringArray[i].c_str();
261263
}
262264

263-
std::vector<escapement_delta>& constDeltas = deltas;
264-
// std::vector<BRect>& constBoundingBoxArray = boundingBoxArray;
265+
self.GetBoundingBoxesForStrings(cStringArray, stringArray.size(), mode, deltas.data(), boundingBoxArray.data());
265266

266-
self.GetBoundingBoxesForStrings(cStrings.data(), numStrings, mode, constDeltas.data(), boundingBoxArray.data());
267-
return boundingBoxArray;
268-
},"",py::arg("stringArray"),py::arg("numStrings"),py::arg("mode"),py::arg("deltas"))*/
267+
delete[] cStringArray;
268+
return std::make_tuple(std::move(deltas), std::move(boundingBoxArray));
269+
},"",py::arg("stringArray"),py::arg("mode"))
269270
//.def("GetGlyphShapes", &BFont::GetGlyphShapes, "", py::arg("charArray"), py::arg("numChars"), py::arg("glyphShapeArray"))
270271
.def("GetGlyphShapes", [](BFont& self, const std::string& charArray){
271272
py::list glyphShapeList;

tmtest.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,19 @@ def __init__(self):
372372
print(a,b)
373373
fen.SetFamilyAndStyle(a,b)
374374
#### test getboundingboxesforStrings
375-
arstr=["test1","prova2","attempt3"]
376-
boxarr=[]
375+
arstr=["test 1","prova 2","attempt 3"]
376+
#boxarr=[]
377377
esc1=escapement_delta()
378378
esc1.nonspace=0
379379
esc1.space=0
380-
fen.GetBoundingBoxesForStrings(arstr,len(arstr),font_metric_mode.B_SCREEN_METRIC,[esc1],boxarr)
381-
print(boxarr)
380+
#fen.GetBoundingBoxesForStrings(arstr,len(arstr),font_metric_mode.B_SCREEN_METRIC,[esc1],boxarr)
381+
o,u=fen.GetBoundingBoxesForStrings(arstr,font_metric_mode.B_SCREEN_METRIC)
382+
print(o,u)
383+
for kld in o:
384+
print(kld.nonspace,kld.space)
385+
for z in u:
386+
print(z.PrintToStream())
387+
#print(boxarr)
382388
### test getescapements
383389
#resesc=[]
384390
#retesc=fen.GetEscapements("gnû gno",3,resesc)
@@ -388,7 +394,7 @@ def __init__(self):
388394
### test gettruncatedstrings
389395
#out1 = fen.GetTruncatedStrings(arstr,B_TRUNCATE_MIDDLE,100)
390396
out2=[]
391-
out1 = fen.GetTruncatedStrings(arstr,B_TRUNCATE_MIDDLE,100,out2)
397+
out1 = fen.GetTruncatedStrings(arstr,B_TRUNCATE_MIDDLE,100)#,out2)
392398
print(out1,out2)
393399
for sturinga in out1:
394400
print("Stringa:",sturinga.String())

0 commit comments

Comments
 (0)