SimpleFont.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) 2023, Rodrigo Tobar <rtobarc@gmail.com>.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Forward.h>
  7. #include <LibPDF/CommonNames.h>
  8. #include <LibPDF/Error.h>
  9. #include <LibPDF/Fonts/PDFFont.h>
  10. #include <LibPDF/Fonts/SimpleFont.h>
  11. #include <LibPDF/Fonts/TrueTypeFont.h>
  12. #include <LibPDF/Fonts/Type1Font.h>
  13. namespace PDF {
  14. PDFErrorOr<void> SimpleFont::initialize(Document* document, NonnullRefPtr<DictObject> const& dict, float font_size)
  15. {
  16. TRY(PDFFont::initialize(document, dict, font_size));
  17. if (dict->contains(CommonNames::Encoding)) {
  18. auto encoding_object = MUST(dict->get_object(document, CommonNames::Encoding));
  19. m_encoding = TRY(Encoding::from_object(document, encoding_object));
  20. }
  21. if (dict->contains(CommonNames::ToUnicode))
  22. m_to_unicode = TRY(dict->get_stream(document, CommonNames::ToUnicode));
  23. if (dict->contains(CommonNames::FirstChar) && dict->contains(CommonNames::LastChar) && dict->contains(CommonNames::Widths)) {
  24. auto first_char = dict->get_value(CommonNames::FirstChar).get<int>();
  25. auto last_char = dict->get_value(CommonNames::LastChar).get<int>();
  26. auto widths_array = TRY(dict->get_array(document, CommonNames::Widths));
  27. VERIFY(widths_array->size() == static_cast<size_t>(last_char - first_char + 1));
  28. for (size_t i = 0; i < widths_array->size(); i++)
  29. m_widths.set(first_char + i, widths_array->at(i).to_int());
  30. }
  31. if (dict->contains(CommonNames::FontDescriptor)) {
  32. auto descriptor = TRY(dict->get_dict(document, CommonNames::FontDescriptor));
  33. if (descriptor->contains(CommonNames::MissingWidth))
  34. m_missing_width = descriptor->get_value(CommonNames::MissingWidth).to_int();
  35. }
  36. return {};
  37. }
  38. PDFErrorOr<Gfx::FloatPoint> SimpleFont::draw_string(Gfx::Painter& painter, Gfx::FloatPoint glyph_position, DeprecatedString const& string, Color const& paint_color, float font_size, float character_spacing, float word_spacing, float horizontal_scaling)
  39. {
  40. for (auto char_code : string.bytes()) {
  41. // Use the width specified in the font's dictionary if available,
  42. // and use the default width for the given font otherwise.
  43. float glyph_width;
  44. if (auto width = m_widths.get(char_code); width.has_value())
  45. glyph_width = font_size * width.value() / 1000.0f;
  46. else
  47. glyph_width = get_glyph_width(char_code);
  48. draw_glyph(painter, glyph_position, glyph_width, char_code, paint_color);
  49. auto tx = glyph_width;
  50. tx += character_spacing;
  51. // ISO 32000 (PDF 2.0), 9.3.3 Wordspacing
  52. // "Word spacing shall be applied to every occurrence of the single-byte character code 32
  53. // in a string when using a simple font (including Type 3) or a composite font that defines
  54. // code 32 as a single-byte code."
  55. if (char_code == ' ')
  56. tx += word_spacing;
  57. tx *= horizontal_scaling;
  58. glyph_position += { tx, 0.0f };
  59. }
  60. return glyph_position;
  61. }
  62. }