Fix full-length SMS rendering and Unicode-aware segment counting

The expanded xsms conversation view handed the whole message body to
pango_xft_render_layout() as one multiline layout; on the target Pango/Xft
stack that call only paints the first line, so long concatenated/UCS-2
messages (e.g. the Ukrainian 'Мелорінг дарує хіти!' example) stopped right
before the first non-ASCII character. Verified on the connected phone with an
instrumented sms.so (530-byte body, 13 measured lines, one painted line) and
a standalone ARM reproducer; the QMI WMS reassembly and sms.txt parser are
correct.

- Draw message bodies and long drafts line by line via a shared
  renderLayoutLines() helper with an Xft clip, so nothing overflows the
  bubble or softkey bar; use the same layout width for measuring and drawing.
- Make the selected message scrollable with Up/Down (one line per press plus
  a right-edge scrollbar), stepping to the previous/next message only at the
  top/bottom of an overflowing message.
- Replace the byte-based compose counter with smsTextMetrics(), which decodes
  UTF-8, detects the GSM 7-bit alphabet/extension table and reports GSM-7
  septets (160/153) or UCS-2 UTF-16 units (70/67) with the right part count.
- Strip [SIM n] tags from QMI/local SMS log headers and stop writing them in
  the offline-send fallback; log units instead of bytes.
- Add a check-rebuild.sh guard against reintroducing the bulk layout render.
- Rebuild prebuilt/overlay sms.so and engmenu.so, sync docs, and regenerate
  images/bananarchy-nokia-argon.img.
This commit is contained in:
Luxferre committed 2026-11-05 17:34:21 +02:00
1 parent 8078f211e5
commit 16df110b0b
10 files changed
+266 -30

No files matched your search

+32 -9
View File
@@ -2141,6 +2141,28 @@ struct SmsThread {
int messageCount = 0;
};
// The QMI WMS daemon tags reassembled (multipart) messages with the receiving
// SIM slot, e.g. "Received from 5050 [SIM 1] at <iso>:". Older local send
// fallbacks wrote "Sent at <iso> [SIM 1]:". Strip the tag so the sender and
// timestamp stay clean for the conversation view and time formatting.
inline std::string stripSmsSimTag(const std::string& s) {
std::string out = s;
size_t pos = 0;
while ((pos = out.find("[SIM ", pos)) != std::string::npos) {
size_t end = out.find(']', pos + 5);
if (end == std::string::npos) {
break;
}
size_t start = pos;
if (start > 0 && out[start - 1] == ' ') {
--start;
}
out.erase(start, end - start + 1);
pos = start;
}
return out;
}
inline bool parseSmsHeaderLine(const std::string& line,
bool* outIncoming,
std::string* outSender,
@@ -2148,25 +2170,26 @@ inline bool parseSmsHeaderLine(const std::string& line,
if (line.empty() || line.back() != ':') {
return false;
}
if (line.rfind("Received at ", 0) == 0 && line.size() > 13) {
std::string hdr = stripSmsSimTag(line.substr(0, line.size() - 1));
if (hdr.rfind("Received at ", 0) == 0 && hdr.size() > 12) {
*outIncoming = true;
outSender->clear();
*outIsoTime = line.substr(12, line.size() - 13);
*outIsoTime = hdr.substr(12);
return true;
}
if (line.rfind("Received from ", 0) == 0) {
size_t atPos = line.find(" at ");
if (atPos != std::string::npos && atPos + 4 < line.size() - 1) {
if (hdr.rfind("Received from ", 0) == 0) {
size_t atPos = hdr.find(" at ", 14);
if (atPos != std::string::npos && atPos + 4 < hdr.size()) {
*outIncoming = true;
*outSender = line.substr(14, atPos - 14);
*outIsoTime = line.substr(atPos + 4, line.size() - (atPos + 4) - 1);
*outSender = hdr.substr(14, atPos - 14);
*outIsoTime = hdr.substr(atPos + 4);
return true;
}
}
if (line.rfind("Sent at ", 0) == 0 && line.size() > 9) {
if (hdr.rfind("Sent at ", 0) == 0 && hdr.size() > 8) {
*outIncoming = false;
outSender->clear();
*outIsoTime = line.substr(8, line.size() - 9);
*outIsoTime = hdr.substr(8);
return true;
}
return false;
+209 -19
View File
@@ -9,8 +9,11 @@
#include <sys/inotify.h>
#include <sys/wait.h>
#include <clocale>
#include <cstdint>
#include <mutex>
#include <thread>
#include <unordered_set>
#include <vector>
namespace {
@@ -18,6 +21,121 @@ using namespace bananarchy;
volatile sig_atomic_t gRunning = 1;
// The PangoXft renderer on the target stack only paints the first line of a
// long multiline layout when the whole layout is handed to
// pango_xft_render_layout() in one call. Message bodies are therefore drawn
// line by line with pango_xft_render_layout_line().
constexpr int kMsgScrollStep = 16;
constexpr int kMsgBodyWidth = kWinWidth - 32; // cardW - 14
std::vector<uint32_t> decodeUtf8CodePoints(const std::string& s) {
std::vector<uint32_t> out;
size_t i = 0;
while (i < s.size()) {
unsigned char c = static_cast<unsigned char>(s[i]);
uint32_t cp = 0;
size_t n = 0;
if (c < 0x80) {
cp = c;
n = 1;
} else if ((c & 0xE0) == 0xC0) {
cp = c & 0x1F;
n = 2;
} else if ((c & 0xF0) == 0xE0) {
cp = c & 0x0F;
n = 3;
} else if ((c & 0xF8) == 0xF0) {
cp = c & 0x07;
n = 4;
} else {
++i;
continue;
}
if (i + n > s.size()) break;
bool ok = true;
for (size_t k = 1; k < n; ++k) {
unsigned char cc = static_cast<unsigned char>(s[i + k]);
if ((cc & 0xC0) != 0x80) {
ok = false;
break;
}
cp = (cp << 6) | (cc & 0x3F);
}
if (!ok) {
++i;
continue;
}
out.push_back(cp);
i += n;
}
return out;
}
struct SmsTextMetrics {
int units = 0; // GSM-7 septets or UTF-16 code units
int parts = 1; // concatenated SMS parts
int limit = 160; // capacity of the current number of parts
bool ucs2 = false;
};
// 3GPP TS 23.038 GSM 7-bit default alphabet and extension table. Any code
// point outside both forces UCS-2 (70 units per part, 67 when concatenated).
SmsTextMetrics smsTextMetrics(const std::string& text) {
static const std::unordered_set<uint32_t> kBasic = [] {
std::unordered_set<uint32_t> set;
for (uint32_t cp : decodeUtf8CodePoints(
"@\u00a3$\u00a5\u00e8\u00e9\u00f9\u00ec\u00f2\u00c7\n"
"\u00d8\u00f8\r\u00c5\u00e5"
"\u0394_\u03a6\u0393\u039b\u03a9\u03a0\u03a8\u03a3\u0398"
"\u039e\u00c6\u00e6\u00df\u00c9"
" !\"#\u00a4%&'()*+,-./0123456789:;<=>?"
"\u00a1ABCDEFGHIJKLMNOPQRSTUVWXYZ\u00c4\u00d6\u00d1\u00dc"
"\u00a7\u00bfabcdefghijklmnopqrstuvwxyz\u00e4\u00f6\u00f1"
"\u00fc\u00e0")) {
set.insert(cp);
}
return set;
}();
static const std::unordered_set<uint32_t> kExt = [] {
std::unordered_set<uint32_t> set;
for (uint32_t cp : decodeUtf8CodePoints("\f^{}\\[~]|\u20ac")) {
set.insert(cp);
}
return set;
}();
std::vector<uint32_t> cps = decodeUtf8CodePoints(text);
bool ucs2 = false;
int septets = 0;
for (uint32_t cp : cps) {
if (kBasic.count(cp)) {
++septets;
} else if (kExt.count(cp)) {
septets += 2;
} else {
ucs2 = true;
break;
}
}
SmsTextMetrics m;
m.ucs2 = ucs2;
if (ucs2) {
int units = 0;
for (uint32_t cp : cps) {
units += (cp > 0xFFFF) ? 2 : 1;
}
m.units = units;
m.parts = units <= 70 ? 1 : (units + 66) / 67;
m.limit = m.parts == 1 ? 70 : m.parts * 67;
} else {
m.units = septets;
m.parts = septets <= 160 ? 1 : (septets + 152) / 153;
m.limit = m.parts == 1 ? 160 : m.parts * 153;
}
return m;
}
void handleSignal(int) {
gRunning = 0;
}
@@ -167,6 +285,9 @@ int runSmsApp(int argc, char** argv) {
std::string searchQuery;
int selectedThread = 0;
int selectedMsg = 0;
int msgScrollPx = 0; // vertical scroll inside the selected message
int msgTextH = 0; // wrapped height of the selected message body
int msgViewH = 0; // visible body height inside its card
int selectedOption = 0;
int selectedPicker = 0;
int confirmSelected = 0;
@@ -272,6 +393,7 @@ int runSmsApp(int argc, char** argv) {
viewingSpamFolder ? getBlockDirPath() : getModemLogDirPath();
threadMessages = loadSmsThreadMessages(activeNumber, curBase);
selectedMsg = std::max(0, static_cast<int>(threadMessages.size()) - 1);
msgScrollPx = 0;
mode = ViewMode::ThreadView;
};
@@ -432,13 +554,13 @@ int runSmsApp(int argc, char** argv) {
ContactBook::ensureParentDir(smsPath);
std::ofstream smsOut(smsPath, std::ios::app);
if (smsOut.is_open()) {
smsOut << "Sent at " << tbuf << " [SIM " << chosenSim << "]:\n"
<< body << "\n\n";
smsOut << "Sent at " << tbuf << ":\n" << body << "\n\n";
}
std::ofstream logOut(getModemLogFilePath(), std::ios::app);
if (logOut.is_open()) {
logOut << tbuf << "\tsent_txt\t" << recipient << "\t[SIM "
<< chosenSim << "] " << body.size() << " chars\n";
<< chosenSim << "] " << smsTextMetrics(body).units
<< " units\n";
}
removeSmsDraft(recipient);
}
@@ -463,6 +585,42 @@ int runSmsApp(int argc, char** argv) {
return neo.drawPango(layout, color, x, y, h, text, maxW);
};
// pango_xft_render_layout() only paints the first line of a long multiline
// UTF-8 layout on the target Pango/Xft stack, so every multiline body is
// drawn one line at a time, clipped to its visible rectangle. yPx is the
// layout origin and scrollPx is the number of layout pixels hidden above
// the clip (negative when the layout starts below the clip top).
auto renderLayoutLines = [&](PangoLayout* layout, XftColor* color, int xPx,
int yPx, int clipX, int clipY, int clipW,
int clipH, int scrollPx) {
if (clipW <= 0 || clipH <= 0) {
return;
}
XRectangle clip {
static_cast<short>(clipX), static_cast<short>(clipY),
static_cast<unsigned short>(clipW),
static_cast<unsigned short>(clipH)};
XftDrawSetClipRectangles(xftDraw, 0, 0, &clip, 1);
PangoLayoutIter* it = pango_layout_get_iter(layout);
if (it) {
do {
PangoRectangle logical{};
pango_layout_iter_get_line_extents(it, nullptr, &logical);
int lineTop = logical.y / PANGO_SCALE;
int lineBottom = (logical.y + logical.height) / PANGO_SCALE;
if (lineBottom <= scrollPx) continue;
if (lineTop >= scrollPx + clipH) break;
PangoLayoutLine* line = pango_layout_iter_get_line_readonly(it);
int baseline = pango_layout_iter_get_baseline(it) / PANGO_SCALE;
pango_xft_render_layout_line(
xftDraw, color, line, xPx * PANGO_SCALE,
(yPx + baseline - scrollPx) * PANGO_SCALE);
} while (pango_layout_iter_next_line(it));
pango_layout_iter_free(it);
}
XftDrawSetClip(xftDraw, nullptr);
};
auto drawSoftkeyBar = [&](const std::string& leftLabel,
const std::string& centerLabel,
const std::string& rightLabel) {
@@ -611,6 +769,7 @@ int runSmsApp(int argc, char** argv) {
struct CardGeom {
int idx;
int h;
int bodyH;
std::string header;
std::string body;
bool incoming;
@@ -626,13 +785,14 @@ int runSmsApp(int argc, char** argv) {
cg.header = (m.incoming ? "Recv " : "Sent ") + m.displayTime;
cg.body = m.text;
pango_layout_set_width(layoutSmall, 204 * PANGO_SCALE);
pango_layout_set_width(layoutSmall, kMsgBodyWidth * PANGO_SCALE);
pango_layout_set_wrap(layoutSmall, PANGO_WRAP_WORD_CHAR);
pango_layout_set_ellipsize(layoutSmall, PANGO_ELLIPSIZE_NONE);
pango_layout_set_text(layoutSmall, cg.body.c_str(),
static_cast<int>(cg.body.size()));
int bw = 0, bh = 0;
pango_layout_get_pixel_size(layoutSmall, &bw, &bh);
cg.bodyH = bh;
cg.h = std::max(38, bh + 22);
cards.push_back(cg);
}
@@ -679,14 +839,30 @@ int runSmsApp(int argc, char** argv) {
drawPango(layoutSmall, cg.incoming ? &colGreen : &colBlue,
cardX + 8, curY + 2, 14, cg.header, cardW - 14);
pango_layout_set_width(layoutSmall, (cardW - 14) * PANGO_SCALE);
pango_layout_set_width(layoutSmall, kMsgBodyWidth * PANGO_SCALE);
pango_layout_set_wrap(layoutSmall, PANGO_WRAP_WORD_CHAR);
pango_layout_set_ellipsize(layoutSmall, PANGO_ELLIPSIZE_NONE);
pango_layout_set_text(layoutSmall, cg.body.c_str(),
static_cast<int>(cg.body.size()));
pango_xft_render_layout(xftDraw, &colWhite, layoutSmall,
(cardX + 8) * PANGO_SCALE,
(curY + 18) * PANGO_SCALE);
const int bodyViewH = std::max(0, cardH - 20);
const int maxScroll = std::max(0, cg.bodyH - bodyViewH);
const int scrollPx =
isSel ? std::clamp(msgScrollPx, 0, maxScroll) : 0;
renderLayoutLines(layoutSmall, &colWhite, cardX + 8, curY + 18,
cardX + 8, curY + 18, cardW - 16, bodyViewH,
scrollPx);
if (isSel) {
msgTextH = cg.bodyH;
msgViewH = bodyViewH;
if (maxScroll > 0) {
int sbH = std::max(14, bodyViewH * bodyViewH / (cg.bodyH + 1));
int sbY = curY + 18 +
(bodyViewH - sbH) * scrollPx / maxScroll;
XSetForeground(dpy, gc, pt.spec.accent);
XFillRectangle(dpy, pix, gc, cardX + cardW - 5, sbY, 3, sbH);
}
}
curY += cardH + 4;
}
@@ -715,10 +891,13 @@ int runSmsApp(int argc, char** argv) {
} else if (caseMode == TextCaseMode::Num) {
modeBadge = "[123]";
}
int segs = std::max(1, static_cast<int>(composeText.size() + 159) / 160);
SmsTextMetrics sm = smsTextMetrics(composeText);
std::string rightHdr = std::string(modeBadge) + " " +
std::to_string(composeText.size()) + "/" +
std::to_string(segs * 160);
std::to_string(sm.units) + "/" +
std::to_string(sm.limit);
if (sm.parts > 1) {
rightHdr += "x" + std::to_string(sm.parts);
}
pango_layout_set_width(layoutSmall, -1);
pango_layout_set_text(layoutSmall, rightHdr.c_str(),
static_cast<int>(rightHdr.size()));
@@ -775,12 +954,13 @@ int runSmsApp(int argc, char** argv) {
if (th > bodyMaxH) {
textY = 76 - (th - bodyMaxH);
}
XRectangle clipRect {6, 73, static_cast<unsigned short>(kWinWidth - 12),
static_cast<unsigned short>(smsTapActive ? 134 : 154)};
XftDrawSetClipRectangles(xftDraw, 0, 0, &clipRect, 1);
pango_xft_render_layout(xftDraw, &colWhite, layoutMain,
12 * PANGO_SCALE, textY * PANGO_SCALE);
XftDrawSetClip(xftDraw, nullptr);
int clipY = 73;
int clipH = smsTapActive ? 134 : 154;
// Keep the layout origin at the clip top and express the upward
// scroll of a long draft as hidden layout pixels, so the tail of the
// text stays visible instead of being skipped by the line loop.
renderLayoutLines(layoutMain, &colWhite, 12, clipY, 6, clipY,
kWinWidth - 12, clipH, clipY - textY);
}
if (smsTapActive) {
@@ -1505,14 +1685,24 @@ int runSmsApp(int argc, char** argv) {
}
}
} else if (mode == ViewMode::ThreadView) {
int maxScroll = std::max(0, msgTextH - msgViewH);
if (sym == XK_Up || sym == XK_KP_Up) {
if (selectedMsg > 0) {
if (msgScrollPx > 0) {
msgScrollPx = std::max(0, msgScrollPx - kMsgScrollStep);
drawUI();
} else if (selectedMsg > 0) {
--selectedMsg;
msgScrollPx = 0;
drawUI();
}
} else if (sym == XK_Down || sym == XK_KP_Down) {
if (selectedMsg + 1 < static_cast<int>(threadMessages.size())) {
if (msgScrollPx < maxScroll) {
msgScrollPx = std::min(maxScroll, msgScrollPx + kMsgScrollStep);
drawUI();
} else if (selectedMsg + 1 <
static_cast<int>(threadMessages.size())) {
++selectedMsg;
msgScrollPx = 0;
drawUI();
}
} else if (sym == XK_Return || sym == XK_KP_Enter) {