view-cairo.hh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright © 2011 Google, Inc.
  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. * Google Author(s): Behdad Esfahbod
  25. */
  26. #ifndef VIEW_CAIRO_HH
  27. #define VIEW_CAIRO_HH
  28. #include "view-options.hh"
  29. #include "output-options.hh"
  30. #include "helper-cairo.hh"
  31. struct view_cairo_t : view_options_t, output_options_t<>
  32. {
  33. ~view_cairo_t ()
  34. {
  35. cairo_debug_reset_static_data ();
  36. }
  37. void add_options (option_parser_t *parser)
  38. {
  39. parser->set_summary ("View text with given font.");
  40. view_options_t::add_options (parser);
  41. output_options_t::add_options (parser, helper_cairo_supported_formats);
  42. }
  43. void init (hb_buffer_t *buffer, const font_options_t *font_opts)
  44. {
  45. lines = g_array_new (false, false, sizeof (helper_cairo_line_t));
  46. subpixel_bits = font_opts->subpixel_bits;
  47. }
  48. void new_line () {}
  49. void consume_text (hb_buffer_t *buffer,
  50. const char *text,
  51. unsigned int text_len,
  52. hb_bool_t utf8_clusters) {}
  53. void error (const char *message)
  54. { g_printerr ("%s: %s\n", g_get_prgname (), message); }
  55. void consume_glyphs (hb_buffer_t *buffer,
  56. const char *text,
  57. unsigned int text_len,
  58. hb_bool_t utf8_clusters)
  59. {
  60. direction = hb_buffer_get_direction (buffer);
  61. helper_cairo_line_t l (text, text_len, buffer, utf8_clusters, subpixel_bits);
  62. g_array_append_val (lines, l);
  63. }
  64. void finish (hb_buffer_t *buffer, const font_options_t *font_opts)
  65. {
  66. render (font_opts);
  67. for (unsigned int i = 0; i < lines->len; i++) {
  68. helper_cairo_line_t &line = g_array_index (lines, helper_cairo_line_t, i);
  69. line.finish ();
  70. }
  71. #if GLIB_CHECK_VERSION (2, 22, 0)
  72. g_array_unref (lines);
  73. #else
  74. g_array_free (lines, TRUE);
  75. #endif
  76. }
  77. protected:
  78. void render (const font_options_t *font_opts);
  79. hb_direction_t direction = HB_DIRECTION_INVALID; // Remove this, make segment_properties accessible
  80. GArray *lines = nullptr;
  81. unsigned subpixel_bits = 0;
  82. };
  83. inline void
  84. view_cairo_t::render (const font_options_t *font_opts)
  85. {
  86. bool vertical = HB_DIRECTION_IS_VERTICAL (direction);
  87. int vert = vertical ? 1 : 0;
  88. int horiz = vertical ? 0 : 1;
  89. int x_sign = font_opts->font_size_x < 0 ? -1 : +1;
  90. int y_sign = font_opts->font_size_y < 0 ? -1 : +1;
  91. hb_font_t *font = font_opts->font;
  92. if (!have_font_extents)
  93. {
  94. hb_font_extents_t hb_extents;
  95. hb_font_get_extents_for_direction (font, direction, &hb_extents);
  96. font_extents.ascent = scalbn ((double) hb_extents.ascender, - (int) subpixel_bits);
  97. font_extents.descent = -scalbn ((double) hb_extents.descender, - (int) subpixel_bits);
  98. font_extents.line_gap = scalbn ((double) hb_extents.line_gap, - (int) subpixel_bits);
  99. have_font_extents = true;
  100. }
  101. double ascent = y_sign * font_extents.ascent;
  102. double descent = y_sign * font_extents.descent;
  103. double line_gap = y_sign * font_extents.line_gap + line_space;
  104. double leading = ascent + descent + line_gap;
  105. /* Calculate surface size. */
  106. double w = 0, h = 0;
  107. (vertical ? w : h) = (int) lines->len * leading - (font_extents.line_gap + line_space);
  108. (vertical ? h : w) = 0;
  109. for (unsigned int i = 0; i < lines->len; i++) {
  110. helper_cairo_line_t &line = g_array_index (lines, helper_cairo_line_t, i);
  111. double x_advance, y_advance;
  112. line.get_advance (&x_advance, &y_advance);
  113. if (vertical)
  114. h = MAX (h, y_sign * y_advance);
  115. else
  116. w = MAX (w, x_sign * x_advance);
  117. }
  118. cairo_scaled_font_t *scaled_font = helper_cairo_create_scaled_font (font_opts,
  119. this);
  120. /* See if font needs color. */
  121. cairo_content_t content = CAIRO_CONTENT_ALPHA;
  122. if (helper_cairo_scaled_font_has_color (scaled_font))
  123. content = CAIRO_CONTENT_COLOR;
  124. /* Create surface. */
  125. cairo_t *cr = helper_cairo_create_context (w + margin.l + margin.r,
  126. h + margin.t + margin.b,
  127. this,
  128. this,
  129. content);
  130. cairo_set_scaled_font (cr, scaled_font);
  131. /* Setup coordinate system. */
  132. cairo_translate (cr, margin.l, margin.t);
  133. if (vertical)
  134. cairo_translate (cr,
  135. w - ascent, /* We currently always stack lines right to left */
  136. y_sign < 0 ? h : 0);
  137. else
  138. {
  139. cairo_translate (cr,
  140. x_sign < 0 ? w : 0,
  141. y_sign < 0 ? descent : ascent);
  142. }
  143. /* Draw. */
  144. cairo_translate (cr, +vert * leading, -horiz * leading);
  145. for (unsigned int i = 0; i < lines->len; i++)
  146. {
  147. helper_cairo_line_t &l = g_array_index (lines, helper_cairo_line_t, i);
  148. cairo_translate (cr, -vert * leading, +horiz * leading);
  149. if (show_extents)
  150. {
  151. cairo_save (cr);
  152. cairo_set_source_rgba (cr, 1., 0., 0., .5);
  153. cairo_set_line_width (cr, 10);
  154. cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
  155. for (unsigned i = 0; i < l.num_glyphs; i++) {
  156. cairo_move_to (cr, l.glyphs[i].x, l.glyphs[i].y);
  157. cairo_rel_line_to (cr, 0, 0);
  158. }
  159. cairo_stroke (cr);
  160. cairo_restore (cr);
  161. cairo_save (cr);
  162. cairo_set_source_rgba (cr, 1., 0., 1., .5);
  163. cairo_set_line_width (cr, 3);
  164. for (unsigned i = 0; i < l.num_glyphs; i++)
  165. {
  166. hb_glyph_extents_t hb_extents;
  167. hb_font_get_glyph_extents (font, l.glyphs[i].index, &hb_extents);
  168. double x1 = scalbn ((double) hb_extents.x_bearing, - (int) subpixel_bits);
  169. double y1 = -scalbn ((double) hb_extents.y_bearing, - (int) subpixel_bits);
  170. double width = scalbn ((double) hb_extents.width, - (int) subpixel_bits);
  171. double height = -scalbn ((double) hb_extents.height, - (int) subpixel_bits);
  172. cairo_rectangle (cr, l.glyphs[i].x + x1, l.glyphs[i].y + y1, width, height);
  173. }
  174. cairo_stroke (cr);
  175. cairo_restore (cr);
  176. }
  177. // https://github.com/harfbuzz/harfbuzz/issues/4378
  178. #if CAIRO_VERSION >= 11705
  179. if (l.num_clusters)
  180. cairo_show_text_glyphs (cr,
  181. l.utf8, l.utf8_len,
  182. l.glyphs, l.num_glyphs,
  183. l.clusters, l.num_clusters,
  184. l.cluster_flags);
  185. else
  186. #endif
  187. cairo_show_glyphs (cr, l.glyphs, l.num_glyphs);
  188. }
  189. /* Clean up. */
  190. helper_cairo_destroy_context (cr);
  191. cairo_scaled_font_destroy (scaled_font);
  192. }
  193. #endif