Freeciv21
Develop your civilization from humble roots to a global empire
canvas.cpp
Go to the documentation of this file.
1 
22 #include "canvas.h"
23 
24 // Qt
25 #include <QFontMetrics>
26 #include <QPainter>
27 #include <QPainterPath>
28 
29 // client
30 #include "colors_common.h"
31 #include "fc_client.h"
32 #include "fonts.h"
33 #include "tileset/sprite.h"
34 
38 void pixmap_copy(QPixmap *dest, const QPixmap *src, int src_x, int src_y,
39  int dest_x, int dest_y, int width, int height)
40 {
41  QRectF source_rect(src_x, src_y, width, height);
42  QRectF dest_rect(dest_x, dest_y, width, height);
43  QPainter p;
44 
45  if (!width || !height) {
46  return;
47  }
48 
49  p.begin(dest);
50  p.drawPixmap(dest_rect, *src, source_rect);
51  p.end();
52 }
53 
57 QFont get_font(client_font font)
58 {
59  QFont qf;
60 
61  switch (font) {
62  case FONT_CITY_NAME:
64  break;
65  case FONT_CITY_PROD:
67  break;
68  case FONT_REQTREE_TEXT:
70  break;
71  case FONT_COUNT:
72  break;
73  }
74 
75  return qf;
76 }
77 
81 QRect zealous_crop_rect(QImage &p)
82 {
83  int r, t, b, l;
84 
85  l = p.width();
86  r = 0;
87  t = p.height();
88  b = 0;
89  for (int y = 0; y < p.height(); ++y) {
90  QRgb *row = reinterpret_cast<QRgb *>(p.scanLine(y));
91  bool row_filled = false;
92  int x;
93 
94  for (x = 0; x < p.width(); ++x) {
95  if (qAlpha(row[x])) {
96  row_filled = true;
97  r = qMax(r, x);
98  if (l > x) {
99  l = x;
100  x = r;
101  }
102  }
103  }
104  if (row_filled) {
105  t = qMin(t, y);
106  b = y;
107  }
108  }
109  return QRect(l, t, qMax(0, r - l + 1), qMax(0, b - t + 1));
110 }
111 
115 QPixmap crop_sprite(const QPixmap *sprite)
116 {
117  QImage img = sprite->toImage();
118  QRect crop = zealous_crop_rect(img);
119  QImage cropped_img = img.copy(crop);
120  QPixmap pix = QPixmap::fromImage(cropped_img);
121 
122  return pix;
123 }
QRect zealous_crop_rect(QImage &p)
Return rectangle containing pure image (crops transparency)
Definition: canvas.cpp:81
void pixmap_copy(QPixmap *dest, const QPixmap *src, int src_x, int src_y, int dest_x, int dest_y, int width, int height)
/ \ *********** / _ \ | / \ | Copyright (c) 1996-2023 Freeciv21 and || || _______ Freeciv contributor...
Definition: canvas.cpp:38
QFont get_font(client_font font)
Returns given font.
Definition: canvas.cpp:57
QPixmap crop_sprite(const QPixmap *sprite)
Helper function to crop a sprite.
Definition: canvas.cpp:115
client_font
Definition: canvas.h:19
@ FONT_CITY_PROD
Definition: canvas.h:21
@ FONT_REQTREE_TEXT
Definition: canvas.h:22
@ FONT_COUNT
Definition: canvas.h:23
@ FONT_CITY_NAME
Definition: canvas.h:20
QFont getFont(const QString &name, double zoom=1.0) const
Returns desired font.
Definition: fonts.cpp:57
static fcFont * instance()
Returns instance of fc_font.
Definition: fonts.cpp:34
const char *const city_productions
Definition: fonts.h:24
const char *const city_names
Definition: fonts.h:23
const char *const reqtree_text
Definition: fonts.h:25
Definition: mapimg.cpp:266