LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/vcl/source/glyphs - graphite_layout.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 1 612 0.2 %
Date: 2013-07-09 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             : // Header files
      33             : //
      34             : // Standard Library
      35             : #include <algorithm>
      36             : #include <cassert>
      37             : #include <functional>
      38             : #include <limits>
      39             : #include <numeric>
      40             : #include <deque>
      41             : 
      42             : // Platform
      43             : #include <svsys.h>
      44             : 
      45             : #include <salgdi.hxx>
      46             : 
      47             : #include <unicode/uchar.h>
      48             : #include <unicode/ubidi.h>
      49             : #include <unicode/uscript.h>
      50             : 
      51             : // Graphite Libraries (must be after vcl headers on windows)
      52             : #include <graphite2/Segment.h>
      53             : 
      54             : #include <graphite_layout.hxx>
      55             : #include <graphite_features.hxx>
      56             : 
      57             : // Module private type definitions and forward declarations.
      58             : //
      59             : // Module private names.
      60             : //
      61             : 
      62             : #ifdef GRLAYOUT_DEBUG
      63             : static FILE * grLog()
      64             : {
      65             : #ifdef WNT
      66             :     static FILE * grLogFile = NULL;
      67             :     if (grLogFile == NULL)
      68             :     {
      69             :         std::string logFileName(getenv("TEMP"));
      70             :         logFileName.append("/graphitelayout.log");
      71             :         grLogFile = fopen(logFileName.c_str(),"w");
      72             :     }
      73             :     else
      74             :         fflush(grLogFile);
      75             :     return grLogFile;
      76             : #else
      77             :     fflush(stdout);
      78             :     return stdout;
      79             : #endif
      80             : }
      81             : #endif
      82             : 
      83             : namespace
      84             : {
      85           0 :     inline long round(const float n) {
      86           0 :         return long(n + (n < 0 ? -0.5 : 0.5));
      87             :     }
      88             : 
      89             :     template<typename T>
      90           0 :     inline bool in_range(const T i, const T b, const T e) {
      91           0 :         return !(b > i) && i < e;
      92             :     }
      93             : 
      94             :     template<typename T>
      95             :     inline bool is_subrange(const T sb, const T se, const T b, const T e) {
      96             :         return !(b > sb || se > e);
      97             :     }
      98             : 
      99             :     template<typename T>
     100             :     inline bool is_subrange(const std::pair<T, T> &s, const T b, const T e) {
     101             :         return is_subrange(s.first, s.second, b, e);
     102             :     }
     103             : 
     104           0 :     int findSameDirLimit(const sal_Unicode* buffer, int charCount, bool rtl)
     105             :     {
     106           0 :         UErrorCode status = U_ZERO_ERROR;
     107           0 :         UBiDi *ubidi = ubidi_openSized(charCount, 0, &status);
     108           0 :         int limit = 0;
     109             :         ubidi_setPara(ubidi, reinterpret_cast<const UChar *>(buffer), charCount,
     110           0 :             (rtl)?UBIDI_DEFAULT_RTL:UBIDI_DEFAULT_LTR, NULL, &status);
     111           0 :         UBiDiLevel level = 0;
     112           0 :         ubidi_getLogicalRun(ubidi, 0, &limit, &level);
     113           0 :         ubidi_close(ubidi);
     114           0 :         if ((rtl && !(level & 1)) || (!rtl && (level & 1)))
     115             :         {
     116           0 :             limit = 0;
     117             :         }
     118           0 :         return limit;
     119             :     }
     120             : 
     121             :     template <typename T>
     122           0 :     T maximum(T a, T b)
     123             :     {
     124           0 :         return (a > b)? a : b;
     125             :     }
     126             :     template <typename T>
     127           0 :     T minimum(T a, T b)
     128             :     {
     129           0 :         return (a < b)? a : b;
     130             :     }
     131             : 
     132             : } // namespace
     133             : 
     134             : // Impementation of the GraphiteLayout::Glyphs container class.
     135             : //    This is an extended vector class with methods added to enable
     136             : //        o Correctly filling with glyphs.
     137             : //        o Querying clustering relationships.
     138             : //        o manipulations that affect neighouring glyphs.
     139             : 
     140             : const int GraphiteLayout::EXTRA_CONTEXT_LENGTH = 10;
     141             : 
     142             : // find first slot of cluster and first slot of subsequent cluster
     143           0 : static void findFirstClusterSlot(const gr_slot* base, gr_slot const** first, gr_slot const** after, int * firstChar, int * lastChar, bool bRtl)
     144             : {
     145           0 :     if (gr_slot_attached_to(base) == NULL)
     146             :     {
     147           0 :         *first = base;
     148             :         *after = (bRtl)? gr_slot_prev_in_segment(base) :
     149           0 :             gr_slot_next_in_segment(base);
     150           0 :         *firstChar = gr_slot_before(base);
     151           0 :         *lastChar = gr_slot_after(base);
     152             :     }
     153           0 :     const gr_slot * attachment = gr_slot_first_attachment(base);
     154           0 :     while (attachment)
     155             :     {
     156           0 :         if (gr_slot_origin_X(*first) > gr_slot_origin_X(attachment))
     157           0 :             *first = attachment;
     158             :         const gr_slot* attachmentNext = (bRtl)?
     159           0 :             gr_slot_prev_in_segment(attachment) : gr_slot_next_in_segment(attachment);
     160           0 :         if (attachmentNext)
     161             :         {
     162           0 :             if (*after && (gr_slot_origin_X(*after) < gr_slot_origin_X(attachmentNext)))
     163           0 :                 *after = attachmentNext;
     164             :         }
     165             :         else
     166             :         {
     167           0 :             *after = NULL;
     168             :         }
     169           0 :         if (gr_slot_before(attachment) < *firstChar)
     170           0 :             *firstChar = gr_slot_before(attachment);
     171           0 :         if (gr_slot_after(attachment) > *lastChar)
     172           0 :             *lastChar = gr_slot_after(attachment);
     173           0 :         if (gr_slot_first_attachment(attachment))
     174           0 :             findFirstClusterSlot(attachment, first, after, firstChar, lastChar, bRtl);
     175           0 :         attachment = gr_slot_next_sibling_attachment(attachment);
     176             :     }
     177           0 : }
     178             : 
     179             : // The Graphite glyph stream is really a sequence of glyph attachment trees
     180             : //  each rooted at a non-attached base glyph.  fill_from walks the glyph stream,
     181             : //  finds each non-attached base glyph and calls append to record them as a
     182             : //  sequence of clusters.
     183             : void
     184           0 : GraphiteLayout::fillFrom(gr_segment * pSegment, ImplLayoutArgs &rArgs, float fScaling)
     185             : {
     186           0 :     bool bRtl = (rArgs.mnFlags & SAL_LAYOUT_BIDI_RTL);
     187           0 :     int nCharRequested = rArgs.mnEndCharPos - rArgs.mnMinCharPos;
     188           0 :     int nChar = gr_seg_n_cinfo(pSegment);
     189           0 :     float fMinX = gr_seg_advance_X(pSegment);
     190           0 :     float fMaxX = 0.0f;
     191           0 :     long nDxOffset = 0; // from dropped glyphs
     192           0 :     int nFirstCharInCluster = 0;
     193           0 :     int nLastCharInCluster = 0;
     194           0 :     unsigned int nGlyphs = gr_seg_n_slots(pSegment);
     195           0 :     mvGlyph2Char.assign(nGlyphs, -1);
     196           0 :     mvGlyphs.reserve(nGlyphs);
     197             : 
     198           0 :     if (bRtl)
     199             :     {
     200           0 :         const gr_slot* baseSlot = gr_seg_last_slot(pSegment);
     201             :         // find first base
     202           0 :         while (baseSlot && (gr_slot_attached_to(baseSlot) != NULL))
     203           0 :             baseSlot = gr_slot_prev_in_segment(baseSlot);
     204           0 :         int iChar = nChar - 1;
     205           0 :         int iNextChar = nChar - 1;
     206           0 :         bool reordered = false;
     207           0 :         int nBaseGlyphIndex = 0;
     208             :         // now loop over bases
     209           0 :         while (baseSlot)
     210             :         {
     211           0 :             bool bCluster = !reordered;
     212           0 :             const gr_slot * clusterFirst = NULL;
     213           0 :             const gr_slot * clusterAfter = NULL;
     214           0 :             int firstChar = -1;
     215           0 :             int lastChar = -1;
     216           0 :             findFirstClusterSlot(baseSlot, &clusterFirst, &clusterAfter, &firstChar, &lastChar, bRtl);
     217           0 :             iNextChar = minimum<int>(firstChar, iNextChar);
     218           0 :             if (bCluster)
     219             :             {
     220           0 :                 nBaseGlyphIndex = mvGlyphs.size();
     221           0 :                 mvGlyph2Char[nBaseGlyphIndex] = iChar + mnSegCharOffset;
     222           0 :                 nFirstCharInCluster = firstChar;
     223           0 :                 nLastCharInCluster = lastChar;
     224             :             }
     225             :             else
     226             :             {
     227           0 :                 mvGlyph2Char[mvGlyphs.size()] = firstChar + mnSegCharOffset;
     228           0 :                 nFirstCharInCluster = minimum<int>(firstChar, nFirstCharInCluster);
     229           0 :                 nLastCharInCluster = maximum<int>(firstChar, nLastCharInCluster);
     230             :             }
     231           0 :             float leftBoundary = gr_slot_origin_X(clusterFirst);
     232             :             float rightBoundary = (clusterAfter)?
     233           0 :                 gr_slot_origin_X(clusterAfter) : gr_seg_advance_X(pSegment);
     234           0 :             if (
     235           0 :                 lastChar < iChar &&
     236           0 :                  (gr_cinfo_after(gr_seg_cinfo(pSegment, iChar)) >
     237           0 :                  static_cast<int>(gr_slot_index(clusterAfter)))
     238             :                )
     239             :             {
     240           0 :                 reordered = true;
     241             :             }
     242             :             else
     243             :             {
     244           0 :                 reordered = false;
     245           0 :                 iChar = iNextChar - 1;
     246             :             }
     247           0 :             if (mnSegCharOffset + nFirstCharInCluster >= mnMinCharPos &&
     248           0 :                 mnSegCharOffset + nFirstCharInCluster < mnEndCharPos)
     249             :             {
     250           0 :                 fMinX = minimum<float>(fMinX, leftBoundary);
     251           0 :                 fMaxX = maximum<float>(fMaxX, rightBoundary);
     252           0 :                 if (!reordered)
     253             :                 {
     254           0 :                     for (int i = nFirstCharInCluster; i <= nLastCharInCluster; i++)
     255             :                     {
     256           0 :                         if (mnSegCharOffset + i >= mnEndCharPos)
     257           0 :                             break;
     258             :                         // from the point of view of the dx array, the xpos is
     259             :                         // the origin of the first glyph of the cluster rtl
     260           0 :                         mvCharDxs[mnSegCharOffset + i - mnMinCharPos] =
     261           0 :                             static_cast<int>(leftBoundary * fScaling) + nDxOffset;
     262           0 :                         mvCharBreaks[mnSegCharOffset + i - mnMinCharPos] = gr_cinfo_break_weight(gr_seg_cinfo(pSegment, i));
     263             :                     }
     264           0 :                     mvChar2BaseGlyph[mnSegCharOffset + nFirstCharInCluster - mnMinCharPos] = nBaseGlyphIndex;
     265             :                 }
     266             :                 append(pSegment, rArgs, baseSlot, gr_slot_origin_X(baseSlot), rightBoundary, fScaling,
     267           0 :                        nDxOffset, bCluster, mnSegCharOffset + firstChar);
     268             :             }
     269           0 :             if (mnSegCharOffset + nLastCharInCluster < mnMinCharPos)
     270           0 :                 break;
     271           0 :             baseSlot = gr_slot_next_sibling_attachment(baseSlot);
     272             :         }
     273             :     }
     274             :     else
     275             :     {
     276           0 :         const gr_slot* baseSlot = gr_seg_first_slot(pSegment);
     277             :         // find first base
     278           0 :         while (baseSlot && (gr_slot_attached_to(baseSlot) != NULL))
     279           0 :             baseSlot = gr_slot_next_in_segment(baseSlot);
     280           0 :         int iChar = 0; // relative to segment
     281           0 :         int iNextChar = 0;
     282           0 :         bool reordered = false;
     283           0 :         int nBaseGlyphIndex = 0;
     284             :         // now loop over bases
     285           0 :         while (baseSlot)
     286             :         {
     287           0 :             bool bCluster = !reordered;
     288           0 :             const gr_slot * clusterFirst = NULL;
     289           0 :             const gr_slot * clusterAfter = NULL;
     290           0 :             int firstChar = -1;
     291           0 :             int lastChar = -1;
     292           0 :             findFirstClusterSlot(baseSlot, &clusterFirst, &clusterAfter, &firstChar, &lastChar, bRtl);
     293           0 :             iNextChar = maximum<int>(lastChar, iNextChar);
     294           0 :             if (bCluster)
     295             :             {
     296           0 :                 nBaseGlyphIndex = mvGlyphs.size();
     297           0 :                 mvGlyph2Char[nBaseGlyphIndex] = iChar + mnSegCharOffset;
     298           0 :                 nFirstCharInCluster = firstChar;
     299           0 :                 nLastCharInCluster = lastChar;
     300             :             }
     301             :             else
     302             :             {
     303           0 :                 mvGlyph2Char[mvGlyphs.size()] = firstChar + mnSegCharOffset;
     304           0 :                 nFirstCharInCluster = minimum<int>(firstChar, nFirstCharInCluster);
     305           0 :                 nLastCharInCluster = maximum<int>(lastChar, nLastCharInCluster);
     306             :             }
     307           0 :             if (
     308           0 :                 firstChar > iChar &&
     309           0 :                  (gr_cinfo_before(gr_seg_cinfo(pSegment, iChar)) >
     310           0 :                  static_cast<int>(gr_slot_index(clusterFirst)))
     311             :                )
     312             :             {
     313           0 :                 reordered = true;
     314             :             }
     315             :             else
     316             :             {
     317           0 :                 reordered = false;
     318           0 :                 iChar = iNextChar + 1;
     319             :             }
     320           0 :             float leftBoundary = gr_slot_origin_X(clusterFirst);
     321             :             float rightBoundary = (clusterAfter)?
     322           0 :                 gr_slot_origin_X(clusterAfter) : gr_seg_advance_X(pSegment);
     323           0 :             int bFirstChar = gr_cinfo_base(gr_seg_cinfo(pSegment, nFirstCharInCluster));
     324           0 :             if (mnSegCharOffset + bFirstChar >= mnMinCharPos &&
     325           0 :                 mnSegCharOffset + bFirstChar < mnEndCharPos)
     326             :             {
     327           0 :                 fMinX = minimum<float>(fMinX, leftBoundary);
     328           0 :                 fMaxX = maximum<float>(fMaxX, rightBoundary);
     329           0 :                 if (!reordered)
     330             :                 {
     331           0 :                     for (int i = nFirstCharInCluster; i <= nLastCharInCluster; i++)
     332             :                     {
     333           0 :                         int ibase = gr_cinfo_base(gr_seg_cinfo(pSegment, i));
     334           0 :                         if (mnSegCharOffset + ibase >= mnEndCharPos)
     335           0 :                             break;
     336             :                         // from the point of view of the dx array, the xpos is
     337             :                         // the origin of the first glyph of the next cluster ltr
     338           0 :                         mvCharDxs[mnSegCharOffset + ibase - mnMinCharPos] =
     339           0 :                             static_cast<int>(rightBoundary * fScaling) + nDxOffset;
     340           0 :                         mvCharBreaks[mnSegCharOffset + ibase - mnMinCharPos] = gr_cinfo_break_weight(gr_seg_cinfo(pSegment, i));
     341             :                     }
     342             :                     // only set mvChar2BaseGlyph for first character of cluster
     343           0 :                     mvChar2BaseGlyph[mnSegCharOffset + bFirstChar - mnMinCharPos] = nBaseGlyphIndex;
     344             :                 }
     345             :                 append(pSegment, rArgs, baseSlot, gr_slot_origin_X(baseSlot), rightBoundary, fScaling,
     346           0 :                        nDxOffset, true, mnSegCharOffset + firstChar);
     347             :             }
     348           0 :             if (mnSegCharOffset + bFirstChar >= mnEndCharPos)
     349           0 :                 break;
     350           0 :             baseSlot = gr_slot_next_sibling_attachment(baseSlot);
     351             :         }
     352             :     }
     353           0 :     long nXOffset = round(fMinX * fScaling);
     354           0 :     mnWidth = round(fMaxX * fScaling) - nXOffset + nDxOffset;
     355           0 :     if (mnWidth < 0)
     356             :     {
     357             :         // This can happen when there was no base inside the range
     358           0 :         mnWidth = 0;
     359             :     }
     360             :     // fill up non-base char dx with cluster widths from previous base glyph
     361           0 :     if (bRtl)
     362             :     {
     363           0 :         if (mvCharDxs[nCharRequested-1] == -1)
     364           0 :             mvCharDxs[nCharRequested-1] = 0;
     365             :         else
     366           0 :             mvCharDxs[nCharRequested-1] -= nXOffset;
     367           0 :         for (int i = nCharRequested - 2; i >= 0; i--)
     368             :         {
     369           0 :             if (mvCharDxs[i] == -1) mvCharDxs[i] = mvCharDxs[i+1];
     370           0 :             else mvCharDxs[i] -= nXOffset;
     371             :         }
     372             :     }
     373             :     else
     374             :     {
     375           0 :         if (mvCharDxs[0] == -1)
     376           0 :             mvCharDxs[0] = 0;
     377             :         else
     378           0 :             mvCharDxs[0] -= nXOffset;
     379           0 :         for (int i = 1; i < nCharRequested; i++)
     380             :         {
     381           0 :             if (mvCharDxs[i] == -1) mvCharDxs[i] = mvCharDxs[i-1];
     382           0 :             else mvCharDxs[i] -= nXOffset;
     383             : #ifdef GRLAYOUT_DEBUG
     384             :             fprintf(grLog(),"%d,%d ", (int)i, (int)mvCharDxs[i]);
     385             : #endif
     386             :         }
     387             :     }
     388             :     // remove offset due to context if there is one
     389           0 :     if (nXOffset != 0)
     390             :     {
     391           0 :         for (size_t i = 0; i < mvGlyphs.size(); i++)
     392           0 :             mvGlyphs[i].maLinearPos.X() -= nXOffset;
     393             :     }
     394             : #ifdef GRLAYOUT_DEBUG
     395             :     fprintf(grLog(), "fillFrom %" SAL_PRI_SIZET "u glyphs offset %ld width %ld\n", mvGlyphs.size(), nXOffset, mnWidth);
     396             : #endif
     397           0 : }
     398             : 
     399             : // append walks an attachment tree, flattening it, and converting it into a
     400             : // sequence of GlyphItem objects which we can later manipulate.
     401             : float
     402           0 : GraphiteLayout::append(gr_segment *pSeg, ImplLayoutArgs &rArgs,
     403             :     const gr_slot * gi, float gOrigin, float nextGlyphOrigin, float scaling, long & rDXOffset,
     404             :     bool bIsBase, int baseChar)
     405             : {
     406           0 :     bool bRtl = (rArgs.mnFlags & SAL_LAYOUT_BIDI_RTL);
     407             :     float nextOrigin;
     408             :     assert(gi);
     409             :     assert(gr_slot_before(gi) <= gr_slot_after(gi));
     410           0 :     int firstChar = gr_slot_before(gi) + mnSegCharOffset;
     411             :     assert(mvGlyphs.size() < mvGlyph2Char.size());
     412           0 :     if (!bIsBase) mvGlyph2Char[mvGlyphs.size()] = baseChar;//firstChar;
     413             :     // is the next glyph attached or in the next cluster?
     414             :     //glyph_set_range_t iAttached = gi.attachedClusterGlyphs();
     415           0 :     const gr_slot * pFirstAttached = gr_slot_first_attachment(gi);
     416           0 :     const gr_slot * pNextSibling = gr_slot_next_sibling_attachment(gi);
     417           0 :     if (pFirstAttached)
     418           0 :         nextOrigin = gr_slot_origin_X(pFirstAttached);
     419           0 :     else if (!bIsBase && pNextSibling)
     420           0 :         nextOrigin = gr_slot_origin_X(pNextSibling);
     421             :     else
     422           0 :         nextOrigin = nextGlyphOrigin;
     423           0 :     long glyphId = gr_slot_gid(gi);
     424           0 :     long deltaOffset = 0;
     425           0 :     int scaledGlyphPos = round(gr_slot_origin_X(gi) * scaling);
     426           0 :     int glyphWidth = round((nextOrigin - gOrigin) * scaling);
     427             : //    if (glyphWidth < 0)
     428             : //    {
     429             : //        nextOrigin = gOrigin;
     430             : //        glyphWidth = 0;
     431             : //    }
     432             : #ifdef GRLAYOUT_DEBUG
     433             :     fprintf(grLog(),"c%d g%ld,X%d W%d nX%f ", firstChar, glyphId,
     434             :         (int)(gr_slot_origin_X(gi) * scaling), glyphWidth, nextOrigin * scaling);
     435             : #endif
     436           0 :     if (glyphId == 0)
     437             :     {
     438           0 :         rArgs.NeedFallback(firstChar, bRtl);
     439           0 :         if( (SAL_LAYOUT_FOR_FALLBACK & rArgs.mnFlags ))
     440             :         {
     441           0 :             glyphId = GF_DROPPED;
     442           0 :             deltaOffset -= glyphWidth;
     443           0 :             glyphWidth = 0;
     444             :         }
     445             :     }
     446           0 :     else if(rArgs.mnFlags & SAL_LAYOUT_FOR_FALLBACK)
     447             :     {
     448             : #ifdef GRLAYOUT_DEBUG
     449             :         fprintf(grLog(),"fallback c%d %x in run %d\n", firstChar, rArgs.mpStr[firstChar],
     450             :             rArgs.maRuns.PosIsInAnyRun(firstChar));
     451             : #endif
     452             :         // glyphs that aren't requested for fallback will be taken from base
     453             :         // layout, so mark them as dropped (should this wait until Simplify(false) is called?)
     454           0 :         if (!rArgs.maRuns.PosIsInAnyRun(firstChar) &&
     455           0 :             in_range(firstChar, rArgs.mnMinCharPos, rArgs.mnEndCharPos))
     456             :         {
     457           0 :             glyphId = GF_DROPPED;
     458           0 :             deltaOffset -= glyphWidth;
     459           0 :             glyphWidth = 0;
     460             :         }
     461             :     }
     462             :     // append this glyph. Set the cluster flag if this glyph is attached to another
     463           0 :     long nGlyphFlags = bIsBase ? 0 : GlyphItem::IS_IN_CLUSTER;
     464           0 :     nGlyphFlags |= (bRtl)? GlyphItem::IS_RTL_GLYPH : 0;
     465           0 :     GlyphItem aGlyphItem(mvGlyphs.size(),
     466             :         glyphId,
     467             :         Point(scaledGlyphPos + rDXOffset,
     468           0 :             round((-gr_slot_origin_Y(gi) * scaling))),
     469             :         nGlyphFlags,
     470           0 :         glyphWidth);
     471           0 :     if (glyphId != static_cast<long>(GF_DROPPED))
     472           0 :         aGlyphItem.mnOrigWidth = round(gr_slot_advance_X(gi, mpFace, mpFont) * scaling);
     473           0 :     mvGlyphs.push_back(aGlyphItem);
     474             : 
     475             :     // update the offset if this glyph was dropped
     476           0 :     rDXOffset += deltaOffset;
     477             : 
     478             :     // Recursively append all the attached glyphs.
     479           0 :     float cOrigin = nextOrigin;
     480           0 :     for (const gr_slot * agi = gr_slot_first_attachment(gi); agi != NULL; agi = gr_slot_next_sibling_attachment(agi))
     481           0 :         cOrigin = append(pSeg, rArgs, agi, cOrigin, nextGlyphOrigin, scaling, rDXOffset, false, baseChar);
     482             : 
     483           0 :     return cOrigin;
     484             : }
     485             : 
     486             : //
     487             : // An implementation of the SalLayout interface to enable Graphite enabled fonts to be used.
     488             : //
     489           0 : GraphiteLayout::GraphiteLayout(const gr_face * face, gr_font * font,
     490             :                                const grutils::GrFeatureParser * pFeatures) throw()
     491             :   : mpFace(face),
     492             :     mpFont(font),
     493             :     mnWidth(0),
     494             :     mfScaling(1.0),
     495           0 :     mpFeatures(pFeatures)
     496             : {
     497             : 
     498           0 : }
     499             : 
     500           0 : GraphiteLayout::~GraphiteLayout() throw()
     501             : {
     502           0 :     clear();
     503             :     // the features and font are owned by the platform layers
     504           0 :     mpFeatures = NULL;
     505           0 :     mpFont = NULL;
     506           0 : }
     507             : 
     508           0 : void GraphiteLayout::clear()
     509             : {
     510             :     // Destroy the segment and text source from any previous invocation of
     511             :     // LayoutText
     512           0 :     mvGlyphs.clear();
     513           0 :     mvCharDxs.clear();
     514           0 :     mvChar2BaseGlyph.clear();
     515           0 :     mvGlyph2Char.clear();
     516             : 
     517             :     // Reset the state to the empty state.
     518           0 :     mnWidth = 0;
     519             :     // Don't reset the scaling, because it is set before LayoutText
     520           0 : }
     521             : 
     522             : // This method shouldn't be called on windows, since it needs the dc reset
     523           0 : bool GraphiteLayout::LayoutText(ImplLayoutArgs & rArgs)
     524             : {
     525           0 :     bool success = true;
     526           0 :     if (rArgs.mnMinCharPos < rArgs.mnEndCharPos)
     527             :     {
     528           0 :         gr_segment * pSegment = CreateSegment(rArgs);
     529           0 :         if (!pSegment)
     530           0 :             return false;
     531           0 :         success = LayoutGlyphs(rArgs, pSegment);
     532           0 :         if (pSegment)
     533             :         {
     534           0 :             gr_seg_destroy(pSegment);
     535           0 :             pSegment = NULL;
     536             :         }
     537             :     }
     538             :     else
     539             :     {
     540           0 :         clear();
     541             :     }
     542           0 :     return success;
     543             : }
     544             : 
     545             : 
     546           0 : gr_segment * GraphiteLayout::CreateSegment(ImplLayoutArgs& rArgs)
     547             : {
     548             :     assert(rArgs.mnLength >= 0);
     549             : 
     550           0 :     gr_segment * pSegment = NULL;
     551             : 
     552             :     // Set the SalLayouts values to be the initial ones.
     553           0 :     SalLayout::AdjustLayout(rArgs);
     554             :     // TODO check if this is needed
     555           0 :     if (mnUnitsPerPixel > 1)
     556           0 :         mfScaling = 1.0f / mnUnitsPerPixel;
     557             : 
     558             :     // Clear out any previous buffers
     559           0 :     clear();
     560           0 :     bool bRtl = mnLayoutFlags & SAL_LAYOUT_BIDI_RTL;
     561             :     try
     562             :     {
     563             :         // Don't set RTL if font doesn't support it otherwise it forces rtl on
     564             :         // everything
     565             :         //if (bRtl && (mrFont.getSupportedScriptDirections() & gr::kfsdcHorizRtl))
     566             :         //    maLayout.setRightToLeft(bRtl);
     567             : 
     568             :         // Context is often needed beyond the specified end, however, we don't
     569             :         // want it if there has been a direction change, since it is hard
     570             :         // to tell between reordering within one direction and multi-directional
     571             :         // text. Extra context, can also cause problems with ligatures stradling
     572             :         // a hyphenation point, so disable if CTL is disabled.
     573           0 :         mnSegCharOffset = rArgs.mnMinCharPos;
     574           0 :         int limit = rArgs.mnEndCharPos;
     575           0 :         if (!(SAL_LAYOUT_COMPLEX_DISABLED & rArgs.mnFlags))
     576             :         {
     577           0 :             const int nSegCharMin = maximum<int>(0, mnMinCharPos - EXTRA_CONTEXT_LENGTH);
     578           0 :             const int nSegCharLimit = minimum(rArgs.mnLength, mnEndCharPos + EXTRA_CONTEXT_LENGTH);
     579           0 :             if (nSegCharMin < mnSegCharOffset)
     580             :             {
     581             :                 int sameDirEnd = findSameDirLimit(rArgs.mpStr + nSegCharMin,
     582           0 :                     rArgs.mnEndCharPos - nSegCharMin, bRtl);
     583           0 :                 if (sameDirEnd == rArgs.mnEndCharPos)
     584           0 :                     mnSegCharOffset = nSegCharMin;
     585             :             }
     586           0 :             if (nSegCharLimit > limit)
     587             :             {
     588             :                 limit += findSameDirLimit(rArgs.mpStr + rArgs.mnEndCharPos,
     589           0 :                     nSegCharLimit - rArgs.mnEndCharPos, bRtl);
     590             :             }
     591             :         }
     592           0 :         int numchars = gr_count_unicode_characters(gr_utf16, rArgs.mpStr + mnSegCharOffset,
     593           0 :                 rArgs.mpStr + (rArgs.mnLength > limit + 64 ? limit + 64 : rArgs.mnLength), NULL);
     594           0 :         if (rArgs.mnMinCharPos + numchars > limit) numchars = limit - rArgs.mnMinCharPos; // fdo#52540
     595           0 :         if (mpFeatures)
     596           0 :             pSegment = gr_make_seg(mpFont, mpFace, 0, mpFeatures->values(), gr_utf16,
     597           0 :                                         rArgs.mpStr + mnSegCharOffset, numchars, bRtl);
     598             :         else
     599             :             pSegment = gr_make_seg(mpFont, mpFace, 0, NULL, gr_utf16,
     600           0 :                                         rArgs.mpStr + mnSegCharOffset, numchars, bRtl);
     601             : 
     602             :         //pSegment = new gr::RangeSegment((gr::Font *)&mrFont, mpTextSrc, &maLayout, mnMinCharPos, limit);
     603           0 :         if (pSegment != NULL)
     604             :         {
     605             : #ifdef GRLAYOUT_DEBUG
     606             :             fprintf(grLog(),"Gr::LayoutText %d-%d, context %d, len %d, numchars %d, rtl %d scaling %f:", rArgs.mnMinCharPos,
     607             :                rArgs.mnEndCharPos, limit, rArgs.mnLength, numchars, bRtl, mfScaling);
     608             :             for (int i = mnSegCharOffset; i < limit; ++i)
     609             :                 fprintf(grLog(), " %04X", rArgs.mpStr[i]);
     610             :             fprintf(grLog(), "\n");
     611             : #endif
     612             :         }
     613             :         else
     614             :         {
     615             : #ifdef GRLAYOUT_DEBUG
     616             :             fprintf(grLog(), "Gr::LayoutText failed: ");
     617             :             for (int i = mnMinCharPos; i < limit; i++)
     618             :             {
     619             :                 fprintf(grLog(), "%04x ", rArgs.mpStr[i]);
     620             :             }
     621             :             fprintf(grLog(), "\n");
     622             : #endif
     623           0 :             clear();
     624           0 :             return NULL;
     625             :         }
     626             :     }
     627           0 :     catch (...)
     628             :     {
     629           0 :         clear();  // destroy the text source and any partially built segments.
     630           0 :         return NULL;
     631             :     }
     632           0 :     return pSegment;
     633             : }
     634             : 
     635           0 : bool GraphiteLayout::LayoutGlyphs(ImplLayoutArgs& rArgs, gr_segment * pSegment)
     636             : {
     637             :     // Calculate the initial character dxs.
     638           0 :     mvCharDxs.assign(mnEndCharPos - mnMinCharPos, -1);
     639           0 :     mvChar2BaseGlyph.assign(mnEndCharPos - mnMinCharPos, -1);
     640           0 :     mvCharBreaks.assign(mnEndCharPos - mnMinCharPos, 0);
     641           0 :     mnWidth = 0;
     642           0 :     if (mvCharDxs.size() > 0)
     643             :     {
     644             :         // Discover all the clusters.
     645             :         try
     646             :         {
     647           0 :             bool bRtl = mnLayoutFlags & SAL_LAYOUT_BIDI_RTL;
     648           0 :             fillFrom(pSegment, rArgs, mfScaling);
     649             : 
     650           0 :             if (bRtl)
     651             :             {
     652             :                 // not needed for adjacent differences, but for mouse clicks to char
     653             :                 std::transform(mvCharDxs.begin(), mvCharDxs.end(), mvCharDxs.begin(),
     654           0 :                     std::bind1st(std::minus<long>(), mnWidth));
     655             :                 // fixup last dx to ensure it always equals the width
     656           0 :                 mvCharDxs[mvCharDxs.size() - 1] = mnWidth;
     657             :             }
     658             :         }
     659           0 :         catch (const std::exception &e)
     660             :         {
     661             : #ifdef GRLAYOUT_DEBUG
     662             :             fprintf(grLog(),"LayoutGlyphs failed %s\n", e.what());
     663             : #else
     664             :             (void)e;
     665             : #endif
     666           0 :             return false;
     667             :         }
     668           0 :         catch (...)
     669             :         {
     670             : #ifdef GRLAYOUT_DEBUG
     671             :             fprintf(grLog(),"LayoutGlyphs failed with exception");
     672             : #endif
     673           0 :             return false;
     674             :         }
     675             :     }
     676             :     else
     677             :     {
     678           0 :         mnWidth = 0;
     679             :     }
     680           0 :     return true;
     681             : }
     682             : 
     683           0 : int GraphiteLayout::GetTextBreak(long maxmnWidth, long char_extra, int factor) const
     684             : {
     685             : #ifdef GRLAYOUT_DEBUG
     686             :     fprintf(grLog(),"Gr::GetTextBreak c[%d-%d) maxWidth %ld char extra %ld factor %d\n",
     687             :         mnMinCharPos, mnEndCharPos, maxmnWidth, char_extra, factor);
     688             : #endif
     689             : 
     690             :     // return quickly if this segment is narrower than the target width
     691           0 :     if (maxmnWidth > mnWidth * factor + char_extra * (mnEndCharPos - mnMinCharPos - 1))
     692           0 :         return STRING_LEN;
     693             : 
     694           0 :     long nWidth = mvCharDxs[0] * factor;
     695           0 :     long wLastBreak = 0;
     696           0 :     int nLastBreak = -1;
     697           0 :     int nEmergency = -1;
     698           0 :     for (size_t i = 1; i < mvCharDxs.size(); i++)
     699             :     {
     700           0 :         nWidth += char_extra;
     701           0 :         if (nWidth > maxmnWidth) break;
     702           0 :         if (mvChar2BaseGlyph[i] != -1)
     703             :         {
     704           0 :             if (
     705           0 :                 (mvCharBreaks[i] > -35 || (mvCharBreaks[i-1] > 0 && mvCharBreaks[i-1] < 35)) &&
     706           0 :                 (mvCharBreaks[i-1] < 35 || (mvCharBreaks[i] < 0 && mvCharBreaks[i] > -35))
     707             :                )
     708             :             {
     709           0 :                 nLastBreak = static_cast<int>(i);
     710           0 :                 wLastBreak = nWidth;
     711             :             }
     712           0 :             nEmergency = static_cast<int>(i);
     713             :         }
     714           0 :         nWidth += (mvCharDxs[i] - mvCharDxs[i-1]) * factor;
     715             :     }
     716           0 :     int nBreak = mnMinCharPos;
     717           0 :     if (wLastBreak > 9 * maxmnWidth / 10)
     718           0 :         nBreak += nLastBreak;
     719             :     else
     720           0 :         if (nEmergency > -1)
     721           0 :             nBreak += nEmergency;
     722             : 
     723             : #ifdef GRLAYOUT_DEBUG
     724             :     fprintf(grLog(), "Gr::GetTextBreak break after %d, weights(%d, %d)\n", nBreak - mnMinCharPos, mvCharBreaks[nBreak - mnMinCharPos], mvCharBreaks[nBreak - mnMinCharPos - 1]);
     725             : #endif
     726             : 
     727           0 :     if (nBreak > mnEndCharPos) nBreak = STRING_LEN;
     728           0 :     else if (nBreak < mnMinCharPos) nBreak = mnMinCharPos;
     729           0 :     return nBreak;
     730             : }
     731             : 
     732           0 : long GraphiteLayout::FillDXArray( sal_Int32* pDXArray ) const
     733             : {
     734           0 :     if (mnEndCharPos == mnMinCharPos)
     735             :         // Then we must be zero width!
     736           0 :         return 0;
     737             : 
     738           0 :     if (pDXArray)
     739             :     {
     740           0 :         for (size_t i = 0; i < mvCharDxs.size(); i++)
     741             :         {
     742             :             assert( (mvChar2BaseGlyph[i] == -1) ||
     743             :                 ((signed)(mvChar2BaseGlyph[i]) < (signed)mvGlyphs.size()));
     744           0 :             if (mvChar2BaseGlyph[i] != -1 &&
     745           0 :                 mvGlyphs[mvChar2BaseGlyph[i]].mnGlyphIndex == GF_DROPPED)
     746             :             {
     747             :                 // when used in MultiSalLayout::GetTextBreak dropped glyphs
     748             :                 // must have zero width
     749           0 :                 pDXArray[i] = 0;
     750             :             }
     751             :             else
     752             :             {
     753           0 :                 pDXArray[i] = mvCharDxs[i];
     754           0 :                 if (i > 0) pDXArray[i] -= mvCharDxs[i-1];
     755             :             }
     756             : #ifdef GRLAYOUT_DEBUG
     757             :             fprintf(grLog(),"%d,%d,%d ", (int)i, (int)mvCharDxs[i], pDXArray[i]);
     758             : #endif
     759             :         }
     760             :         //std::adjacent_difference(mvCharDxs.begin(), mvCharDxs.end(), pDXArray);
     761             :         //for (size_t i = 0; i < mvCharDxs.size(); i++)
     762             :         //    fprintf(grLog(),"%d,%d,%d ", (int)i, (int)mvCharDxs[i], pDXArray[i]);
     763             :         //fprintf(grLog(),"FillDX %ld,%d\n", mnWidth, std::accumulate(pDXArray, pDXArray + mvCharDxs.size(), 0));
     764             :     }
     765             : #ifdef GRLAYOUT_DEBUG
     766             :     fprintf(grLog(),"FillDXArray %d-%d=%ld\n", mnMinCharPos, mnEndCharPos, mnWidth);
     767             : #endif
     768           0 :     return mnWidth;
     769             : }
     770             : 
     771           0 : void  GraphiteLayout::AdjustLayout(ImplLayoutArgs& rArgs)
     772             : {
     773           0 :     SalLayout::AdjustLayout(rArgs);
     774           0 :     if(rArgs.mpDXArray)
     775             :     {
     776           0 :         std::vector<int> vDeltaWidths(mvGlyphs.size(), 0);
     777           0 :         ApplyDXArray(rArgs, vDeltaWidths);
     778             : 
     779           0 :         if( (mnLayoutFlags & SAL_LAYOUT_BIDI_RTL) &&
     780           0 :            !(rArgs.mnFlags & SAL_LAYOUT_FOR_FALLBACK) )
     781             :         {
     782             :             // check if this is a kashida script
     783           0 :             bool bKashidaScript = false;
     784           0 :             for (int i = rArgs.mnMinCharPos; i < rArgs.mnEndCharPos; i++)
     785             :             {
     786           0 :                 UErrorCode aStatus = U_ZERO_ERROR;
     787           0 :                 UScriptCode scriptCode = uscript_getScript(rArgs.mpStr[i], &aStatus);
     788           0 :                 if (scriptCode == USCRIPT_ARABIC || scriptCode == USCRIPT_SYRIAC)
     789             :                 {
     790           0 :                     bKashidaScript = true;
     791           0 :                     break;
     792             :                 }
     793             :             }
     794           0 :             int nKashidaWidth = 0;
     795           0 :             int nKashidaIndex = getKashidaGlyph(nKashidaWidth);
     796           0 :             if( nKashidaIndex != 0 && bKashidaScript)
     797             :             {
     798           0 :                 kashidaJustify( vDeltaWidths, nKashidaIndex, nKashidaWidth );
     799             :             }
     800           0 :         }
     801             :     }
     802           0 :     else if (rArgs.mnLayoutWidth > 0)
     803             :     {
     804             : #ifdef GRLAYOUT_DEBUG
     805             :         fprintf(grLog(), "AdjustLayout width %ld=>%ld\n", mnWidth, rArgs.mnLayoutWidth);
     806             : #endif
     807           0 :         expandOrCondense(rArgs);
     808             :     }
     809           0 : }
     810             : 
     811           0 : void GraphiteLayout::expandOrCondense(ImplLayoutArgs &rArgs)
     812             : {
     813           0 :     int nDeltaWidth = rArgs.mnLayoutWidth - mnWidth;
     814           0 :     if (nDeltaWidth > 0) // expand, just expand between clusters
     815             :     {
     816             :         // NOTE: for expansion we can use base glyphs (which have IsClusterStart set)
     817             :         // even though they may have been reordered in which case they will have
     818             :         // been placed in a bigger cluster for other purposes.
     819           0 :         int nClusterCount = 0;
     820           0 :         for (size_t j = 0; j < mvGlyphs.size(); j++)
     821             :         {
     822           0 :             if (mvGlyphs[j].IsClusterStart())
     823             :             {
     824           0 :                 ++nClusterCount;
     825             :             }
     826             :         }
     827           0 :         if (nClusterCount > 1)
     828             :         {
     829           0 :             float fExtraPerCluster = static_cast<float>(nDeltaWidth) / static_cast<float>(nClusterCount - 1);
     830           0 :             int nCluster = 0;
     831           0 :             int nOffset = 0;
     832           0 :             for (size_t i = 0; i < mvGlyphs.size(); i++)
     833             :             {
     834           0 :                 if (mvGlyphs[i].IsClusterStart())
     835             :                 {
     836           0 :                     nOffset = static_cast<int>(fExtraPerCluster * nCluster);
     837           0 :                     int nCharIndex = mvGlyph2Char[i];
     838             :                     assert(nCharIndex > -1);
     839           0 :                     if (nCharIndex < mnMinCharPos ||
     840           0 :                         static_cast<size_t>(nCharIndex-mnMinCharPos)
     841           0 :                             >= mvCharDxs.size())
     842             :                     {
     843           0 :                         continue;
     844             :                     }
     845           0 :                     mvCharDxs[nCharIndex-mnMinCharPos] += nOffset;
     846             :                     // adjust char dxs for rest of characters in cluster
     847           0 :                     while (++nCharIndex - mnMinCharPos < static_cast<int>(mvChar2BaseGlyph.size()))
     848             :                     {
     849           0 :                         int nChar2Base = mvChar2BaseGlyph[nCharIndex-mnMinCharPos];
     850           0 :                         if (nChar2Base == -1 || nChar2Base == static_cast<int>(i))
     851           0 :                             mvCharDxs[nCharIndex-mnMinCharPos] += nOffset;
     852             :                         else
     853             :                             break;
     854             :                     }
     855           0 :                     ++nCluster;
     856             :                 }
     857           0 :                 mvGlyphs[i].maLinearPos.X() += nOffset;
     858             :             }
     859             :         }
     860             :     }
     861           0 :     else if (nDeltaWidth < 0)// condense - apply a factor to all glyph positions
     862             :     {
     863           0 :         if (mvGlyphs.empty()) return;
     864           0 :         Glyphs::iterator iLastGlyph = mvGlyphs.begin() + (mvGlyphs.size() - 1);
     865             :         // position last glyph using original width
     866           0 :         float fXFactor = static_cast<float>(rArgs.mnLayoutWidth - iLastGlyph->mnOrigWidth) / static_cast<float>(iLastGlyph->maLinearPos.X());
     867             : #ifdef GRLAYOUT_DEBUG
     868             :         fprintf(grLog(), "Condense by factor %f last x%ld\n", fXFactor, iLastGlyph->maLinearPos.X());
     869             : #endif
     870           0 :         if (fXFactor < 0)
     871           0 :             return; // probably a bad mnOrigWidth value
     872           0 :         iLastGlyph->maLinearPos.X() = rArgs.mnLayoutWidth - iLastGlyph->mnOrigWidth;
     873           0 :         Glyphs::iterator iGlyph = mvGlyphs.begin();
     874           0 :         while (iGlyph != iLastGlyph)
     875             :         {
     876           0 :             iGlyph->maLinearPos.X() = static_cast<int>(static_cast<float>(iGlyph->maLinearPos.X()) * fXFactor);
     877           0 :             ++iGlyph;
     878             :         }
     879           0 :         for (size_t i = 0; i < mvCharDxs.size(); i++)
     880             :         {
     881           0 :             mvCharDxs[i] = static_cast<int>(fXFactor * static_cast<float>(mvCharDxs[i]));
     882             :         }
     883             :     }
     884           0 :     mnWidth = rArgs.mnLayoutWidth;
     885             : }
     886             : 
     887           0 : void GraphiteLayout::ApplyDXArray(ImplLayoutArgs &args, std::vector<int> & rDeltaWidth)
     888             : {
     889           0 :     const size_t nChars = args.mnEndCharPos - args.mnMinCharPos;
     890           0 :     if (nChars == 0) return;
     891             : 
     892             : #ifdef GRLAYOUT_DEBUG
     893             :     for (size_t iDx = 0; iDx < mvCharDxs.size(); iDx++)
     894             :          fprintf(grLog(),"%d,%d,%d ", (int)iDx, (int)mvCharDxs[iDx], args.mpDXArray[iDx]);
     895             :     fprintf(grLog(),"ApplyDx\n");
     896             : #endif
     897           0 :     bool bRtl = mnLayoutFlags & SAL_LAYOUT_BIDI_RTL;
     898           0 :     int nXOffset = 0;
     899           0 :     if (bRtl)
     900             :     {
     901           0 :         nXOffset = args.mpDXArray[nChars - 1] - mvCharDxs[nChars - 1];
     902             :     }
     903           0 :     int nPrevClusterGlyph = (bRtl)? (signed)mvGlyphs.size() : -1;
     904           0 :     int nPrevClusterLastChar = -1;
     905           0 :     for (size_t i = 0; i < nChars; i++)
     906             :     {
     907           0 :         int nChar2Base = mvChar2BaseGlyph[i];
     908           0 :         if ((nChar2Base > -1) && (nChar2Base != nPrevClusterGlyph))
     909             :         {
     910             :             assert((nChar2Base > -1) && (nChar2Base < (signed)mvGlyphs.size()));
     911           0 :             GlyphItem & gi = mvGlyphs[nChar2Base];
     912           0 :             if (!gi.IsClusterStart())
     913           0 :                 continue;
     914             : 
     915             :             // find last glyph of this cluster
     916           0 :             size_t j = i + 1;
     917           0 :             int nLastChar = i;
     918           0 :             int nLastGlyph = nChar2Base;
     919           0 :             int nChar2BaseJ = -1;
     920           0 :             for (; j < nChars; j++)
     921             :             {
     922           0 :                 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.mnGlyphIndex != 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 %d(%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).mnGlyphIndex ) )
    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).mnGlyphIndex);
    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, sal_Int32* 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 & SAL_LAYOUT_BIDI_RTL);//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.mnGlyphIndex == 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,%d-%d\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, sal_Int32 * 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)->mnGlyphIndex == 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->mnGlyphIndex;
    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->mnGlyphIndex,
    1277             :             mvGlyph2Char[glyph_slot-1], glyph_itr->maLinearPos.X(), glyph_itr->maLinearPos.Y(), 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->mnGlyphIndex == 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)->mnGlyphIndex == 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 actualy 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].mnGlyphIndex == 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.mnGlyphIndex = 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->mnGlyphIndex == 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         465 : }
    1372             : 
    1373             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10