1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stdint.h>
6 #include <stdio.h>
7
8 #include "ppapi/cpp/completion_callback.h"
9 #include "ppapi/cpp/graphics_2d.h"
10 #include "ppapi/cpp/image_data.h"
11 #include "ppapi/cpp/instance.h"
12 #include "ppapi/cpp/module.h"
13 #include "ppapi/cpp/rect.h"
14 #include "ppapi/cpp/size.h"
15 #include "ppapi/cpp/trusted/browser_font_trusted.h"
16
DummyCompletionCallback(void *,int32_t)17 static void DummyCompletionCallback(void* /*user_data*/, int32_t /*result*/) {
18 }
19
20 class MyInstance : public pp::Instance {
21 public:
MyInstance(PP_Instance instance)22 MyInstance(PP_Instance instance)
23 : pp::Instance(instance) {
24 }
25
DidChangeView(const pp::Rect & position,const pp::Rect & clip)26 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) {
27 if (position.size() == last_size_)
28 return;
29 last_size_ = position.size();
30
31 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, last_size_, true);
32 pp::Graphics2D graphics(this, last_size_, false);
33 BindGraphics(graphics);
34
35 pp::BrowserFontDescription desc;
36 desc.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_SANSSERIF);
37 desc.set_size(100);
38 pp::BrowserFont_Trusted font(this, desc);
39
40 // Draw some large, alpha blended text, including Arabic shaping.
41 pp::Rect text_clip(position.size()); // Use entire bounds for clip.
42 font.DrawTextAt(&image,
43 pp::BrowserFontTextRun(
44 "\xD9\x85\xD8\xB1\xD8\xAD\xD8\xA8\xD8\xA7\xE2\x80\x8E", true, true),
45 pp::Point(20, 100), 0x80008000, clip, false);
46
47 // Draw the default font names and sizes.
48 int y = 160;
49 {
50 pp::BrowserFontDescription desc;
51 pp::BrowserFont_Trusted default_font(this, desc);
52 default_font.DrawSimpleText(
53 &image, DescribeFont(default_font, "Default font"),
54 pp::Point(10, y), 0xFF000000);
55 y += 20;
56 }
57 {
58 pp::BrowserFontDescription desc;
59 desc.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_SERIF);
60 pp::BrowserFont_Trusted serif_font(this, desc);
61 serif_font.DrawSimpleText(
62 &image, DescribeFont(serif_font, "Serif font"),
63 pp::Point(10, y), 0xFF000000);
64 y += 20;
65 }
66 {
67 pp::BrowserFontDescription desc;
68 desc.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_SANSSERIF);
69 pp::BrowserFont_Trusted sans_serif_font(this, desc);
70 sans_serif_font.DrawSimpleText(
71 &image, DescribeFont(sans_serif_font, "Sans serif font"),
72 pp::Point(10, y), 0xFF000000);
73 y += 20;
74 }
75 {
76 pp::BrowserFontDescription desc;
77 desc.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_MONOSPACE);
78 pp::BrowserFont_Trusted monospace_font(this, desc);
79 monospace_font.DrawSimpleText(
80 &image, DescribeFont(monospace_font, "Monospace font"),
81 pp::Point(10, y), 0xFF000000);
82 y += 20;
83 }
84
85 graphics.PaintImageData(image, pp::Point(0, 0));
86 graphics.Flush(pp::CompletionCallback(&DummyCompletionCallback, NULL));
87 }
88
89 private:
90 // Returns a string describing the given font, using the given title.
DescribeFont(const pp::BrowserFont_Trusted & font,const char * title)91 std::string DescribeFont(const pp::BrowserFont_Trusted& font,
92 const char* title) {
93 pp::BrowserFontDescription desc;
94 PP_BrowserFont_Trusted_Metrics metrics;
95 font.Describe(&desc, &metrics);
96
97 char buf[256];
98 sprintf(buf, "%s = %s %dpt",
99 title, desc.face().AsString().c_str(), desc.size());
100 return std::string(buf);
101 }
102
103 pp::Size last_size_;
104 };
105
106 class MyModule : public pp::Module {
107 public:
CreateInstance(PP_Instance instance)108 virtual pp::Instance* CreateInstance(PP_Instance instance) {
109 return new MyInstance(instance);
110 }
111 };
112
113 namespace pp {
114
115 // Factory function for your specialization of the Module object.
CreateModule()116 Module* CreateModule() {
117 return new MyModule();
118 }
119
120 } // namespace pp
121