LibreOffice Module vcl (master)  1
ImplLayoutArgs.cxx
Go to the documentation of this file.
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 #include <ImplLayoutArgs.hxx>
21 
22 #include <unicode/ubidi.h>
23 #include <unicode/uchar.h>
24 
25 #include <algorithm>
26 #include <memory>
27 
28 namespace vcl::text
29 {
30 ImplLayoutArgs::ImplLayoutArgs(const OUString& rStr, int nMinCharPos, int nEndCharPos,
31  SalLayoutFlags nFlags, const LanguageTag& rLanguageTag,
32  vcl::text::TextLayoutCache const* const pLayoutCache)
33  : maLanguageTag(rLanguageTag)
34  , mnFlags(nFlags)
35  , mrStr(rStr)
36  , mnMinCharPos(nMinCharPos)
37  , mnEndCharPos(nEndCharPos)
38  , m_pTextLayoutCache(pLayoutCache)
39  , mpDXArray(nullptr)
40  , mnLayoutWidth(0)
41  , mnOrientation(0)
42 {
44  {
45  // handle strong BiDi mode
46 
47  // do not bother to BiDi analyze strong LTR/RTL
48  // TODO: can we assume these strings do not have unicode control chars?
49  // if not remove the control characters from the runs
50  bool bRTL(mnFlags & SalLayoutFlags::BiDiRtl);
52  }
53  else
54  {
55  // handle weak BiDi mode
56  UBiDiLevel nLevel = (mnFlags & SalLayoutFlags::BiDiRtl) ? 1 : 0;
57 
58  // prepare substring for BiDi analysis
59  // TODO: reuse allocated pParaBidi
60  UErrorCode rcI18n = U_ZERO_ERROR;
61  const int nLength = mrStr.getLength();
62  UBiDi* pParaBidi = ubidi_openSized(nLength, 0, &rcI18n);
63  if (!pParaBidi)
64  return;
65  ubidi_setPara(pParaBidi, reinterpret_cast<const UChar*>(mrStr.getStr()), nLength, nLevel,
66  nullptr, &rcI18n);
67 
68  UBiDi* pLineBidi = pParaBidi;
69  int nSubLength = mnEndCharPos - mnMinCharPos;
70  if (nSubLength != nLength)
71  {
72  pLineBidi = ubidi_openSized(nSubLength, 0, &rcI18n);
73  ubidi_setLine(pParaBidi, mnMinCharPos, mnEndCharPos, pLineBidi, &rcI18n);
74  }
75 
76  // run BiDi algorithm
77  const int nRunCount = ubidi_countRuns(pLineBidi, &rcI18n);
78  //maRuns.resize( 2 * nRunCount );
79  for (int i = 0; i < nRunCount; ++i)
80  {
81  int32_t nMinPos, nRunLength;
82  const UBiDiDirection nDir = ubidi_getVisualRun(pLineBidi, i, &nMinPos, &nRunLength);
83  const int nPos0 = nMinPos + mnMinCharPos;
84  const int nPos1 = nPos0 + nRunLength;
85 
86  const bool bRTL = (nDir == UBIDI_RTL);
87  AddRun(nPos0, nPos1, bRTL);
88  }
89 
90  // cleanup BiDi engine
91  if (pLineBidi != pParaBidi)
92  ubidi_close(pLineBidi);
93  ubidi_close(pParaBidi);
94  }
95 
96  // prepare calls to GetNextPos/GetNextRun
97  maRuns.ResetPos();
98 }
99 
101 
102 void ImplLayoutArgs::SetDXArray(DeviceCoordinate const* pDXArray) { mpDXArray = pDXArray; }
103 
104 void ImplLayoutArgs::SetOrientation(Degree10 nOrientation) { mnOrientation = nOrientation; }
105 
107 
108 bool ImplLayoutArgs::GetNextPos(int* nCharPos, bool* bRTL)
109 {
110  return maRuns.GetNextPos(nCharPos, bRTL);
111 }
112 
113 void ImplLayoutArgs::AddFallbackRun(int nMinRunPos, int nEndRunPos, bool bRTL)
114 {
115  maFallbackRuns.AddRun(nMinRunPos, nEndRunPos, bRTL);
116 }
117 
119 
120 static bool IsControlChar(sal_UCS4 cChar)
121 {
122  // C0 control characters
123  if ((0x0001 <= cChar) && (cChar <= 0x001F))
124  return true;
125  // formatting characters
126  if ((0x200E <= cChar) && (cChar <= 0x200F))
127  return true;
128  if ((0x2028 <= cChar) && (cChar <= 0x202E))
129  return true;
130  // deprecated formatting characters
131  if ((0x206A <= cChar) && (cChar <= 0x206F))
132  return true;
133  if (0x2060 == cChar)
134  return true;
135  // byte order markers and invalid unicode
136  if ((cChar == 0xFEFF) || (cChar == 0xFFFE) || (cChar == 0xFFFF))
137  return true;
138  return false;
139 }
140 
141 // add a run after splitting it up to get rid of control chars
142 void ImplLayoutArgs::AddRun(int nCharPos0, int nCharPos1, bool bRTL)
143 {
144  SAL_WARN_IF(nCharPos0 > nCharPos1, "vcl", "ImplLayoutArgs::AddRun() nCharPos0>=nCharPos1");
145 
146  // remove control characters from runs by splitting them up
147  if (!bRTL)
148  {
149  for (int i = nCharPos0; i < nCharPos1; ++i)
150  if (IsControlChar(mrStr[i]))
151  {
152  // add run until control char
153  maRuns.AddRun(nCharPos0, i, bRTL);
154  nCharPos0 = i + 1;
155  }
156  }
157  else
158  {
159  for (int i = nCharPos1; --i >= nCharPos0;)
160  if (IsControlChar(mrStr[i]))
161  {
162  // add run until control char
163  maRuns.AddRun(i + 1, nCharPos1, bRTL);
164  nCharPos1 = i;
165  }
166  }
167 
168  // add remainder of run
169  maRuns.AddRun(nCharPos0, nCharPos1, bRTL);
170 }
171 
173 {
174  // Generate runs with pre-calculated glyph items instead maFallbackRuns.
175  if (pGlyphsImpl != nullptr)
176  {
177  maRuns.Clear();
179 
180  for (auto const& aGlyphItem : *pGlyphsImpl)
181  {
182  for (int i = aGlyphItem.charPos(); i < aGlyphItem.charPos() + aGlyphItem.charCount();
183  ++i)
184  maRuns.AddPos(i, aGlyphItem.IsRTLGlyph());
185  }
186 
187  return !maRuns.IsEmpty();
188  }
189 
190  // short circuit if no fallback is needed
191  if (maFallbackRuns.IsEmpty())
192  {
193  maRuns.Clear();
194  return false;
195  }
196 
197  // convert the fallback requests to layout requests
198  bool bRTL;
199  int nMin, nEnd;
200 
201  // get the individual fallback requests
202  std::vector<int> aPosVector;
203  aPosVector.reserve(mrStr.getLength());
205  for (; maFallbackRuns.GetRun(&nMin, &nEnd, &bRTL); maFallbackRuns.NextRun())
206  for (int i = nMin; i < nEnd; ++i)
207  aPosVector.push_back(i);
209 
210  // sort the individual fallback requests
211  std::sort(aPosVector.begin(), aPosVector.end());
212 
213  // adjust fallback runs to have the same order and limits of the original runs
214  ImplLayoutRuns aNewRuns;
215  maRuns.ResetPos();
216  for (; maRuns.GetRun(&nMin, &nEnd, &bRTL); maRuns.NextRun())
217  {
218  if (!bRTL)
219  {
220  auto it = std::lower_bound(aPosVector.begin(), aPosVector.end(), nMin);
221  for (; (it != aPosVector.end()) && (*it < nEnd); ++it)
222  aNewRuns.AddPos(*it, bRTL);
223  }
224  else
225  {
226  auto it = std::upper_bound(aPosVector.begin(), aPosVector.end(), nEnd);
227  while ((it != aPosVector.begin()) && (*--it >= nMin))
228  aNewRuns.AddPos(*it, bRTL);
229  }
230  }
231 
232  maRuns = aNewRuns; // TODO: use vector<>::swap()
233  maRuns.ResetPos();
234  return true;
235 }
236 
237 bool ImplLayoutArgs::GetNextRun(int* nMinRunPos, int* nEndRunPos, bool* bRTL)
238 {
239  bool bValid = maRuns.GetRun(nMinRunPos, nEndRunPos, bRTL);
240  maRuns.NextRun();
241  return bValid;
242 }
243 }
244 
245 std::ostream& operator<<(std::ostream& s, vcl::text::ImplLayoutArgs const& rArgs)
246 {
247 #ifndef SAL_LOG_INFO
248  (void)rArgs;
249 #else
250  s << "ImplLayoutArgs{";
251 
252  s << "Flags=";
253  if (rArgs.mnFlags == SalLayoutFlags::NONE)
254  s << 0;
255  else
256  {
257  bool need_or = false;
258  s << "{";
259 #define TEST(x) \
260  if (rArgs.mnFlags & SalLayoutFlags::x) \
261  { \
262  if (need_or) \
263  s << "|"; \
264  s << #x; \
265  need_or = true; \
266  }
267  TEST(BiDiRtl);
268  TEST(BiDiStrong);
269  TEST(RightAlign);
272  TEST(Vertical);
274  TEST(ForFallback);
275 #undef TEST
276  s << "}";
277  }
278 
279  const int nLength = rArgs.mrStr.getLength();
280 
281  s << ",Length=" << nLength;
282  s << ",MinCharPos=" << rArgs.mnMinCharPos;
283  s << ",EndCharPos=" << rArgs.mnEndCharPos;
284 
285  s << ",Str=\"";
286  int lim = nLength;
287  if (lim > 10)
288  lim = 7;
289  for (int i = 0; i < lim; i++)
290  {
291  if (rArgs.mrStr[i] == '\n')
292  s << "\\n";
293  else if (rArgs.mrStr[i] < ' ' || (rArgs.mrStr[i] >= 0x7F && rArgs.mrStr[i] <= 0xFF))
294  s << "\\0x" << std::hex << std::setw(2) << std::setfill('0')
295  << static_cast<int>(rArgs.mrStr[i]) << std::setfill(' ') << std::setw(1) << std::dec;
296  else if (rArgs.mrStr[i] < 0x7F)
297  s << static_cast<char>(rArgs.mrStr[i]);
298  else
299  s << "\\u" << std::hex << std::setw(4) << std::setfill('0')
300  << static_cast<int>(rArgs.mrStr[i]) << std::setfill(' ') << std::setw(1) << std::dec;
301  }
302  if (nLength > lim)
303  s << "...";
304  s << "\"";
305 
306  s << ",DXArray=";
307  if (rArgs.mpDXArray)
308  {
309  s << "[";
310  int count = rArgs.mnEndCharPos - rArgs.mnMinCharPos;
311  lim = count;
312  if (lim > 10)
313  lim = 7;
314  for (int i = 0; i < lim; i++)
315  {
316  s << rArgs.mpDXArray[i];
317  if (i < lim - 1)
318  s << ",";
319  }
320  if (count > lim)
321  {
322  if (count > lim + 1)
323  s << "...";
324  s << rArgs.mpDXArray[count - 1];
325  }
326  s << "]";
327  }
328  else
329  s << "NULL";
330 
331  s << ",LayoutWidth=" << rArgs.mnLayoutWidth;
332 
333  s << "}";
334 
335 #endif
336  return s;
337 }
338 
339 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ImplLayoutArgs(OUString const &rStr, int nMinCharPos, int nEndCharPos, SalLayoutFlags nFlags, LanguageTag const &rLanguageTag, vcl::text::TextLayoutCache const *pLayoutCache)
sal_uInt32 sal_UCS4
Definition: vclenum.hxx:192
std::basic_ostream< charT, traits > & operator<<(std::basic_ostream< charT, traits > &stream, const Region &rRegion)
Definition: region.hxx:136
bool GetNextRun(int *nMinRunPos, int *nEndRunPos, bool *bRTL)
void SetLayoutWidth(DeviceCoordinate nWidth)
bool PrepareFallback(const SalLayoutGlyphsImpl *pGlyphsImpl)
void SetOrientation(Degree10 nOrientation)
const DeviceCoordinate * mpDXArray
FuncFlags mnFlags
void AddPos(int nCharPos, bool bRTL)
bool IsEmpty() const
TEST
int i
tools::Long DeviceCoordinate
static bool IsControlChar(sal_UCS4 cChar)
bool GetNextPos(int *nCharPos, bool *bRTL)
double mnLayoutWidth
bool GetRun(int *nMinRunPos, int *nEndRunPos, bool *bRTL) const
#define SAL_WARN_IF(condition, area, stream)
void AddRun(int nMinRunPos, int nEndRunPos, bool bRTL)
bool GetNextPos(int *nCharPos, bool *bRTL)
LanguageTag maLanguageTag
void AddFallbackRun(int nMinRunPos, int nEndRunPos, bool bRTL)
sal_Int32 nLength
void SetDXArray(const DeviceCoordinate *pDXArray)
DeviceCoordinate mnLayoutWidth
void AddRun(int nMinCharPos, int nEndCharPos, bool bRTL)
SalLayoutFlags
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo