LCOV - code coverage report
Current view: top level - vcl/source/glyphs - graphite_layout.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 1 618 0.2 %
Date: 2015-06-13 12:38:46 Functions: 2 33 6.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : // Description: An implementation of the SalLayout interface that uses the
      21             : //              Graphite engine.
      22             : 
      23             : // Enable lots of debug info
      24             : #if OSL_DEBUG_LEVEL > 1
      25             : #include <cstdio>
      26             : #define GRLAYOUT_DEBUG 1
      27             : #undef NDEBUG
      28             : #endif
      29             : 
      30             : // #define GRLAYOUT_DEBUG 1
      31             : 
      32             : #include <algorithm>
      33             : #include <cassert>
      34             : #include <functional>
      35             : #include <limits>
      36             : #include <numeric>
      37             : #include <deque>
      38             : 
      39             : #include <config_global.h>
      40             : 
      41             : #include <svsys.h>
      42             : 
      43             : #include <salgdi.hxx>
      44             : 
      45             : #include <unicode/uchar.h>
      46             : #include <unicode/ubidi.h>
      47             : #include <unicode/uscript.h>
      48             : 
      49             : #include <vcl/unohelp.hxx>
      50             : #include <com/sun/star/i18n/XCharacterClassification.hpp>
      51             : #include <com/sun/star/i18n/UnicodeType.hpp>
      52             : 
      53             : // Graphite Libraries (must be after vcl headers on windows)
      54             : #include <graphite_static.hxx>
      55             : #include <graphite2/Segment.h>
      56             : 
      57             : #include <graphite_layout.hxx>
      58             : #include <graphite_features.hxx>
      59             : 
      60             : #ifdef GRLAYOUT_DEBUG
      61             : static FILE * grLog()
      62             : {
      63             : #ifdef WNT
      64             :     static FILE * grLogFile = NULL;
      65             :     if (grLogFile == NULL)
      66             :     {
      67             :         std::string logFileName(getenv("TEMP"));
      68             :         logFileName.append("/graphitelayout.log");
      69             :         grLogFile = fopen(logFileName.c_str(),"w");
      70             :     }
      71             :     else
      72             :         fflush(grLogFile);
      73             :     return grLogFile;
      74             : #else
      75             :     fflush(stdout);
      76             :     return stdout;
      77             : #endif
      78             : }
      79             : #endif
      80             : 
      81             : namespace
      82             : {
      83           0 :     inline long round_to_long(const float n) {
      84           0 :         return long(n + (n < 0 ? -0.5 : 0.5));
      85             :     }
      86             : 
      87             :     template<typename T>
      88           0 :     inline bool in_range(const T i, const T b, const T e) {
      89           0 :         return !(b > i) && i < e;
      90             :     }
      91             : 
      92           0 :     int findSameDirLimit(const sal_Unicode* buffer, int charCount, bool rtl)
      93             :     {
      94           0 :         UErrorCode status = U_ZERO_ERROR;
      95           0 :         UBiDi *ubidi = ubidi_openSized(charCount, 0, &status);
      96           0 :         int limit = 0;
      97             :         ubidi_setPara(ubidi, reinterpret_cast<const UChar *>(buffer), charCount,
      98           0 :             (rtl)?UBIDI_DEFAULT_RTL:UBIDI_DEFAULT_LTR, NULL, &status);
      99           0 :         UBiDiLevel level = 0;
     100           0 :         ubidi_getLogicalRun(ubidi, 0, &limit, &level);
     101           0 :         ubidi_close(ubidi);
     102           0 :         if ((rtl && !(level & 1)) || (!rtl && (level & 1)))
     103             :         {
     104           0 :             limit = 0;
     105             :         }
     106           0 :         return limit;
     107             :     }
     108             : 
     109             :     template <typename T>
     110           0 :     T maximum(T a, T b)
     111             :     {
     112           0 :         return (a > b)? a : b;
     113             :     }
     114             :     template <typename T>
     115           0 :     T minimum(T a, T b)
     116             :     {
     117           0 :         return (a < b)? a : b;
     118             :     }
     119             : 
     120             : } // namespace
     121             : 
     122             : // Impementation of the GraphiteLayout::Glyphs container class.
     123             : //    This is an extended vector class with methods added to enable
     124             : //        o Correctly filling with glyphs.
     125             : //        o Querying clustering relationships.
     126             : //        o manipulations that affect neighouring glyphs.
     127             : 
     128             : const int GraphiteLayout::EXTRA_CONTEXT_LENGTH = 10;
     129             : 
     130             : // find first slot of cluster and first slot of subsequent cluster
     131           0 : static void findFirstClusterSlot(const gr_slot* base, gr_slot const** first, gr_slot const** after, int * firstChar, int * lastChar, bool bRtl)
     132             : {
     133           0 :     if (gr_slot_attached_to(base) == NULL)
     134             :     {
     135           0 :         *first = base;
     136             :         *after = (bRtl)? gr_slot_prev_in_segment(base) :
     137           0 :             gr_slot_next_in_segment(base);
     138           0 :         *firstChar = gr_slot_before(base);
     139           0 :         *lastChar = gr_slot_after(base);
     140             :     }
     141           0 :     const gr_slot * attachment = gr_slot_first_attachment(base);
     142           0 :     while (attachment)
     143             :     {
     144           0 :         if (gr_slot_origin_X(*first) > gr_slot_origin_X(attachment))
     145           0 :             *first = attachment;
     146             :         const gr_slot* attachmentNext = (bRtl)?
     147           0 :             gr_slot_prev_in_segment(attachment) : gr_slot_next_in_segment(attachment);
     148           0 :         if (attachmentNext)
     149             :         {
     150           0 :             if (*after && (gr_slot_origin_X(*after) < gr_slot_origin_X(attachmentNext)))
     151           0 :                 *after = attachmentNext;
     152             :         }
     153             :         else
     154             :         {
     155           0 :             *after = NULL;
     156             :         }
     157           0 :         if (gr_slot_before(attachment) < *firstChar)
     158           0 :             *firstChar = gr_slot_before(attachment);
     159           0 :         if (gr_slot_after(attachment) > *lastChar)
     160           0 :             *lastChar = gr_slot_after(attachment);
     161           0 :         if (gr_slot_first_attachment(attachment))
     162           0 :             findFirstClusterSlot(attachment, first, after, firstChar, lastChar, bRtl);
     163           0 :         attachment = gr_slot_next_sibling_attachment(attachment);
     164             :     }
     165           0 : }
     166             : 
     167             : // The Graphite glyph stream is really a sequence of glyph attachment trees
     168             : //  each rooted at a non-attached base glyph.  fill_from walks the glyph stream,
     169             : //  finds each non-attached base glyph and calls append to record them as a
     170             : //  sequence of clusters.
     171             : void
     172           0 : GraphiteLayout::fillFrom(gr_segment * pSegment, ImplLayoutArgs &rArgs, float fScaling)
     173             : {
     174           0 :     bool bRtl(rArgs.mnFlags & SalLayoutFlags::BiDiRtl);
     175           0 :     int nCharRequested = rArgs.mnEndCharPos - rArgs.mnMinCharPos;
     176           0 :     int nChar = gr_seg_n_cinfo(pSegment);
     177           0 :     float fMinX = gr_seg_advance_X(pSegment);
     178           0 :     float fMaxX = 0.0f;
     179           0 :     long nDxOffset = 0; // from dropped glyphs
     180           0 :     int nFirstCharInCluster = 0;
     181           0 :     int nLastCharInCluster = 0;
     182           0 :     unsigned int nGlyphs = gr_seg_n_slots(pSegment);
     183           0 :     mvGlyph2Char.assign(nGlyphs, -1);
     184           0 :     mvGlyphs.reserve(nGlyphs);
     185             : 
     186           0 :     if (bRtl)
     187             :     {
     188           0 :         const gr_slot* baseSlot = gr_seg_last_slot(pSegment);
     189             :         // find first base
     190           0 :         while (baseSlot && (gr_slot_attached_to(baseSlot) != NULL))
     191           0 :             baseSlot = gr_slot_prev_in_segment(baseSlot);
     192           0 :         int iChar = nChar - 1;
     193           0 :         int iNextChar = nChar - 1;
     194           0 :         bool reordered = false;
     195           0 :         int nBaseGlyphIndex = 0;
     196             :         // now loop over bases
     197           0 :         while (baseSlot)
     198             :         {
     199           0 :             bool bCluster = !reordered;
     200           0 :             const gr_slot * clusterFirst = NULL;
     201           0 :             const gr_slot * clusterAfter = NULL;
     202           0 :             int firstChar = -1;
     203           0 :             int lastChar = -1;
     204           0 :             findFirstClusterSlot(baseSlot, &clusterFirst, &clusterAfter, &firstChar, &lastChar, bRtl);
     205           0 :             iNextChar = minimum<int>(firstChar, iNextChar);
     206           0 :             if (bCluster)
     207             :             {
     208           0 :                 nBaseGlyphIndex = mvGlyphs.size();
     209           0 :                 mvGlyph2Char[nBaseGlyphIndex] = iChar + mnSegCharOffset;
     210           0 :                 nFirstCharInCluster = firstChar;
     211           0 :                 nLastCharInCluster = lastChar;
     212             :             }
     213             :             else
     214             :             {
     215           0 :                 mvGlyph2Char[mvGlyphs.size()] = firstChar + mnSegCharOffset;
     216           0 :                 nFirstCharInCluster = minimum<int>(firstChar, nFirstCharInCluster);
     217           0 :                 nLastCharInCluster = maximum<int>(firstChar, nLastCharInCluster);
     218             :             }
     219           0 :             float leftBoundary = gr_slot_origin_X(clusterFirst);
     220             :             float rightBoundary = (clusterAfter)?
     221           0 :                 gr_slot_origin_X(clusterAfter) : gr_seg_advance_X(pSegment);
     222           0 :             if (
     223           0 :                 lastChar < iChar && clusterAfter &&
     224           0 :                  (gr_cinfo_after(gr_seg_cinfo(pSegment, iChar)) >
     225           0 :                  static_cast<int>(gr_slot_index(clusterAfter)))
     226             :                )
     227             :             {
     228           0 :                 reordered = true;
     229             :             }
     230             :             else
     231             :             {
     232           0 :                 reordered = false;
     233           0 :                 iChar = iNextChar - 1;
     234             :             }
     235           0 :             if (mnSegCharOffset + nFirstCharInCluster >= mnMinCharPos &&
     236           0 :                 mnSegCharOffset + nFirstCharInCluster < mnEndCharPos)
     237             :             {
     238           0 :                 fMinX = minimum<float>(fMinX, leftBoundary);
     239           0 :                 fMaxX = maximum<float>(fMaxX, rightBoundary);
     240           0 :                 if (!reordered)
     241             :                 {
     242           0 :                     for (int i = nFirstCharInCluster; i <= nLastCharInCluster; i++)
     243             :                     {
     244           0 :                         if (mnSegCharOffset + i >= mnEndCharPos)
     245           0 :                             break;
     246             :                         // from the point of view of the dx array, the xpos is
     247             :                         // the origin of the first glyph of the cluster rtl
     248           0 :                         mvCharDxs[mnSegCharOffset + i - mnMinCharPos] =
     249           0 :                             static_cast<int>(leftBoundary * fScaling) + nDxOffset;
     250           0 :                         mvCharBreaks[mnSegCharOffset + i - mnMinCharPos] = gr_cinfo_break_weight(gr_seg_cinfo(pSegment, i));
     251             :                     }
     252           0 :                     mvChar2BaseGlyph[mnSegCharOffset + nFirstCharInCluster - mnMinCharPos] = nBaseGlyphIndex;
     253             :                 }
     254             :                 append(pSegment, rArgs, baseSlot, gr_slot_origin_X(baseSlot), rightBoundary, fScaling,
     255           0 :                        nDxOffset, bCluster, mnSegCharOffset + firstChar);
     256             :             }
     257           0 :             if (mnSegCharOffset + nLastCharInCluster < mnMinCharPos)
     258           0 :                 break;
     259           0 :             baseSlot = gr_slot_next_sibling_attachment(baseSlot);
     260             :         }
     261             :     }
     262             :     else
     263             :     {
     264           0 :         const gr_slot* baseSlot = gr_seg_first_slot(pSegment);
     265             :         // find first base
     266           0 :         while (baseSlot && (gr_slot_attached_to(baseSlot) != NULL))
     267           0 :             baseSlot = gr_slot_next_in_segment(baseSlot);
     268           0 :         int iChar = 0; // relative to segment
     269           0 :         int iNextChar = 0;
     270           0 :         bool reordered = false;
     271           0 :         int nBaseGlyphIndex = 0;
     272             :         // now loop over bases
     273           0 :         while (baseSlot)
     274             :         {
     275           0 :             bool bCluster = !reordered;
     276           0 :             const gr_slot * clusterFirst = NULL;
     277           0 :             const gr_slot * clusterAfter = NULL;
     278           0 :             int firstChar = -1;
     279           0 :             int lastChar = -1;
     280           0 :             findFirstClusterSlot(baseSlot, &clusterFirst, &clusterAfter, &firstChar, &lastChar, bRtl);
     281           0 :             iNextChar = maximum<int>(lastChar, iNextChar);
     282           0 :             if (bCluster)
     283             :             {
     284           0 :                 nBaseGlyphIndex = mvGlyphs.size();
     285           0 :                 mvGlyph2Char[nBaseGlyphIndex] = iChar + mnSegCharOffset;
     286           0 :                 nFirstCharInCluster = firstChar;
     287           0 :                 nLastCharInCluster = lastChar;
     288             :             }
     289             :             else
     290             :             {
     291           0 :                 mvGlyph2Char[mvGlyphs.size()] = firstChar + mnSegCharOffset;
     292           0 :                 nFirstCharInCluster = minimum<int>(firstChar, nFirstCharInCluster);
     293           0 :                 nLastCharInCluster = maximum<int>(lastChar, nLastCharInCluster);
     294             :             }
     295           0 :             if (
     296           0 :                 firstChar > iChar &&
     297           0 :                  (gr_cinfo_before(gr_seg_cinfo(pSegment, iChar)) >
     298           0 :                  static_cast<int>(gr_slot_index(clusterFirst)))
     299             :                )
     300             :             {
     301           0 :                 reordered = true;
     302             :             }
     303             :             else
     304             :             {
     305           0 :                 reordered = false;
     306           0 :                 iChar = iNextChar + 1;
     307             :             }
     308           0 :             float leftBoundary = gr_slot_origin_X(clusterFirst);
     309             :             float rightBoundary = (clusterAfter)?
     310           0 :                 gr_slot_origin_X(clusterAfter) : gr_seg_advance_X(pSegment);
     311           0 :             int bFirstChar = gr_cinfo_base(gr_seg_cinfo(pSegment, nFirstCharInCluster));
     312           0 :             if (mnSegCharOffset + bFirstChar >= mnMinCharPos &&
     313           0 :                 mnSegCharOffset + bFirstChar < mnEndCharPos)
     314             :             {
     315           0 :                 fMinX = minimum<float>(fMinX, leftBoundary);
     316           0 :                 fMaxX = maximum<float>(fMaxX, rightBoundary);
     317           0 :                 if (!reordered)
     318             :                 {
     319           0 :                     for (int i = nFirstCharInCluster; i <= nLastCharInCluster; i++)
     320             :                     {
     321           0 :                         int ibase = gr_cinfo_base(gr_seg_cinfo(pSegment, i));
     322           0 :                         if (mnSegCharOffset + ibase >= mnEndCharPos)
     323           0 :                             break;
     324             :                         // from the point of view of the dx array, the xpos is
     325             :                         // the origin of the first glyph of the next cluster ltr
     326           0 :                         mvCharDxs[mnSegCharOffset + ibase - mnMinCharPos] =
     327           0 :                             static_cast<int>(rightBoundary * fScaling) + nDxOffset;
     328           0 :                         mvCharBreaks[mnSegCharOffset + ibase - mnMinCharPos] = gr_cinfo_break_weight(gr_seg_cinfo(pSegment, i));
     329             :                     }
     330             :                     // only set mvChar2BaseGlyph for first character of cluster
     331           0 :                     mvChar2BaseGlyph[mnSegCharOffset + bFirstChar - mnMinCharPos] = nBaseGlyphIndex;
     332             :                 }
     333             :                 append(pSegment, rArgs, baseSlot, gr_slot_origin_X(baseSlot), rightBoundary, fScaling,
     334           0 :                        nDxOffset, true, mnSegCharOffset + firstChar);
     335             :             }
     336           0 :             if (mnSegCharOffset + bFirstChar >= mnEndCharPos)
     337           0 :                 break;
     338           0 :             baseSlot = gr_slot_next_sibling_attachment(baseSlot);
     339             :         }
     340             :     }
     341           0 :     long nXOffset = round_to_long(fMinX * fScaling);
     342           0 :     mnWidth = round_to_long(fMaxX * fScaling) - nXOffset + nDxOffset;
     343           0 :     if (mnWidth < 0)
     344             :     {
     345             :         // This can happen when there was no base inside the range
     346           0 :         mnWidth = 0;
     347             :     }
     348             :     // fill up non-base char dx with cluster widths from previous base glyph
     349           0 :     if (bRtl)
     350             :     {
     351           0 :         if (mvCharDxs[nCharRequested-1] == -1)
     352           0 :             mvCharDxs[nCharRequested-1] = 0;
     353             :         else
     354           0 :             mvCharDxs[nCharRequested-1] -= nXOffset;
     355           0 :         for (int i = nCharRequested - 2; i >= 0; i--)
     356             :         {
     357           0 :             if (mvCharDxs[i] == -1) mvCharDxs[i] = mvCharDxs[i+1];
     358           0 :             else mvCharDxs[i] -= nXOffset;
     359             :         }
     360             :     }
     361             :     else
     362             :     {
     363           0 :         if (mvCharDxs[0] == -1)
     364           0 :             mvCharDxs[0] = 0;
     365             :         else
     366           0 :             mvCharDxs[0] -= nXOffset;
     367           0 :         for (int i = 1; i < nCharRequested; i++)
     368             :         {
     369           0 :             if (mvCharDxs[i] == -1) mvCharDxs[i] = mvCharDxs[i-1];
     370           0 :             else mvCharDxs[i] -= nXOffset;
     371             : #ifdef GRLAYOUT_DEBUG
     372             :             fprintf(grLog(),"%d,%d ", (int)i, (int)mvCharDxs[i]);
     373             : #endif
     374             :         }
     375             :     }
     376             :     // remove offset due to context if there is one
     377           0 :     if (nXOffset != 0)
     378             :     {
     379           0 :         for (size_t i = 0; i < mvGlyphs.size(); i++)
     380           0 :             mvGlyphs[i].maLinearPos.X() -= nXOffset;
     381             :     }
     382             : #ifdef GRLAYOUT_DEBUG
     383             :     fprintf(grLog(), "fillFrom %" SAL_PRI_SIZET "u glyphs offset %ld width %ld\n", mvGlyphs.size(), nXOffset, mnWidth);
     384             : #endif
     385           0 : }
     386             : 
     387             : // append walks an attachment tree, flattening it, and converting it into a
     388             : // sequence of GlyphItem objects which we can later manipulate.
     389             : float
     390           0 : GraphiteLayout::append(gr_segment *pSeg, ImplLayoutArgs &rArgs,
     391             :     const gr_slot * gi, float gOrigin, float nextGlyphOrigin, float scaling, long & rDXOffset,
     392             :     bool bIsBase, int baseChar)
     393             : {
     394           0 :     bool bRtl(rArgs.mnFlags & SalLayoutFlags::BiDiRtl);
     395             :     float nextOrigin;
     396             :     assert(gi);
     397             :     assert(gr_slot_before(gi) <= gr_slot_after(gi));
     398           0 :     int firstChar = gr_slot_before(gi) + mnSegCharOffset;
     399             :     assert(mvGlyphs.size() < mvGlyph2Char.size());
     400           0 :     if (!bIsBase) mvGlyph2Char[mvGlyphs.size()] = baseChar;//firstChar;
     401             :     // is the next glyph attached or in the next cluster?
     402             :     //glyph_set_range_t iAttached = gi.attachedClusterGlyphs();
     403           0 :     const gr_slot * pFirstAttached = gr_slot_first_attachment(gi);
     404           0 :     const gr_slot * pNextSibling = gr_slot_next_sibling_attachment(gi);
     405           0 :     if (pFirstAttached)
     406           0 :         nextOrigin = gr_slot_origin_X(pFirstAttached);
     407           0 :     else if (!bIsBase && pNextSibling)
     408           0 :         nextOrigin = gr_slot_origin_X(pNextSibling);
     409             :     else
     410           0 :         nextOrigin = nextGlyphOrigin;
     411           0 :     long glyphId = gr_slot_gid(gi);
     412           0 :     long deltaOffset = 0;
     413           0 :     int scaledGlyphPos = round_to_long(gr_slot_origin_X(gi) * scaling);
     414           0 :     int glyphWidth = round_to_long((nextOrigin - gOrigin) * scaling);
     415             : //    if (glyphWidth < 0)
     416             : //    {
     417             : //        nextOrigin = gOrigin;
     418             : //        glyphWidth = 0;
     419             : //    }
     420             : #ifdef GRLAYOUT_DEBUG
     421             :     fprintf(grLog(),"c%d g%ld,X%d W%d nX%f ", firstChar, glyphId,
     422             :         (int)(gr_slot_origin_X(gi) * scaling), glyphWidth, nextOrigin * scaling);
     423             : #endif
     424           0 :     if (glyphId == 0)
     425             :     {
     426           0 :         rArgs.NeedFallback(firstChar, bRtl);
     427           0 :         if( (SalLayoutFlags::ForFallback & rArgs.mnFlags ))
     428             :         {
     429           0 :             glyphId = GF_DROPPED;
     430           0 :             deltaOffset -= glyphWidth;
     431           0 :             glyphWidth = 0;
     432             :         }
     433             :     }
     434           0 :     else if(rArgs.mnFlags & SalLayoutFlags::ForFallback)
     435             :     {
     436             : #ifdef GRLAYOUT_DEBUG
     437             :         fprintf(grLog(),"fallback c%d %x in run %d\n", firstChar, rArgs.mpStr[firstChar],
     438             :             rArgs.maRuns.PosIsInAnyRun(firstChar));
     439             : #endif
     440             :         // glyphs that aren't requested for fallback will be taken from base
     441             :         // layout, so mark them as dropped (should this wait until Simplify(false) is called?)
     442           0 :         if (!rArgs.maRuns.PosIsInAnyRun(firstChar) &&
     443           0 :             in_range(firstChar, rArgs.mnMinCharPos, rArgs.mnEndCharPos))
     444             :         {
     445           0 :             glyphId = GF_DROPPED;
     446           0 :             deltaOffset -= glyphWidth;
     447           0 :             glyphWidth = 0;
     448             :         }
     449             :     }
     450             :     // append this glyph. Set the cluster flag if this glyph is attached to another
     451           0 :     long nGlyphFlags = bIsBase ? 0 : GlyphItem::IS_IN_CLUSTER;
     452           0 :     nGlyphFlags |= (bRtl)? GlyphItem::IS_RTL_GLYPH : 0;
     453           0 :     GlyphItem aGlyphItem(mvGlyphs.size(),
     454             :         glyphId,
     455             :         Point(scaledGlyphPos + rDXOffset,
     456           0 :             round_to_long((-gr_slot_origin_Y(gi) * scaling))),
     457             :         nGlyphFlags,
     458           0 :         glyphWidth);
     459           0 :     if (glyphId != static_cast<long>(GF_DROPPED))
     460           0 :         aGlyphItem.mnOrigWidth = round_to_long(gr_slot_advance_X(gi, mpFace, mpFont) * scaling);
     461           0 :     mvGlyphs.push_back(aGlyphItem);
     462             : 
     463             :     // update the offset if this glyph was dropped
     464           0 :     rDXOffset += deltaOffset;
     465             : 
     466             :     // Recursively append all the attached glyphs.
     467           0 :     float cOrigin = nextOrigin;
     468           0 :     for (const gr_slot * agi = gr_slot_first_attachment(gi); agi != NULL; agi = gr_slot_next_sibling_attachment(agi))
     469           0 :         cOrigin = append(pSeg, rArgs, agi, cOrigin, nextGlyphOrigin, scaling, rDXOffset, false, baseChar);
     470             : 
     471           0 :     return cOrigin;
     472             : }
     473             : 
     474             : // An implementation of the SalLayout interface to enable Graphite enabled fonts to be used.
     475             : 
     476           0 : GraphiteLayout::GraphiteLayout(const gr_face * face, gr_font * font,
     477             :                                const grutils::GrFeatureParser * pFeatures) throw()
     478             :     : mpFace(face)
     479             :     , mpFont(font)
     480             :     , mnSegCharOffset(0)
     481             :     , mnWidth(0)
     482             :     , mfScaling(1.0)
     483           0 :     , mpFeatures(pFeatures)
     484             : {
     485             : 
     486           0 : }
     487             : 
     488           0 : GraphiteLayout::~GraphiteLayout() throw()
     489             : {
     490           0 :     clear();
     491             :     // the features and font are owned by the platform layers
     492           0 :     mpFeatures = NULL;
     493           0 :     mpFont = NULL;
     494           0 : }
     495             : 
     496           0 : void GraphiteLayout::clear()
     497             : {
     498             :     // Destroy the segment and text source from any previous invocation of
     499             :     // LayoutText
     500           0 :     mvGlyphs.clear();
     501           0 :     mvCharDxs.clear();
     502           0 :     mvChar2BaseGlyph.clear();
     503           0 :     mvGlyph2Char.clear();
     504             : 
     505             :     // Reset the state to the empty state.
     506           0 :     mnWidth = 0;
     507             :     // Don't reset the scaling, because it is set before LayoutText
     508           0 : }
     509             : 
     510             : // This method shouldn't be called on windows, since it needs the dc reset
     511           0 : bool GraphiteLayout::LayoutText(ImplLayoutArgs & rArgs)
     512             : {
     513           0 :     bool success = true;
     514           0 :     if (rArgs.mnMinCharPos < rArgs.mnEndCharPos)
     515             :     {
     516           0 :         gr_segment * pSegment = CreateSegment(rArgs);
     517           0 :         if (!pSegment)
     518           0 :             return false;
     519           0 :         success = LayoutGlyphs(rArgs, pSegment);
     520           0 :         if (pSegment)
     521             :         {
     522           0 :             gr_seg_destroy(pSegment);
     523           0 :             pSegment = NULL;
     524             :         }
     525             :     }
     526             :     else
     527             :     {
     528           0 :         clear();
     529             :     }
     530           0 :     return success;
     531             : }
     532             : 
     533           0 : gr_segment * GraphiteLayout::CreateSegment(ImplLayoutArgs& rArgs)
     534             : {
     535             :     assert(rArgs.mnLength >= 0);
     536             : 
     537           0 :     gr_segment * pSegment = NULL;
     538             : 
     539             :     // Set the SalLayouts values to be the initial ones.
     540           0 :     SalLayout::AdjustLayout(rArgs);
     541             :     // TODO check if this is needed
     542           0 :     if (mnUnitsPerPixel > 1)
     543           0 :         mfScaling = 1.0f / mnUnitsPerPixel;
     544             : 
     545             :     // Clear out any previous buffers
     546           0 :     clear();
     547           0 :     bool bRtl(mnLayoutFlags & SalLayoutFlags::BiDiRtl);
     548             :     try
     549             :     {
     550             :         // Don't set RTL if font doesn't support it otherwise it forces rtl on
     551             :         // everything
     552             :         //if (bRtl && (mrFont.getSupportedScriptDirections() & gr::kfsdcHorizRtl))
     553             :         //    maLayout.setRightToLeft(bRtl);
     554             : 
     555             :         // Context is often needed beyond the specified end, however, we don't
     556             :         // want it if there has been a direction change, since it is hard
     557             :         // to tell between reordering within one direction and multi-directional
     558             :         // text. Extra context, can also cause problems with ligatures stradling
     559             :         // a hyphenation point, so disable if CTL is disabled.
     560           0 :         mnSegCharOffset = rArgs.mnMinCharPos;
     561           0 :         int limit = rArgs.mnEndCharPos;
     562           0 :         if (!(SalLayoutFlags::ComplexDisabled & rArgs.mnFlags))
     563             :         {
     564           0 :             int nSegCharMin = maximum<int>(0, mnMinCharPos - EXTRA_CONTEXT_LENGTH);
     565           0 :             int nSegCharLimit = minimum(rArgs.mnLength, mnEndCharPos + EXTRA_CONTEXT_LENGTH);
     566           0 :             while (nSegCharMin < mnSegCharOffset)
     567             :             {
     568             :                 int sameDirEnd = nSegCharMin + findSameDirLimit(rArgs.mpStr + nSegCharMin,
     569           0 :                     rArgs.mnEndCharPos - nSegCharMin, bRtl);
     570           0 :                 if (sameDirEnd >= rArgs.mnMinCharPos)
     571             :                 {
     572           0 :                     mnSegCharOffset = nSegCharMin;
     573           0 :                     break;
     574             :                 }
     575             :                 else
     576           0 :                     nSegCharMin = sameDirEnd;
     577             :             }
     578           0 :             if (nSegCharLimit > limit)
     579             :             {
     580             :                 limit += findSameDirLimit(rArgs.mpStr + rArgs.mnEndCharPos,
     581           0 :                     nSegCharLimit - rArgs.mnEndCharPos, bRtl);
     582           0 :                 if (limit > rArgs.mnLength)
     583           0 :                     limit = rArgs.mnLength;
     584             :             }
     585             :         }
     586             :         else
     587             :         {
     588           0 :             limit = minimum(rArgs.mnLength, mnEndCharPos + EXTRA_CONTEXT_LENGTH);
     589           0 :             mnSegCharOffset = maximum<int>(0, mnMinCharPos - EXTRA_CONTEXT_LENGTH);
     590             :         }
     591             : 
     592           0 :         size_t numchars = gr_count_unicode_characters(gr_utf16, rArgs.mpStr + mnSegCharOffset,
     593           0 :                  rArgs.mpStr + limit, NULL);
     594           0 :         if (mpFeatures)
     595           0 :             pSegment = gr_make_seg(mpFont, mpFace, 0, mpFeatures->values(), gr_utf16,
     596           0 :                                         rArgs.mpStr + mnSegCharOffset, numchars, bRtl);
     597             :         else
     598             :             pSegment = gr_make_seg(mpFont, mpFace, 0, NULL, gr_utf16,
     599           0 :                                         rArgs.mpStr + mnSegCharOffset, numchars, bRtl);
     600             : 
     601             :         //pSegment = new gr::RangeSegment((gr::Font *)&mrFont, mpTextSrc, &maLayout, mnMinCharPos, limit);
     602           0 :         if (pSegment != NULL)
     603             :         {
     604             : #ifdef GRLAYOUT_DEBUG
     605             :             fprintf(grLog(),"Gr::LayoutText %d-%d, context %d, len %d, numchars %" SAL_PRI_SIZET "u, rtl %d scaling %f:", rArgs.mnMinCharPos,
     606             :                rArgs.mnEndCharPos, limit, rArgs.mnLength, numchars, bRtl, mfScaling);
     607             :             for (int i = mnSegCharOffset; i < limit; ++i)
     608             :                 fprintf(grLog(), " %04X", rArgs.mpStr[i]);
     609             :             fprintf(grLog(), "\n");
     610             : #endif
     611             :         }
     612             :         else
     613             :         {
     614             : #ifdef GRLAYOUT_DEBUG
     615             :             fprintf(grLog(), "Gr::LayoutText failed: ");
     616             :             for (int i = mnMinCharPos; i < limit; i++)
     617             :             {
     618             :                 fprintf(grLog(), "%04x ", rArgs.mpStr[i]);
     619             :             }
     620             :             fprintf(grLog(), "\n");
     621             : #endif
     622           0 :             clear();
     623           0 :             return NULL;
     624             :         }
     625             :     }
     626           0 :     catch (...)
     627             :     {
     628           0 :         clear();  // destroy the text source and any partially built segments.
     629           0 :         return NULL;
     630             :     }
     631           0 :     return pSegment;
     632             : }
     633             : 
     634           0 : bool GraphiteLayout::LayoutGlyphs(ImplLayoutArgs& rArgs, gr_segment * pSegment)
     635             : {
     636             :     // Calculate the initial character dxs.
     637           0 :     mvCharDxs.assign(mnEndCharPos - mnMinCharPos, -1);
     638           0 :     mvChar2BaseGlyph.assign(mnEndCharPos - mnMinCharPos, -1);
     639           0 :     mvCharBreaks.assign(mnEndCharPos - mnMinCharPos, 0);
     640           0 :     mnWidth = 0;
     641           0 :     if (mvCharDxs.size() > 0)
     642             :     {
     643             :         // Discover all the clusters.
     644             :         try
     645             :         {
     646           0 :             bool bRtl(mnLayoutFlags & SalLayoutFlags::BiDiRtl);
     647           0 :             fillFrom(pSegment, rArgs, mfScaling);
     648             : 
     649           0 :             if (bRtl)
     650             :             {
     651             :                 // not needed for adjacent differences, but for mouse clicks to char
     652             :                 std::transform(mvCharDxs.begin(), mvCharDxs.end(), mvCharDxs.begin(),
     653           0 :                     std::bind1st(std::minus<long>(), mnWidth));
     654             :                 // fixup last dx to ensure it always equals the width
     655           0 :                 mvCharDxs[mvCharDxs.size() - 1] = mnWidth;
     656             :             }
     657             :         }
     658           0 :         catch (const std::exception &e)
     659             :         {
     660             : #ifdef GRLAYOUT_DEBUG
     661             :             fprintf(grLog(),"LayoutGlyphs failed %s\n", e.what());
     662             : #else
     663             :             (void)e;
     664             : #endif
     665           0 :             return false;
     666             :         }
     667           0 :         catch (...)
     668             :         {
     669             : #ifdef GRLAYOUT_DEBUG
     670             :             fprintf(grLog(),"LayoutGlyphs failed with exception");
     671             : #endif
     672           0 :             return false;
     673             :         }
     674             :     }
     675             :     else
     676             :     {
     677           0 :         mnWidth = 0;
     678             :     }
     679           0 :     return true;
     680             : }
     681             : 
     682           0 : sal_Int32 GraphiteLayout::GetTextBreak(DeviceCoordinate maxmnWidth, DeviceCoordinate char_extra, int factor) const
     683             : {
     684             : #ifdef GRLAYOUT_DEBUG
     685             :     fprintf(grLog(),"Gr::GetTextBreak c[%d-%d) maxWidth %ld char extra %ld factor %d\n",
     686             :         mnMinCharPos, mnEndCharPos, maxmnWidth, char_extra, factor);
     687             : #endif
     688             : 
     689             :     // return quickly if this segment is narrower than the target width
     690           0 :     if (maxmnWidth > mnWidth * factor + char_extra * (mnEndCharPos - mnMinCharPos - 1))
     691           0 :         return -1;
     692             : 
     693           0 :     DeviceCoordinate nWidth = mvCharDxs[0] * factor;
     694           0 :     long wLastBreak = 0;
     695           0 :     int nLastBreak = -1;
     696           0 :     int nEmergency = -1;
     697           0 :     for (size_t i = 1; i < mvCharDxs.size(); i++)
     698             :     {
     699           0 :         nWidth += char_extra;
     700           0 :         if (nWidth > maxmnWidth) break;
     701           0 :         if (mvChar2BaseGlyph[i] != -1)
     702             :         {
     703           0 :             if (
     704           0 :                 (mvCharBreaks[i] > -35 || (mvCharBreaks[i-1] > 0 && mvCharBreaks[i-1] < 35)) &&
     705           0 :                 (mvCharBreaks[i-1] < 35 || (mvCharBreaks[i] < 0 && mvCharBreaks[i] > -35))
     706             :                )
     707             :             {
     708           0 :                 nLastBreak = static_cast<int>(i);
     709           0 :                 wLastBreak = nWidth;
     710             :             }
     711           0 :             nEmergency = static_cast<int>(i);
     712             :         }
     713           0 :         nWidth += (mvCharDxs[i] - mvCharDxs[i-1]) * factor;
     714             :     }
     715           0 :     int nBreak = mnMinCharPos;
     716           0 :     if (wLastBreak > 9 * maxmnWidth / 10)
     717           0 :         nBreak += nLastBreak;
     718             :     else
     719           0 :         if (nEmergency > -1)
     720           0 :             nBreak += nEmergency;
     721             : 
     722             : #ifdef GRLAYOUT_DEBUG
     723             :     fprintf(grLog(), "Gr::GetTextBreak break after %d, weights(%d, %d)\n", nBreak - mnMinCharPos, mvCharBreaks[nBreak - mnMinCharPos], mvCharBreaks[nBreak - mnMinCharPos - 1]);
     724             : #endif
     725             : 
     726           0 :     if (nBreak > mnEndCharPos)
     727           0 :         nBreak = -1;
     728           0 :     else if (nBreak < mnMinCharPos)
     729           0 :         nBreak = mnMinCharPos;
     730           0 :     return nBreak;
     731             : }
     732             : 
     733           0 : DeviceCoordinate GraphiteLayout::FillDXArray( DeviceCoordinate* pDXArray ) const
     734             : {
     735           0 :     if (mnEndCharPos == mnMinCharPos)
     736             :         // Then we must be zero width!
     737           0 :         return 0;
     738             : 
     739           0 :     if (pDXArray)
     740             :     {
     741           0 :         for (size_t i = 0; i < mvCharDxs.size(); i++)
     742             :         {
     743             :             assert( (mvChar2BaseGlyph[i] == -1) ||
     744             :                 ((signed)(mvChar2BaseGlyph[i]) < (signed)mvGlyphs.size()));
     745           0 :             if (mvChar2BaseGlyph[i] != -1 &&
     746           0 :                 mvGlyphs[mvChar2BaseGlyph[i]].maGlyphId == GF_DROPPED)
     747             :             {
     748             :                 // when used in MultiSalLayout::GetTextBreak dropped glyphs
     749             :                 // must have zero width
     750           0 :                 pDXArray[i] = 0;
     751             :             }
     752             :             else
     753             :             {
     754           0 :                 pDXArray[i] = mvCharDxs[i];
     755           0 :                 if (i > 0) pDXArray[i] -= mvCharDxs[i-1];
     756             :             }
     757             : #ifdef GRLAYOUT_DEBUG
     758             :             fprintf(grLog(),"%d,%d,%ld ", (int)i, (int)mvCharDxs[i], pDXArray[i]);
     759             : #endif
     760             :         }
     761             :         //std::adjacent_difference(mvCharDxs.begin(), mvCharDxs.end(), pDXArray);
     762             :         //for (size_t i = 0; i < mvCharDxs.size(); i++)
     763             :         //    fprintf(grLog(),"%d,%d,%d ", (int)i, (int)mvCharDxs[i], pDXArray[i]);
     764             :         //fprintf(grLog(),"FillDX %ld,%d\n", mnWidth, std::accumulate(pDXArray, pDXArray + mvCharDxs.size(), 0));
     765             :     }
     766             : #ifdef GRLAYOUT_DEBUG
     767             :     fprintf(grLog(),"FillDXArray %d-%d=%g\n", mnMinCharPos, mnEndCharPos, (double)mnWidth);
     768             : #endif
     769           0 :     return mnWidth;
     770             : }
     771             : 
     772           0 : void  GraphiteLayout::AdjustLayout(ImplLayoutArgs& rArgs)
     773             : {
     774           0 :     SalLayout::AdjustLayout(rArgs);
     775           0 :     if(rArgs.mpDXArray)
     776             :     {
     777           0 :         std::vector<int> vDeltaWidths(mvGlyphs.size(), 0);
     778           0 :         ApplyDXArray(rArgs, vDeltaWidths);
     779             : 
     780           0 :         if( (mnLayoutFlags & SalLayoutFlags::BiDiRtl) &&
     781           0 :            !(rArgs.mnFlags & SalLayoutFlags::ForFallback) )
     782             :         {
     783             :             // check if this is a kashida script
     784           0 :             bool bKashidaScript = false;
     785           0 :             for (int i = rArgs.mnMinCharPos; i < rArgs.mnEndCharPos; i++)
     786             :             {
     787           0 :                 UErrorCode aStatus = U_ZERO_ERROR;
     788           0 :                 UScriptCode scriptCode = uscript_getScript(rArgs.mpStr[i], &aStatus);
     789           0 :                 if (scriptCode == USCRIPT_ARABIC || scriptCode == USCRIPT_SYRIAC)
     790             :                 {
     791           0 :                     bKashidaScript = true;
     792           0 :                     break;
     793             :                 }
     794             :             }
     795           0 :             int nKashidaWidth = 0;
     796           0 :             int nKashidaIndex = getKashidaGlyph(nKashidaWidth);
     797           0 :             if( nKashidaIndex != 0 && bKashidaScript)
     798             :             {
     799           0 :                 kashidaJustify( vDeltaWidths, nKashidaIndex, nKashidaWidth );
     800             :             }
     801           0 :         }
     802             :     }
     803           0 :     else if (rArgs.mnLayoutWidth > 0)
     804             :     {
     805             : #ifdef GRLAYOUT_DEBUG
     806             :         fprintf(grLog(), "AdjustLayout width %ld=>%ld\n", mnWidth, rArgs.mnLayoutWidth);
     807             : #endif
     808           0 :         expandOrCondense(rArgs);
     809             :     }
     810           0 : }
     811             : 
     812           0 : void GraphiteLayout::expandOrCondense(ImplLayoutArgs &rArgs)
     813             : {
     814           0 :     int nDeltaWidth = rArgs.mnLayoutWidth - mnWidth;
     815           0 :     if (nDeltaWidth > 0) // expand, just expand between clusters
     816             :     {
     817             :         // NOTE: for expansion we can use base glyphs (which have IsClusterStart set)
     818             :         // even though they may have been reordered in which case they will have
     819             :         // been placed in a bigger cluster for other purposes.
     820           0 :         int nClusterCount = 0;
     821           0 :         for (size_t j = 0; j < mvGlyphs.size(); j++)
     822             :         {
     823           0 :             if (mvGlyphs[j].IsClusterStart())
     824             :             {
     825           0 :                 ++nClusterCount;
     826             :             }
     827             :         }
     828           0 :         if (nClusterCount > 1)
     829             :         {
     830           0 :             float fExtraPerCluster = static_cast<float>(nDeltaWidth) / static_cast<float>(nClusterCount - 1);
     831           0 :             int nCluster = 0;
     832           0 :             int nOffset = 0;
     833           0 :             for (size_t i = 0; i < mvGlyphs.size(); i++)
     834             :             {
     835           0 :                 if (mvGlyphs[i].IsClusterStart())
     836             :                 {
     837           0 :                     nOffset = static_cast<int>(fExtraPerCluster * nCluster);
     838           0 :                     int nCharIndex = mvGlyph2Char[i];
     839             :                     assert(nCharIndex > -1);
     840           0 :                     if (nCharIndex < mnMinCharPos ||
     841           0 :                         static_cast<size_t>(nCharIndex-mnMinCharPos)
     842           0 :                             >= mvCharDxs.size())
     843             :                     {
     844           0 :                         continue;
     845             :                     }
     846           0 :                     mvCharDxs[nCharIndex-mnMinCharPos] += nOffset;
     847             :                     // adjust char dxs for rest of characters in cluster
     848           0 :                     while (++nCharIndex - mnMinCharPos < static_cast<int>(mvChar2BaseGlyph.size()))
     849             :                     {
     850           0 :                         int nChar2Base = mvChar2BaseGlyph[nCharIndex-mnMinCharPos];
     851           0 :                         if (nChar2Base == -1 || nChar2Base == static_cast<int>(i))
     852           0 :                             mvCharDxs[nCharIndex-mnMinCharPos] += nOffset;
     853             :                         else
     854             :                             break;
     855             :                     }
     856           0 :                     ++nCluster;
     857             :                 }
     858           0 :                 mvGlyphs[i].maLinearPos.X() += nOffset;
     859             :             }
     860             :         }
     861             :     }
     862           0 :     else if (nDeltaWidth < 0)// condense - apply a factor to all glyph positions
     863             :     {
     864           0 :         if (mvGlyphs.empty()) return;
     865           0 :         Glyphs::iterator iLastGlyph = mvGlyphs.begin() + (mvGlyphs.size() - 1);
     866             :         // position last glyph using original width
     867           0 :         float fXFactor = static_cast<float>(rArgs.mnLayoutWidth - iLastGlyph->mnOrigWidth) / static_cast<float>(iLastGlyph->maLinearPos.X());
     868             : #ifdef GRLAYOUT_DEBUG
     869             :         fprintf(grLog(), "Condense by factor %f last x%ld\n", fXFactor, iLastGlyph->maLinearPos.X());
     870             : #endif
     871           0 :         if (fXFactor < 0)
     872           0 :             return; // probably a bad mnOrigWidth value
     873           0 :         iLastGlyph->maLinearPos.X() = rArgs.mnLayoutWidth - iLastGlyph->mnOrigWidth;
     874           0 :         Glyphs::iterator iGlyph = mvGlyphs.begin();
     875           0 :         while (iGlyph != iLastGlyph)
     876             :         {
     877           0 :             iGlyph->maLinearPos.X() = static_cast<int>(static_cast<float>(iGlyph->maLinearPos.X()) * fXFactor);
     878           0 :             ++iGlyph;
     879             :         }
     880           0 :         for (size_t i = 0; i < mvCharDxs.size(); i++)
     881             :         {
     882           0 :             mvCharDxs[i] = static_cast<int>(fXFactor * static_cast<float>(mvCharDxs[i]));
     883             :         }
     884             :     }
     885           0 :     mnWidth = rArgs.mnLayoutWidth;
     886             : }
     887             : 
     888           0 : void GraphiteLayout::ApplyDXArray(ImplLayoutArgs &args, std::vector<int> & rDeltaWidth)
     889             : {
     890           0 :     const size_t nChars = args.mnEndCharPos - args.mnMinCharPos;
     891           0 :     if (nChars == 0) return;
     892             : 
     893             : #ifdef GRLAYOUT_DEBUG
     894             :     for (size_t iDx = 0; iDx < mvCharDxs.size(); iDx++)
     895             :          fprintf(grLog(),"%d,%d,%ld ", (int)iDx, (int)mvCharDxs[iDx], args.mpDXArray[iDx]);
     896             :     fprintf(grLog(),"ApplyDx\n");
     897             : #endif
     898           0 :     bool bRtl(mnLayoutFlags & SalLayoutFlags::BiDiRtl);
     899           0 :     int nXOffset = 0;
     900           0 :     if (bRtl)
     901             :     {
     902           0 :         nXOffset = args.mpDXArray[nChars - 1] - mvCharDxs[nChars - 1];
     903             :     }
     904           0 :     int nPrevClusterGlyph = (bRtl)? (signed)mvGlyphs.size() : -1;
     905           0 :     int nPrevClusterLastChar = -1;
     906           0 :     for (size_t i = 0; i < nChars; i++)
     907             :     {
     908           0 :         int nChar2Base = mvChar2BaseGlyph[i];
     909           0 :         if ((nChar2Base > -1) && (nChar2Base != nPrevClusterGlyph))
     910             :         {
     911             :             assert((nChar2Base > -1) && (nChar2Base < (signed)mvGlyphs.size()));
     912           0 :             GlyphItem & gi = mvGlyphs[nChar2Base];
     913           0 :             if (!gi.IsClusterStart())
     914           0 :                 continue;
     915             : 
     916             :             // find last glyph of this cluster
     917           0 :             size_t j = i + 1;
     918           0 :             int nLastChar = i;
     919           0 :             int nLastGlyph = nChar2Base;
     920           0 :             for (; j < nChars; j++)
     921             :             {
     922           0 :                 const int nChar2BaseJ = mvChar2BaseGlyph[j];
     923             :                 assert((nChar2BaseJ >= -1) && (nChar2BaseJ < (signed)mvGlyphs.size()));
     924           0 :                 if (nChar2BaseJ != -1 )
     925             :                 {
     926           0 :                     nLastGlyph = nChar2BaseJ + ((bRtl)? +1 : -1);
     927           0 :                     nLastChar = j - 1;
     928           0 :                     break;
     929             :                 }
     930             :             }
     931           0 :             if (nLastGlyph < 0)
     932             :             {
     933           0 :                 nLastGlyph = nChar2Base;
     934             :             }
     935             :             // Its harder to find the last glyph rtl, since the first of
     936             :             // cluster is still on the left so we need to search towards
     937             :             // the previous cluster to the right
     938           0 :             if (bRtl)
     939             :             {
     940           0 :                 nLastGlyph = nChar2Base;
     941           0 :                 while (nLastGlyph + 1 < (signed)mvGlyphs.size() &&
     942           0 :                        !mvGlyphs[nLastGlyph+1].IsClusterStart())
     943             :                 {
     944           0 :                     ++nLastGlyph;
     945             :                 }
     946             :             }
     947           0 :             if (j == nChars)
     948             :             {
     949           0 :                 nLastChar = nChars - 1;
     950           0 :                 if (!bRtl) nLastGlyph = mvGlyphs.size() - 1;
     951             :             }
     952           0 :             int nBaseCount = 0;
     953             :             // count bases within cluster - may be more than 1 with reordering
     954           0 :             for (int k = nChar2Base; k <= nLastGlyph; k++)
     955             :             {
     956           0 :                 if (mvGlyphs[k].IsClusterStart()) ++nBaseCount;
     957             :             }
     958             :             assert((nLastChar > -1) && (nLastChar < (signed)nChars));
     959           0 :             long nNewClusterWidth = args.mpDXArray[nLastChar];
     960           0 :             long nOrigClusterWidth = mvCharDxs[nLastChar];
     961           0 :             long nDGlyphOrigin = 0;
     962           0 :             if (nPrevClusterLastChar > - 1)
     963             :             {
     964             :                 assert(nPrevClusterLastChar < (signed)nChars);
     965           0 :                 nNewClusterWidth -= args.mpDXArray[nPrevClusterLastChar];
     966           0 :                 nOrigClusterWidth -= mvCharDxs[nPrevClusterLastChar];
     967           0 :                 nDGlyphOrigin = args.mpDXArray[nPrevClusterLastChar] - mvCharDxs[nPrevClusterLastChar];
     968             :             }
     969           0 :             long nDWidth = nNewClusterWidth - nOrigClusterWidth;
     970             : #ifdef GRLAYOUT_DEBUG
     971             :             fprintf(grLog(), "c%lu last glyph %d/%lu\n", i, nLastGlyph, mvGlyphs.size());
     972             : #endif
     973             :             assert((nLastGlyph > -1) && (nLastGlyph < (signed)mvGlyphs.size()));
     974           0 :             mvGlyphs[nLastGlyph].mnNewWidth += nDWidth;
     975           0 :             if (gi.maGlyphId != GF_DROPPED)
     976           0 :                 mvGlyphs[nLastGlyph].mnNewWidth += nDWidth;
     977             :             else
     978           0 :                 nDGlyphOrigin += nDWidth;
     979           0 :             long nDOriginPerBase = (nBaseCount > 0)? nDWidth / nBaseCount : 0;
     980           0 :             nBaseCount = -1;
     981             :             // update glyph positions
     982           0 :             if (bRtl)
     983             :             {
     984           0 :                 for (int n = nChar2Base; n <= nLastGlyph; n++)
     985             :                 {
     986           0 :                     if (mvGlyphs[n].IsClusterStart()) ++nBaseCount;
     987             :                     assert((n > - 1) && (n < (signed)mvGlyphs.size()));
     988           0 :                     mvGlyphs[n].maLinearPos.X() += -(nDGlyphOrigin + nDOriginPerBase * nBaseCount) + nXOffset;
     989             :                 }
     990             :             }
     991             :             else
     992             :             {
     993           0 :                 for (int n = nChar2Base; n <= nLastGlyph; n++)
     994             :                 {
     995           0 :                     if (mvGlyphs[n].IsClusterStart()) ++nBaseCount;
     996             :                     assert((n > - 1) && (n < (signed)mvGlyphs.size()));
     997           0 :                     mvGlyphs[n].maLinearPos.X() += nDGlyphOrigin + (nDOriginPerBase * nBaseCount) + nXOffset;
     998             :                 }
     999             :             }
    1000           0 :             rDeltaWidth[nChar2Base] = nDWidth;
    1001             : #ifdef GRLAYOUT_DEBUG
    1002             :             fprintf(grLog(),"c%d g%d-%d dW%ld-%ld=%ld dX%ld x%ld\t", (int)i, nChar2Base, nLastGlyph, nNewClusterWidth, nOrigClusterWidth, nDWidth, nDGlyphOrigin, mvGlyphs[nChar2Base].maLinearPos.X());
    1003             : #endif
    1004           0 :             nPrevClusterGlyph = nChar2Base;
    1005           0 :             nPrevClusterLastChar = nLastChar;
    1006           0 :             i = nLastChar;
    1007             :         }
    1008             :     }
    1009             :     // Update the dx vector with the new values.
    1010             :     std::copy(args.mpDXArray, args.mpDXArray + nChars,
    1011           0 :       mvCharDxs.begin() + (args.mnMinCharPos - mnMinCharPos));
    1012             : #ifdef GRLAYOUT_DEBUG
    1013             :     fprintf(grLog(),"ApplyDx %ld(%ld)\n", args.mpDXArray[nChars - 1], mnWidth);
    1014             : #endif
    1015           0 :     mnWidth = args.mpDXArray[nChars - 1];
    1016             : }
    1017             : 
    1018           0 : void GraphiteLayout::kashidaJustify(std::vector<int>& rDeltaWidths, sal_GlyphId nKashidaIndex, int nKashidaWidth)
    1019             : {
    1020             :     // skip if the kashida glyph in the font looks suspicious
    1021           0 :     if( nKashidaWidth <= 0 )
    1022           0 :         return;
    1023             : 
    1024             :     // calculate max number of needed kashidas
    1025           0 :     Glyphs::iterator i = mvGlyphs.begin();
    1026           0 :     int nKashidaCount = 0;
    1027           0 :     int nOrigGlyphIndex = -1;
    1028           0 :     int nGlyphIndex = -1;
    1029           0 :     while (i != mvGlyphs.end())
    1030             :     {
    1031           0 :         nOrigGlyphIndex++;
    1032           0 :         nGlyphIndex++;
    1033             :         // only inject kashidas in RTL contexts
    1034           0 :         if( !(*i).IsRTLGlyph() )
    1035             :         {
    1036           0 :             ++i;
    1037           0 :             continue;
    1038             :         }
    1039             :         // no kashida-injection for blank justified expansion either
    1040           0 :         if( IsSpacingGlyph( (*i).maGlyphId ) )
    1041             :         {
    1042           0 :             ++i;
    1043           0 :             continue;
    1044             :         }
    1045             :         // calculate gap, ignore if too small
    1046           0 :         int nGapWidth = rDeltaWidths[nOrigGlyphIndex];
    1047             :         // worst case is one kashida even for mini-gaps
    1048           0 :         if( 3 * nGapWidth < nKashidaWidth )
    1049             :         {
    1050           0 :             ++i;
    1051           0 :             continue;
    1052             :         }
    1053           0 :         nKashidaCount = 1 + (nGapWidth / nKashidaWidth);
    1054             : #ifdef GRLAYOUT_DEBUG
    1055             :         printf("inserting %d kashidas at %u\n", nKashidaCount, (*i).maGlyphId);
    1056             : #endif
    1057           0 :         GlyphItem glyphItem = *i;
    1058           0 :         Point aPos(0, 0);
    1059           0 :         aPos.X() = (*i).maLinearPos.X();
    1060             :         GlyphItem newGi(glyphItem.mnCharPos, nKashidaIndex, aPos,
    1061           0 :                 GlyphItem::IS_IN_CLUSTER|GlyphItem::IS_RTL_GLYPH, nKashidaWidth);
    1062           0 :         mvGlyphs.reserve(mvGlyphs.size() + nKashidaCount);
    1063           0 :         i = mvGlyphs.begin() + nGlyphIndex;
    1064           0 :         mvGlyphs.insert(i, nKashidaCount, newGi);
    1065           0 :         i = mvGlyphs.begin() + nGlyphIndex;
    1066           0 :         nGlyphIndex += nKashidaCount;
    1067             :         // now fix up the kashida positions
    1068           0 :         for (int j = 0; j < nKashidaCount; j++)
    1069             :         {
    1070           0 :             (*(i)).maLinearPos.X() -= nGapWidth;
    1071           0 :             nGapWidth -= nKashidaWidth;
    1072           0 :             ++i;
    1073             :         }
    1074             : 
    1075             :         // fixup rightmost kashida for gap remainder
    1076           0 :         if( nGapWidth < 0 )
    1077             :         {
    1078           0 :             if( nKashidaCount <= 1 )
    1079           0 :                 nGapWidth /= 2;               // for small gap move kashida to middle
    1080           0 :             (*(i-1)).mnNewWidth += nGapWidth;  // adjust kashida width to gap width
    1081           0 :             (*(i-1)).maLinearPos.X() += nGapWidth;
    1082             :         }
    1083             : 
    1084           0 :         (*i).mnNewWidth = (*i).mnOrigWidth;
    1085           0 :         ++i;
    1086             :     }
    1087             : 
    1088             : }
    1089             : 
    1090           0 : void GraphiteLayout::GetCaretPositions( int nArraySize, long* pCaretXArray ) const
    1091             : {
    1092             :     // For each character except the last discover the caret positions
    1093             :     // immediately before and after that character.
    1094             :     // This is used for underlines in the GUI amongst other things.
    1095             :     // It may be used from MultiSalLayout, in which case it must take into account
    1096             :     // glyphs that have been moved.
    1097           0 :     std::fill(pCaretXArray, pCaretXArray + nArraySize, -1);
    1098             :     // the layout method doesn't modify the layout even though it isn't
    1099             :     // const in the interface
    1100           0 :     bool bRtl(mnLayoutFlags & SalLayoutFlags::BiDiRtl);//const_cast<GraphiteLayout*>(this)->maLayout.rightToLeft();
    1101           0 :     int prevBase = -1;
    1102           0 :     long prevClusterWidth = 0;
    1103           0 :     for (int i = 0, nCharSlot = 0; i < nArraySize && nCharSlot < static_cast<int>(mvCharDxs.size()); ++nCharSlot, i+=2)
    1104             :     {
    1105           0 :         if (mvChar2BaseGlyph[nCharSlot] != -1)
    1106             :         {
    1107           0 :             int nChar2Base = mvChar2BaseGlyph[nCharSlot];
    1108             :             assert((nChar2Base > -1) && (nChar2Base < (signed)mvGlyphs.size()));
    1109           0 :             GlyphItem gi = mvGlyphs[nChar2Base];
    1110           0 :             if (gi.maGlyphId == GF_DROPPED)
    1111             :             {
    1112           0 :                 continue;
    1113             :             }
    1114           0 :             int nCluster = nChar2Base;
    1115           0 :             long origClusterWidth = gi.mnNewWidth;
    1116           0 :             long nMin = gi.maLinearPos.X();
    1117           0 :             long nMax = gi.maLinearPos.X() + gi.mnNewWidth;
    1118             :             // attached glyphs are always stored after their base rtl or ltr
    1119           0 :             while (++nCluster < static_cast<int>(mvGlyphs.size()) &&
    1120           0 :                 !mvGlyphs[nCluster].IsClusterStart())
    1121             :             {
    1122           0 :                 origClusterWidth += mvGlyphs[nCluster].mnNewWidth;
    1123           0 :                 if (mvGlyph2Char[nCluster] == nCharSlot)
    1124             :                 {
    1125           0 :                     nMin = minimum(nMin, mvGlyphs[nCluster].maLinearPos.X());
    1126           0 :                     nMax = maximum(nMax, mvGlyphs[nCluster].maLinearPos.X() + mvGlyphs[nCluster].mnNewWidth);
    1127             :                 }
    1128             :             }
    1129           0 :             if (bRtl)
    1130             :             {
    1131           0 :                 pCaretXArray[i+1] = nMin;
    1132           0 :                 pCaretXArray[i] = nMax;
    1133             :             }
    1134             :             else
    1135             :             {
    1136           0 :                 pCaretXArray[i] = nMin;
    1137           0 :                 pCaretXArray[i+1] = nMax;
    1138             :             }
    1139           0 :             prevBase = nChar2Base;
    1140           0 :             prevClusterWidth = origClusterWidth;
    1141             :         }
    1142           0 :         else if (prevBase > -1)
    1143             :         {
    1144             :             // this could probably be improved
    1145             :             assert((prevBase > -1) && (prevBase < (signed)mvGlyphs.size()));
    1146           0 :             GlyphItem gi = mvGlyphs[prevBase];
    1147           0 :             int nGlyph = prevBase + 1;
    1148             :             // try to find a better match, otherwise default to complete cluster
    1149           0 :             for (; nGlyph < static_cast<int>(mvGlyphs.size()) &&
    1150           0 :                  !mvGlyphs[nGlyph].IsClusterStart(); nGlyph++)
    1151             :             {
    1152           0 :                 if (mvGlyph2Char[nGlyph] == nCharSlot)
    1153             :                 {
    1154           0 :                     gi = mvGlyphs[nGlyph];
    1155           0 :                     break;
    1156             :                 }
    1157             :             }
    1158             :             // if no match position at end of cluster
    1159           0 :             if (nGlyph == static_cast<int>(mvGlyphs.size()) ||
    1160           0 :                 mvGlyphs[nGlyph].IsClusterStart())
    1161             :             {
    1162           0 :                 if (bRtl)
    1163             :                 {
    1164           0 :                     pCaretXArray[i+1] = gi.maLinearPos.X();
    1165           0 :                     pCaretXArray[i] = gi.maLinearPos.X();
    1166             :                 }
    1167             :                 else
    1168             :                 {
    1169           0 :                     pCaretXArray[i] = gi.maLinearPos.X() + prevClusterWidth;
    1170           0 :                     pCaretXArray[i+1] = gi.maLinearPos.X() + prevClusterWidth;
    1171             :                 }
    1172             :             }
    1173             :             else
    1174             :             {
    1175           0 :                 if (bRtl)
    1176             :                 {
    1177           0 :                     pCaretXArray[i+1] = gi.maLinearPos.X();
    1178           0 :                     pCaretXArray[i] = gi.maLinearPos.X() + gi.mnNewWidth;
    1179             :                 }
    1180             :                 else
    1181             :                 {
    1182           0 :                     pCaretXArray[i] = gi.maLinearPos.X();
    1183           0 :                     pCaretXArray[i+1] = gi.maLinearPos.X() + gi.mnNewWidth;
    1184             :                 }
    1185             :             }
    1186             :         }
    1187             :         else
    1188             :         {
    1189           0 :             pCaretXArray[i] = pCaretXArray[i+1] = 0;
    1190             :         }
    1191             : #ifdef GRLAYOUT_DEBUG
    1192             :         fprintf(grLog(),"%d,%ld-%ld\t", nCharSlot, pCaretXArray[i], pCaretXArray[i+1]);
    1193             : #endif
    1194             :     }
    1195             : #ifdef GRLAYOUT_DEBUG
    1196             :     fprintf(grLog(),"\n");
    1197             : #endif
    1198           0 : }
    1199             : 
    1200             : // GetNextGlyphs returns a contiguous sequence of glyphs that can be
    1201             : // rendered together. It should never return a dropped glyph.
    1202             : // The glyph_slot returned should be the index of the next visible
    1203             : // glyph after the last glyph returned by this call.
    1204             : // The char_index array should be filled with the characters corresponding
    1205             : // to each glyph returned.
    1206             : // glyph_adv array should be a virtual width such that if successive
    1207             : // glyphs returned by this method are added one after the other they
    1208             : // have the correct spacing.
    1209             : // The logic in this method must match that expected in MultiSalLayout which
    1210             : // is used when glyph fallback is in operation.
    1211           0 : int GraphiteLayout::GetNextGlyphs( int length, sal_GlyphId * glyph_out,
    1212             :                                    ::Point & aPosOut, int &glyph_slot, DeviceCoordinate* glyph_adv, int *char_index,
    1213             :                                    const PhysicalFontFace** /*pFallbackFonts*/ ) const
    1214             : {
    1215             :   // Sanity check on the slot index.
    1216           0 :   if (glyph_slot >= signed(mvGlyphs.size()))
    1217             :   {
    1218           0 :     glyph_slot = mvGlyphs.size();
    1219           0 :     return 0;
    1220             :   }
    1221             :   assert(glyph_slot >= 0);
    1222             :   // Find the first glyph in the substring.
    1223           0 :   for (; glyph_slot < signed(mvGlyphs.size()) &&
    1224           0 :           ((mvGlyphs.begin() + glyph_slot)->maGlyphId == GF_DROPPED);
    1225             :           ++glyph_slot) {};
    1226             : 
    1227             :   // Update the length
    1228           0 :   const int nGlyphSlotEnd = minimum(size_t(glyph_slot + length), mvGlyphs.size());
    1229             : 
    1230             :   // We're all out of glyphs here.
    1231           0 :   if (glyph_slot == nGlyphSlotEnd)
    1232             :   {
    1233           0 :     return 0;
    1234             :   }
    1235             : 
    1236             :   // Find as many glyphs as we can which can be drawn in one go.
    1237           0 :   Glyphs::const_iterator glyph_itr = mvGlyphs.begin() + glyph_slot;
    1238           0 :   const int         glyph_slot_begin = glyph_slot;
    1239           0 :   const int            initial_y_pos = glyph_itr->maLinearPos.Y();
    1240             : 
    1241             :   // Set the position to the position of the start glyph.
    1242           0 :   ::Point aStartPos = glyph_itr->maLinearPos;
    1243             :   //aPosOut = glyph_itr->maLinearPos;
    1244           0 :   aPosOut = GetDrawPosition(aStartPos);
    1245             : 
    1246             :   for (;;)  // Forever
    1247             :   {
    1248             :      // last index of the range from glyph_to_chars does not include this glyph
    1249           0 :      if (char_index)
    1250             :      {
    1251           0 :          if (glyph_slot >= (signed)mvGlyph2Char.size())
    1252             :          {
    1253           0 :             *char_index++ = mnMinCharPos + mvCharDxs.size();
    1254             :          }
    1255             :          else
    1256             :          {
    1257             :             assert(glyph_slot > -1);
    1258           0 :             if (mvGlyph2Char[glyph_slot] == -1)
    1259           0 :                 *char_index++ = mnMinCharPos + mvCharDxs.size();
    1260             :             else
    1261           0 :                 *char_index++ = mvGlyph2Char[glyph_slot];
    1262             :          }
    1263             :      }
    1264             :      // Copy out this glyphs data.
    1265           0 :      ++glyph_slot;
    1266           0 :      *glyph_out++ = glyph_itr->maGlyphId;
    1267             : 
    1268             :      // Find the actual advance - this must be correct if called from
    1269             :      // MultiSalLayout::AdjustLayout which requests one glyph at a time.
    1270           0 :      const long nGlyphAdvance = (glyph_slot == static_cast<int>(mvGlyphs.size()))?
    1271           0 :           glyph_itr->mnNewWidth :
    1272           0 :           ((glyph_itr+1)->maLinearPos.X() - glyph_itr->maLinearPos.X());
    1273             : 
    1274             : #ifdef GRLAYOUT_DEBUG
    1275             :     fprintf(grLog(),"GetNextGlyphs g%d gid%d c%d x%ld,%ld adv%ld, pos %ld,%ld\n",
    1276             :             glyph_slot - 1, glyph_itr->maGlyphId,
    1277             :             mvGlyph2Char[glyph_slot-1], glyph_itr->maLinearPos.X(), glyph_itr->maLinearPos.Y(), (long)nGlyphAdvance,
    1278             :             aPosOut.X(), aPosOut.Y());
    1279             : #endif
    1280             : 
    1281           0 :      if (glyph_adv)  // If we are returning advance store it.
    1282           0 :        *glyph_adv++ = nGlyphAdvance;
    1283             :      else // Stop when next advance is unexpected.
    1284           0 :        if (glyph_itr->mnOrigWidth != nGlyphAdvance)  break;
    1285             : 
    1286             :      // Have fetched all the glyphs we need to
    1287           0 :      if (glyph_slot == nGlyphSlotEnd)
    1288           0 :          break;
    1289             : 
    1290           0 :      ++glyph_itr;
    1291             :      // Stop when next y position is unexpected.
    1292           0 :      if (initial_y_pos != glyph_itr->maLinearPos.Y())
    1293           0 :        break;
    1294             : 
    1295             :      // Stop if glyph dropped
    1296           0 :      if (glyph_itr->maGlyphId == GF_DROPPED)
    1297           0 :        break;
    1298           0 :   }
    1299           0 :   int numGlyphs = glyph_slot - glyph_slot_begin;
    1300             :   // move the next glyph_slot to a glyph that hasn't been dropped
    1301           0 :   while (glyph_slot < static_cast<int>(mvGlyphs.size()) &&
    1302           0 :          (mvGlyphs.begin() + glyph_slot)->maGlyphId == GF_DROPPED)
    1303           0 :          ++glyph_slot;
    1304           0 :   return numGlyphs;
    1305             : }
    1306             : 
    1307           0 : void GraphiteLayout::MoveGlyph( int nGlyphIndex, long nNewPos )
    1308             : {
    1309             :     // TODO it might be better to actually implement simplify properly, but this
    1310             :     // needs to be done carefully so the glyph/char maps are maintained
    1311             :     // If a glyph has been dropped then it wasn't returned by GetNextGlyphs, so
    1312             :     // the index here may be wrong
    1313           0 :     while ((mvGlyphs[nGlyphIndex].maGlyphId == GF_DROPPED) &&
    1314           0 :            (nGlyphIndex < (signed)mvGlyphs.size()))
    1315             :     {
    1316           0 :         nGlyphIndex++;
    1317             :     }
    1318           0 :     const long dx = nNewPos - mvGlyphs[nGlyphIndex].maLinearPos.X();
    1319             : 
    1320           0 :     if (dx == 0)  return;
    1321             :     // GenericSalLayout only changes maLinearPos, mvCharDxs doesn't change
    1322             : #ifdef GRLAYOUT_DEBUG
    1323             :     fprintf(grLog(),"Move %d (%ld,%ld) c%d by %ld\n", nGlyphIndex, mvGlyphs[nGlyphIndex].maLinearPos.X(), nNewPos, mvGlyph2Char[nGlyphIndex], dx);
    1324             : #endif
    1325           0 :     for (size_t gi = nGlyphIndex; gi < mvGlyphs.size(); gi++)
    1326             :     {
    1327           0 :         mvGlyphs[gi].maLinearPos.X() += dx;
    1328             :     }
    1329             :     // width does need to be updated for correct fallback
    1330           0 :     mnWidth += dx;
    1331             : }
    1332             : 
    1333           0 : void GraphiteLayout::DropGlyph( int nGlyphIndex )
    1334             : {
    1335           0 :     if(nGlyphIndex >= signed(mvGlyphs.size()))
    1336           0 :         return;
    1337             : 
    1338           0 :     GlyphItem & glyph = mvGlyphs[nGlyphIndex];
    1339           0 :     glyph.maGlyphId = GF_DROPPED;
    1340             : #ifdef GRLAYOUT_DEBUG
    1341             :     fprintf(grLog(),"Dropped %d\n", nGlyphIndex);
    1342             : #endif
    1343             : }
    1344             : 
    1345           0 : void GraphiteLayout::Simplify( bool isBaseLayout )
    1346             : {
    1347           0 :   const sal_GlyphId dropMarker = isBaseLayout ? GF_DROPPED : 0;
    1348             : 
    1349           0 :   Glyphs::iterator gi = mvGlyphs.begin();
    1350             :   // TODO check whether we need to adjust positions here
    1351             :   // MultiSalLayout seems to move the glyphs itself, so it may not be needed.
    1352           0 :   long deltaX = 0;
    1353           0 :   while (gi != mvGlyphs.end())
    1354             :   {
    1355           0 :       if (gi->maGlyphId == dropMarker)
    1356             :       {
    1357           0 :         deltaX += gi->mnNewWidth;
    1358           0 :         gi->mnNewWidth = 0;
    1359             :       }
    1360             :       else
    1361             :       {
    1362           0 :         deltaX = 0;
    1363             :       }
    1364           0 :       ++gi;
    1365             :   }
    1366             : #ifdef GRLAYOUT_DEBUG
    1367             :   fprintf(grLog(),"Simplify base%d dx=%ld newW=%ld\n", isBaseLayout, deltaX, mnWidth - deltaX);
    1368             : #endif
    1369             :   // discard width from trailing dropped glyphs, but not those in the middle
    1370           0 :   mnWidth -= deltaX;
    1371         801 : }
    1372             : 
    1373             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11