1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MEDIAPROVIDER_PDF_JNI_PDFCLIENT_UTILS_UTF_H_ 18 #define MEDIAPROVIDER_PDF_JNI_PDFCLIENT_UTILS_UTF_H_ 19 20 #include <functional> 21 #include <string> 22 23 namespace pdfClient_utils { 24 // Wrapper around a Pdfium function to make it easier to get a UTF-8-encoded 25 // string. 26 // Many Pdfium functions have the following form: 27 // size_t FPDF_GetFooString(other_args ..., void* buffer, size_t buffer_len); 28 // These functions return the number of bytes in the result, regardless of the 29 // value of buffer_len. If buffer_len >= the number of bytes in the result, 30 // buffer is also filled in with the result value. That value is 31 // UTF16LE-encoded. 32 // 33 // GetUtf8Result accepts as its only argument a std::function that accepts a 34 // void* buffer and size_t buffer_len (with any other arguments being pre-bound 35 // using, e.g., BindFront, a lambda, or a wrapper), and returns a UTF-8-encoded 36 // string. Allocating the buffer and dealing with UTF conversions are abstracted 37 // away. 38 template <class T> 39 std::string GetUtf8Result(const std::function<size_t(T*, size_t)>& f); 40 41 // Converts a UTF-8-encoded string into a UTF16LE u16string. 42 std::u16string Utf8ToUtf16Le(std::string_view utf8); 43 } // namespace pdfClient_utils 44 45 #endif // MEDIAPROVIDER_PDF_JNI_PDFCLIENT_UTILS_UTF_H_