| 1 | #include <cstdlib> |
|---|
| 2 | #include <sstream> |
|---|
| 3 | #include <osg/io_utils> |
|---|
| 4 | #include <osg/ArgumentParser> |
|---|
| 5 | #include <osg/Geode> |
|---|
| 6 | #include <osgViewer/Viewer> |
|---|
| 7 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 8 | |
|---|
| 9 | void textInfo(osgText::Text* text) |
|---|
| 10 | { |
|---|
| 11 | const osgText::Text::TextureGlyphQuadMap& tgqm = text->getTextureGlyphQuadMap(); |
|---|
| 12 | |
|---|
| 13 | const osgText::Text::TextureGlyphQuadMap::const_iterator tgqmi = tgqm.begin(); |
|---|
| 14 | |
|---|
| 15 | const osgText::Text::GlyphQuads& gq = tgqmi->second; |
|---|
| 16 | |
|---|
| 17 | osgText::String& s = text->getText(); |
|---|
| 18 | |
|---|
| 19 | for(unsigned int i = 0; i < s.size(); i++) |
|---|
| 20 | { |
|---|
| 21 | osg::Vec2 ul = gq.getCoords()[0 + (i * 4)]; |
|---|
| 22 | osg::Vec2 ll = gq.getCoords()[1 + (i * 4)]; |
|---|
| 23 | osg::Vec2 lr = gq.getCoords()[2 + (i * 4)]; |
|---|
| 24 | osg::Vec2 ur = gq.getCoords()[3 + (i * 4)]; |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | osg::notify(osg::NOTICE) |
|---|
| 34 | << "'" << static_cast<char>(s[i]) << "':" |
|---|
| 35 | << " width(" << lr.x() - ll.x() << ")" |
|---|
| 36 | << " height(" << ul.y() - ll.y() << ")" << std::endl << "\t" |
|---|
| 37 | << "ul(" << ul << "), " |
|---|
| 38 | << "ll(" << ll << "), " |
|---|
| 39 | << "lr(" << lr << "), " |
|---|
| 40 | << "ur(" << ur << ")" |
|---|
| 41 | << std::endl |
|---|
| 42 | ; |
|---|
| 43 | } |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | osg::Camera* createOrthoCamera(double width, double height) |
|---|
| 47 | { |
|---|
| 48 | osg::Camera* camera = new osg::Camera(); |
|---|
| 49 | |
|---|
| 50 | camera->getOrCreateStateSet()->setMode( |
|---|
| 51 | GL_LIGHTING, |
|---|
| 52 | osg::StateAttribute::PROTECTED | osg::StateAttribute::OFF |
|---|
| 53 | ); |
|---|
| 54 | |
|---|
| 55 | osg::Matrix m = osg::Matrix::ortho2D(0.0f, width, 0.0f, height); |
|---|
| 56 | |
|---|
| 57 | camera->setProjectionMatrix(m); |
|---|
| 58 | camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
|---|
| 59 | camera->setViewMatrix(osg::Matrix::identity()); |
|---|
| 60 | camera->setClearMask(GL_DEPTH_BUFFER_BIT); |
|---|
| 61 | camera->setRenderOrder(osg::Camera::POST_RENDER); |
|---|
| 62 | |
|---|
| 63 | return camera; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | osgText::Text* createLabel(const std::string& l, const char* f, unsigned int size) |
|---|
| 67 | { |
|---|
| 68 | static osg::Vec3 pos(10.0f, 10.0f, 0.0f); |
|---|
| 69 | |
|---|
| 70 | osgText::Text* label = new osgText::Text(); |
|---|
| 71 | osgText::Font* font = osgText::readFontFile(f); |
|---|
| 72 | |
|---|
| 73 | label->setFont(font); |
|---|
| 74 | label->setCharacterSize(size); |
|---|
| 75 | label->setFontResolution(size, size); |
|---|
| 76 | label->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f)); |
|---|
| 77 | label->setPosition(pos); |
|---|
| 78 | label->setAlignment(osgText::Text::LEFT_BOTTOM); |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | label->setText(l); |
|---|
| 82 | |
|---|
| 83 | textInfo(label); |
|---|
| 84 | |
|---|
| 85 | pos.y() += size + 10.0f; |
|---|
| 86 | |
|---|
| 87 | return label; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | typedef std::list<unsigned int> Sizes; |
|---|
| 91 | |
|---|
| 92 | int main(int argc, char** argv) |
|---|
| 93 | { |
|---|
| 94 | osgViewer::Viewer viewer; |
|---|
| 95 | osg::ArgumentParser args(&argc, argv); |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | if(argc <= 2) |
|---|
| 99 | { |
|---|
| 100 | osg::notify(osg::FATAL) << "usage: " << args[0] << " FONTFILE [sizes...]" << std::endl; |
|---|
| 101 | |
|---|
| 102 | return 1; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | viewer.addEventHandler(new osgViewer::StatsHandler()); |
|---|
| 106 | viewer.addEventHandler(new osgViewer::WindowSizeHandler()); |
|---|
| 107 | |
|---|
| 108 | osg::Group* group = new osg::Group(); |
|---|
| 109 | osg::Camera* camera = createOrthoCamera(1280.0f, 1024.0f); |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | Sizes sizes; |
|---|
| 113 | |
|---|
| 114 | for(int i = 2; i < argc; i++) |
|---|
| 115 | { |
|---|
| 116 | if(!args.isNumber(i)) continue; |
|---|
| 117 | |
|---|
| 118 | sizes.push_back(std::atoi(args[i])); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | osg::Geode* geode = new osg::Geode(); |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | for(Sizes::const_iterator i = sizes.begin(); i != sizes.end(); i++) |
|---|
| 125 | { |
|---|
| 126 | std::stringstream ss; |
|---|
| 127 | |
|---|
| 128 | ss << *i << " 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|---|
| 129 | |
|---|
| 130 | geode->addDrawable(createLabel(ss.str(), args[1], *i)); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | camera->addChild(geode); |
|---|
| 134 | |
|---|
| 135 | group->addChild(camera); |
|---|
| 136 | |
|---|
| 137 | viewer.setSceneData(group); |
|---|
| 138 | |
|---|
| 139 | return viewer.run(); |
|---|
| 140 | } |
|---|