gfx/graphite2/src/gr_segment.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /*  GRAPHITE2 LICENSING
     3     Copyright 2010, SIL International
     4     All rights reserved.
     6     This library is free software; you can redistribute it and/or modify
     7     it under the terms of the GNU Lesser General Public License as published
     8     by the Free Software Foundation; either version 2.1 of License, or
     9     (at your option) any later version.
    11     This program is distributed in the hope that it will be useful,
    12     but WITHOUT ANY WARRANTY; without even the implied warranty of
    13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    14     Lesser General Public License for more details.
    16     You should also have received a copy of the GNU Lesser General Public
    17     License along with this library in the file named "LICENSE".
    18     If not, write to the Free Software Foundation, 51 Franklin Street, 
    19     Suite 500, Boston, MA 02110-1335, USA or visit their web page on the 
    20     internet at http://www.fsf.org/licenses/lgpl.html.
    22 Alternatively, the contents of this file may be used under the terms of the
    23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
    24 License, as published by the Free Software Foundation, either version 2
    25 of the License or (at your option) any later version.
    26 */
    27 #include "graphite2/Segment.h"
    28 #include "inc/UtfCodec.h"
    29 #include "inc/Segment.h"
    31 using namespace graphite2;
    33 namespace 
    34 {
    36   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)
    37   {
    38       if (script == 0x20202020) script = 0;
    39       else if ((script & 0x00FFFFFF) == 0x00202020) script = script & 0xFF000000;
    40       else if ((script & 0x0000FFFF) == 0x00002020) script = script & 0xFFFF0000;
    41       else if ((script & 0x000000FF) == 0x00000020) script = script & 0xFFFFFF00;
    42       // if (!font) return NULL;
    43       Segment* pRes=new Segment(nChars, face, script, dir);
    46       if (!pRes->read_text(face, pFeats, enc, pStart, nChars) || !pRes->runGraphite())
    47       {
    48         delete pRes;
    49         return NULL;
    50       }
    51       // run the line break passes
    52       // run the substitution passes
    53       pRes->prepare_pos(font);
    54       // run the positioning passes
    55       pRes->finalise(font);
    57       return static_cast<gr_segment*>(pRes);
    58   }
    61 }
    64 template <typename utf_iter>
    65 inline size_t count_unicode_chars(utf_iter first, const utf_iter last, const void **error)
    66 {
    67     size_t n_chars = 0;
    68     uint32 usv = 0;
    70     if (last)
    71     {
    72         for (;first != last; ++first, ++n_chars)
    73             if ((usv = *first) == 0 || first.error()) break;
    74     }
    75     else
    76     {
    77         while ((usv = *first) != 0 && !first.error())
    78         {
    79             ++first;
    80             ++n_chars;
    81         }
    82     }
    84     if (error)  *error = first.error() ? first : 0;
    85     return n_chars;
    86 }
    88 extern "C" {
    90 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
    91 {
    92     assert(buffer_begin);
    94     switch (enc)
    95     {
    96     case gr_utf8:   return count_unicode_chars<utf8::const_iterator>(buffer_begin, buffer_end, pError); break;
    97     case gr_utf16:  return count_unicode_chars<utf16::const_iterator>(buffer_begin, buffer_end, pError); break;
    98     case gr_utf32:  return count_unicode_chars<utf32::const_iterator>(buffer_begin, buffer_end, pError); break;
    99     default:        return 0;
   100     }
   101 }
   104 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)
   105 {
   106     const gr_feature_val * tmp_feats = 0;
   107     if (pFeats == 0)
   108         pFeats = tmp_feats = static_cast<const gr_feature_val*>(face->theSill().cloneFeatures(0));
   109     gr_segment * seg = makeAndInitialize(font, face, script, pFeats, enc, pStart, nChars, dir);
   110     delete tmp_feats;
   112     return seg;
   113 }
   116 void gr_seg_destroy(gr_segment* p)
   117 {
   118     delete p;
   119 }
   122 float gr_seg_advance_X(const gr_segment* pSeg/*not NULL*/)
   123 {
   124     assert(pSeg);
   125     return pSeg->advance().x;
   126 }
   129 float gr_seg_advance_Y(const gr_segment* pSeg/*not NULL*/)
   130 {
   131     assert(pSeg);
   132     return pSeg->advance().y;
   133 }
   136 unsigned int gr_seg_n_cinfo(const gr_segment* pSeg/*not NULL*/)
   137 {
   138     assert(pSeg);
   139     return pSeg->charInfoCount();
   140 }
   143 const gr_char_info* gr_seg_cinfo(const gr_segment* pSeg/*not NULL*/, unsigned int index/*must be <number_of_CharInfo*/)
   144 {
   145     assert(pSeg);
   146     return static_cast<const gr_char_info*>(pSeg->charinfo(index));
   147 }
   149 unsigned int gr_seg_n_slots(const gr_segment* pSeg/*not NULL*/)
   150 {
   151     assert(pSeg);
   152     return pSeg->slotCount();
   153 }
   155 const gr_slot* gr_seg_first_slot(gr_segment* pSeg/*not NULL*/)
   156 {
   157     assert(pSeg);
   158     return static_cast<const gr_slot*>(pSeg->first());
   159 }
   161 const gr_slot* gr_seg_last_slot(gr_segment* pSeg/*not NULL*/)
   162 {
   163     assert(pSeg);
   164     return static_cast<const gr_slot*>(pSeg->last());
   165 }
   167 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)
   168 {
   169     assert(pSeg);
   170     assert(pSlot);
   171     return pSeg->justify(const_cast<gr_slot *>(pSlot), pFont, float(width), justFlags(flags), const_cast<gr_slot *>(pFirst), const_cast<gr_slot *>(pLast));
   172 }
   174 } // extern "C"

mercurial