summaryrefslogtreecommitdiff
path: root/xfa/fde/fde_gedevice.cpp
blob: ab629991180216496d903be3f57139214fd63bfd (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "xfa/fde/fde_gedevice.h"

#include <algorithm>

#include "xfa/fde/fde_brush.h"
#include "xfa/fde/fde_devbasic.h"
#include "xfa/fde/fde_geobject.h"
#include "xfa/fde/fde_image.h"
#include "xfa/fde/fde_pen.h"

FX_BOOL FDE_GetStockHatchMask(int32_t iHatchStyle, CFX_DIBitmap& hatchMask) {
  FDE_LPCHATCHDATA pData = FDE_DEVGetHatchData(iHatchStyle);
  if (!pData) {
    return FALSE;
  }
  hatchMask.Create(pData->iWidth, pData->iHeight, FXDIB_1bppMask);
  FXSYS_memcpy(hatchMask.GetBuffer(), pData->MaskBits,
               hatchMask.GetPitch() * pData->iHeight);
  return TRUE;
}

IFDE_RenderDevice* IFDE_RenderDevice::Create(CFX_DIBitmap* pBitmap,
                                             FX_BOOL bRgbByteOrder) {
  if (pBitmap == NULL) {
    return NULL;
  }
  CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
  pDevice->Attach(pBitmap, 0, bRgbByteOrder);
  return new CFDE_FxgeDevice(pDevice, TRUE);
}
IFDE_RenderDevice* IFDE_RenderDevice::Create(CFX_RenderDevice* pDevice) {
  return pDevice ? new CFDE_FxgeDevice(pDevice, FALSE) : nullptr;
}
CFDE_FxgeDevice::CFDE_FxgeDevice(CFX_RenderDevice* pDevice,
                                 FX_BOOL bOwnerDevice)
    : m_pDevice(pDevice),
      m_bOwnerDevice(bOwnerDevice),
      m_pCharPos(NULL),
      m_iCharCount(0) {
  FXSYS_assert(pDevice != NULL);
  FX_RECT rt = m_pDevice->GetClipBox();
  m_rtClip.Set((FX_FLOAT)rt.left, (FX_FLOAT)rt.top, (FX_FLOAT)rt.Width(),
               (FX_FLOAT)rt.Height());
}
CFDE_FxgeDevice::~CFDE_FxgeDevice() {
  FX_Free(m_pCharPos);
  if (m_bOwnerDevice)
    delete m_pDevice;
}
int32_t CFDE_FxgeDevice::GetWidth() const {
  return m_pDevice->GetWidth();
}
int32_t CFDE_FxgeDevice::GetHeight() const {
  return m_pDevice->GetHeight();
}
FDE_HDEVICESTATE CFDE_FxgeDevice::SaveState() {
  m_pDevice->SaveState();
  return NULL;
}
void CFDE_FxgeDevice::RestoreState(FDE_HDEVICESTATE hState) {
  m_pDevice->RestoreState();
  const FX_RECT& rt = m_pDevice->GetClipBox();
  m_rtClip.Set((FX_FLOAT)rt.left, (FX_FLOAT)rt.top, (FX_FLOAT)rt.Width(),
               (FX_FLOAT)rt.Height());
}
FX_BOOL CFDE_FxgeDevice::SetClipRect(const CFX_RectF& rtClip) {
  m_rtClip = rtClip;
  return m_pDevice->SetClip_Rect(FX_RECT((int32_t)FXSYS_floor(rtClip.left),
                                         (int32_t)FXSYS_floor(rtClip.top),
                                         (int32_t)FXSYS_ceil(rtClip.right()),
                                         (int32_t)FXSYS_ceil(rtClip.bottom())));
}
const CFX_RectF& CFDE_FxgeDevice::GetClipRect() {
  return m_rtClip;
}
FX_BOOL CFDE_FxgeDevice::SetClipPath(const IFDE_Path* pClip) {
  return FALSE;
}
IFDE_Path* CFDE_FxgeDevice::GetClipPath() const {
  return NULL;
}
FX_FLOAT CFDE_FxgeDevice::GetDpiX() const {
  return 96;
}
FX_FLOAT CFDE_FxgeDevice::GetDpiY() const {
  return 96;
}
FX_BOOL CFDE_FxgeDevice::DrawImage(CFX_DIBSource* pDib,
                                   const CFX_RectF* pSrcRect,
                                   const CFX_RectF& dstRect,
                                   const CFX_Matrix* pImgMatrix,
                                   const CFX_Matrix* pDevMatrix) {
  FXSYS_assert(pDib != NULL);
  CFX_RectF srcRect;
  if (pSrcRect) {
    srcRect = *pSrcRect;
  } else {
    srcRect.Set(0, 0, (FX_FLOAT)pDib->GetWidth(), (FX_FLOAT)pDib->GetHeight());
  }
  if (srcRect.IsEmpty()) {
    return FALSE;
  }
  CFX_Matrix dib2fxdev;
  if (pImgMatrix) {
    dib2fxdev = *pImgMatrix;
  } else {
    dib2fxdev.SetIdentity();
  }
  dib2fxdev.a = dstRect.width;
  dib2fxdev.d = -dstRect.height;
  dib2fxdev.e = dstRect.left;
  dib2fxdev.f = dstRect.bottom();
  if (pDevMatrix) {
    dib2fxdev.Concat(*pDevMatrix);
  }
  void* handle = NULL;
  m_pDevice->StartDIBits(pDib, 255, 0, (const CFX_Matrix*)&dib2fxdev, 0,
                         handle);
  while (m_pDevice->ContinueDIBits(handle, NULL)) {
  }
  m_pDevice->CancelDIBits(handle);
  return handle != NULL;
}
FX_BOOL CFDE_FxgeDevice::DrawString(IFDE_Brush* pBrush,
                                    IFX_Font* pFont,
                                    const FXTEXT_CHARPOS* pCharPos,
                                    int32_t iCount,
                                    FX_FLOAT fFontSize,
                                    const CFX_Matrix* pMatrix) {
  FXSYS_assert(pBrush != NULL && pFont != NULL && pCharPos != NULL &&
               iCount > 0);
  CFX_FontCache* pCache = CFX_GEModule::Get()->GetFontCache();
  CFX_Font* pFxFont = (CFX_Font*)pFont->GetDevFont();
  switch (pBrush->GetType()) {
    case FDE_BRUSHTYPE_Solid: {
      FX_ARGB argb = ((IFDE_SolidBrush*)pBrush)->GetColor();
      if ((pFont->GetFontStyles() & FX_FONTSTYLE_Italic) != 0 &&
          !pFxFont->IsItalic()) {
        FXTEXT_CHARPOS* pCP = (FXTEXT_CHARPOS*)pCharPos;
        FX_FLOAT* pAM;
        for (int32_t i = 0; i < iCount; ++i) {
          static const FX_FLOAT mc = 0.267949f;
          pAM = pCP->m_AdjustMatrix;
          pAM[2] = mc * pAM[0] + pAM[2];
          pAM[3] = mc * pAM[1] + pAM[3];
          pCP++;
        }
      }
      FXTEXT_CHARPOS* pCP = (FXTEXT_CHARPOS*)pCharPos;
      IFX_Font* pCurFont = NULL;
      IFX_Font* pSTFont = NULL;
      FXTEXT_CHARPOS* pCurCP = NULL;
      int32_t iCurCount = 0;
#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
      FX_DWORD dwFontStyle = pFont->GetFontStyles();
      CFX_Font FxFont;
      CFX_SubstFont SubstFxFont;
      FxFont.SetSubstFont(&SubstFxFont);
      SubstFxFont.m_Weight = dwFontStyle & FX_FONTSTYLE_Bold ? 700 : 400;
      SubstFxFont.m_WeightCJK = SubstFxFont.m_Weight;
      SubstFxFont.m_ItalicAngle = dwFontStyle & FX_FONTSTYLE_Italic ? -12 : 0;
      SubstFxFont.m_bItlicCJK = !!(dwFontStyle & FX_FONTSTYLE_Italic);
#endif  // _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
      for (int32_t i = 0; i < iCount; ++i) {
        pSTFont = pFont->GetSubstFont((int32_t)pCP->m_GlyphIndex);
        pCP->m_GlyphIndex &= 0x00FFFFFF;
        pCP->m_bFontStyle = FALSE;
        if (pCurFont != pSTFont) {
          if (pCurFont != NULL) {
            pFxFont = (CFX_Font*)pCurFont->GetDevFont();
#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
            FxFont.SetFace(pFxFont->GetFace());
            m_pDevice->DrawNormalText(iCurCount, pCurCP, &FxFont, pCache,
                                      -fFontSize, (const CFX_Matrix*)pMatrix,
                                      argb, FXTEXT_CLEARTYPE);
#else
            m_pDevice->DrawNormalText(iCurCount, pCurCP, pFxFont, pCache,
                                      -fFontSize, (const CFX_Matrix*)pMatrix,
                                      argb, FXTEXT_CLEARTYPE);
#endif  // _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
          }
          pCurFont = pSTFont;
          pCurCP = pCP;
          iCurCount = 1;
        } else {
          iCurCount++;
        }
        pCP++;
      }
      if (pCurFont != NULL && iCurCount) {
        pFxFont = (CFX_Font*)pCurFont->GetDevFont();
#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
        FxFont.SetFace(pFxFont->GetFace());
        FX_BOOL bRet = m_pDevice->DrawNormalText(
            iCurCount, pCurCP, &FxFont, pCache, -fFontSize,
            (const CFX_Matrix*)pMatrix, argb, FXTEXT_CLEARTYPE);
        FxFont.SetSubstFont(nullptr);
        FxFont.SetFace(nullptr);
        return bRet;
#else
        return m_pDevice->DrawNormalText(iCurCount, pCurCP, pFxFont, pCache,
                                         -fFontSize, (const CFX_Matrix*)pMatrix,
                                         argb, FXTEXT_CLEARTYPE);
#endif  // _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
      }
#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
      FxFont.SetSubstFont(nullptr);
      FxFont.SetFace(nullptr);
#endif  // _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
      return TRUE;
    } break;
    default:
      return FALSE;
  }
}
FX_BOOL CFDE_FxgeDevice::DrawBezier(IFDE_Pen* pPen,
                                    FX_FLOAT fPenWidth,
                                    const CFX_PointF& pt1,
                                    const CFX_PointF& pt2,
                                    const CFX_PointF& pt3,
                                    const CFX_PointF& pt4,
                                    const CFX_Matrix* pMatrix) {
  CFX_PointsF points;
  points.Add(pt1);
  points.Add(pt2);
  points.Add(pt3);
  points.Add(pt4);
  CFDE_Path path;
  path.AddBezier(points);
  return DrawPath(pPen, fPenWidth, &path, pMatrix);
}
FX_BOOL CFDE_FxgeDevice::DrawCurve(IFDE_Pen* pPen,
                                   FX_FLOAT fPenWidth,
                                   const CFX_PointsF& points,
                                   FX_BOOL bClosed,
                                   FX_FLOAT fTension,
                                   const CFX_Matrix* pMatrix) {
  CFDE_Path path;
  path.AddCurve(points, bClosed, fTension);
  return DrawPath(pPen, fPenWidth, &path, pMatrix);
}
FX_BOOL CFDE_FxgeDevice::DrawEllipse(IFDE_Pen* pPen,
                                     FX_FLOAT fPenWidth,
                                     const CFX_RectF& rect,
                                     const CFX_Matrix* pMatrix) {
  CFDE_Path path;
  path.AddEllipse(rect);
  return DrawPath(pPen, fPenWidth, &path, pMatrix);
}
FX_BOOL CFDE_FxgeDevice::DrawLines(IFDE_Pen* pPen,
                                   FX_FLOAT fPenWidth,
                                   const CFX_PointsF& points,
                                   const CFX_Matrix* pMatrix) {
  CFDE_Path path;
  path.AddLines(points);
  return DrawPath(pPen, fPenWidth, &path, pMatrix);
}
FX_BOOL CFDE_FxgeDevice::DrawLine(IFDE_Pen* pPen,
                                  FX_FLOAT fPenWidth,
                                  const CFX_PointF& pt1,
                                  const CFX_PointF& pt2,
                                  const CFX_Matrix* pMatrix) {
  CFDE_Path path;
  path.AddLine(pt1, pt2);
  return DrawPath(pPen, fPenWidth, &path, pMatrix);
}
FX_BOOL CFDE_FxgeDevice::DrawPath(IFDE_Pen* pPen,
                                  FX_FLOAT fPenWidth,
                                  const IFDE_Path* pPath,
                                  const CFX_Matrix* pMatrix) {
  CFDE_Path* pGePath = (CFDE_Path*)pPath;
  if (pGePath == NULL) {
    return FALSE;
  }
  CFX_GraphStateData graphState;
  if (!CreatePen(pPen, fPenWidth, graphState)) {
    return FALSE;
  }
  return m_pDevice->DrawPath(&pGePath->m_Path, (const CFX_Matrix*)pMatrix,
                             &graphState, 0, pPen->GetColor(), 0);
}
FX_BOOL CFDE_FxgeDevice::DrawPolygon(IFDE_Pen* pPen,
                                     FX_FLOAT fPenWidth,
                                     const CFX_PointsF& points,
                                     const CFX_Matrix* pMatrix) {
  CFDE_Path path;
  path.AddPolygon(points);
  return DrawPath(pPen, fPenWidth, &path, pMatrix);
}
FX_BOOL CFDE_FxgeDevice::DrawRectangle(IFDE_Pen* pPen,
                                       FX_FLOAT fPenWidth,
                                       const CFX_RectF& rect,
                                       const CFX_Matrix* pMatrix) {
  CFDE_Path path;
  path.AddRectangle(rect);
  return DrawPath(pPen, fPenWidth, &path, pMatrix);
}
FX_BOOL CFDE_FxgeDevice::FillClosedCurve(IFDE_Brush* pBrush,
                                         const CFX_PointsF& points,
                                         FX_FLOAT fTension,
                                         const CFX_Matrix* pMatrix) {
  CFDE_Path path;
  path.AddCurve(points, TRUE, fTension);
  return FillPath(pBrush, &path, pMatrix);
}
FX_BOOL CFDE_FxgeDevice::FillEllipse(IFDE_Brush* pBrush,
                                     const CFX_RectF& rect,
                                     const CFX_Matrix* pMatrix) {
  CFDE_Path path;
  path.AddEllipse(rect);
  return FillPath(pBrush, &path, pMatrix);
}
FX_BOOL CFDE_FxgeDevice::FillPolygon(IFDE_Brush* pBrush,
                                     const CFX_PointsF& points,
                                     const CFX_Matrix* pMatrix) {
  CFDE_Path path;
  path.AddPolygon(points);
  return FillPath(pBrush, &path, pMatrix);
}
FX_BOOL CFDE_FxgeDevice::FillRectangle(IFDE_Brush* pBrush,
                                       const CFX_RectF& rect,
                                       const CFX_Matrix* pMatrix) {
  CFDE_Path path;
  path.AddRectangle(rect);
  return FillPath(pBrush, &path, pMatrix);
}
FX_BOOL CFDE_FxgeDevice::CreatePen(IFDE_Pen* pPen,
                                   FX_FLOAT fPenWidth,
                                   CFX_GraphStateData& graphState) {
  if (pPen == NULL) {
    return FALSE;
  }
  graphState.m_LineCap = (CFX_GraphStateData::LineCap)pPen->GetLineCap();
  graphState.m_LineJoin = (CFX_GraphStateData::LineJoin)pPen->GetLineJoin();
  graphState.m_LineWidth = fPenWidth;
  graphState.m_MiterLimit = pPen->GetMiterLimit();
  graphState.m_DashPhase = pPen->GetDashPhase();
  CFX_FloatArray dashArray;
  switch (pPen->GetDashStyle()) {
    case FDE_DASHSTYLE_Dash:
      dashArray.Add(3);
      dashArray.Add(1);
      break;
    case FDE_DASHSTYLE_Dot:
      dashArray.Add(1);
      dashArray.Add(1);
      break;
    case FDE_DASHSTYLE_DashDot:
      dashArray.Add(3);
      dashArray.Add(1);
      dashArray.Add(1);
      dashArray.Add(1);
      break;
    case FDE_DASHSTYLE_DashDotDot:
      dashArray.Add(3);
      dashArray.Add(1);
      dashArray.Add(1);
      dashArray.Add(1);
      dashArray.Add(1);
      dashArray.Add(1);
      break;
    case FDE_DASHSTYLE_Customized:
      pPen->GetDashArray(dashArray);
      break;
  }
  int32_t iDashCount = dashArray.GetSize();
  if (iDashCount > 0) {
    graphState.SetDashCount(iDashCount);
    for (int32_t i = 0; i < iDashCount; ++i) {
      graphState.m_DashArray[i] = dashArray[i] * fPenWidth;
    }
  }
  return TRUE;
}
typedef FX_BOOL (CFDE_FxgeDevice::*pfFillPath)(IFDE_Brush* pBrush,
                                               const CFX_PathData* pPath,
                                               const CFX_Matrix* pMatrix);
static const pfFillPath gs_FillPath[] = {
    &CFDE_FxgeDevice::FillSolidPath, &CFDE_FxgeDevice::FillHatchPath,
    &CFDE_FxgeDevice::FillTexturePath, &CFDE_FxgeDevice::FillLinearGradientPath,
};
FX_BOOL CFDE_FxgeDevice::FillPath(IFDE_Brush* pBrush,
                                  const IFDE_Path* pPath,
                                  const CFX_Matrix* pMatrix) {
  CFDE_Path* pGePath = (CFDE_Path*)pPath;
  if (pGePath == NULL) {
    return FALSE;
  }
  if (pBrush == NULL) {
    return FALSE;
  }
  int32_t iType = pBrush->GetType();
  if (iType < 0 || iType > FDE_BRUSHTYPE_MAX) {
    return FALSE;
  }
  return (this->*gs_FillPath[iType])(pBrush, &pGePath->m_Path, pMatrix);
}
FX_BOOL CFDE_FxgeDevice::FillSolidPath(IFDE_Brush* pBrush,
                                       const CFX_PathData* pPath,
                                       const CFX_Matrix* pMatrix) {
  FXSYS_assert(pPath && pBrush && pBrush->GetType() == FDE_BRUSHTYPE_Solid);
  IFDE_SolidBrush* pSolidBrush = (IFDE_SolidBrush*)pBrush;
  return m_pDevice->DrawPath(pPath, (const CFX_Matrix*)pMatrix, NULL,
                             pSolidBrush->GetColor(), 0, FXFILL_WINDING);
}
FX_BOOL CFDE_FxgeDevice::FillHatchPath(IFDE_Brush* pBrush,
                                       const CFX_PathData* pPath,
                                       const CFX_Matrix* pMatrix) {
  FXSYS_assert(pPath && pBrush && pBrush->GetType() == FDE_BRUSHTYPE_Hatch);
  IFDE_HatchBrush* pHatchBrush = (IFDE_HatchBrush*)pBrush;
  int32_t iStyle = pHatchBrush->GetHatchStyle();
  if (iStyle < FDE_HATCHSTYLE_Min || iStyle > FDE_HATCHSTYLE_Max) {
    return FALSE;
  }
  CFX_DIBitmap mask;
  if (!FDE_GetStockHatchMask(iStyle, mask)) {
    return FALSE;
  }
  FX_ARGB dwForeColor = pHatchBrush->GetColor(TRUE);
  FX_ARGB dwBackColor = pHatchBrush->GetColor(FALSE);
  CFX_FloatRect rectf = pPath->GetBoundingBox();
  if (pMatrix) {
    rectf.Transform((const CFX_Matrix*)pMatrix);
  }
  FX_RECT rect(FXSYS_round(rectf.left), FXSYS_round(rectf.top),
               FXSYS_round(rectf.right), FXSYS_round(rectf.bottom));
  m_pDevice->SaveState();
  m_pDevice->StartRendering();
  m_pDevice->SetClip_PathFill(pPath, (const CFX_Matrix*)pMatrix,
                              FXFILL_WINDING);
  m_pDevice->FillRect(&rect, dwBackColor);
  for (int32_t j = rect.bottom; j < rect.top; j += mask.GetHeight())
    for (int32_t i = rect.left; i < rect.right; i += mask.GetWidth()) {
      m_pDevice->SetBitMask(&mask, i, j, dwForeColor);
    }
  m_pDevice->EndRendering();
  m_pDevice->RestoreState();
  return TRUE;
}
FX_BOOL CFDE_FxgeDevice::FillTexturePath(IFDE_Brush* pBrush,
                                         const CFX_PathData* pPath,
                                         const CFX_Matrix* pMatrix) {
  FXSYS_assert(pPath && pBrush && pBrush->GetType() == FDE_BRUSHTYPE_Texture);
  IFDE_TextureBrush* pTextureBrush = static_cast<IFDE_TextureBrush*>(pBrush);
  IFDE_Image* pImage = pTextureBrush->GetImage();
  if (!pImage)
    return FALSE;

  CFX_Size size(pImage->GetImageWidth(), pImage->GetImageHeight());
  CFX_DIBitmap bmp;
  bmp.Create(size.x, size.y, FXDIB_Argb);
  if (!pImage->StartLoadImage(&bmp, 0, 0, size.x, size.y, 0, 0, size.x,
                              size.y)) {
    return FALSE;
  }
  if (pImage->DoLoadImage() < 100) {
    return FALSE;
  }
  pImage->StopLoadImage();
  return WrapTexture(pTextureBrush->GetWrapMode(), &bmp, pPath, pMatrix);
}
FX_BOOL CFDE_FxgeDevice::WrapTexture(int32_t iWrapMode,
                                     const CFX_DIBitmap* pBitmap,
                                     const CFX_PathData* pPath,
                                     const CFX_Matrix* pMatrix) {
  CFX_FloatRect rectf = pPath->GetBoundingBox();
  if (pMatrix) {
    rectf.Transform((const CFX_Matrix*)pMatrix);
  }
  FX_RECT rect(FXSYS_round(rectf.left), FXSYS_round(rectf.top),
               FXSYS_round(rectf.right), FXSYS_round(rectf.bottom));
  rect.Normalize();
  if (rect.IsEmpty()) {
    return FALSE;
  }
  m_pDevice->SaveState();
  m_pDevice->StartRendering();
  m_pDevice->SetClip_PathFill(pPath, (const CFX_Matrix*)pMatrix,
                              FXFILL_WINDING);
  switch (iWrapMode) {
    case FDE_WRAPMODE_Tile:
    case FDE_WRAPMODE_TileFlipX:
    case FDE_WRAPMODE_TileFlipY:
    case FDE_WRAPMODE_TileFlipXY: {
      FX_BOOL bFlipX = iWrapMode == FDE_WRAPMODE_TileFlipXY ||
                       iWrapMode == FDE_WRAPMODE_TileFlipX;
      FX_BOOL bFlipY = iWrapMode == FDE_WRAPMODE_TileFlipXY ||
                       iWrapMode == FDE_WRAPMODE_TileFlipY;
      const CFX_DIBitmap* pFlip[2][2];
      pFlip[0][0] = pBitmap;
      pFlip[0][1] = bFlipX ? pBitmap->FlipImage(TRUE, FALSE) : pBitmap;
      pFlip[1][0] = bFlipY ? pBitmap->FlipImage(FALSE, TRUE) : pBitmap;
      pFlip[1][1] =
          (bFlipX || bFlipY) ? pBitmap->FlipImage(bFlipX, bFlipY) : pBitmap;
      int32_t iCounterY = 0;
      for (int32_t j = rect.top; j < rect.bottom; j += pBitmap->GetHeight()) {
        int32_t indexY = iCounterY++ % 2;
        int32_t iCounterX = 0;
        for (int32_t i = rect.left; i < rect.right; i += pBitmap->GetWidth()) {
          int32_t indexX = iCounterX++ % 2;
          m_pDevice->SetDIBits(pFlip[indexY][indexX], i, j);
        }
      }
      if (pFlip[0][1] != pFlip[0][0]) {
        delete pFlip[0][1];
      }
      if (pFlip[1][0] != pFlip[0][0]) {
        delete pFlip[1][0];
      }
      if (pFlip[1][1] != pFlip[0][0]) {
        delete pFlip[1][1];
      }
    } break;
    case FDE_WRAPMODE_Clamp: {
      m_pDevice->SetDIBits(pBitmap, rect.left, rect.bottom);
    } break;
  }
  m_pDevice->EndRendering();
  m_pDevice->RestoreState();
  return TRUE;
}
FX_BOOL CFDE_FxgeDevice::FillLinearGradientPath(IFDE_Brush* pBrush,
                                                const CFX_PathData* pPath,
                                                const CFX_Matrix* pMatrix) {
  FXSYS_assert(pPath && pBrush &&
               pBrush->GetType() == FDE_BRUSHTYPE_LinearGradient);
  IFDE_LinearGradientBrush* pLinearBrush = (IFDE_LinearGradientBrush*)pBrush;
  CFX_PointF pt0, pt1;
  pLinearBrush->GetLinearPoints(pt0, pt1);
  CFX_VectorF fDiagonal(pt0, pt1);
  FX_FLOAT fTheta = FXSYS_atan2(fDiagonal.y, fDiagonal.x);
  FX_FLOAT fLength = fDiagonal.Length();
  FX_FLOAT fTotalX = fLength / FXSYS_cos(fTheta);
  FX_FLOAT fTotalY = fLength / FXSYS_cos(FX_PI / 2 - fTheta);
  FX_FLOAT fSteps = std::max(fTotalX, fTotalY);
  FX_FLOAT dx = fTotalX / fSteps;
  FX_FLOAT dy = fTotalY / fSteps;
  FX_ARGB cr0, cr1;
  pLinearBrush->GetLinearColors(cr0, cr1);
  FX_FLOAT a0 = FXARGB_A(cr0);
  FX_FLOAT r0 = FXARGB_R(cr0);
  FX_FLOAT g0 = FXARGB_G(cr0);
  FX_FLOAT b0 = FXARGB_B(cr0);
  FX_FLOAT da = (FXARGB_A(cr1) - a0) / fSteps;
  FX_FLOAT dr = (FXARGB_R(cr1) - r0) / fSteps;
  FX_FLOAT dg = (FXARGB_G(cr1) - g0) / fSteps;
  FX_FLOAT db = (FXARGB_B(cr1) - b0) / fSteps;
  CFX_DIBitmap bmp;
  bmp.Create(FXSYS_round(FXSYS_fabs(fDiagonal.x)),
             FXSYS_round(FXSYS_fabs(fDiagonal.y)), FXDIB_Argb);
  CFX_FxgeDevice dev;
  dev.Attach(&bmp);
  pt1 = pt0;
  int32_t iSteps = FXSYS_round(FXSYS_ceil(fSteps));
  while (--iSteps >= 0) {
    cr0 = ArgbEncode(FXSYS_round(a0), FXSYS_round(r0), FXSYS_round(g0),
                     FXSYS_round(b0));
    dev.DrawCosmeticLine(pt0.x, pt0.y, pt1.x, pt1.y, cr0);
    pt1.x += dx;
    pt0.y += dy;
    a0 += da;
    r0 += dr;
    g0 += dg;
    b0 += db;
  }
  return WrapTexture(pLinearBrush->GetWrapMode(), &bmp, pPath, pMatrix);
}