Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextComponent.cpp
More file actions
Latest commit
688 lines (599 loc) · 23.6 KB
/
Copy pathTextComponent.cpp
File metadata and controls
688 lines (599 loc) · 23.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#include"TextComponent.h"
#include"PixelFormat.h"
#include"DekiObject.h"
#include"DekiEngine.h"
#include"deki-rendering/CameraComponent.h"
#include"DekiLogSystem.h"
#include"deki-rendering/QuadBlit.h"
#include"profiling/DekiProfiler.h"
#include"Sprite.h"// for reading chroma-key fields off the font atlas
#include<cstring>
#include<vector>
#include<iomanip>
#include<unordered_map>
// Font resolve callback — set by editor to handle GUID sync, preview, baking
TextComponent::FontResolveCallback TextComponent::s_fontResolveCallback = nullptr;
voidTextComponent::SetFontResolveCallback(FontResolveCallback cb) { s_fontResolveCallback = cb; }
// ============================================================================
// Component Registration
// ============================================================================
// NOTE: s_Properties[] and s_ComponentMeta are now auto-generated in
// TextComponent.gen.h (included at end of TextComponent.h)
TextComponent::TextComponent()
: RendererComponent(),
color(255, 255, 255, 255),
decorationColor(0, 0, 0, 255)
{
}
TextComponent::~TextComponent()
{
delete[] m_cachedBuffer;
m_cachedBuffer = nullptr;
}
voidTextComponent::UnloadAssets()
{
// Clear the cached font pointer and load flag to force reload on next frame
// This is essential for editor font re-baking workflow:
// 1. User changes font settings (e.g., No Antialiasing) and clicks Apply & Bake
// 2. Editor calls InvalidateAllAssets() → UnloadAssets() on all components
// 3. Cleared ptr + loadAttempted triggers full re-resolution on next frame
// 4. AssetManager loads the fresh .dfont file with updated glyph data
font.ptr = nullptr;
font.loadAttempted = false;
InvalidateRenderCache();
}
voidTextComponent::InvalidateRenderCache()
{
delete[] m_cachedBuffer;
m_cachedBuffer = nullptr;
m_cachedBufferSize = 0;
}
voidTextComponent::SetText(constchar* newText)
{
if (newText)
{
text = newText;
}
else
{
text.clear();
}
InvalidateRenderCache();
}
voidTextComponent::SetText(const std::string& newText)
{
text = newText;
InvalidateRenderCache();
}
voidTextComponent::SetFont(BitmapFont* f)
{
font = f;
InvalidateRenderCache();
}
voidTextComponent::SetColor(const Deki::Color& newColor)
{
color = newColor;
InvalidateRenderCache();
}
voidTextComponent::SetColor(uint8_t r, uint8_t g, uint8_t b)
{
color = Deki::Color(r, g, b, 255);
InvalidateRenderCache();
}
int32_tTextComponent::GetTextWidth() const
{
if (!font || text.empty())
return0;
return font->MeasureWidth(text.c_str());
}
int32_tTextComponent::GetTextHeight() const
{
if (!font || text.empty())
return0;
// TODO: Handle word wrapping for multi-line text
return font->GetLineHeight();
}
std::vector<std::string> TextComponent::WrapTextWithFont(const BitmapFont* fontPtr) const
{
std::vector<std::string> result;
// Width is stored in meters; wrap math operates on pixel-measured glyph widths.
constfloat ppm = DekiEngineSettings::Global().pixelsPerMeter;
constint32_t widthPx = static_cast<int32_t>(width * ppm);
if (!fontPtr || text.empty() || widthPx <= 0)
{
if (!text.empty())
result.push_back(text);
return result;
}
// Bitmap fonts have no kerning, so a line's width is the sum of its words'
// widths plus the spaces between them. Measure each word once and add,
// instead of re-measuring the whole growing line for every word appended
// (quadratic in line length, with a string concatenation per step).
constint32_t ps = (std::max)(1, pixelScale);
constint32_t spaceWidth = fontPtr->MeasureWidth("") * ps;
std::string currentLine;
size_t paraStart = 0;
while (paraStart <= text.size())
{
// One paragraph per explicit newline.
size_t paraEnd = text.find('\n', paraStart);
if (paraEnd == std::string::npos) paraEnd = text.size();
const std::string para = text.substr(paraStart, paraEnd - paraStart);
paraStart = paraEnd + 1;
if (paraEnd == text.size() && para.empty() && !result.empty())
break; // trailing newline: no extra empty line
if (para.empty())
{
result.push_back("");
continue;
}
// Whole paragraph fits: no wrapping needed.
if (fontPtr->MeasureWidth(para.c_str()) * ps <= widthPx)
{
result.push_back(para);
continue;
}
// Wrap word by word.
currentLine.clear();
int32_t currentLineWidth = 0;
size_t wordStart = 0;
while (wordStart < para.size())
{
// Skip runs of spaces; find the next word's extent.
while (wordStart < para.size() && para[wordStart] == '') ++wordStart;
if (wordStart >= para.size()) break;
size_t wordEnd = para.find('', wordStart);
if (wordEnd == std::string::npos) wordEnd = para.size();
const std::string word = para.substr(wordStart, wordEnd - wordStart);
wordStart = wordEnd;
constint32_t wordWidth = fontPtr->MeasureWidth(word.c_str()) * ps;
constint32_t joinedWidth = currentLine.empty() ? wordWidth
: currentLineWidth + spaceWidth + wordWidth;
if (joinedWidth <= widthPx)
{
if (!currentLine.empty()) currentLine += '';
currentLine += word;
currentLineWidth = joinedWidth;
}
else
{
// Current line is full, start a new one.
if (!currentLine.empty())
result.push_back(currentLine);
if (wordWidth > widthPx)
{
// A single word wider than the box goes on its own line.
result.push_back(word);
currentLine.clear();
currentLineWidth = 0;
}
else
{
currentLine = word;
currentLineWidth = wordWidth;
}
}
}
if (!currentLine.empty())
result.push_back(currentLine);
}
return result;
}
voidTextComponent::CalculateGlyphLayout(const BitmapFont* fontPtr, std::vector<GlyphLayout>& outGlyphs) const
{
outGlyphs.clear();
if (!fontPtr || text.empty())
return;
// width/height are world meters; layout math runs in pixel space.
constfloat ppm = DekiEngineSettings::Global().pixelsPerMeter;
float containerW = width * ppm;
float containerH = height * ppm;
// Word-wrap text
std::vector<std::string> lines = WrapTextWithFont(fontPtr);
// Pixel scale factor
float ps = static_cast<float>((std::max)(1, pixelScale));
// Calculate total text height
float lineHeightF = static_cast<float>(fontPtr->GetLineHeight()) * ps;
float totalTextHeight = lineHeightF * static_cast<float>(lines.size());
// Get visual bounds for ascender/descender
int32_t minY = 0, maxY = 0;
fontPtr->GetVisualBounds(minY, maxY);
float ascenderHeight = static_cast<float>(-minY) * ps;
float descenderDepth = static_cast<float>(maxY) * ps;
float visualLineHeight = ascenderHeight + descenderDepth;
// Calculate baseline Y position (relative to center)
float worldStartY = -containerH * 0.5f + ascenderHeight;
switch (verticalAlign)
{
case TextVerticalAlign::Top:
break;
case TextVerticalAlign::Middle:
// Use visual bounds for single line, lineHeight for multi-line
if (lines.size() == 1)
{
// Center single line based on actual visual height
worldStartY += (containerH - visualLineHeight) * 0.5f;
}
else
{
// Multi-line: center total text block
worldStartY += (containerH - totalTextHeight) * 0.5f;
}
break;
case TextVerticalAlign::Bottom:
worldStartY = containerH * 0.5f - descenderDepth - (static_cast<float>(lines.size()) - 1.0f) * lineHeightF;
break;
case TextVerticalAlign::CapCenter:
case TextVerticalAlign::XCenter:
case TextVerticalAlign::TypoCenter:
case TextVerticalAlign::Baseline:
{
// Anchor-based centering: offset-from-baseline of the chosen anchor,
// in font-space pixels. Positive values mean below baseline.
float anchorOffset = 0.0f;
if (verticalAlign == TextVerticalAlign::CapCenter)
anchorOffset = -static_cast<float>(fontPtr->GetCapHeight()) * 0.5f;
elseif (verticalAlign == TextVerticalAlign::XCenter)
anchorOffset = -static_cast<float>(fontPtr->GetXHeight()) * 0.5f;
elseif (verticalAlign == TextVerticalAlign::TypoCenter)
anchorOffset = static_cast<float>(minY + maxY) * 0.5f;
// Baseline: anchorOffset stays 0
// Baseline world-Y so the group of baselines is centered with anchor at 0.
constfloat totalLineSpan = lineHeightF * (static_cast<float>(lines.size()) - 1.0f);
worldStartY = -anchorOffset * ps - totalLineSpan * 0.5f;
break;
}
}
// Process each line
float worldLineY = worldStartY;
for (constauto& lineText : lines)
{
if (lineText.empty())
{
worldLineY += lineHeightF;
continue;
}
// Measure line width (scaled)
float lineWidth = static_cast<float>(fontPtr->MeasureWidth(lineText.c_str())) * ps;
// Apply horizontal alignment (relative to center)
float worldLineX = -containerW * 0.5f;
switch (align)
{
case TextAlign::Center:
worldLineX += (containerW - lineWidth) * 0.5f;
break;
case TextAlign::Right:
worldLineX += containerW - lineWidth;
break;
case TextAlign::Left:
default:
break;
}
// Process each character (UTF-8 aware)
float cursorX = 0;
size_t ci = 0;
size_t lineLen = lineText.length();
constchar* lineData = lineText.c_str();
while (ci < lineLen)
{
uint32_t cp = BitmapFont::DecodeUtf8(lineData, lineLen, ci);
const GlyphInfo* glyph = fontPtr->GetGlyphByCodepoint(cp);
if (!glyph)
continue;
// Store glyph layout
GlyphLayout layout;
layout.glyph = glyph;
layout.worldX = worldLineX + cursorX;
layout.worldY = worldLineY;
outGlyphs.push_back(layout);
cursorX += glyph->advance * ps;
}
worldLineY += lineHeightF;
}
}
boolTextComponent::RenderContent(const DekiObject* owner,
QuadBlit::Source& outSource,
float& outPivotX,
float& outPivotY,
uint8_t& outTintR,
uint8_t& outTintG,
uint8_t& outTintB,
uint8_t& outTintA)
{
DEKI_PROFILE_SCOPE_N("TextComponent::RenderContent");
if (!owner || text.empty())
returnfalse;
// width/height are world meters; rasterization runs in pixel space.
constfloat ppm = DekiEngineSettings::Global().pixelsPerMeter;
constint32_t widthPx = static_cast<int32_t>(width * ppm);
constint32_t heightPx = static_cast<int32_t>(height * ppm);
if (widthPx <= 0 || heightPx <= 0)
returnfalse;
BitmapFont* fontPtr = nullptr;
// Editor hook: let external code handle font resolution (GUID sync, preview, baking)
if (s_fontResolveCallback)
fontPtr = s_fontResolveCallback(this);
// Runtime path: direct load from AssetRef
if (!fontPtr)
{
if (!font) returnfalse;
fontPtr = font.Get();
}
if (!fontPtr || !fontPtr->GetAtlas())
returnfalse;
Texture2D* atlas = fontPtr->GetAtlas();
if (!atlas->data)
returnfalse;
// Check if we can reuse the cached buffer
bool cacheValid = m_cachedBuffer != nullptr
&& m_cachedText == text
&& m_cachedWidth == widthPx
&& m_cachedHeight == heightPx
&& m_cachedColor == color
&& m_cachedDecorationColor == decorationColor
&& m_cachedAlign == align
&& m_cachedVerticalAlign == verticalAlign
&& m_cachedFont == fontPtr
&& m_cachedPixelScale == pixelScale;
if (cacheValid)
{
outSource = QuadBlit::MakeSource(
m_cachedBuffer + m_cropFirstRow * widthPx * 3,
widthPx, m_cropHeight, 3, true, true, false);
outSource.pixelsPerMeter = ppm;
outPivotX = 0.5f;
outPivotY = m_cropPivotY;
outTintR = 255;
outTintG = 255;
outTintB = 255;
outTintA = 255;
returntrue;
}
// Allocate RGB565A8 buffer for text with alpha (3 bytes/pixel, faster blit than RGBA8888)
size_t bufferSize = widthPx * heightPx * 3;
// Reuse existing cache buffer if same size, otherwise reallocate
if (m_cachedBufferSize != bufferSize)
{
delete[] m_cachedBuffer;
m_cachedBuffer = newuint8_t[bufferSize];
m_cachedBufferSize = bufferSize;
}
memset(m_cachedBuffer, 0, bufferSize); // Clear to transparent
// Calculate glyph layout using shared method
std::vector<GlyphLayout> glyphLayouts;
CalculateGlyphLayout(fontPtr, glyphLayouts);
// Get bytes per pixel for atlas format
uint32_t atlas_bpp = Texture2D::GetBytesPerPixel(atlas->format);
// Center of our buffer in local pixel coordinates
float centerX = widthPx * 0.5f;
float centerY = heightPx * 0.5f;
// Everything below depends on the font and the component colours only:
// computed once per layout, outside the glyph loop.
// Render glyph pixels to buffer (RGB565A8 — 3 bytes per pixel)
// Pre-compute text color as RGB565
uint16_t text_rgb565 = ((color.r >> 3) << 11) | ((color.g >> 2) << 5) | (color.b >> 3);
// Decoration palette (v4 palette-indexed fonts). Depends only on the
// component colours and the font's baked decoration mode, so it is built
// once per layout here, not once per glyph as it used to be. Entries:
// idx 0 = transparent (skipped)
// idx 1..4 = decoration edge AA: {64, 128, 192, 255}
// idx 5..15 = Outline: lerp(decoColor → fillColor), alpha 255
// Shadow: fillColor with alpha ramp 24..255
FontDecorationMode decoMode = fontPtr->GetDecorationMode();
constbool isPalette = (decoMode != FontDecorationMode::None);
uint16_t pal_rgb[16] = {0};
uint8_t pal_a[16] = {0};
if (isPalette)
{
constuint16_t decoRgb = ((decorationColor.r >> 3) << 11) |
((decorationColor.g >> 2) << 5) |
(decorationColor.b >> 3);
pal_rgb[0] = 0; pal_a[0] = 0;
pal_rgb[1] = decoRgb; pal_a[1] = 64;
pal_rgb[2] = decoRgb; pal_a[2] = 128;
pal_rgb[3] = decoRgb; pal_a[3] = 192;
pal_rgb[4] = decoRgb; pal_a[4] = 255;
if (decoMode == FontDecorationMode::Outline)
{
for (int i = 5; i <= 15; ++i)
{
int t = i - 5; // 0..10
int mixR = (decorationColor.r * (10 - t) + color.r * t) / 10;
int mixG = (decorationColor.g * (10 - t) + color.g * t) / 10;
int mixB = (decorationColor.b * (10 - t) + color.b * t) / 10;
pal_rgb[i] = ((mixR >> 3) << 11) | ((mixG >> 2) << 5) | (mixB >> 3);
pal_a[i] = 255;
}
}
else// Shadow
{
for (int i = 5; i <= 15; ++i)
{
pal_rgb[i] = text_rgb565;
pal_a[i] = static_cast<uint8_t>(24 + (i - 5) * 23); // 24..277 → clamps to 255 at top
if (i == 15) pal_a[i] = 255;
}
}
}
// Pre-compute alpha offset based on format to avoid per-pixel switch
// -1 = opaque (no alpha channel), -2 = transparency color check
int32_t alpha_offset;
switch (atlas->format)
{
case Texture2D::TextureFormat::RGBA8888: alpha_offset = 3; break;
case Texture2D::TextureFormat::RGB565A8: alpha_offset = 2; break;
case Texture2D::TextureFormat::ALPHA8: alpha_offset = 0; break;
default:
alpha_offset = (atlas->hasTransparency && atlas->format == Texture2D::TextureFormat::RGB565) ? -2 : -1;
break;
}
constuint8_t* atlas_data = atlas->data;
constint32_t atlas_width = atlas->width;
// Pre-quantize chroma key once. Atlas is always a Sprite at runtime.
Sprite* atlasSprite = static_cast<Sprite*>(atlas);
uint8_t key_r = 255, key_g = 0, key_b = 255;
if (atlasSprite->hasChromaKey)
{
key_r = atlasSprite->transparentR;
key_g = atlasSprite->transparentG;
key_b = atlasSprite->transparentB;
DekiPixel::QuantizeRGB565(key_r, key_g, key_b);
}
// Rows the glyphs actually touch, so the crop scan below reads only them.
int32_t touchedFirstRow = heightPx;
int32_t touchedLastRow = -1;
// Render each glyph to the buffer
for (constauto& layout : glyphLayouts)
{
if (!layout.glyph)
continue;
const GlyphInfo* glyph = layout.glyph;
int32_t ps = (std::max)(1, pixelScale);
// Convert world position to buffer position (center + world offset, scaled)
int32_t dest_x = static_cast<int32_t>(std::floor(centerX + layout.worldX)) + glyph->offsetX * ps;
int32_t dest_y = static_cast<int32_t>(std::floor(centerY + layout.worldY)) + glyph->offsetY * ps;
// Calculate source rectangle in atlas (native size)
int32_t src_x = glyph->x;
int32_t src_y = glyph->y;
int32_t glyph_w = glyph->width;
int32_t glyph_h = glyph->height;
// Scaled dimensions in buffer
int32_t scaled_w = glyph_w * ps;
int32_t scaled_h = glyph_h * ps;
// Clip to buffer bounds (in scaled pixel space)
int32_t clip_left = 0;
int32_t clip_top = 0;
int32_t clip_right = scaled_w;
int32_t clip_bottom = scaled_h;
if (dest_x < 0)
{
clip_left = -dest_x;
dest_x = 0;
}
if (dest_y < 0)
{
clip_top = -dest_y;
dest_y = 0;
}
if (dest_x + (clip_right - clip_left) > widthPx)
{
clip_right = widthPx - dest_x + clip_left;
}
if (dest_y + (clip_bottom - clip_top) > heightPx)
{
clip_bottom = heightPx - dest_y + clip_top;
}
// Nothing to render for this glyph
if (clip_left >= clip_right || clip_top >= clip_bottom)
continue;
touchedFirstRow = (std::min)(touchedFirstRow, dest_y);
touchedLastRow = (std::max)(touchedLastRow, dest_y + (clip_bottom - clip_top) - 1);
for (int32_t py = clip_top; py < clip_bottom; py++)
{
// Map scaled pixel back to atlas pixel
int32_t atlas_y = src_y + py / ps;
int32_t buf_y = dest_y + (py - clip_top);
size_t atlas_row = atlas_y * atlas_width;
size_t buf_row = buf_y * widthPx;
for (int32_t px = clip_left; px < clip_right; px++)
{
int32_t atlas_x = src_x + px / ps;
int32_t buf_x = dest_x + (px - clip_left);
// Get pixel from atlas
size_t atlas_off = (atlas_row + atlas_x) * atlas_bpp;
if (isPalette)
{
// V4 palette path: atlas byte's low nibble is a 0..15 index.
uint8_t idx = atlas_data[atlas_off + (alpha_offset >= 0 ? alpha_offset : 0)] & 0x0F;
if (idx == 0)
continue;
size_t buf_offset = (buf_row + buf_x) * 3;
*(uint16_t*)(m_cachedBuffer + buf_offset) = pal_rgb[idx];
m_cachedBuffer[buf_offset + 2] = pal_a[idx];
continue;
}
uint8_t alpha;
if (alpha_offset >= 0)
{
alpha = atlas_data[atlas_off + alpha_offset];
}
elseif (alpha_offset == -2)
{
// RGB565 atlas with chroma-key transparency. Per-font
// chroma color comes from the underlying Sprite.
uint16_t pixel = *((constuint16_t*)(atlas_data + atlas_off));
uint8_t r = ((pixel >> 11) & 0x1F) << 3;
uint8_t g = ((pixel >> 5) & 0x3F) << 2;
uint8_t b = (pixel & 0x1F) << 3;
alpha = (r == key_r && g == key_g && b == key_b) ? 0 : 255;
}
else
{
alpha = 255;
}
// Skip fully transparent pixels
if (alpha == 0)
continue;
// Write to buffer (RGB565A8: 2 bytes RGB565 + 1 byte alpha)
size_t buf_offset = (buf_row + buf_x) * 3;
*(uint16_t*)(m_cachedBuffer + buf_offset) = text_rgb565;
m_cachedBuffer[buf_offset + 2] = alpha;
}
}
}
// Update cache keys
m_cachedText = text;
m_cachedWidth = widthPx;
m_cachedHeight = heightPx;
m_cachedColor = color;
m_cachedDecorationColor = decorationColor;
m_cachedAlign = align;
m_cachedVerticalAlign = verticalAlign;
m_cachedFont = fontPtr;
m_cachedPixelScale = pixelScale;
// Compute tight vertical crop bounds (scan for first/last non-empty row).
// Only the rows some glyph wrote to can be non-empty; this used to scan
// the whole buffer's alpha bytes.
int32_t firstRow = heightPx;
int32_t lastRow = -1;
constint32_t scanEnd = (std::min)(heightPx, touchedLastRow + 1);
for (int32_t row = (std::max)(0, touchedFirstRow); row < scanEnd; row++)
{
constuint8_t* rowPtr = m_cachedBuffer + row * widthPx * 3;
for (int32_t col = 0; col < widthPx; col++)
{
if (rowPtr[col * 3 + 2] != 0) // Check alpha byte
{
if (row < firstRow) firstRow = row;
lastRow = row;
break;
}
}
}
if (lastRow < firstRow)
{
// Entirely empty — fall back to full buffer
firstRow = 0;
lastRow = heightPx - 1;
}
m_cropFirstRow = firstRow;
m_cropHeight = lastRow - firstRow + 1;
// Adjust pivot so the cropped sub-region stays at the correct world position
// Original pivot 0.5 corresponds to the center of the full height buffer.
// We need pivotY such that: cropFirstRow + pivotY * cropHeight == 0.5 * heightPx
m_cropPivotY = (0.5f * heightPx - static_cast<float>(m_cropFirstRow))
/ static_cast<float>(m_cropHeight);
// Return source - RGB565A8 format (not owned by caller, we manage lifetime)
outSource = QuadBlit::MakeSource(
m_cachedBuffer + m_cropFirstRow * widthPx * 3,
widthPx, m_cropHeight, 3, true, true, false);
outSource.pixelsPerMeter = ppm;
outPivotX = 0.5f;
outPivotY = m_cropPivotY;
// Text color is baked into buffer - no additional tint
outTintR = 255;
outTintG = 255;
outTintB = 255;
outTintA = 255;
returntrue;
}