xref: /aosp_15_r20/external/skia/modules/skunicode/src/SkUnicode_icu_builtin.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2021 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 #include "include/private/base/SkFeatures.h"  // IWYU pragma: keep
8 #include "modules/skunicode/src/SkUnicode_icupriv.h"
9 
10 #include <memory>
11 #include <type_traits>
12 #include <utility>
13 
14 #include <unicode/ubrk.h>
15 #include <unicode/uloc.h>
16 #include <unicode/utypes.h>
17 
18 namespace {
19 
20 // ubrk_clone added as draft in ICU69 and Android API 31 (first ICU NDK).
21 // ubrk_safeClone deprecated in ICU69 and not exposed by Android.
22 template<typename T, typename = void>
23 struct SkUbrkClone {
clone__anon3b0bd79d0111::SkUbrkClone24     static UBreakIterator* clone(T bi, UErrorCode* status) {
25         return ubrk_safeClone(bi, nullptr, nullptr, status);
26     }
27 };
28 template<typename T>
29 struct SkUbrkClone<T, std::void_t<decltype(ubrk_clone(std::declval<T>(), nullptr))>> {
clone__anon3b0bd79d0111::SkUbrkClone30     static UBreakIterator* clone(T bi, UErrorCode* status) {
31         return ubrk_clone(bi, status);
32     }
33 };
34 
35 // ubrk_getLocaleByType has been in ICU since version 2.8
36 // However, it was not included in the Android NDK
37 template<typename T, typename = void>
38 struct SkUbrkGetLocaleByType {
getLocaleByType__anon3b0bd79d0111::SkUbrkGetLocaleByType39     static const char* getLocaleByType(T bi, ULocDataLocaleType type, UErrorCode* status) {
40         *status = U_UNSUPPORTED_ERROR;
41         return nullptr;
42     }
43 };
44 template<typename T>
45 struct SkUbrkGetLocaleByType<
46     T,
47     std::void_t<decltype(ubrk_getLocaleByType(std::declval<T>(),
48                                               std::declval<ULocDataLocaleType>(),
49                                               nullptr))>>
50 {
getLocaleByType__anon3b0bd79d0111::SkUbrkGetLocaleByType51     static const char* getLocaleByType(T bi, ULocDataLocaleType type, UErrorCode* status) {
52         return ubrk_getLocaleByType(bi, type, status);
53     }
54 };
55 
56 }  // namespace
57 
58 #define SKICU_FUNC(funcname) funcname,
SkLoadICULib()59 std::unique_ptr<SkICULib> SkLoadICULib() {
60     return std::make_unique<SkICULib>(SkICULib{
61         SKICU_EMIT_FUNCS
62         &SkUbrkClone<const UBreakIterator*>::clone,
63         nullptr,
64         &SkUbrkGetLocaleByType<const UBreakIterator*>::getLocaleByType,
65     });
66 }
67 
68