10 #if defined(WITH_UNISCRIBE) 12 #include "../../stdafx.h" 13 #include "../../debug.h" 15 #include "../../language.h" 16 #include "../../strings_func.h" 17 #include "../../string_func.h" 18 #include "../../table/control_codes.h" 25 #include "../../safeguards.h" 28 # pragma comment(lib, "usp10") 33 static SCRIPT_CACHE _script_cache[FS_END];
44 std::vector<GlyphID> ft_glyphs;
47 std::vector<WORD> char_to_glyph;
49 std::vector<SCRIPT_VISATTR> vis_attribs;
50 std::vector<WORD> glyphs;
51 std::vector<int> advances;
52 std::vector<GOFFSET> offsets;
55 UniscribeRun(
int pos,
int len, Font *font, SCRIPT_ANALYSIS &sa) : pos(pos), len(len), font(font), sa(sa) {}
59 static std::vector<SCRIPT_ITEM> UniscribeItemizeString(UniscribeParagraphLayoutFactory::CharType *buff, int32 length);
61 static bool UniscribeShapeRun(
const UniscribeParagraphLayoutFactory::CharType *buff, UniscribeRun &range);
68 const UniscribeParagraphLayoutFactory::CharType *text_buffer;
70 std::vector<UniscribeRun> ranges;
71 std::vector<UniscribeRun>::iterator cur_range;
72 int cur_range_offset = 0;
78 std::vector<GlyphID> glyphs;
79 std::vector<float> positions;
80 std::vector<WORD> char_to_glyph;
87 mutable int *glyph_to_char =
nullptr;
90 UniscribeVisualRun(
const UniscribeRun &range,
int x);
91 UniscribeVisualRun(UniscribeVisualRun &&other) noexcept;
92 ~UniscribeVisualRun()
override 94 free(this->glyph_to_char);
97 const GlyphID *GetGlyphs()
const override {
return &this->glyphs[0]; }
98 const float *GetPositions()
const override {
return &this->positions[0]; }
99 const int *GetGlyphToCharMap()
const override;
101 const Font *GetFont()
const override {
return this->font; }
102 int GetLeading()
const override {
return this->font->fc->GetHeight(); }
103 int GetGlyphCount()
const override {
return this->num_glyphs; }
104 int GetAdvance()
const {
return this->total_advance; }
110 int GetLeading()
const override;
111 int GetWidth()
const override;
112 int CountRuns()
const override {
return (uint)this->size(); }
113 const VisualRun &GetVisualRun(
int run)
const override {
return this->at(run); }
115 int GetInternalCharLength(
WChar c)
const override 118 return c >= 0x010000U ? 2 : 1;
122 UniscribeParagraphLayout(std::vector<UniscribeRun> &ranges,
const UniscribeParagraphLayoutFactory::CharType *buffer) : text_buffer(buffer), ranges(ranges)
127 ~UniscribeParagraphLayout()
override {}
129 void Reflow()
override 131 this->cur_range = this->ranges.begin();
132 this->cur_range_offset = 0;
135 std::unique_ptr<const Line> NextLine(
int max_width)
override;
138 void UniscribeResetScriptCache(
FontSize size)
140 if (_script_cache[size] !=
nullptr) {
141 ScriptFreeCache(&_script_cache[size]);
142 _script_cache[size] =
nullptr;
147 static HFONT HFontFromFont(Font *font)
149 if (font->fc->GetOSHandle() !=
nullptr)
return CreateFontIndirect((PLOGFONT)font->fc->GetOSHandle());
152 ZeroMemory(&logfont,
sizeof(LOGFONT));
153 logfont.lfHeight = font->fc->GetHeight();
154 logfont.lfWeight = FW_NORMAL;
155 logfont.lfCharSet = DEFAULT_CHARSET;
158 return CreateFontIndirect(&logfont);
162 static bool UniscribeShapeRun(
const UniscribeParagraphLayoutFactory::CharType *buff, UniscribeRun &range)
165 range.glyphs.resize(range.len * 3 / 2 + 16);
166 range.vis_attribs.resize(range.glyphs.size());
169 range.char_to_glyph.resize(range.len);
171 HDC temp_dc =
nullptr;
172 HFONT old_font =
nullptr;
173 HFONT cur_font =
nullptr;
178 HRESULT hr = ScriptShape(temp_dc, &_script_cache[range.font->fc->GetSize()], buff + range.pos, range.len, (int)range.glyphs.size(), &range.sa, &range.glyphs[0], &range.char_to_glyph[0], &range.vis_attribs[0], &glyphs_used);
181 range.glyphs.resize(glyphs_used);
182 range.vis_attribs.resize(glyphs_used);
186 range.advances.resize(range.glyphs.size());
187 range.offsets.resize(range.glyphs.size());
188 hr = ScriptPlace(temp_dc, &_script_cache[range.font->fc->GetSize()], &range.glyphs[0], (int)range.glyphs.size(), &range.vis_attribs[0], &range.sa, &range.advances[0], &range.offsets[0], &abc);
192 range.ft_glyphs.resize(range.glyphs.size());
193 for (
size_t g_id = 0; g_id < range.glyphs.size(); g_id++) {
194 range.ft_glyphs[g_id] = range.glyphs[g_id];
196 for (
int i = 0; i < range.len; i++) {
197 if (buff[range.pos + i] >= SCC_SPRITE_START && buff[range.pos + i] <= SCC_SPRITE_END) {
198 auto pos = range.char_to_glyph[i];
199 range.ft_glyphs[pos] = range.font->fc->MapCharToGlyph(buff[range.pos + i]);
200 range.offsets[pos].dv = range.font->fc->GetAscender() - range.font->fc->GetGlyph(range.ft_glyphs[pos])->height - 1;
201 range.advances[pos] = range.font->fc->GetGlyphWidth(range.ft_glyphs[pos]);
205 range.total_advance = 0;
206 for (
size_t i = 0; i < range.advances.size(); i++) {
209 if (range.advances[i] > 0 && range.ft_glyphs[i] != 0xFFFF) range.advances[i] = range.font->fc->GetGlyphWidth(range.ft_glyphs[i]);
211 range.total_advance += range.advances[i];
217 if (hr == E_OUTOFMEMORY) {
219 range.glyphs.resize(range.glyphs.size() * 2);
220 range.vis_attribs.resize(range.vis_attribs.size() * 2);
221 }
else if (hr == E_PENDING) {
223 cur_font = HFontFromFont(range.font);
224 if (cur_font ==
nullptr)
return false;
226 temp_dc = CreateCompatibleDC(
nullptr);
227 SetMapMode(temp_dc, MM_TEXT);
228 old_font = (HFONT)SelectObject(temp_dc, cur_font);
229 }
else if (hr == USP_E_SCRIPT_NOT_IN_FONT && range.sa.eScript != SCRIPT_UNDEFINED) {
231 range.sa.eScript = SCRIPT_UNDEFINED;
234 if (temp_dc !=
nullptr) {
235 SelectObject(temp_dc, old_font);
236 DeleteObject(cur_font);
237 ReleaseDC(
nullptr, temp_dc);
243 if (temp_dc !=
nullptr) {
244 SelectObject(temp_dc, old_font);
245 DeleteObject(cur_font);
246 ReleaseDC(
nullptr, temp_dc);
252 static std::vector<SCRIPT_ITEM> UniscribeItemizeString(UniscribeParagraphLayoutFactory::CharType *buff, int32 length)
255 SCRIPT_CONTROL control;
256 ZeroMemory(&control,
sizeof(SCRIPT_CONTROL));
260 ZeroMemory(&state,
sizeof(SCRIPT_STATE));
263 std::vector<SCRIPT_ITEM> items(16);
267 HRESULT hr = ScriptItemize(buff, length, (
int)items.size() - 1, &control, &state, &items[0], &generated);
271 items.resize(generated + 1);
275 if (hr != E_OUTOFMEMORY)
return std::vector<SCRIPT_ITEM>();
277 items.resize(items.size() * 2);
283 ParagraphLayouter *UniscribeParagraphLayoutFactory::GetParagraphLayout(CharType *buff, CharType *buff_end,
FontMap &fontMapping)
285 int32 length = buff_end - buff;
287 if (length == 0)
return nullptr;
290 for (
auto const &pair : fontMapping) {
291 if (pair.second->fc->IsBuiltInFont())
return nullptr;
295 std::vector<SCRIPT_ITEM> items = UniscribeItemizeString(buff, length);
296 if (items.size() == 0)
return nullptr;
300 std::vector<UniscribeRun> ranges;
303 std::vector<SCRIPT_ITEM>::iterator cur_item = items.begin();
304 for (
auto const &i : fontMapping) {
305 while (cur_pos < i.first && cur_item != items.end() - 1) {
307 int stop_pos =
min(i.first, (cur_item + 1)->iCharPos);
308 assert(stop_pos - cur_pos > 0);
309 ranges.push_back(UniscribeRun(cur_pos, stop_pos - cur_pos, i.second, cur_item->a));
312 if (!UniscribeShapeRun(buff, ranges.back())) {
317 if (stop_pos == (cur_item + 1)->iCharPos) cur_item++;
322 return new UniscribeParagraphLayout(ranges, buff);
325 std::unique_ptr<const ParagraphLayouter::Line> UniscribeParagraphLayout::NextLine(
int max_width)
327 std::vector<UniscribeRun>::iterator start_run = this->cur_range;
328 std::vector<UniscribeRun>::iterator last_run = this->cur_range;
330 if (start_run == this->ranges.end())
return nullptr;
334 if (this->cur_range_offset != 0) {
335 std::vector<int> dx(start_run->len);
336 ScriptGetLogicalWidths(&start_run->sa, start_run->len, (
int)start_run->glyphs.size(), &start_run->advances[0], &start_run->char_to_glyph[0], &start_run->vis_attribs[0], &dx[0]);
338 for (std::vector<int>::const_iterator c = dx.begin() + this->cur_range_offset; c != dx.end(); c++) {
345 while (last_run != this->ranges.end() && cur_width < max_width) {
346 cur_width += last_run->total_advance;
351 int remaing_offset = (last_run - 1)->len;
352 if (cur_width > max_width) {
353 std::vector<SCRIPT_LOGATTR> log_attribs;
356 int width_avail = max_width;
357 int num_chars = this->cur_range_offset;
358 int start_offs = this->cur_range_offset;
359 int last_cluster = this->cur_range_offset + 1;
360 for (std::vector<UniscribeRun>::iterator r = start_run; r != last_run; r++) {
361 log_attribs.resize(r->pos - start_run->pos + r->len);
362 if (FAILED(ScriptBreak(this->text_buffer + r->pos + start_offs, r->len - start_offs, &r->sa, &log_attribs[r->pos - start_run->pos + start_offs])))
return nullptr;
364 std::vector<int> dx(r->len);
365 ScriptGetLogicalWidths(&r->sa, r->len, (
int)r->glyphs.size(), &r->advances[0], &r->char_to_glyph[0], &r->vis_attribs[0], &dx[0]);
368 for (
int c = start_offs; c < r->len && width_avail > 0; c++, num_chars++) {
369 if (c > start_offs && log_attribs[num_chars].fCharStop) last_cluster = num_chars;
370 width_avail -= dx[c];
377 while (--num_chars > this->cur_range_offset && !log_attribs[num_chars].fSoftBreak && !log_attribs[num_chars].fWhiteSpace) {}
379 if (num_chars == this->cur_range_offset) {
381 num_chars = last_cluster;
385 while (num_chars < (
int)log_attribs.size() && log_attribs[num_chars].fWhiteSpace) {
390 for (std::vector<UniscribeRun>::iterator run = start_run; run != last_run; run++) {
391 num_chars -= run->len;
393 if (num_chars <= 0) {
394 remaing_offset = num_chars + run->len + 1;
396 assert(remaing_offset - 1 > 0);
403 std::vector<BYTE> bidi_level;
404 for (std::vector<UniscribeRun>::iterator r = start_run; r != last_run; r++) {
405 bidi_level.push_back(r->sa.s.uBidiLevel);
407 std::vector<INT> vis_to_log(bidi_level.size());
408 if (FAILED(ScriptLayout((
int)bidi_level.size(), &bidi_level[0], &vis_to_log[0],
nullptr)))
return nullptr;
411 std::unique_ptr<UniscribeLine> line(
new UniscribeLine());
414 for (std::vector<INT>::iterator l = vis_to_log.begin(); l != vis_to_log.end(); l++) {
415 std::vector<UniscribeRun>::iterator i_run = start_run + *l;
416 UniscribeRun run = *i_run;
419 if (i_run == last_run - 1 && remaing_offset < (last_run - 1)->len) {
420 run.len = remaing_offset - 1;
422 if (!UniscribeShapeRun(this->text_buffer, run))
return nullptr;
424 if (i_run == start_run && this->cur_range_offset > 0) {
425 assert(run.len - this->cur_range_offset > 0);
426 run.pos += this->cur_range_offset;
427 run.len -= this->cur_range_offset;
429 if (!UniscribeShapeRun(this->text_buffer, run))
return nullptr;
432 line->emplace_back(run, cur_pos);
433 cur_pos += run.total_advance;
436 if (remaing_offset < (last_run - 1)->len) {
438 this->cur_range_offset = remaing_offset - 1;
439 this->cur_range = last_run - 1;
440 assert(this->cur_range->len > this->cur_range_offset);
442 this->cur_range_offset = 0;
443 this->cur_range = last_run;
453 int UniscribeParagraphLayout::UniscribeLine::GetLeading()
const 456 for (
const auto &run : *
this) {
457 leading =
max(leading, run.GetLeading());
467 int UniscribeParagraphLayout::UniscribeLine::GetWidth()
const 470 for (
const auto &run : *
this) {
471 length += run.GetAdvance();
477 UniscribeParagraphLayout::UniscribeVisualRun::UniscribeVisualRun(
const UniscribeRun &range,
int x) : glyphs(range.ft_glyphs), char_to_glyph(range.char_to_glyph), start_pos(range.pos), total_advance(range.total_advance), font(range.font)
479 this->num_glyphs = (int)glyphs.size();
480 this->positions.resize(this->num_glyphs * 2 + 2);
483 for (
int i = 0; i < this->num_glyphs; i++) {
484 this->positions[i * 2 + 0] = range.offsets[i].du + advance + x;
485 this->positions[i * 2 + 1] = range.offsets[i].dv;
487 advance += range.advances[i];
489 this->positions[this->num_glyphs * 2] = advance + x;
492 UniscribeParagraphLayout::UniscribeVisualRun::UniscribeVisualRun(UniscribeVisualRun&& other) noexcept
493 : glyphs(std::move(other.glyphs)), positions(std::move(other.positions)), char_to_glyph(std::move(other.char_to_glyph)),
494 start_pos(other.start_pos), total_advance(other.total_advance), num_glyphs(other.num_glyphs), font(other.font)
496 this->glyph_to_char = other.glyph_to_char;
497 other.glyph_to_char =
nullptr;
500 const int *UniscribeParagraphLayout::UniscribeVisualRun::GetGlyphToCharMap()
const 502 if (this->glyph_to_char ==
nullptr) {
503 this->glyph_to_char = CallocT<int>(this->GetGlyphCount());
507 for (
int c = 0; c < (int)this->char_to_glyph.size(); c++) {
509 if (this->glyph_to_char[this->char_to_glyph[c]] == 0) this->glyph_to_char[this->char_to_glyph[c]] = c + this->start_pos;
513 int last_char = this->glyph_to_char[0];
514 for (
int g = 0; g < this->GetGlyphCount(); g++) {
515 if (this->glyph_to_char[g] != 0) last_char = this->glyph_to_char[g];
516 this->glyph_to_char[g] = last_char;
520 return this->glyph_to_char;
524 void UniscribeStringIterator::SetString(
const char *s)
526 const char *string_base = s;
528 this->utf16_to_utf8.clear();
529 this->str_info.clear();
534 std::vector<wchar_t> utf16_str;
536 size_t idx = s - string_base;
538 WChar c = Utf8Consume(&s);
540 utf16_str.push_back((
wchar_t)c);
543 utf16_str.push_back((
wchar_t)(0xD800 + ((c - 0x10000) >> 10)));
544 utf16_str.push_back((
wchar_t)(0xDC00 + ((c - 0x10000) & 0x3FF)));
545 this->utf16_to_utf8.push_back(idx);
547 this->utf16_to_utf8.push_back(idx);
549 this->utf16_to_utf8.push_back(s - string_base);
552 this->str_info.resize(utf16_to_utf8.size());
554 if (utf16_str.size() > 0) {
556 std::vector<SCRIPT_ITEM> runs = UniscribeItemizeString(&utf16_str[0], (int32)utf16_str.size());
558 for (std::vector<SCRIPT_ITEM>::const_iterator run = runs.begin(); runs.size() > 0 && run != runs.end() - 1; run++) {
560 int len = (run + 1)->iCharPos - run->iCharPos;
561 std::vector<SCRIPT_LOGATTR> attr(len);
562 ScriptBreak(&utf16_str[run->iCharPos], len, &run->a, &attr[0]);
565 for (
size_t c = 0; c < attr.size(); c++) {
567 this->str_info[c + run->iCharPos].word_stop = attr[c].fWordStop || c == 0;
568 this->str_info[c + run->iCharPos].char_stop = attr[c].fCharStop;
574 this->str_info.back().char_stop =
true;
575 this->str_info.back().word_stop =
true;
578 size_t UniscribeStringIterator::SetCurPosition(
size_t pos)
581 size_t utf16_pos = 0;
582 for (
size_t i = 0; i < this->utf16_to_utf8.size(); i++) {
583 if (this->utf16_to_utf8[i] == pos) {
590 while (utf16_pos > 0 && !this->str_info[utf16_pos].char_stop) utf16_pos--;
591 this->cur_pos = utf16_pos;
593 return this->utf16_to_utf8[this->cur_pos];
596 size_t UniscribeStringIterator::Next(IterType what)
598 assert(this->cur_pos <= this->utf16_to_utf8.size());
601 if (this->cur_pos == this->utf16_to_utf8.size())
return END;
605 }
while (this->cur_pos < this->utf16_to_utf8.size() && (what == ITER_WORD ? !this->str_info[this->cur_pos].word_stop : !this->str_info[this->cur_pos].char_stop));
607 return this->cur_pos == this->utf16_to_utf8.size() ? END : this->utf16_to_utf8[this->cur_pos];
610 size_t UniscribeStringIterator::Prev(IterType what)
612 assert(this->cur_pos <= this->utf16_to_utf8.size());
615 if (this->cur_pos == 0)
return END;
619 }
while (this->cur_pos > 0 && (what == ITER_WORD ? !this->str_info[this->cur_pos].word_stop : !this->str_info[this->cur_pos].char_stop));
621 return this->utf16_to_utf8[this->cur_pos];
Functions related to laying out text on Win32.
TCHAR * convert_to_fs(const char *name, TCHAR *system_buf, size_t buflen, bool console_cp)
Convert from OpenTTD's encoding to that of the environment in UNICODE.
const LanguageMetadata * _current_language
The currently loaded language.
Implementation of simple mapping class.
Visual run contains data about the bit of text with the same font.
static T max(const T a, const T b)
Returns the maximum of two values.
Iterate over characters (or more exactly grapheme clusters).
A single line worth of VisualRuns.
Interface to glue fallback and normal layouter into one.
#define lengthof(x)
Return the length of an fixed size array.
static T min(const T a, const T b)
Returns the minimum of two values.
TextDirection _current_text_dir
Text direction of the currently selected language.
FontSize
Available font sizes.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Text is written right-to-left by default.
uint32 GlyphID
Glyphs are characters from a font.
uint32 WChar
Type for wide characters, i.e.
declarations of functions for MS windows systems