PDFFont.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2022, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/String.h>
  7. #include <AK/StringView.h>
  8. #include <LibGfx/Font/FontDatabase.h>
  9. #include <LibPDF/CommonNames.h>
  10. #include <LibPDF/Fonts/PDFFont.h>
  11. #include <LibPDF/Fonts/TrueTypeFont.h>
  12. #include <LibPDF/Fonts/Type0Font.h>
  13. #include <LibPDF/Fonts/Type1Font.h>
  14. #include <LibPDF/Fonts/Type3Font.h>
  15. namespace PDF {
  16. [[maybe_unused]] static bool is_standard_latin_font(DeprecatedFlyString const& font)
  17. {
  18. return font.is_one_of(
  19. "Times-Roman", "TimesNewRoman",
  20. "Helvetica", "Arial",
  21. "Courier", "CourierNew",
  22. "Times-Bold", "TimesNewRoman,Bold",
  23. "Helvetica-Bold", "Arial,Bold",
  24. "Courier-Bold", "CourierNew,Bold",
  25. "Times-Italic", "TimesNewRoman,Italic",
  26. "Helvetica-Oblique", "Arial,Italic",
  27. "Courier-Oblique", "CourierNew,Italic",
  28. "Times-BoldItalic", "TimesNewRoman,BoldItalic",
  29. "Helvetica-BoldOblique", "Arial,BoldItalic",
  30. "Courier-BoldOblique", "CourierNew,BoldItalic");
  31. }
  32. PDFErrorOr<NonnullRefPtr<PDFFont>> PDFFont::create(Document* document, NonnullRefPtr<DictObject> const& dict, float font_size)
  33. {
  34. auto subtype = TRY(dict->get_name(document, CommonNames::Subtype))->name();
  35. RefPtr<PDFFont> font;
  36. if (subtype == "Type1") {
  37. font = adopt_ref(*new Type1Font());
  38. } else if (subtype == "TrueType") {
  39. font = adopt_ref(*new TrueTypeFont());
  40. } else if (subtype == "Type0") {
  41. font = adopt_ref(*new Type0Font());
  42. } else if (subtype == "Type3") {
  43. font = adopt_ref(*new Type3Font());
  44. } else {
  45. dbgln_if(PDF_DEBUG, "Unhandled font subtype: {}", subtype);
  46. return Error::internal_error("Unhandled font subtype");
  47. }
  48. TRY(font->initialize(document, dict, font_size));
  49. return font.release_nonnull();
  50. }
  51. PDFErrorOr<void> PDFFont::initialize(Document*, NonnullRefPtr<DictObject> const&, float)
  52. {
  53. return {};
  54. }
  55. PDFErrorOr<NonnullRefPtr<Gfx::Font>> PDFFont::replacement_for(StringView name, float font_size)
  56. {
  57. bool is_bold = name.contains("bold"sv, CaseSensitivity::CaseInsensitive);
  58. bool is_italic = name.contains("italic"sv, CaseSensitivity::CaseInsensitive);
  59. FlyString font_family;
  60. if (name.contains("times"sv, CaseSensitivity::CaseInsensitive)) {
  61. font_family = "Liberation Serif"_fly_string;
  62. } else if (name.contains("courier"sv, CaseSensitivity::CaseInsensitive)) {
  63. font_family = "Liberation Mono"_fly_string;
  64. } else {
  65. font_family = "Liberation Sans"_fly_string;
  66. }
  67. FlyString font_variant;
  68. if (is_bold && is_italic) {
  69. font_variant = "Bold Italic"_fly_string;
  70. } else if (is_bold) {
  71. font_variant = "Bold"_fly_string;
  72. } else if (is_italic) {
  73. font_variant = "Italic"_fly_string;
  74. } else {
  75. font_variant = "Regular"_fly_string;
  76. }
  77. float point_size = (font_size * POINTS_PER_INCH) / DEFAULT_DPI;
  78. auto font = Gfx::FontDatabase::the().get(font_family, font_variant, point_size);
  79. if (!font)
  80. return Error::internal_error("Failed to load {} {} at {}pt", font_family, font_variant, point_size);
  81. return font.release_nonnull();
  82. }
  83. }