Path: blob/master/thirdparty/graphite/src/gr_segment.cpp
9903 views
// SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later1// Copyright 2010, SIL International, All rights reserved.23#include "graphite2/Segment.h"4#include "inc/UtfCodec.h"5#include "inc/Segment.h"67using namespace graphite2;89namespace10{1112gr_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)13{14if (script == 0x20202020) script = 0;15else if ((script & 0x00FFFFFF) == 0x00202020) script = script & 0xFF000000;16else if ((script & 0x0000FFFF) == 0x00002020) script = script & 0xFFFF0000;17else if ((script & 0x000000FF) == 0x00000020) script = script & 0xFFFFFF00;18// if (!font) return NULL;19Segment* pRes=new Segment(nChars, face, script, dir);202122if (!pRes->read_text(face, pFeats, enc, pStart, nChars) || !pRes->runGraphite())23{24delete pRes;25return NULL;26}27pRes->finalise(font, true);2829return static_cast<gr_segment*>(pRes);30}3132template <typename utf_iter>33inline size_t count_unicode_chars(utf_iter first, const utf_iter last, const void **error)34{35size_t n_chars = 0;36uint32 usv = 0;3738if (last)39{40if (!first.validate(last))41{42if (error) *error = last - 1;43return 0;44}45for (;first != last; ++first, ++n_chars)46if ((usv = *first) == 0 || first.error()) break;47}48else49{50while ((usv = *first) != 0 && !first.error())51{52++first;53++n_chars;54}55}5657if (error) *error = first.error() ? first : 0;58return n_chars;59}60}616263extern "C" {6465size_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 count66{67assert(buffer_begin);6869switch (enc)70{71case gr_utf8: return count_unicode_chars<utf8::const_iterator>(buffer_begin, buffer_end, pError); break;72case gr_utf16: return count_unicode_chars<utf16::const_iterator>(buffer_begin, buffer_end, pError); break;73case gr_utf32: return count_unicode_chars<utf32::const_iterator>(buffer_begin, buffer_end, pError); break;74default: return 0;75}76}777879gr_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)80{81if (!face) return nullptr;8283const gr_feature_val * tmp_feats = 0;84if (pFeats == 0)85pFeats = tmp_feats = static_cast<const gr_feature_val*>(face->theSill().cloneFeatures(0));86gr_segment * seg = makeAndInitialize(font, face, script, pFeats, enc, pStart, nChars, dir);87delete static_cast<const FeatureVal*>(tmp_feats);8889return seg;90}919293void gr_seg_destroy(gr_segment* p)94{95delete static_cast<Segment*>(p);96}979899float gr_seg_advance_X(const gr_segment* pSeg/*not NULL*/)100{101assert(pSeg);102return pSeg->advance().x;103}104105106float gr_seg_advance_Y(const gr_segment* pSeg/*not NULL*/)107{108assert(pSeg);109return pSeg->advance().y;110}111112113unsigned int gr_seg_n_cinfo(const gr_segment* pSeg/*not NULL*/)114{115assert(pSeg);116return static_cast<unsigned int>(pSeg->charInfoCount());117}118119120const gr_char_info* gr_seg_cinfo(const gr_segment* pSeg/*not NULL*/, unsigned int index/*must be <number_of_CharInfo*/)121{122assert(pSeg);123return static_cast<const gr_char_info*>(pSeg->charinfo(index));124}125126unsigned int gr_seg_n_slots(const gr_segment* pSeg/*not NULL*/)127{128assert(pSeg);129return static_cast<unsigned int>(pSeg->slotCount());130}131132const gr_slot* gr_seg_first_slot(gr_segment* pSeg/*not NULL*/)133{134assert(pSeg);135return static_cast<const gr_slot*>(pSeg->first());136}137138const gr_slot* gr_seg_last_slot(gr_segment* pSeg/*not NULL*/)139{140assert(pSeg);141return static_cast<const gr_slot*>(pSeg->last());142}143144float 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)145{146assert(pSeg);147assert(pSlot);148return pSeg->justify(const_cast<gr_slot *>(pSlot), pFont, float(width), justFlags(flags), const_cast<gr_slot *>(pFirst), const_cast<gr_slot *>(pLast));149}150151} // extern "C"152153154