| 291 | typename pixel_type |
| 292 | > |
| 293 | void draw_string ( |
| 294 | image_type& image, |
| 295 | const dlib::point& p, |
| 296 | const std::basic_string<T,traits,alloc>& str, |
| 297 | const pixel_type& color, |
| 298 | const std::shared_ptr<font>& f_ptr = default_font::get_font(), |
| 299 | typename std::basic_string<T,traits,alloc>::size_type first = 0, |
| 300 | typename std::basic_string<T,traits,alloc>::size_type last = (std::basic_string<T,traits,alloc>::npos) |
| 301 | ) |
| 302 | { |
| 303 | using string = std::basic_string<T,traits,alloc>; |
| 304 | DLIB_ASSERT((last == string::npos) || (first <= last && last < str.size()), |
| 305 | "\tvoid dlib::draw_string()" |
| 306 | << "\n\tlast == string::npos: " << ((last == string::npos)?"true":"false") |
| 307 | << "\n\tfirst: " << (unsigned long)first |
| 308 | << "\n\tlast: " << (unsigned long)last |
| 309 | << "\n\tstr.size(): " << (unsigned long)str.size()); |
| 310 | |
| 311 | if (last == string::npos) |
| 312 | last = str.size(); |
| 313 | |
| 314 | const rectangle rect(p, p); |
| 315 | const font& f = *f_ptr; |
| 316 | image_view<image_type> img(image); |
| 317 | |
| 318 | long y_offset = rect.top() + f.ascender() - 1; |
| 319 | |
| 320 | long pos = rect.left()+f.left_overflow(); |
| 321 | convert_to_utf32(str.begin() + first, str.begin() + last, [&](unichar ch) |
| 322 | { |
| 323 | // ignore the '\r' character |
| 324 | if (ch == '\r') |
| 325 | return; |
| 326 | |
| 327 | // A combining character should be applied to the previous character, and we |
| 328 | // therefore make one step back. If a combining comes right after a newline, |
| 329 | // then there must be some kind of error in the string, and we don't combine. |
| 330 | if(is_combining_char(ch) && |
| 331 | pos > rect.left() + static_cast<long>(f.left_overflow())) |
| 332 | { |
| 333 | pos -= f[ch].width(); |
| 334 | } |
| 335 | |
| 336 | if (ch == '\n') |
| 337 | { |
| 338 | y_offset += f.height(); |
| 339 | pos = rect.left()+f.left_overflow(); |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | // only look at letters in the intersection area |
| 344 | if (img.nr() + static_cast<long>(f.height()) < y_offset) |
| 345 | { |
| 346 | // the string is now below our rectangle so we are done |
| 347 | return; |
| 348 | } |
| 349 | else if (0 > pos - static_cast<long>(f.left_overflow()) && |
| 350 | pos + static_cast<long>(f[ch].width() + f.right_overflow()) < 0) |