gfx/graphite2/src/gr_segment.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/graphite2/src/gr_segment.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,174 @@
     1.4 +/*  GRAPHITE2 LICENSING
     1.5 +
     1.6 +    Copyright 2010, SIL International
     1.7 +    All rights reserved.
     1.8 +
     1.9 +    This library is free software; you can redistribute it and/or modify
    1.10 +    it under the terms of the GNU Lesser General Public License as published
    1.11 +    by the Free Software Foundation; either version 2.1 of License, or
    1.12 +    (at your option) any later version.
    1.13 +
    1.14 +    This program is distributed in the hope that it will be useful,
    1.15 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.17 +    Lesser General Public License for more details.
    1.18 +
    1.19 +    You should also have received a copy of the GNU Lesser General Public
    1.20 +    License along with this library in the file named "LICENSE".
    1.21 +    If not, write to the Free Software Foundation, 51 Franklin Street, 
    1.22 +    Suite 500, Boston, MA 02110-1335, USA or visit their web page on the 
    1.23 +    internet at http://www.fsf.org/licenses/lgpl.html.
    1.24 +
    1.25 +Alternatively, the contents of this file may be used under the terms of the
    1.26 +Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
    1.27 +License, as published by the Free Software Foundation, either version 2
    1.28 +of the License or (at your option) any later version.
    1.29 +*/
    1.30 +#include "graphite2/Segment.h"
    1.31 +#include "inc/UtfCodec.h"
    1.32 +#include "inc/Segment.h"
    1.33 +
    1.34 +using namespace graphite2;
    1.35 +
    1.36 +namespace 
    1.37 +{
    1.38 +
    1.39 +  gr_segment* makeAndInitialize(const Font *font, const Face *face, uint32 script, const Features* pFeats/*must not be NULL*/, gr_encform enc, const void* pStart, size_t nChars, int dir)
    1.40 +  {
    1.41 +      if (script == 0x20202020) script = 0;
    1.42 +      else if ((script & 0x00FFFFFF) == 0x00202020) script = script & 0xFF000000;
    1.43 +      else if ((script & 0x0000FFFF) == 0x00002020) script = script & 0xFFFF0000;
    1.44 +      else if ((script & 0x000000FF) == 0x00000020) script = script & 0xFFFFFF00;
    1.45 +      // if (!font) return NULL;
    1.46 +      Segment* pRes=new Segment(nChars, face, script, dir);
    1.47 +
    1.48 +      
    1.49 +      if (!pRes->read_text(face, pFeats, enc, pStart, nChars) || !pRes->runGraphite())
    1.50 +      {
    1.51 +        delete pRes;
    1.52 +        return NULL;
    1.53 +      }
    1.54 +      // run the line break passes
    1.55 +      // run the substitution passes
    1.56 +      pRes->prepare_pos(font);
    1.57 +      // run the positioning passes
    1.58 +      pRes->finalise(font);
    1.59 +
    1.60 +      return static_cast<gr_segment*>(pRes);
    1.61 +  }
    1.62 +
    1.63 +
    1.64 +}
    1.65 +
    1.66 +
    1.67 +template <typename utf_iter>
    1.68 +inline size_t count_unicode_chars(utf_iter first, const utf_iter last, const void **error)
    1.69 +{
    1.70 +    size_t n_chars = 0;
    1.71 +    uint32 usv = 0;
    1.72 +
    1.73 +    if (last)
    1.74 +    {
    1.75 +        for (;first != last; ++first, ++n_chars)
    1.76 +            if ((usv = *first) == 0 || first.error()) break;
    1.77 +    }
    1.78 +    else
    1.79 +    {
    1.80 +        while ((usv = *first) != 0 && !first.error())
    1.81 +        {
    1.82 +            ++first;
    1.83 +            ++n_chars;
    1.84 +        }
    1.85 +    }
    1.86 +
    1.87 +    if (error)  *error = first.error() ? first : 0;
    1.88 +    return n_chars;
    1.89 +}
    1.90 +
    1.91 +extern "C" {
    1.92 +
    1.93 +size_t gr_count_unicode_characters(gr_encform enc, const void* buffer_begin, const void* buffer_end/*don't go on or past end, If NULL then ignored*/, const void** pError)   //Also stops on nul. Any nul is not in the count
    1.94 +{
    1.95 +    assert(buffer_begin);
    1.96 +
    1.97 +    switch (enc)
    1.98 +    {
    1.99 +    case gr_utf8:   return count_unicode_chars<utf8::const_iterator>(buffer_begin, buffer_end, pError); break;
   1.100 +    case gr_utf16:  return count_unicode_chars<utf16::const_iterator>(buffer_begin, buffer_end, pError); break;
   1.101 +    case gr_utf32:  return count_unicode_chars<utf32::const_iterator>(buffer_begin, buffer_end, pError); break;
   1.102 +    default:        return 0;
   1.103 +    }
   1.104 +}
   1.105 +
   1.106 +
   1.107 +gr_segment* gr_make_seg(const gr_font *font, const gr_face *face, gr_uint32 script, const gr_feature_val* pFeats, gr_encform enc, const void* pStart, size_t nChars, int dir)
   1.108 +{
   1.109 +    const gr_feature_val * tmp_feats = 0;
   1.110 +    if (pFeats == 0)
   1.111 +        pFeats = tmp_feats = static_cast<const gr_feature_val*>(face->theSill().cloneFeatures(0));
   1.112 +    gr_segment * seg = makeAndInitialize(font, face, script, pFeats, enc, pStart, nChars, dir);
   1.113 +    delete tmp_feats;
   1.114 +
   1.115 +    return seg;
   1.116 +}
   1.117 +
   1.118 +
   1.119 +void gr_seg_destroy(gr_segment* p)
   1.120 +{
   1.121 +    delete p;
   1.122 +}
   1.123 +
   1.124 +
   1.125 +float gr_seg_advance_X(const gr_segment* pSeg/*not NULL*/)
   1.126 +{
   1.127 +    assert(pSeg);
   1.128 +    return pSeg->advance().x;
   1.129 +}
   1.130 +
   1.131 +
   1.132 +float gr_seg_advance_Y(const gr_segment* pSeg/*not NULL*/)
   1.133 +{
   1.134 +    assert(pSeg);
   1.135 +    return pSeg->advance().y;
   1.136 +}
   1.137 +
   1.138 +
   1.139 +unsigned int gr_seg_n_cinfo(const gr_segment* pSeg/*not NULL*/)
   1.140 +{
   1.141 +    assert(pSeg);
   1.142 +    return pSeg->charInfoCount();
   1.143 +}
   1.144 +
   1.145 +
   1.146 +const gr_char_info* gr_seg_cinfo(const gr_segment* pSeg/*not NULL*/, unsigned int index/*must be <number_of_CharInfo*/)
   1.147 +{
   1.148 +    assert(pSeg);
   1.149 +    return static_cast<const gr_char_info*>(pSeg->charinfo(index));
   1.150 +}
   1.151 +
   1.152 +unsigned int gr_seg_n_slots(const gr_segment* pSeg/*not NULL*/)
   1.153 +{
   1.154 +    assert(pSeg);
   1.155 +    return pSeg->slotCount();
   1.156 +}
   1.157 +
   1.158 +const gr_slot* gr_seg_first_slot(gr_segment* pSeg/*not NULL*/)
   1.159 +{
   1.160 +    assert(pSeg);
   1.161 +    return static_cast<const gr_slot*>(pSeg->first());
   1.162 +}
   1.163 +
   1.164 +const gr_slot* gr_seg_last_slot(gr_segment* pSeg/*not NULL*/)
   1.165 +{
   1.166 +    assert(pSeg);
   1.167 +    return static_cast<const gr_slot*>(pSeg->last());
   1.168 +}
   1.169 +
   1.170 +float gr_seg_justify(gr_segment* pSeg/*not NULL*/, const gr_slot* pSlot/*not NULL*/, const gr_font *pFont, double width, enum gr_justFlags flags, const gr_slot *pFirst, const gr_slot *pLast)
   1.171 +{
   1.172 +    assert(pSeg);
   1.173 +    assert(pSlot);
   1.174 +    return pSeg->justify(const_cast<gr_slot *>(pSlot), pFont, float(width), justFlags(flags), const_cast<gr_slot *>(pFirst), const_cast<gr_slot *>(pLast));
   1.175 +}
   1.176 +
   1.177 +} // extern "C"

mercurial