| 1 |
new file mode 100644 |
| 2 |
index 0000000..fffffff |
| 3 |
--- /dev/null |
| 4 |
+++ SkLoadICU.cpp |
| 5 |
@@ -0,0 +1,121 @@ |
| 6 |
+// Copyright 2019 Google LLC. |
| 7 |
+// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
| 8 |
+ |
| 9 |
+#include "SkLoadICU.h" |
| 10 |
+ |
| 11 |
+#if defined(_WIN32) && defined(SK_USING_THIRD_PARTY_ICU) |
| 12 |
+ |
| 13 |
+#ifndef WIN32_LEAN_AND_MEAN |
| 14 |
+#define WIN32_LEAN_AND_MEAN |
| 15 |
+#endif |
| 16 |
+#include <windows.h> |
| 17 |
+#include <io.h> |
| 18 |
+ |
| 19 |
+#include <cstdio> |
| 20 |
+#include <cstring> |
| 21 |
+#include <mutex> |
| 22 |
+#include <string> |
| 23 |
+ |
| 24 |
+#include "unicode/udata.h" |
| 25 |
+ |
| 26 |
+static void* win_mmap(const wchar_t* dataFile) { |
| 27 |
+ if (!dataFile) { |
| 28 |
+ return nullptr; |
| 29 |
+ } |
| 30 |
+ struct FCloseWrapper { void operator()(FILE* f) { fclose(f); } }; |
| 31 |
+ std::unique_ptr<FILE, FCloseWrapper> stream(_wfopen(dataFile, L"rb")); |
| 32 |
+ if (!stream) { |
| 33 |
+ fprintf(stderr, "SkIcuLoader: datafile missing: %ls.\n", dataFile); |
| 34 |
+ return nullptr; |
| 35 |
+ } |
| 36 |
+ int fileno = _fileno(stream.get()); |
| 37 |
+ if (fileno < 0) { |
| 38 |
+ fprintf(stderr, "SkIcuLoader: datafile fileno error.\n"); |
| 39 |
+ return nullptr; |
| 40 |
+ } |
| 41 |
+ HANDLE file = (HANDLE)_get_osfhandle(fileno); |
| 42 |
+ if ((HANDLE)INVALID_HANDLE_VALUE == file) { |
| 43 |
+ fprintf(stderr, "SkIcuLoader: datafile handle error.\n"); |
| 44 |
+ return nullptr; |
| 45 |
+ } |
| 46 |
+ struct CloseHandleWrapper { void operator()(HANDLE h) { CloseHandle(h); } }; |
| 47 |
+ std::unique_ptr<void, CloseHandleWrapper> mmapHandle( |
| 48 |
+ CreateFileMapping(file, nullptr, PAGE_READONLY, 0, 0, nullptr)); |
| 49 |
+ if (!mmapHandle) { |
| 50 |
+ fprintf(stderr, "SkIcuLoader: datafile mmap error.\n"); |
| 51 |
+ return nullptr; |
| 52 |
+ } |
| 53 |
+ void* addr = MapViewOfFile(mmapHandle.get(), FILE_MAP_READ, 0, 0, 0); |
| 54 |
+ if (nullptr == addr) { |
| 55 |
+ fprintf(stderr, "SkIcuLoader: datafile view error.\n"); |
| 56 |
+ return nullptr; |
| 57 |
+ } |
| 58 |
+ return addr; |
| 59 |
+} |
| 60 |
+ |
| 61 |
+static bool init_icu(void* addr) { |
| 62 |
+ UErrorCode err = U_ZERO_ERROR; |
| 63 |
+ udata_setCommonData(addr, &err); |
| 64 |
+ if (err != U_ZERO_ERROR) { |
| 65 |
+ fprintf(stderr, "udata_setCommonData() returned %d.\n", (int)err); |
| 66 |
+ return false; |
| 67 |
+ } |
| 68 |
+ udata_setFileAccess(UDATA_ONLY_PACKAGES, &err); |
| 69 |
+ if (err != U_ZERO_ERROR) { |
| 70 |
+ fprintf(stderr, "udata_setFileAccess() returned %d.\n", (int)err); |
| 71 |
+ return false; |
| 72 |
+ } |
| 73 |
+ return true; |
| 74 |
+} |
| 75 |
+ |
| 76 |
+static std::wstring get_module_path(HMODULE module) { |
| 77 |
+ DWORD len; |
| 78 |
+ std::wstring path; |
| 79 |
+ path.resize(MAX_PATH); |
| 80 |
+ |
| 81 |
+ len = GetModuleFileNameW(module, (LPWSTR)path.data(), (DWORD)path.size()); |
| 82 |
+ if (len > path.size()) { |
| 83 |
+ path.resize(len); |
| 84 |
+ len = GetModuleFileNameW(module, (LPWSTR)path.data(), (DWORD)path.size()); |
| 85 |
+ } |
| 86 |
+ path.resize(len); |
| 87 |
+ std::size_t end = path.rfind('\\'); |
| 88 |
+ if (end == std::wstring::npos) { |
| 89 |
+ return std::wstring(); |
| 90 |
+ } |
| 91 |
+ path.resize(end); |
| 92 |
+ return path; |
| 93 |
+} |
| 94 |
+ |
| 95 |
+static std::wstring library_directory() { |
| 96 |
+ HMODULE hModule = NULL; |
| 97 |
+ GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, |
| 98 |
+ reinterpret_cast<LPCSTR>(&library_directory), &hModule); |
| 99 |
+ return get_module_path(hModule); |
| 100 |
+} |
| 101 |
+ |
| 102 |
+static std::wstring executable_directory() { |
| 103 |
+ HMODULE hModule = GetModuleHandleA(NULL); |
| 104 |
+ return get_module_path(hModule); |
| 105 |
+} |
| 106 |
+ |
| 107 |
+static bool load_from(const std::wstring& dir) { |
| 108 |
+ auto sPath = dir + L"\\icudtl.dat"; |
| 109 |
+ if (void* addr = win_mmap(sPath.c_str())) { |
| 110 |
+ if (init_icu(addr)) { |
| 111 |
+ return true; |
| 112 |
+ } |
| 113 |
+ } |
| 114 |
+ return false; |
| 115 |
+} |
| 116 |
+ |
| 117 |
+bool SkLoadICU() { |
| 118 |
+ static bool good = false; |
| 119 |
+ static std::once_flag flag; |
| 120 |
+ std::call_once(flag, []() { |
| 121 |
+ good = load_from(executable_directory()) || load_from(library_directory()); |
| 122 |
+ }); |
| 123 |
+ return good; |
| 124 |
+} |
| 125 |
+ |
| 126 |
+#endif // defined(_WIN32) && defined(SK_USING_THIRD_PARTY_ICU) |
| 127 |
new file mode 100644 |
| 128 |
index 0000000..fffffff |
| 129 |
--- /dev/null |
| 130 |
+++ SkLoadICU.h |
| 131 |
@@ -0,0 +1,16 @@ |
| 132 |
+/* |
| 133 |
+ * Copyright 2018 Google Inc. |
| 134 |
+ * |
| 135 |
+ * Use of this source code is governed by a BSD-style license that can be |
| 136 |
+ * found in the LICENSE file. |
| 137 |
+ */ |
| 138 |
+#ifndef load_icu_DEFINED |
| 139 |
+#define load_icu_DEFINED |
| 140 |
+ |
| 141 |
+#if defined(_WIN32) && defined(SK_USING_THIRD_PARTY_ICU) |
| 142 |
+bool SkLoadICU(); |
| 143 |
+#else |
| 144 |
+static inline bool SkLoadICU() { return true; } |
| 145 |
+#endif // defined(_WIN32) && defined(SK_USING_THIRD_PARTY_ICU) |
| 146 |
+ |
| 147 |
+#endif // load_icu_DEFINED |
| 148 |
new file mode 100644 |
| 149 |
index 0000000..fffffff |
| 150 |
--- /dev/null |
| 151 |
+++ make_data_cpp.py |
| 152 |
@@ -0,0 +1,50 @@ |
| 153 |
+#!/usr/bin/env python |
| 154 |
+ |
| 155 |
+# Copyright 2019 Google LLC. |
| 156 |
+# Use of this source code is governed by a BSD-style license that can be |
| 157 |
+# found in the LICENSE file. |
| 158 |
+ |
| 159 |
+''' |
| 160 |
+Generate a source file containing the given binary data. |
| 161 |
+ |
| 162 |
+Output type is C++. |
| 163 |
+''' |
| 164 |
+ |
| 165 |
+from __future__ import print_function |
| 166 |
+ |
| 167 |
+import os |
| 168 |
+import struct |
| 169 |
+import sys |
| 170 |
+import mmap |
| 171 |
+ |
| 172 |
+def iterate_as_uint32(path): |
| 173 |
+ with open(path, 'rb') as f: |
| 174 |
+ s = struct.Struct('@I') |
| 175 |
+ assert s.size == 4 |
| 176 |
+ mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) |
| 177 |
+ assert (len(mm) % s.size) == 0 |
| 178 |
+ for offset in range(0, len(mm), s.size): |
| 179 |
+ yield s.unpack_from(mm, offset)[0] |
| 180 |
+ mm.close() |
| 181 |
+ |
| 182 |
+ |
| 183 |
+def convert(fmt, name, src_path, dst_path): |
| 184 |
+ header, line_begin, line_end, footer = fmt |
| 185 |
+ assert os.path.exists(src_path) |
| 186 |
+ src = iterate_as_uint32(src_path) |
| 187 |
+ with open(dst_path, 'w') as o: |
| 188 |
+ o.write(header.format(name)) |
| 189 |
+ while True: |
| 190 |
+ line = ','.join('%d' % v for _, v in zip(range(8), src)) |
| 191 |
+ if not line: |
| 192 |
+ break |
| 193 |
+ o.write('%s%s%s\n' % (line_begin, line, line_end)) |
| 194 |
+ o.write(footer.format(name)) |
| 195 |
+ |
| 196 |
+ |
| 197 |
+cpp = ('#include <cstdint>\nextern "C" uint32_t {0}[] __attribute__((aligned(16))) = {{\n', |
| 198 |
+ '', ',', '}};\n') |
| 199 |
+ |
| 200 |
+if __name__ == '__main__': |
| 201 |
+ print('\n'.join('>>> %r' % x for x in sys.argv)) |
| 202 |
+ convert(cpp, sys.argv[1], sys.argv[2], sys.argv[3]) |
| 203 |
|