2016-06-14 14:38:12 +05:30
|
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
|
/*
|
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*
|
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
|
*
|
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "CommonSalLayout.hxx"
|
|
|
|
|
|
|
|
|
|
#include <vcl/unohelp.hxx>
|
|
|
|
|
#include <scrptrun.h>
|
|
|
|
|
#include <com/sun/star/i18n/CharacterIteratorMode.hpp>
|
|
|
|
|
#include <i18nlangtag/mslangid.hxx>
|
|
|
|
|
#include <limits>
|
|
|
|
|
#include <salgdi.hxx>
|
2016-10-10 01:36:45 +02:00
|
|
|
|
#include <unicode/uchar.h>
|
2016-06-14 14:38:12 +05:30
|
|
|
|
|
2016-08-17 21:31:22 +05:30
|
|
|
|
|
2016-06-14 14:38:12 +05:30
|
|
|
|
static hb_blob_t* getFontTable(hb_face_t* /*face*/, hb_tag_t nTableTag, void* pUserData)
|
|
|
|
|
{
|
|
|
|
|
char pTagName[5];
|
|
|
|
|
pTagName[0] = (char)(nTableTag >> 24);
|
|
|
|
|
pTagName[1] = (char)(nTableTag >> 16);
|
|
|
|
|
pTagName[2] = (char)(nTableTag >> 8);
|
|
|
|
|
pTagName[3] = (char)(nTableTag);
|
|
|
|
|
pTagName[4] = 0;
|
|
|
|
|
|
|
|
|
|
sal_uLong nLength = 0;
|
|
|
|
|
#if defined(_WIN32)
|
2016-10-21 21:00:21 -07:00
|
|
|
|
unsigned char* pBuffer = nullptr;
|
|
|
|
|
HFONT hFont = static_cast<HFONT>(pUserData);
|
|
|
|
|
HDC hDC = GetDC(nullptr);
|
|
|
|
|
SelectObject(hDC, hFont);
|
|
|
|
|
nLength = ::GetFontData(hDC, OSL_NETDWORD(nTableTag), 0, nullptr, 0);
|
|
|
|
|
if (nLength > 0 && nLength != GDI_ERROR)
|
|
|
|
|
{
|
|
|
|
|
pBuffer = new unsigned char[nLength];
|
|
|
|
|
::GetFontData(hDC, OSL_NETDWORD(nTableTag), 0, pBuffer, nLength);
|
|
|
|
|
}
|
|
|
|
|
ReleaseDC(nullptr, hDC);
|
2016-06-14 14:38:12 +05:30
|
|
|
|
#elif defined(MACOSX) || defined(IOS)
|
|
|
|
|
unsigned char* pBuffer = nullptr;
|
|
|
|
|
CoreTextFontFace* pFont = static_cast<CoreTextFontFace*>(pUserData);
|
|
|
|
|
nLength = pFont->GetFontTable(pTagName, nullptr);
|
|
|
|
|
if (nLength > 0)
|
|
|
|
|
{
|
|
|
|
|
pBuffer = new unsigned char[nLength];
|
2016-10-21 21:00:21 -07:00
|
|
|
|
pFont->GetFontTable(pTagName, pBuffer);
|
2016-06-14 14:38:12 +05:30
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
const unsigned char* pBuffer = nullptr;
|
2016-10-30 02:52:48 +01:00
|
|
|
|
FreetypeFont* pFont = static_cast<FreetypeFont*>(pUserData);
|
2016-06-14 14:38:12 +05:30
|
|
|
|
pBuffer = pFont->GetTable(pTagName, &nLength);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
hb_blob_t* pBlob = nullptr;
|
|
|
|
|
if (pBuffer != nullptr)
|
2016-10-21 21:00:21 -07:00
|
|
|
|
#if defined(_WIN32) || defined(MACOSX) || defined(IOS)
|
2016-07-06 17:56:15 +05:30
|
|
|
|
pBlob = hb_blob_create(reinterpret_cast<const char*>(pBuffer), nLength, HB_MEMORY_MODE_READONLY,
|
2016-10-19 20:41:14 +02:00
|
|
|
|
pBuffer, [](void* data){ delete[] static_cast<unsigned char*>(data); });
|
2016-07-06 17:56:15 +05:30
|
|
|
|
#else
|
2016-06-14 14:38:12 +05:30
|
|
|
|
pBlob = hb_blob_create(reinterpret_cast<const char*>(pBuffer), nLength, HB_MEMORY_MODE_READONLY, nullptr, nullptr);
|
2016-07-06 17:56:15 +05:30
|
|
|
|
#endif
|
2016-06-14 14:38:12 +05:30
|
|
|
|
|
|
|
|
|
return pBlob;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 19:40:11 +02:00
|
|
|
|
static hb_font_t* createHbFont(hb_face_t* pHbFace)
|
|
|
|
|
{
|
|
|
|
|
hb_font_t* pHbFont = hb_font_create(pHbFace);
|
2016-11-01 02:15:23 +02:00
|
|
|
|
unsigned int nUPEM = hb_face_get_upem(pHbFace);
|
|
|
|
|
hb_font_set_scale(pHbFont, nUPEM, nUPEM);
|
2016-09-07 19:40:11 +02:00
|
|
|
|
hb_ot_font_set_funcs(pHbFont);
|
|
|
|
|
|
2016-11-01 02:15:23 +02:00
|
|
|
|
hb_face_destroy(pHbFace);
|
|
|
|
|
|
2016-09-07 19:40:11 +02:00
|
|
|
|
return pHbFont;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 02:15:23 +02:00
|
|
|
|
void CommonSalLayout::getScale(double* nXScale, double* nYScale)
|
2016-09-07 19:40:11 +02:00
|
|
|
|
{
|
2016-11-01 02:15:23 +02:00
|
|
|
|
hb_face_t* pHbFace = hb_font_get_face(mpHbFont);
|
|
|
|
|
unsigned int nUPEM = hb_face_get_upem(pHbFace);
|
2016-09-07 19:40:11 +02:00
|
|
|
|
|
2016-11-01 02:15:23 +02:00
|
|
|
|
double nHeight(mrFontSelData.mnHeight);
|
2016-11-11 13:08:11 +00:00
|
|
|
|
#if defined(_WIN32)
|
2016-11-11 13:58:21 +02:00
|
|
|
|
// FIXME: we get very weird font width on Windows, the number below is
|
|
|
|
|
// “reverse engineered” so that I get the width I’m expecting.
|
|
|
|
|
double nWidth(mrFontSelData.mnWidth ? mrFontSelData.mnWidth * 1.8285 : nHeight);
|
|
|
|
|
#else
|
2016-11-01 02:15:23 +02:00
|
|
|
|
double nWidth(mrFontSelData.mnWidth ? mrFontSelData.mnWidth : nHeight);
|
2016-11-11 13:58:21 +02:00
|
|
|
|
#endif
|
2016-09-07 19:40:11 +02:00
|
|
|
|
|
2016-11-01 02:15:23 +02:00
|
|
|
|
if (nYScale)
|
|
|
|
|
*nYScale = nHeight / nUPEM;
|
2016-10-30 18:43:10 +02:00
|
|
|
|
|
2016-11-01 02:15:23 +02:00
|
|
|
|
if (nXScale)
|
|
|
|
|
*nXScale = nWidth / nUPEM;
|
2016-09-07 19:40:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-10 01:36:45 +02:00
|
|
|
|
#if !HB_VERSION_ATLEAST(1, 1, 0)
|
2016-10-24 10:08:09 +02:00
|
|
|
|
// Disabled Unicode compatibility decomposition, see fdo#66715
|
|
|
|
|
static unsigned int unicodeDecomposeCompatibility(hb_unicode_funcs_t* /*ufuncs*/,
|
|
|
|
|
hb_codepoint_t /*u*/,
|
|
|
|
|
hb_codepoint_t* /*decomposed*/,
|
|
|
|
|
void* /*user_data*/)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-14 14:38:12 +05:30
|
|
|
|
static hb_unicode_funcs_t* getUnicodeFuncs()
|
|
|
|
|
{
|
|
|
|
|
static hb_unicode_funcs_t* ufuncs = hb_unicode_funcs_create(hb_icu_get_unicode_funcs());
|
|
|
|
|
hb_unicode_funcs_set_decompose_compatibility_func(ufuncs, unicodeDecomposeCompatibility, nullptr, nullptr);
|
|
|
|
|
return ufuncs;
|
|
|
|
|
}
|
2016-10-10 01:36:45 +02:00
|
|
|
|
#endif
|
2016-06-14 14:38:12 +05:30
|
|
|
|
|
2016-10-17 19:59:23 +02:00
|
|
|
|
void CommonSalLayout::ParseFeatures(const OUString& name)
|
2016-10-17 15:22:32 +01:00
|
|
|
|
{
|
2016-10-17 19:59:23 +02:00
|
|
|
|
int nFeatures = 0;
|
2016-11-04 05:06:54 +02:00
|
|
|
|
int nStart = name.indexOf(FontSelectPatternAttributes::FEAT_PREFIX);
|
2016-10-17 15:22:32 +01:00
|
|
|
|
if (nStart < 0)
|
|
|
|
|
return;
|
|
|
|
|
OString oName = OUStringToOString(name, RTL_TEXTENCODING_ASCII_US);
|
2016-11-04 05:06:54 +02:00
|
|
|
|
for (int nNext = nStart; nNext > 0; nNext = name.indexOf(FontSelectPatternAttributes::FEAT_SEPARATOR, nNext + 1))
|
2016-10-17 15:22:32 +01:00
|
|
|
|
{
|
2016-10-17 19:59:23 +02:00
|
|
|
|
if (name.match("lang=", nNext + 1))
|
2016-10-17 15:22:32 +01:00
|
|
|
|
{
|
2016-11-04 05:06:54 +02:00
|
|
|
|
int endamp = name.indexOf(FontSelectPatternAttributes::FEAT_SEPARATOR, nNext+1);
|
2016-10-17 15:22:32 +01:00
|
|
|
|
int enddelim = name.indexOf(' ', nNext+1);
|
|
|
|
|
int end = name.getLength();
|
|
|
|
|
if (endamp < 0)
|
|
|
|
|
{
|
|
|
|
|
if (enddelim > 0)
|
|
|
|
|
end = enddelim;
|
|
|
|
|
}
|
|
|
|
|
else if (enddelim < 0 || endamp < enddelim)
|
|
|
|
|
end = endamp;
|
|
|
|
|
else
|
|
|
|
|
end = enddelim;
|
2016-10-17 19:59:23 +02:00
|
|
|
|
msLanguage = oName.copy(nNext + 6, end - nNext - 6);
|
2016-10-17 15:22:32 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
2016-10-17 19:59:23 +02:00
|
|
|
|
++nFeatures;
|
2016-10-17 15:22:32 +01:00
|
|
|
|
}
|
2016-10-17 19:59:23 +02:00
|
|
|
|
if (nFeatures == 0)
|
2016-10-17 15:22:32 +01:00
|
|
|
|
return;
|
|
|
|
|
|
2016-10-17 19:59:23 +02:00
|
|
|
|
maFeatures.reserve(nFeatures);
|
2016-11-04 05:06:54 +02:00
|
|
|
|
for (int nThis = nStart, nNext = name.indexOf(FontSelectPatternAttributes::FEAT_SEPARATOR, nStart + 1);
|
|
|
|
|
nThis > 0;
|
|
|
|
|
nThis = nNext, nNext = name.indexOf(FontSelectPatternAttributes::FEAT_SEPARATOR, nNext + 1))
|
2016-10-17 15:22:32 +01:00
|
|
|
|
{
|
2016-10-17 19:59:23 +02:00
|
|
|
|
if (!name.match("lang=", nThis + 1))
|
2016-10-17 15:22:32 +01:00
|
|
|
|
{
|
|
|
|
|
int end = nNext > 0 ? nNext : name.getLength();
|
2016-10-17 19:59:23 +02:00
|
|
|
|
hb_feature_t aFeature;
|
|
|
|
|
if (hb_feature_from_string(oName.getStr() + nThis + 1, end - nThis - 1, &aFeature))
|
|
|
|
|
maFeatures.push_back(aFeature);
|
2016-10-17 15:22:32 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-14 14:38:12 +05:30
|
|
|
|
#if defined(_WIN32)
|
2016-10-21 21:00:21 -07:00
|
|
|
|
CommonSalLayout::CommonSalLayout(HDC hDC, WinFontInstance& rWinFontInstance, const WinFontFace& rWinFontFace)
|
|
|
|
|
: mrFontSelData(rWinFontInstance.maFontSelData)
|
2016-10-30 22:09:10 +02:00
|
|
|
|
, mhDC(hDC)
|
|
|
|
|
, mhFont(static_cast<HFONT>(GetCurrentObject(hDC, OBJ_FONT)))
|
2016-06-14 14:38:12 +05:30
|
|
|
|
{
|
2016-09-07 19:40:11 +02:00
|
|
|
|
mpHbFont = rWinFontFace.GetHbFont();
|
|
|
|
|
if (!mpHbFont)
|
2016-08-17 21:31:22 +05:30
|
|
|
|
{
|
2016-10-30 22:09:10 +02:00
|
|
|
|
hb_face_t* pHbFace = hb_face_create_for_tables(getFontTable, mhFont, nullptr);
|
2016-09-07 19:40:11 +02:00
|
|
|
|
|
|
|
|
|
mpHbFont = createHbFont(pHbFace);
|
|
|
|
|
rWinFontFace.SetHbFont(mpHbFont);
|
2016-08-17 21:31:22 +05:30
|
|
|
|
}
|
2016-06-14 14:38:12 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#elif defined(MACOSX) || defined(IOS)
|
|
|
|
|
CommonSalLayout::CommonSalLayout(const CoreTextStyle& rCoreTextStyle)
|
2016-09-24 23:04:39 +02:00
|
|
|
|
: mrFontSelData(rCoreTextStyle.maFontSelData),
|
2016-06-14 14:38:12 +05:30
|
|
|
|
mrCoreTextStyle(rCoreTextStyle)
|
|
|
|
|
{
|
2016-09-07 19:40:11 +02:00
|
|
|
|
mpHbFont = rCoreTextStyle.GetHbFont();
|
|
|
|
|
if (!mpHbFont)
|
2016-08-17 21:31:22 +05:30
|
|
|
|
{
|
2016-11-02 15:12:59 +02:00
|
|
|
|
// On macOS we use HarfBuzz for AAT shaping, but HarfBuzz will then
|
|
|
|
|
// need a CGFont (as it offloads the actual AAT shaping to Core Text),
|
|
|
|
|
// if we have one we use it to create the hb_face_t.
|
2016-09-07 19:40:11 +02:00
|
|
|
|
hb_face_t* pHbFace;
|
2016-08-17 21:31:22 +05:30
|
|
|
|
CTFontRef pCTFont = static_cast<CTFontRef>(CFDictionaryGetValue(rCoreTextStyle.GetStyleDict(), kCTFontAttributeName));
|
2016-10-19 20:41:14 +02:00
|
|
|
|
CGFontRef pCGFont = CTFontCopyGraphicsFont(pCTFont, nullptr);
|
2016-08-17 21:31:22 +05:30
|
|
|
|
if (pCGFont)
|
2016-09-07 19:40:11 +02:00
|
|
|
|
pHbFace = hb_coretext_face_create(pCGFont);
|
2016-08-17 21:31:22 +05:30
|
|
|
|
else
|
2016-09-07 19:40:11 +02:00
|
|
|
|
pHbFace = hb_face_create_for_tables(getFontTable, const_cast<CoreTextFontFace*>(rCoreTextStyle.mpFontData), nullptr);
|
2016-08-17 21:31:22 +05:30
|
|
|
|
CGFontRelease(pCGFont);
|
2016-09-07 19:40:11 +02:00
|
|
|
|
|
|
|
|
|
mpHbFont = createHbFont(pHbFace);
|
|
|
|
|
rCoreTextStyle.SetHbFont(mpHbFont);
|
2016-08-17 21:31:22 +05:30
|
|
|
|
}
|
2016-06-14 14:38:12 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
2016-10-30 02:52:48 +01:00
|
|
|
|
CommonSalLayout::CommonSalLayout(FreetypeFont& rFreetypeFont)
|
|
|
|
|
: mrFontSelData(rFreetypeFont.GetFontSelData()),
|
|
|
|
|
mrFreetypeFont(rFreetypeFont)
|
2016-06-14 14:38:12 +05:30
|
|
|
|
{
|
2016-10-30 02:52:48 +01:00
|
|
|
|
mpHbFont = rFreetypeFont.GetHbFont();
|
2016-09-07 19:40:11 +02:00
|
|
|
|
if (!mpHbFont)
|
2016-08-17 21:31:22 +05:30
|
|
|
|
{
|
2016-10-30 02:52:48 +01:00
|
|
|
|
hb_face_t* pHbFace = hb_face_create_for_tables(getFontTable, &rFreetypeFont, nullptr);
|
2016-06-14 14:38:12 +05:30
|
|
|
|
|
2016-09-07 19:40:11 +02:00
|
|
|
|
mpHbFont = createHbFont(pHbFace);
|
2016-10-30 02:52:48 +01:00
|
|
|
|
mrFreetypeFont.SetHbFont(mpHbFont);
|
2016-09-07 19:40:11 +02:00
|
|
|
|
}
|
2016-06-14 14:38:12 +05:30
|
|
|
|
}
|
2016-09-07 19:40:11 +02:00
|
|
|
|
#endif
|
2016-06-14 14:38:12 +05:30
|
|
|
|
|
2016-11-09 23:50:53 +02:00
|
|
|
|
struct SubRun
|
2016-06-14 14:38:12 +05:30
|
|
|
|
{
|
|
|
|
|
int32_t mnMin;
|
|
|
|
|
int32_t mnEnd;
|
2016-11-10 03:59:28 +02:00
|
|
|
|
hb_script_t maScript;
|
2016-11-10 00:57:08 +02:00
|
|
|
|
hb_direction_t maDirection;
|
2016-06-14 14:38:12 +05:30
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
namespace vcl {
|
|
|
|
|
struct Run
|
|
|
|
|
{
|
|
|
|
|
int32_t nStart;
|
|
|
|
|
int32_t nEnd;
|
|
|
|
|
UScriptCode nCode;
|
|
|
|
|
Run(int32_t nStart_, int32_t nEnd_, UScriptCode nCode_)
|
|
|
|
|
: nStart(nStart_)
|
|
|
|
|
, nEnd(nEnd_)
|
|
|
|
|
, nCode(nCode_)
|
|
|
|
|
{}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TextLayoutCache
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
std::vector<vcl::Run> runs;
|
|
|
|
|
TextLayoutCache(sal_Unicode const* pStr, sal_Int32 const nEnd)
|
|
|
|
|
{
|
|
|
|
|
vcl::ScriptRun aScriptRun(
|
|
|
|
|
reinterpret_cast<const UChar *>(pStr),
|
|
|
|
|
nEnd);
|
|
|
|
|
while (aScriptRun.next())
|
|
|
|
|
{
|
|
|
|
|
runs.push_back(Run(aScriptRun.getScriptStart(),
|
|
|
|
|
aScriptRun.getScriptEnd(), aScriptRun.getScriptCode()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2016-11-09 23:39:44 +02:00
|
|
|
|
|
|
|
|
|
#include "VerticalOrientationData.cxx"
|
|
|
|
|
|
|
|
|
|
VerticalOrientation GetVerticalOrientation(sal_UCS4 cCh)
|
|
|
|
|
{
|
|
|
|
|
uint8_t nRet = 1;
|
|
|
|
|
|
|
|
|
|
if (cCh < 0x10000)
|
|
|
|
|
{
|
|
|
|
|
nRet = sVerticalOrientationValues[sVerticalOrientationPages[0][cCh >> kVerticalOrientationCharBits]]
|
|
|
|
|
[cCh & ((1 << kVerticalOrientationCharBits) - 1)];
|
|
|
|
|
}
|
|
|
|
|
else if (cCh < (kVerticalOrientationMaxPlane + 1) * 0x10000)
|
|
|
|
|
{
|
|
|
|
|
nRet = sVerticalOrientationValues[sVerticalOrientationPages[sVerticalOrientationPlanes[(cCh >> 16) - 1]]
|
|
|
|
|
[(cCh & 0xffff) >> kVerticalOrientationCharBits]]
|
|
|
|
|
[cCh & ((1 << kVerticalOrientationCharBits) - 1)];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Default value for unassigned
|
|
|
|
|
SAL_WARN("vcl.gdi", "Getting VerticalOrientation for codepoint outside Unicode range");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return VerticalOrientation(nRet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace vcl
|
2016-06-14 14:38:12 +05:30
|
|
|
|
|
|
|
|
|
std::shared_ptr<vcl::TextLayoutCache> CommonSalLayout::CreateTextLayoutCache(OUString const& rString) const
|
|
|
|
|
{
|
|
|
|
|
return std::make_shared<vcl::TextLayoutCache>(rString.getStr(), rString.getLength());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CommonSalLayout::SetNeedFallback(ImplLayoutArgs& rArgs, sal_Int32 nCharPos, bool bRightToLeft)
|
|
|
|
|
{
|
|
|
|
|
if (nCharPos < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
using namespace ::com::sun::star;
|
|
|
|
|
|
|
|
|
|
if (!mxBreak.is())
|
|
|
|
|
mxBreak = vcl::unohelper::CreateBreakIterator();
|
|
|
|
|
|
|
|
|
|
lang::Locale aLocale(rArgs.maLanguageTag.getLocale());
|
|
|
|
|
|
|
|
|
|
//if position nCharPos is missing in the font, grab the entire grapheme and
|
|
|
|
|
//mark all glyphs as missing so the whole thing is rendered with the same
|
|
|
|
|
//font
|
|
|
|
|
sal_Int32 nDone;
|
|
|
|
|
sal_Int32 nGraphemeStartPos =
|
|
|
|
|
mxBreak->previousCharacters(rArgs.mrStr, nCharPos + 1, aLocale,
|
|
|
|
|
i18n::CharacterIteratorMode::SKIPCELL, 1, nDone);
|
|
|
|
|
sal_Int32 nGraphemeEndPos =
|
|
|
|
|
mxBreak->nextCharacters(rArgs.mrStr, nCharPos, aLocale,
|
|
|
|
|
i18n::CharacterIteratorMode::SKIPCELL, 1, nDone);
|
|
|
|
|
|
|
|
|
|
rArgs.NeedFallback(nGraphemeStartPos, nGraphemeEndPos, bRightToLeft);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CommonSalLayout::AdjustLayout(ImplLayoutArgs& rArgs)
|
|
|
|
|
{
|
2016-10-09 23:23:45 +02:00
|
|
|
|
SalLayout::AdjustLayout(rArgs);
|
|
|
|
|
|
|
|
|
|
if (rArgs.mpDXArray)
|
|
|
|
|
ApplyDXArray(rArgs);
|
|
|
|
|
else if (rArgs.mnLayoutWidth)
|
|
|
|
|
Justify(rArgs.mnLayoutWidth);
|
2016-06-14 14:38:12 +05:30
|
|
|
|
|
|
|
|
|
// apply asian kerning if the glyphs are not already formatted
|
|
|
|
|
if ((rArgs.mnFlags & SalLayoutFlags::KerningAsian)
|
|
|
|
|
&& !(rArgs.mnFlags & SalLayoutFlags::Vertical))
|
|
|
|
|
if ((rArgs.mpDXArray != nullptr) || (rArgs.mnLayoutWidth != 0))
|
|
|
|
|
ApplyAsianKerning(rArgs.mrStr);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-06 10:15:49 +05:30
|
|
|
|
void CommonSalLayout::DrawText(SalGraphics& rSalGraphics) const
|
2016-06-14 14:38:12 +05:30
|
|
|
|
{
|
|
|
|
|
//call platform dependent DrawText functions
|
2016-07-06 10:15:49 +05:30
|
|
|
|
rSalGraphics.DrawSalLayout( *this );
|
2016-06-14 14:38:12 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CommonSalLayout::LayoutText(ImplLayoutArgs& rArgs)
|
|
|
|
|
{
|
2016-11-01 02:15:23 +02:00
|
|
|
|
hb_face_t* pHbFace = hb_font_get_face(mpHbFont);
|
2016-06-14 14:38:12 +05:30
|
|
|
|
hb_script_t aHbScript = HB_SCRIPT_INVALID;
|
|
|
|
|
|
|
|
|
|
int nGlyphCapacity = 2 * (rArgs.mnEndCharPos - rArgs.mnMinCharPos);
|
|
|
|
|
Reserve(nGlyphCapacity);
|
|
|
|
|
|
|
|
|
|
const int nLength = rArgs.mrStr.getLength();
|
|
|
|
|
const sal_Unicode *pStr = rArgs.mrStr.getStr();
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<vcl::TextLayoutCache> pNewScriptRun;
|
|
|
|
|
vcl::TextLayoutCache const* pTextLayout;
|
|
|
|
|
if (rArgs.m_pTextLayoutCache)
|
|
|
|
|
{
|
|
|
|
|
pTextLayout = rArgs.m_pTextLayoutCache; // use cache!
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pNewScriptRun.reset(new vcl::TextLayoutCache(pStr, rArgs.mnEndCharPos));
|
|
|
|
|
pTextLayout = pNewScriptRun.get();
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-20 16:37:22 +02:00
|
|
|
|
hb_buffer_t* pHbBuffer = hb_buffer_create();
|
|
|
|
|
hb_buffer_pre_allocate(pHbBuffer, nGlyphCapacity);
|
|
|
|
|
#if !HB_VERSION_ATLEAST(1, 1, 0)
|
|
|
|
|
static hb_unicode_funcs_t* pHbUnicodeFuncs = getUnicodeFuncs();
|
|
|
|
|
hb_buffer_set_unicode_funcs(pHbBuffer, pHbUnicodeFuncs);
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-10-30 18:50:01 +02:00
|
|
|
|
ParseFeatures(mrFontSelData.maTargetName);
|
|
|
|
|
|
2016-11-01 02:15:23 +02:00
|
|
|
|
double nXScale = 0;
|
|
|
|
|
double nYScale = 0;
|
|
|
|
|
getScale(&nXScale, &nYScale);
|
|
|
|
|
|
2016-06-14 14:38:12 +05:30
|
|
|
|
Point aCurrPos(0, 0);
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
int nBidiMinRunPos, nBidiEndRunPos;
|
|
|
|
|
bool bRightToLeft;
|
|
|
|
|
if (!rArgs.GetNextRun(&nBidiMinRunPos, &nBidiEndRunPos, &bRightToLeft))
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Find script subruns.
|
|
|
|
|
int nCurrentPos = nBidiMinRunPos;
|
2016-11-09 23:50:53 +02:00
|
|
|
|
std::vector<SubRun> aSubRuns;
|
2016-06-14 14:38:12 +05:30
|
|
|
|
size_t k = 0;
|
|
|
|
|
for (; k < pTextLayout->runs.size(); ++k)
|
|
|
|
|
{
|
|
|
|
|
vcl::Run const& rRun(pTextLayout->runs[k]);
|
|
|
|
|
if (rRun.nStart <= nCurrentPos && nCurrentPos < rRun.nEnd)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (nCurrentPos < nBidiEndRunPos && k < pTextLayout->runs.size())
|
|
|
|
|
{
|
|
|
|
|
int32_t nMinRunPos = nCurrentPos;
|
|
|
|
|
int32_t nEndRunPos = std::min(pTextLayout->runs[k].nEnd, nBidiEndRunPos);
|
2016-11-10 00:57:08 +02:00
|
|
|
|
hb_direction_t aDirection = bRightToLeft ? HB_DIRECTION_RTL : HB_DIRECTION_LTR;
|
2016-11-10 03:59:28 +02:00
|
|
|
|
hb_script_t aScript = hb_icu_script_to_script(pTextLayout->runs[k].nCode);
|
2016-11-10 00:57:08 +02:00
|
|
|
|
|
|
|
|
|
// For vertical text, further divide the runs based on character
|
|
|
|
|
// orientation.
|
|
|
|
|
if (rArgs.mnFlags & SalLayoutFlags::Vertical)
|
|
|
|
|
{
|
2016-11-10 01:48:29 +02:00
|
|
|
|
sal_Int32 nIdx = nMinRunPos;
|
2016-11-10 00:57:08 +02:00
|
|
|
|
while (nIdx < nEndRunPos)
|
|
|
|
|
{
|
2016-11-10 01:48:29 +02:00
|
|
|
|
sal_Int32 nPrevIdx = nIdx;
|
2016-11-10 00:57:08 +02:00
|
|
|
|
sal_UCS4 aChar = rArgs.mrStr.iterateCodePoints(&nIdx);
|
|
|
|
|
switch (vcl::GetVerticalOrientation(aChar))
|
|
|
|
|
{
|
|
|
|
|
case VerticalOrientation::Upright:
|
|
|
|
|
case VerticalOrientation::TransformedUpright:
|
|
|
|
|
case VerticalOrientation::TransformedRotated:
|
|
|
|
|
aDirection = HB_DIRECTION_TTB;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
aDirection = bRightToLeft ? HB_DIRECTION_RTL : HB_DIRECTION_LTR;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (aSubRuns.empty() || aSubRuns.back().maDirection != aDirection)
|
|
|
|
|
aSubRuns.push_back({ nPrevIdx, nIdx, aScript, aDirection });
|
|
|
|
|
else
|
|
|
|
|
aSubRuns.back().mnEnd = nIdx;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
aSubRuns.push_back({ nMinRunPos, nEndRunPos, aScript, aDirection });
|
|
|
|
|
}
|
2016-06-14 14:38:12 +05:30
|
|
|
|
|
|
|
|
|
nCurrentPos = nEndRunPos;
|
|
|
|
|
++k;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RTL subruns should be reversed to ensure that final glyph order is
|
|
|
|
|
// correct.
|
|
|
|
|
if (bRightToLeft)
|
2016-11-09 23:50:53 +02:00
|
|
|
|
std::reverse(aSubRuns.begin(), aSubRuns.end());
|
2016-06-14 14:38:12 +05:30
|
|
|
|
|
2016-11-09 23:50:53 +02:00
|
|
|
|
for (const auto& aSubRun : aSubRuns)
|
2016-06-14 14:38:12 +05:30
|
|
|
|
{
|
2016-10-20 16:37:22 +02:00
|
|
|
|
hb_buffer_clear_contents(pHbBuffer);
|
|
|
|
|
|
2016-11-09 23:50:53 +02:00
|
|
|
|
int nMinRunPos = aSubRun.mnMin;
|
|
|
|
|
int nEndRunPos = aSubRun.mnEnd;
|
2016-06-14 14:38:12 +05:30
|
|
|
|
int nRunLen = nEndRunPos - nMinRunPos;
|
2016-11-10 03:59:28 +02:00
|
|
|
|
aHbScript = aSubRun.maScript;
|
2016-09-26 19:09:52 +02:00
|
|
|
|
|
2016-10-17 19:59:23 +02:00
|
|
|
|
OString sLanguage = msLanguage;
|
|
|
|
|
if (sLanguage.isEmpty())
|
|
|
|
|
sLanguage = OUStringToOString(rArgs.maLanguageTag.getBcp47(), RTL_TEXTENCODING_ASCII_US);
|
2016-06-14 14:38:12 +05:30
|
|
|
|
|
|
|
|
|
int nHbFlags = HB_BUFFER_FLAGS_DEFAULT;
|
|
|
|
|
if (nMinRunPos == 0)
|
|
|
|
|
nHbFlags |= HB_BUFFER_FLAG_BOT; /* Beginning-of-text */
|
|
|
|
|
if (nEndRunPos == nLength)
|
|
|
|
|
nHbFlags |= HB_BUFFER_FLAG_EOT; /* End-of-text */
|
|
|
|
|
|
2016-11-10 00:57:08 +02:00
|
|
|
|
hb_buffer_set_direction(pHbBuffer, aSubRun.maDirection);
|
2016-11-10 03:59:28 +02:00
|
|
|
|
hb_buffer_set_script(pHbBuffer, aSubRun.maScript);
|
2016-06-14 14:38:12 +05:30
|
|
|
|
hb_buffer_set_language(pHbBuffer, hb_language_from_string(sLanguage.getStr(), -1));
|
|
|
|
|
hb_buffer_set_flags(pHbBuffer, (hb_buffer_flags_t) nHbFlags);
|
|
|
|
|
hb_buffer_add_utf16(
|
|
|
|
|
pHbBuffer, reinterpret_cast<uint16_t const *>(pStr), nLength,
|
|
|
|
|
nMinRunPos, nRunLen);
|
|
|
|
|
#if HB_VERSION_ATLEAST(0, 9, 42)
|
|
|
|
|
hb_buffer_set_cluster_level(pHbBuffer, HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS);
|
|
|
|
|
#endif
|
2016-10-06 04:15:41 +02:00
|
|
|
|
// The shapers that we want HarfBuzz to use, in the order of
|
|
|
|
|
// preference. The coretext_aat shaper is available only on macOS,
|
|
|
|
|
// but there is no harm in always including it, HarfBuzz will
|
|
|
|
|
// ignore unavailable shapers.
|
|
|
|
|
const char* pHbShapers[] = { "coretext_aat", "graphite2", "ot", "fallback", nullptr };
|
|
|
|
|
hb_segment_properties_t aHbProps;
|
|
|
|
|
hb_buffer_get_segment_properties(pHbBuffer, &aHbProps);
|
|
|
|
|
hb_shape_plan_t* pHbPlan = hb_shape_plan_create_cached(pHbFace, &aHbProps, maFeatures.data(), maFeatures.size(), pHbShapers);
|
2016-11-01 02:15:23 +02:00
|
|
|
|
bool ok = hb_shape_plan_execute(pHbPlan, mpHbFont, pHbBuffer, maFeatures.data(), maFeatures.size());
|
2016-10-06 04:15:41 +02:00
|
|
|
|
assert(ok);
|
|
|
|
|
(void) ok;
|
2016-10-30 17:59:23 +02:00
|
|
|
|
hb_buffer_set_content_type(pHbBuffer, HB_BUFFER_CONTENT_TYPE_GLYPHS);
|
2016-10-06 04:15:41 +02:00
|
|
|
|
SAL_INFO("vcl.harfbuzz", hb_shape_plan_get_shaper(pHbPlan) << " shaper used for " << mrFontSelData.GetFamilyName());
|
2016-06-14 14:38:12 +05:30
|
|
|
|
|
|
|
|
|
int nRunGlyphCount = hb_buffer_get_length(pHbBuffer);
|
|
|
|
|
hb_glyph_info_t *pHbGlyphInfos = hb_buffer_get_glyph_infos(pHbBuffer, nullptr);
|
|
|
|
|
hb_glyph_position_t *pHbPositions = hb_buffer_get_glyph_positions(pHbBuffer, nullptr);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < nRunGlyphCount; ++i) {
|
|
|
|
|
int32_t nGlyphIndex = pHbGlyphInfos[i].codepoint;
|
|
|
|
|
int32_t nCharPos = pHbGlyphInfos[i].cluster;
|
|
|
|
|
|
|
|
|
|
// if needed request glyph fallback by updating LayoutArgs
|
|
|
|
|
if (!nGlyphIndex)
|
|
|
|
|
{
|
|
|
|
|
SetNeedFallback(rArgs, nCharPos, bRightToLeft);
|
|
|
|
|
if (SalLayoutFlags::ForFallback & rArgs.mnFlags)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool bInCluster = false;
|
|
|
|
|
if (i > 0 && pHbGlyphInfos[i].cluster == pHbGlyphInfos[i - 1].cluster)
|
|
|
|
|
bInCluster = true;
|
|
|
|
|
|
|
|
|
|
long nGlyphFlags = 0;
|
|
|
|
|
if (bRightToLeft)
|
|
|
|
|
nGlyphFlags |= GlyphItem::IS_RTL_GLYPH;
|
|
|
|
|
|
|
|
|
|
if (bInCluster)
|
|
|
|
|
nGlyphFlags |= GlyphItem::IS_IN_CLUSTER;
|
|
|
|
|
|
|
|
|
|
bool bDiacritic = false;
|
2016-09-07 19:40:11 +02:00
|
|
|
|
if (hb_ot_layout_has_glyph_classes(pHbFace))
|
2016-06-14 14:38:12 +05:30
|
|
|
|
{
|
|
|
|
|
// the font has GDEF table
|
2016-09-07 23:26:14 +02:00
|
|
|
|
if (pHbPositions[i].x_advance == 0)
|
|
|
|
|
bDiacritic = hb_ot_layout_get_glyph_class(pHbFace, nGlyphIndex) == HB_OT_LAYOUT_GLYPH_CLASS_MARK;
|
2016-06-14 14:38:12 +05:30
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
#if HB_VERSION_ATLEAST(0, 9, 42)
|
2016-11-10 19:48:08 +02:00
|
|
|
|
sal_Int32 indexUtf16 = nCharPos;
|
|
|
|
|
sal_UCS4 aChar = rArgs.mrStr.iterateCodePoints(&indexUtf16, 0);
|
2016-10-10 01:36:45 +02:00
|
|
|
|
if (u_getIntPropertyValue(aChar, UCHAR_GENERAL_CATEGORY) == U_NON_SPACING_MARK)
|
2016-06-14 14:38:12 +05:30
|
|
|
|
bDiacritic = true;
|
|
|
|
|
#else
|
|
|
|
|
// the font lacks GDEF table
|
|
|
|
|
if (pHbPositions[i].x_advance == 0)
|
|
|
|
|
bDiacritic = true;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bDiacritic)
|
|
|
|
|
nGlyphFlags |= GlyphItem::IS_DIACRITIC;
|
|
|
|
|
|
2016-11-01 02:15:23 +02:00
|
|
|
|
DeviceCoordinate nAdvance, nXOffset, nYOffset;
|
2016-11-10 00:57:08 +02:00
|
|
|
|
if (aSubRun.maDirection == HB_DIRECTION_TTB)
|
2016-09-26 19:09:52 +02:00
|
|
|
|
{
|
2016-11-04 23:39:38 +02:00
|
|
|
|
nGlyphIndex |= GF_ROTL;
|
2016-11-11 18:24:11 +02:00
|
|
|
|
nAdvance = -pHbPositions[i].y_advance;
|
|
|
|
|
nXOffset = pHbPositions[i].y_offset;
|
|
|
|
|
nYOffset = pHbPositions[i].x_offset;
|
2016-09-26 19:09:52 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-11-11 18:24:11 +02:00
|
|
|
|
nAdvance = pHbPositions[i].x_advance;
|
|
|
|
|
nXOffset = pHbPositions[i].x_offset;
|
|
|
|
|
nYOffset = -pHbPositions[i].y_offset;
|
2016-09-26 19:09:52 +02:00
|
|
|
|
}
|
2016-06-14 14:38:12 +05:30
|
|
|
|
|
2016-11-11 18:24:11 +02:00
|
|
|
|
Point aNewPos(lround((aCurrPos.X() + nXOffset) * nXScale),
|
|
|
|
|
lround((aCurrPos.Y() + nYOffset) * nYScale));
|
|
|
|
|
const GlyphItem aGI(nCharPos, nGlyphIndex, aNewPos, nGlyphFlags,
|
|
|
|
|
lround(nAdvance * nXScale),
|
|
|
|
|
lround(nXOffset * nXScale));
|
2016-06-14 14:38:12 +05:30
|
|
|
|
AppendGlyph(aGI);
|
|
|
|
|
|
2016-09-26 19:09:52 +02:00
|
|
|
|
aCurrPos.X() += nAdvance;
|
2016-06-14 14:38:12 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-20 16:37:22 +02:00
|
|
|
|
hb_buffer_destroy(pHbBuffer);
|
|
|
|
|
|
2016-06-14 14:38:12 +05:30
|
|
|
|
// sort glyphs in visual order
|
|
|
|
|
// and then in logical order (e.g. diacritics after cluster start)
|
|
|
|
|
// XXX: why?
|
|
|
|
|
SortGlyphItems();
|
|
|
|
|
|
|
|
|
|
// determine need for kashida justification
|
2016-11-10 03:59:28 +02:00
|
|
|
|
// XXX: This assumes all the text is in the same script, which is not
|
|
|
|
|
// guaranteed. The flag should be per glyph.
|
2016-06-14 14:38:12 +05:30
|
|
|
|
if ((rArgs.mpDXArray || rArgs.mnLayoutWidth)
|
|
|
|
|
&& ((aHbScript == HB_SCRIPT_ARABIC) || (aHbScript == HB_SCRIPT_SYRIAC)))
|
|
|
|
|
rArgs.mnFlags |= SalLayoutFlags::KashidaJustification;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-09-11 10:25:46 +02:00
|
|
|
|
|
|
|
|
|
bool CommonSalLayout::GetCharWidths(DeviceCoordinate* pCharWidths) const
|
|
|
|
|
{
|
|
|
|
|
int nCharCount = mnEndCharPos - mnMinCharPos;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < nCharCount; ++i)
|
|
|
|
|
pCharWidths[i] = 0;
|
|
|
|
|
|
|
|
|
|
for (auto const& aGlyphItem : m_GlyphItems)
|
|
|
|
|
pCharWidths[aGlyphItem.mnCharPos - mnMinCharPos] += aGlyphItem.mnNewWidth;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-09 23:23:45 +02:00
|
|
|
|
// A note on how Kashida justification is implemented (because it took me 5
|
|
|
|
|
// years to figure it out):
|
|
|
|
|
// The decision to insert Kashidas, where and how much is taken by Writer.
|
|
|
|
|
// This decision is communicated to us in a very indirect way; by increasing
|
|
|
|
|
// the width of the character after which Kashidas should be inserted by the
|
|
|
|
|
// desired amount.
|
2016-10-10 00:54:00 +02:00
|
|
|
|
//
|
|
|
|
|
// Writer eventually calls IsKashidaPosValid() to check whether it can insert a
|
|
|
|
|
// Kashida between two characters or not.
|
|
|
|
|
//
|
2016-10-09 23:23:45 +02:00
|
|
|
|
// Here we do:
|
|
|
|
|
// - In LayoutText() set KashidaJustification flag based on text script.
|
|
|
|
|
// - In ApplyDXArray():
|
|
|
|
|
// * Check the above flag to decide whether to insert Kashidas or not.
|
2016-10-23 12:56:00 +02:00
|
|
|
|
// * For any RTL glyph that has DX adjustment, insert enough Kashidas to
|
2016-10-09 23:23:45 +02:00
|
|
|
|
// fill in the added space.
|
|
|
|
|
|
2016-09-11 10:25:46 +02:00
|
|
|
|
void CommonSalLayout::ApplyDXArray(ImplLayoutArgs& rArgs)
|
|
|
|
|
{
|
|
|
|
|
if (rArgs.mpDXArray == nullptr)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
int nCharCount = mnEndCharPos - mnMinCharPos;
|
|
|
|
|
std::unique_ptr<DeviceCoordinate[]> const pOldCharWidths(new DeviceCoordinate[nCharCount]);
|
|
|
|
|
std::unique_ptr<DeviceCoordinate[]> const pNewCharWidths(new DeviceCoordinate[nCharCount]);
|
|
|
|
|
|
|
|
|
|
// Get the natural character widths (i.e. before applying DX adjustments).
|
|
|
|
|
GetCharWidths(pOldCharWidths.get());
|
|
|
|
|
|
|
|
|
|
// Calculate the character widths after DX adjustments.
|
|
|
|
|
for (int i = 0; i < nCharCount; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (i == 0)
|
|
|
|
|
pNewCharWidths[i] = rArgs.mpDXArray[i];
|
|
|
|
|
else
|
|
|
|
|
pNewCharWidths[i] = rArgs.mpDXArray[i] - rArgs.mpDXArray[i - 1];
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-09 23:23:45 +02:00
|
|
|
|
|
|
|
|
|
bool bKashidaJustify = false;
|
|
|
|
|
DeviceCoordinate nKashidaWidth = 0;
|
|
|
|
|
hb_codepoint_t nKashidaIndex = 0;
|
|
|
|
|
if (rArgs.mnFlags & SalLayoutFlags::KashidaJustification)
|
|
|
|
|
{
|
|
|
|
|
// Find Kashida glyph width and index.
|
2016-11-01 02:15:23 +02:00
|
|
|
|
if (hb_font_get_glyph(mpHbFont, 0x0640, 0, &nKashidaIndex))
|
|
|
|
|
{
|
|
|
|
|
double nXScale = 0;
|
|
|
|
|
getScale(&nXScale, nullptr);
|
|
|
|
|
nKashidaWidth = hb_font_get_glyph_h_advance(mpHbFont, nKashidaIndex) * nXScale;
|
|
|
|
|
}
|
2016-10-09 23:23:45 +02:00
|
|
|
|
bKashidaJustify = nKashidaWidth != 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Map of Kashida insertion points (in the glyph items vector) and the
|
|
|
|
|
// requested width.
|
|
|
|
|
std::map<size_t, DeviceCoordinate> pKashidas;
|
|
|
|
|
|
2016-09-11 10:25:46 +02:00
|
|
|
|
// The accumulated difference in X position.
|
|
|
|
|
DeviceCoordinate nDelta = 0;
|
|
|
|
|
|
|
|
|
|
// Apply the DX adjustments to glyph positions and widths.
|
|
|
|
|
size_t i = 0;
|
|
|
|
|
while (i < m_GlyphItems.size())
|
|
|
|
|
{
|
|
|
|
|
int nCharPos = m_GlyphItems[i].mnCharPos - mnMinCharPos;
|
|
|
|
|
DeviceCoordinate nDiff = pNewCharWidths[nCharPos] - pOldCharWidths[nCharPos];
|
|
|
|
|
|
2016-10-09 23:23:45 +02:00
|
|
|
|
// nDiff > 1 to ignore rounding errors.
|
|
|
|
|
if (bKashidaJustify && nDiff > 1)
|
|
|
|
|
pKashidas[i] = nDiff;
|
|
|
|
|
|
2016-10-30 00:36:07 +02:00
|
|
|
|
// Adjust the width of the first glyph belonging to current character.
|
|
|
|
|
m_GlyphItems[i].mnNewWidth += nDiff;
|
|
|
|
|
|
|
|
|
|
// Apply the X position of all glyphs belonging to current character.
|
2016-09-11 10:25:46 +02:00
|
|
|
|
size_t j = i;
|
2016-10-09 23:23:45 +02:00
|
|
|
|
while (j < m_GlyphItems.size())
|
2016-09-11 10:25:46 +02:00
|
|
|
|
{
|
|
|
|
|
if (m_GlyphItems[j].mnCharPos != m_GlyphItems[i].mnCharPos)
|
|
|
|
|
break;
|
|
|
|
|
m_GlyphItems[j].maLinearPos.X() += nDelta;
|
2016-10-09 23:23:45 +02:00
|
|
|
|
// For RTL, put all DX adjustment space to the left of the glyph.
|
|
|
|
|
if (m_GlyphItems[i].IsRTLGlyph())
|
|
|
|
|
m_GlyphItems[j].maLinearPos.X() += nDiff;
|
|
|
|
|
++j;
|
2016-09-11 10:25:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Increment the delta, the loop above makes sure we do so only once
|
|
|
|
|
// for every character not for every glyph (otherwise we would apply it
|
|
|
|
|
// multiple times for each glyphs belonging to the same character which
|
|
|
|
|
// is wrong since DX adjustments are character based).
|
|
|
|
|
nDelta += nDiff;
|
|
|
|
|
i = j;
|
|
|
|
|
}
|
2016-10-09 23:23:45 +02:00
|
|
|
|
|
|
|
|
|
// Insert Kashida glyphs.
|
|
|
|
|
if (bKashidaJustify && !pKashidas.empty())
|
|
|
|
|
{
|
|
|
|
|
size_t nInserted = 0;
|
|
|
|
|
for (auto const& pKashida : pKashidas)
|
|
|
|
|
{
|
|
|
|
|
auto pGlyphIter = m_GlyphItems.begin() + nInserted + pKashida.first;
|
|
|
|
|
|
|
|
|
|
// Don’t insert Kashida after LTR glyphs.
|
|
|
|
|
if (!pGlyphIter->IsRTLGlyph())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// Don’t insert Kashida after space.
|
|
|
|
|
sal_Int32 indexUtf16 = pGlyphIter->mnCharPos;
|
|
|
|
|
sal_UCS4 aChar = rArgs.mrStr.iterateCodePoints(&indexUtf16, 0);
|
2016-10-10 01:36:45 +02:00
|
|
|
|
if (u_isUWhiteSpace(aChar))
|
2016-10-09 23:23:45 +02:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// The total Kashida width.
|
|
|
|
|
DeviceCoordinate nTotalWidth = pKashida.second;
|
|
|
|
|
|
|
|
|
|
// Number of times to repeat each Kashida.
|
|
|
|
|
int nCopies = 1;
|
|
|
|
|
if (nTotalWidth > nKashidaWidth)
|
|
|
|
|
nCopies = nTotalWidth / nKashidaWidth;
|
|
|
|
|
|
|
|
|
|
// See if we can improve the fit by adding an extra Kashidas and
|
|
|
|
|
// squeezing them together a bit.
|
|
|
|
|
DeviceCoordinate nOverlap = 0;
|
|
|
|
|
DeviceCoordinate nShortfall = nTotalWidth - nKashidaWidth * nCopies;
|
|
|
|
|
if (nShortfall > 0)
|
|
|
|
|
{
|
|
|
|
|
++nCopies;
|
|
|
|
|
DeviceCoordinate nExcess = nCopies * nKashidaWidth - nTotalWidth;
|
|
|
|
|
if (nExcess > 0)
|
|
|
|
|
nOverlap = nExcess / (nCopies - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Point aPos(pGlyphIter->maLinearPos.X() - nTotalWidth, 0);
|
|
|
|
|
int nCharPos = pGlyphIter->mnCharPos;
|
|
|
|
|
int nFlags = GlyphItem::IS_IN_CLUSTER | GlyphItem::IS_RTL_GLYPH;
|
|
|
|
|
while (nCopies--)
|
|
|
|
|
{
|
|
|
|
|
GlyphItem aKashida(nCharPos, nKashidaIndex, aPos, nFlags, nKashidaWidth);
|
|
|
|
|
pGlyphIter = m_GlyphItems.insert(pGlyphIter, aKashida);
|
|
|
|
|
aPos.X() += nKashidaWidth;
|
|
|
|
|
aPos.X() -= nOverlap;
|
|
|
|
|
++pGlyphIter;
|
|
|
|
|
++nInserted;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-11 10:25:46 +02:00
|
|
|
|
}
|
2016-10-10 00:54:00 +02:00
|
|
|
|
|
|
|
|
|
bool CommonSalLayout::IsKashidaPosValid(int nCharPos) const
|
|
|
|
|
{
|
|
|
|
|
for (auto pIter = m_GlyphItems.begin(); pIter != m_GlyphItems.end(); ++pIter)
|
|
|
|
|
{
|
|
|
|
|
if (pIter->mnCharPos == nCharPos)
|
|
|
|
|
{
|
2016-11-09 16:19:08 +02:00
|
|
|
|
// The position is the first glyphs, this would happen if we
|
|
|
|
|
// changed the text styling in the middle of a word. Since we don’t
|
|
|
|
|
// do ligatures accross layout engine instances, thid can’t be a
|
|
|
|
|
// ligature so it should be fine.
|
|
|
|
|
if (pIter == m_GlyphItems.begin())
|
|
|
|
|
return true;
|
|
|
|
|
|
2016-11-05 05:19:31 +02:00
|
|
|
|
// If the character was not supported by this layout, return false
|
|
|
|
|
// so that fallback layouts would be checked for it.
|
|
|
|
|
if (pIter->maGlyphId == 0)
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-10 00:54:00 +02:00
|
|
|
|
// Search backwards for previous glyph belonging to a different
|
|
|
|
|
// character. We are looking backwards because we are dealing with
|
|
|
|
|
// RTL glyphs, which will be in visual order.
|
|
|
|
|
for (auto pPrev = pIter - 1; pPrev != m_GlyphItems.begin(); --pPrev)
|
|
|
|
|
{
|
|
|
|
|
if (pPrev->mnCharPos != nCharPos)
|
|
|
|
|
{
|
|
|
|
|
// Check if the found glyph belongs to the next character,
|
|
|
|
|
// otherwise the current glyph will be a ligature which is
|
|
|
|
|
// invalid kashida position.
|
|
|
|
|
if (pPrev->mnCharPos == (nCharPos + 1))
|
|
|
|
|
return true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-10-21 02:30:27 +02:00
|
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|