michael@0: /* GRAPHITE2 LICENSING michael@0: michael@0: Copyright 2010, SIL International michael@0: All rights reserved. michael@0: michael@0: This library is free software; you can redistribute it and/or modify michael@0: it under the terms of the GNU Lesser General Public License as published michael@0: by the Free Software Foundation; either version 2.1 of License, or michael@0: (at your option) any later version. michael@0: michael@0: This program is distributed in the hope that it will be useful, michael@0: but WITHOUT ANY WARRANTY; without even the implied warranty of michael@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU michael@0: Lesser General Public License for more details. michael@0: michael@0: You should also have received a copy of the GNU Lesser General Public michael@0: License along with this library in the file named "LICENSE". michael@0: If not, write to the Free Software Foundation, 51 Franklin Street, michael@0: Suite 500, Boston, MA 02110-1335, USA or visit their web page on the michael@0: internet at http://www.fsf.org/licenses/lgpl.html. michael@0: michael@0: Alternatively, the contents of this file may be used under the terms of the michael@0: Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public michael@0: License, as published by the Free Software Foundation, either version 2 michael@0: of the License or (at your option) any later version. michael@0: */ michael@0: #include "graphite2/Segment.h" michael@0: #include "inc/UtfCodec.h" michael@0: #include "inc/Segment.h" michael@0: michael@0: using namespace graphite2; michael@0: michael@0: namespace michael@0: { michael@0: michael@0: 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) michael@0: { michael@0: if (script == 0x20202020) script = 0; michael@0: else if ((script & 0x00FFFFFF) == 0x00202020) script = script & 0xFF000000; michael@0: else if ((script & 0x0000FFFF) == 0x00002020) script = script & 0xFFFF0000; michael@0: else if ((script & 0x000000FF) == 0x00000020) script = script & 0xFFFFFF00; michael@0: // if (!font) return NULL; michael@0: Segment* pRes=new Segment(nChars, face, script, dir); michael@0: michael@0: michael@0: if (!pRes->read_text(face, pFeats, enc, pStart, nChars) || !pRes->runGraphite()) michael@0: { michael@0: delete pRes; michael@0: return NULL; michael@0: } michael@0: // run the line break passes michael@0: // run the substitution passes michael@0: pRes->prepare_pos(font); michael@0: // run the positioning passes michael@0: pRes->finalise(font); michael@0: michael@0: return static_cast(pRes); michael@0: } michael@0: michael@0: michael@0: } michael@0: michael@0: michael@0: template michael@0: inline size_t count_unicode_chars(utf_iter first, const utf_iter last, const void **error) michael@0: { michael@0: size_t n_chars = 0; michael@0: uint32 usv = 0; michael@0: michael@0: if (last) michael@0: { michael@0: for (;first != last; ++first, ++n_chars) michael@0: if ((usv = *first) == 0 || first.error()) break; michael@0: } michael@0: else michael@0: { michael@0: while ((usv = *first) != 0 && !first.error()) michael@0: { michael@0: ++first; michael@0: ++n_chars; michael@0: } michael@0: } michael@0: michael@0: if (error) *error = first.error() ? first : 0; michael@0: return n_chars; michael@0: } michael@0: michael@0: extern "C" { michael@0: michael@0: 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 michael@0: { michael@0: assert(buffer_begin); michael@0: michael@0: switch (enc) michael@0: { michael@0: case gr_utf8: return count_unicode_chars(buffer_begin, buffer_end, pError); break; michael@0: case gr_utf16: return count_unicode_chars(buffer_begin, buffer_end, pError); break; michael@0: case gr_utf32: return count_unicode_chars(buffer_begin, buffer_end, pError); break; michael@0: default: return 0; michael@0: } michael@0: } michael@0: michael@0: michael@0: 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) michael@0: { michael@0: const gr_feature_val * tmp_feats = 0; michael@0: if (pFeats == 0) michael@0: pFeats = tmp_feats = static_cast(face->theSill().cloneFeatures(0)); michael@0: gr_segment * seg = makeAndInitialize(font, face, script, pFeats, enc, pStart, nChars, dir); michael@0: delete tmp_feats; michael@0: michael@0: return seg; michael@0: } michael@0: michael@0: michael@0: void gr_seg_destroy(gr_segment* p) michael@0: { michael@0: delete p; michael@0: } michael@0: michael@0: michael@0: float gr_seg_advance_X(const gr_segment* pSeg/*not NULL*/) michael@0: { michael@0: assert(pSeg); michael@0: return pSeg->advance().x; michael@0: } michael@0: michael@0: michael@0: float gr_seg_advance_Y(const gr_segment* pSeg/*not NULL*/) michael@0: { michael@0: assert(pSeg); michael@0: return pSeg->advance().y; michael@0: } michael@0: michael@0: michael@0: unsigned int gr_seg_n_cinfo(const gr_segment* pSeg/*not NULL*/) michael@0: { michael@0: assert(pSeg); michael@0: return pSeg->charInfoCount(); michael@0: } michael@0: michael@0: michael@0: const gr_char_info* gr_seg_cinfo(const gr_segment* pSeg/*not NULL*/, unsigned int index/*must be (pSeg->charinfo(index)); michael@0: } michael@0: michael@0: unsigned int gr_seg_n_slots(const gr_segment* pSeg/*not NULL*/) michael@0: { michael@0: assert(pSeg); michael@0: return pSeg->slotCount(); michael@0: } michael@0: michael@0: const gr_slot* gr_seg_first_slot(gr_segment* pSeg/*not NULL*/) michael@0: { michael@0: assert(pSeg); michael@0: return static_cast(pSeg->first()); michael@0: } michael@0: michael@0: const gr_slot* gr_seg_last_slot(gr_segment* pSeg/*not NULL*/) michael@0: { michael@0: assert(pSeg); michael@0: return static_cast(pSeg->last()); michael@0: } michael@0: michael@0: 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) michael@0: { michael@0: assert(pSeg); michael@0: assert(pSlot); michael@0: return pSeg->justify(const_cast(pSlot), pFont, float(width), justFlags(flags), const_cast(pFirst), const_cast(pLast)); michael@0: } michael@0: michael@0: } // extern "C"