Home | History | Annotate | Download | only in util
      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 
     27 #include "view-cairo.hh"
     28 
     29 #include <assert.h>
     30 
     31 
     32 void
     33 view_cairo_t::render (const font_options_t *font_opts)
     34 {
     35   bool vertical = HB_DIRECTION_IS_VERTICAL (direction);
     36   int vert  = vertical ? 1 : 0;
     37   int horiz = vertical ? 0 : 1;
     38 
     39   int x_sign = font_opts->font_size_x < 0 ? -1 : +1;
     40   int y_sign = font_opts->font_size_y < 0 ? -1 : +1;
     41 
     42   hb_font_t *font = font_opts->get_font();
     43   hb_font_extents_t extents;
     44   hb_font_get_extents_for_direction (font, direction, &extents);
     45 
     46   double ascent = y_sign * scalbn ((double) extents.ascender, scale_bits);
     47   double descent = y_sign * -scalbn ((double) extents.descender, scale_bits);
     48   double font_height = y_sign * scalbn ((double) extents.ascender - extents.descender + extents.line_gap, scale_bits);
     49   double leading = font_height + view_options.line_space;
     50 
     51   /* Calculate surface size. */
     52   double w, h;
     53   (vertical ? w : h) = (int) lines->len * leading - view_options.line_space;
     54   (vertical ? h : w) = 0;
     55   for (unsigned int i = 0; i < lines->len; i++) {
     56     helper_cairo_line_t &line = g_array_index (lines, helper_cairo_line_t, i);
     57     double x_advance, y_advance;
     58     line.get_advance (&x_advance, &y_advance);
     59     if (vertical)
     60       h =  MAX (h, y_sign * y_advance);
     61     else
     62       w =  MAX (w, x_sign * x_advance);
     63   }
     64 
     65   cairo_scaled_font_t *scaled_font = helper_cairo_create_scaled_font (font_opts);
     66 
     67   /* See if font needs color. */
     68   cairo_content_t content = CAIRO_CONTENT_ALPHA;
     69   if (helper_cairo_scaled_font_has_color (scaled_font))
     70     content = CAIRO_CONTENT_COLOR;
     71 
     72   /* Create surface. */
     73   cairo_t *cr = helper_cairo_create_context (w + view_options.margin.l + view_options.margin.r,
     74 					     h + view_options.margin.t + view_options.margin.b,
     75 					     &view_options, &output_options, content);
     76   cairo_set_scaled_font (cr, scaled_font);
     77 
     78   /* Setup coordinate system. */
     79   cairo_translate (cr, view_options.margin.l, view_options.margin.t);
     80   if (vertical)
     81     cairo_translate (cr,
     82 		     w /* We stack lines right to left */
     83 		     -font_height * .5 /* "ascent" for vertical */,
     84 		     y_sign < 0 ? h : 0);
     85   else
     86    {
     87     cairo_translate (cr,
     88 		     x_sign < 0 ? w : 0,
     89 		     y_sign < 0 ? descent : ascent);
     90    }
     91 
     92   /* Draw. */
     93   cairo_translate (cr, +vert * leading, -horiz * leading);
     94   for (unsigned int i = 0; i < lines->len; i++)
     95   {
     96     helper_cairo_line_t &l = g_array_index (lines, helper_cairo_line_t, i);
     97 
     98     cairo_translate (cr, -vert * leading, +horiz * leading);
     99 
    100     if (view_options.annotate) {
    101       cairo_save (cr);
    102 
    103       /* Draw actual glyph origins */
    104       cairo_set_source_rgba (cr, 1., 0., 0., .5);
    105       cairo_set_line_width (cr, 5);
    106       cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
    107       for (unsigned i = 0; i < l.num_glyphs; i++) {
    108 	cairo_move_to (cr, l.glyphs[i].x, l.glyphs[i].y);
    109 	cairo_rel_line_to (cr, 0, 0);
    110       }
    111       cairo_stroke (cr);
    112 
    113       cairo_restore (cr);
    114     }
    115 
    116     if (0 && cairo_surface_get_type (cairo_get_target (cr)) == CAIRO_SURFACE_TYPE_IMAGE) {
    117       /* cairo_show_glyphs() doesn't support subpixel positioning */
    118       cairo_glyph_path (cr, l.glyphs, l.num_glyphs);
    119       cairo_fill (cr);
    120     } else if (l.num_clusters)
    121       cairo_show_text_glyphs (cr,
    122 			      l.utf8, l.utf8_len,
    123 			      l.glyphs, l.num_glyphs,
    124 			      l.clusters, l.num_clusters,
    125 			      l.cluster_flags);
    126     else
    127       cairo_show_glyphs (cr, l.glyphs, l.num_glyphs);
    128   }
    129 
    130   /* Clean up. */
    131   helper_cairo_destroy_context (cr);
    132   cairo_scaled_font_destroy (scaled_font);
    133 }
    134