~graphite-dev/graphite/0.9

« back to all changes in this revision

Viewing changes to webapp/graphite/render/glyph.py

  • Committer: Jason Dixon
  • Date: 2016-06-09 17:34:28 UTC
  • mfrom: (1094.2.3)
  • Revision ID: git-v1:0349faf603cd4d14e6bdae1355e076b92eef12d4
Merge pull request #1475 from squarebracket/feature/render-pdf

Allow for rendering PDF files

Show diffs side-by-side

added added

removed removed

Lines of Context:
167
167
    if self.margin < 0:
168
168
      self.margin = 10
169
169
 
 
170
    self.setupCairo( params.get('outputFormat','png').lower() )
 
171
 
170
172
    self.area = {
171
173
      'xmin' : self.margin + 10, # Need extra room when the time is near the left edge
172
174
      'xmax' : self.width - self.margin,
176
178
 
177
179
    self.loadTemplate( params.get('template','default') )
178
180
 
179
 
    self.setupCairo( params.get('outputFormat','png').lower() )
180
 
 
181
181
    opts = self.ctx.get_font_options()
182
182
    opts.set_antialias( cairo.ANTIALIAS_NONE )
183
183
    self.ctx.set_font_options( opts )
199
199
    self.outputFormat = outputFormat
200
200
    if outputFormat == 'png':
201
201
      self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, self.width, self.height)
202
 
    else:
 
202
    elif outputFormat == 'svg':
203
203
      self.surfaceData = StringIO.StringIO()
204
204
      self.surface = cairo.SVGSurface(self.surfaceData, self.width, self.height)
 
205
    elif outputFormat == 'pdf':
 
206
      self.surfaceData = StringIO.StringIO()
 
207
      self.surface = cairo.PDFSurface(self.surfaceData, self.width, self.height)
 
208
      res_x, res_y = self.surface.get_fallback_resolution()
 
209
      self.width = float(self.width / res_x) * 72
 
210
      self.height = float(self.height / res_y) * 72
 
211
      self.surface.set_size(self.width, self.height)
205
212
    self.ctx = cairo.Context(self.surface)
206
213
 
207
214
  def setColor(self, value, alpha=1.0, forceAlpha=False):
443
450
  def output(self, fileObj):
444
451
    if self.outputFormat == 'png':
445
452
      self.surface.write_to_png(fileObj)
 
453
    elif self.outputFormat == 'pdf':
 
454
      self.surface.finish()
 
455
      pdfData = self.surfaceData.getvalue()
 
456
      self.surfaceData.close()
 
457
      fileObj.write(pdfData)
446
458
    else:
447
459
      metaData = {
448
460
        'x': {