summaryrefslogtreecommitdiffstats
path: root/skia/ext/vector_platform_device_emf_win.cc
blob: f77fbc8d20d8d714f20544f7f59295b638a4d52c (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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "skia/ext/vector_platform_device_emf_win.h"

#include <windows.h>

#include "base/logging.h"
#include "base/strings/string16.h"
#include "skia/ext/bitmap_platform_device.h"
#include "skia/ext/skia_utils_win.h"
#include "third_party/skia/include/core/SkFontHost.h"
#include "third_party/skia/include/core/SkPathEffect.h"
#include "third_party/skia/include/core/SkTemplates.h"
#include "third_party/skia/include/core/SkUtils.h"
#include "third_party/skia/include/ports/SkTypeface_win.h"

namespace skia {

#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
    do { if (paint.isNoDrawAnnotation()) { return; } } while (0)

// static
SkBaseDevice* VectorPlatformDeviceEmf::CreateDevice(
    int width, int height, bool is_opaque, HANDLE shared_section) {
  if (!is_opaque) {
    // TODO(maruel):  http://crbug.com/18382 When restoring a semi-transparent
    // layer, i.e. merging it, we need to rasterize it because GDI doesn't
    // support transparency except for AlphaBlend(). Right now, a
    // BitmapPlatformDevice is created when VectorCanvas think a saveLayers()
    // call is being done. The way to save a layer would be to create an
    // EMF-based VectorDevice and have this device registers the drawing. When
    // playing back the device into a bitmap, do it at the printer's dpi instead
    // of the layout's dpi (which is much lower).
    return BitmapPlatformDevice::Create(width, height, is_opaque,
                                        shared_section);
  }

  // TODO(maruel):  http://crbug.com/18383 Look if it would be worth to
  // increase the resolution by ~10x (any worthy factor) to increase the
  // rendering precision (think about printing) while using a relatively
  // low dpi. This happens because we receive float as input but the GDI
  // functions works with integers. The idea is to premultiply the matrix
  // with this factor and multiply each SkScalar that are passed to
  // SkScalarRound(value) as SkScalarRound(value * 10). Safari is already
  // doing the same for text rendering.
  SkASSERT(shared_section);
  SkBaseDevice* device = VectorPlatformDeviceEmf::create(
      reinterpret_cast<HDC>(shared_section), width, height);
  return device;
}

static void FillBitmapInfoHeader(int width, int height, BITMAPINFOHEADER* hdr) {
  hdr->biSize = sizeof(BITMAPINFOHEADER);
  hdr->biWidth = width;
  hdr->biHeight = -height;  // Minus means top-down bitmap.
  hdr->biPlanes = 1;
  hdr->biBitCount = 32;
  hdr->biCompression = BI_RGB;  // no compression
  hdr->biSizeImage = 0;
  hdr->biXPelsPerMeter = 1;
  hdr->biYPelsPerMeter = 1;
  hdr->biClrUsed = 0;
  hdr->biClrImportant = 0;
}

SkBaseDevice* VectorPlatformDeviceEmf::create(HDC dc, int width, int height) {
  InitializeDC(dc);

  // Link the SkBitmap to the current selected bitmap in the device context.
  SkBitmap bitmap;
  HGDIOBJ selected_bitmap = GetCurrentObject(dc, OBJ_BITMAP);
  bool succeeded = false;
  if (selected_bitmap != NULL) {
    BITMAP bitmap_data;
    if (GetObject(selected_bitmap, sizeof(BITMAP), &bitmap_data) ==
        sizeof(BITMAP)) {
      // The context has a bitmap attached. Attach our SkBitmap to it.
      // Warning: If the bitmap gets unselected from the HDC,
      // VectorPlatformDeviceEmf has no way to detect this, so the HBITMAP
      // could be released while SkBitmap still has a reference to it. Be
      // cautious.
      if (width == bitmap_data.bmWidth &&
          height == bitmap_data.bmHeight) {
        bitmap.setConfig(SkBitmap::kARGB_8888_Config,
                         bitmap_data.bmWidth,
                         bitmap_data.bmHeight,
                         bitmap_data.bmWidthBytes);
        bitmap.setPixels(bitmap_data.bmBits);
        succeeded = true;
      }
    }
  }

  if (!succeeded)
    bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);

  return new VectorPlatformDeviceEmf(dc, bitmap);
}

VectorPlatformDeviceEmf::VectorPlatformDeviceEmf(HDC dc, const SkBitmap& bitmap)
    : SkBitmapDevice(bitmap),
      hdc_(dc),
      previous_brush_(NULL),
      previous_pen_(NULL) {
  transform_.reset();
  SetPlatformDevice(this, this);
}

VectorPlatformDeviceEmf::~VectorPlatformDeviceEmf() {
  SkASSERT(previous_brush_ == NULL);
  SkASSERT(previous_pen_ == NULL);
}

HDC VectorPlatformDeviceEmf::BeginPlatformPaint() {
  return hdc_;
}

uint32_t VectorPlatformDeviceEmf::getDeviceCapabilities() {
  return SkBitmapDevice::getDeviceCapabilities() | kVector_Capability;
}

void VectorPlatformDeviceEmf::drawPaint(const SkDraw& draw,
                                        const SkPaint& paint) {
  // TODO(maruel):  Bypass the current transformation matrix.
  SkRect rect;
  rect.fLeft = 0;
  rect.fTop = 0;
  rect.fRight = SkIntToScalar(width() + 1);
  rect.fBottom = SkIntToScalar(height() + 1);
  drawRect(draw, rect, paint);
}

void VectorPlatformDeviceEmf::drawPoints(const SkDraw& draw,
                                         SkCanvas::PointMode mode,
                                         size_t count,
                                         const SkPoint pts[],
                                         const SkPaint& paint) {
  if (!count)
    return;

  if (mode == SkCanvas::kPoints_PointMode) {
    SkASSERT(false);
    return;
  }

  SkPaint tmp_paint(paint);
  tmp_paint.setStyle(SkPaint::kStroke_Style);

  // Draw a path instead.
  SkPath path;
  switch (mode) {
    case SkCanvas::kLines_PointMode:
      if (count % 2) {
        SkASSERT(false);
        return;
      }
      for (size_t i = 0; i < count / 2; ++i) {
        path.moveTo(pts[2 * i]);
        path.lineTo(pts[2 * i + 1]);
      }
      break;
    case SkCanvas::kPolygon_PointMode:
      path.moveTo(pts[0]);
      for (size_t i = 1; i < count; ++i) {
        path.lineTo(pts[i]);
      }
      break;
    default:
      SkASSERT(false);
      return;
  }
  // Draw the calculated path.
  drawPath(draw, path, tmp_paint);
}

void VectorPlatformDeviceEmf::drawRect(const SkDraw& draw,
                                       const SkRect& rect,
                                       const SkPaint& paint) {
  CHECK_FOR_NODRAW_ANNOTATION(paint);
  if (paint.getPathEffect()) {
    // Draw a path instead.
    SkPath path_orginal;
    path_orginal.addRect(rect);

    // Apply the path effect to the rect.
    SkPath path_modified;
    paint.getFillPath(path_orginal, &path_modified);

    // Removes the path effect from the temporary SkPaint object.
    SkPaint paint_no_effet(paint);
    paint_no_effet.setPathEffect(NULL);

    // Draw the calculated path.
    drawPath(draw, path_modified, paint_no_effet);
    return;
  }

  if (!ApplyPaint(paint)) {
    return;
  }
  HDC dc = BeginPlatformPaint();
  if (!Rectangle(dc, SkScalarRoundToInt(rect.fLeft),
                 SkScalarRoundToInt(rect.fTop),
                 SkScalarRoundToInt(rect.fRight),
                 SkScalarRoundToInt(rect.fBottom))) {
    SkASSERT(false);
  }
  EndPlatformPaint();
  Cleanup();
}

void VectorPlatformDeviceEmf::drawRRect(const SkDraw& draw, const SkRRect& rr,
                                        const SkPaint& paint) {
  SkPath path;
  path.addRRect(rr);
  this->drawPath(draw, path, paint, NULL, true);
}

void VectorPlatformDeviceEmf::drawPath(const SkDraw& draw,
                                       const SkPath& path,
                                       const SkPaint& paint,
                                       const SkMatrix* prePathMatrix,
                                       bool pathIsMutable) {
  CHECK_FOR_NODRAW_ANNOTATION(paint);
  if (paint.getPathEffect()) {
    // Apply the path effect forehand.
    SkPath path_modified;
    paint.getFillPath(path, &path_modified);

    // Removes the path effect from the temporary SkPaint object.
    SkPaint paint_no_effet(paint);
    paint_no_effet.setPathEffect(NULL);

    // Draw the calculated path.
    drawPath(draw, path_modified, paint_no_effet);
    return;
  }

  if (!ApplyPaint(paint)) {
    return;
  }
  HDC dc = BeginPlatformPaint();
  if (PlatformDevice::LoadPathToDC(dc, path)) {
    switch (paint.getStyle()) {
      case SkPaint::kFill_Style: {
        BOOL res = StrokeAndFillPath(dc);
        SkASSERT(res != 0);
        break;
      }
      case SkPaint::kStroke_Style: {
        BOOL res = StrokePath(dc);
        SkASSERT(res != 0);
        break;
      }
      case SkPaint::kStrokeAndFill_Style: {
        BOOL res = StrokeAndFillPath(dc);
        SkASSERT(res != 0);
        break;
      }
      default:
        SkASSERT(false);
        break;
    }
  }
  EndPlatformPaint();
  Cleanup();
}

void VectorPlatformDeviceEmf::drawBitmapRect(const SkDraw& draw,
                                             const SkBitmap& bitmap,
                                             const SkRect* src,
                                             const SkRect& dst,
                                             const SkPaint& paint,
                                             SkCanvas::DrawBitmapRectFlags flags) {
    SkMatrix    matrix;
    SkRect      bitmapBounds, tmpSrc, tmpDst;
    SkBitmap    tmpBitmap;

    bitmapBounds.isetWH(bitmap.width(), bitmap.height());

    // Compute matrix from the two rectangles
    if (src) {
        tmpSrc = *src;
    } else {
        tmpSrc = bitmapBounds;
    }
    matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);

    const SkBitmap* bitmapPtr = &bitmap;

    // clip the tmpSrc to the bounds of the bitmap, and recompute dstRect if
    // needed (if the src was clipped). No check needed if src==null.
    if (src) {
        if (!bitmapBounds.contains(*src)) {
            if (!tmpSrc.intersect(bitmapBounds)) {
                return; // nothing to draw
            }
            // recompute dst, based on the smaller tmpSrc
            matrix.mapRect(&tmpDst, tmpSrc);
        }

        // since we may need to clamp to the borders of the src rect within
        // the bitmap, we extract a subset.
        // TODO: make sure this is handled in drawrect and remove it from here.
        SkIRect srcIR;
        tmpSrc.roundOut(&srcIR);
        if (!bitmap.extractSubset(&tmpBitmap, srcIR)) {
            return;
        }
        bitmapPtr = &tmpBitmap;

        // Since we did an extract, we need to adjust the matrix accordingly
        SkScalar dx = 0, dy = 0;
        if (srcIR.fLeft > 0) {
            dx = SkIntToScalar(srcIR.fLeft);
        }
        if (srcIR.fTop > 0) {
            dy = SkIntToScalar(srcIR.fTop);
        }
        if (dx || dy) {
            matrix.preTranslate(dx, dy);
        }
    }
    this->drawBitmap(draw, *bitmapPtr, matrix, paint);
}

void VectorPlatformDeviceEmf::drawBitmap(const SkDraw& draw,
                                         const SkBitmap& bitmap,
                                         const SkMatrix& matrix,
                                         const SkPaint& paint) {
  // Load the temporary matrix. This is what will translate, rotate and resize
  // the bitmap.
  SkMatrix actual_transform(transform_);
  actual_transform.preConcat(matrix);
  LoadTransformToDC(hdc_, actual_transform);

  InternalDrawBitmap(bitmap, 0, 0, paint);

  // Restore the original matrix.
  LoadTransformToDC(hdc_, transform_);
}

void VectorPlatformDeviceEmf::drawSprite(const SkDraw& draw,
                                         const SkBitmap& bitmap,
                                         int x, int y,
                                         const SkPaint& paint) {
  SkMatrix identity;
  identity.reset();
  LoadTransformToDC(hdc_, identity);

  InternalDrawBitmap(bitmap, x, y, paint);

  // Restore the original matrix.
  LoadTransformToDC(hdc_, transform_);
}

/////////////////////////////////////////////////////////////////////////

static bool gdiCanHandleText(const SkPaint& paint) {
  return !paint.getShader() &&
         !paint.getPathEffect() &&
         (SkPaint::kFill_Style == paint.getStyle()) &&
         (255 == paint.getAlpha());
}

class SkGDIFontSetup {
 public:
  SkGDIFontSetup() :
      fHDC(NULL),
      fNewFont(NULL),
      fSavedFont(NULL),
      fSavedTextColor(0),
      fUseGDI(false) {
    SkDEBUGCODE(fUseGDIHasBeenCalled = false;)
  }
  ~SkGDIFontSetup();

  // can only be called once
  bool useGDI(HDC hdc, const SkPaint&);

 private:
  HDC      fHDC;
  HFONT    fNewFont;
  HFONT    fSavedFont;
  COLORREF fSavedTextColor;
  bool     fUseGDI;
  SkDEBUGCODE(bool fUseGDIHasBeenCalled;)
};

bool SkGDIFontSetup::useGDI(HDC hdc, const SkPaint& paint) {
  SkASSERT(!fUseGDIHasBeenCalled);
  SkDEBUGCODE(fUseGDIHasBeenCalled = true;)

  fUseGDI = gdiCanHandleText(paint);
  if (fUseGDI) {
    fSavedTextColor = GetTextColor(hdc);
    SetTextColor(hdc, skia::SkColorToCOLORREF(paint.getColor()));

    LOGFONT lf;
    SkLOGFONTFromTypeface(paint.getTypeface(), &lf);
    lf.lfHeight = -SkScalarRoundToInt(paint.getTextSize());
    fNewFont = CreateFontIndirect(&lf);
    fSavedFont = (HFONT)::SelectObject(hdc, fNewFont);
    fHDC = hdc;
  }
  return fUseGDI;
}

SkGDIFontSetup::~SkGDIFontSetup() {
  if (fUseGDI) {
    ::SelectObject(fHDC, fSavedFont);
    ::DeleteObject(fNewFont);
    SetTextColor(fHDC, fSavedTextColor);
  }
}

static SkScalar getAscent(const SkPaint& paint) {
  SkPaint::FontMetrics fm;
  paint.getFontMetrics(&fm);
  return fm.fAscent;
}

// return the options int for ExtTextOut. Only valid if the paint's text
// encoding is not UTF8 (in which case ExtTextOut can't be used).
static UINT getTextOutOptions(const SkPaint& paint) {
  if (SkPaint::kGlyphID_TextEncoding == paint.getTextEncoding()) {
    return ETO_GLYPH_INDEX;
  } else {
    SkASSERT(SkPaint::kUTF16_TextEncoding == paint.getTextEncoding());
    return 0;
  }
}

static SkiaEnsureTypefaceCharactersAccessible
    g_skia_ensure_typeface_characters_accessible = NULL;

SK_API void SetSkiaEnsureTypefaceCharactersAccessible(
    SkiaEnsureTypefaceCharactersAccessible func) {
  // This function is supposed to be called once in process life time.
  SkASSERT(g_skia_ensure_typeface_characters_accessible == NULL);
  g_skia_ensure_typeface_characters_accessible = func;
}

void EnsureTypefaceCharactersAccessible(
    const SkTypeface& typeface, const wchar_t* text, unsigned int text_length) {
  LOGFONT lf;
  SkLOGFONTFromTypeface(&typeface, &lf);
  g_skia_ensure_typeface_characters_accessible(lf, text, text_length);
}

bool EnsureExtTextOut(HDC hdc, int x, int y, UINT options, const RECT * lprect,
                      LPCWSTR text, unsigned int characters, const int * lpDx,
                      SkTypeface* const typeface) {
  bool success = ExtTextOut(hdc, x, y, options, lprect, text, characters, lpDx);
  if (!success) {
    if (typeface) {
      EnsureTypefaceCharactersAccessible(*typeface,
                                         text,
                                         characters);
      success = ExtTextOut(hdc, x, y, options, lprect, text, characters, lpDx);
      if (!success) {
        LOGFONT lf;
        SkLOGFONTFromTypeface(typeface, &lf);
        VLOG(1) << "SkFontHost::EnsureTypefaceCharactersAccessible FAILED for "
                << " FaceName = " << lf.lfFaceName
                << " and characters: " << base::string16(text, characters);
      }
    } else {
      VLOG(1) << "ExtTextOut FAILED for default FaceName "
              << " and characters: " << base::string16(text, characters);
    }
  }
  return success;
}

void VectorPlatformDeviceEmf::drawText(const SkDraw& draw,
                                       const void* text,
                                       size_t byteLength,
                                       SkScalar x,
                                       SkScalar y,
                                       const SkPaint& paint) {
  SkGDIFontSetup setup;
  bool useDrawPath = true;

  if (SkPaint::kUTF8_TextEncoding != paint.getTextEncoding()
      && setup.useGDI(hdc_, paint)) {
    UINT options = getTextOutOptions(paint);
    UINT count = byteLength >> 1;
    useDrawPath = !EnsureExtTextOut(hdc_, SkScalarRoundToInt(x),
        SkScalarRoundToInt(y + getAscent(paint)), options, 0,
        reinterpret_cast<const wchar_t*>(text), count, NULL,
        paint.getTypeface());
  }

  if (useDrawPath) {
    SkPath path;
    paint.getTextPath(text, byteLength, x, y, &path);
    drawPath(draw, path, paint);
  }
}

static size_t size_utf8(const char* text) {
  return SkUTF8_CountUTF8Bytes(text);
}

static size_t size_utf16(const char* text) {
  uint16_t c = *reinterpret_cast<const uint16_t*>(text);
  return SkUTF16_IsHighSurrogate(c) ? 4 : 2;
}

static size_t size_glyphid(const char* text) {
  return 2;
}

void VectorPlatformDeviceEmf::drawPosText(const SkDraw& draw,
                                          const void* text,
                                          size_t len,
                                          const SkScalar pos[],
                                          SkScalar constY,
                                          int scalarsPerPos,
                                          const SkPaint& paint) {
  SkGDIFontSetup setup;
  bool useDrawText = true;

  if (2 == scalarsPerPos
      && SkPaint::kUTF8_TextEncoding != paint.getTextEncoding()
      && setup.useGDI(hdc_, paint)) {
    int startX = SkScalarRoundToInt(pos[0]);
    int startY = SkScalarRoundToInt(pos[1] + getAscent(paint));
    const int count = len >> 1;
    SkAutoSTMalloc<64, INT> storage(count);
    INT* advances = storage.get();
    for (int i = 0; i < count - 1; ++i) {
      advances[i] = SkScalarRoundToInt(pos[2] - pos[0]);
      pos += 2;
    }
    useDrawText = !EnsureExtTextOut(hdc_, startX, startY,
        getTextOutOptions(paint), 0, reinterpret_cast<const wchar_t*>(text),
        count, advances, paint.getTypeface());
  }

  if (useDrawText) {
    size_t (*bytesPerCodePoint)(const char*);
    switch (paint.getTextEncoding()) {
    case SkPaint::kUTF8_TextEncoding:
      bytesPerCodePoint = size_utf8;
      break;
    case SkPaint::kUTF16_TextEncoding:
      bytesPerCodePoint = size_utf16;
      break;
    default:
      SkASSERT(SkPaint::kGlyphID_TextEncoding == paint.getTextEncoding());
      bytesPerCodePoint = size_glyphid;
      break;
    }

    const char* curr = reinterpret_cast<const char*>(text);
    const char* stop = curr + len;
    while (curr < stop) {
      SkScalar y = (1 == scalarsPerPos) ? constY : pos[1];
      size_t bytes = bytesPerCodePoint(curr);
      drawText(draw, curr, bytes, pos[0], y, paint);
      curr += bytes;
      pos += scalarsPerPos;
    }
  }
}

void VectorPlatformDeviceEmf::drawTextOnPath(const SkDraw& draw,
                                             const void* text,
                                             size_t len,
                                             const SkPath& path,
                                             const SkMatrix* matrix,
                                             const SkPaint& paint) {
  // This function isn't used in the code. Verify this assumption.
  SkASSERT(false);
}

void VectorPlatformDeviceEmf::drawVertices(const SkDraw& draw,
                                           SkCanvas::VertexMode vmode,
                                           int vertexCount,
                                           const SkPoint vertices[],
                                           const SkPoint texs[],
                                           const SkColor colors[],
                                           SkXfermode* xmode,
                                           const uint16_t indices[],
                                           int indexCount,
                                           const SkPaint& paint) {
  // This function isn't used in the code. Verify this assumption.
  SkASSERT(false);
}

void VectorPlatformDeviceEmf::drawDevice(const SkDraw& draw,
                                         SkBaseDevice* device,
                                         int x,
                                         int y,
                                         const SkPaint& paint) {
  // TODO(maruel):  http://b/1183870 Playback the EMF buffer at printer's dpi if
  // it is a vectorial device.
  drawSprite(draw, device->accessBitmap(false), x, y, paint);
}

bool VectorPlatformDeviceEmf::ApplyPaint(const SkPaint& paint) {
  // Note: The goal here is to transfert the SkPaint's state to the HDC's state.
  // This function does not execute the SkPaint drawing commands. These should
  // be executed in drawPaint().

  SkPaint::Style style = paint.getStyle();
  if (!paint.getAlpha())
      style = (SkPaint::Style) SkPaint::kStyleCount;

  switch (style) {
    case SkPaint::kFill_Style:
      if (!CreateBrush(true, paint) ||
          !CreatePen(false, paint))
        return false;
      break;
    case SkPaint::kStroke_Style:
      if (!CreateBrush(false, paint) ||
          !CreatePen(true, paint))
        return false;
      break;
    case SkPaint::kStrokeAndFill_Style:
      if (!CreateBrush(true, paint) ||
          !CreatePen(true, paint))
        return false;
      break;
    default:
      if (!CreateBrush(false, paint) ||
          !CreatePen(false, paint))
        return false;
      break;
  }

  /*
  getFlags();
    isAntiAlias();
    isDither()
    isLinearText()
    isSubpixelText()
    isUnderlineText()
    isStrikeThruText()
    isFakeBoldText()
    isDevKernText()
    isFilterBitmap()

  // Skia's text is not used. This should be fixed.
  getTextAlign()
  getTextScaleX()
  getTextSkewX()
  getTextEncoding()
  getFontMetrics()
  getFontSpacing()
  */

  // BUG 1094907: Implement shaders. Shaders currently in use:
  //  SkShader::CreateBitmapShader
  //  SkGradientShader::CreateRadial
  //  SkGradientShader::CreateLinear
  // SkASSERT(!paint.getShader());

  // http://b/1106647 Implement loopers and mask filter. Looper currently in
  // use:
  //   SkBlurDrawLooper is used for shadows.
  // SkASSERT(!paint.getLooper());
  // SkASSERT(!paint.getMaskFilter());

  // http://b/1165900 Implement xfermode.
  // SkASSERT(!paint.getXfermode());

  // The path effect should be processed before arriving here.
  SkASSERT(!paint.getPathEffect());

  // This isn't used in the code. Verify this assumption.
  SkASSERT(!paint.getRasterizer());
  // Reuse code to load Win32 Fonts.
  return true;
}

void VectorPlatformDeviceEmf::setMatrixClip(const SkMatrix& transform,
                                            const SkRegion& region,
                                            const SkClipStack&) {
  transform_ = transform;
  LoadTransformToDC(hdc_, transform_);
  clip_region_ = region;
  if (!clip_region_.isEmpty())
    LoadClipRegion();
}

void VectorPlatformDeviceEmf::DrawToNativeContext(HDC dc, int x, int y,
                                                  const RECT* src_rect) {
  SkASSERT(false);
}

void VectorPlatformDeviceEmf::LoadClipRegion() {
  SkMatrix t;
  t.reset();
  LoadClippingRegionToDC(hdc_, clip_region_, t);
}

SkBaseDevice* VectorPlatformDeviceEmf::onCreateCompatibleDevice(
    SkBitmap::Config config, int width, int height, bool isOpaque,
    Usage /*usage*/) {
  SkASSERT(config == SkBitmap::kARGB_8888_Config);
  return VectorPlatformDeviceEmf::CreateDevice(width, height, isOpaque, NULL);
}

bool VectorPlatformDeviceEmf::CreateBrush(bool use_brush, COLORREF color) {
  SkASSERT(previous_brush_ == NULL);
  // We can't use SetDCBrushColor() or DC_BRUSH when drawing to a EMF buffer.
  // SetDCBrushColor() calls are not recorded at all and DC_BRUSH will use
  // WHITE_BRUSH instead.

  if (!use_brush) {
    // Set the transparency.
    if (0 == SetBkMode(hdc_, TRANSPARENT)) {
      SkASSERT(false);
      return false;
    }

    // Select the NULL brush.
    previous_brush_ = SelectObject(GetStockObject(NULL_BRUSH));
    return previous_brush_ != NULL;
  }

  // Set the opacity.
  if (0 == SetBkMode(hdc_, OPAQUE)) {
    SkASSERT(false);
    return false;
  }

  // Create and select the brush.
  previous_brush_ = SelectObject(CreateSolidBrush(color));
  return previous_brush_ != NULL;
}

bool VectorPlatformDeviceEmf::CreatePen(bool use_pen,
                                        COLORREF color,
                                        int stroke_width,
                                        float stroke_miter,
                                        DWORD pen_style) {
  SkASSERT(previous_pen_ == NULL);
  // We can't use SetDCPenColor() or DC_PEN when drawing to a EMF buffer.
  // SetDCPenColor() calls are not recorded at all and DC_PEN will use BLACK_PEN
  // instead.

  // No pen case
  if (!use_pen) {
    previous_pen_ = SelectObject(GetStockObject(NULL_PEN));
    return previous_pen_ != NULL;
  }

  // Use the stock pen if the stroke width is 0.
  if (stroke_width == 0) {
    // Create a pen with the right color.
    previous_pen_ = SelectObject(::CreatePen(PS_SOLID, 0, color));
    return previous_pen_ != NULL;
  }

  // Load a custom pen.
  LOGBRUSH brush;
  brush.lbStyle = BS_SOLID;
  brush.lbColor = color;
  brush.lbHatch = 0;
  HPEN pen = ExtCreatePen(pen_style, stroke_width, &brush, 0, NULL);
  SkASSERT(pen != NULL);
  previous_pen_ = SelectObject(pen);
  if (previous_pen_ == NULL)
    return false;

  if (!SetMiterLimit(hdc_, stroke_miter, NULL)) {
    SkASSERT(false);
    return false;
  }
  return true;
}

void VectorPlatformDeviceEmf::Cleanup() {
  if (previous_brush_) {
    HGDIOBJ result = SelectObject(previous_brush_);
    previous_brush_ = NULL;
    if (result) {
      BOOL res = DeleteObject(result);
      SkASSERT(res != 0);
    }
  }
  if (previous_pen_) {
    HGDIOBJ result = SelectObject(previous_pen_);
    previous_pen_ = NULL;
    if (result) {
      BOOL res = DeleteObject(result);
      SkASSERT(res != 0);
    }
  }
  // Remove any loaded path from the context.
  AbortPath(hdc_);
}

HGDIOBJ VectorPlatformDeviceEmf::SelectObject(HGDIOBJ object) {
  HGDIOBJ result = ::SelectObject(hdc_, object);
  SkASSERT(result != HGDI_ERROR);
  if (result == HGDI_ERROR)
    return NULL;
  return result;
}

bool VectorPlatformDeviceEmf::CreateBrush(bool use_brush,
                                          const SkPaint& paint) {
  // Make sure that for transparent color, no brush is used.
  if (paint.getAlpha() == 0) {
    use_brush = false;
  }

  return CreateBrush(use_brush, SkColorToCOLORREF(paint.getColor()));
}

bool VectorPlatformDeviceEmf::CreatePen(bool use_pen, const SkPaint& paint) {
  // Make sure that for transparent color, no pen is used.
  if (paint.getAlpha() == 0) {
    use_pen = false;
  }

  DWORD pen_style = PS_GEOMETRIC | PS_SOLID;
  switch (paint.getStrokeJoin()) {
    case SkPaint::kMiter_Join:
      // Connects path segments with a sharp join.
      pen_style |= PS_JOIN_MITER;
      break;
    case SkPaint::kRound_Join:
      // Connects path segments with a round join.
      pen_style |= PS_JOIN_ROUND;
      break;
    case SkPaint::kBevel_Join:
      // Connects path segments with a flat bevel join.
      pen_style |= PS_JOIN_BEVEL;
      break;
    default:
      SkASSERT(false);
      break;
  }
  switch (paint.getStrokeCap()) {
    case SkPaint::kButt_Cap:
      // Begin/end contours with no extension.
      pen_style |= PS_ENDCAP_FLAT;
      break;
    case SkPaint::kRound_Cap:
      // Begin/end contours with a semi-circle extension.
      pen_style |= PS_ENDCAP_ROUND;
      break;
    case SkPaint::kSquare_Cap:
      // Begin/end contours with a half square extension.
      pen_style |= PS_ENDCAP_SQUARE;
      break;
    default:
      SkASSERT(false);
      break;
  }

  return CreatePen(use_pen,
                   SkColorToCOLORREF(paint.getColor()),
                   SkScalarRoundToInt(paint.getStrokeWidth()),
                   paint.getStrokeMiter(),
                   pen_style);
}

void VectorPlatformDeviceEmf::InternalDrawBitmap(const SkBitmap& bitmap,
                                                 int x, int y,
                                                 const SkPaint& paint) {
  unsigned char alpha = paint.getAlpha();
  if (alpha == 0)
    return;

  bool is_translucent;
  if (alpha != 255) {
    // ApplyPaint expect an opaque color.
    SkPaint tmp_paint(paint);
    tmp_paint.setAlpha(255);
    if (!ApplyPaint(tmp_paint))
      return;
    is_translucent = true;
  } else {
    if (!ApplyPaint(paint))
      return;
    is_translucent = false;
  }
  int src_size_x = bitmap.width();
  int src_size_y = bitmap.height();
  if (!src_size_x || !src_size_y)
    return;

  // Create a BMP v4 header that we can serialize. We use the shared "V3"
  // fillter to fill the stardard items, then add in the "V4" stuff we want.
  BITMAPV4HEADER bitmap_header;
  memset(&bitmap_header, 0, sizeof(BITMAPV4HEADER));
  FillBitmapInfoHeader(src_size_x, src_size_y,
                       reinterpret_cast<BITMAPINFOHEADER*>(&bitmap_header));
  bitmap_header.bV4Size = sizeof(BITMAPV4HEADER);
  bitmap_header.bV4RedMask   = 0x00ff0000;
  bitmap_header.bV4GreenMask = 0x0000ff00;
  bitmap_header.bV4BlueMask  = 0x000000ff;
  bitmap_header.bV4AlphaMask = 0xff000000;

  SkAutoLockPixels lock(bitmap);
  SkASSERT(bitmap.config() == SkBitmap::kARGB_8888_Config);
  const uint32_t* pixels = static_cast<const uint32_t*>(bitmap.getPixels());
  if (pixels == NULL) {
    SkASSERT(false);
    return;
  }

  if (!is_translucent) {
    int row_length = bitmap.rowBytesAsPixels();
    // There is no quick way to determine if an image is opaque.
    for (int y2 = 0; y2 < src_size_y; ++y2) {
      for (int x2 = 0; x2 < src_size_x; ++x2) {
        if (SkColorGetA(pixels[(y2 * row_length) + x2]) != 255) {
          is_translucent = true;
          y2 = src_size_y;
          break;
        }
      }
    }
  }

  HDC dc = BeginPlatformPaint();
  BITMAPINFOHEADER hdr;
  FillBitmapInfoHeader(src_size_x, src_size_y, &hdr);
  if (is_translucent) {
    // The image must be loaded as a bitmap inside a device context.
    HDC bitmap_dc = ::CreateCompatibleDC(dc);
    void* bits = NULL;
    HBITMAP hbitmap = ::CreateDIBSection(
        bitmap_dc, reinterpret_cast<const BITMAPINFO*>(&hdr),
        DIB_RGB_COLORS, &bits, NULL, 0);

    // static cast to a char so we can do byte ptr arithmatic to
    // get the offset.
    unsigned char* dest_buffer = static_cast<unsigned char *>(bits);

    // We will copy row by row to avoid having to worry about
    // the row strides being different.
    const int dest_row_size = hdr.biBitCount / 8 * hdr.biWidth;
    for (int row = 0; row < bitmap.height(); ++row) {
      int dest_offset = row * dest_row_size;
      // pixels_offset in terms of pixel count.
      int src_offset = row * bitmap.rowBytesAsPixels();
      memcpy(dest_buffer + dest_offset, pixels + src_offset, dest_row_size);
    }
    SkASSERT(hbitmap);
    HGDIOBJ old_bitmap = ::SelectObject(bitmap_dc, hbitmap);

    // After some analysis of IE7's behavior, this is the thing to do. I was
    // sure IE7 was doing so kind of bitmasking due to the way translucent image
    // where renderered but after some windbg tracing, it is being done by the
    // printer driver after all (mostly HP printers). IE7 always use AlphaBlend
    // for bitmasked images. The trick seems to switch the stretching mode in
    // what the driver expects.
    DWORD previous_mode = GetStretchBltMode(dc);
    BOOL result = SetStretchBltMode(dc, COLORONCOLOR);
    SkASSERT(result);
    // Note that this function expect premultiplied colors (!)
    BLENDFUNCTION blend_function = {AC_SRC_OVER, 0, alpha, AC_SRC_ALPHA};
    result = GdiAlphaBlend(dc,
                           x, y,  // Destination origin.
                           src_size_x, src_size_y,  // Destination size.
                           bitmap_dc,
                           0, 0,  // Source origin.
                           src_size_x, src_size_y,  // Source size.
                           blend_function);
    SkASSERT(result);
    result = SetStretchBltMode(dc, previous_mode);
    SkASSERT(result);

    ::SelectObject(bitmap_dc, static_cast<HBITMAP>(old_bitmap));
    DeleteObject(hbitmap);
    DeleteDC(bitmap_dc);
  } else {
    int nCopied = StretchDIBits(dc,
                                x, y,  // Destination origin.
                                src_size_x, src_size_y,
                                0, 0,  // Source origin.
                                src_size_x, src_size_y,  // Source size.
                                pixels,
                                reinterpret_cast<const BITMAPINFO*>(&hdr),
                                DIB_RGB_COLORS,
                                SRCCOPY);
  }
  EndPlatformPaint();
  Cleanup();
}

}  // namespace skia