1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2020 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#ifndef SkScalerContext_mac_ct_DEFINED
9cb93a386Sopenharmony_ci#define SkScalerContext_mac_ct_DEFINED
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include "include/core/SkTypes.h"
12cb93a386Sopenharmony_ci#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_ci#include "include/core/SkRefCnt.h"
15cb93a386Sopenharmony_ci#include "include/core/SkSize.h"
16cb93a386Sopenharmony_ci#include "src/core/SkAutoMalloc.h"
17cb93a386Sopenharmony_ci#include "src/core/SkScalerContext.h"
18cb93a386Sopenharmony_ci#include "src/utils/mac/SkUniqueCFRef.h"
19cb93a386Sopenharmony_ci
20cb93a386Sopenharmony_ci#ifdef SK_BUILD_FOR_MAC
21cb93a386Sopenharmony_ci#import <ApplicationServices/ApplicationServices.h>
22cb93a386Sopenharmony_ci#endif
23cb93a386Sopenharmony_ci
24cb93a386Sopenharmony_ci#ifdef SK_BUILD_FOR_IOS
25cb93a386Sopenharmony_ci#include <CoreText/CoreText.h>
26cb93a386Sopenharmony_ci#include <CoreText/CTFontManager.h>
27cb93a386Sopenharmony_ci#include <CoreGraphics/CoreGraphics.h>
28cb93a386Sopenharmony_ci#include <CoreFoundation/CoreFoundation.h>
29cb93a386Sopenharmony_ci#endif
30cb93a386Sopenharmony_ci
31cb93a386Sopenharmony_ci#include <memory>
32cb93a386Sopenharmony_ci
33cb93a386Sopenharmony_ciclass SkDescriptor;
34cb93a386Sopenharmony_ciclass SkGlyph;
35cb93a386Sopenharmony_ciclass SkPath;
36cb93a386Sopenharmony_ciclass SkTypeface_Mac;
37cb93a386Sopenharmony_cistruct SkFontMetrics;
38cb93a386Sopenharmony_ci
39cb93a386Sopenharmony_ci
40cb93a386Sopenharmony_citypedef uint32_t CGRGBPixel;
41cb93a386Sopenharmony_ci
42cb93a386Sopenharmony_ciclass SkScalerContext_Mac : public SkScalerContext {
43cb93a386Sopenharmony_cipublic:
44cb93a386Sopenharmony_ci    SkScalerContext_Mac(sk_sp<SkTypeface_Mac>, const SkScalerContextEffects&, const SkDescriptor*);
45cb93a386Sopenharmony_ci
46cb93a386Sopenharmony_ciprotected:
47cb93a386Sopenharmony_ci    bool generateAdvance(SkGlyph* glyph) override;
48cb93a386Sopenharmony_ci    void generateMetrics(SkGlyph* glyph) override;
49cb93a386Sopenharmony_ci    void generateImage(const SkGlyph& glyph) override;
50cb93a386Sopenharmony_ci    bool generatePath(SkGlyphID glyph, SkPath* path) override;
51cb93a386Sopenharmony_ci    void generateFontMetrics(SkFontMetrics*) override;
52cb93a386Sopenharmony_ci
53cb93a386Sopenharmony_ciprivate:
54cb93a386Sopenharmony_ci    class Offscreen {
55cb93a386Sopenharmony_ci    public:
56cb93a386Sopenharmony_ci        Offscreen()
57cb93a386Sopenharmony_ci            : fRGBSpace(nullptr)
58cb93a386Sopenharmony_ci            , fCG(nullptr)
59cb93a386Sopenharmony_ci            , fDoAA(false)
60cb93a386Sopenharmony_ci            , fDoLCD(false)
61cb93a386Sopenharmony_ci        {
62cb93a386Sopenharmony_ci            fSize.set(0, 0);
63cb93a386Sopenharmony_ci        }
64cb93a386Sopenharmony_ci
65cb93a386Sopenharmony_ci        CGRGBPixel* getCG(const SkScalerContext_Mac& context, const SkGlyph& glyph,
66cb93a386Sopenharmony_ci                          CGGlyph glyphID, size_t* rowBytesPtr, bool generateA8FromLCD);
67cb93a386Sopenharmony_ci
68cb93a386Sopenharmony_ci    private:
69cb93a386Sopenharmony_ci        enum {
70cb93a386Sopenharmony_ci            kSize = 32 * 32 * sizeof(CGRGBPixel)
71cb93a386Sopenharmony_ci        };
72cb93a386Sopenharmony_ci        SkAutoSMalloc<kSize> fImageStorage;
73cb93a386Sopenharmony_ci        SkUniqueCFRef<CGColorSpaceRef> fRGBSpace;
74cb93a386Sopenharmony_ci
75cb93a386Sopenharmony_ci        // cached state
76cb93a386Sopenharmony_ci        SkUniqueCFRef<CGContextRef> fCG;
77cb93a386Sopenharmony_ci        SkISize fSize;
78cb93a386Sopenharmony_ci        bool fDoAA;
79cb93a386Sopenharmony_ci        bool fDoLCD;
80cb93a386Sopenharmony_ci    };
81cb93a386Sopenharmony_ci    Offscreen fOffscreen;
82cb93a386Sopenharmony_ci
83cb93a386Sopenharmony_ci    /** Unrotated variant of fCTFont.
84cb93a386Sopenharmony_ci     *
85cb93a386Sopenharmony_ci     *  In 10.10.1 CTFontGetAdvancesForGlyphs applies the font transform to the width of the
86cb93a386Sopenharmony_ci     *  advances, but always sets the height to 0. This font is used to get the advances of the
87cb93a386Sopenharmony_ci     *  unrotated glyph, and then the rotation is applied separately.
88cb93a386Sopenharmony_ci     *
89cb93a386Sopenharmony_ci     *  CT vertical metrics are pre-rotated (in em space, before transform) 90deg clock-wise.
90cb93a386Sopenharmony_ci     *  This makes kCTFontOrientationDefault dangerous, because the metrics from
91cb93a386Sopenharmony_ci     *  kCTFontOrientationHorizontal are in a different space from kCTFontOrientationVertical.
92cb93a386Sopenharmony_ci     *  With kCTFontOrientationVertical the advances must be unrotated.
93cb93a386Sopenharmony_ci     *
94cb93a386Sopenharmony_ci     *  Sometimes, creating a copy of a CTFont with the same size but different trasform will select
95cb93a386Sopenharmony_ci     *  different underlying font data. As a result, avoid ever creating more than one CTFont per
96cb93a386Sopenharmony_ci     *  SkScalerContext to ensure that only one CTFont is used.
97cb93a386Sopenharmony_ci     *
98cb93a386Sopenharmony_ci     *  As a result of the above (and other constraints) this font contains the size, but not the
99cb93a386Sopenharmony_ci     *  transform. The transform must always be applied separately.
100cb93a386Sopenharmony_ci     */
101cb93a386Sopenharmony_ci    SkUniqueCFRef<CTFontRef> fCTFont;
102cb93a386Sopenharmony_ci
103cb93a386Sopenharmony_ci    /** The transform without the font size. */
104cb93a386Sopenharmony_ci    CGAffineTransform fTransform;
105cb93a386Sopenharmony_ci    CGAffineTransform fInvTransform;
106cb93a386Sopenharmony_ci
107cb93a386Sopenharmony_ci    SkUniqueCFRef<CGFontRef> fCGFont;
108cb93a386Sopenharmony_ci    const bool fDoSubPosition;
109cb93a386Sopenharmony_ci
110cb93a386Sopenharmony_ci    friend class Offscreen;
111cb93a386Sopenharmony_ci
112cb93a386Sopenharmony_ci    using INHERITED = SkScalerContext;
113cb93a386Sopenharmony_ci};
114cb93a386Sopenharmony_ci
115cb93a386Sopenharmony_ci#endif
116cb93a386Sopenharmony_ci#endif //SkScalerContext_mac_ct_DEFINED
117