145675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod/*
245675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod * Copyright © 2011,2012  Google, Inc.
345675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod *
445675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod *  This is part of HarfBuzz, a text shaping library.
545675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod *
645675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod * Permission is hereby granted, without written agreement and without
745675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this
845675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod * software and its documentation for any purpose, provided that the
945675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod * above copyright notice and the following two paragraphs appear in
1045675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod * all copies of this software.
1145675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod *
1245675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
1345675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
1445675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
1545675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
1645675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod * DAMAGE.
1745675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod *
1845675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
1945675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
2045675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
2145675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
2245675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
2345675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod *
2445675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod * Google Author(s): Behdad Esfahbod
2545675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod */
2645675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod
2745675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod#include "options.hh"
2845675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod
2945675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod#ifndef HB_SHAPE_CONSUMER_HH
3045675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod#define HB_SHAPE_CONSUMER_HH
3145675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod
3245675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod
3345675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbodtemplate <typename output_t>
3445675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbodstruct shape_consumer_t
3545675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod{
3645675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  shape_consumer_t (option_parser_t *parser)
3745675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod		  : shaper (parser),
3845675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod		    output (parser) {}
3945675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod
4045675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  void init (const font_options_t *font_opts)
4145675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  {
4245675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod    font = hb_font_reference (font_opts->get_font ());
4345675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod    output.init (font_opts);
445db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod    failed = false;
4545675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  }
4645675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  void consume_line (hb_buffer_t  *buffer,
4745675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod		     const char   *text,
48321f73c16efa0730c96e001d65268f4927a0f735Behdad Esfahbod		     unsigned int  text_len,
49321f73c16efa0730c96e001d65268f4927a0f735Behdad Esfahbod		     const char   *text_before,
50321f73c16efa0730c96e001d65268f4927a0f735Behdad Esfahbod		     const char   *text_after)
5145675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  {
525db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod    output.new_line ();
53ae62166519291057316a9d15cea3f1570fcb5eafBehdad Esfahbod
54321f73c16efa0730c96e001d65268f4927a0f735Behdad Esfahbod    shaper.populate_buffer (buffer, text, text_len, text_before, text_after);
555db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod    output.consume_text (buffer, text, text_len, shaper.utf8_clusters);
5645675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod
57ae62166519291057316a9d15cea3f1570fcb5eafBehdad Esfahbod    if (!shaper.shape (font, buffer)) {
585db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod      failed = true;
595db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod      hb_buffer_set_length (buffer, 0);
605db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod      output.shape_failed (buffer, text, text_len, shaper.utf8_clusters);
615db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod      return;
625db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod    }
635db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod
645db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod    output.consume_glyphs (buffer, text, text_len, shaper.utf8_clusters);
6545675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  }
6645675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  void finish (const font_options_t *font_opts)
6745675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  {
6845675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod    output.finish (font_opts);
6945675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod    hb_font_destroy (font);
7045675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod    font = NULL;
7145675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  }
7245675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod
735db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod  public:
745db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod  bool failed;
755db0683a822f70c914468430cda6487cee740ae3Behdad Esfahbod
7645675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  protected:
7745675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  shape_options_t shaper;
7845675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  output_t output;
7945675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod
8045675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod  hb_font_t *font;
8145675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod};
8245675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod
8345675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod
8445675e589e6ef9f81b2a4199cf33e3e7778433a8Behdad Esfahbod#endif
85