20#include "3rdparty/icu/scriptrun.h"
22#include <unicode/ubidi.h>
23#include <unicode/brkiter.h>
73 std::vector<GlyphID> glyphs;
74 std::vector<Position> positions;
75 std::vector<int> glyph_to_char;
83 std::span<const GlyphID>
GetGlyphs()
const override {
return this->glyphs; }
84 std::span<const Position>
GetPositions()
const override {
return this->positions; }
90 int GetAdvance()
const {
return this->total_advance; }
98 size_t CountRuns()
const override {
return this->size(); }
104 return c >= 0x010000U ? 2 : 1;
109 std::vector<ICURun> runs;
112 std::vector<ICURun>::iterator current_run;
116 ICUParagraphLayout(std::vector<ICURun> &&runs, UChar *buff,
size_t buff_length) : runs(std::move(runs)), buff(buff), buff_length(buff_length)
121 ~ICUParagraphLayout()
override =
default;
125 this->current_run = this->runs.begin();
126 this->partial_offset = 0;
129 std::unique_ptr<const Line>
NextLine(
int max_width)
override;
141 glyphs(run.glyphs), glyph_to_char(run.glyph_to_char), total_advance(run.total_advance), font(run.font)
145 this->positions.reserve(run.
positions.size());
149 this->positions.emplace_back(pos.left + x, pos.right + x, pos.top);
164 auto hbfont = hb_ft_font_create_referenced(*(
static_cast<const FT_Face *
>(fc.
GetOSHandle())));
166 hb_ft_font_set_load_flags(hbfont, GetFontAAState() ? FT_LOAD_TARGET_NORMAL : FT_LOAD_TARGET_MONO);
169 auto hbbuf = hb_buffer_create();
170 hb_buffer_add_utf16(hbbuf,
reinterpret_cast<uint16_t *
>(buff), buff_length, this->
start, this->
length);
173 hb_buffer_set_direction(hbbuf, (this->
level & 1) == 1 ? HB_DIRECTION_RTL : HB_DIRECTION_LTR);
174 hb_buffer_set_script(hbbuf, hb_script_from_string(uscript_getShortName(this->
script), -1));
175 hb_buffer_set_language(hbbuf, hb_language_from_string(
_current_language->isocode, -1));
176 hb_buffer_set_cluster_level(hbbuf, HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES);
179 hb_shape(hbfont, hbbuf,
nullptr, 0);
181 unsigned int glyph_count;
182 auto glyph_info = hb_buffer_get_glyph_infos(hbbuf, &glyph_count);
183 auto glyph_pos = hb_buffer_get_glyph_positions(hbbuf, &glyph_count);
192 this->
glyphs.reserve(glyph_count);
195 this->
advance.reserve(glyph_count);
199 for (
unsigned int i = 0; i < glyph_count; i++) {
202 if (buff[glyph_info[i].cluster] >= SCC_SPRITE_START && buff[glyph_info[i].cluster] <= SCC_SPRITE_END && glyph_info[i].codepoint == 0) {
205 this->
glyphs.push_back(glyph);
208 x_advance = glyph_pos[i].x_advance /
FONT_SCALE;
209 this->
glyphs.push_back(glyph_info[i].codepoint);
214 this->advance.push_back(x_advance);
221 hb_buffer_destroy(hbbuf);
222 hb_font_destroy(hbfont);
232 for (
const auto &run : *
this) {
233 leading = std::max(leading, run.GetLeading());
246 for (
const auto &run : *
this) {
247 length += run.GetAdvance();
268 UErrorCode err = U_ZERO_ERROR;
269 ubidi_setPara(ubidi.get(), buff, length, parLevel,
nullptr, &err);
270 if (U_FAILURE(err)) {
275 int32_t count = ubidi_countRuns(ubidi.get(), &err);
276 if (U_FAILURE(err)) {
281 std::vector<ICURun> runs;
285 int32_t logical_pos = 0;
286 while (
static_cast<size_t>(logical_pos) < length) {
287 auto start_pos = logical_pos;
291 ubidi_getLogicalRun(ubidi.get(), start_pos, &logical_pos, &level);
293 runs.emplace_back(start_pos, logical_pos - start_pos, level, USCRIPT_UNKNOWN,
Font{});
296 assert(
static_cast<size_t>(count) == runs.size());
311std::vector<ICURun>
ItemizeScript(UChar *buff,
size_t length, std::vector<ICURun> &runs_current)
313 std::vector<ICURun> runs;
314 icu::ScriptRun script_itemizer(buff, length);
317 auto cur_run = runs_current.begin();
319 while (cur_pos < script_itemizer.getScriptEnd() && cur_run != runs_current.end()) {
320 int stop_pos = std::min(script_itemizer.getScriptEnd(), cur_run->start + cur_run->length);
321 assert(stop_pos - cur_pos > 0);
323 runs.emplace_back(cur_pos, stop_pos - cur_pos, cur_run->level, script_itemizer.getScriptCode(),
Font{});
325 if (stop_pos == cur_run->start + cur_run->length) cur_run++;
329 if (!script_itemizer.next())
break;
346 std::vector<ICURun> runs;
349 auto cur_run = runs_current.begin();
350 for (
auto const &[position, font] : font_mapping) {
351 while (cur_pos < position && cur_run != runs_current.end()) {
352 int stop_pos = std::min(position, cur_run->start + cur_run->length);
353 assert(stop_pos - cur_pos > 0);
355 runs.emplace_back(cur_pos, stop_pos - cur_pos, cur_run->level, cur_run->script, font);
357 if (stop_pos == cur_run->start + cur_run->length) cur_run++;
365 std::unique_ptr<ParagraphLayouter> ICUParagraphLayoutFactory::GetParagraphLayout(UChar *buff, UChar *buff_end,
FontMap &font_mapping)
367 size_t length = buff_end - buff;
369 if (length == 0)
return nullptr;
372 for (
auto const &[position, font] : font_mapping) {
373 if (font.GetFontCache().IsBuiltInFont())
return nullptr;
380 if (runs.empty())
return nullptr;
382 for (
auto &run : runs) {
383 run.Shape(buff, length);
386 return std::make_unique<ICUParagraphLayout>(std::move(runs), buff, length);
389 std::unique_ptr<icu::BreakIterator> ICUParagraphLayoutFactory::break_iterator;
397 UErrorCode status = U_ZERO_ERROR;
398 ICUParagraphLayoutFactory::break_iterator.reset(icu::BreakIterator::createLineInstance(locale, status));
399 assert(U_SUCCESS(status));
408 assert(ICUParagraphLayoutFactory::break_iterator !=
nullptr);
410 return std::unique_ptr<icu::BreakIterator>(ICUParagraphLayoutFactory::break_iterator->clone());
415 std::vector<ICURun>::iterator start_run = this->current_run;
416 std::vector<ICURun>::iterator last_run = this->current_run;
418 if (start_run == this->runs.end())
return nullptr;
423 if (this->partial_offset > 0) {
424 if ((start_run->level & 1) == 0) {
425 for (
size_t i = this->partial_offset; i < start_run->advance.size(); i++) {
426 cur_width += start_run->advance[i];
429 for (
int i = 0; i < this->partial_offset; i++) {
430 cur_width += start_run->advance[i];
437 while (last_run != this->runs.end() && cur_width < max_width) {
438 cur_width += last_run->total_advance;
443 int new_partial_length = 0;
444 if (cur_width > max_width) {
447 icu::UnicodeString text(this->buff, this->buff_length);
448 break_iterator->setText(text);
450 auto overflow_run = last_run - 1;
454 if ((overflow_run->level & 1) == 0) {
456 for (index = overflow_run->glyphs.size(); index > 0; ) {
458 cur_width -= overflow_run->advance[index];
459 if (cur_width <= max_width)
break;
463 for (index = 0; index < overflow_run->glyphs.size(); index++) {
464 cur_width -= overflow_run->advance[index];
465 if (cur_width <= max_width)
break;
470 auto char_pos = overflow_run->glyph_to_char[index];
473 int32_t break_pos = break_iterator->preceding(char_pos + 1);
474 auto overflow_run_start = overflow_run->start;
475 if (overflow_run == start_run) overflow_run_start += this->partial_offset;
476 if (break_pos != icu::BreakIterator::DONE && break_pos > overflow_run_start) {
478 new_partial_length = break_pos - overflow_run_start;
479 }
else if (overflow_run != start_run) {
487 new_partial_length = char_pos - overflow_run_start;
492 std::vector<UBiDiLevel> bidi_level;
493 for (
auto run = start_run; run != last_run; run++) {
494 bidi_level.push_back(run->level);
496 std::vector<int32_t> vis_to_log(bidi_level.size());
497 ubidi_reorderVisual(bidi_level.data(), bidi_level.size(), vis_to_log.data());
500 std::unique_ptr<ICULine> line = std::make_unique<ICULine>();
503 for (
auto &i : vis_to_log) {
504 auto i_run = start_run + i;
508 if (i_run == last_run - 1 && new_partial_length > 0) {
509 if (i_run == start_run && this->partial_offset > 0) {
510 assert(run.
length > this->partial_offset);
511 run.
start += this->partial_offset;
512 run.
length -= this->partial_offset;
515 assert(run.
length > new_partial_length);
516 run.
length = new_partial_length;
518 run.
Shape(this->buff, this->buff_length);
519 }
else if (i_run == start_run && this->partial_offset > 0) {
520 assert(run.
length > this->partial_offset);
522 run.
start += this->partial_offset;
523 run.
length -= this->partial_offset;
525 run.
Shape(this->buff, this->buff_length);
529 line->emplace_back(std::move(run), cur_pos);
530 cur_pos += total_advance;
533 if (new_partial_length > 0) {
534 this->current_run = last_run - 1;
535 if (this->current_run != start_run) this->partial_offset = 0;
536 this->partial_offset += new_partial_length;
538 this->current_run = last_run;
539 this->partial_offset = 0;
545 size_t ICUParagraphLayoutFactory::AppendToBuffer(UChar *buff,
const UChar *buffer_last,
char32_t c)
547 assert(buff < buffer_last);
550 UErrorCode err = U_ZERO_ERROR;
551 u_strFromUTF32(buff, buffer_last - buff, &length, (UChar32*)&c, 1, &err);
Helper for std::unique_ptr to use an arbitrary function as the deleter.
std::unique_ptr< T, DeleterFromFunc< Tfunc > > AutoRelease
Specialisation of std::unique_ptr for objects which must be deleted by calling a function.
Font cache for basic fonts.
int GetHeight() const
Get the height of the font.
virtual const void * GetOSHandle()
Get the native OS font handle, if there is one.
virtual GlyphID MapCharToGlyph(char32_t key, bool fallback=true)=0
Map a character into a glyph.
virtual uint GetGlyphWidth(GlyphID key)=0
Get the width of the glyph with the given key.
FontSize GetSize() const
Get the FontSize of the font.
Container with information about a font.
FontCache & GetFontCache() const
Get the font cache of this font.
static std::unique_ptr< icu::BreakIterator > GetBreakIterator()
Get a thread-safe line break iterator.
static void InitializeLayouter()
Initialize data needed for the ICU layouter.
A single line worth of VisualRuns.
int GetInternalCharLength(char32_t c) const override
Get the number of elements the given character occupies in the underlying text buffer of the Layouter...
const VisualRun & GetVisualRun(size_t run) const override
Get a reference to the given run.
int GetLeading() const override
Get the height of the line.
int GetWidth() const override
Get the width of this line.
size_t CountRuns() const override
Get the number of runs in this line.
std::span< const Position > GetPositions() const override
Get the positions for each of the glyphs.
ICUVisualRun(const ICURun &run, int x)
Constructor for a new ICUVisualRun.
int GetLeading() const override
Get the font leading, or distance between the baselines of consecutive lines.
std::span< const GlyphID > GetGlyphs() const override
Get the glyphs to draw.
size_t GetGlyphCount() const override
Get the number of glyphs.
std::span< const int > GetGlyphToCharMap() const override
The offset for each of the glyphs to the character run that was passed to the Layouter.
const Font & GetFont() const override
Get the font.
Wrapper for doing layouts with ICU.
void Reflow() override
Reset the position to the start of the paragraph.
std::unique_ptr< const Line > NextLine(int max_width) override
Construct a new line with a maximum width.
Helper class to store the information of all the runs of a paragraph in.
UScriptCode script
Script of the run.
Font font
Font of the run.
std::vector< int > glyph_to_char
The mapping from glyphs to characters. Valid after Shape() is called.
ICURun(int start, int length, UBiDiLevel level, UScriptCode script, const Font &font)
Construct a new ICURun object.
void Shape(UChar *buff, size_t length)
Shape a single run.
std::vector< GlyphID > glyphs
The glyphs of the run. Valid after Shape() is called.
int total_advance
The total advance of the run. Valid after Shape() is called.
std::vector< int > advance
The advance (width) of the glyphs. Valid after Shape() is called.
int length
Length of the run in the buffer.
int start
Start of the run in the buffer.
std::vector< ParagraphLayouter::Position > positions
The positions of the glyphs. Valid after Shape() is called.
UBiDiLevel level
Embedding level of the run.
A single line worth of VisualRuns.
Visual run contains data about the bit of text with the same font.
Interface to glue fallback and normal layouter into one.
Control codes that are embedded in the translation strings.
Functions related to debugging.
#define Debug(facility, severity, format_string,...)
Output a line of debugging information.
@ Fontcache
Fontcache message facility.
@ Critical
Critical, user should know about this.
std::vector< std::pair< int, Font > > FontMap
Mapping from index to font.
std::vector< ICURun > ItemizeStyle(std::vector< ICURun > &runs_current, FontMap &font_mapping)
Itemize the string into runs per style, based on the previous created runs.
constexpr float FONT_SCALE
HarfBuzz FreeType integration sets the font scaling, which is always in 1/64th of a pixel.
std::vector< ICURun > ItemizeBidi(UChar *buff, size_t length)
Itemize the string into runs per embedding level.
std::vector< ICURun > ItemizeScript(UChar *buff, size_t length, std::vector< ICURun > &runs_current)
Itemize the string into runs per script, based on the previous created runs.
Functions related to laying out the texts with ICU.
Information about languages and their files.
const LanguageMetadata * _current_language
The currently loaded language.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
TextDirection _current_text_dir
Text direction of the currently selected language.
Functions related to OTTD's strings.
@ TD_RTL
Text is written right-to-left by default.
Functions related to zooming.
int ScaleSpriteTrad(int value)
Scale traditional pixel dimensions to GUI zoom level, for drawing sprites.