Anti-Grain Geometry Tutorial
tutorial_font_2.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_conv_stroke.h>
18 #include <agg_image_accessors.h>
19 #include <agg_path_storage.h>
20 #include <agg_pixfmt_rgba.h>
21 #include <agg_rasterizer_scanline_aa.h>
22 #include <agg_renderer_base.h>
23 #include <agg_renderer_scanline.h>
24 #include <agg_scanline_p.h>
25 #include <agg_font_freetype.h>
26 #include <agg_span_allocator.h>
27 #include <agg_span_image_filter_rgba.h>
28 #include <agg_span_interpolator_linear.h>
29 
30 #include "path.h"
31 #include "writepng.h"
32 
35 
36 template<class Path>
37 void
38 drawImage(RendererBaseType& dst, RendererBaseType& src, const agg::trans_affine& matrix, Path& path, agg::filling_rule_e fillRule)
39 {
40  typedef agg::span_interpolator_linear<agg::trans_affine> InterpolatorType;
41  typedef agg::image_accessor_clip<PixelFormat> ImageAccessorType;
43  typedef agg::span_allocator<agg::rgba8> SpanAllocatorType;
44 
45  const agg::rgba8 transparentWhiteColor (0xff, 0xff, 0xff, 0);
46 
47  double srcWidth = src.width();
48  double srcHeight = src.height();
49 
50  // from the matrix, compute the invert matrix. It will
51  // be the lookup of src pixels from the dst location.
52  agg::trans_affine invertMatrix (matrix);
53  invertMatrix.invert();
54 
55  InterpolatorType interpolator (invertMatrix);
56  ImageAccessorType ia (src.ren(), transparentWhiteColor);
57  SpanGeneratorType sg (ia, interpolator);
58 
60  agg::scanline_p8 scanline;
61  SpanAllocatorType sa;
62 
63  ras.reset();
64  ras.filling_rule(fillRule);
65  ras.add_path(path);
66  agg::render_scanlines_aa(ras, scanline, dst, sa, sg);
67 }
68 
69 void
70 genTextPath (const char* str,
71  double x,
72  double y,
73  bool kerning,
76  agg::rgba8 color,
77  agg::path_storage& path)
78 {
79  bool first = true;
80 
81  // these three are only needed for outline rendering
83 
84  for (; *str != 0; ++str)
85  {
86  unsigned ch = *str;
87  const agg::glyph_cache* glyph = fontCache.glyph(ch);
88  if (glyph)
89  {
90  if (first)
91  {
92  first = false;
93  }
94  else
95  {
96  if (kerning)
97  {
98  fontCache.add_kerning(&x, &y);
99  }
100  }
101  fontCache.init_embedded_adaptors(glyph, x, y);
102  switch (glyph->data_type)
103  {
104  case agg::glyph_data_outline:
105  {
106  path.concat_path(curves, 0);
107  break;
108  }
109  }
110  x += glyph->advance_x;
111  y += glyph->advance_y;
112  }
113  }
114 }
115 
116 int
117 main (int argc, const char* argv[])
118 {
119  try
120  {
121  const int imageWidth = 550;
122  const int imageHeight = 300;
123 
124  const int pixelSize = PixelFormat::pix_width;
125 
126  agg::rendering_buffer renderBuffer;
127  PixelFormat pixFmt;
128  RendererBaseType rBase;
129 
130  unsigned char *imageBuffer = new unsigned char[imageWidth * imageHeight * pixelSize];
131 
132  // Hook everything up
133  renderBuffer.attach (imageBuffer, imageWidth, imageHeight, imageWidth * pixelSize);
134  pixFmt.attach(renderBuffer);
135  rBase.attach(pixFmt);
136 
137  const agg::rgba8 transparentwhiteColor (0xff, 0xff, 0xff, 0);
138  const agg::rgba8 blueColor (0, 0, 0xff, 0xff);
139 
140  // clear the buffer with transparent white color
141  rBase.clear(transparentwhiteColor);
142 
144  agg::scanline_p8 scanline;
145 
146  ras.auto_close(false);
147 
150 
151  // hard code path for simplicity.
152  char fontPath[] = "../fonts/DejaVuSerif.ttf";
153  double fontSize = 80;
154  bool fontHint = true;
155  bool fontKerning = true;
156 
157  if (!fontEngine.load_font(fontPath, 0, agg::glyph_ren_outline))
158  {
159  throw TutorialException ("Font is not found.");
160  }
161  char text[] = "Hello World!";
162 
163  agg::path_storage path;
164  // enable font hint
165  fontEngine.hinting(fontHint);
166  // flip y since the image coordinate system has the origin at
167  // top left corner and y axis is flipped
168  fontEngine.flip_y(true);
169  // set font size
170  fontEngine.height(fontSize);
171 
172  // generate a bunch of horizontal blue lines
173  {
174  for (int y = 5; y < 100; y += 10)
175  {
176  double line[] =
177  {
178  (double)0, (double)y,
179  (double)imageWidth, (double)y
180  };
181  SimplePath path (line, sizeof(line) / sizeof(double));
182  agg::conv_stroke<SimplePath> strokePath (path);
183 
184  double strokeWidth = 5.0;
185  strokePath.width(strokeWidth);
186  strokePath.line_cap(agg::square_cap);
187  strokePath.line_join(agg::miter_join);
188  strokePath.miter_limit(strokeWidth);
189 
190  ras.reset();
191  ras.add_path(strokePath);
192 
193  agg::render_scanlines_aa_solid(ras, scanline, rBase, blueColor);
194  }
195  }
196 
197  // draw stripped Hello World!
198  {
199  genTextPath (text,
200  10, 180,
201  fontKerning,
202  fontEngine,
203  fontCache,
204  blueColor,
205  path);
206  agg::trans_affine matrix;
207  matrix.translate(0, 100);
208 
209  drawImage (rBase, rBase, matrix, path, agg::fill_non_zero);
210  }
211 
212  // draw a hallow Hello World!
213  {
214  agg::path_storage path;
215 
216  // outter box
217  path.move_to(0, 200);
218  path.line_to(imageWidth, 200);
219  path.line_to(imageWidth, 300);
220  path.line_to(0, 300);
221  path.line_to(0, 200);
222 
223  genTextPath (text,
224  10, 280,
225  fontKerning,
226  fontEngine,
227  fontCache,
228  blueColor,
229  path);
230  agg::trans_affine matrix;
231  matrix.translate(0, 200);
232 
233  drawImage (rBase, rBase, matrix, path, agg::fill_even_odd);
234  }
235 
236  // now write the image out for visualization
237  char fileName[1000] = { 0 };
238  if (argc > 1)
239  {
240  sprintf (fileName, "%s/", argv[1]);
241  }
242  strcat(fileName, "tutorial_font_2.png");
243  writePng<RendererBaseType> (fileName, rBase);
244 
245  delete imageBuffer;
246  }
247  catch (TutorialException& ex)
248  {
249  printf ("%s\n", ex.getMessage());
250  return 1;
251  }
252  catch (...)
253  {
254  printf ("Unknown exception detected.\n");
255  return 1;
256  }
257 
258  return 0;
259 }