File: user-font_8cc-example.html

package info (click to toggle)
cairomm 1.10.0-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,496 kB
  • sloc: sh: 10,127; cpp: 5,652; perl: 236; makefile: 157
file content (174 lines) | stat: -rw-r--r-- 13,797 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
        <title>cairomm: user-font.cc</title>
        <link href="cairomm.css" rel="stylesheet" type="text/css">
    </head>
<body>
<!-- Generated by Doxygen 1.7.3 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<h1>user-font.cc</h1> </div>
</div>
<div class="contents">
<p>A relatively simple example of using <a class="el" href="classCairo_1_1UserFontFace.html" title="Font support with font data provided by the user.">Cairo::UserFontFace</a></p>
<div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;cairomm/cairomm.h&gt;</span>
<span class="preprocessor">#include &lt;iostream&gt;</span>
<span class="preprocessor">#include &lt;map&gt;</span>

<span class="keyword">const</span> <span class="keywordtype">double</span> HEIGHT = 200.0;
<span class="keyword">const</span> <span class="keywordtype">double</span> WIDTH = 400.0;
<span class="keyword">const</span> <span class="keywordtype">double</span> FONT_SIZE = 64.0;
<span class="keyword">const</span> <span class="keywordtype">double</span> TEXT_ORIGIN_Y = (HEIGHT / 2.0) + (FONT_SIZE / 2.0);
<span class="keyword">const</span> <span class="keywordtype">double</span> TEXT_ORIGIN_X = 50.0; <span class="comment">// arbitrary</span>
<span class="keyword">const</span> <span class="keywordtype">double</span> GLYPH_SPACING = 0.1;

<span class="keyword">struct </span>GlyphBounds
{
  <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> glyph;
  <span class="keywordtype">double</span> width;
  <span class="keywordtype">double</span> height;
};

<span class="comment">// an array that stores the bounds of the glyphs that we&#39;re going to draw</span>
<span class="keyword">static</span> <span class="keyword">const</span> GlyphBounds glyphs[] =
{
  { <span class="charliteral">&#39;c&#39;</span>, 0.45, 0.5 },
  { <span class="charliteral">&#39;a&#39;</span>, 0.45, 0.5 },
  { <span class="charliteral">&#39;i&#39;</span>, 0.2, 0.75 },
  { <span class="charliteral">&#39;r&#39;</span>, 0.4, 0.5 },
  { <span class="charliteral">&#39;o&#39;</span>, 0.44, 0.5 },
  { <span class="charliteral">&#39;m&#39;</span>, 0.75, 0.5 },
  { <span class="charliteral">&#39;!&#39;</span>, 0.2, 0.75 }
};

<span class="comment">// A *very* simple font that just draws a box for every glyph</span>
<span class="keyword">class </span>BoxFontFace : <span class="keyword">public</span> Cairo::UserFontFace
{
<span class="keyword">public</span>:
  <span class="comment">// Derived user font classes should have a factory method to create an object</span>
  <span class="comment">// and return it with a RefPtr</span>
  <span class="keyword">static</span> <a name="_a0"></a><a class="code" href="classCairo_1_1RefPtr.html" title="RefPtr&amp;lt;&amp;gt; is a reference-counting shared smartpointer.">Cairo::RefPtr&lt;BoxFontFace&gt;</a> create()
  {
    <span class="keywordflow">return</span> <a class="code" href="classCairo_1_1RefPtr.html" title="RefPtr&amp;lt;&amp;gt; is a reference-counting shared smartpointer.">Cairo::RefPtr&lt;BoxFontFace&gt;</a>(<span class="keyword">new</span> BoxFontFace());
  }

  <span class="keyword">virtual</span> Cairo::ErrorStatus
    init(<span class="keyword">const</span> <a class="code" href="classCairo_1_1RefPtr.html" title="RefPtr&amp;lt;&amp;gt; is a reference-counting shared smartpointer.">Cairo::RefPtr&lt;Cairo::ScaledFont&gt;</a>&amp; <span class="comment">/*scaled_font*/</span>,
         <span class="keyword">const</span> <a class="code" href="classCairo_1_1RefPtr.html" title="RefPtr&amp;lt;&amp;gt; is a reference-counting shared smartpointer.">Cairo::RefPtr&lt;Cairo::Context&gt;</a>&amp; <span class="comment">/*cr*/</span>,
         <a class="code" href="namespaceCairo.html#aa7da46c699a67bfbdd5400af7e6b7b39" title="See the cairo_font_extents_t reference in the cairo manual for more information.">Cairo::FontExtents</a> &amp;extents)
  {
    <span class="keywordtype">double</span> <a name="a1"></a><a class="codeRef" doxygen="libstdc++.tag:http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01176.html#ga8010118c8f0472172a808754940c3b66">max</a> = 0;
    <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i = 0; i &lt; <span class="keyword">sizeof</span> (glyphs) / <span class="keyword">sizeof</span> (GlyphBounds); ++i) {
      <span class="keywordflow">if</span> (glyphs[i].width &gt; max)
        max = glyphs[i].width;
    }
    <span class="comment">// add some spacing between characters</span>
    max += GLYPH_SPACING;
    extents.max_x_advance = <a class="codeRef" doxygen="libstdc++.tag:http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01176.html#ga8010118c8f0472172a808754940c3b66">max</a>;
    <span class="keywordflow">return</span> CAIRO_STATUS_SUCCESS;
  }

  <span class="keyword">virtual</span> Cairo::ErrorStatus
  unicode_to_glyph (<span class="keyword">const</span> <a class="code" href="classCairo_1_1RefPtr.html" title="RefPtr&amp;lt;&amp;gt; is a reference-counting shared smartpointer.">Cairo::RefPtr&lt;Cairo::ScaledFont&gt;</a>&amp; <span class="comment">/*scaled_font*/</span>,
                    <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> unicode, <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>&amp; glyph)
  {
    glyph = 0;
    <span class="comment">// yes this is a stupid an ineffienct way to do this but we only have a few</span>
    <span class="comment">// glyphs and this is just demonstration code</span>
    <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i = 0; i &lt; <span class="keyword">sizeof</span> (glyphs) / <span class="keyword">sizeof</span> (GlyphBounds); ++i) {
      <span class="keywordflow">if</span> (glyphs[i].glyph == unicode) {
        <span class="comment">// glyph 0 is often a special glyph-not-found value, so offset it by 1</span>
        glyph = i+1;
        <span class="keywordflow">break</span>;
      }
    }
    <span class="keywordflow">return</span> CAIRO_STATUS_SUCCESS;
  }

  <span class="keyword">virtual</span> Cairo::ErrorStatus
  render_glyph(<span class="keyword">const</span> <a class="code" href="classCairo_1_1RefPtr.html" title="RefPtr&amp;lt;&amp;gt; is a reference-counting shared smartpointer.">Cairo::RefPtr&lt;Cairo::ScaledFont&gt;</a>&amp; <span class="comment">/*scaled_font*/</span>,
               <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> glyph,
               <span class="keyword">const</span> <a class="code" href="classCairo_1_1RefPtr.html" title="RefPtr&amp;lt;&amp;gt; is a reference-counting shared smartpointer.">Cairo::RefPtr&lt;Cairo::Context&gt;</a>&amp; cr,
               <a class="code" href="namespaceCairo.html#af6d8a4744616383984c99f8f052c7a5d" title="See the cairo_text_extents_t reference in the cairo manual for more information.">Cairo::TextExtents</a>&amp; metrics)
  {
    <span class="comment">// check that the glyph is in our table</span>
    <span class="keywordflow">if</span> (glyph &gt;= 1 &amp;&amp; glyph &lt;= <span class="keyword">sizeof</span>(glyphs)/<span class="keyword">sizeof</span>(GlyphBounds)) {
      cr-&gt;set_line_width(0.05);
      <span class="comment">// Need a negative Y value since the text origin is at the bottom left point</span>
      <span class="comment">// and cairo&#39;s positive Y axis is down and we want to draw up</span>
      cr-&gt;rectangle(0.0, 0.0, glyphs[glyph-1].width, -glyphs[glyph-1].height);
      cr-&gt;stroke();
      metrics.x_advance = glyphs[glyph-1].width + GLYPH_SPACING;
    }
    <span class="keywordflow">return</span> CAIRO_STATUS_SUCCESS;
  }

<span class="keyword">protected</span>:
  <span class="comment">// FontFace is a ref-counted object, so the constructor should be protected so</span>
  <span class="comment">// it is not created without a refptr to manage it. See the create() method</span>
  BoxFontFace() : UserFontFace() { }
};

<span class="keywordtype">int</span> main(<span class="keywordtype">int</span>, <span class="keywordtype">char</span>**)
{
  <a class="code" href="classCairo_1_1RefPtr.html" title="RefPtr&amp;lt;&amp;gt; is a reference-counting shared smartpointer.">Cairo::RefPtr&lt;Cairo::ImageSurface&gt;</a> surface =
    <a name="a2"></a><a class="code" href="classCairo_1_1ImageSurface.html#a82887e1a0480ab16aa891e135f2b28d6" title="Creates an image surface of the specified format and dimensions.">Cairo::ImageSurface::create</a>(<a name="a3"></a><a class="code" href="namespaceCairo.html#ad3f86970e1bd354b263303c9b8759166afc97f1888578477fd656cf72d3421fbc">Cairo::FORMAT_ARGB32</a>, WIDTH, HEIGHT);
  <a class="code" href="classCairo_1_1RefPtr.html" title="RefPtr&amp;lt;&amp;gt; is a reference-counting shared smartpointer.">Cairo::RefPtr&lt;Cairo::Context&gt;</a> cr = <a name="a4"></a><a class="code" href="classCairo_1_1Context.html#a9a27f6ec57d788fd3ecbc310aeb24d99">Cairo::Context::create</a>(surface);
  <span class="comment">// fill background in white</span>
  cr-&gt;set_source_rgb(1.0, 1.0, 1.0);
  cr-&gt;paint();

  <span class="comment">// draw a little dot at the point where text will be drawn</span>
  cr-&gt;arc(TEXT_ORIGIN_X, TEXT_ORIGIN_Y, FONT_SIZE / 4.0, 0, 2*M_PI);
  cr-&gt;set_source_rgba(0.0, 1.0, 0.0, 0.5);
  cr-&gt;fill();

  <span class="comment">// draw the text</span>
  cr-&gt;move_to(TEXT_ORIGIN_X, TEXT_ORIGIN_Y);
  cr-&gt;set_source_rgb(0.8, 0.2, 0.2);
  <a class="code" href="classCairo_1_1RefPtr.html" title="RefPtr&amp;lt;&amp;gt; is a reference-counting shared smartpointer.">Cairo::RefPtr&lt;BoxFontFace&gt;</a> font = BoxFontFace::create();
  cr-&gt;set_font_face(font);
  cr-&gt;set_font_size(FONT_SIZE);
  cr-&gt;show_text(<span class="stringliteral">&quot;cairomm!&quot;</span>);

  <span class="comment">// Now show it with the toy text API to demonstrate how the glyphs match up</span>
  cr-&gt;move_to(TEXT_ORIGIN_X, TEXT_ORIGIN_Y);
  cr-&gt;set_source_rgba(0.2, 0.2, 0.2, 0.3);
  <a class="code" href="classCairo_1_1RefPtr.html" title="RefPtr&amp;lt;&amp;gt; is a reference-counting shared smartpointer.">Cairo::RefPtr&lt;Cairo::ToyFontFace&gt;</a> toy_font =
    <a name="a5"></a><a class="code" href="classCairo_1_1ToyFontFace.html#a07c0ca7fd0dc54c31bfa1d8a813aff59" title="Creates a font face from a triplet of family, slant, and weight.">Cairo::ToyFontFace::create</a>(<span class="stringliteral">&quot;Bitstream Charter&quot;</span>,
                               <a name="a6"></a><a class="code" href="namespaceCairo.html#a0a2c19fefac301a702a6b7fdb368bf55a428cfea8fca5951033f3d433855cb97d">Cairo::FONT_SLANT_NORMAL</a>,
                               <a name="a7"></a><a class="code" href="namespaceCairo.html#ad8514818fb9292a5864b57f4b8a1e546a438bd2ac850ef23ce7a537ae3395f398">Cairo::FONT_WEIGHT_BOLD</a>);
  cr-&gt;set_font_face(toy_font);
  cr-&gt;set_font_size(FONT_SIZE);
  cr-&gt;show_text(<span class="stringliteral">&quot;cairomm!&quot;</span>);

  <span class="keyword">const</span> <span class="keywordtype">char</span>* filename = <span class="stringliteral">&quot;user-font.png&quot;</span>;
  <span class="keywordflow">try</span> {
    surface-&gt;write_to_png(filename);
    <a name="a8"></a><a class="codeRef" doxygen="libstdc++.tag:http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01136.html#aaf93fdf0812752e0e02c501dea1b38f0">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Wrote Image &quot;</span> &lt;&lt; filename &lt;&lt; <a name="a9"></a><a class="codeRef" doxygen="libstdc++.tag:http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01136.html#a9e2ba1ed9813a1f03adc9a87dbf491a5">std::endl</a>;
    <span class="keywordflow">return</span> 0;
  } <span class="keywordflow">catch</span> (<span class="keyword">const</span> <a name="_a10"></a><a class="codeRef" doxygen="libstdc++.tag:http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00469.html">std::exception</a>&amp; e)
  {
    <a class="codeRef" doxygen="libstdc++.tag:http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01136.html#aaf93fdf0812752e0e02c501dea1b38f0">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;** Unable to write Image &quot;</span> &lt;&lt; filename &lt;&lt; <a class="codeRef" doxygen="libstdc++.tag:http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01136.html#a9e2ba1ed9813a1f03adc9a87dbf491a5">std::endl</a>;
    <span class="keywordflow">return</span> 1;
  }
}
</pre></div> </div>
</div>
<hr class="footer"/><address class="footer"><small>Generated on Mon May 9 2011 09:51:07 for cairomm by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.3 </small></address>
</body>
</html>