1111
1212#include " TextField.hpp"
1313#include " ../UIContext.hpp"
14+ #include " ../font/Font.hpp"
1415#include " ../rendering/UIBatchRenderer.hpp"
1516#include " ../../core/Log.hpp"
1617#include " ../../math/Math.hpp"
@@ -499,7 +500,7 @@ void TextField::cutToClipboard() {
499500}
500501
501502usize TextField::getCharIndexAtX (f32 x) const {
502- if (!getContext () || ! getContext ()-> getDefaultFont () ) {
503+ if (!getContext ()) {
503504 return 0 ;
504505 }
505506
@@ -512,14 +513,19 @@ usize TextField::getCharIndexAtX(f32 x) const {
512513 return 0 ;
513514 }
514515
515- f32 fontSize = 14 . 0f ;
516- if ( getContext ()) {
517- fontSize = getContext ()-> getTheme (). typography . fontSizeNormal ;
518- }
516+ f32 fontSize = getContext ()-> getTheme (). typography . fontSizeNormal ;
517+
518+ // Use regular font for character measurement
519+ Font* font = getContext ()-> getDefaultFont ();
519520
520521 f32 currentX = 0 .0f ;
521522 for (usize i = 0 ; i < text_.size (); ++i) {
522- f32 charWidth = fontSize * 0 .6f ;
523+ f32 charWidth;
524+ if (font) {
525+ charWidth = font->getCharWidth (static_cast <u32 >(text_[i]), fontSize);
526+ } else {
527+ charWidth = fontSize * 0 .6f ;
528+ }
523529
524530 if (relativeX < currentX + charWidth * 0 .5f ) {
525531 return i;
@@ -536,9 +542,17 @@ f32 TextField::getXForCharIndex(usize index) const {
536542 return 0 .0f ;
537543 }
538544
539- f32 fontSize = 14 .0f ;
540- if (getContext ()) {
541- fontSize = getContext ()->getTheme ().typography .fontSizeNormal ;
545+ f32 fontSize = getContext ()->getTheme ().typography .fontSizeNormal ;
546+
547+ // Use regular font for character measurement
548+ Font* font = getContext ()->getDefaultFont ();
549+
550+ if (font) {
551+ f32 currentX = 0 .0f ;
552+ for (usize i = 0 ; i < index && i < text_.size (); ++i) {
553+ currentX += font->getCharWidth (static_cast <u32 >(text_[i]), fontSize);
554+ }
555+ return currentX;
542556 }
543557
544558 f32 charWidth = fontSize * 0 .6f ;
0 commit comments