Path: blob/master/thirdparty/graphite/src/inc/Segment.h
9913 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#pragma once45#include "inc/Main.h"67#include <cassert>89#include "inc/CharInfo.h"10#include "inc/Face.h"11#include "inc/FeatureVal.h"12#include "inc/GlyphCache.h"13#include "inc/GlyphFace.h"14#include "inc/Slot.h"15#include "inc/Position.h"16#include "inc/List.h"17#include "inc/Collider.h"1819#define MAX_SEG_GROWTH_FACTOR 642021namespace graphite2 {2223typedef Vector<Features> FeatureList;24typedef Vector<Slot *> SlotRope;25typedef Vector<int16 *> AttributeRope;26typedef Vector<SlotJustify *> JustifyRope;2728class Font;29class Segment;30class Silf;3132enum SpliceParam {33/** sub-Segments longer than this are not cached34* (in Unicode code points) */35eMaxSpliceSize = 9636};3738enum justFlags {39gr_justStartInline = 1,40gr_justEndInline = 241};4243class SegmentScopeState44{45private:46friend class Segment;47Slot * realFirstSlot;48Slot * slotBeforeScope;49Slot * slotAfterScope;50Slot * realLastSlot;51size_t numGlyphsOutsideScope;52};5354class Segment55{56// Prevent copying of any kind.57Segment(const Segment&);58Segment& operator=(const Segment&);5960public:6162enum {63SEG_INITCOLLISIONS = 1,64SEG_HASCOLLISIONS = 265};6667size_t slotCount() const { return m_numGlyphs; } //one slot per glyph68void extendLength(ptrdiff_t num) { m_numGlyphs += num; }69Position advance() const { return m_advance; }70bool runGraphite() { if (m_silf) return m_face->runGraphite(this, m_silf); else return true;};71void chooseSilf(uint32 script) { m_silf = m_face->chooseSilf(script); }72const Silf *silf() const { return m_silf; }73size_t charInfoCount() const { return m_numCharinfo; }74const CharInfo *charinfo(unsigned int index) const { return index < m_numCharinfo ? m_charinfo + index : NULL; }75CharInfo *charinfo(unsigned int index) { return index < m_numCharinfo ? m_charinfo + index : NULL; }7677Segment(size_t numchars, const Face* face, uint32 script, int dir);78~Segment();79uint8 flags() const { return m_flags; }80void flags(uint8 f) { m_flags = f; }81Slot *first() { return m_first; }82void first(Slot *p) { m_first = p; }83Slot *last() { return m_last; }84void last(Slot *p) { m_last = p; }85void appendSlot(int i, int cid, int gid, int fid, size_t coffset);86Slot *newSlot();87void freeSlot(Slot *);88SlotJustify *newJustify();89void freeJustify(SlotJustify *aJustify);90Position positionSlots(const Font *font=0, Slot *first=0, Slot *last=0, bool isRtl = false, bool isFinal = true);91void associateChars(int offset, size_t num);92void linkClusters(Slot *first, Slot *last);93uint16 getClassGlyph(uint16 cid, uint16 offset) const { return m_silf->getClassGlyph(cid, offset); }94uint16 findClassIndex(uint16 cid, uint16 gid) const { return m_silf->findClassIndex(cid, gid); }95int addFeatures(const Features& feats) { m_feats.push_back(feats); return int(m_feats.size()) - 1; }96uint32 getFeature(int index, uint8 findex) const { const FeatureRef* pFR=m_face->theSill().theFeatureMap().featureRef(findex); if (!pFR) return 0; else return pFR->getFeatureVal(m_feats[index]); }97void setFeature(int index, uint8 findex, uint32 val) {98const FeatureRef* pFR=m_face->theSill().theFeatureMap().featureRef(findex);99if (pFR)100{101if (val > pFR->maxVal()) val = pFR->maxVal();102pFR->applyValToFeature(val, m_feats[index]);103} }104int8 dir() const { return m_dir; }105void dir(int8 val) { m_dir = val; }106bool currdir() const { return ((m_dir >> 6) ^ m_dir) & 1; }107uint8 passBits() const { return m_passBits; }108void mergePassBits(const uint8 val) { m_passBits &= val; }109int16 glyphAttr(uint16 gid, uint16 gattr) const { const GlyphFace * p = m_face->glyphs().glyphSafe(gid); return p ? p->attrs()[gattr] : 0; }110int32 getGlyphMetric(Slot *iSlot, uint8 metric, uint8 attrLevel, bool rtl) const;111float glyphAdvance(uint16 gid) const { return m_face->glyphs().glyph(gid)->theAdvance().x; }112const Rect &theGlyphBBoxTemporary(uint16 gid) const { return m_face->glyphs().glyph(gid)->theBBox(); } //warning value may become invalid when another glyph is accessed113Slot *findRoot(Slot *is) const { return is->attachedTo() ? findRoot(is->attachedTo()) : is; }114int numAttrs() const { return m_silf->numUser(); }115int defaultOriginal() const { return m_defaultOriginal; }116const Face * getFace() const { return m_face; }117const Features & getFeatures(unsigned int /*charIndex*/) { assert(m_feats.size() == 1); return m_feats[0]; }118void bidiPass(int paradir, uint8 aMirror);119int8 getSlotBidiClass(Slot *s) const;120void doMirror(uint16 aMirror);121Slot *addLineEnd(Slot *nSlot);122void delLineEnd(Slot *s);123bool hasJustification() const { return m_justifies.size() != 0; }124void reverseSlots();125126bool isWhitespace(const int cid) const;127bool hasCollisionInfo() const { return (m_flags & SEG_HASCOLLISIONS) && m_collisions; }128SlotCollision *collisionInfo(const Slot *s) const { return m_collisions ? m_collisions + s->index() : 0; }129CLASS_NEW_DELETE130131public: //only used by: GrSegment* makeAndInitialize(const GrFont *font, const GrFace *face, uint32 script, const FeaturesHandle& pFeats/*must not be IsNull*/, encform enc, const void* pStart, size_t nChars, int dir);132bool read_text(const Face *face, const Features* pFeats/*must not be NULL*/, gr_encform enc, const void*pStart, size_t nChars);133void finalise(const Font *font, bool reverse=false);134float justify(Slot *pSlot, const Font *font, float width, enum justFlags flags, Slot *pFirst, Slot *pLast);135bool initCollisions();136137private:138Position m_advance; // whole segment advance139SlotRope m_slots; // Vector of slot buffers140AttributeRope m_userAttrs; // Vector of userAttrs buffers141JustifyRope m_justifies; // Slot justification info buffers142FeatureList m_feats; // feature settings referenced by charinfos in this segment143Slot * m_freeSlots; // linked list of free slots144SlotJustify * m_freeJustifies; // Slot justification blocks free list145CharInfo * m_charinfo; // character info, one per input character146SlotCollision * m_collisions;147const Face * m_face; // GrFace148const Silf * m_silf;149Slot * m_first; // first slot in segment150Slot * m_last; // last slot in segment151size_t m_bufSize, // how big a buffer to create when need more slots152m_numGlyphs,153m_numCharinfo; // size of the array and number of input characters154int m_defaultOriginal; // number of whitespace chars in the string155int8 m_dir;156uint8 m_flags, // General purpose flags157m_passBits; // if bit set then skip pass158};159160inline161int8 Segment::getSlotBidiClass(Slot *s) const162{163int8 res = s->getBidiClass();164if (res != -1) return res;165res = int8(glyphAttr(s->gid(), m_silf->aBidi()));166s->setBidiClass(res);167return res;168}169170inline171void Segment::finalise(const Font *font, bool reverse)172{173if (!m_first || !m_last) return;174175m_advance = positionSlots(font, m_first, m_last, m_silf->dir(), true);176//associateChars(0, m_numCharinfo);177if (reverse && currdir() != (m_dir & 1))178reverseSlots();179linkClusters(m_first, m_last);180}181182inline183int32 Segment::getGlyphMetric(Slot *iSlot, uint8 metric, uint8 attrLevel, bool rtl) const {184if (attrLevel > 0)185{186Slot *is = findRoot(iSlot);187return is->clusterMetric(this, metric, attrLevel, rtl);188}189else190return m_face->getGlyphMetric(iSlot->gid(), metric);191}192193inline194bool Segment::isWhitespace(const int cid) const195{196return ((cid >= 0x0009) * (cid <= 0x000D)197+ (cid == 0x0020)198+ (cid == 0x0085)199+ (cid == 0x00A0)200+ (cid == 0x1680)201+ (cid == 0x180E)202+ (cid >= 0x2000) * (cid <= 0x200A)203+ (cid == 0x2028)204+ (cid == 0x2029)205+ (cid == 0x202F)206+ (cid == 0x205F)207+ (cid == 0x3000)) != 0;208}209210} // namespace graphite2211212struct gr_segment : public graphite2::Segment {};213214215