Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
fx_quartz_device.cpp
Go to the documentation of this file.
1// Copyright 2014 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "core/fxge/apple/fx_quartz_device.h"
8
9#include <CoreGraphics/CoreGraphics.h>
10
11#include "core/fxcrt/fx_extension.h"
12#include "core/fxge/cfx_graphstatedata.h"
13#include "core/fxge/cfx_path.h"
14#include "core/fxge/cfx_renderdevice.h"
15#include "core/fxge/dib/cfx_dibitmap.h"
16
17#ifndef CGFLOAT_IS_DOUBLE
18#error Expected CGFLOAT_IS_DOUBLE to be defined by CoreGraphics headers
19#endif
20
21void* CQuartz2D::CreateGraphics(const RetainPtr<CFX_DIBitmap>& pBitmap) {
22 if (!pBitmap)
23 return nullptr;
24 CGBitmapInfo bmpInfo = kCGBitmapByteOrder32Little;
25 switch (pBitmap->GetFormat()) {
26 case FXDIB_Format::kRgb32:
27 bmpInfo |= kCGImageAlphaNoneSkipFirst;
28 break;
29 case FXDIB_Format::kArgb:
30 default:
31 return nullptr;
32 }
33 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
34 CGContextRef context = CGBitmapContextCreate(
35 pBitmap->GetWritableBuffer().data(), pBitmap->GetWidth(),
36 pBitmap->GetHeight(), 8, pBitmap->GetPitch(), colorSpace, bmpInfo);
37 CGColorSpaceRelease(colorSpace);
38 return context;
39}
40
41void CQuartz2D::DestroyGraphics(void* graphics) {
42 if (graphics)
43 CGContextRelease((CGContextRef)graphics);
44}
45
46void* CQuartz2D::CreateFont(pdfium::span<const uint8_t> pFontData) {
47 CGDataProviderRef pDataProvider = CGDataProviderCreateWithData(
48 nullptr, pFontData.data(), pFontData.size(), nullptr);
49 if (!pDataProvider)
50 return nullptr;
51
52 CGFontRef pCGFont = CGFontCreateWithDataProvider(pDataProvider);
53 CGDataProviderRelease(pDataProvider);
54 return pCGFont;
55}
56
57void CQuartz2D::DestroyFont(void* pFont) {
58 CGFontRelease((CGFontRef)pFont);
59}
60
61void CQuartz2D::SetGraphicsTextMatrix(void* graphics,
62 const CFX_Matrix& matrix) {
63 if (!graphics)
64 return;
65 CGContextRef context = reinterpret_cast<CGContextRef>(graphics);
66 CGFloat ty = CGBitmapContextGetHeight(context) - matrix.f;
67 CGContextSetTextMatrix(
68 context, CGAffineTransformMake(matrix.a, matrix.b, matrix.c, matrix.d,
69 matrix.e, ty));
70}
71
72bool CQuartz2D::DrawGraphicsString(void* graphics,
73 void* font,
74 float fontSize,
75 pdfium::span<uint16_t> glyphIndices,
76 pdfium::span<CGPoint> glyphPositions,
77 FX_ARGB argb) {
78 if (!graphics)
79 return false;
80
81 CGContextRef context = (CGContextRef)graphics;
82 CGContextSetFont(context, (CGFontRef)font);
83 CGContextSetFontSize(context, fontSize);
84
85 int32_t a;
86 int32_t r;
87 int32_t g;
88 int32_t b;
89 std::tie(a, r, g, b) = ArgbDecode(argb);
90 CGContextSetRGBFillColor(context, r / 255.f, g / 255.f, b / 255.f, a / 255.f);
91 CGContextSaveGState(context);
92#if CGFLOAT_IS_DOUBLE
93 CGPoint* glyphPositionsCG = new CGPoint[glyphPositions.size()];
94 for (size_t index = 0; index < glyphPositions.size(); ++index) {
95 glyphPositionsCG[index].x = glyphPositions[index].x;
96 glyphPositionsCG[index].y = glyphPositions[index].y;
97 }
98#else
99 CGPoint* glyphPositionsCG = glyphPositions.data();
100#endif
101 CGContextShowGlyphsAtPositions(
102 context, reinterpret_cast<CGGlyph*>(glyphIndices.data()),
103 glyphPositionsCG, glyphPositions.size());
104#if CGFLOAT_IS_DOUBLE
105 delete[] glyphPositionsCG;
106#endif
107 CGContextRestoreGState(context);
108 return true;
109}
void SetGraphicsTextMatrix(void *graphics, const CFX_Matrix &matrix)
void DestroyFont(void *pFont)
void DestroyGraphics(void *graphics)
void * CreateGraphics(const RetainPtr< CFX_DIBitmap > &bitmap)
bool DrawGraphicsString(void *graphics, void *font, float fontSize, pdfium::span< uint16_t > glyphIndices, pdfium::span< CGPoint > glyphPositions, FX_ARGB argb)
void * CreateFont(pdfium::span< const uint8_t > pFontData)