2012-02-12 10:32:41 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file textfile_gui.cpp Implementation of textfile window. */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "fileio_func.h"
|
|
|
|
#include "fontcache.h"
|
|
|
|
#include "gfx_type.h"
|
|
|
|
#include "gfx_func.h"
|
|
|
|
#include "string_func.h"
|
|
|
|
#include "textfile_gui.h"
|
|
|
|
|
|
|
|
#include "widgets/misc_widget.h"
|
|
|
|
|
|
|
|
#include "table/strings.h"
|
|
|
|
|
2014-10-23 10:49:16 +00:00
|
|
|
#if defined(WITH_ZLIB)
|
|
|
|
#include <zlib.h>
|
|
|
|
#endif
|
|
|
|
|
2019-03-10 15:41:01 +00:00
|
|
|
#if defined(WITH_LIBLZMA)
|
2014-10-23 10:49:19 +00:00
|
|
|
#include <lzma.h>
|
|
|
|
#endif
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2012-02-12 10:32:41 +00:00
|
|
|
/** Widgets for the textfile window. */
|
|
|
|
static const NWidgetPart _nested_textfile_widgets[] = {
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
|
|
|
NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
|
|
|
|
NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_TF_CAPTION), SetDataTip(STR_NULL, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
|
2013-10-06 11:29:14 +00:00
|
|
|
NWidget(WWT_TEXTBTN, COLOUR_MAUVE, WID_TF_WRAPTEXT), SetDataTip(STR_TEXTFILE_WRAP_TEXT, STR_TEXTFILE_WRAP_TEXT_TOOLTIP),
|
2013-05-26 19:30:07 +00:00
|
|
|
NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
|
2012-02-12 10:32:41 +00:00
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
|
|
|
NWidget(WWT_PANEL, COLOUR_MAUVE, WID_TF_BACKGROUND), SetMinimalSize(200, 125), SetResize(1, 12), SetScrollbar(WID_TF_VSCROLLBAR),
|
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_VERTICAL),
|
|
|
|
NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_TF_VSCROLLBAR),
|
|
|
|
EndContainer(),
|
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
|
|
|
NWidget(NWID_HSCROLLBAR, COLOUR_MAUVE, WID_TF_HSCROLLBAR),
|
|
|
|
NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
|
|
|
|
EndContainer(),
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Window definition for the textfile window */
|
2013-05-26 19:23:42 +00:00
|
|
|
static WindowDesc _textfile_desc(
|
2013-05-26 19:25:01 +00:00
|
|
|
WDP_CENTER, "textfile", 630, 460,
|
2012-02-12 10:32:41 +00:00
|
|
|
WC_TEXTFILE, WC_NONE,
|
2012-11-11 16:10:43 +00:00
|
|
|
0,
|
2012-02-12 10:32:41 +00:00
|
|
|
_nested_textfile_widgets, lengthof(_nested_textfile_widgets)
|
|
|
|
);
|
|
|
|
|
2013-05-26 19:23:42 +00:00
|
|
|
TextfileWindow::TextfileWindow(TextfileType file_type) : Window(&_textfile_desc), file_type(file_type)
|
2012-02-12 10:32:41 +00:00
|
|
|
{
|
2013-05-26 19:23:42 +00:00
|
|
|
this->CreateNestedTree();
|
2012-02-12 10:32:41 +00:00
|
|
|
this->vscroll = this->GetScrollbar(WID_TF_VSCROLLBAR);
|
|
|
|
this->hscroll = this->GetScrollbar(WID_TF_HSCROLLBAR);
|
2019-01-28 22:51:34 +00:00
|
|
|
this->FinishInitNested(file_type);
|
2012-08-13 18:52:47 +00:00
|
|
|
this->GetWidget<NWidgetCore>(WID_TF_CAPTION)->SetDataTip(STR_TEXTFILE_README_CAPTION + file_type, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
|
2013-10-06 11:29:14 +00:00
|
|
|
|
|
|
|
this->hscroll->SetStepSize(10); // Speed up horizontal scrollbar
|
2012-02-12 10:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ TextfileWindow::~TextfileWindow()
|
|
|
|
{
|
|
|
|
free(this->text);
|
|
|
|
}
|
|
|
|
|
2013-10-06 11:29:14 +00:00
|
|
|
/**
|
|
|
|
* Get the total height of the content displayed in this window, if wrapping is disabled.
|
|
|
|
* @return the height in pixels
|
|
|
|
*/
|
2021-04-29 17:58:26 +00:00
|
|
|
uint TextfileWindow::ReflowContent()
|
2013-10-06 11:29:14 +00:00
|
|
|
{
|
|
|
|
uint height = 0;
|
2021-04-29 17:58:26 +00:00
|
|
|
if (!IsWidgetLowered(WID_TF_WRAPTEXT)) {
|
|
|
|
for (auto &line : this->lines) {
|
|
|
|
line.top = height;
|
|
|
|
height++;
|
|
|
|
line.bottom = height;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int max_width = this->GetWidget<NWidgetCore>(WID_TF_BACKGROUND)->current_x - WD_FRAMETEXT_LEFT - WD_FRAMERECT_RIGHT;
|
|
|
|
for (auto &line : this->lines) {
|
|
|
|
line.top = height;
|
|
|
|
height += GetStringHeight(line.text, max_width, FS_MONO) / FONT_HEIGHT_MONO;
|
|
|
|
line.bottom = height;
|
|
|
|
}
|
2013-10-06 11:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
2021-04-29 17:58:26 +00:00
|
|
|
uint TextfileWindow::GetContentHeight()
|
|
|
|
{
|
|
|
|
if (this->lines.size() == 0) return 0;
|
|
|
|
return this->lines.back().bottom;
|
|
|
|
}
|
|
|
|
|
2012-02-12 10:32:41 +00:00
|
|
|
/* virtual */ void TextfileWindow::UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
|
|
|
|
{
|
|
|
|
switch (widget) {
|
|
|
|
case WID_TF_BACKGROUND:
|
2021-04-29 17:58:26 +00:00
|
|
|
resize->height = FONT_HEIGHT_MONO;
|
2012-02-12 10:32:41 +00:00
|
|
|
|
|
|
|
size->height = 4 * resize->height + TOP_SPACING + BOTTOM_SPACING; // At least 4 lines are visible.
|
2021-01-08 10:16:18 +00:00
|
|
|
size->width = std::max(200u, size->width); // At least 200 pixels wide.
|
2012-02-12 10:32:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-06 11:29:14 +00:00
|
|
|
/** Set scrollbars to the right lengths. */
|
2021-04-29 17:58:26 +00:00
|
|
|
void TextfileWindow::SetupScrollbars(bool force_reflow)
|
2013-10-06 11:29:14 +00:00
|
|
|
{
|
|
|
|
if (IsWidgetLowered(WID_TF_WRAPTEXT)) {
|
2021-04-29 17:58:26 +00:00
|
|
|
/* Reflow is mandatory if text wrapping is on */
|
|
|
|
uint height = this->ReflowContent();
|
|
|
|
this->vscroll->SetCount(std::min<uint>(UINT16_MAX, height));
|
2013-10-06 11:29:14 +00:00
|
|
|
this->hscroll->SetCount(0);
|
|
|
|
} else {
|
2021-04-29 17:58:26 +00:00
|
|
|
uint height = force_reflow ? this->ReflowContent() : this->GetContentHeight();
|
|
|
|
this->vscroll->SetCount(std::min<uint>(UINT16_MAX, height));
|
|
|
|
this->hscroll->SetCount(this->max_length + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
|
2013-10-06 11:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this->SetWidgetDisabledState(WID_TF_HSCROLLBAR, IsWidgetLowered(WID_TF_WRAPTEXT));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ void TextfileWindow::OnClick(Point pt, int widget, int click_count)
|
|
|
|
{
|
|
|
|
switch (widget) {
|
|
|
|
case WID_TF_WRAPTEXT:
|
|
|
|
this->ToggleWidgetLoweredState(WID_TF_WRAPTEXT);
|
|
|
|
this->InvalidateData();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-12 10:32:41 +00:00
|
|
|
/* virtual */ void TextfileWindow::DrawWidget(const Rect &r, int widget) const
|
|
|
|
{
|
|
|
|
if (widget != WID_TF_BACKGROUND) return;
|
|
|
|
|
2013-10-06 11:29:14 +00:00
|
|
|
const int x = r.left + WD_FRAMETEXT_LEFT;
|
|
|
|
const int y = r.top + WD_FRAMETEXT_TOP;
|
|
|
|
const int right = r.right - WD_FRAMETEXT_RIGHT;
|
|
|
|
const int bottom = r.bottom - WD_FRAMETEXT_BOTTOM;
|
2012-02-12 10:32:41 +00:00
|
|
|
|
|
|
|
DrawPixelInfo new_dpi;
|
2013-10-28 12:15:44 +00:00
|
|
|
if (!FillDrawPixelInfo(&new_dpi, x, y, right - x + 1, bottom - y + 1)) return;
|
2012-02-12 10:32:41 +00:00
|
|
|
DrawPixelInfo *old_dpi = _cur_dpi;
|
|
|
|
_cur_dpi = &new_dpi;
|
|
|
|
|
2013-10-06 11:29:14 +00:00
|
|
|
/* Draw content (now coordinates given to DrawString* are local to the new clipping region). */
|
|
|
|
int line_height = FONT_HEIGHT_MONO;
|
2021-04-29 17:58:26 +00:00
|
|
|
int pos = this->vscroll->GetPosition();
|
|
|
|
int cap = this->vscroll->GetCapacity();
|
|
|
|
|
|
|
|
for (auto &line : this->lines) {
|
|
|
|
if (line.bottom < pos) continue;
|
|
|
|
if (line.top > pos + cap) break;
|
2013-10-06 11:29:14 +00:00
|
|
|
|
2021-04-29 17:58:26 +00:00
|
|
|
int y_offset = (line.top - pos) * line_height;
|
2013-10-06 11:29:14 +00:00
|
|
|
if (IsWidgetLowered(WID_TF_WRAPTEXT)) {
|
2021-04-29 17:58:26 +00:00
|
|
|
DrawStringMultiLine(0, right - x, y_offset, bottom - y, line.text, TC_WHITE, SA_TOP | SA_LEFT, false, FS_MONO);
|
2013-10-06 11:29:14 +00:00
|
|
|
} else {
|
2021-04-29 17:58:26 +00:00
|
|
|
DrawString(-this->hscroll->GetPosition(), right - x, y_offset, line.text, TC_WHITE, SA_TOP | SA_LEFT, false, FS_MONO);
|
2013-10-06 11:29:14 +00:00
|
|
|
}
|
2012-02-12 10:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_cur_dpi = old_dpi;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ void TextfileWindow::OnResize()
|
|
|
|
{
|
|
|
|
this->vscroll->SetCapacityFromWidget(this, WID_TF_BACKGROUND, TOP_SPACING + BOTTOM_SPACING);
|
|
|
|
this->hscroll->SetCapacityFromWidget(this, WID_TF_BACKGROUND);
|
2013-10-06 11:29:14 +00:00
|
|
|
|
2021-04-29 17:58:26 +00:00
|
|
|
this->SetupScrollbars(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ void TextfileWindow::OnInvalidateData(int data, bool gui_scope)
|
|
|
|
{
|
|
|
|
if (!gui_scope) return;
|
|
|
|
|
|
|
|
this->SetupScrollbars(true);
|
2012-02-12 10:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ void TextfileWindow::Reset()
|
|
|
|
{
|
|
|
|
this->search_iterator = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ FontSize TextfileWindow::DefaultSize()
|
|
|
|
{
|
|
|
|
return FS_MONO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ const char *TextfileWindow::NextString()
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->search_iterator >= this->lines.size()) return nullptr;
|
2012-02-12 10:32:41 +00:00
|
|
|
|
2021-04-29 17:58:26 +00:00
|
|
|
return this->lines[this->search_iterator++].text;
|
2012-02-12 10:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ bool TextfileWindow::Monospace()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-25 01:02:20 +00:00
|
|
|
/* virtual */ void TextfileWindow::SetFontNames(FreeTypeSettings *settings, const char *font_name, const void *os_data)
|
2012-02-12 10:32:41 +00:00
|
|
|
{
|
2021-02-13 21:51:18 +00:00
|
|
|
#if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
|
2021-04-28 15:10:15 +00:00
|
|
|
settings->mono.font = font_name;
|
2018-11-25 01:02:20 +00:00
|
|
|
settings->mono.os_handle = os_data;
|
|
|
|
#endif
|
2012-02-12 10:32:41 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 10:49:16 +00:00
|
|
|
#if defined(WITH_ZLIB)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do an in-memory gunzip operation. This works on a raw deflate stream,
|
|
|
|
* or a file with gzip or zlib header.
|
|
|
|
* @param bufp A pointer to a buffer containing the input data. This
|
|
|
|
* buffer will be freed and replaced by a buffer containing
|
|
|
|
* the uncompressed data.
|
|
|
|
* @param sizep A pointer to the buffer size. Before the call, the value
|
|
|
|
* pointed to should contain the size of the input buffer.
|
|
|
|
* After the call, it contains the size of the uncompressed
|
|
|
|
* data.
|
|
|
|
*
|
2019-04-10 21:07:06 +00:00
|
|
|
* When decompressing fails, *bufp is set to nullptr and *sizep to 0. The
|
2014-10-23 10:49:16 +00:00
|
|
|
* compressed buffer passed in is still freed in this case.
|
|
|
|
*/
|
|
|
|
static void Gunzip(byte **bufp, size_t *sizep)
|
|
|
|
{
|
|
|
|
static const int BLOCKSIZE = 8192;
|
2019-04-10 21:07:06 +00:00
|
|
|
byte *buf = nullptr;
|
2014-10-23 10:49:16 +00:00
|
|
|
size_t alloc_size = 0;
|
|
|
|
z_stream z;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
memset(&z, 0, sizeof(z));
|
|
|
|
z.next_in = *bufp;
|
2015-08-10 20:24:13 +00:00
|
|
|
z.avail_in = (uInt)*sizep;
|
2014-10-23 10:49:16 +00:00
|
|
|
|
|
|
|
/* window size = 15, add 32 to enable gzip or zlib header processing */
|
|
|
|
res = inflateInit2(&z, 15 + 32);
|
|
|
|
/* Z_BUF_ERROR just means we need more space */
|
|
|
|
while (res == Z_OK || (res == Z_BUF_ERROR && z.avail_out == 0)) {
|
|
|
|
/* When we get here, we're either just starting, or
|
|
|
|
* inflate is out of output space - allocate more */
|
|
|
|
alloc_size += BLOCKSIZE;
|
|
|
|
z.avail_out += BLOCKSIZE;
|
|
|
|
buf = ReallocT(buf, alloc_size);
|
|
|
|
z.next_out = buf + alloc_size - z.avail_out;
|
|
|
|
res = inflate(&z, Z_FINISH);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(*bufp);
|
|
|
|
inflateEnd(&z);
|
|
|
|
|
|
|
|
if (res == Z_STREAM_END) {
|
|
|
|
*bufp = buf;
|
|
|
|
*sizep = alloc_size - z.avail_out;
|
|
|
|
} else {
|
|
|
|
/* Something went wrong */
|
2019-04-10 21:07:06 +00:00
|
|
|
*bufp = nullptr;
|
2014-10-23 10:49:16 +00:00
|
|
|
*sizep = 0;
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-03-10 15:41:01 +00:00
|
|
|
#if defined(WITH_LIBLZMA)
|
2014-10-23 10:49:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Do an in-memory xunzip operation. This works on a .xz or (legacy)
|
|
|
|
* .lzma file.
|
|
|
|
* @param bufp A pointer to a buffer containing the input data. This
|
|
|
|
* buffer will be freed and replaced by a buffer containing
|
|
|
|
* the uncompressed data.
|
|
|
|
* @param sizep A pointer to the buffer size. Before the call, the value
|
|
|
|
* pointed to should contain the size of the input buffer.
|
|
|
|
* After the call, it contains the size of the uncompressed
|
|
|
|
* data.
|
|
|
|
*
|
2019-04-10 21:07:06 +00:00
|
|
|
* When decompressing fails, *bufp is set to nullptr and *sizep to 0. The
|
2014-10-23 10:49:19 +00:00
|
|
|
* compressed buffer passed in is still freed in this case.
|
|
|
|
*/
|
|
|
|
static void Xunzip(byte **bufp, size_t *sizep)
|
|
|
|
{
|
|
|
|
static const int BLOCKSIZE = 8192;
|
2019-04-10 21:07:06 +00:00
|
|
|
byte *buf = nullptr;
|
2014-10-23 10:49:19 +00:00
|
|
|
size_t alloc_size = 0;
|
|
|
|
lzma_stream z = LZMA_STREAM_INIT;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
z.next_in = *bufp;
|
|
|
|
z.avail_in = *sizep;
|
|
|
|
|
|
|
|
res = lzma_auto_decoder(&z, UINT64_MAX, LZMA_CONCATENATED);
|
|
|
|
/* Z_BUF_ERROR just means we need more space */
|
|
|
|
while (res == LZMA_OK || (res == LZMA_BUF_ERROR && z.avail_out == 0)) {
|
|
|
|
/* When we get here, we're either just starting, or
|
|
|
|
* inflate is out of output space - allocate more */
|
|
|
|
alloc_size += BLOCKSIZE;
|
|
|
|
z.avail_out += BLOCKSIZE;
|
|
|
|
buf = ReallocT(buf, alloc_size);
|
|
|
|
z.next_out = buf + alloc_size - z.avail_out;
|
|
|
|
res = lzma_code(&z, LZMA_FINISH);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(*bufp);
|
|
|
|
lzma_end(&z);
|
|
|
|
|
|
|
|
if (res == LZMA_STREAM_END) {
|
|
|
|
*bufp = buf;
|
|
|
|
*sizep = alloc_size - z.avail_out;
|
|
|
|
} else {
|
|
|
|
/* Something went wrong */
|
2019-04-10 21:07:06 +00:00
|
|
|
*bufp = nullptr;
|
2014-10-23 10:49:19 +00:00
|
|
|
*sizep = 0;
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2012-02-12 10:32:41 +00:00
|
|
|
/**
|
2013-10-06 11:29:14 +00:00
|
|
|
* Loads the textfile text from file and setup #lines.
|
2012-02-12 10:32:41 +00:00
|
|
|
*/
|
|
|
|
/* virtual */ void TextfileWindow::LoadTextfile(const char *textfile, Subdirectory dir)
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (textfile == nullptr) return;
|
2012-02-12 10:32:41 +00:00
|
|
|
|
2018-09-20 22:44:14 +00:00
|
|
|
this->lines.clear();
|
2012-02-12 10:32:41 +00:00
|
|
|
|
|
|
|
/* Get text from file */
|
|
|
|
size_t filesize;
|
|
|
|
FILE *handle = FioFOpenFile(textfile, "rb", dir, &filesize);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (handle == nullptr) return;
|
2012-02-12 10:32:41 +00:00
|
|
|
|
2014-10-23 10:49:16 +00:00
|
|
|
this->text = ReallocT(this->text, filesize);
|
2012-02-12 10:32:41 +00:00
|
|
|
size_t read = fread(this->text, 1, filesize, handle);
|
|
|
|
fclose(handle);
|
|
|
|
|
|
|
|
if (read != filesize) return;
|
|
|
|
|
2019-03-10 15:41:01 +00:00
|
|
|
#if defined(WITH_ZLIB) || defined(WITH_LIBLZMA)
|
2014-10-23 10:49:16 +00:00
|
|
|
const char *suffix = strrchr(textfile, '.');
|
2019-04-10 21:07:06 +00:00
|
|
|
if (suffix == nullptr) return;
|
2014-10-23 10:49:19 +00:00
|
|
|
#endif
|
2014-10-23 10:49:16 +00:00
|
|
|
|
2014-10-23 10:49:19 +00:00
|
|
|
#if defined(WITH_ZLIB)
|
2014-10-23 10:49:16 +00:00
|
|
|
/* In-place gunzip */
|
|
|
|
if (strcmp(suffix, ".gz") == 0) Gunzip((byte**)&this->text, &filesize);
|
|
|
|
#endif
|
|
|
|
|
2019-03-10 15:41:01 +00:00
|
|
|
#if defined(WITH_LIBLZMA)
|
2014-10-23 10:49:19 +00:00
|
|
|
/* In-place xunzip */
|
|
|
|
if (strcmp(suffix, ".xz") == 0) Xunzip((byte**)&this->text, &filesize);
|
|
|
|
#endif
|
|
|
|
|
2014-10-23 10:49:16 +00:00
|
|
|
if (!this->text) return;
|
|
|
|
|
|
|
|
/* Add space for trailing \0 */
|
|
|
|
this->text = ReallocT(this->text, filesize + 1);
|
2012-02-12 10:32:41 +00:00
|
|
|
this->text[filesize] = '\0';
|
|
|
|
|
2021-05-29 09:21:38 +00:00
|
|
|
/* Replace tabs and line feeds with a space since StrMakeValidInPlace removes those. */
|
2012-02-12 10:32:41 +00:00
|
|
|
for (char *p = this->text; *p != '\0'; p++) {
|
|
|
|
if (*p == '\t' || *p == '\r') *p = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for the byte-order-mark, and skip it if needed. */
|
2020-12-14 19:25:01 +00:00
|
|
|
char *p = this->text + (strncmp(u8"\ufeff", this->text, 3) == 0 ? 3 : 0);
|
2012-02-12 10:32:41 +00:00
|
|
|
|
|
|
|
/* Make sure the string is a valid UTF-8 sequence. */
|
2021-05-29 09:21:38 +00:00
|
|
|
StrMakeValidInPlace(p, this->text + filesize, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
|
2012-02-12 10:32:41 +00:00
|
|
|
|
|
|
|
/* Split the string on newlines. */
|
2021-04-29 17:58:26 +00:00
|
|
|
int row = 0;
|
|
|
|
this->lines.emplace_back(row, p);
|
2012-02-12 10:32:41 +00:00
|
|
|
for (; *p != '\0'; p++) {
|
|
|
|
if (*p == '\n') {
|
|
|
|
*p = '\0';
|
2021-04-29 17:58:26 +00:00
|
|
|
this->lines.emplace_back(++row, p + 1);
|
2012-02-12 10:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 17:58:26 +00:00
|
|
|
/* Calculate maximum text line length. */
|
|
|
|
uint max_length = 0;
|
|
|
|
for (auto &line : this->lines) {
|
|
|
|
max_length = std::max(max_length, GetStringBoundingBox(line.text, FS_MONO).width);
|
|
|
|
}
|
|
|
|
this->max_length = max_length;
|
|
|
|
|
2012-02-12 10:32:41 +00:00
|
|
|
CheckForMissingGlyphs(true, this);
|
|
|
|
}
|
2012-02-12 10:51:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Search a textfile file next to the given content.
|
|
|
|
* @param type The type of the textfile to search for.
|
|
|
|
* @param dir The subdirectory to search in.
|
|
|
|
* @param filename The filename of the content to look for.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return The path to the textfile, \c nullptr otherwise.
|
2012-02-12 10:51:43 +00:00
|
|
|
*/
|
|
|
|
const char *GetTextfile(TextfileType type, Subdirectory dir, const char *filename)
|
|
|
|
{
|
|
|
|
static const char * const prefixes[] = {
|
|
|
|
"readme",
|
|
|
|
"changelog",
|
|
|
|
"license",
|
|
|
|
};
|
2020-12-27 10:44:22 +00:00
|
|
|
static_assert(lengthof(prefixes) == TFT_END);
|
2012-02-12 10:51:43 +00:00
|
|
|
|
|
|
|
const char *prefix = prefixes[type];
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (filename == nullptr) return nullptr;
|
2012-02-12 10:51:43 +00:00
|
|
|
|
|
|
|
static char file_path[MAX_PATH];
|
|
|
|
strecpy(file_path, filename, lastof(file_path));
|
|
|
|
|
|
|
|
char *slash = strrchr(file_path, PATHSEPCHAR);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (slash == nullptr) return nullptr;
|
2012-02-12 10:51:43 +00:00
|
|
|
|
2014-10-23 10:49:14 +00:00
|
|
|
static const char * const exts[] = {
|
|
|
|
"txt",
|
2014-10-23 10:49:16 +00:00
|
|
|
#if defined(WITH_ZLIB)
|
|
|
|
"txt.gz",
|
2014-10-23 10:49:19 +00:00
|
|
|
#endif
|
2019-03-10 15:41:01 +00:00
|
|
|
#if defined(WITH_LIBLZMA)
|
2014-10-23 10:49:19 +00:00
|
|
|
"txt.xz",
|
2014-10-23 10:49:16 +00:00
|
|
|
#endif
|
2014-10-23 10:49:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < lengthof(exts); i++) {
|
|
|
|
seprintf(slash + 1, lastof(file_path), "%s_%s.%s", prefix, GetCurrentLanguageIsoCode(), exts[i]);
|
|
|
|
if (FioCheckFileExists(file_path, dir)) return file_path;
|
2012-02-12 10:51:43 +00:00
|
|
|
|
2014-10-23 10:49:14 +00:00
|
|
|
seprintf(slash + 1, lastof(file_path), "%s_%.2s.%s", prefix, GetCurrentLanguageIsoCode(), exts[i]);
|
|
|
|
if (FioCheckFileExists(file_path, dir)) return file_path;
|
2012-02-12 10:51:43 +00:00
|
|
|
|
2014-10-23 10:49:14 +00:00
|
|
|
seprintf(slash + 1, lastof(file_path), "%s.%s", prefix, exts[i]);
|
|
|
|
if (FioCheckFileExists(file_path, dir)) return file_path;
|
|
|
|
}
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2012-02-12 10:51:43 +00:00
|
|
|
}
|