diff --git a/backend/src/main/java/org/booklore/util/koreader/CfiConverter.java b/backend/src/main/java/org/booklore/util/koreader/CfiConverter.java
new file mode 100644
index 000000000..1c7d04454
--- /dev/null
+++ b/backend/src/main/java/org/booklore/util/koreader/CfiConverter.java
@@ -0,0 +1,478 @@
+/*
+ *
+ * Architecture inspired by epub.js (BSD-2-Clause, futurepress): CFI strings are
+ * first parsed into a typed data model (CfiExpression / CfiParser), then resolved
+ * against the document via a pluggable DocumentNavigator.
+ *
+ * The CFI grammar follows the EPUB CFI 1.1 specification (IDPF / W3C public standard).
+ *
+ * The KOReader XPointer format (/body/DocFragment[N]/body/.../text().O) follows
+ * conventions documented in the KOReader / CREngine source code (AGPL-3.0).
+ */
+package org.booklore.util.koreader;
+
+import java.util.*;
+
+/**
+ * Bidirectional converter between EPUB CFI expressions and KOReader-style XPointer strings.
+ *
+ *
Typical lifecycle:
+ *
+ *
{@code
+ * CfiConverter conv = new CfiConverter(navigator, spineIndex);
+ * XPointerResult xp = conv.cfiToXPointer("epubcfi(/6/2!/4/2/2)");
+ * String cfi = conv.xPointerToCfi("/body/DocFragment[1]/body/div[1]/p[1]");
+ * }
+ *
+ * Static helpers ({@link #extractSpineIndex}, {@link #normalizeProgressXPointer}) operate
+ * without a document and can be called before creating a converter instance.
+ */
+public class CfiConverter {
+
+ /**
+ * Tags considered "inline" when mapping text offsets. Text within these elements is treated as
+ * part of the surrounding block for XPointer purposes.
+ */
+ private static final Set INLINE_TAGS =
+ Set.of(
+ "a", "abbr", "b", "bdi", "bdo", "cite", "code", "dfn", "em", "i", "kbd", "mark", "q",
+ "rp", "rt", "ruby", "s", "samp", "small", "span", "strong", "sub", "sup", "time", "u",
+ "var", "wbr");
+
+ /** KOReader XPointer prefix format: {@code /body/DocFragment[<1-based>]/body}. */
+ private static final String XPOINTER_PREFIX = "/body/DocFragment[";
+
+ private static final String XPOINTER_BODY_SUFFIX = "]/body";
+
+ private final DocumentNavigator nav;
+ private final int spineIdx;
+
+ /**
+ * Creates a converter for a specific spine item.
+ *
+ * @param navigator document structure abstraction
+ * @param spineIndex zero-based index of the spine item this document represents
+ */
+ public CfiConverter(DocumentNavigator navigator, int spineIndex) {
+ this.nav = navigator;
+ this.spineIdx = spineIndex;
+ }
+
+ // -- Static utilities -------------------------------------------------------
+
+ /**
+ * Determines the zero-based spine-item index from either a CFI string or a KOReader XPointer.
+ *
+ * @throws IllegalArgumentException if the format is unrecognised or the value cannot be parsed
+ */
+ public static int extractSpineIndex(String locator) {
+ if (locator == null || locator.isBlank()) {
+ throw new IllegalArgumentException("CFI/XPointer string cannot be null or empty");
+ }
+
+ if (locator.startsWith("epubcfi(")) {
+ CfiExpression expr = CfiParser.parse(locator);
+ return expr.spineIndex();
+ }
+
+ if (locator.startsWith(XPOINTER_PREFIX)) {
+ return parseDocFragmentIndex(locator);
+ }
+
+ throw new IllegalArgumentException("Unsupported locator format: " + locator);
+ }
+
+ /**
+ * Strips text-offset and node-suffix components from a KOReader XPointer, leaving only the
+ * structural element path. Useful for reading-progress tracking where character precision is
+ * unnecessary.
+ *
+ * @return the normalised XPointer, or {@code null} when the input is {@code null}
+ */
+ public static String normalizeProgressXPointer(String xpointer) {
+ if (xpointer == null) return null;
+
+ // Remove "/text().N" suffix
+ int textIdx = xpointer.indexOf("/text().");
+ if (textIdx >= 0) {
+ xpointer = xpointer.substring(0, textIdx);
+ }
+
+ // Remove trailing ".N" node suffix (e.g. ".5" on the last segment)
+ int lastSlash = xpointer.lastIndexOf('/');
+ String tail = lastSlash >= 0 ? xpointer.substring(lastSlash) : xpointer;
+ int dotIdx = tail.lastIndexOf('.');
+ if (dotIdx > 0) {
+ String afterDot = tail.substring(dotIdx + 1);
+ if (!afterDot.isEmpty() && afterDot.chars().allMatch(Character::isDigit)) {
+ xpointer = xpointer.substring(0, xpointer.length() - afterDot.length() - 1);
+ }
+ }
+
+ return xpointer;
+ }
+
+ // -- Instance conversion methods --------------------------------------------
+
+ /**
+ * Resolves a CFI to a KOReader XPointer result.
+ *
+ * @throws IllegalArgumentException if the CFI is malformed or targets a different spine item
+ */
+ public XPointerResult cfiToXPointer(String cfi) {
+ CfiExpression expr = CfiParser.parse(cfi);
+
+ if (expr.spineIndex() != spineIdx) {
+ throw new IllegalArgumentException(
+ "CFI spine index "
+ + expr.spineIndex()
+ + " does not match converter spine index "
+ + spineIdx);
+ }
+
+ String xp = resolveToXPointer(expr.contentSteps(), expr.charOffset());
+ return new XPointerResult(xp, xp, xp);
+ }
+
+ /** Converts a single KOReader XPointer to a CFI string. */
+ public String xPointerToCfi(String xpointer) {
+ return xPointerToCfi(xpointer, null);
+ }
+
+ /**
+ * Converts one or two KOReader XPointers (point or range) to a CFI string.
+ *
+ * When {@code endXPointer} is non-blank a range CFI is produced.
+ */
+ public String xPointerToCfi(String startXPointer, String endXPointer) {
+ XPointerParts startParts = decomposeXPointer(startXPointer);
+ List startSteps = domToCfiSteps(startParts.element());
+ String startPath = CfiParser.formatContentPath(startSteps, startParts.textOffset());
+
+ if (endXPointer == null || endXPointer.isBlank()) {
+ return wrapCfi(startPath);
+ }
+
+ XPointerParts endParts = decomposeXPointer(endXPointer);
+ List endSteps = domToCfiSteps(endParts.element());
+ String endPath = CfiParser.formatContentPath(endSteps, endParts.textOffset());
+
+ if (startPath.equals(endPath)) {
+ return wrapCfi(startPath);
+ }
+ return wrapCfi(startPath + "," + endPath);
+ }
+
+ /** Returns {@code true} if the given CFI can be resolved against this document. */
+ public boolean validateCfi(String cfi) {
+ try {
+ cfiToXPointer(cfi);
+ return true;
+ } catch (RuntimeException e) {
+ return false;
+ }
+ }
+
+ /** Returns {@code true} if the given XPointer can be converted to a CFI. */
+ public boolean validateXPointer(String xpointer) {
+ try {
+ xPointerToCfi(xpointer, null);
+ return true;
+ } catch (RuntimeException e) {
+ return false;
+ }
+ }
+
+ // -- CFI to XPointer resolution ---------------------------------------------
+
+ /** Walk the parsed CFI steps against the document and produce an XPointer string. */
+ private String resolveToXPointer(List steps, Integer charOffset) {
+ Object target = walkSteps(steps);
+ if (target == null) {
+ throw new IllegalArgumentException("Cannot resolve CFI path to a document element");
+ }
+
+ if (charOffset != null) {
+ return mapCharacterOffset(target, charOffset);
+ }
+ return elementToXPointer(target);
+ }
+
+ /**
+ * Walk the CFI path steps against the document tree. Each even step position selects a child
+ * element by its zero-based index (position / 2 - 1).
+ */
+ private Object walkSteps(List steps) {
+ Object node = nav.getBody();
+ if (node == null) return null;
+
+ for (var step : steps) {
+ int idx = step.childElementIndex();
+ List