1cb93a386Sopenharmony_ci// Copyright 2021 Google LLC.
2cb93a386Sopenharmony_ci#include "include/core/SkBitmap.h"
3cb93a386Sopenharmony_ci#include "include/core/SkCanvas.h"
4cb93a386Sopenharmony_ci#include "include/core/SkColor.h"
5cb93a386Sopenharmony_ci#include "include/core/SkEncodedImageFormat.h"
6cb93a386Sopenharmony_ci#include "include/core/SkFontMgr.h"
7cb93a386Sopenharmony_ci#include "include/core/SkFontStyle.h"
8cb93a386Sopenharmony_ci#include "include/core/SkImageEncoder.h"
9cb93a386Sopenharmony_ci#include "include/core/SkPaint.h"
10cb93a386Sopenharmony_ci#include "include/core/SkPoint.h"
11cb93a386Sopenharmony_ci#include "include/core/SkRect.h"
12cb93a386Sopenharmony_ci#include "include/core/SkRefCnt.h"
13cb93a386Sopenharmony_ci#include "include/core/SkScalar.h"
14cb93a386Sopenharmony_ci#include "include/core/SkSpan.h"
15cb93a386Sopenharmony_ci#include "include/core/SkStream.h"
16cb93a386Sopenharmony_ci#include "include/core/SkString.h"
17cb93a386Sopenharmony_ci#include "include/core/SkTypeface.h"
18cb93a386Sopenharmony_ci#include "include/core/SkTypes.h"
19cb93a386Sopenharmony_ci#include "tests/Test.h"
20cb93a386Sopenharmony_ci#include "tools/Resources.h"
21cb93a386Sopenharmony_ci
22cb93a386Sopenharmony_ci#include "experimental/sktext/include/Text.h"
23cb93a386Sopenharmony_ci
24cb93a386Sopenharmony_ci#include <string.h>
25cb93a386Sopenharmony_ci#include <algorithm>
26cb93a386Sopenharmony_ci#include <limits>
27cb93a386Sopenharmony_ci#include <memory>
28cb93a386Sopenharmony_ci#include <string>
29cb93a386Sopenharmony_ci#include <utility>
30cb93a386Sopenharmony_ci#include <vector>
31cb93a386Sopenharmony_ci
32cb93a386Sopenharmony_cistruct GrContextOptions;
33cb93a386Sopenharmony_ci
34cb93a386Sopenharmony_ci#define VeryLongCanvasWidth 1000000
35cb93a386Sopenharmony_ci#define TestCanvasWidth 1000
36cb93a386Sopenharmony_ci#define TestCanvasHeight 600
37cb93a386Sopenharmony_ci
38cb93a386Sopenharmony_ciusing namespace skia::text;
39cb93a386Sopenharmony_ci
40cb93a386Sopenharmony_cinamespace {
41cb93a386Sopenharmony_ci    bool operator==(SkSpan<const char16_t> a, SkSpan<const char16_t> b) {
42cb93a386Sopenharmony_ci        if (a.size() != b.size()) {
43cb93a386Sopenharmony_ci            return false;
44cb93a386Sopenharmony_ci        }
45cb93a386Sopenharmony_ci        for (size_t i = 0; i < a.size(); ++i) {
46cb93a386Sopenharmony_ci            if (a[i] != b[i]) {
47cb93a386Sopenharmony_ci                return false;
48cb93a386Sopenharmony_ci            }
49cb93a386Sopenharmony_ci        }
50cb93a386Sopenharmony_ci        return true;
51cb93a386Sopenharmony_ci    }
52cb93a386Sopenharmony_ci}
53cb93a386Sopenharmony_ci
54cb93a386Sopenharmony_ciUNIX_ONLY_TEST(SkText_UnicodeText_Flags, reporter) {
55cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, true);
56cb93a386Sopenharmony_ci    //                       01234567890  1234567890
57cb93a386Sopenharmony_ci    std::u16string utf16(u"Hello word\nHello world");
58cb93a386Sopenharmony_ci    SkString utf8("Hello word\nHello world");
59cb93a386Sopenharmony_ci    UnicodeText unicodeText16(SkUnicode::Make(), SkSpan<uint16_t>((uint16_t*)utf16.data(), utf16.size()));
60cb93a386Sopenharmony_ci    UnicodeText unicodeText8(SkUnicode::Make(), utf8);
61cb93a386Sopenharmony_ci
62cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, unicodeText16.getText16() == unicodeText8.getText16(), "UTF16 and UTF8 texts should be the same\n");
63cb93a386Sopenharmony_ci    auto lineBreak = utf16.find_first_of(u"\n");
64cb93a386Sopenharmony_ci    for (size_t i = 0; i < unicodeText16.getText16().size(); ++i) {
65cb93a386Sopenharmony_ci        if (i == lineBreak) {
66cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, unicodeText16.hasProperty(i, CodeUnitFlags::kHardLineBreakBefore), "Pos16 %d should point to hard line break\n", lineBreak);
67cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, unicodeText8 .hasProperty(i, CodeUnitFlags::kHardLineBreakBefore), "Pos8 %d should point to hard line break\n", lineBreak);
68cb93a386Sopenharmony_ci        } else {
69cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, unicodeText16.hasProperty(i, CodeUnitFlags::kGraphemeStart), "Pos16 %d should be a grapheme start\n", i);
70cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, unicodeText8 .hasProperty(i, CodeUnitFlags::kGraphemeStart), "Pos8 %d should be a grapheme start\n", i);
71cb93a386Sopenharmony_ci        }
72cb93a386Sopenharmony_ci    }
73cb93a386Sopenharmony_ci
74cb93a386Sopenharmony_ci    auto space1 = utf16.find_first_of(u" ");
75cb93a386Sopenharmony_ci    auto space2 = utf16.find_last_of(u" ");
76cb93a386Sopenharmony_ci
77cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, unicodeText16.hasProperty(space1, CodeUnitFlags::kPartOfWhiteSpace), "Pos16 %d should be a part of whitespaces\n", space1);
78cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, unicodeText16.hasProperty(space1 + 1, CodeUnitFlags::kSoftLineBreakBefore), "Pos16 %d should have soft line break before\n", space1 + 1);
79cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, unicodeText16.hasProperty(space2, CodeUnitFlags::kPartOfWhiteSpace), "Pos16 %d should be a part of whitespaces\n", space2);
80cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, unicodeText16.hasProperty(space2 + 1, CodeUnitFlags::kSoftLineBreakBefore), "Pos16 %d should have soft line break before\n", space2 + 1);
81cb93a386Sopenharmony_ci
82cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, unicodeText8 .hasProperty(space1, CodeUnitFlags::kPartOfWhiteSpace), "Pos8 %d should be a part of whitespaces\n", space1);
83cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, unicodeText8 .hasProperty(space1 + 1, CodeUnitFlags::kSoftLineBreakBefore), "Pos8 %d should have soft line break before\n", space1 + 1);
84cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, unicodeText8 .hasProperty(space2, CodeUnitFlags::kPartOfWhiteSpace), "Pos8 %d should be a part of whitespaces\n", space2);
85cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, unicodeText8 .hasProperty(space2 + 1, CodeUnitFlags::kSoftLineBreakBefore), "Pos8 %d should have soft line break before\n", space2 + 1);
86cb93a386Sopenharmony_ci}
87cb93a386Sopenharmony_ci
88cb93a386Sopenharmony_ci// TODO: Test RTL text
89