gfx/graphite2/src/Slot.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

michael@0 1 /* GRAPHITE2 LICENSING
michael@0 2
michael@0 3 Copyright 2010, SIL International
michael@0 4 All rights reserved.
michael@0 5
michael@0 6 This library is free software; you can redistribute it and/or modify
michael@0 7 it under the terms of the GNU Lesser General Public License as published
michael@0 8 by the Free Software Foundation; either version 2.1 of License, or
michael@0 9 (at your option) any later version.
michael@0 10
michael@0 11 This program is distributed in the hope that it will be useful,
michael@0 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
michael@0 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
michael@0 14 Lesser General Public License for more details.
michael@0 15
michael@0 16 You should also have received a copy of the GNU Lesser General Public
michael@0 17 License along with this library in the file named "LICENSE".
michael@0 18 If not, write to the Free Software Foundation, 51 Franklin Street,
michael@0 19 Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
michael@0 20 internet at http://www.fsf.org/licenses/lgpl.html.
michael@0 21
michael@0 22 Alternatively, the contents of this file may be used under the terms of the
michael@0 23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
michael@0 24 License, as published by the Free Software Foundation, either version 2
michael@0 25 of the License or (at your option) any later version.
michael@0 26 */
michael@0 27 #include "inc/Segment.h"
michael@0 28 #include "inc/Slot.h"
michael@0 29 #include "inc/Silf.h"
michael@0 30 #include "inc/CharInfo.h"
michael@0 31 #include "inc/Rule.h"
michael@0 32
michael@0 33
michael@0 34 using namespace graphite2;
michael@0 35
michael@0 36 Slot::Slot() :
michael@0 37 m_next(NULL), m_prev(NULL),
michael@0 38 m_glyphid(0), m_realglyphid(0), m_original(0), m_before(0), m_after(0),
michael@0 39 m_index(0), m_parent(NULL), m_child(NULL), m_sibling(NULL),
michael@0 40 m_position(0, 0), m_shift(0, 0), m_advance(0, 0),
michael@0 41 m_attach(0, 0), m_with(0, 0), m_just(0.),
michael@0 42 m_flags(0), m_attLevel(0), m_bidiCls(-1), m_bidiLevel(0), m_justs(NULL)
michael@0 43 // Do not set m_userAttr since it is set *before* new is called since this
michael@0 44 // is used as a positional new to reset the GrSlot
michael@0 45 {
michael@0 46 }
michael@0 47
michael@0 48 // take care, this does not copy any of the GrSlot pointer fields
michael@0 49 void Slot::set(const Slot & orig, int charOffset, size_t numUserAttr, size_t justLevels, size_t numChars)
michael@0 50 {
michael@0 51 // leave m_next and m_prev unchanged
michael@0 52 m_glyphid = orig.m_glyphid;
michael@0 53 m_realglyphid = orig.m_realglyphid;
michael@0 54 m_original = orig.m_original + charOffset;
michael@0 55 if (charOffset + int(orig.m_before) < 0)
michael@0 56 m_before = 0;
michael@0 57 else
michael@0 58 m_before = orig.m_before + charOffset;
michael@0 59 if (charOffset <= 0 && orig.m_after + charOffset >= numChars)
michael@0 60 m_after = numChars - 1;
michael@0 61 else
michael@0 62 m_after = orig.m_after + charOffset;
michael@0 63 m_parent = NULL;
michael@0 64 m_child = NULL;
michael@0 65 m_sibling = NULL;
michael@0 66 m_position = orig.m_position;
michael@0 67 m_shift = orig.m_shift;
michael@0 68 m_advance = orig.m_advance;
michael@0 69 m_attach = orig.m_attach;
michael@0 70 m_with = orig.m_with;
michael@0 71 m_flags = orig.m_flags;
michael@0 72 m_attLevel = orig.m_attLevel;
michael@0 73 m_bidiCls = orig.m_bidiCls;
michael@0 74 m_bidiLevel = orig.m_bidiLevel;
michael@0 75 if (m_userAttr && orig.m_userAttr)
michael@0 76 memcpy(m_userAttr, orig.m_userAttr, numUserAttr * sizeof(*m_userAttr));
michael@0 77 if (m_justs && orig.m_justs)
michael@0 78 memcpy(m_justs, orig.m_justs, SlotJustify::size_of(justLevels));
michael@0 79 }
michael@0 80
michael@0 81 void Slot::update(int /*numGrSlots*/, int numCharInfo, Position &relpos)
michael@0 82 {
michael@0 83 m_before += numCharInfo;
michael@0 84 m_after += numCharInfo;
michael@0 85 m_position = m_position + relpos;
michael@0 86 }
michael@0 87
michael@0 88 Position Slot::finalise(const Segment *seg, const Font *font, Position & base, Rect & bbox, uint8 attrLevel, float & clusterMin)
michael@0 89 {
michael@0 90 if (attrLevel && m_attLevel > attrLevel) return Position(0, 0);
michael@0 91 float scale = 1.0;
michael@0 92 Position shift(m_shift.x * ((seg->dir() & 1) * -2 + 1) + m_just, m_shift.y);
michael@0 93 float tAdvance = m_advance.x + m_just;
michael@0 94 const GlyphFace * glyphFace = seg->getFace()->glyphs().glyphSafe(glyph());
michael@0 95 if (font)
michael@0 96 {
michael@0 97 scale = font->scale();
michael@0 98 shift *= scale;
michael@0 99 if (font->isHinted() && glyphFace)
michael@0 100 tAdvance = (m_advance.x - glyphFace->theAdvance().x + m_just) * scale + font->advance(m_glyphid);
michael@0 101 else
michael@0 102 tAdvance *= scale;
michael@0 103 }
michael@0 104 Position res;
michael@0 105
michael@0 106 m_position = base + shift;
michael@0 107 if (!m_parent)
michael@0 108 {
michael@0 109 res = base + Position(tAdvance, m_advance.y * scale);
michael@0 110 clusterMin = base.x;
michael@0 111 }
michael@0 112 else
michael@0 113 {
michael@0 114 float tAdv;
michael@0 115 m_position += (m_attach - m_with) * scale;
michael@0 116 tAdv = m_advance.x >= 0.5 ? m_position.x + tAdvance - shift.x : 0.f;
michael@0 117 res = Position(tAdv, 0);
michael@0 118 if ((m_advance.x >= 0.5 || m_position.x < 0) && m_position.x < clusterMin) clusterMin = m_position.x;
michael@0 119 }
michael@0 120
michael@0 121 if (glyphFace)
michael@0 122 {
michael@0 123 Rect ourBbox = glyphFace->theBBox() * scale + m_position;
michael@0 124 bbox = bbox.widen(ourBbox);
michael@0 125 }
michael@0 126
michael@0 127 if (m_child && m_child != this && m_child->attachedTo() == this)
michael@0 128 {
michael@0 129 Position tRes = m_child->finalise(seg, font, m_position, bbox, attrLevel, clusterMin);
michael@0 130 if ((!m_parent || m_advance.x >= 0.5) && tRes.x > res.x) res = tRes;
michael@0 131 }
michael@0 132
michael@0 133 if (m_parent && m_sibling && m_sibling != this && m_sibling->attachedTo() == m_parent)
michael@0 134 {
michael@0 135 Position tRes = m_sibling->finalise(seg, font, base, bbox, attrLevel, clusterMin);
michael@0 136 if (tRes.x > res.x) res = tRes;
michael@0 137 }
michael@0 138
michael@0 139 if (!m_parent && clusterMin < base.x)
michael@0 140 {
michael@0 141 Position adj = Position(base.x - clusterMin, 0.);
michael@0 142 res += adj;
michael@0 143 m_position += adj;
michael@0 144 if (m_child) m_child->floodShift(adj);
michael@0 145 }
michael@0 146 return res;
michael@0 147 }
michael@0 148
michael@0 149 int32 Slot::clusterMetric(const Segment *seg, uint8 metric, uint8 attrLevel)
michael@0 150 {
michael@0 151 Position base;
michael@0 152 Rect bbox = seg->theGlyphBBoxTemporary(glyph());
michael@0 153 float clusterMin = 0.;
michael@0 154 Position res = finalise(seg, NULL, base, bbox, attrLevel, clusterMin);
michael@0 155
michael@0 156 switch (metrics(metric))
michael@0 157 {
michael@0 158 case kgmetLsb :
michael@0 159 return static_cast<uint32>(bbox.bl.x);
michael@0 160 case kgmetRsb :
michael@0 161 return static_cast<uint32>(res.x - bbox.tr.x);
michael@0 162 case kgmetBbTop :
michael@0 163 return static_cast<uint32>(bbox.tr.y);
michael@0 164 case kgmetBbBottom :
michael@0 165 return static_cast<uint32>(bbox.bl.y);
michael@0 166 case kgmetBbLeft :
michael@0 167 return static_cast<uint32>(bbox.bl.x);
michael@0 168 case kgmetBbRight :
michael@0 169 return static_cast<uint32>(bbox.tr.x);
michael@0 170 case kgmetBbWidth :
michael@0 171 return static_cast<uint32>(bbox.tr.x - bbox.bl.x);
michael@0 172 case kgmetBbHeight :
michael@0 173 return static_cast<uint32>(bbox.tr.y - bbox.bl.y);
michael@0 174 case kgmetAdvWidth :
michael@0 175 return static_cast<uint32>(res.x);
michael@0 176 case kgmetAdvHeight :
michael@0 177 return static_cast<uint32>(res.y);
michael@0 178 default :
michael@0 179 return 0;
michael@0 180 }
michael@0 181 }
michael@0 182
michael@0 183 int Slot::getAttr(const Segment *seg, attrCode ind, uint8 subindex) const
michael@0 184 {
michael@0 185 if (!this) return 0;
michael@0 186 if (ind == gr_slatUserDefnV1)
michael@0 187 {
michael@0 188 ind = gr_slatUserDefn;
michael@0 189 subindex = 0;
michael@0 190 }
michael@0 191 else if (ind >= gr_slatJStretch && ind < gr_slatJStretch + 20 && ind != gr_slatJWidth)
michael@0 192 {
michael@0 193 int indx = ind - gr_slatJStretch;
michael@0 194 return getJustify(seg, indx / 5, indx % 5);
michael@0 195 }
michael@0 196
michael@0 197 switch (ind)
michael@0 198 {
michael@0 199 case gr_slatAdvX : return int(m_advance.x);
michael@0 200 case gr_slatAdvY : return int(m_advance.y);
michael@0 201 case gr_slatAttTo : return m_parent ? 1 : 0;
michael@0 202 case gr_slatAttX : return int(m_attach.x);
michael@0 203 case gr_slatAttY : return int(m_attach.y);
michael@0 204 case gr_slatAttXOff :
michael@0 205 case gr_slatAttYOff : return 0;
michael@0 206 case gr_slatAttWithX : return int(m_with.x);
michael@0 207 case gr_slatAttWithY : return int(m_with.y);
michael@0 208 case gr_slatAttWithXOff:
michael@0 209 case gr_slatAttWithYOff:return 0;
michael@0 210 case gr_slatAttLevel : return m_attLevel;
michael@0 211 case gr_slatBreak : return seg->charinfo(m_original)->breakWeight();
michael@0 212 case gr_slatCompRef : return 0;
michael@0 213 case gr_slatDir : if (m_bidiCls == -1)
michael@0 214 const_cast<Slot *>(this)->setBidiClass(seg->glyphAttr(gid(), seg->silf()->aBidi()));
michael@0 215 return m_bidiCls;
michael@0 216 case gr_slatInsert : return isInsertBefore();
michael@0 217 case gr_slatPosX : return int(m_position.x); // but need to calculate it
michael@0 218 case gr_slatPosY : return int(m_position.y);
michael@0 219 case gr_slatShiftX : return int(m_shift.x);
michael@0 220 case gr_slatShiftY : return int(m_shift.y);
michael@0 221 case gr_slatMeasureSol: return -1; // err what's this?
michael@0 222 case gr_slatMeasureEol: return -1;
michael@0 223 case gr_slatJWidth: return int(m_just);
michael@0 224 case gr_slatUserDefn : return m_userAttr[subindex];
michael@0 225 case gr_slatSegSplit : return seg->charinfo(m_original)->flags() & 3;
michael@0 226 case gr_slatBidiLevel: return m_bidiLevel;
michael@0 227 default : return 0;
michael@0 228 }
michael@0 229 }
michael@0 230
michael@0 231 void Slot::setAttr(Segment *seg, attrCode ind, uint8 subindex, int16 value, const SlotMap & map)
michael@0 232 {
michael@0 233 if (!this) return;
michael@0 234 if (ind == gr_slatUserDefnV1)
michael@0 235 {
michael@0 236 ind = gr_slatUserDefn;
michael@0 237 subindex = 0;
michael@0 238 }
michael@0 239 else if (ind >= gr_slatJStretch && ind < gr_slatJStretch + 20 && ind != gr_slatJWidth)
michael@0 240 {
michael@0 241 int indx = ind - gr_slatJStretch;
michael@0 242 return setJustify(seg, indx / 5, indx % 5, value);
michael@0 243 }
michael@0 244
michael@0 245 switch (ind)
michael@0 246 {
michael@0 247 case gr_slatAdvX : m_advance.x = value; break;
michael@0 248 case gr_slatAdvY : m_advance.y = value; break;
michael@0 249 case gr_slatAttTo :
michael@0 250 {
michael@0 251 const uint16 idx = uint16(value);
michael@0 252 if (idx < map.size() && map[idx])
michael@0 253 {
michael@0 254 Slot *other = map[idx];
michael@0 255 if (other == this) break;
michael@0 256 if (m_parent) m_parent->removeChild(this);
michael@0 257 if (other->child(this))
michael@0 258 {
michael@0 259 attachTo(other);
michael@0 260 if (((seg->dir() & 1) != 0) ^ (idx > subindex))
michael@0 261 m_with = Position(advance(), 0);
michael@0 262 else // normal match to previous root
michael@0 263 m_attach = Position(other->advance(), 0);
michael@0 264 }
michael@0 265 }
michael@0 266 break;
michael@0 267 }
michael@0 268 case gr_slatAttX : m_attach.x = value; break;
michael@0 269 case gr_slatAttY : m_attach.y = value; break;
michael@0 270 case gr_slatAttXOff :
michael@0 271 case gr_slatAttYOff : break;
michael@0 272 case gr_slatAttWithX : m_with.x = value; break;
michael@0 273 case gr_slatAttWithY : m_with.y = value; break;
michael@0 274 case gr_slatAttWithXOff :
michael@0 275 case gr_slatAttWithYOff : break;
michael@0 276 case gr_slatAttLevel :
michael@0 277 m_attLevel = byte(value);
michael@0 278 break;
michael@0 279 case gr_slatBreak :
michael@0 280 seg->charinfo(m_original)->breakWeight(value);
michael@0 281 break;
michael@0 282 case gr_slatCompRef : break; // not sure what to do here
michael@0 283 case gr_slatDir : m_bidiCls = value; break;
michael@0 284 case gr_slatInsert :
michael@0 285 markInsertBefore(value? true : false);
michael@0 286 break;
michael@0 287 case gr_slatPosX : break; // can't set these here
michael@0 288 case gr_slatPosY : break;
michael@0 289 case gr_slatShiftX : m_shift.x = value; break;
michael@0 290 case gr_slatShiftY : m_shift.y = value; break;
michael@0 291 case gr_slatMeasureSol : break;
michael@0 292 case gr_slatMeasureEol : break;
michael@0 293 case gr_slatJWidth : just(value); break;
michael@0 294 case gr_slatSegSplit : seg->charinfo(m_original)->addflags(value & 3); break;
michael@0 295 case gr_slatUserDefn : m_userAttr[subindex] = value; break;
michael@0 296 default :
michael@0 297 break;
michael@0 298 }
michael@0 299 }
michael@0 300
michael@0 301 int Slot::getJustify(const Segment *seg, uint8 level, uint8 subindex) const
michael@0 302 {
michael@0 303 if (level && level >= seg->silf()->numJustLevels()) return 0;
michael@0 304
michael@0 305 if (m_justs)
michael@0 306 return m_justs->values[level * SlotJustify::NUMJUSTPARAMS + subindex];
michael@0 307
michael@0 308 if (level >= seg->silf()->numJustLevels()) return 0;
michael@0 309 Justinfo *jAttrs = seg->silf()->justAttrs() + level;
michael@0 310
michael@0 311 switch (subindex) {
michael@0 312 case 0 : return seg->glyphAttr(gid(), jAttrs->attrStretch());
michael@0 313 case 1 : return seg->glyphAttr(gid(), jAttrs->attrShrink());
michael@0 314 case 2 : return seg->glyphAttr(gid(), jAttrs->attrStep());
michael@0 315 case 3 : return seg->glyphAttr(gid(), jAttrs->attrWeight());
michael@0 316 case 4 : return 0; // not been set yet, so clearly 0
michael@0 317 default: return 0;
michael@0 318 }
michael@0 319 }
michael@0 320
michael@0 321 void Slot::setJustify(Segment *seg, uint8 level, uint8 subindex, int16 value)
michael@0 322 {
michael@0 323 if (level && level >= seg->silf()->numJustLevels()) return;
michael@0 324 if (!m_justs)
michael@0 325 {
michael@0 326 SlotJustify *j = seg->newJustify();
michael@0 327 if (!j) return;
michael@0 328 j->LoadSlot(this, seg);
michael@0 329 m_justs = j;
michael@0 330 }
michael@0 331 m_justs->values[level * SlotJustify::NUMJUSTPARAMS + subindex] = value;
michael@0 332 }
michael@0 333
michael@0 334 bool Slot::child(Slot *ap)
michael@0 335 {
michael@0 336 if (this == ap) return false;
michael@0 337 else if (ap == m_child) return true;
michael@0 338 else if (!m_child)
michael@0 339 m_child = ap;
michael@0 340 else
michael@0 341 return m_child->sibling(ap);
michael@0 342 return true;
michael@0 343 }
michael@0 344
michael@0 345 bool Slot::sibling(Slot *ap)
michael@0 346 {
michael@0 347 if (this == ap) return false;
michael@0 348 else if (ap == m_sibling) return true;
michael@0 349 else if (!m_sibling || !ap)
michael@0 350 m_sibling = ap;
michael@0 351 else
michael@0 352 return m_sibling->sibling(ap);
michael@0 353 return true;
michael@0 354 }
michael@0 355
michael@0 356 bool Slot::removeChild(Slot *ap)
michael@0 357 {
michael@0 358 if (this == ap || !m_child) return false;
michael@0 359 else if (ap == m_child)
michael@0 360 {
michael@0 361 Slot *nSibling = m_child->nextSibling();
michael@0 362 m_child->sibling(NULL);
michael@0 363 m_child = nSibling;
michael@0 364 return true;
michael@0 365 }
michael@0 366 else
michael@0 367 return m_child->removeSibling(ap);
michael@0 368 return true;
michael@0 369 }
michael@0 370
michael@0 371 bool Slot::removeSibling(Slot *ap)
michael@0 372 {
michael@0 373 if (this == ap || !m_sibling) return false;
michael@0 374 else if (ap == m_sibling)
michael@0 375 {
michael@0 376 m_sibling = m_sibling->nextSibling();
michael@0 377 return true;
michael@0 378 }
michael@0 379 else
michael@0 380 return m_sibling->removeSibling(ap);
michael@0 381 return true;
michael@0 382 }
michael@0 383
michael@0 384 void Slot::setGlyph(Segment *seg, uint16 glyphid, const GlyphFace * theGlyph)
michael@0 385 {
michael@0 386 m_glyphid = glyphid;
michael@0 387 if (!theGlyph)
michael@0 388 {
michael@0 389 theGlyph = seg->getFace()->glyphs().glyphSafe(glyphid);
michael@0 390 if (!theGlyph)
michael@0 391 {
michael@0 392 m_realglyphid = 0;
michael@0 393 m_advance = Position(0.,0.);
michael@0 394 return;
michael@0 395 }
michael@0 396 }
michael@0 397 m_realglyphid = theGlyph->attrs()[seg->silf()->aPseudo()];
michael@0 398 const GlyphFace *aGlyph = theGlyph;
michael@0 399 if (m_realglyphid)
michael@0 400 {
michael@0 401 aGlyph = seg->getFace()->glyphs().glyphSafe(m_realglyphid);
michael@0 402 if (!aGlyph) aGlyph = theGlyph;
michael@0 403 }
michael@0 404 m_advance = Position(aGlyph->theAdvance().x, 0.);
michael@0 405 if (seg->silf()->aPassBits())
michael@0 406 seg->mergePassBits(theGlyph->attrs()[seg->silf()->aPassBits()]);
michael@0 407 }
michael@0 408
michael@0 409 void Slot::floodShift(Position adj)
michael@0 410 {
michael@0 411 m_position += adj;
michael@0 412 if (m_child) m_child->floodShift(adj);
michael@0 413 if (m_sibling) m_sibling->floodShift(adj);
michael@0 414 }
michael@0 415
michael@0 416 void SlotJustify::LoadSlot(const Slot *s, const Segment *seg)
michael@0 417 {
michael@0 418 for (int i = seg->silf()->numJustLevels() - 1; i >= 0; --i)
michael@0 419 {
michael@0 420 Justinfo *justs = seg->silf()->justAttrs() + i;
michael@0 421 int16 *v = values + i * NUMJUSTPARAMS;
michael@0 422 v[0] = seg->glyphAttr(s->gid(), justs->attrStretch());
michael@0 423 v[1] = seg->glyphAttr(s->gid(), justs->attrShrink());
michael@0 424 v[2] = seg->glyphAttr(s->gid(), justs->attrStep());
michael@0 425 v[3] = seg->glyphAttr(s->gid(), justs->attrWeight());
michael@0 426 }
michael@0 427 }

mercurial