Add more error checking, correct caret positioning

This commit is contained in:
Henrik Rydgård committed 2026-04-27 21:08:40 +02:00
1 parent a147e1644b
commit b02c184829
2 files changed
+50 -1

No files matched your search

+45
View File
@@ -237,9 +237,23 @@ bool TextDrawerCocoa::DrawStringBitmap(std::vector<uint8_t> &bitmapData, TextStr
NSString* string = [[NSString alloc] initWithBytes:str.data() length:str.length() encoding: NSUTF8StringEncoding];
// If UTF-8 fails, fallback to ASCII to prevent nil
if (!string) {
string = [[NSString alloc] initWithBytes:str.data() length:str.length() encoding:NSASCIIStringEncoding];
}
// Skip if string creation failed
if (!string) {
return false;
}
NSDictionary* attributes = iter->second->attributes;
NSAttributedString* as = [[NSAttributedString alloc] initWithString:string attributes:attributes];
// Safety check for AttributedString allocation
if (!as) {
return false;
}
// Figure out how big an image we need.
// We re-use MeasureString here.
float w, h;
@@ -267,6 +281,14 @@ bool TextDrawerCocoa::DrawStringBitmap(std::vector<uint8_t> &bitmapData, TextStr
CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast;
CGContextRef ctx = CGBitmapContextCreate(bitmap, bmWidth, bmHeight, 8, bmWidth*4, space, bitmapInfo);
CGColorSpaceRelease(space);
// Safety check for CGContext creation
if (!ctx) {
WARN_LOG(Log::G3D, "Failed to create CGBitmapContext");
delete [] bitmap;
return false;
}
// CGContextSetRGBStrokeColor(ctx, 1.0, 1.0, 1.0, 1.0); // white background
CGContextSetStrokeColorWithColor(ctx, [ColorType whiteColor].CGColor);
CGContextSetFillColorWithColor(ctx, [ColorType whiteColor].CGColor);
@@ -277,8 +299,30 @@ bool TextDrawerCocoa::DrawStringBitmap(std::vector<uint8_t> &bitmapData, TextStr
float lineY = 0.0;
for (std::string_view line : lines) {
NSString *string = [[NSString alloc] initWithBytes:line.data() length:line.size() encoding: NSUTF8StringEncoding];
// If UTF-8 fails, fallback to ASCII to prevent nil
if (!string) {
string = [[NSString alloc] initWithBytes:line.data() length:line.size() encoding:NSASCIIStringEncoding];
}
// Skip empty or failed strings to prevent crash
if (!string) {
continue;
}
NSAttributedString* as = [[NSAttributedString alloc] initWithString:string attributes:attributes];
// Safety check for AttributedString allocation
if (!as) {
continue;
}
CTLineRef ctline = CTLineCreateWithAttributedString((CFAttributedStringRef)as);
// Core Text safety check
if (!ctline) {
continue;
}
CGFloat ascent, descent, leading;
double fWidth = CTLineGetTypographicBounds(ctline, &ascent, &descent, &leading);
@@ -349,6 +393,7 @@ bool TextDrawerCocoa::DrawStringBitmap(std::vector<uint8_t> &bitmapData, TextStr
_assert_msg_(false, "Bad TextDrawer format");
}
CGContextRelease(ctx);
delete [] bitmap;
return true;
}
+5 -1
View File
@@ -1298,13 +1298,17 @@ void TextEdit::Draw(UIContext &dc) {
if (selectAtX_ >= 0) {
caret_ = -1;
for (int i = 0; i <= text_.size(); i++) {
for (int i = 0; i <= text_.size(); ) {
dc.MeasureText(dc.GetTheme().uiFont, 1.0f, 1.0f, textToDisplay.substr(0, i), &w, &h, ALIGN_VCENTER | ALIGN_LEFT | align_);
float charX = w - scrollPos_;
if (charX >= selectAtX_ - 3) {
caret_ = i;
break;
}
if (i >= text_.size()) {
break;
}
u8_inc(text_.c_str(), &i);
}
if (caret_ == -1) {
caret_ = (int)text_.size();