/* logic/reader/nav.c — 阅读导航纯逻辑实现(见 nav.h 的方向语义说明)。 */ #include "logic/reader/nav.h" #include "logic/reader/page_source.h" /* MR_BOOK_MAX_PAGES(条漫排版页数上限) */ #include int mr_nav_tap_zone(int x, int view_w) { if (view_w <= 0) return MR_ZONE_MIDDLE; if (x < view_w / 3) return MR_ZONE_LEFT; if (x >= (view_w * 2) / 3) return MR_ZONE_RIGHT; return MR_ZONE_MIDDLE; } int mr_nav_tap_delta(int rtl, int zone) { if (zone == MR_ZONE_MIDDLE) return 0; if (rtl) return (zone == MR_ZONE_LEFT) ? 1 : -1; /* RTL:左进右退 */ return (zone == MR_ZONE_LEFT) ? -1 : 1; /* LTR:右进左退 */ } int mr_nav_swipe_delta(int rtl, int dx) { if (dx == 0) return 0; if (rtl) return dx > 0 ? 1 : -1; return dx > 0 ? -1 : 1; } int mr_nav_clamp_page(int page, int count) { if (count <= 0) return -1; if (page < 0) return 0; if (page >= count) return count - 1; return page; } int mr_nav_commit_dist(int view_w) { int t = view_w / 4; if (t < 60) t = 60; if (t > 160) t = 160; return t; } int mr_nav_commit(int dx, int view_w) { int adx = dx < 0 ? -dx : dx; return adx >= mr_nav_commit_dist(view_w); } int mr_nav_fit_size(int fit, int src_w, int src_h, int vw, int vh, int *out_w, int *out_h) { if (!out_w || !out_h || src_w <= 0 || src_h <= 0 || vw <= 0 || vh <= 0) return -1; long long w, h; if (fit == MR_FIT_ORIGINAL) { w = src_w; h = src_h; } else if (fit == MR_FIT_HEIGHT) { h = vh; w = (long long)src_w * vh / src_h; } else { w = vw; h = (long long)src_h * vw / src_w; } if (w < 1) w = 1; if (h < 1) h = 1; *out_w = (int)w; *out_h = (int)h; return 0; } int mr_nav_zoom_size(int w, int h, int zoom_pct, int *out_w, int *out_h) { if (!out_w || !out_h || w <= 0 || h <= 0 || zoom_pct <= 0) return -1; long long ww = (long long)w * zoom_pct / 100; long long hh = (long long)h * zoom_pct / 100; if (ww < 1) ww = 1; if (hh < 1) hh = 1; *out_w = (int)ww; *out_h = (int)hh; return 0; } void mr_nav_pan_limit(int content_w, int content_h, int vw, int vh, int *lim_x, int *lim_y) { int lx = content_w - vw; int ly = content_h - vh; if (lx < 0) lx = 0; if (ly < 0) ly = 0; /* 平移量是「中心偏移」:可左右各移动溢出量的一半,两边才都看得到。 */ if (lim_x) *lim_x = lx / 2; if (lim_y) *lim_y = ly / 2; } void mr_nav_pan_clamp(int content_w, int content_h, int vw, int vh, int *pan_x, int *pan_y) { int lx, ly; mr_nav_pan_limit(content_w, content_h, vw, vh, &lx, &ly); if (pan_x) { if (*pan_x > lx) *pan_x = lx; if (*pan_x < -lx) *pan_x = -lx; } if (pan_y) { if (*pan_y > ly) *pan_y = ly; if (*pan_y < -ly) *pan_y = -ly; } } int mr_nav_slide_from(int rtl, int delta, int view_w) { if (!delta) return 0; int from = rtl ? -delta : delta; return from * view_w; } int mr_nav_fit_next(int fit) { if (fit < 0 || fit >= MR_FIT_COUNT) return MR_FIT_WIDTH; return (fit + 1) % MR_FIT_COUNT; } /* ============================================================ */ /* 条漫排版 / 滚动窗口 */ /* ============================================================ */ static int strip_page_height(int src_w, int src_h, int vw) { long long h; if (vw <= 0) vw = 1; if (src_w > 0 && src_h > 0) h = (long long)src_h * vw / src_w; /* 适宽等比 */ else h = (long long)vw * 3 / 2; /* 未知尺寸:2:3 竖图占位 */ if (h < 1) h = 1; if (h > MR_STRIP_MAX_PAGE_H) h = MR_STRIP_MAX_PAGE_H; return (int)h; } void mr_strip_free(mr_strip *s) { if (!s) return; free(s->top); free(s->height); s->top = NULL; s->height = NULL; s->count = s->total = 0; s->vw = s->vh = 0; } int mr_strip_build(mr_strip *s, int count, int vw, int vh, mr_strip_meta_fn meta, void *ctx) { if (!s) return -1; mr_strip_free(s); if (count <= 0 || count > MR_BOOK_MAX_PAGES || vw <= 0 || vh <= 0) return -1; s->top = (int *)calloc((size_t)count + 1, sizeof *s->top); s->height = (int *)calloc((size_t)count, sizeof *s->height); if (!s->top || !s->height) { mr_strip_free(s); return -1; } s->count = count; s->vw = vw; s->vh = vh; long long y = 0; for (int i = 0; i < count; i++) { int sw = 0, sh = 0; if (meta && !meta(ctx, i, &sw, &sh)) { sw = sh = 0; } /* 未知/失败 → 占位 */ int h = strip_page_height(sw, sh, vw); s->height[i] = h; s->top[i] = (int)y; y += h; if (y > 1000000000LL) { /* 累计偏移上限(防溢出;正常书远达不到) */ y = 1000000000LL; for (int k = i + 1; k < count; k++) { s->height[k] = 1; s->top[k] = (int)y; y += 1; if (y > 1000000000LL) y = 1000000000LL; } s->top[count] = (int)y; s->total = (int)y; return 0; } } s->top[count] = (int)y; s->total = (int)y; return 0; } int mr_strip_page_h(const mr_strip *s, int index) { if (!s || !s->height || index < 0 || index >= s->count) return 0; return s->height[index]; } int mr_strip_top(const mr_strip *s, int index) { if (!s || !s->top || index < 0 || index > s->count) return 0; return s->top[index]; } int mr_strip_total(const mr_strip *s) { return s ? s->total : 0; } int mr_strip_max_offset(const mr_strip *s) { if (!s || s->count <= 0) return 0; int m = s->total - s->vh; return m > 0 ? m : 0; } int mr_strip_page_at(const mr_strip *s, int off) { if (!s || s->count <= 0 || !s->top) return -1; if (off <= 0) return 0; if (off >= s->total) return s->count - 1; int lo = 0, hi = s->count - 1; while (lo < hi) { /* 最大的 top[i] <= off */ int mid = lo + (hi - lo + 1) / 2; if (s->top[mid] <= off) lo = mid; else hi = mid - 1; } return lo; } int mr_strip_offset_for_page(const mr_strip *s, int page) { if (!s || s->count <= 0) return -1; if (page < 0) page = 0; if (page >= s->count) page = s->count - 1; int off = s->top[page]; int m = mr_strip_max_offset(s); if (off > m) off = m; if (off < 0) off = 0; return off; } int mr_strip_window(const mr_strip *s, int off, int max_pages, int *first, int *last) { if (first) *first = 0; if (last) *last = -1; if (!s || s->count <= 0) return 0; if (max_pages <= 0) max_pages = 1; int m = mr_strip_max_offset(s); if (off < 0) off = 0; if (off > m) off = m; int f = mr_strip_page_at(s, off); int l = mr_strip_page_at(s, off + s->vh - 1); if (l < f) l = f; int lo = f - 1; /* 视口 ±1 页 */ int hi = l + 1; if (lo < 0) lo = 0; if (hi > s->count - 1) hi = s->count - 1; if (hi - lo + 1 > max_pages) { /* 驻留上限:从视口中心向外截 */ int center = (f + l) / 2; lo = center - (max_pages - 1) / 2; hi = lo + max_pages - 1; if (lo < 0) { lo = 0; hi = max_pages - 1; } if (hi > s->count - 1) { hi = s->count - 1; lo = hi - max_pages + 1; } if (lo < 0) lo = 0; } if (first) *first = lo; if (last) *last = hi; return hi - lo + 1; } int mr_strip_at_end(const mr_strip *s, int off) { if (!s || s->count <= 0) return 0; return off >= mr_strip_max_offset(s); } int mr_strip_at_start(const mr_strip *s, int off) { if (!s || s->count <= 0) return 0; return off <= 0; } /* ============================================================ */ /* 跳页状态 */ /* ============================================================ */ void mr_jump_cancel(mr_jump *j) { if (!j) return; j->pending = 0; j->target = -1; j->t0 = 0; } void mr_jump_begin(mr_jump *j, int page, int count, unsigned long long now) { if (!j) return; if (count <= 0 || page < 0 || page >= count) { mr_jump_cancel(j); return; } j->pending = 1; j->target = page; j->t0 = now; } int mr_jump_active(const mr_jump *j) { return j && j->pending ? 1 : 0; } int mr_jump_target(const mr_jump *j) { return (j && j->pending) ? j->target : -1; } int mr_jump_poll(mr_jump *j, int have_img, unsigned long long now, unsigned long long timeout_ms, int *timed_out) { if (timed_out) *timed_out = 0; if (!j || !j->pending) return 0; if (have_img) { mr_jump_cancel(j); return 1; } if (timeout_ms > 0 && now - j->t0 >= timeout_ms) { mr_jump_cancel(j); if (timed_out) *timed_out = 1; return 1; } return 0; }