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