PDFFont.cpp 3.3 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. namespace PDF {
  15. [[maybe_unused]] static bool is_standard_latin_font(DeprecatedFlyString const& font)
  16. {
  17. return font.is_one_of(
  18. "Times-Roman", "TimesNewRoman",
  19. "Helvetica", "Arial",
  20. "Courier", "CourierNew",
  21. "Times-Bold", "TimesNewRoman,Bold",
  22. "Helvetica-Bold", "Arial,Bold",
  23. "Courier-Bold", "CourierNew,Bold",
  24. "Times-Italic", "TimesNewRoman,Italic",
  25. "Helvetica-Oblique", "Arial,Italic",
  26. "Courier-Oblique", "CourierNew,Italic",
  27. "Times-BoldItalic", "TimesNewRoman,BoldItalic",
  28. "Helvetica-BoldOblique", "Arial,BoldItalic",
  29. "Courier-BoldOblique", "CourierNew,BoldItalic");
  30. }
  31. PDFErrorOr<NonnullRefPtr<PDFFont>> PDFFont::create(Document* document, NonnullRefPtr<DictObject> const& dict, float font_size)
  32. {
  33. auto subtype = TRY(dict->get_name(document, CommonNames::Subtype))->name();
  34. RefPtr<PDFFont> font;
  35. if (subtype == "Type1") {
  36. font = adopt_ref(*new Type1Font());
  37. } else if (subtype == "TrueType") {
  38. font = adopt_ref(*new TrueTypeFont());
  39. } else if (subtype == "Type0") {
  40. font = adopt_ref(*new Type0Font());
  41. } else if (subtype == "Type3") {
  42. return Error { Error::Type::RenderingUnsupported, "Type3 fonts not yet implemented" };
  43. } else {
  44. dbgln_if(PDF_DEBUG, "Unhandled font subtype: {}", subtype);
  45. return Error::internal_error("Unhandled font subtype");
  46. }
  47. TRY(font->initialize(document, dict, font_size));
  48. return font.release_nonnull();
  49. }
  50. PDFErrorOr<void> PDFFont::initialize(Document* document, NonnullRefPtr<DictObject> const& dict, float)
  51. {
  52. m_base_font_name = TRY(dict->get_name(document, CommonNames::BaseFont))->name();
  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. }