kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame^] | 1 | // Copyright 2022 The PDFium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "testing/font_renamer.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include "testing/test_fonts.h" |
| 10 | |
| 11 | namespace { |
| 12 | |
| 13 | FPDF_SYSFONTINFO* GetImpl(FPDF_SYSFONTINFO* info) { |
| 14 | return static_cast<FontRenamer*>(info)->impl(); |
| 15 | } |
| 16 | |
| 17 | void ReleaseImpl(FPDF_SYSFONTINFO* info) { |
| 18 | FPDF_SYSFONTINFO* impl = GetImpl(info); |
| 19 | impl->Release(impl); |
| 20 | } |
| 21 | |
| 22 | void EnumFontsImpl(FPDF_SYSFONTINFO* info, void* mapper) { |
| 23 | FPDF_SYSFONTINFO* impl = GetImpl(info); |
| 24 | impl->EnumFonts(impl, mapper); |
| 25 | } |
| 26 | |
| 27 | void* MapFontImpl(FPDF_SYSFONTINFO* info, |
| 28 | int weight, |
| 29 | FPDF_BOOL italic, |
| 30 | int charset, |
| 31 | int pitch_family, |
| 32 | const char* face, |
| 33 | FPDF_BOOL* exact) { |
| 34 | std::string renamed_face = TestFonts::RenameFont(face); |
| 35 | FPDF_SYSFONTINFO* impl = GetImpl(info); |
| 36 | return impl->MapFont(impl, weight, italic, charset, pitch_family, |
| 37 | renamed_face.c_str(), exact); |
| 38 | } |
| 39 | |
| 40 | void* GetFontImpl(FPDF_SYSFONTINFO* info, const char* face) { |
| 41 | // Any non-null return will do. |
| 42 | FPDF_SYSFONTINFO* impl = GetImpl(info); |
| 43 | std::string renamed_face = TestFonts::RenameFont(face); |
| 44 | return impl->GetFont(impl, renamed_face.c_str()); |
| 45 | } |
| 46 | |
| 47 | unsigned long GetFontDataImpl(FPDF_SYSFONTINFO* info, |
| 48 | void* font, |
| 49 | unsigned int table, |
| 50 | unsigned char* buffer, |
| 51 | unsigned long buf_size) { |
| 52 | FPDF_SYSFONTINFO* impl = GetImpl(info); |
| 53 | return impl->GetFontData(impl, font, table, buffer, buf_size); |
| 54 | } |
| 55 | |
| 56 | unsigned long GetFaceNameImpl(FPDF_SYSFONTINFO* info, |
| 57 | void* font, |
| 58 | char* buffer, |
| 59 | unsigned long buf_size) { |
| 60 | FPDF_SYSFONTINFO* impl = GetImpl(info); |
| 61 | return impl->GetFaceName(impl, font, buffer, buf_size); |
| 62 | } |
| 63 | |
| 64 | int GetFontCharsetImpl(FPDF_SYSFONTINFO* info, void* font) { |
| 65 | FPDF_SYSFONTINFO* impl = GetImpl(info); |
| 66 | return impl->GetFontCharset(impl, font); |
| 67 | } |
| 68 | |
| 69 | void DeleteFontImpl(FPDF_SYSFONTINFO* info, void* font) { |
| 70 | FPDF_SYSFONTINFO* impl = GetImpl(info); |
| 71 | impl->DeleteFont(impl, font); |
| 72 | } |
| 73 | |
| 74 | } // namespace |
| 75 | |
| 76 | FontRenamer::FontRenamer() : impl_(FPDF_GetDefaultSystemFontInfo()) { |
| 77 | version = 1; |
| 78 | Release = ReleaseImpl; |
| 79 | EnumFonts = EnumFontsImpl; |
| 80 | MapFont = MapFontImpl; |
| 81 | GetFont = GetFontImpl; |
| 82 | GetFontData = GetFontDataImpl; |
| 83 | GetFaceName = GetFaceNameImpl; |
| 84 | GetFontCharset = GetFontCharsetImpl; |
| 85 | DeleteFont = DeleteFontImpl; |
| 86 | FPDF_SetSystemFontInfo(this); |
| 87 | } |
| 88 | |
| 89 | FontRenamer::~FontRenamer() { |
| 90 | FPDF_FreeDefaultSystemFontInfo(impl_.ExtractAsDangling()); |
| 91 | } |