Merge branch 'master' into jgrpp
# Conflicts: # .github/workflows/ci-build.yml # .github/workflows/release-linux.yml # src/aircraft_cmd.cpp # src/airport_gui.cpp # src/articulated_vehicles.cpp # src/build_vehicle_gui.cpp # src/company_gui.cpp # src/genworld_gui.cpp # src/gfx_layout.cpp # src/misc_gui.cpp # src/newgrf.cpp # src/newgrf_config.h # src/newgrf_engine.cpp # src/news_gui.cpp # src/order_gui.cpp # src/roadveh_cmd.cpp # src/saveload/saveload.h # src/saveload/vehicle_sl.cpp # src/ship_cmd.cpp # src/statusbar_gui.cpp # src/table/settings/network_private_settings.ini # src/table/settings/network_settings.ini # src/toolbar_gui.cpp # src/train_cmd.cpp # src/vehicle_gui.cpppull/532/head
commit
2ed248a61d
@ -0,0 +1,65 @@
|
||||
#[=======================================================================[.rst:
|
||||
FindHarfBuzz
|
||||
-------
|
||||
|
||||
Finds the harfbuzz library.
|
||||
|
||||
Result Variables
|
||||
^^^^^^^^^^^^^^^^
|
||||
|
||||
This will define the following variables:
|
||||
|
||||
``Harfbuzz_FOUND``
|
||||
True if the system has the harfbuzz library.
|
||||
``Harfbuzz_INCLUDE_DIRS``
|
||||
Include directories needed to use harfbuzz.
|
||||
``Harfbuzz_LIBRARIES``
|
||||
Libraries needed to link to harfbuzz.
|
||||
``Harfbuzz_VERSION``
|
||||
The version of the harfbuzz library which was found.
|
||||
|
||||
Cache Variables
|
||||
^^^^^^^^^^^^^^^
|
||||
|
||||
The following cache variables may also be set:
|
||||
|
||||
``Harfbuzz_INCLUDE_DIR``
|
||||
The directory containing ``hb.h``.
|
||||
``Harfbuzz_LIBRARY``
|
||||
The path to the harfbuzz library.
|
||||
|
||||
#]=======================================================================]
|
||||
|
||||
find_package(PkgConfig QUIET)
|
||||
pkg_check_modules(PC_Harfbuzz QUIET harfbuzz)
|
||||
|
||||
find_path(Harfbuzz_INCLUDE_DIR
|
||||
NAMES hb.h
|
||||
PATHS ${PC_Harfbuzz_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
find_library(Harfbuzz_LIBRARY
|
||||
NAMES harfbuzz
|
||||
PATHS ${PC_Harfbuzz_LIBRARY_DIRS}
|
||||
)
|
||||
|
||||
set(Harfbuzz_VERSION ${PC_Harfbuzz_VERSION})
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args(Harfbuzz
|
||||
FOUND_VAR Harfbuzz_FOUND
|
||||
REQUIRED_VARS
|
||||
Harfbuzz_LIBRARY
|
||||
Harfbuzz_INCLUDE_DIR
|
||||
VERSION_VAR Harfbuzz_VERSION
|
||||
)
|
||||
|
||||
if(Harfbuzz_FOUND)
|
||||
set(Harfbuzz_LIBRARIES ${Harfbuzz_LIBRARY})
|
||||
set(Harfbuzz_INCLUDE_DIRS ${Harfbuzz_INCLUDE_DIR})
|
||||
endif()
|
||||
|
||||
mark_as_advanced(
|
||||
Harfbuzz_INCLUDE_DIR
|
||||
Harfbuzz_LIBRARY
|
||||
)
|
@ -1,4 +1,4 @@
|
||||
FROM emscripten/emsdk:3.1.28
|
||||
FROM emscripten/emsdk:3.1.37
|
||||
|
||||
COPY emsdk-liblzma.patch /
|
||||
RUN cd /emsdk/upstream/emscripten && patch -p1 < /emsdk-liblzma.patch
|
||||
|
@ -0,0 +1,5 @@
|
||||
add_files(
|
||||
scriptrun.cpp
|
||||
scriptrun.h
|
||||
CONDITION ICU_i18n_FOUND
|
||||
)
|
@ -0,0 +1,208 @@
|
||||
// © 2016 and later: Unicode, Inc. and others.
|
||||
// License & terms of use: http://www.unicode.org/copyright.html
|
||||
/*
|
||||
*******************************************************************************
|
||||
*
|
||||
* Copyright (C) 1999-2016, International Business Machines
|
||||
* Corporation and others. All Rights Reserved.
|
||||
*
|
||||
*******************************************************************************
|
||||
* file name: scrptrun.cpp
|
||||
*
|
||||
* created on: 10/17/2001
|
||||
* created by: Eric R. Mader
|
||||
*/
|
||||
|
||||
#include <unicode/utypes.h>
|
||||
#include <unicode/uscript.h>
|
||||
|
||||
#include "scriptrun.h"
|
||||
|
||||
// Copied from cmemory.h
|
||||
#define UPRV_LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
|
||||
|
||||
U_NAMESPACE_BEGIN
|
||||
|
||||
const char ScriptRun::fgClassID=0;
|
||||
|
||||
UChar32 ScriptRun::pairedChars[] = {
|
||||
0x0028, 0x0029, // ascii paired punctuation
|
||||
0x003c, 0x003e,
|
||||
0x005b, 0x005d,
|
||||
0x007b, 0x007d,
|
||||
0x00ab, 0x00bb, // guillemets
|
||||
0x2018, 0x2019, // general punctuation
|
||||
0x201c, 0x201d,
|
||||
0x2039, 0x203a,
|
||||
0x3008, 0x3009, // chinese paired punctuation
|
||||
0x300a, 0x300b,
|
||||
0x300c, 0x300d,
|
||||
0x300e, 0x300f,
|
||||
0x3010, 0x3011,
|
||||
0x3014, 0x3015,
|
||||
0x3016, 0x3017,
|
||||
0x3018, 0x3019,
|
||||
0x301a, 0x301b
|
||||
};
|
||||
|
||||
const int32_t ScriptRun::pairedCharCount = UPRV_LENGTHOF(pairedChars);
|
||||
const int32_t ScriptRun::pairedCharPower = 1 << highBit(pairedCharCount);
|
||||
const int32_t ScriptRun::pairedCharExtra = pairedCharCount - pairedCharPower;
|
||||
|
||||
int8_t ScriptRun::highBit(int32_t value)
|
||||
{
|
||||
if (value <= 0) {
|
||||
return -32;
|
||||
}
|
||||
|
||||
int8_t bit = 0;
|
||||
|
||||
if (value >= 1 << 16) {
|
||||
value >>= 16;
|
||||
bit += 16;
|
||||
}
|
||||
|
||||
if (value >= 1 << 8) {
|
||||
value >>= 8;
|
||||
bit += 8;
|
||||
}
|
||||
|
||||
if (value >= 1 << 4) {
|
||||
value >>= 4;
|
||||
bit += 4;
|
||||
}
|
||||
|
||||
if (value >= 1 << 2) {
|
||||
value >>= 2;
|
||||
bit += 2;
|
||||
}
|
||||
|
||||
if (value >= 1 << 1) {
|
||||
value >>= 1;
|
||||
bit += 1;
|
||||
}
|
||||
|
||||
return bit;
|
||||
}
|
||||
|
||||
int32_t ScriptRun::getPairIndex(UChar32 ch)
|
||||
{
|
||||
int32_t probe = pairedCharPower;
|
||||
int32_t index = 0;
|
||||
|
||||
if (ch >= pairedChars[pairedCharExtra]) {
|
||||
index = pairedCharExtra;
|
||||
}
|
||||
|
||||
while (probe > (1 << 0)) {
|
||||
probe >>= 1;
|
||||
|
||||
if (ch >= pairedChars[index + probe]) {
|
||||
index += probe;
|
||||
}
|
||||
}
|
||||
|
||||
if (pairedChars[index] != ch) {
|
||||
index = -1;
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
UBool ScriptRun::sameScript(int32_t scriptOne, int32_t scriptTwo)
|
||||
{
|
||||
return scriptOne <= USCRIPT_INHERITED || scriptTwo <= USCRIPT_INHERITED || scriptOne == scriptTwo;
|
||||
}
|
||||
|
||||
UBool ScriptRun::next()
|
||||
{
|
||||
int32_t startSP = parenSP; // used to find the first new open character
|
||||
UErrorCode error = U_ZERO_ERROR;
|
||||
|
||||
// if we've fallen off the end of the text, we're done
|
||||
if (scriptEnd >= charLimit) {
|
||||
return false;
|
||||
}
|
||||
|
||||
scriptCode = USCRIPT_COMMON;
|
||||
|
||||
for (scriptStart = scriptEnd; scriptEnd < charLimit; scriptEnd += 1) {
|
||||
char16_t high = charArray[scriptEnd];
|
||||
UChar32 ch = high;
|
||||
|
||||
// if the character is a high surrogate and it's not the last one
|
||||
// in the text, see if it's followed by a low surrogate
|
||||
if (high >= 0xD800 && high <= 0xDBFF && scriptEnd < charLimit - 1)
|
||||
{
|
||||
char16_t low = charArray[scriptEnd + 1];
|
||||
|
||||
// if it is followed by a low surrogate,
|
||||
// consume it and form the full character
|
||||
if (low >= 0xDC00 && low <= 0xDFFF) {
|
||||
ch = (high - 0xD800) * 0x0400 + low - 0xDC00 + 0x10000;
|
||||
scriptEnd += 1;
|
||||
}
|
||||
}
|
||||
|
||||
UScriptCode sc = uscript_getScript(ch, &error);
|
||||
int32_t pairIndex = getPairIndex(ch);
|
||||
|
||||
// Paired character handling:
|
||||
//
|
||||
// if it's an open character, push it onto the stack.
|
||||
// if it's a close character, find the matching open on the
|
||||
// stack, and use that script code. Any non-matching open
|
||||
// characters above it on the stack will be poped.
|
||||
if (pairIndex >= 0) {
|
||||
if ((pairIndex & 1) == 0) {
|
||||
parenStack[++parenSP].pairIndex = pairIndex;
|
||||
parenStack[parenSP].scriptCode = scriptCode;
|
||||
} else if (parenSP >= 0) {
|
||||
int32_t pi = pairIndex & ~1;
|
||||
|
||||
while (parenSP >= 0 && parenStack[parenSP].pairIndex != pi) {
|
||||
parenSP -= 1;
|
||||
}
|
||||
|
||||
if (parenSP < startSP) {
|
||||
startSP = parenSP;
|
||||
}
|
||||
|
||||
if (parenSP >= 0) {
|
||||
sc = parenStack[parenSP].scriptCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sameScript(scriptCode, sc)) {
|
||||
if (scriptCode <= USCRIPT_INHERITED && sc > USCRIPT_INHERITED) {
|
||||
scriptCode = sc;
|
||||
|
||||
// now that we have a final script code, fix any open
|
||||
// characters we pushed before we knew the script code.
|
||||
while (startSP < parenSP) {
|
||||
parenStack[++startSP].scriptCode = scriptCode;
|
||||
}
|
||||
}
|
||||
|
||||
// if this character is a close paired character,
|
||||
// pop it from the stack
|
||||
if (pairIndex >= 0 && (pairIndex & 1) != 0 && parenSP >= 0) {
|
||||
parenSP -= 1;
|
||||
startSP -= 1;
|
||||
}
|
||||
} else {
|
||||
// if the run broke on a surrogate pair,
|
||||
// end it before the high surrogate
|
||||
if (ch >= 0x10000) {
|
||||
scriptEnd -= 1;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
U_NAMESPACE_END
|
@ -0,0 +1,159 @@
|
||||
// © 2016 and later: Unicode, Inc. and others.
|
||||
// License & terms of use: http://www.unicode.org/copyright.html
|
||||
/*
|
||||
*******************************************************************************
|
||||
*
|
||||
* Copyright (C) 1999-2003, International Business Machines
|
||||
* Corporation and others. All Rights Reserved.
|
||||
*
|
||||
*******************************************************************************
|
||||
* file name: scrptrun.h
|
||||
*
|
||||
* created on: 10/17/2001
|
||||
* created by: Eric R. Mader
|
||||
*/
|
||||
|
||||
#ifndef __SCRPTRUN_H
|
||||
#define __SCRPTRUN_H
|
||||
|
||||
#include <unicode/utypes.h>
|
||||
#include <unicode/uobject.h>
|
||||
#include <unicode/uscript.h>
|
||||
|
||||
U_NAMESPACE_BEGIN
|
||||
|
||||
struct ScriptRecord
|
||||
{
|
||||
UChar32 startChar;
|
||||
UChar32 endChar;
|
||||
UScriptCode scriptCode;
|
||||
};
|
||||
|
||||
struct ParenStackEntry
|
||||
{
|
||||
int32_t pairIndex;
|
||||
UScriptCode scriptCode;
|
||||
};
|
||||
|
||||
class ScriptRun : public UObject {
|
||||
public:
|
||||
ScriptRun();
|
||||
|
||||
ScriptRun(const char16_t *chars, int32_t length);
|
||||
|
||||
ScriptRun(const char16_t *chars, int32_t start, int32_t length);
|
||||
|
||||
void reset();
|
||||
|
||||
void reset(int32_t start, int32_t count);
|
||||
|
||||
void reset(const char16_t *chars, int32_t start, int32_t length);
|
||||
|
||||
int32_t getScriptStart();
|
||||
|
||||
int32_t getScriptEnd();
|
||||
|
||||
UScriptCode getScriptCode();
|
||||
|
||||
UBool next();
|
||||
|
||||
/**
|
||||
* ICU "poor man's RTTI", returns a UClassID for the actual class.
|
||||
*
|
||||
* @stable ICU 2.2
|
||||
*/
|
||||
virtual inline UClassID getDynamicClassID() const override { return getStaticClassID(); }
|
||||
|
||||
/**
|
||||
* ICU "poor man's RTTI", returns a UClassID for this class.
|
||||
*
|
||||
* @stable ICU 2.2
|
||||
*/
|
||||
static inline UClassID getStaticClassID() { return (UClassID)const_cast<char *>(&fgClassID); }
|
||||
|
||||
private:
|
||||
|
||||
static UBool sameScript(int32_t scriptOne, int32_t scriptTwo);
|
||||
|
||||
int32_t charStart;
|
||||
int32_t charLimit;
|
||||
const char16_t *charArray;
|
||||
|
||||
int32_t scriptStart;
|
||||
int32_t scriptEnd;
|
||||
UScriptCode scriptCode;
|
||||
|
||||
ParenStackEntry parenStack[128];
|
||||
int32_t parenSP;
|
||||
|
||||
static int8_t highBit(int32_t value);
|
||||
static int32_t getPairIndex(UChar32 ch);
|
||||
|
||||
static UChar32 pairedChars[];
|
||||
static const int32_t pairedCharCount;
|
||||
static const int32_t pairedCharPower;
|
||||
static const int32_t pairedCharExtra;
|
||||
|
||||
/**
|
||||
* The address of this static class variable serves as this class's ID
|
||||
* for ICU "poor man's RTTI".
|
||||
*/
|
||||
static const char fgClassID;
|
||||
};
|
||||
|
||||
inline ScriptRun::ScriptRun()
|
||||
{
|
||||
reset(nullptr, 0, 0);
|
||||
}
|
||||
|
||||
inline ScriptRun::ScriptRun(const char16_t *chars, int32_t length)
|
||||
{
|
||||
reset(chars, 0, length);
|
||||
}
|
||||
|
||||
inline ScriptRun::ScriptRun(const char16_t *chars, int32_t start, int32_t length)
|
||||
{
|
||||
reset(chars, start, length);
|
||||
}
|
||||
|
||||
inline int32_t ScriptRun::getScriptStart()
|
||||
{
|
||||
return scriptStart;
|
||||
}
|
||||
|
||||
inline int32_t ScriptRun::getScriptEnd()
|
||||
{
|
||||
return scriptEnd;
|
||||
}
|
||||
|
||||
inline UScriptCode ScriptRun::getScriptCode()
|
||||
{
|
||||
return scriptCode;
|
||||
}
|
||||
|
||||
inline void ScriptRun::reset()
|
||||
{
|
||||
scriptStart = charStart;
|
||||
scriptEnd = charStart;
|
||||
scriptCode = USCRIPT_INVALID_CODE;
|
||||
parenSP = -1;
|
||||
}
|
||||
|
||||
inline void ScriptRun::reset(int32_t start, int32_t length)
|
||||
{
|
||||
charStart = start;
|
||||
charLimit = start + length;
|
||||
|
||||
reset();
|
||||
}
|
||||
|
||||
inline void ScriptRun::reset(const char16_t *chars, int32_t start, int32_t length)
|
||||
{
|
||||
charArray = chars;
|
||||
|
||||
reset(start, length);
|
||||
}
|
||||
|
||||
U_NAMESPACE_END
|
||||
|
||||
#endif
|
@ -0,0 +1,375 @@
|
||||
/*
|
||||
* 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 gfx_layout_fallback.cpp Handling of laying out text as fallback. */
|
||||
|
||||
#include "stdafx.h"
|
||||
|
||||
#include "gfx_layout_fallback.h"
|
||||
#include "string_func.h"
|
||||
#include "zoom_func.h"
|
||||
|
||||
#include "table/control_codes.h"
|
||||
|
||||
#include "safeguards.h"
|
||||
|
||||
/*** Paragraph layout ***/
|
||||
/**
|
||||
* Class handling the splitting of a paragraph of text into lines and
|
||||
* visual runs.
|
||||
*
|
||||
* One constructs this class with the text that needs to be split into
|
||||
* lines. Then nextLine is called with the maximum width until nullptr is
|
||||
* returned. Each nextLine call creates VisualRuns which contain the
|
||||
* length of text that are to be drawn with the same font. In other
|
||||
* words, the result of this class is a list of sub strings with their
|
||||
* font. The sub strings are then already fully laid out, and only
|
||||
* need actual drawing.
|
||||
*
|
||||
* The positions in a visual run are sequential pairs of X,Y of the
|
||||
* begin of each of the glyphs plus an extra pair to mark the end.
|
||||
*
|
||||
* @note This variant does not handle right-to-left properly.
|
||||
*/
|
||||
class FallbackParagraphLayout : public ParagraphLayouter {
|
||||
public:
|
||||
/** Visual run contains data about the bit of text with the same font. */
|
||||
class FallbackVisualRun : public ParagraphLayouter::VisualRun {
|
||||
Font *font; ///< The font used to layout these.
|
||||
GlyphID *glyphs; ///< The glyphs we're drawing.
|
||||
float *positions; ///< The positions of the glyphs.
|
||||
int *glyph_to_char; ///< The char index of the glyphs.
|
||||
int glyph_count; ///< The number of glyphs.
|
||||
|
||||
public:
|
||||
FallbackVisualRun(Font *font, const WChar *chars, int glyph_count, int x);
|
||||
FallbackVisualRun(FallbackVisualRun &&other) noexcept;
|
||||
~FallbackVisualRun() override;
|
||||
const Font *GetFont() const override;
|
||||
int GetGlyphCount() const override;
|
||||
const GlyphID *GetGlyphs() const override;
|
||||
const float *GetPositions() const override;
|
||||
int GetLeading() const override;
|
||||
const int *GetGlyphToCharMap() const override;
|
||||
};
|
||||
|
||||
/** A single line worth of VisualRuns. */
|
||||
class FallbackLine : public std::vector<FallbackVisualRun>, public ParagraphLayouter::Line {
|
||||
public:
|
||||
int GetLeading() const override;
|
||||
int GetWidth() const override;
|
||||
int CountRuns() const override;
|
||||
const ParagraphLayouter::VisualRun &GetVisualRun(int run) const override;
|
||||
|
||||
int GetInternalCharLength(WChar c) const override { return 1; }
|
||||
};
|
||||
|
||||
const WChar *buffer_begin; ///< Begin of the buffer.
|
||||
const WChar *buffer; ///< The current location in the buffer.
|
||||
FontMap &runs; ///< The fonts we have to use for this paragraph.
|
||||
|
||||
FallbackParagraphLayout(WChar *buffer, int length, FontMap &runs);
|
||||
void Reflow() override;
|
||||
std::unique_ptr<const Line> NextLine(int max_width) override;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the actual ParagraphLayout for the given buffer.
|
||||
* @param buff The begin of the buffer.
|
||||
* @param buff_end The location after the last element in the buffer.
|
||||
* @param fontMapping THe mapping of the fonts.
|
||||
* @return The ParagraphLayout instance.
|
||||
*/
|
||||
/* static */ ParagraphLayouter *FallbackParagraphLayoutFactory::GetParagraphLayout(WChar *buff, WChar *buff_end, FontMap &fontMapping)
|
||||
{
|
||||
return new FallbackParagraphLayout(buff, buff_end - buff, fontMapping);
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a wide character to the internal buffer.
|
||||
* @param buff The buffer to append to.
|
||||
* @param buffer_last The end of the buffer.
|
||||
* @param c The character to add.
|
||||
* @return The number of buffer spaces that were used.
|
||||
*/
|
||||
/* static */ size_t FallbackParagraphLayoutFactory::AppendToBuffer(WChar *buff, const WChar *buffer_last, WChar c)
|
||||
{
|
||||
*buff = c;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the visual run.
|
||||
* @param font The font to use for this run.
|
||||
* @param chars The characters to use for this run.
|
||||
* @param char_count The number of characters in this run.
|
||||
* @param x The initial x position for this run.
|
||||
*/
|
||||
FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun(Font *font, const WChar *chars, int char_count, int x) :
|
||||
font(font), glyph_count(char_count)
|
||||
{
|
||||
const bool isbuiltin = font->fc->IsBuiltInFont();
|
||||
|
||||
this->glyphs = MallocT<GlyphID>(this->glyph_count);
|
||||
this->glyph_to_char = MallocT<int>(this->glyph_count);
|
||||
|
||||
/* Positions contains the location of the begin of each of the glyphs, and the end of the last one. */
|
||||
this->positions = MallocT<float>(this->glyph_count * 2 + 2);
|
||||
this->positions[0] = x;
|
||||
|
||||
for (int i = 0; i < this->glyph_count; i++) {
|
||||
this->glyphs[i] = font->fc->MapCharToGlyph(chars[i]);
|
||||
if (isbuiltin) {
|
||||
this->positions[2 * i + 1] = font->fc->GetAscender(); // Apply sprite font's ascender.
|
||||
} else if (chars[i] >= SCC_SPRITE_START && chars[i] <= SCC_SPRITE_END) {
|
||||
this->positions[2 * i + 1] = (font->fc->GetHeight() - ScaleSpriteTrad(FontCache::GetDefaultFontHeight(font->fc->GetSize()))) / 2; // Align sprite font to centre
|
||||
} else {
|
||||
this->positions[2 * i + 1] = 0; // No ascender adjustment.
|
||||
}
|
||||
this->positions[2 * i + 2] = this->positions[2 * i] + font->fc->GetGlyphWidth(this->glyphs[i]);
|
||||
this->glyph_to_char[i] = i;
|
||||
}
|
||||
}
|
||||
|
||||
/** Move constructor for visual runs.*/
|
||||
FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun(FallbackVisualRun &&other) noexcept : font(other.font), glyph_count(other.glyph_count)
|
||||
{
|
||||
this->positions = other.positions;
|
||||
this->glyph_to_char = other.glyph_to_char;
|
||||
this->glyphs = other.glyphs;
|
||||
|
||||
other.positions = nullptr;
|
||||
other.glyph_to_char = nullptr;
|
||||
other.glyphs = nullptr;
|
||||
}
|
||||
|
||||
/** Free all data. */
|
||||
FallbackParagraphLayout::FallbackVisualRun::~FallbackVisualRun()
|
||||
{
|
||||
free(this->positions);
|
||||
free(this->glyph_to_char);
|
||||
free(this->glyphs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the font associated with this run.
|
||||
* @return The font.
|
||||
*/
|
||||
const Font *FallbackParagraphLayout::FallbackVisualRun::GetFont() const
|
||||
{
|
||||
return this->font;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of glyphs in this run.
|
||||
* @return The number of glyphs.
|
||||
*/
|
||||
int FallbackParagraphLayout::FallbackVisualRun::GetGlyphCount() const
|
||||
{
|
||||
return this->glyph_count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the glyphs of this run.
|
||||
* @return The glyphs.
|
||||
*/
|
||||
const GlyphID *FallbackParagraphLayout::FallbackVisualRun::GetGlyphs() const
|
||||
{
|
||||
return this->glyphs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the positions of this run.
|
||||
* @return The positions.
|
||||
*/
|
||||
const float *FallbackParagraphLayout::FallbackVisualRun::GetPositions() const
|
||||
{
|
||||
return this->positions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the glyph-to-character map for this visual run.
|
||||
* @return The glyph-to-character map.
|
||||
*/
|
||||
const int *FallbackParagraphLayout::FallbackVisualRun::GetGlyphToCharMap() const
|
||||
{
|
||||
return this->glyph_to_char;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the height of this font.
|
||||
* @return The height of the font.
|
||||
*/
|
||||
int FallbackParagraphLayout::FallbackVisualRun::GetLeading() const
|
||||
{
|
||||
return this->GetFont()->fc->GetHeight();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the height of the line.
|
||||
* @return The maximum height of the line.
|
||||
*/
|
||||
int FallbackParagraphLayout::FallbackLine::GetLeading() const
|
||||
{
|
||||
int leading = 0;
|
||||
for (const auto &run : *this) {
|
||||
leading = std::max(leading, run.GetLeading());
|
||||
}
|
||||
|
||||
return leading;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the width of this line.
|
||||
* @return The width of the line.
|
||||
*/
|
||||
int FallbackParagraphLayout::FallbackLine::GetWidth() const
|
||||
{
|
||||
if (this->size() == 0) return 0;
|
||||
|
||||
/*
|
||||
* The last X position of a run contains is the end of that run.
|
||||
* Since there is no left-to-right support, taking this value of
|
||||
* the last run gives us the end of the line and thus the width.
|
||||
*/
|
||||
const auto &run = this->GetVisualRun(this->CountRuns() - 1);
|
||||
return (int)run.GetPositions()[run.GetGlyphCount() * 2];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of runs in this line.
|
||||
* @return The number of runs.
|
||||
*/
|
||||
int FallbackParagraphLayout::FallbackLine::CountRuns() const
|
||||
{
|
||||
return (uint)this->size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific visual run.
|
||||
* @return The visual run.
|
||||
*/
|
||||
const ParagraphLayouter::VisualRun &FallbackParagraphLayout::FallbackLine::GetVisualRun(int run) const
|
||||
{
|
||||
return this->at(run);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new paragraph layouter.
|
||||
* @param buffer The characters of the paragraph.
|
||||
* @param length The length of the paragraph.
|
||||
* @param runs The font mapping of this paragraph.
|
||||
*/
|
||||
FallbackParagraphLayout::FallbackParagraphLayout(WChar *buffer, int length, FontMap &runs) : buffer_begin(buffer), buffer(buffer), runs(runs)
|
||||
{
|
||||
assert(runs.End()[-1].first == length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the position to the start of the paragraph.
|
||||
*/
|
||||
void FallbackParagraphLayout::Reflow()
|
||||
{
|
||||
this->buffer = this->buffer_begin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a new line with a maximum width.
|
||||
* @param max_width The maximum width of the string.
|
||||
* @return A Line, or nullptr when at the end of the paragraph.
|
||||
*/
|
||||
std::unique_ptr<const ParagraphLayouter::Line> FallbackParagraphLayout::NextLine(int max_width)
|
||||
{
|
||||
/* Simple idea:
|
||||
* - split a line at a newline character, or at a space where we can break a line.
|
||||
* - split for a visual run whenever a new line happens, or the font changes.
|
||||
*/
|
||||
if (this->buffer == nullptr) return nullptr;
|
||||
|
||||
std::unique_ptr<FallbackLine> l(new FallbackLine());
|
||||
|
||||
if (*this->buffer == '\0') {
|
||||
/* Only a newline. */
|
||||
this->buffer = nullptr;
|
||||
l->emplace_back(this->runs.front().second, this->buffer, 0, 0);
|
||||
return l;
|
||||
}
|
||||
|
||||
int offset = this->buffer - this->buffer_begin;
|
||||
FontMap::iterator iter = this->runs.data();
|
||||
while (iter->first <= offset) {
|
||||
iter++;
|
||||
assert(iter != this->runs.End());
|
||||
}
|
||||
|
||||
const FontCache *fc = iter->second->fc;
|
||||
const WChar *next_run = this->buffer_begin + iter->first;
|
||||
|
||||
const WChar *begin = this->buffer;
|
||||
const WChar *last_space = nullptr;
|
||||
const WChar *last_char;
|
||||
int width = 0;
|
||||
for (;;) {
|
||||
WChar c = *this->buffer;
|
||||
last_char = this->buffer;
|
||||
|
||||
if (c == '\0') {
|
||||
this->buffer = nullptr;
|
||||
break;
|
||||
}
|
||||
|
||||
if (this->buffer == next_run) {
|
||||
int w = l->GetWidth();
|
||||
l->emplace_back(iter->second, begin, this->buffer - begin, w);
|
||||
iter++;
|
||||
assert(iter != this->runs.End());
|
||||
|
||||
next_run = this->buffer_begin + iter->first;
|
||||
begin = this->buffer;
|
||||
}
|
||||
|
||||
if (IsWhitespace(c)) last_space = this->buffer;
|
||||
|
||||
if (IsPrintable(c) && !IsTextDirectionChar(c)) {
|
||||
int char_width = GetCharacterWidth(fc->GetSize(), c);
|
||||
width += char_width;
|
||||
if (width > max_width) {
|
||||
/* The string is longer than maximum width so we need to decide
|
||||
* what to do with it. */
|
||||
if (width == char_width) {
|
||||
/* The character is wider than allowed width; don't know
|
||||
* what to do with this case... bail out! */
|
||||
this->buffer = nullptr;
|
||||
return l;
|
||||
}
|
||||
|
||||
if (last_space == nullptr) {
|
||||
/* No space has been found. Just terminate at our current
|
||||
* location. This usually happens for languages that do not
|
||||
* require spaces in strings, like Chinese, Japanese and
|
||||
* Korean. For other languages terminating mid-word might
|
||||
* not be the best, but terminating the whole string instead
|
||||
* of continuing the word at the next line is worse. */
|
||||
last_char = this->buffer;
|
||||
} else {
|
||||
/* A space is found; perfect place to terminate */
|
||||
this->buffer = last_space + 1;
|
||||
last_char = last_space;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
this->buffer++;
|
||||
}
|
||||
|
||||
if (l->size() == 0 || last_char - begin > 0) {
|
||||
int w = l->GetWidth();
|
||||
l->emplace_back(iter->second, begin, last_char - begin, w);
|
||||
}
|
||||
return l;
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* 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 gfx_layout_fallback.h Functions related to laying out the texts as fallback. */
|
||||
|
||||
#ifndef GFX_LAYOUT_FALLBACK_H
|
||||
#define GFX_LAYOUT_FALLBACK_H
|
||||
|
||||
#include "gfx_layout.h"
|
||||
|
||||
/**
|
||||
* Helper class to construct a new #FallbackParagraphLayout.
|
||||
*/
|
||||
class FallbackParagraphLayoutFactory {
|
||||
public:
|
||||
/** Helper for GetLayouter, to get the right type. */
|
||||
typedef WChar CharType;
|
||||
/** Helper for GetLayouter, to get whether the layouter supports RTL. */
|
||||
static const bool SUPPORTS_RTL = false;
|
||||
|
||||
static ParagraphLayouter *GetParagraphLayout(WChar *buff, WChar *buff_end, FontMap &fontMapping);
|
||||
static size_t AppendToBuffer(WChar *buff, const WChar *buffer_last, WChar c);
|
||||
};
|
||||
|
||||
|
||||
#endif /* GFX_LAYOUT_FALLBACK_H */
|
@ -0,0 +1,534 @@
|
||||
/*
|
||||
* 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 gfx_layout_icu.cpp Handling of laying out with ICU / Harfbuzz. */
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "gfx_layout_icu.h"
|
||||
|
||||
#include "debug.h"
|
||||
#include "strings_func.h"
|
||||
#include "language.h"
|
||||
#include "table/control_codes.h"
|
||||
#include "zoom_func.h"
|
||||
|
||||
#include "3rdparty/icu/scriptrun.h"
|
||||
|
||||
#include <unicode/ubidi.h>
|
||||
#include <unicode/brkiter.h>
|
||||
|
||||
#include <hb.h>
|
||||
#include <hb-ft.h>
|
||||
|
||||
#include <deque>
|
||||
|
||||
#include "safeguards.h"
|
||||
|
||||
/** harfbuzz doesn't use floats, so we need a value to scale position with to get sub-pixel precision. */
|
||||
constexpr float FONT_SCALE = 64.0;
|
||||
|
||||
/**
|
||||
* Helper class to store the information of all the runs of a paragraph in.
|
||||
*
|
||||
* During itemization, more and more information is filled in.
|
||||
*/
|
||||
class ICURun {
|
||||
public:
|
||||
int start; ///< Start of the run in the buffer.
|
||||
int length; ///< Length of the run in the buffer.
|
||||
UBiDiLevel level; ///< Embedding level of the run.
|
||||
UScriptCode script; ///< Script of the run.
|
||||
Font *font; ///< Font of the run.
|
||||
|
||||
std::vector<GlyphID> glyphs; ///< The glyphs of the run. Valid after Shape() is called.
|
||||
std::vector<int> advance; ///< The advance (width) of the glyphs. Valid after Shape() is called.
|
||||
std::vector<int> glyph_to_char; ///< The mapping from glyphs to characters. Valid after Shape() is called.
|
||||
std::vector<float> positions; ///< The positions of the glyphs. Valid after Shape() is called.
|
||||
int total_advance; ///< The total advance of the run. Valid after Shape() is called.
|
||||
|
||||
ICURun(int start, int length, UBiDiLevel level, UScriptCode script = USCRIPT_UNKNOWN, Font *font = nullptr) : start(start), length(length), level(level), script(script), font(font) {}
|
||||
|
||||
void Shape(UChar *buff, size_t length);
|
||||
};
|
||||
|
||||
/**
|
||||
* Wrapper for doing layouts with ICU.
|
||||
*/
|
||||
class ICUParagraphLayout : public ParagraphLayouter {
|
||||
public:
|
||||
/** Visual run contains data about the bit of text with the same font. */
|
||||
class ICUVisualRun : public ParagraphLayouter::VisualRun {
|
||||
private:
|
||||
std::vector<GlyphID> glyphs;
|
||||
std::vector<float> positions;
|
||||
std::vector<int> glyph_to_char;
|
||||
|
||||
int total_advance;
|
||||
const Font *font;
|
||||
|
||||
public:
|
||||
ICUVisualRun(const ICURun &run, int x);
|
||||
|
||||
const GlyphID *GetGlyphs() const override { return this->glyphs.data(); }
|
||||
const float *GetPositions() const override { return this->positions.data(); }
|
||||
const int *GetGlyphToCharMap() const override { return this->glyph_to_char.data(); }
|
||||
|
||||
const Font *GetFont() const override { return this->font; }
|
||||
int GetLeading() const override { return this->font->fc->GetHeight(); }
|
||||
int GetGlyphCount() const override { return this->glyphs.size(); }
|
||||
int GetAdvance() const { return this->total_advance; }
|
||||
};
|
||||
|
||||
/** A single line worth of VisualRuns. */
|
||||
class ICULine : public std::vector<ICUVisualRun>, public ParagraphLayouter::Line {
|
||||
public:
|
||||
int GetLeading() const override;
|
||||
int GetWidth() const override;
|
||||
int CountRuns() const override { return (uint)this->size(); }
|
||||
const VisualRun &GetVisualRun(int run) const override { return this->at(run); }
|
||||
|
||||
int GetInternalCharLength(WChar c) const override
|
||||
{
|
||||
/* ICU uses UTF-16 internally which means we need to account for surrogate pairs. */
|
||||
return c >= 0x010000U ? 2 : 1;
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
std::vector<ICURun> runs;
|
||||
UChar *buff;
|
||||
size_t buff_length;
|
||||
std::vector<ICURun>::iterator current_run;
|
||||
int partial_offset;
|
||||
|
||||
public:
|
||||
ICUParagraphLayout(std::vector<ICURun> &runs, UChar *buff, size_t buff_length) : runs(runs), buff(buff), buff_length(buff_length)
|
||||
{
|
||||
this->Reflow();
|
||||
}
|
||||
|
||||
~ICUParagraphLayout() override { }
|
||||
|
||||
void Reflow() override
|
||||
{
|
||||
this->current_run = this->runs.begin();
|
||||
this->partial_offset = 0;
|
||||
}
|
||||
|
||||
std::unique_ptr<const Line> NextLine(int max_width) override;
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructor for a new ICUVisualRun.
|
||||
*
|
||||
* It bases all information on the ICURun, which should already be shaped.
|
||||
*
|
||||
* @param run The ICURun to base the visual run on.
|
||||
* @param x The offset of the run on the line.
|
||||
*/
|
||||
ICUParagraphLayout::ICUVisualRun::ICUVisualRun(const ICURun &run, int x) :
|
||||
glyphs(run.glyphs), glyph_to_char(run.glyph_to_char), total_advance(run.total_advance), font(run.font)
|
||||
{
|
||||
/* If there are no positions, the ICURun was not Shaped; that should never happen. */
|
||||
assert(run.positions.size() != 0);
|
||||
this->positions.reserve(run.positions.size());
|
||||
|
||||
/* "positions" is an array of x/y. So we need to alternate. */
|
||||
bool is_x = true;
|
||||
for (auto &position : run.positions) {
|
||||
if (is_x) {
|
||||
this->positions.push_back(position + x);
|
||||
} else {
|
||||
this->positions.push_back(position);
|
||||
}
|
||||
|
||||
is_x = !is_x;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shape a single run.
|
||||
*
|
||||
* @param buff The buffer of which a partial (depending on start/length of the run) will be shaped.
|
||||
* @param length The length of the buffer.
|
||||
*/
|
||||
void ICURun::Shape(UChar *buff, size_t buff_length) {
|
||||
auto hbfont = hb_ft_font_create_referenced(*(static_cast<const FT_Face *>(font->fc->GetOSHandle())));
|
||||
hb_font_set_scale(hbfont, this->font->fc->GetFontSize() * FONT_SCALE, this->font->fc->GetFontSize() * FONT_SCALE);
|
||||
|
||||
/* ICU buffer is in UTF-16. */
|
||||
auto hbbuf = hb_buffer_create();
|
||||
hb_buffer_add_utf16(hbbuf, reinterpret_cast<uint16 *>(buff), buff_length, this->start, this->length);
|
||||
|
||||
/* Set all the properties of this segment. */
|
||||
hb_buffer_set_direction(hbbuf, (this->level & 1) == 1 ? HB_DIRECTION_RTL : HB_DIRECTION_LTR);
|
||||
hb_buffer_set_script(hbbuf, hb_script_from_string(uscript_getShortName(this->script), -1));
|
||||
hb_buffer_set_language(hbbuf, hb_language_from_string(_current_language->isocode, -1));
|
||||
hb_buffer_set_cluster_level(hbbuf, HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES);
|
||||
|
||||
/* Shape the segment. */
|
||||
hb_shape(hbfont, hbbuf, nullptr, 0);
|
||||
|
||||
unsigned int glyph_count;
|
||||
auto glyph_info = hb_buffer_get_glyph_infos(hbbuf, &glyph_count);
|
||||
auto glyph_pos = hb_buffer_get_glyph_positions(hbbuf, &glyph_count);
|
||||
|
||||
/* Make sure any former run is lost. */
|
||||
this->glyphs.clear();
|
||||
this->glyph_to_char.clear();
|
||||
this->positions.clear();
|
||||
this->advance.clear();
|
||||
|
||||
/* Reserve space, as we already know the size. */
|
||||
this->glyphs.reserve(glyph_count);
|
||||
this->glyph_to_char.reserve(glyph_count);
|
||||
this->positions.reserve(glyph_count * 2 + 2);
|
||||
this->advance.reserve(glyph_count);
|
||||
|
||||
/* Prepare the glyphs/position. ICUVisualRun will give the position an offset if needed. */
|
||||
hb_position_t advance = 0;
|
||||
for (unsigned int i = 0; i < glyph_count; i++) {
|
||||
int x_advance;
|
||||
|
||||
if (buff[glyph_info[i].cluster] >= SCC_SPRITE_START && buff[glyph_info[i].cluster] <= SCC_SPRITE_END) {
|
||||
auto glyph = this->font->fc->MapCharToGlyph(buff[glyph_info[i].cluster]);
|
||||
|
||||
this->glyphs.push_back(glyph);
|
||||
this->positions.push_back(advance);
|
||||
this->positions.push_back((this->font->fc->GetHeight() - ScaleSpriteTrad(FontCache::GetDefaultFontHeight(this->font->fc->GetSize()))) / 2); // Align sprite font to centre
|
||||
x_advance = this->font->fc->GetGlyphWidth(glyph);
|
||||
} else {
|
||||
this->glyphs.push_back(glyph_info[i].codepoint);
|
||||
this->positions.push_back(glyph_pos[i].x_offset / FONT_SCALE + advance);
|
||||
this->positions.push_back(glyph_pos[i].y_offset / FONT_SCALE);
|
||||
x_advance = glyph_pos[i].x_advance / FONT_SCALE;
|
||||
}
|
||||
|
||||
this->glyph_to_char.push_back(glyph_info[i].cluster);
|
||||
this->advance.push_back(x_advance);
|
||||
advance += x_advance;
|
||||
}
|
||||
|
||||
/* Position has one more element to close off the array. */
|
||||
this->positions.push_back(advance);
|
||||
this->positions.push_back(0);
|
||||
|
||||
/* Track the total advancement we made. */
|
||||
this->total_advance = advance;
|
||||
|
||||
hb_buffer_destroy(hbbuf);
|
||||
hb_font_destroy(hbfont);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the height of the line.
|
||||
* @return The maximum height of the line.
|
||||
*/
|
||||
int ICUParagraphLayout::ICULine::GetLeading() const
|
||||
{
|
||||
int leading = 0;
|
||||
for (const auto &run : *this) {
|
||||
leading = std::max(leading, run.GetLeading());
|
||||
}
|
||||
|
||||
return leading;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the width of this line.
|
||||
* @return The width of the line.
|
||||
*/
|
||||
int ICUParagraphLayout::ICULine::GetWidth() const
|
||||
{
|
||||
int length = 0;
|
||||
for (const auto &run : *this) {
|
||||
length += run.GetAdvance();
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Itemize the string into runs per embedding level.
|
||||
*
|
||||
* Later on, based on the levels, we can deduce the order of a subset of runs.
|
||||
*
|
||||
* @param buff The string to itemize.
|
||||
* @param length The length of the string.
|
||||
* @return The runs.
|
||||
*/
|
||||
std::vector<ICURun> ItemizeBidi(UChar *buff, size_t length)
|
||||
{
|
||||
auto ubidi = ubidi_open();
|
||||
|
||||
auto parLevel = _current_text_dir == TD_RTL ? UBIDI_RTL : UBIDI_LTR;
|
||||
|
||||
UErrorCode err = U_ZERO_ERROR;
|
||||
ubidi_setPara(ubidi, buff, length, parLevel, nullptr, &err);
|
||||
if (U_FAILURE(err)) {
|
||||
DEBUG(fontcache, 0, "Failed to set paragraph: %s", u_errorName(err));
|
||||
ubidi_close(ubidi);
|
||||
return std::vector<ICURun>();
|
||||
}
|
||||
|
||||
int32_t count = ubidi_countRuns(ubidi, &err);
|
||||
if (U_FAILURE(err)) {
|
||||
DEBUG(fontcache, 0, "Failed to count runs: %s", u_errorName(err));
|
||||
ubidi_close(ubidi);
|
||||
return std::vector<ICURun>();
|
||||
}
|
||||
|
||||
std::vector<ICURun> runs;
|
||||
runs.reserve(count);
|
||||
|
||||
/* Find the breakpoints for the logical runs. So we get runs that say "from START to END". */
|
||||
int32_t logical_pos = 0;
|
||||
while (static_cast<size_t>(logical_pos) < length) {
|
||||
auto start_pos = logical_pos;
|
||||
|
||||
/* Fetch the embedding level, so we can order bidi correctly later on. */
|
||||
UBiDiLevel level;
|
||||
ubidi_getLogicalRun(ubidi, start_pos, &logical_pos, &level);
|
||||
|
||||
runs.emplace_back(ICURun(start_pos, logical_pos - start_pos, level));
|
||||
}
|
||||
|
||||
assert(static_cast<size_t>(count) == runs.size());
|
||||
|
||||
ubidi_close(ubidi);
|
||||
return runs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Itemize the string into runs per script, based on the previous created runs.
|
||||
*
|
||||
* Basically, this always returns the same or more runs than given.
|
||||
*
|
||||
* @param buff The string to itemize.
|
||||
* @param length The length of the string.
|
||||
* @param runs_current The current runs.
|
||||
* @return The runs.
|
||||
*/
|
||||
std::vector<ICURun> ItemizeScript(UChar *buff, size_t length, std::vector<ICURun> &runs_current)
|
||||
{
|
||||
std::vector<ICURun> runs;
|
||||
icu::ScriptRun script_itemizer(buff, length);
|
||||
|
||||
int cur_pos = 0;
|
||||
auto cur_run = runs_current.begin();
|
||||
while (true) {
|
||||
while (cur_pos < script_itemizer.getScriptEnd() && cur_run != runs_current.end()) {
|
||||
int stop_pos = std::min(script_itemizer.getScriptEnd(), cur_run->start + cur_run->length);
|
||||
assert(stop_pos - cur_pos > 0);
|
||||
|
||||
runs.push_back(ICURun(cur_pos, stop_pos - cur_pos, cur_run->level, script_itemizer.getScriptCode()));
|
||||
|
||||
if (stop_pos == cur_run->start + cur_run->length) cur_run++;
|
||||
cur_pos = stop_pos;
|
||||
}
|
||||
|
||||
if (!script_itemizer.next()) break;
|
||||
}
|
||||
|
||||
return runs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Itemize the string into runs per style, based on the previous created runs.
|
||||
*
|
||||
* Basically, this always returns the same or more runs than given.
|
||||
*
|
||||
* @param buff The string to itemize.
|
||||
* @param length The length of the string.
|
||||
* @param runs_current The current runs.
|
||||
* @param font_mapping The font mapping.
|
||||
* @return The runs.
|
||||
*/
|
||||
std::vector<ICURun> ItemizeStyle(UChar *buff, size_t length, std::vector<ICURun> &runs_current, FontMap &font_mapping)
|
||||
{
|
||||
std::vector<ICURun> runs;
|
||||
|
||||
int cur_pos = 0;
|
||||
auto cur_run = runs_current.begin();
|
||||
for (auto const &font_map : font_mapping) {
|
||||
while (cur_pos < font_map.first && cur_run != runs_current.end()) {
|
||||
int stop_pos = std::min(font_map.first, cur_run->start + cur_run->length);
|
||||
assert(stop_pos - cur_pos > 0);
|
||||
|
||||
runs.push_back(ICURun(cur_pos, stop_pos - cur_pos, cur_run->level, cur_run->script, font_map.second));
|
||||
|
||||
if (stop_pos == cur_run->start + cur_run->length) cur_run++;
|
||||
cur_pos = stop_pos;
|
||||
}
|
||||
}
|
||||
|
||||
return runs;
|
||||
}
|
||||
|
||||
/* static */ ParagraphLayouter *ICUParagraphLayoutFactory::GetParagraphLayout(UChar *buff, UChar *buff_end, FontMap &font_mapping)
|
||||
{
|
||||
size_t length = buff_end - buff;
|
||||
/* Can't layout an empty string. */
|
||||
if (length == 0) return nullptr;
|
||||
|
||||
/* Can't layout our in-built sprite fonts. */
|
||||
for (auto const &pair : font_mapping) {
|
||||
if (pair.second->fc->IsBuiltInFont()) return nullptr;
|
||||
}
|
||||
|
||||
auto runs = ItemizeBidi(buff, length);
|
||||
runs = ItemizeScript(buff, length, runs);
|
||||
runs = ItemizeStyle(buff, length, runs, font_mapping);
|
||||
|
||||
if (runs.size() == 0) return nullptr;
|
||||
|
||||
for (auto &run : runs) {
|
||||
run.Shape(buff, length);
|
||||
}
|
||||
|
||||
return new ICUParagraphLayout(runs, buff, length);
|
||||
}
|
||||
|
||||
std::unique_ptr<const ICUParagraphLayout::Line> ICUParagraphLayout::NextLine(int max_width)
|
||||
{
|
||||
std::vector<ICURun>::iterator start_run = this->current_run;
|
||||
std::vector<ICURun>::iterator last_run = this->current_run;
|
||||
|
||||
if (start_run == this->runs.end()) return nullptr;
|
||||
|
||||
int cur_width = 0;
|
||||
|
||||
/* Add remaining width of the first run if it is a broken run. */
|
||||
if (this->partial_offset > 0) {
|
||||
if ((start_run->level & 1) == 0) {
|
||||
for (size_t i = this->partial_offset; i < start_run->advance.size(); i++) {
|
||||
cur_width += start_run->advance[i];
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < this->partial_offset; i++) {
|
||||
cur_width += start_run->advance[i];
|
||||
}
|
||||
}
|
||||
last_run++;
|
||||
}
|
||||
|
||||
/* Gather runs until the line is full. */
|
||||
while (last_run != this->runs.end() && cur_width < max_width) {
|
||||
cur_width += last_run->total_advance;
|
||||
last_run++;
|
||||
}
|
||||
|
||||
/* If the text does not fit into the available width, find a suitable breaking point. */
|
||||
int new_partial_length = 0;
|
||||
if (cur_width > max_width) {
|
||||
auto locale = icu::Locale(_current_language->isocode);
|
||||
|
||||
/* Create a break-iterator to find a good place to break lines. */
|
||||
UErrorCode err = U_ZERO_ERROR;
|
||||
auto break_iterator = icu::BreakIterator::createLineInstance(locale, err);
|
||||
break_iterator->setText(icu::UnicodeString(this->buff, this->buff_length));
|
||||
|
||||
auto overflow_run = last_run - 1;
|
||||
|
||||
/* Find the last glyph that fits. */
|
||||
size_t index;
|
||||
if ((overflow_run->level & 1) == 0) {
|
||||
/* LTR */
|
||||
for (index = overflow_run->glyphs.size(); index > 0; index--) {
|
||||
cur_width -= overflow_run->advance[index - 1];
|
||||
if (cur_width <= max_width) break;
|
||||
}
|
||||
index--;
|
||||
} else {
|
||||
/* RTL */
|
||||
for (index = 0; index < overflow_run->glyphs.size(); index++) {
|
||||
cur_width -= overflow_run->advance[index];
|
||||
if (cur_width <= max_width) break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Find the character that matches; this is the start of the cluster. */
|
||||
auto char_pos = overflow_run->glyph_to_char[index];
|
||||
|
||||
/* See if there is a good breakpoint inside this run. */
|
||||
int32_t break_pos = break_iterator->preceding(char_pos + 1);
|
||||
if (break_pos != icu::BreakIterator::DONE && break_pos > overflow_run->start + this->partial_offset) {
|
||||
/* There is a line-break inside this run that is suitable. */
|
||||
new_partial_length = break_pos - overflow_run->start - this->partial_offset;
|
||||
} else if (overflow_run != start_run) {
|
||||
/* There is no suitable line-break in this run, but it is also not
|
||||
* the only run on this line. So we remove the run. */
|
||||
last_run--;
|
||||
} else {
|
||||
/* There is no suitable line-break and this is the only run on the
|
||||
* line. So we break at the cluster. This is not pretty, but the
|
||||
* best we can do. */
|
||||
new_partial_length = char_pos - this->partial_offset;
|
||||
}
|
||||
}
|
||||
|
||||
/* Reorder the runs on this line for display. */
|
||||
std::vector<UBiDiLevel> bidi_level;
|
||||
for (auto run = start_run; run != last_run; run++) {
|
||||
bidi_level.push_back(run->level);
|
||||
}
|
||||
std::vector<int32_t> vis_to_log(bidi_level.size());
|
||||
ubidi_reorderVisual(bidi_level.data(), bidi_level.size(), vis_to_log.data());
|
||||
|
||||
/* Create line. */
|
||||
std::unique_ptr<ICULine> line(new ICULine());
|
||||
|
||||
int cur_pos = 0;
|
||||
for (auto &i : vis_to_log) {
|
||||
auto i_run = start_run + i;
|
||||
/* Copy the ICURun here, so we can modify it in case of a partial. */
|
||||
ICURun run = *i_run;
|
||||
|
||||
if (i_run == last_run - 1 && new_partial_length > 0) {
|
||||
if (i_run == start_run && this->partial_offset > 0) {
|
||||
assert(run.length > this->partial_offset);
|
||||
run.start += this->partial_offset;
|
||||
run.length -= this->partial_offset;
|
||||
}
|
||||
|
||||
assert(run.length > new_partial_length);
|
||||
run.length = new_partial_length;
|
||||
|
||||
run.Shape(this->buff, this->buff_length);
|
||||
} else if (i_run == start_run && this->partial_offset > 0) {
|
||||
assert(run.length > this->partial_offset);
|
||||
|
||||
run.start += this->partial_offset;
|
||||
run.length -= this->partial_offset;
|
||||
|
||||
run.Shape(this->buff, this->buff_length);
|
||||
}
|
||||
|
||||
auto total_advance = run.total_advance;
|
||||
line->emplace_back(std::move(run), cur_pos);
|
||||
cur_pos += total_advance;
|
||||
}
|
||||
|
||||
if (new_partial_length > 0) {
|
||||
this->current_run = last_run - 1;
|
||||
this->partial_offset += new_partial_length;
|
||||
} else {
|
||||
this->current_run = last_run;
|
||||
this->partial_offset = 0;
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
/* static */ size_t ICUParagraphLayoutFactory::AppendToBuffer(UChar *buff, const UChar *buffer_last, WChar c)
|
||||
{
|
||||
/* Transform from UTF-32 to internal ICU format of UTF-16. */
|
||||
int32 length = 0;
|
||||
UErrorCode err = U_ZERO_ERROR;
|
||||
u_strFromUTF32(buff, buffer_last - buff, &length, (UChar32*)&c, 1, &err);
|
||||
return length;
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* 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 gfx_layout_icu.h Functions related to laying out the texts with ICU. */
|
||||
|
||||
#ifndef GFX_LAYOUT_ICU_H
|
||||
#define GFX_LAYOUT_ICU_H
|
||||
|
||||
#include "gfx_layout.h"
|
||||
|
||||
#include <unicode/ustring.h>
|
||||
|
||||
/**
|
||||
* Helper class to construct a new #ICUParagraphLayout.
|
||||
*/
|
||||
class ICUParagraphLayoutFactory {
|
||||
public:
|
||||
/** Helper for GetLayouter, to get the right type. */
|
||||
typedef UChar CharType;
|
||||
/** Helper for GetLayouter, to get whether the layouter supports RTL. */
|
||||
static const bool SUPPORTS_RTL = true;
|
||||
|
||||
static ParagraphLayouter *GetParagraphLayout(UChar *buff, UChar *buff_end, FontMap &fontMapping);
|
||||
static size_t AppendToBuffer(UChar *buff, const UChar *buffer_last, WChar c);
|
||||
};
|
||||
|
||||
#endif /* GFX_LAYOUT_ICU_H */
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue