PDFFont.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2022, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/HashMap.h>
  8. #include <LibGfx/Font/OpenType/Font.h>
  9. #include <LibGfx/Forward.h>
  10. #include <LibPDF/Document.h>
  11. #include <LibPDF/Encoding.h>
  12. namespace PDF {
  13. class Renderer;
  14. // PDF files don't need most of the data in OpenType fonts, and even contain invalid data for
  15. // these tables in some cases. Skip reading these tables.
  16. constexpr u32 pdf_skipped_opentype_tables = OpenType::FontOptions::SkipTables::Name | OpenType::FontOptions::SkipTables::Hmtx | OpenType::FontOptions::SkipTables::OS2;
  17. enum class WritingMode {
  18. Horizontal,
  19. Vertical,
  20. };
  21. class PDFFont : public RefCounted<PDFFont> {
  22. public:
  23. static PDFErrorOr<NonnullRefPtr<PDFFont>> create(Document*, NonnullRefPtr<DictObject> const&, float font_size);
  24. virtual ~PDFFont() = default;
  25. virtual void set_font_size(float font_size) = 0;
  26. virtual PDFErrorOr<Gfx::FloatPoint> draw_string(Gfx::Painter&, Gfx::FloatPoint, ByteString const&, Renderer const&) = 0;
  27. virtual WritingMode writing_mode() const { return WritingMode::Horizontal; }
  28. // TABLE 5.20 Font flags
  29. bool is_fixed_pitch() const { return m_flags & (1 << (1 - 1)); }
  30. bool is_serif() const { return m_flags & (1 << (2 - 1)); }
  31. static constexpr unsigned Symbolic = 1 << (3 - 1);
  32. bool is_symbolic() const { return m_flags & Symbolic; }
  33. bool is_script() const { return m_flags & (1 << (4 - 1)); }
  34. // Note: No bit position 5.
  35. static constexpr unsigned NonSymbolic = 1 << (6 - 1);
  36. bool is_nonsymbolic() const { return m_flags & NonSymbolic; }
  37. bool is_italic() const { return m_flags & (1 << (7 - 1)); }
  38. // Note: Big jump in bit positions.
  39. bool is_all_cap() const { return m_flags & (1 << (17 - 1)); }
  40. bool is_small_cap() const { return m_flags & (1 << (18 - 1)); }
  41. bool is_force_bold() const { return m_flags & (1 << (19 - 1)); }
  42. protected:
  43. virtual PDFErrorOr<void> initialize(Document* document, NonnullRefPtr<DictObject> const& dict, float font_size);
  44. static PDFErrorOr<NonnullRefPtr<Gfx::ScaledFont>> replacement_for(StringView name, float font_size);
  45. unsigned m_flags { 0 };
  46. };
  47. }