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 "inc/Segment.h" michael@0: #include "inc/Slot.h" michael@0: #include "inc/Silf.h" michael@0: #include "inc/CharInfo.h" michael@0: #include "inc/Rule.h" michael@0: michael@0: michael@0: using namespace graphite2; michael@0: michael@0: Slot::Slot() : michael@0: m_next(NULL), m_prev(NULL), michael@0: m_glyphid(0), m_realglyphid(0), m_original(0), m_before(0), m_after(0), michael@0: m_index(0), m_parent(NULL), m_child(NULL), m_sibling(NULL), michael@0: m_position(0, 0), m_shift(0, 0), m_advance(0, 0), michael@0: m_attach(0, 0), m_with(0, 0), m_just(0.), michael@0: m_flags(0), m_attLevel(0), m_bidiCls(-1), m_bidiLevel(0), m_justs(NULL) michael@0: // Do not set m_userAttr since it is set *before* new is called since this michael@0: // is used as a positional new to reset the GrSlot michael@0: { michael@0: } michael@0: michael@0: // take care, this does not copy any of the GrSlot pointer fields michael@0: void Slot::set(const Slot & orig, int charOffset, size_t numUserAttr, size_t justLevels, size_t numChars) michael@0: { michael@0: // leave m_next and m_prev unchanged michael@0: m_glyphid = orig.m_glyphid; michael@0: m_realglyphid = orig.m_realglyphid; michael@0: m_original = orig.m_original + charOffset; michael@0: if (charOffset + int(orig.m_before) < 0) michael@0: m_before = 0; michael@0: else michael@0: m_before = orig.m_before + charOffset; michael@0: if (charOffset <= 0 && orig.m_after + charOffset >= numChars) michael@0: m_after = numChars - 1; michael@0: else michael@0: m_after = orig.m_after + charOffset; michael@0: m_parent = NULL; michael@0: m_child = NULL; michael@0: m_sibling = NULL; michael@0: m_position = orig.m_position; michael@0: m_shift = orig.m_shift; michael@0: m_advance = orig.m_advance; michael@0: m_attach = orig.m_attach; michael@0: m_with = orig.m_with; michael@0: m_flags = orig.m_flags; michael@0: m_attLevel = orig.m_attLevel; michael@0: m_bidiCls = orig.m_bidiCls; michael@0: m_bidiLevel = orig.m_bidiLevel; michael@0: if (m_userAttr && orig.m_userAttr) michael@0: memcpy(m_userAttr, orig.m_userAttr, numUserAttr * sizeof(*m_userAttr)); michael@0: if (m_justs && orig.m_justs) michael@0: memcpy(m_justs, orig.m_justs, SlotJustify::size_of(justLevels)); michael@0: } michael@0: michael@0: void Slot::update(int /*numGrSlots*/, int numCharInfo, Position &relpos) michael@0: { michael@0: m_before += numCharInfo; michael@0: m_after += numCharInfo; michael@0: m_position = m_position + relpos; michael@0: } michael@0: michael@0: Position Slot::finalise(const Segment *seg, const Font *font, Position & base, Rect & bbox, uint8 attrLevel, float & clusterMin) michael@0: { michael@0: if (attrLevel && m_attLevel > attrLevel) return Position(0, 0); michael@0: float scale = 1.0; michael@0: Position shift(m_shift.x * ((seg->dir() & 1) * -2 + 1) + m_just, m_shift.y); michael@0: float tAdvance = m_advance.x + m_just; michael@0: const GlyphFace * glyphFace = seg->getFace()->glyphs().glyphSafe(glyph()); michael@0: if (font) michael@0: { michael@0: scale = font->scale(); michael@0: shift *= scale; michael@0: if (font->isHinted() && glyphFace) michael@0: tAdvance = (m_advance.x - glyphFace->theAdvance().x + m_just) * scale + font->advance(m_glyphid); michael@0: else michael@0: tAdvance *= scale; michael@0: } michael@0: Position res; michael@0: michael@0: m_position = base + shift; michael@0: if (!m_parent) michael@0: { michael@0: res = base + Position(tAdvance, m_advance.y * scale); michael@0: clusterMin = base.x; michael@0: } michael@0: else michael@0: { michael@0: float tAdv; michael@0: m_position += (m_attach - m_with) * scale; michael@0: tAdv = m_advance.x >= 0.5 ? m_position.x + tAdvance - shift.x : 0.f; michael@0: res = Position(tAdv, 0); michael@0: if ((m_advance.x >= 0.5 || m_position.x < 0) && m_position.x < clusterMin) clusterMin = m_position.x; michael@0: } michael@0: michael@0: if (glyphFace) michael@0: { michael@0: Rect ourBbox = glyphFace->theBBox() * scale + m_position; michael@0: bbox = bbox.widen(ourBbox); michael@0: } michael@0: michael@0: if (m_child && m_child != this && m_child->attachedTo() == this) michael@0: { michael@0: Position tRes = m_child->finalise(seg, font, m_position, bbox, attrLevel, clusterMin); michael@0: if ((!m_parent || m_advance.x >= 0.5) && tRes.x > res.x) res = tRes; michael@0: } michael@0: michael@0: if (m_parent && m_sibling && m_sibling != this && m_sibling->attachedTo() == m_parent) michael@0: { michael@0: Position tRes = m_sibling->finalise(seg, font, base, bbox, attrLevel, clusterMin); michael@0: if (tRes.x > res.x) res = tRes; michael@0: } michael@0: michael@0: if (!m_parent && clusterMin < base.x) michael@0: { michael@0: Position adj = Position(base.x - clusterMin, 0.); michael@0: res += adj; michael@0: m_position += adj; michael@0: if (m_child) m_child->floodShift(adj); michael@0: } michael@0: return res; michael@0: } michael@0: michael@0: int32 Slot::clusterMetric(const Segment *seg, uint8 metric, uint8 attrLevel) michael@0: { michael@0: Position base; michael@0: Rect bbox = seg->theGlyphBBoxTemporary(glyph()); michael@0: float clusterMin = 0.; michael@0: Position res = finalise(seg, NULL, base, bbox, attrLevel, clusterMin); michael@0: michael@0: switch (metrics(metric)) michael@0: { michael@0: case kgmetLsb : michael@0: return static_cast(bbox.bl.x); michael@0: case kgmetRsb : michael@0: return static_cast(res.x - bbox.tr.x); michael@0: case kgmetBbTop : michael@0: return static_cast(bbox.tr.y); michael@0: case kgmetBbBottom : michael@0: return static_cast(bbox.bl.y); michael@0: case kgmetBbLeft : michael@0: return static_cast(bbox.bl.x); michael@0: case kgmetBbRight : michael@0: return static_cast(bbox.tr.x); michael@0: case kgmetBbWidth : michael@0: return static_cast(bbox.tr.x - bbox.bl.x); michael@0: case kgmetBbHeight : michael@0: return static_cast(bbox.tr.y - bbox.bl.y); michael@0: case kgmetAdvWidth : michael@0: return static_cast(res.x); michael@0: case kgmetAdvHeight : michael@0: return static_cast(res.y); michael@0: default : michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: int Slot::getAttr(const Segment *seg, attrCode ind, uint8 subindex) const michael@0: { michael@0: if (!this) return 0; michael@0: if (ind == gr_slatUserDefnV1) michael@0: { michael@0: ind = gr_slatUserDefn; michael@0: subindex = 0; michael@0: } michael@0: else if (ind >= gr_slatJStretch && ind < gr_slatJStretch + 20 && ind != gr_slatJWidth) michael@0: { michael@0: int indx = ind - gr_slatJStretch; michael@0: return getJustify(seg, indx / 5, indx % 5); michael@0: } michael@0: michael@0: switch (ind) michael@0: { michael@0: case gr_slatAdvX : return int(m_advance.x); michael@0: case gr_slatAdvY : return int(m_advance.y); michael@0: case gr_slatAttTo : return m_parent ? 1 : 0; michael@0: case gr_slatAttX : return int(m_attach.x); michael@0: case gr_slatAttY : return int(m_attach.y); michael@0: case gr_slatAttXOff : michael@0: case gr_slatAttYOff : return 0; michael@0: case gr_slatAttWithX : return int(m_with.x); michael@0: case gr_slatAttWithY : return int(m_with.y); michael@0: case gr_slatAttWithXOff: michael@0: case gr_slatAttWithYOff:return 0; michael@0: case gr_slatAttLevel : return m_attLevel; michael@0: case gr_slatBreak : return seg->charinfo(m_original)->breakWeight(); michael@0: case gr_slatCompRef : return 0; michael@0: case gr_slatDir : if (m_bidiCls == -1) michael@0: const_cast(this)->setBidiClass(seg->glyphAttr(gid(), seg->silf()->aBidi())); michael@0: return m_bidiCls; michael@0: case gr_slatInsert : return isInsertBefore(); michael@0: case gr_slatPosX : return int(m_position.x); // but need to calculate it michael@0: case gr_slatPosY : return int(m_position.y); michael@0: case gr_slatShiftX : return int(m_shift.x); michael@0: case gr_slatShiftY : return int(m_shift.y); michael@0: case gr_slatMeasureSol: return -1; // err what's this? michael@0: case gr_slatMeasureEol: return -1; michael@0: case gr_slatJWidth: return int(m_just); michael@0: case gr_slatUserDefn : return m_userAttr[subindex]; michael@0: case gr_slatSegSplit : return seg->charinfo(m_original)->flags() & 3; michael@0: case gr_slatBidiLevel: return m_bidiLevel; michael@0: default : return 0; michael@0: } michael@0: } michael@0: michael@0: void Slot::setAttr(Segment *seg, attrCode ind, uint8 subindex, int16 value, const SlotMap & map) michael@0: { michael@0: if (!this) return; michael@0: if (ind == gr_slatUserDefnV1) michael@0: { michael@0: ind = gr_slatUserDefn; michael@0: subindex = 0; michael@0: } michael@0: else if (ind >= gr_slatJStretch && ind < gr_slatJStretch + 20 && ind != gr_slatJWidth) michael@0: { michael@0: int indx = ind - gr_slatJStretch; michael@0: return setJustify(seg, indx / 5, indx % 5, value); michael@0: } michael@0: michael@0: switch (ind) michael@0: { michael@0: case gr_slatAdvX : m_advance.x = value; break; michael@0: case gr_slatAdvY : m_advance.y = value; break; michael@0: case gr_slatAttTo : michael@0: { michael@0: const uint16 idx = uint16(value); michael@0: if (idx < map.size() && map[idx]) michael@0: { michael@0: Slot *other = map[idx]; michael@0: if (other == this) break; michael@0: if (m_parent) m_parent->removeChild(this); michael@0: if (other->child(this)) michael@0: { michael@0: attachTo(other); michael@0: if (((seg->dir() & 1) != 0) ^ (idx > subindex)) michael@0: m_with = Position(advance(), 0); michael@0: else // normal match to previous root michael@0: m_attach = Position(other->advance(), 0); michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: case gr_slatAttX : m_attach.x = value; break; michael@0: case gr_slatAttY : m_attach.y = value; break; michael@0: case gr_slatAttXOff : michael@0: case gr_slatAttYOff : break; michael@0: case gr_slatAttWithX : m_with.x = value; break; michael@0: case gr_slatAttWithY : m_with.y = value; break; michael@0: case gr_slatAttWithXOff : michael@0: case gr_slatAttWithYOff : break; michael@0: case gr_slatAttLevel : michael@0: m_attLevel = byte(value); michael@0: break; michael@0: case gr_slatBreak : michael@0: seg->charinfo(m_original)->breakWeight(value); michael@0: break; michael@0: case gr_slatCompRef : break; // not sure what to do here michael@0: case gr_slatDir : m_bidiCls = value; break; michael@0: case gr_slatInsert : michael@0: markInsertBefore(value? true : false); michael@0: break; michael@0: case gr_slatPosX : break; // can't set these here michael@0: case gr_slatPosY : break; michael@0: case gr_slatShiftX : m_shift.x = value; break; michael@0: case gr_slatShiftY : m_shift.y = value; break; michael@0: case gr_slatMeasureSol : break; michael@0: case gr_slatMeasureEol : break; michael@0: case gr_slatJWidth : just(value); break; michael@0: case gr_slatSegSplit : seg->charinfo(m_original)->addflags(value & 3); break; michael@0: case gr_slatUserDefn : m_userAttr[subindex] = value; break; michael@0: default : michael@0: break; michael@0: } michael@0: } michael@0: michael@0: int Slot::getJustify(const Segment *seg, uint8 level, uint8 subindex) const michael@0: { michael@0: if (level && level >= seg->silf()->numJustLevels()) return 0; michael@0: michael@0: if (m_justs) michael@0: return m_justs->values[level * SlotJustify::NUMJUSTPARAMS + subindex]; michael@0: michael@0: if (level >= seg->silf()->numJustLevels()) return 0; michael@0: Justinfo *jAttrs = seg->silf()->justAttrs() + level; michael@0: michael@0: switch (subindex) { michael@0: case 0 : return seg->glyphAttr(gid(), jAttrs->attrStretch()); michael@0: case 1 : return seg->glyphAttr(gid(), jAttrs->attrShrink()); michael@0: case 2 : return seg->glyphAttr(gid(), jAttrs->attrStep()); michael@0: case 3 : return seg->glyphAttr(gid(), jAttrs->attrWeight()); michael@0: case 4 : return 0; // not been set yet, so clearly 0 michael@0: default: return 0; michael@0: } michael@0: } michael@0: michael@0: void Slot::setJustify(Segment *seg, uint8 level, uint8 subindex, int16 value) michael@0: { michael@0: if (level && level >= seg->silf()->numJustLevels()) return; michael@0: if (!m_justs) michael@0: { michael@0: SlotJustify *j = seg->newJustify(); michael@0: if (!j) return; michael@0: j->LoadSlot(this, seg); michael@0: m_justs = j; michael@0: } michael@0: m_justs->values[level * SlotJustify::NUMJUSTPARAMS + subindex] = value; michael@0: } michael@0: michael@0: bool Slot::child(Slot *ap) michael@0: { michael@0: if (this == ap) return false; michael@0: else if (ap == m_child) return true; michael@0: else if (!m_child) michael@0: m_child = ap; michael@0: else michael@0: return m_child->sibling(ap); michael@0: return true; michael@0: } michael@0: michael@0: bool Slot::sibling(Slot *ap) michael@0: { michael@0: if (this == ap) return false; michael@0: else if (ap == m_sibling) return true; michael@0: else if (!m_sibling || !ap) michael@0: m_sibling = ap; michael@0: else michael@0: return m_sibling->sibling(ap); michael@0: return true; michael@0: } michael@0: michael@0: bool Slot::removeChild(Slot *ap) michael@0: { michael@0: if (this == ap || !m_child) return false; michael@0: else if (ap == m_child) michael@0: { michael@0: Slot *nSibling = m_child->nextSibling(); michael@0: m_child->sibling(NULL); michael@0: m_child = nSibling; michael@0: return true; michael@0: } michael@0: else michael@0: return m_child->removeSibling(ap); michael@0: return true; michael@0: } michael@0: michael@0: bool Slot::removeSibling(Slot *ap) michael@0: { michael@0: if (this == ap || !m_sibling) return false; michael@0: else if (ap == m_sibling) michael@0: { michael@0: m_sibling = m_sibling->nextSibling(); michael@0: return true; michael@0: } michael@0: else michael@0: return m_sibling->removeSibling(ap); michael@0: return true; michael@0: } michael@0: michael@0: void Slot::setGlyph(Segment *seg, uint16 glyphid, const GlyphFace * theGlyph) michael@0: { michael@0: m_glyphid = glyphid; michael@0: if (!theGlyph) michael@0: { michael@0: theGlyph = seg->getFace()->glyphs().glyphSafe(glyphid); michael@0: if (!theGlyph) michael@0: { michael@0: m_realglyphid = 0; michael@0: m_advance = Position(0.,0.); michael@0: return; michael@0: } michael@0: } michael@0: m_realglyphid = theGlyph->attrs()[seg->silf()->aPseudo()]; michael@0: const GlyphFace *aGlyph = theGlyph; michael@0: if (m_realglyphid) michael@0: { michael@0: aGlyph = seg->getFace()->glyphs().glyphSafe(m_realglyphid); michael@0: if (!aGlyph) aGlyph = theGlyph; michael@0: } michael@0: m_advance = Position(aGlyph->theAdvance().x, 0.); michael@0: if (seg->silf()->aPassBits()) michael@0: seg->mergePassBits(theGlyph->attrs()[seg->silf()->aPassBits()]); michael@0: } michael@0: michael@0: void Slot::floodShift(Position adj) michael@0: { michael@0: m_position += adj; michael@0: if (m_child) m_child->floodShift(adj); michael@0: if (m_sibling) m_sibling->floodShift(adj); michael@0: } michael@0: michael@0: void SlotJustify::LoadSlot(const Slot *s, const Segment *seg) michael@0: { michael@0: for (int i = seg->silf()->numJustLevels() - 1; i >= 0; --i) michael@0: { michael@0: Justinfo *justs = seg->silf()->justAttrs() + i; michael@0: int16 *v = values + i * NUMJUSTPARAMS; michael@0: v[0] = seg->glyphAttr(s->gid(), justs->attrStretch()); michael@0: v[1] = seg->glyphAttr(s->gid(), justs->attrShrink()); michael@0: v[2] = seg->glyphAttr(s->gid(), justs->attrStep()); michael@0: v[3] = seg->glyphAttr(s->gid(), justs->attrWeight()); michael@0: } michael@0: }