Anti-Grain Geometry Tutorial
tutorial_font_1.cpp
1 /*
2  * Copyright (c) 2019 Heng Yuan
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <agg_conv_curve.h>
17 #include <agg_pixfmt_rgba.h>
18 #include <agg_rasterizer_scanline_aa.h>
19 #include <agg_renderer_base.h>
20 #include <agg_renderer_scanline.h>
21 #include <agg_scanline_p.h>
22 #include <agg_font_freetype.h>
23 
24 #include "path.h"
25 #include "writepng.h"
26 
29 
30 void
31 drawText (const char* str,
32  double x,
33  double y,
34  bool kerning,
37  agg::rgba8 color,
38  RendererBaseType& rBase)
39 {
40  bool first = true;
41 
42  // these three are only needed for outline rendering
45  agg::scanline_p8 scanline;
46 
47  for (; *str != 0; ++str)
48  {
49  unsigned ch = *str;
50  const agg::glyph_cache* glyph = fontCache.glyph(ch);
51  if (glyph)
52  {
53  if (first)
54  {
55  first = false;
56  }
57  else
58  {
59  if (kerning)
60  {
61  fontCache.add_kerning(&x, &y);
62  }
63  }
64  fontCache.init_embedded_adaptors(glyph, x, y);
65  switch (glyph->data_type)
66  {
67  case agg::glyph_data_mono:
68  {
69  agg::render_scanlines_bin_solid(fontCache.mono_adaptor(), fontCache.mono_scanline(), rBase, color);
70  break;
71  }
72  case agg::glyph_data_gray8:
73  {
74  agg::render_scanlines_aa_solid(fontCache.gray8_adaptor(), fontCache.gray8_scanline(), rBase, color);
75  break;
76  }
77  case agg::glyph_data_outline:
78  {
79  ras.reset();
80  ras.add_path(curves);
81  agg::render_scanlines_aa_solid(ras, scanline, rBase, color);
82  break;
83  }
84  default:
85  {
86  throw TutorialException ("Unknown glyph type: %d", glyph->data_type);
87  }
88  }
89  x += glyph->advance_x;
90  y += glyph->advance_y;
91  }
92  }
93 }
94 
95 int
96 main (int argc, const char* argv[])
97 {
98  try
99  {
100  const int imageWidth = 550;
101  const int imageHeight = 300;
102 
103  const int pixelSize = PixelFormat::pix_width;
104 
105  agg::rendering_buffer renderBuffer;
106  PixelFormat pixFmt;
107  RendererBaseType rBase;
108 
109  // Allocate a 100 x 100 rgba buffer
110  unsigned char *imageBuffer = new unsigned char[imageWidth * imageHeight * pixelSize];
111 
112  // Hook everything up
113  renderBuffer.attach (imageBuffer, imageWidth, imageHeight, imageWidth * pixelSize);
114  pixFmt.attach(renderBuffer);
115  rBase.attach(pixFmt);
116 
117  const agg::rgba8 transparentwhiteColor (0xff, 0xff, 0xff, 0);
118  const agg::rgba8 blueColor (0, 0, 0xff, 0xff);
119 
120  // clear the buffer with transparent white color
121  rBase.clear(transparentwhiteColor);
122 
124  agg::scanline_p8 scanline;
125 
126  ras.auto_close(false);
127 
130 
131  char text[] = "Hello World!";
132 
133  // hard code path for simplicity.
134  char fontPath[] = "../fonts/DejaVuSerif.ttf";
135  double fontSize = 80;
136  bool fontHint = true;
137  bool fontKerning = true;
138 
139  // mono
140  {
141  if (!fontEngine.load_font(fontPath, 0, agg::glyph_ren_agg_mono))
142  {
143  throw TutorialException ("Font is not found.");
144  }
145 
146  // enable font hint
147  fontEngine.hinting(fontHint);
148  // flip y since the image coordinate system has the origin at
149  // top left corner and y axis is flipped
150  fontEngine.flip_y(true);
151  // set font size
152  fontEngine.height(fontSize);
153  drawText (text,
154  10, 80,
155  fontKerning,
156  fontEngine,
157  fontCache,
158  blueColor,
159  rBase);
160  }
161 
162  // gray8
163  {
164  if (!fontEngine.load_font(fontPath, 0, agg::glyph_ren_agg_gray8))
165  {
166  throw TutorialException ("Font is not found.");
167  }
168 
169  // enable font hint
170  fontEngine.hinting(fontHint);
171  // flip y since the image coordinate system has the origin at
172  // top left corner and y axis is flipped
173  fontEngine.flip_y(true);
174  // set font size
175  fontEngine.height(fontSize);
176  drawText (text,
177  10, 180,
178  fontKerning,
179  fontEngine,
180  fontCache,
181  blueColor,
182  rBase);
183  }
184 
185  // outline
186  {
187  if (!fontEngine.load_font(fontPath, 0, agg::glyph_ren_outline))
188  {
189  throw TutorialException ("Font is not found.");
190  }
191 
192  // enable font hint
193  fontEngine.hinting(fontHint);
194  // flip y since the image coordinate system has the origin at
195  // top left corner and y axis is flipped
196  fontEngine.flip_y(true);
197  // set font size
198  fontEngine.height(fontSize);
199  drawText (text,
200  10, 280,
201  fontKerning,
202  fontEngine,
203  fontCache,
204  blueColor,
205  rBase);
206  }
207 
208  // now write the image out for visualization
209  char fileName[1000] = { 0 };
210  if (argc > 1)
211  {
212  sprintf (fileName, "%s/", argv[1]);
213  }
214  strcat(fileName, "tutorial_font_1.png");
215  writePng<RendererBaseType> (fileName, rBase);
216 
217  delete imageBuffer;
218  }
219  catch (TutorialException& ex)
220  {
221  printf ("%s\n", ex.getMessage());
222  return 1;
223  }
224  catch (...)
225  {
226  printf ("Unknown exception detected.\n");
227  return 1;
228  }
229 
230  return 0;
231 }