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 <stdio.h> 6 7 #include "ppapi/cpp/completion_callback.h" 8 #include "ppapi/cpp/graphics_2d.h" 9 #include "ppapi/cpp/image_data.h" 10 #include "ppapi/cpp/instance.h" 11 #include "ppapi/cpp/module.h" 12 #include "ppapi/cpp/rect.h" 13 #include "ppapi/cpp/size.h" 14 #include "ppapi/cpp/trusted/browser_font_trusted.h" 15 16 static void DummyCompletionCallback(void* /*user_data*/, int32_t /*result*/) { 17 } 18 19 class MyInstance : public pp::Instance { 20 public: 21 MyInstance(PP_Instance instance) 22 : pp::Instance(instance) { 23 } 24 25 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) { 26 if (position.size() == last_size_) 27 return; 28 last_size_ = position.size(); 29 30 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, last_size_, true); 31 pp::Graphics2D graphics(this, last_size_, false); 32 BindGraphics(graphics); 33 34 pp::BrowserFontDescription desc; 35 desc.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_SANSSERIF); 36 desc.set_size(100); 37 pp::BrowserFont_Trusted font(this, desc); 38 39 // Draw some large, alpha blended text, including Arabic shaping. 40 pp::Rect text_clip(position.size()); // Use entire bounds for clip. 41 font.DrawTextAt(&image, 42 pp::BrowserFontTextRun( 43 "\xD9\x85\xD8\xB1\xD8\xAD\xD8\xA8\xD8\xA7\xE2\x80\x8E", true, true), 44 pp::Point(20, 100), 0x80008000, clip, false); 45 46 // Draw the default font names and sizes. 47 int y = 160; 48 { 49 pp::BrowserFontDescription desc; 50 pp::BrowserFont_Trusted 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::BrowserFontDescription desc; 58 desc.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_SERIF); 59 pp::BrowserFont_Trusted 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::BrowserFontDescription desc; 67 desc.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_SANSSERIF); 68 pp::BrowserFont_Trusted 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::BrowserFontDescription desc; 76 desc.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_MONOSPACE); 77 pp::BrowserFont_Trusted 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 } 83 84 graphics.PaintImageData(image, pp::Point(0, 0)); 85 graphics.Flush(pp::CompletionCallback(&DummyCompletionCallback, NULL)); 86 } 87 88 private: 89 // Returns a string describing the given font, using the given title. 90 std::string DescribeFont(const pp::BrowserFont_Trusted& font, 91 const char* title) { 92 pp::BrowserFontDescription desc; 93 PP_BrowserFont_Trusted_Metrics metrics; 94 font.Describe(&desc, &metrics); 95 96 char buf[256]; 97 sprintf(buf, "%s = %s %dpt", 98 title, desc.face().AsString().c_str(), desc.size()); 99 return std::string(buf); 100 } 101 102 pp::Size last_size_; 103 }; 104 105 class MyModule : public pp::Module { 106 public: 107 virtual pp::Instance* CreateInstance(PP_Instance instance) { 108 return new MyInstance(instance); 109 } 110 }; 111 112 namespace pp { 113 114 // Factory function for your specialization of the Module object. 115 Module* CreateModule() { 116 return new MyModule(); 117 } 118 119 } // namespace pp 120