hb-gdi.cc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright © 2019 Ebrahim Byagowi
  3. *
  4. * This is part of HarfBuzz, a text shaping library.
  5. *
  6. * Permission is hereby granted, without written agreement and without
  7. * license or royalty fees, to use, copy, modify, and distribute this
  8. * software and its documentation for any purpose, provided that the
  9. * above copyright notice and the following two paragraphs appear in
  10. * all copies of this software.
  11. *
  12. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  13. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  14. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  15. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  16. * DAMAGE.
  17. *
  18. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  19. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  21. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  22. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  23. */
  24. #include "hb.hh"
  25. #ifdef HAVE_GDI
  26. #include "hb-gdi.h"
  27. /**
  28. * SECTION:hb-gdi
  29. * @title: hb-gdi
  30. * @short_description: GDI integration
  31. * @include: hb-gdi.h
  32. *
  33. * Functions for using HarfBuzz with GDI fonts.
  34. **/
  35. static hb_blob_t *
  36. _hb_gdi_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
  37. {
  38. char *buffer = nullptr;
  39. DWORD length = 0;
  40. HDC hdc = GetDC (nullptr);
  41. if (unlikely (!SelectObject (hdc, (HFONT) user_data))) goto fail;
  42. length = GetFontData (hdc, hb_uint32_swap (tag), 0, buffer, length);
  43. if (unlikely (length == GDI_ERROR)) goto fail_with_releasedc;
  44. buffer = (char *) hb_malloc (length);
  45. if (unlikely (!buffer)) goto fail_with_releasedc;
  46. length = GetFontData (hdc, hb_uint32_swap (tag), 0, buffer, length);
  47. if (unlikely (length == GDI_ERROR)) goto fail_with_releasedc_and_free;
  48. ReleaseDC (nullptr, hdc);
  49. return hb_blob_create ((const char *) buffer, length, HB_MEMORY_MODE_WRITABLE, buffer, hb_free);
  50. fail_with_releasedc_and_free:
  51. hb_free (buffer);
  52. fail_with_releasedc:
  53. ReleaseDC (nullptr, hdc);
  54. fail:
  55. return hb_blob_get_empty ();
  56. }
  57. /**
  58. * hb_gdi_face_create:
  59. * @hfont: a HFONT object.
  60. *
  61. * Constructs a new face object from the specified GDI HFONT.
  62. *
  63. * Return value: #hb_face_t object corresponding to the given input
  64. *
  65. * Since: 2.6.0
  66. **/
  67. hb_face_t *
  68. hb_gdi_face_create (HFONT hfont)
  69. {
  70. return hb_face_create_for_tables (_hb_gdi_reference_table, (void *) hfont, nullptr);
  71. }
  72. #endif