Typeface.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Typeface.h>
  7. namespace Gfx {
  8. unsigned Typeface::weight() const
  9. {
  10. VERIFY(m_ttf_font || m_bitmap_fonts.size() > 0);
  11. if (is_fixed_size())
  12. return m_bitmap_fonts[0]->weight();
  13. return m_ttf_font->weight();
  14. }
  15. u8 Typeface::slope() const
  16. {
  17. VERIFY(m_ttf_font || m_bitmap_fonts.size() > 0);
  18. if (is_fixed_size())
  19. return m_bitmap_fonts[0]->slope();
  20. return m_ttf_font->slope();
  21. }
  22. bool Typeface::is_fixed_width() const
  23. {
  24. VERIFY(m_ttf_font || m_bitmap_fonts.size() > 0);
  25. if (is_fixed_size())
  26. return m_bitmap_fonts[0]->is_fixed_width();
  27. return m_ttf_font->is_fixed_width();
  28. }
  29. void Typeface::add_bitmap_font(RefPtr<BitmapFont> font)
  30. {
  31. m_bitmap_fonts.append(font);
  32. }
  33. void Typeface::set_ttf_font(RefPtr<TTF::Font> font)
  34. {
  35. m_ttf_font = move(font);
  36. }
  37. RefPtr<Font> Typeface::get_font(unsigned size) const
  38. {
  39. for (auto font : m_bitmap_fonts) {
  40. if (font->presentation_size() == size)
  41. return font;
  42. }
  43. if (m_ttf_font)
  44. return adopt_ref(*new TTF::ScaledFont(*m_ttf_font, size, size));
  45. return {};
  46. }
  47. void Typeface::for_each_fixed_size_font(Function<void(const Font&)> callback) const
  48. {
  49. for (auto font : m_bitmap_fonts) {
  50. callback(*font);
  51. }
  52. }
  53. }