summaryrefslogtreecommitdiffstats
path: root/vcl/quartz/CoreTextFont.cxx
blob: 6248a255c64ed10f535224f20740768676d96f90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * 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 <sal/config.h>
#include <sal/log.hxx>

#include <basegfx/polygon/b2dpolygon.hxx>
#include <basegfx/matrix/b2dhommatrix.hxx>

#ifdef MACOSX
#include <osx/saldata.hxx>
#include <osx/salinst.h>
#endif
#include <font/LogicalFontInstance.hxx>
#include <impglyphitem.hxx>
#include <quartz/CoreTextFont.hxx>
#include <quartz/CoreTextFontFace.hxx>
#include <quartz/salgdi.h>
#include <quartz/utils.h>
#include <hb.h>

CoreTextFont::CoreTextFont(const CoreTextFontFace& rPFF, const vcl::font::FontSelectPattern& rFSP)
    : LogicalFontInstance(rPFF, rFSP)
    , mfFontStretch(1.0)
    , mfFontRotation(0.0)
    , mpCTFont(nullptr)
{
    double fScaledFontHeight = rFSP.mfExactHeight;

    // convert font rotation to radian
    mfFontRotation = toRadians(rFSP.mnOrientation);

    // dummy matrix so we can use CGAffineTransformConcat() below
    CGAffineTransform aMatrix = CGAffineTransformMakeTranslation(0, 0);

    // handle font stretching if any
    if ((rFSP.mnWidth != 0) && (rFSP.mnWidth != rFSP.mnHeight))
    {
        mfFontStretch = float(rFSP.mnWidth) / rFSP.mnHeight;
        aMatrix = CGAffineTransformConcat(aMatrix, CGAffineTransformMakeScale(mfFontStretch, 1.0F));
    }

    // artificial italic
    if (NeedsArtificialItalic())
        aMatrix = CGAffineTransformConcat(
            aMatrix, CGAffineTransformMake(1, 0, ARTIFICIAL_ITALIC_SKEW, 1, 0, 0));

    CTFontDescriptorRef pFontDesc = rPFF.GetFontDescriptorRef();
    mpCTFont = CTFontCreateWithFontDescriptor(pFontDesc, fScaledFontHeight, &aMatrix);
}

CoreTextFont::~CoreTextFont()
{
    if (mpCTFont)
        CFRelease(mpCTFont);
}

void CoreTextFont::GetFontMetric(FontMetricDataRef const& rxFontMetric)
{
    rxFontMetric->ImplCalcLineSpacing(this);
    rxFontMetric->ImplInitBaselines(this);

    // since FontMetricData::mnWidth is only used for stretching/squeezing fonts
    // setting this width to the pixel height of the fontsize is good enough
    // it also makes the calculation of the stretch factor simple
    rxFontMetric->SetWidth(lrint(CTFontGetSize(mpCTFont) * mfFontStretch));

    rxFontMetric->SetMinKashida(GetKashidaWidth());
}

namespace
{
// callbacks from CTFontCreatePathForGlyph+CGPathApply for GetGlyphOutline()
struct GgoData
{
    basegfx::B2DPolygon maPolygon;
    basegfx::B2DPolyPolygon* mpPolyPoly;
};
}

static void MyCGPathApplierFunc(void* pData, const CGPathElement* pElement)
{
    basegfx::B2DPolygon& rPolygon = static_cast<GgoData*>(pData)->maPolygon;
    const int nPointCount = rPolygon.count();

    switch (pElement->type)
    {
        case kCGPathElementCloseSubpath:
        case kCGPathElementMoveToPoint:
            if (nPointCount > 0)
            {
                static_cast<GgoData*>(pData)->mpPolyPoly->append(rPolygon);
                rPolygon.clear();
            }
            // fall through for kCGPathElementMoveToPoint:
            if (pElement->type != kCGPathElementMoveToPoint)
            {
                break;
            }
            [[fallthrough]];
        case kCGPathElementAddLineToPoint:
            rPolygon.append(basegfx::B2DPoint(+pElement->points[0].x, -pElement->points[0].y));
            break;

        case kCGPathElementAddCurveToPoint:
            rPolygon.append(basegfx::B2DPoint(+pElement->points[2].x, -pElement->points[2].y));
            rPolygon.setNextControlPoint(
                nPointCount - 1, basegfx::B2DPoint(pElement->points[0].x, -pElement->points[0].y));
            rPolygon.setPrevControlPoint(
                nPointCount + 0, basegfx::B2DPoint(pElement->points[1].x, -pElement->points[1].y));
            break;

        case kCGPathElementAddQuadCurveToPoint:
        {
            const basegfx::B2DPoint aStartPt = rPolygon.getB2DPoint(nPointCount - 1);
            const basegfx::B2DPoint aCtrPt1((aStartPt.getX() + 2 * pElement->points[0].x) / 3.0,
                                            (aStartPt.getY() - 2 * pElement->points[0].y) / 3.0);
            const basegfx::B2DPoint aCtrPt2(
                (+2 * pElement->points[0].x + pElement->points[1].x) / 3.0,
                (-2 * pElement->points[0].y - pElement->points[1].y) / 3.0);
            rPolygon.append(basegfx::B2DPoint(+pElement->points[1].x, -pElement->points[1].y));
            rPolygon.setNextControlPoint(nPointCount - 1, aCtrPt1);
            rPolygon.setPrevControlPoint(nPointCount + 0, aCtrPt2);
        }
        break;
    }
}

bool CoreTextFont::GetGlyphOutline(sal_GlyphId nId, basegfx::B2DPolyPolygon& rResult, bool) const
{
    rResult.clear();

    CGGlyph nCGGlyph = nId;

    SAL_WNODEPRECATED_DECLARATIONS_PUSH
    const CTFontOrientation aFontOrientation = kCTFontDefaultOrientation;
    SAL_WNODEPRECATED_DECLARATIONS_POP
    CGRect aCGRect
        = CTFontGetBoundingRectsForGlyphs(mpCTFont, aFontOrientation, &nCGGlyph, nullptr, 1);

    if (!CGRectIsNull(aCGRect) && CGRectIsEmpty(aCGRect))
    {
        // CTFontCreatePathForGlyph returns NULL for blank glyphs, but we want
        // to return true for them.
        return true;
    }

    CGPathRef xPath = CTFontCreatePathForGlyph(mpCTFont, nCGGlyph, nullptr);
    if (!xPath)
    {
        return false;
    }

    GgoData aGgoData;
    aGgoData.mpPolyPoly = &rResult;
    CGPathApply(xPath, static_cast<void*>(&aGgoData), MyCGPathApplierFunc);
#if 0 // TODO: does OSX ensure that the last polygon is always closed?
    const CGPathElement aClosingElement = { kCGPathElementCloseSubpath, NULL };
    MyCGPathApplierFunc( (void*)&aGgoData, &aClosingElement );
#endif
    CFRelease(xPath);

    return true;
}

hb_blob_t* CoreTextFontFace::GetHbTable(hb_tag_t nTag) const
{
    hb_blob_t* pBlob = nullptr;
    CTFontRef pFont = CTFontCreateWithFontDescriptor(mxFontDescriptor, 0.0, nullptr);

    if (!nTag)
    {
        // If nTag is 0, the whole font data is requested. CoreText does not
        // give us that, so we will construct an HarfBuzz face from CoreText
        // table data and return the blob of that face.

        auto pTags = CTFontCopyAvailableTables(pFont, kCTFontTableOptionNoOptions);
        CFIndex nTags = pTags ? CFArrayGetCount(pTags) : 0;
        if (nTags > 0)
        {
            hb_face_t* pHbFace = hb_face_builder_create();
            for (CFIndex i = 0; i < nTags; i++)
            {
                auto nTable = reinterpret_cast<intptr_t>(CFArrayGetValueAtIndex(pTags, i));
                assert(nTable);
                auto pTable = GetHbTable(nTable);
                assert(pTable);
                hb_face_builder_add_table(pHbFace, nTable, pTable);
            }
            pBlob = hb_face_reference_blob(pHbFace);

            hb_face_destroy(pHbFace);
        }
        if (pTags)
            CFRelease(pTags);
    }
    else
    {
        CFDataRef pData = CTFontCopyTable(pFont, nTag, kCTFontTableOptionNoOptions);
        const CFIndex nLength = pData ? CFDataGetLength(pData) : 0;
        if (nLength > 0)
        {
            auto pBuffer = new UInt8[nLength];
            const CFRange aRange = CFRangeMake(0, nLength);
            CFDataGetBytes(pData, aRange, pBuffer);

            pBlob = hb_blob_create(reinterpret_cast<const char*>(pBuffer), nLength,
                                   HB_MEMORY_MODE_READONLY, pBuffer,
                                   [](void* data) { delete[] static_cast<UInt8*>(data); });
        }
        if (pData)
            CFRelease(pData);
    }

    CFRelease(pFont);
    return pBlob;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */