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
GetImpl(FPDF_SYSFONTINFO * info)13 FPDF_SYSFONTINFO* GetImpl(FPDF_SYSFONTINFO* info) {
14 return static_cast<FontRenamer*>(info)->impl();
15 }
16
ReleaseImpl(FPDF_SYSFONTINFO * info)17 void ReleaseImpl(FPDF_SYSFONTINFO* info) {
18 FPDF_SYSFONTINFO* impl = GetImpl(info);
19 impl->Release(impl);
20 }
21
EnumFontsImpl(FPDF_SYSFONTINFO * info,void * mapper)22 void EnumFontsImpl(FPDF_SYSFONTINFO* info, void* mapper) {
23 FPDF_SYSFONTINFO* impl = GetImpl(info);
24 impl->EnumFonts(impl, mapper);
25 }
26
MapFontImpl(FPDF_SYSFONTINFO * info,int weight,FPDF_BOOL italic,int charset,int pitch_family,const char * face,FPDF_BOOL * exact)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
GetFontImpl(FPDF_SYSFONTINFO * info,const char * face)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
GetFontDataImpl(FPDF_SYSFONTINFO * info,void * font,unsigned int table,unsigned char * buffer,unsigned long buf_size)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
GetFaceNameImpl(FPDF_SYSFONTINFO * info,void * font,char * buffer,unsigned long buf_size)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
GetFontCharsetImpl(FPDF_SYSFONTINFO * info,void * font)64 int GetFontCharsetImpl(FPDF_SYSFONTINFO* info, void* font) {
65 FPDF_SYSFONTINFO* impl = GetImpl(info);
66 return impl->GetFontCharset(impl, font);
67 }
68
DeleteFontImpl(FPDF_SYSFONTINFO * info,void * font)69 void DeleteFontImpl(FPDF_SYSFONTINFO* info, void* font) {
70 FPDF_SYSFONTINFO* impl = GetImpl(info);
71 impl->DeleteFont(impl, font);
72 }
73
74 } // namespace
75
FontRenamer()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
~FontRenamer()89 FontRenamer::~FontRenamer() {
90 FPDF_FreeDefaultSystemFontInfo(impl_.ExtractAsDangling());
91 }
92