Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / vcl / quartz / CoreTextFont.cxx
blob6248a255c64ed10f535224f20740768676d96f90
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
21 #include <sal/log.hxx>
23 #include <basegfx/polygon/b2dpolygon.hxx>
24 #include <basegfx/matrix/b2dhommatrix.hxx>
26 #ifdef MACOSX
27 #include <osx/saldata.hxx>
28 #include <osx/salinst.h>
29 #endif
30 #include <font/LogicalFontInstance.hxx>
31 #include <impglyphitem.hxx>
32 #include <quartz/CoreTextFont.hxx>
33 #include <quartz/CoreTextFontFace.hxx>
34 #include <quartz/salgdi.h>
35 #include <quartz/utils.h>
36 #include <hb.h>
38 CoreTextFont::CoreTextFont(const CoreTextFontFace& rPFF, const vcl::font::FontSelectPattern& rFSP)
39 : LogicalFontInstance(rPFF, rFSP)
40 , mfFontStretch(1.0)
41 , mfFontRotation(0.0)
42 , mpCTFont(nullptr)
44 double fScaledFontHeight = rFSP.mfExactHeight;
46 // convert font rotation to radian
47 mfFontRotation = toRadians(rFSP.mnOrientation);
49 // dummy matrix so we can use CGAffineTransformConcat() below
50 CGAffineTransform aMatrix = CGAffineTransformMakeTranslation(0, 0);
52 // handle font stretching if any
53 if ((rFSP.mnWidth != 0) && (rFSP.mnWidth != rFSP.mnHeight))
55 mfFontStretch = float(rFSP.mnWidth) / rFSP.mnHeight;
56 aMatrix = CGAffineTransformConcat(aMatrix, CGAffineTransformMakeScale(mfFontStretch, 1.0F));
59 // artificial italic
60 if (NeedsArtificialItalic())
61 aMatrix = CGAffineTransformConcat(
62 aMatrix, CGAffineTransformMake(1, 0, ARTIFICIAL_ITALIC_SKEW, 1, 0, 0));
64 CTFontDescriptorRef pFontDesc = rPFF.GetFontDescriptorRef();
65 mpCTFont = CTFontCreateWithFontDescriptor(pFontDesc, fScaledFontHeight, &aMatrix);
68 CoreTextFont::~CoreTextFont()
70 if (mpCTFont)
71 CFRelease(mpCTFont);
74 void CoreTextFont::GetFontMetric(FontMetricDataRef const& rxFontMetric)
76 rxFontMetric->ImplCalcLineSpacing(this);
77 rxFontMetric->ImplInitBaselines(this);
79 // since FontMetricData::mnWidth is only used for stretching/squeezing fonts
80 // setting this width to the pixel height of the fontsize is good enough
81 // it also makes the calculation of the stretch factor simple
82 rxFontMetric->SetWidth(lrint(CTFontGetSize(mpCTFont) * mfFontStretch));
84 rxFontMetric->SetMinKashida(GetKashidaWidth());
87 namespace
89 // callbacks from CTFontCreatePathForGlyph+CGPathApply for GetGlyphOutline()
90 struct GgoData
92 basegfx::B2DPolygon maPolygon;
93 basegfx::B2DPolyPolygon* mpPolyPoly;
97 static void MyCGPathApplierFunc(void* pData, const CGPathElement* pElement)
99 basegfx::B2DPolygon& rPolygon = static_cast<GgoData*>(pData)->maPolygon;
100 const int nPointCount = rPolygon.count();
102 switch (pElement->type)
104 case kCGPathElementCloseSubpath:
105 case kCGPathElementMoveToPoint:
106 if (nPointCount > 0)
108 static_cast<GgoData*>(pData)->mpPolyPoly->append(rPolygon);
109 rPolygon.clear();
111 // fall through for kCGPathElementMoveToPoint:
112 if (pElement->type != kCGPathElementMoveToPoint)
114 break;
116 [[fallthrough]];
117 case kCGPathElementAddLineToPoint:
118 rPolygon.append(basegfx::B2DPoint(+pElement->points[0].x, -pElement->points[0].y));
119 break;
121 case kCGPathElementAddCurveToPoint:
122 rPolygon.append(basegfx::B2DPoint(+pElement->points[2].x, -pElement->points[2].y));
123 rPolygon.setNextControlPoint(
124 nPointCount - 1, basegfx::B2DPoint(pElement->points[0].x, -pElement->points[0].y));
125 rPolygon.setPrevControlPoint(
126 nPointCount + 0, basegfx::B2DPoint(pElement->points[1].x, -pElement->points[1].y));
127 break;
129 case kCGPathElementAddQuadCurveToPoint:
131 const basegfx::B2DPoint aStartPt = rPolygon.getB2DPoint(nPointCount - 1);
132 const basegfx::B2DPoint aCtrPt1((aStartPt.getX() + 2 * pElement->points[0].x) / 3.0,
133 (aStartPt.getY() - 2 * pElement->points[0].y) / 3.0);
134 const basegfx::B2DPoint aCtrPt2(
135 (+2 * pElement->points[0].x + pElement->points[1].x) / 3.0,
136 (-2 * pElement->points[0].y - pElement->points[1].y) / 3.0);
137 rPolygon.append(basegfx::B2DPoint(+pElement->points[1].x, -pElement->points[1].y));
138 rPolygon.setNextControlPoint(nPointCount - 1, aCtrPt1);
139 rPolygon.setPrevControlPoint(nPointCount + 0, aCtrPt2);
141 break;
145 bool CoreTextFont::GetGlyphOutline(sal_GlyphId nId, basegfx::B2DPolyPolygon& rResult, bool) const
147 rResult.clear();
149 CGGlyph nCGGlyph = nId;
151 SAL_WNODEPRECATED_DECLARATIONS_PUSH
152 const CTFontOrientation aFontOrientation = kCTFontDefaultOrientation;
153 SAL_WNODEPRECATED_DECLARATIONS_POP
154 CGRect aCGRect
155 = CTFontGetBoundingRectsForGlyphs(mpCTFont, aFontOrientation, &nCGGlyph, nullptr, 1);
157 if (!CGRectIsNull(aCGRect) && CGRectIsEmpty(aCGRect))
159 // CTFontCreatePathForGlyph returns NULL for blank glyphs, but we want
160 // to return true for them.
161 return true;
164 CGPathRef xPath = CTFontCreatePathForGlyph(mpCTFont, nCGGlyph, nullptr);
165 if (!xPath)
167 return false;
170 GgoData aGgoData;
171 aGgoData.mpPolyPoly = &rResult;
172 CGPathApply(xPath, static_cast<void*>(&aGgoData), MyCGPathApplierFunc);
173 #if 0 // TODO: does OSX ensure that the last polygon is always closed?
174 const CGPathElement aClosingElement = { kCGPathElementCloseSubpath, NULL };
175 MyCGPathApplierFunc( (void*)&aGgoData, &aClosingElement );
176 #endif
177 CFRelease(xPath);
179 return true;
182 hb_blob_t* CoreTextFontFace::GetHbTable(hb_tag_t nTag) const
184 hb_blob_t* pBlob = nullptr;
185 CTFontRef pFont = CTFontCreateWithFontDescriptor(mxFontDescriptor, 0.0, nullptr);
187 if (!nTag)
189 // If nTag is 0, the whole font data is requested. CoreText does not
190 // give us that, so we will construct an HarfBuzz face from CoreText
191 // table data and return the blob of that face.
193 auto pTags = CTFontCopyAvailableTables(pFont, kCTFontTableOptionNoOptions);
194 CFIndex nTags = pTags ? CFArrayGetCount(pTags) : 0;
195 if (nTags > 0)
197 hb_face_t* pHbFace = hb_face_builder_create();
198 for (CFIndex i = 0; i < nTags; i++)
200 auto nTable = reinterpret_cast<intptr_t>(CFArrayGetValueAtIndex(pTags, i));
201 assert(nTable);
202 auto pTable = GetHbTable(nTable);
203 assert(pTable);
204 hb_face_builder_add_table(pHbFace, nTable, pTable);
206 pBlob = hb_face_reference_blob(pHbFace);
208 hb_face_destroy(pHbFace);
210 if (pTags)
211 CFRelease(pTags);
213 else
215 CFDataRef pData = CTFontCopyTable(pFont, nTag, kCTFontTableOptionNoOptions);
216 const CFIndex nLength = pData ? CFDataGetLength(pData) : 0;
217 if (nLength > 0)
219 auto pBuffer = new UInt8[nLength];
220 const CFRange aRange = CFRangeMake(0, nLength);
221 CFDataGetBytes(pData, aRange, pBuffer);
223 pBlob = hb_blob_create(reinterpret_cast<const char*>(pBuffer), nLength,
224 HB_MEMORY_MODE_READONLY, pBuffer,
225 [](void* data) { delete[] static_cast<UInt8*>(data); });
227 if (pData)
228 CFRelease(pData);
231 CFRelease(pFont);
232 return pBlob;
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */