Blame docs/reference/html/user-font_8cc-example.html

Packit 908522
Packit 908522
<html xmlns="http://www.w3.org/1999/xhtml">
Packit 908522
<head>
Packit 908522
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
Packit 908522
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
Packit 908522
<meta name="generator" content="Doxygen 1.8.9.1"/>
Packit 908522
<title>cairomm: user-font.cc</title>
Packit 908522
<link href="tabs.css" rel="stylesheet" type="text/css"/>
Packit 908522
<script type="text/javascript" src="jquery.js"></script>
Packit 908522
<script type="text/javascript" src="dynsections.js"></script>
Packit 908522
<link href="doxygen.css" rel="stylesheet" type="text/css" />
Packit 908522
</head>
Packit 908522
<body>
Packit 908522
Packit 908522
Packit 908522
Packit 908522
 
Packit 908522
 
Packit 908522
  
Packit 908522
   
cairomm
Packit 908522
    1.0
Packit 908522
   
Packit 908522
  
Packit 908522
 
Packit 908522
 
Packit 908522
Packit 908522
Packit 908522
Packit 908522
Packit 908522
  
Packit 908522
    
    Packit 908522
          
  • Main Page
  • Packit 908522
          
  • Related Pages
  • Packit 908522
          
  • Namespaces
  • Packit 908522
          
  • Classes
  • Packit 908522
          
  • Examples
  • Packit 908522
        
    Packit 908522
      
    Packit 908522
    Packit 908522
    Packit 908522
      
    Packit 908522
    user-font.cc
    Packit 908522
    Packit 908522
    Packit 908522

    A relatively simple example of using Cairo::UserFontFace

    Packit 908522
    #include <cairomm/cairomm.h>
    Packit 908522
    #include <iostream>
    Packit 908522
    #include <map>
    Packit 908522
    Packit 908522
    const double HEIGHT = 200.0;
    Packit 908522
    const double WIDTH = 400.0;
    Packit 908522
    const double FONT_SIZE = 64.0;
    Packit 908522
    const double TEXT_ORIGIN_Y = (HEIGHT / 2.0) + (FONT_SIZE / 2.0);
    Packit 908522
    const double TEXT_ORIGIN_X = 50.0; // arbitrary
    Packit 908522
    const double GLYPH_SPACING = 0.1;
    Packit 908522
    Packit 908522
    struct GlyphBounds
    Packit 908522
    {
    Packit 908522
    unsigned long glyph;
    Packit 908522
    double width;
    Packit 908522
    double height;
    Packit 908522
    };
    Packit 908522
    Packit 908522
    // an array that stores the bounds of the glyphs that we're going to draw
    Packit 908522
    static const GlyphBounds glyphs[] =
    Packit 908522
    {
    Packit 908522
    { 'c', 0.45, 0.5 },
    Packit 908522
    { 'a', 0.45, 0.5 },
    Packit 908522
    { 'i', 0.2, 0.75 },
    Packit 908522
    { 'r', 0.4, 0.5 },
    Packit 908522
    { 'o', 0.44, 0.5 },
    Packit 908522
    { 'm', 0.75, 0.5 },
    Packit 908522
    { '!', 0.2, 0.75 }
    Packit 908522
    };
    Packit 908522
    Packit 908522
    // A *very* simple font that just draws a box for every glyph
    Packit 908522
    class BoxFontFace : public Cairo::UserFontFace
    Packit 908522
    {
    Packit 908522
    public:
    Packit 908522
    // Derived user font classes should have a factory method to create an object
    Packit 908522
    // and return it with a RefPtr
    Packit 908522
    Packit 908522
    {
    Packit 908522
    return Cairo::RefPtr<BoxFontFace>(new BoxFontFace());
    Packit 908522
    }
    Packit 908522
    Packit 908522
    Cairo::ErrorStatus
    Packit 908522
    init(const Cairo::RefPtr<Cairo::ScaledFont>& /*scaled_font*/,
    Packit 908522
    Packit 908522
    Cairo::FontExtents &extents) override
    Packit 908522
    {
    Packit 908522
    double max = 0;
    Packit 908522
    for (unsigned int i = 0; i < sizeof (glyphs) / sizeof (GlyphBounds); ++i) {
    Packit 908522
    if (glyphs[i].width > max)
    Packit 908522
    max = glyphs[i].width;
    Packit 908522
    }
    Packit 908522
    // add some spacing between characters
    Packit 908522
    max += GLYPH_SPACING;
    Packit 908522
    extents.max_x_advance = max;
    Packit 908522
    return CAIRO_STATUS_SUCCESS;
    Packit 908522
    }
    Packit 908522
    Packit 908522
    Cairo::ErrorStatus
    Packit 908522
    Packit 908522
    unsigned long unicode, unsigned long& glyph) override
    Packit 908522
    {
    Packit 908522
    glyph = 0;
    Packit 908522
    // yes this is a stupid an ineffienct way to do this but we only have a few
    Packit 908522
    // glyphs and this is just demonstration code
    Packit 908522
    for (unsigned int i = 0; i < sizeof (glyphs) / sizeof (GlyphBounds); ++i) {
    Packit 908522
    if (glyphs[i].glyph == unicode) {
    Packit 908522
    // glyph 0 is often a special glyph-not-found value, so offset it by 1
    Packit 908522
    glyph = i+1;
    Packit 908522
    break;
    Packit 908522
    }
    Packit 908522
    }
    Packit 908522
    return CAIRO_STATUS_SUCCESS;
    Packit 908522
    }
    Packit 908522
    Packit 908522
    Cairo::ErrorStatus
    Packit 908522
    Packit 908522
    unsigned long glyph,
    Packit 908522
    Packit 908522
    Cairo::TextExtents& metrics) override
    Packit 908522
    {
    Packit 908522
    // check that the glyph is in our table
    Packit 908522
    if (glyph >= 1 && glyph <= sizeof(glyphs)/sizeof(GlyphBounds)) {
    Packit 908522
    cr->set_line_width(0.05);
    Packit 908522
    // Need a negative Y value since the text origin is at the bottom left point
    Packit 908522
    // and cairo's positive Y axis is down and we want to draw up
    Packit 908522
    cr->rectangle(0.0, 0.0, glyphs[glyph-1].width, -glyphs[glyph-1].height);
    Packit 908522
    cr->stroke();
    Packit 908522
    metrics.x_advance = glyphs[glyph-1].width + GLYPH_SPACING;
    Packit 908522
    }
    Packit 908522
    return CAIRO_STATUS_SUCCESS;
    Packit 908522
    }
    Packit 908522
    Packit 908522
    protected:
    Packit 908522
    // FontFace is a ref-counted object, so the constructor should be protected so
    Packit 908522
    // it is not created without a refptr to manage it. See the create() method
    Packit 908522
    BoxFontFace() : UserFontFace() { }
    Packit 908522
    };
    Packit 908522
    Packit 908522
    int main(int, char**)
    Packit 908522
    {
    Packit 908522
    auto surface =
    Packit 908522
    Packit 908522
    auto cr = Cairo::Context::create(surface);
    Packit 908522
    // fill background in white
    Packit 908522
    cr->set_source_rgb(1.0, 1.0, 1.0);
    Packit 908522
    cr->paint();
    Packit 908522
    Packit 908522
    // draw a little dot at the point where text will be drawn
    Packit 908522
    cr->arc(TEXT_ORIGIN_X, TEXT_ORIGIN_Y, FONT_SIZE / 4.0, 0, 2*M_PI);
    Packit 908522
    cr->set_source_rgba(0.0, 1.0, 0.0, 0.5);
    Packit 908522
    cr->fill();
    Packit 908522
    Packit 908522
    // draw the text
    Packit 908522
    cr->move_to(TEXT_ORIGIN_X, TEXT_ORIGIN_Y);
    Packit 908522
    cr->set_source_rgb(0.8, 0.2, 0.2);
    Packit 908522
    auto font = BoxFontFace::create();
    Packit 908522
    cr->set_font_face(font);
    Packit 908522
    cr->set_font_size(FONT_SIZE);
    Packit 908522
    cr->show_text("cairomm!");
    Packit 908522
    Packit 908522
    // Now show it with the toy text API to demonstrate how the glyphs match up
    Packit 908522
    cr->move_to(TEXT_ORIGIN_X, TEXT_ORIGIN_Y);
    Packit 908522
    cr->set_source_rgba(0.2, 0.2, 0.2, 0.3);
    Packit 908522
    auto toy_font =
    Packit 908522
    Cairo::ToyFontFace::create("Bitstream Charter",
    Packit 908522
    Packit 908522
    Packit 908522
    cr->set_font_face(toy_font);
    Packit 908522
    cr->set_font_size(FONT_SIZE);
    Packit 908522
    cr->show_text("cairomm!");
    Packit 908522
    Packit 908522
    const char* filename = "user-font.png";
    Packit 908522
    try {
    Packit 908522
    surface->write_to_png(filename);
    Packit 908522
    std::cout << "Wrote Image " << filename << std::endl;
    Packit 908522
    return 0;
    Packit 908522
    } catch (const std::exception& e)
    Packit 908522
    {
    Packit 908522
    std::cout << "** Unable to write Image " << filename << std::endl;
    Packit 908522
    return 1;
    Packit 908522
    }
    Packit 908522
    }
    Packit 908522
     
    Packit 908522
    Packit 908522

    <address class="footer"><small>
    Packit 908522
    Generated on Mon Sep 21 2015 21:56:36 for cairomm by  
    Packit 908522
    doxygen
    Packit 908522
     1.8.9.1
    Packit 908522
    </small></address>
    Packit 908522
    </body>
    Packit 908522
    </html>