10 #include "../../stdafx.h"
11 #include "../../debug.h"
13 #include "../../language.h"
14 #include "../../strings_func.h"
15 #include "../../string_func.h"
16 #include "../../table/control_codes.h"
23 #include "../../safeguards.h"
26 # pragma comment(lib, "usp10")
42 std::vector<GlyphID> ft_glyphs;
45 std::vector<WORD> char_to_glyph;
47 std::vector<SCRIPT_VISATTR> vis_attribs;
48 std::vector<WORD> glyphs;
49 std::vector<int> advances;
50 std::vector<GOFFSET> offsets;
53 UniscribeRun(
int pos,
int len, Font *font, SCRIPT_ANALYSIS &sa) : pos(pos), len(len), font(font), sa(sa) {}
76 std::vector<GlyphID> glyphs;
77 std::vector<float> positions;
78 std::vector<WORD> char_to_glyph;
85 mutable int *glyph_to_char =
nullptr;
92 free(this->glyph_to_char);
95 const GlyphID *GetGlyphs()
const override {
return &this->glyphs[0]; }
96 const float *GetPositions()
const override {
return &this->positions[0]; }
97 const int *GetGlyphToCharMap()
const override;
99 const Font *GetFont()
const override {
return this->font; }
100 int GetLeading()
const override {
return this->font->fc->GetHeight(); }
101 int GetGlyphCount()
const override {
return this->num_glyphs; }
102 int GetAdvance()
const {
return this->total_advance; }
110 int CountRuns()
const override {
return (uint)this->size(); }
111 const VisualRun &GetVisualRun(
int run)
const override {
return this->at(run); }
113 int GetInternalCharLength(
WChar c)
const override
116 return c >= 0x010000U ? 2 : 1;
127 void Reflow()
override
129 this->cur_range = this->ranges.begin();
130 this->cur_range_offset = 0;
133 std::unique_ptr<const Line> NextLine(
int max_width)
override;
136 void UniscribeResetScriptCache(
FontSize size)
147 if (font->fc->GetOSHandle() !=
nullptr)
return CreateFontIndirect((
const PLOGFONT)font->fc->GetOSHandle());
150 ZeroMemory(&logfont,
sizeof(LOGFONT));
151 logfont.lfHeight = font->fc->GetHeight();
152 logfont.lfWeight = FW_NORMAL;
153 logfont.lfCharSet = DEFAULT_CHARSET;
156 return CreateFontIndirect(&logfont);
163 range.glyphs.resize(range.len * 3 / 2 + 16);
164 range.vis_attribs.resize(range.glyphs.size());
167 range.char_to_glyph.resize(range.len);
169 HDC temp_dc =
nullptr;
170 HFONT old_font =
nullptr;
171 HFONT cur_font =
nullptr;
176 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);
179 range.glyphs.resize(glyphs_used);
180 range.vis_attribs.resize(glyphs_used);
184 range.advances.resize(range.glyphs.size());
185 range.offsets.resize(range.glyphs.size());
186 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);
190 range.ft_glyphs.resize(range.glyphs.size());
191 for (
size_t g_id = 0; g_id < range.glyphs.size(); g_id++) {
192 range.ft_glyphs[g_id] = range.glyphs[g_id];
194 for (
int i = 0; i < range.len; i++) {
195 if (buff[range.pos + i] >= SCC_SPRITE_START && buff[range.pos + i] <= SCC_SPRITE_END) {
196 auto pos = range.char_to_glyph[i];
197 range.ft_glyphs[pos] = range.font->fc->MapCharToGlyph(buff[range.pos + i]);
198 range.offsets[pos].dv = range.font->fc->GetAscender() - range.font->fc->GetGlyph(range.ft_glyphs[pos])->height - 1;
199 range.advances[pos] = range.font->fc->GetGlyphWidth(range.ft_glyphs[pos]);
203 range.total_advance = 0;
204 for (
size_t i = 0; i < range.advances.size(); i++) {
207 if (range.advances[i] > 0 && range.ft_glyphs[i] != 0xFFFF) range.advances[i] = range.font->fc->GetGlyphWidth(range.ft_glyphs[i]);
209 range.total_advance += range.advances[i];
215 if (hr == E_OUTOFMEMORY) {
217 range.glyphs.resize(range.glyphs.size() * 2);
218 range.vis_attribs.resize(range.vis_attribs.size() * 2);
219 }
else if (hr == E_PENDING) {
222 if (cur_font ==
nullptr)
return false;
224 temp_dc = CreateCompatibleDC(
nullptr);
225 SetMapMode(temp_dc, MM_TEXT);
226 old_font = (HFONT)SelectObject(temp_dc, cur_font);
227 }
else if (hr == USP_E_SCRIPT_NOT_IN_FONT && range.sa.eScript != SCRIPT_UNDEFINED) {
229 range.sa.eScript = SCRIPT_UNDEFINED;
232 if (temp_dc !=
nullptr) {
233 SelectObject(temp_dc, old_font);
234 DeleteObject(cur_font);
235 ReleaseDC(
nullptr, temp_dc);
241 if (temp_dc !=
nullptr) {
242 SelectObject(temp_dc, old_font);
243 DeleteObject(cur_font);
244 ReleaseDC(
nullptr, temp_dc);
253 SCRIPT_CONTROL control;
254 ZeroMemory(&control,
sizeof(SCRIPT_CONTROL));
258 ZeroMemory(&state,
sizeof(SCRIPT_STATE));
261 std::vector<SCRIPT_ITEM> items(16);
265 HRESULT hr = ScriptItemize(buff, length, (
int)items.size() - 1, &control, &state, &items[0], &generated);
269 items.resize(generated + 1);
273 if (hr != E_OUTOFMEMORY)
return std::vector<SCRIPT_ITEM>();
275 items.resize(items.size() * 2);
283 int32 length = buff_end - buff;
285 if (length == 0)
return nullptr;
288 for (
auto const &pair : fontMapping) {
289 if (pair.second->fc->IsBuiltInFont())
return nullptr;
294 if (items.size() == 0)
return nullptr;
298 std::vector<UniscribeRun> ranges;
301 std::vector<SCRIPT_ITEM>::iterator cur_item = items.begin();
302 for (
auto const &i : fontMapping) {
303 while (cur_pos < i.first && cur_item != items.end() - 1) {
305 int stop_pos = std::min(i.first, (cur_item + 1)->iCharPos);
306 assert(stop_pos - cur_pos > 0);
307 ranges.push_back(
UniscribeRun(cur_pos, stop_pos - cur_pos, i.second, cur_item->a));
315 if (stop_pos == (cur_item + 1)->iCharPos) cur_item++;
323 std::unique_ptr<const ParagraphLayouter::Line> UniscribeParagraphLayout::NextLine(
int max_width)
325 std::vector<UniscribeRun>::iterator start_run = this->
cur_range;
326 std::vector<UniscribeRun>::iterator last_run = this->
cur_range;
328 if (start_run == this->ranges.end())
return nullptr;
332 if (this->cur_range_offset != 0) {
333 std::vector<int> dx(start_run->len);
334 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]);
336 for (std::vector<int>::const_iterator c = dx.begin() + this->cur_range_offset; c != dx.end(); c++) {
343 while (last_run != this->ranges.end() && cur_width < max_width) {
344 cur_width += last_run->total_advance;
349 int remaing_offset = (last_run - 1)->len;
350 if (cur_width > max_width) {
351 std::vector<SCRIPT_LOGATTR> log_attribs;
354 int width_avail = max_width;
357 int last_cluster = this->cur_range_offset + 1;
358 for (std::vector<UniscribeRun>::iterator r = start_run; r != last_run; r++) {
359 log_attribs.resize(r->pos - start_run->pos + r->len);
360 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;
362 std::vector<int> dx(r->len);
363 ScriptGetLogicalWidths(&r->sa, r->len, (
int)r->glyphs.size(), &r->advances[0], &r->char_to_glyph[0], &r->vis_attribs[0], &dx[0]);
366 for (
int c = start_offs; c < r->len && width_avail > 0; c++, num_chars++) {
367 if (c > start_offs && log_attribs[num_chars].fCharStop) last_cluster = num_chars;
368 width_avail -= dx[c];
375 while (--num_chars > this->cur_range_offset && !log_attribs[num_chars].fSoftBreak && !log_attribs[num_chars].fWhiteSpace) {}
377 if (num_chars == this->cur_range_offset) {
379 num_chars = last_cluster;
383 while (num_chars < (
int)log_attribs.size() && log_attribs[num_chars].fWhiteSpace) {
388 for (std::vector<UniscribeRun>::iterator run = start_run; run != last_run; run++) {
389 num_chars -= run->len;
391 if (num_chars <= 0) {
392 remaing_offset = num_chars + run->len + 1;
394 assert(remaing_offset - 1 > 0);
401 std::vector<BYTE> bidi_level;
402 for (std::vector<UniscribeRun>::iterator r = start_run; r != last_run; r++) {
403 bidi_level.push_back(r->sa.s.uBidiLevel);
405 std::vector<INT> vis_to_log(bidi_level.size());
406 if (FAILED(ScriptLayout((
int)bidi_level.size(), &bidi_level[0], &vis_to_log[0],
nullptr)))
return nullptr;
409 std::unique_ptr<UniscribeLine> line(
new UniscribeLine());
412 for (std::vector<INT>::iterator l = vis_to_log.begin(); l != vis_to_log.end(); l++) {
413 std::vector<UniscribeRun>::iterator i_run = start_run + *l;
417 if (i_run == last_run - 1 && remaing_offset < (last_run - 1)->len) {
418 run.len = remaing_offset - 1;
422 if (i_run == start_run && this->cur_range_offset > 0) {
423 assert(run.len - this->cur_range_offset > 0);
430 line->emplace_back(run, cur_pos);
431 cur_pos += run.total_advance;
434 if (remaing_offset < (last_run - 1)->len) {
436 this->cur_range_offset = remaing_offset - 1;
437 this->cur_range = last_run - 1;
438 assert(this->cur_range->len > this->cur_range_offset);
440 this->cur_range_offset = 0;
441 this->cur_range = last_run;
454 for (
const auto &run : *
this) {
455 leading = std::max(leading, run.GetLeading());
468 for (
const auto &run : *
this) {
469 length += run.GetAdvance();
475 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)
477 this->num_glyphs = (int)glyphs.size();
478 this->positions.resize(this->num_glyphs * 2 + 2);
481 for (
int i = 0; i < this->num_glyphs; i++) {
482 this->positions[i * 2 + 0] = range.offsets[i].du + advance + x;
483 this->positions[i * 2 + 1] = range.offsets[i].dv;
485 advance += range.advances[i];
487 this->positions[this->num_glyphs * 2] = advance + x;
490 UniscribeParagraphLayout::UniscribeVisualRun::UniscribeVisualRun(UniscribeVisualRun&& other) noexcept
491 : glyphs(std::move(other.glyphs)), positions(std::move(other.positions)), char_to_glyph(std::move(other.char_to_glyph)),
492 start_pos(other.start_pos), total_advance(other.total_advance), num_glyphs(other.num_glyphs), font(other.font)
494 this->glyph_to_char = other.glyph_to_char;
495 other.glyph_to_char =
nullptr;
498 const int *UniscribeParagraphLayout::UniscribeVisualRun::GetGlyphToCharMap()
const
500 if (this->glyph_to_char ==
nullptr) {
501 this->glyph_to_char = CallocT<int>(this->GetGlyphCount());
505 for (
int c = 0; c < (int)this->char_to_glyph.size(); c++) {
507 if (this->glyph_to_char[this->char_to_glyph[c]] == 0) this->glyph_to_char[this->char_to_glyph[c]] = c + this->start_pos;
511 int last_char = this->glyph_to_char[0];
512 for (
int g = 0; g < this->GetGlyphCount(); g++) {
513 if (this->glyph_to_char[g] != 0) last_char = this->glyph_to_char[g];
514 this->glyph_to_char[g] = last_char;
518 return this->glyph_to_char;
524 const char *string_base = s;
526 this->utf16_to_utf8.clear();
527 this->str_info.clear();
532 std::vector<wchar_t> utf16_str;
534 size_t idx = s - string_base;
536 WChar c = Utf8Consume(&s);
538 utf16_str.push_back((
wchar_t)c);
541 utf16_str.push_back((
wchar_t)(0xD800 + ((c - 0x10000) >> 10)));
542 utf16_str.push_back((
wchar_t)(0xDC00 + ((c - 0x10000) & 0x3FF)));
543 this->utf16_to_utf8.push_back(idx);
545 this->utf16_to_utf8.push_back(idx);
547 this->utf16_to_utf8.push_back(s - string_base);
550 this->str_info.resize(utf16_to_utf8.size());
552 if (utf16_str.size() > 0) {
556 for (std::vector<SCRIPT_ITEM>::const_iterator run = runs.begin(); runs.size() > 0 && run != runs.end() - 1; run++) {
558 int len = (run + 1)->iCharPos - run->iCharPos;
559 std::vector<SCRIPT_LOGATTR> attr(len);
560 ScriptBreak(&utf16_str[run->iCharPos], len, &run->a, &attr[0]);
563 for (
size_t c = 0; c < attr.size(); c++) {
565 this->str_info[c + run->iCharPos].word_stop = attr[c].fWordStop || c == 0;
566 this->str_info[c + run->iCharPos].char_stop = attr[c].fCharStop;
572 this->str_info.back().char_stop =
true;
573 this->str_info.back().word_stop =
true;
579 size_t utf16_pos = 0;
580 for (
size_t i = 0; i < this->utf16_to_utf8.size(); i++) {
581 if (this->utf16_to_utf8[i] == pos) {
588 while (utf16_pos > 0 && !this->str_info[utf16_pos].char_stop) utf16_pos--;
589 this->cur_pos = utf16_pos;
591 return this->utf16_to_utf8[this->cur_pos];
596 assert(this->cur_pos <= this->utf16_to_utf8.size());
599 if (this->cur_pos == this->utf16_to_utf8.size())
return END;
603 }
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));
605 return this->cur_pos == this->utf16_to_utf8.size() ? END : this->utf16_to_utf8[this->cur_pos];
610 assert(this->cur_pos <= this->utf16_to_utf8.size());
613 if (this->cur_pos == 0)
return END;
617 }
while (this->cur_pos > 0 && (what == ITER_WORD ? !this->str_info[this->cur_pos].word_stop : !this->str_info[this->cur_pos].char_stop));
619 return this->utf16_to_utf8[this->cur_pos];