@@ -27,6 +27,7 @@ class MdReaderActivity final : public Activity {
|
||||
std::vector<MdParser::Span> spans;
|
||||
int indent = 0; // left indent in pixels
|
||||
bool isHR = false; // draw as horizontal rule
|
||||
bool isCodeBlock = false;
|
||||
};
|
||||
|
||||
// Streaming reader state
|
||||
@@ -71,14 +72,11 @@ class MdReaderActivity final : public Activity {
|
||||
void assignHeadingPageNumbers();
|
||||
int getHeadingIndexForOffset(size_t offset) const;
|
||||
void jumpToHeading(bool next);
|
||||
void scanHeadings();
|
||||
int getHeadingIndexForOffset(size_t offset) const;
|
||||
void jumpToHeading(bool next);
|
||||
|
||||
// Word-wrap a parsed markdown line into one or more RenderedLines.
|
||||
// Returns true if all content was emitted, false if truncated by maxLines.
|
||||
bool wordWrapParsedLine(const MdParser::ParsedLine& parsed, int indent, std::vector<RenderedLine>& outLines,
|
||||
int maxLines);
|
||||
int maxLines, bool isCodeBlock = false);
|
||||
|
||||
// Measure total pixel width of a span list
|
||||
int measureSpans(const std::vector<MdParser::Span>& spans) const;
|
||||
|
||||
@@ -320,7 +320,7 @@ int MdReaderActivity::measureSpans(const std::vector<MdParser::Span>& spans) con
|
||||
}
|
||||
|
||||
bool MdReaderActivity::wordWrapParsedLine(const MdParser::ParsedLine& parsed, int indent,
|
||||
std::vector<RenderedLine>& outLines, int maxLines) {
|
||||
std::vector<RenderedLine>& outLines, int maxLines, bool isCodeBlock) {
|
||||
const size_t startSize = outLines.size();
|
||||
|
||||
if (parsed.spans.empty()) {
|
||||
@@ -347,6 +347,7 @@ bool MdReaderActivity::wordWrapParsedLine(const MdParser::ParsedLine& parsed, in
|
||||
RenderedLine rl;
|
||||
rl.spans = std::move(allSpans);
|
||||
rl.indent = indent;
|
||||
rl.isCodeBlock = isCodeBlock;
|
||||
outLines.push_back(std::move(rl));
|
||||
return true;
|
||||
}
|
||||
@@ -354,6 +355,7 @@ bool MdReaderActivity::wordWrapParsedLine(const MdParser::ParsedLine& parsed, in
|
||||
// Word-wrap across spans
|
||||
RenderedLine currentLine;
|
||||
currentLine.indent = indent;
|
||||
currentLine.isCodeBlock = isCodeBlock;
|
||||
int currentWidth = 0;
|
||||
bool fullyConsumed = true;
|
||||
|
||||
@@ -423,6 +425,7 @@ bool MdReaderActivity::wordWrapParsedLine(const MdParser::ParsedLine& parsed, in
|
||||
outLines.push_back(std::move(currentLine));
|
||||
currentLine = RenderedLine();
|
||||
currentLine.indent = indent;
|
||||
currentLine.isCodeBlock = isCodeBlock;
|
||||
currentWidth = 0;
|
||||
|
||||
size_t skip = breakPos;
|
||||
@@ -539,7 +542,8 @@ bool MdReaderActivity::loadPageAtOffset(size_t offset, bool startInCodeBlock, st
|
||||
if (!wasFence) {
|
||||
size_t linesBefore = outLines.size();
|
||||
int remainingLines = linesPerPage - static_cast<int>(outLines.size());
|
||||
bool fullyConsumed = wordWrapParsedLine(parsed, indent, outLines, remainingLines);
|
||||
bool fullyConsumed = wordWrapParsedLine(parsed, indent, outLines, remainingLines,
|
||||
parsed.blockType == MdParser::BlockType::CodeBlock);
|
||||
|
||||
if (!fullyConsumed) {
|
||||
if (linesBefore > 0) {
|
||||
@@ -660,7 +664,12 @@ void MdReaderActivity::renderPage() {
|
||||
// Draw horizontal rule as a thin line
|
||||
int hrY = y + lineHeight / 2;
|
||||
renderer.drawLine(cachedOrientedMarginLeft + line.indent, hrY, cachedOrientedMarginLeft + viewportWidth, hrY);
|
||||
} else if (!line.spans.empty()) {
|
||||
} else {
|
||||
if (line.isCodeBlock) {
|
||||
const int barX = cachedOrientedMarginLeft + std::max(line.indent - 6, 0);
|
||||
renderer.drawLine(barX, y + 2, barX, y + lineHeight - 2);
|
||||
}
|
||||
if (!line.spans.empty()) {
|
||||
int x = cachedOrientedMarginLeft + line.indent;
|
||||
|
||||
// Apply text alignment for non-indented lines
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
#include "MdReaderTocSelectionActivity.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <GfxRenderer.h>
|
||||
#include <I18n.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "components/UITheme.h"
|
||||
#include "fontIds.h"
|
||||
|
||||
@@ -99,7 +99,8 @@ void MdReaderTocSelectionActivity::render(RenderLock&&) {
|
||||
|
||||
const auto& heading = headings[itemIndex];
|
||||
const int indentSize = contentRect.x + 20 + (heading.level - 1) * 10;
|
||||
const std::string title = renderer.truncatedText(UI_10_FONT_ID, heading.title.c_str(), contentRect.width - 40 - indentSize);
|
||||
const std::string title =
|
||||
renderer.truncatedText(UI_10_FONT_ID, heading.title.c_str(), contentRect.width - 40 - indentSize);
|
||||
renderer.drawText(UI_10_FONT_ID, indentSize, displayY, title.c_str(), !isSelected);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "MdReaderActivity.h"
|
||||
#include "../Activity.h"
|
||||
#include "MdReaderActivity.h"
|
||||
#include "util/ButtonNavigator.h"
|
||||
|
||||
class MdReaderTocSelectionActivity final : public Activity {
|
||||
@@ -16,7 +16,7 @@ class MdReaderTocSelectionActivity final : public Activity {
|
||||
|
||||
public:
|
||||
explicit MdReaderTocSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
||||
std::vector<MdHeading> headings, int currentHeadingIndex)
|
||||
std::vector<MdHeading> headings, int currentHeadingIndex)
|
||||
: Activity("MdReaderTocSelection", renderer, mappedInput), headings(std::move(headings)), selectorIndex(0) {
|
||||
if (currentHeadingIndex >= 0 && currentHeadingIndex < static_cast<int>(headings.size())) {
|
||||
selectorIndex = currentHeadingIndex;
|
||||
|
||||
@@ -49,7 +49,7 @@ bool ReaderActivity::isXtcFile(const std::string& path) { return FsHelpers::hasX
|
||||
|
||||
bool ReaderActivity::isTxtFile(const std::string& path) { return FsHelpers::hasTxtExtension(path); }
|
||||
|
||||
bool ReaderActivity::isMDFile(const std::string& path) { return FsHelpers::hasMarkdownExtension(path); }
|
||||
bool ReaderActivity::isMdFile(const std::string& path) { return FsHelpers::hasMarkdownExtension(path); }
|
||||
|
||||
bool ReaderActivity::isImageFile(const std::string& path) {
|
||||
return FsHelpers::hasBmpExtension(path) || FsHelpers::hasJpgExtension(path) || FsHelpers::hasPngExtension(path);
|
||||
@@ -167,7 +167,7 @@ void ReaderActivity::onEnter() {
|
||||
return;
|
||||
}
|
||||
onGoToXtcReader(std::move(xtc));
|
||||
} else if (isMDFile(initialBookPath)) {
|
||||
} else if (isMdFile(initialBookPath)) {
|
||||
auto txt = loadTxt(initialBookPath);
|
||||
if (!txt) {
|
||||
onGoBack();
|
||||
|
||||
@@ -16,7 +16,7 @@ class ReaderActivity final : public Activity {
|
||||
static std::unique_ptr<Txt> loadTxt(const std::string& path);
|
||||
static bool isXtcFile(const std::string& path);
|
||||
static bool isTxtFile(const std::string& path);
|
||||
static bool isMDFile(const std::string& path);
|
||||
static bool isMdFile(const std::string& path);
|
||||
static bool isImageFile(const std::string& path);
|
||||
|
||||
static std::string extractFolderPath(const std::string& filePath);
|
||||
|
||||
Reference in New Issue
Block a user