コード例 #1
0
QSizeF QgsNumericScaleBarRenderer::calculateBoxSize( const QgsScaleBarSettings &settings,
    const QgsScaleBarRenderer::ScaleBarContext &scaleContext ) const
{
  QFont font = settings.textFormat().toQFont();

  double textWidth = QgsLayoutUtils::textWidthMM( font, scaleText( scaleContext.scale ) );
  double textHeight = QgsLayoutUtils::fontAscentMM( font );

  return QSizeF( 2 * settings.boxContentSpace() + 2 * settings.pen().width() + textWidth,
                 textHeight + 2 * settings.boxContentSpace() );
}
コード例 #2
0
void QgsNumericScaleBarStyle::draw( QPainter *p, double xOffset ) const
{
  Q_UNUSED( xOffset );
  if ( !p || !mScaleBar )
  {
    return;
  }

  p->save();
  //antialiasing on
  p->setRenderHint( QPainter::Antialiasing, true );
  p->setFont( mScaleBar->font() );

  //call QgsComposerItem's pen() function, since that refers to the frame pen
  //and QgsComposerScalebar's pen() function refers to the scale bar line width,
  //which is not used for numeric scale bars. Divide the pen width by 2 since
  //half the width of the frame is drawn outside the item.
  double penWidth = mScaleBar->QgsComposerItem::pen().widthF() / 2.0;
  double margin = mScaleBar->boxContentSpace();
  //map scalebar alignment to Qt::AlignmentFlag type
  Qt::AlignmentFlag hAlign;
  switch ( mScaleBar->alignment() )
  {
    case QgsComposerScaleBar::Left:
      hAlign = Qt::AlignLeft;
      break;
    case QgsComposerScaleBar::Middle:
      hAlign = Qt::AlignHCenter;
      break;
    case QgsComposerScaleBar::Right:
      hAlign = Qt::AlignRight;
      break;
    default:
      hAlign = Qt::AlignLeft;
      break;
  }

  //text destination is item's rect, excluding the margin and frame
  QRectF painterRect( penWidth + margin, penWidth + margin, mScaleBar->rect().width() - 2 * penWidth - 2 * margin, mScaleBar->rect().height() - 2 * penWidth - 2 * margin );
  QgsComposerUtils::drawText( p, painterRect, scaleText(), mScaleBar->font(), mScaleBar->fontColor(), hAlign, Qt::AlignTop );

  p->restore();
}
コード例 #3
0
QRectF QgsNumericScaleBarStyle::calculateBoxSize() const
{
  QRectF rect;
  if ( !mScaleBar )
  {
    return rect;
  }

  double textWidth = QgsComposerUtils::textWidthMM( mScaleBar->font(), scaleText() );
  double textHeight = QgsComposerUtils::fontAscentMM( mScaleBar->font() );

  rect = QRectF( mScaleBar->pos().x(), mScaleBar->pos().y(), 2 * mScaleBar->boxContentSpace()
                 + 2 * mScaleBar->pen().width() + textWidth,
                 textHeight + 2 * mScaleBar->boxContentSpace() );

  if ( !qgsDoubleNear( mLastScaleBarWidth, rect.width() ) && mLastScaleBarWidth > 0 && rect.width() > 0 )
  {
    //hack to move scale bar the left / right in order to keep the bar alignment
    const_cast<QgsComposerScaleBar *>( mScaleBar )->correctXPositionAlignment( mLastScaleBarWidth, rect.width() );
  }
  mLastScaleBarWidth = rect.width();
  return rect;
}
コード例 #4
0
void QgsNumericScaleBarRenderer::draw( QgsRenderContext &context, const QgsScaleBarSettings &settings, const ScaleBarContext &scaleContext ) const
{
  if ( !context.painter() )
  {
    return;
  }

  QPainter *painter = context.painter();

  painter->save();
  if ( context.flags() & QgsRenderContext::Antialiasing )
    painter->setRenderHint( QPainter::Antialiasing, true );

  double margin = context.convertToPainterUnits( settings.boxContentSpace(), QgsUnitTypes::RenderMillimeters );
  //map scalebar alignment to Qt::AlignmentFlag type
  QgsTextRenderer::HAlignment hAlign = QgsTextRenderer::AlignLeft;
  switch ( settings.alignment() )
  {
    case QgsScaleBarSettings::AlignLeft:
      hAlign = QgsTextRenderer::AlignLeft;
      break;
    case QgsScaleBarSettings::AlignMiddle:
      hAlign = QgsTextRenderer::AlignCenter;
      break;
    case QgsScaleBarSettings::AlignRight:
      hAlign = QgsTextRenderer::AlignRight;
      break;
  }

  //text destination is item's rect, excluding the margin
  QRectF painterRect( margin, margin, context.convertToPainterUnits( scaleContext.size.width(), QgsUnitTypes::RenderMillimeters ) - 2 * margin,
                      context.convertToPainterUnits( scaleContext.size.height(), QgsUnitTypes::RenderMillimeters ) - 2 * margin );
  QgsTextRenderer::drawText( painterRect, 0, hAlign, QStringList() << scaleText( scaleContext.scale ), context, settings.textFormat() );

  painter->restore();
}
コード例 #5
0
ファイル: memebot.c プロジェクト: ctbarbour/memegen
void
generate(char *source_image_path, char *sink_image_path, char *top_text, char *bottom_text)
{

  MagickWand *wand = NULL;
  DrawingWand *drawing_wand = NULL;
  PixelWand *pixel_wand = NULL;

  MagickWandGenesis();

  wand = NewMagickWand();
  drawing_wand = NewDrawingWand();
  pixel_wand = NewPixelWand();

  // read base image
  MagickReadImage(wand, source_image_path);

  ssize_t width;
  ssize_t pointsize;
  ssize_t stroke_width;
  double scale;
  char formatted_text[100] = { '\0' };

  // Scale text
  width = MagickGetImageWidth(wand);
  scale = scaleText(top_text, formatted_text);
  pointsize = width / 5.0;
  stroke_width = pointsize / 30.0;

  // Draw top text
  PixelSetColor(pixel_wand, "white");
  DrawSetFillColor(drawing_wand, pixel_wand);
  DrawSetFont(drawing_wand, "Impact");
  DrawSetFontSize(drawing_wand, pointsize * scale);
  DrawSetFontWeight(drawing_wand, 700);
  DrawSetGravity(drawing_wand, NorthGravity);

  // Add a black outline to the text
  PixelSetColor(pixel_wand, "black");
  DrawSetStrokeWidth(drawing_wand, stroke_width * scale);
  DrawSetStrokeColor(drawing_wand, pixel_wand);

  // Turn on Anitalias
  DrawSetTextAntialias(drawing_wand, MagickTrue);

  // Draw the text
  DrawAnnotation(drawing_wand, 0, 0, (const unsigned char *)formatted_text);

  if (bottom_text) {
    char formatted_bottom_text[100] = { '\0' };

    // Scale text
    width = MagickGetImageWidth(wand);
    scale = scaleText(bottom_text, formatted_bottom_text);
    pointsize = width / 5.0;
    stroke_width = pointsize / 30.0;

    // Draw bottom text
    PixelSetColor(pixel_wand, "white");
    DrawSetFillColor(drawing_wand, pixel_wand);
    DrawSetFont(drawing_wand, "Impact");
    DrawSetFontSize(drawing_wand, pointsize * scale);
    DrawSetFontWeight(drawing_wand, 700);
    DrawSetGravity(drawing_wand, SouthGravity);

    // Add a black outline to the text
    PixelSetColor(pixel_wand, "black");
    DrawSetStrokeWidth(drawing_wand, stroke_width * scale);
    DrawSetStrokeColor(drawing_wand, pixel_wand);

    // Turn on Anitalias
    DrawSetTextAntialias(drawing_wand, MagickTrue);

    // Draw the text
    DrawAnnotation(drawing_wand, 0, 0, (const unsigned char *)formatted_bottom_text);
  }

  // Draw the image on the magick wand
  MagickDrawImage(wand, drawing_wand);

  // Write the image
  if (sink_image_path) {
    MagickWriteImage(wand, sink_image_path);
  } else {
    MagickWriteImageFile(wand, stdout);
  }

  // Clean up
  if(pixel_wand) pixel_wand = DestroyPixelWand(pixel_wand);
  if(drawing_wand) drawing_wand = DestroyDrawingWand(drawing_wand);
  if(wand) wand = DestroyMagickWand(wand);

  MagickWandTerminus();
}