aboutsummaryrefslogtreecommitdiff
path: root/src/util.c
blob: bdc36f2c32363d07bb9ed58b1c84c1688bb33b55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* Copyright (c) 2015 imv authors

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "util.h"
#include <unistd.h>
#include <stddef.h>
#include <errno.h>
#include <fontconfig/fontconfig.h>

size_t read_from_stdin(void **buffer) {
  size_t len = 0;
  ssize_t r;
  size_t step = 1024; /* Arbitrary value of 1 KiB */
  void *p;

  errno = 0; /* clear errno */

  for(*buffer = NULL; (*buffer = realloc((p = *buffer), len + step));
      len += (size_t)r) {
    if((r = read(STDIN_FILENO, (uint8_t *)*buffer + len, step)) <= 0) {
      break;
    }
  }

  /* realloc(3) leaves old buffer allocated in case of error */
  if(*buffer == NULL && p != NULL) {
    int save = errno;
    free(p);
    errno = save;
    len = 0;
  }
  return len;
}

TTF_Font *load_font(const char *font_spec)
{
  int font_size;
  char *font_name;

  /* figure out font size from name, or default to 24 */
  char *sep = strchr(font_spec, ':');
  if(sep) {
    font_name = strndup(font_spec, sep - font_spec);
    font_size = strtol(sep+1, NULL, 10);
  } else {
    font_name = strdup(font_spec);
    font_size = 24;
  }


  FcConfig *cfg = FcInitLoadConfigAndFonts();
  FcPattern *pattern = FcNameParse((const FcChar8*)font_name);
  FcConfigSubstitute(cfg, pattern, FcMatchPattern);
  FcDefaultSubstitute(pattern);

  TTF_Font *ret = NULL;

  FcResult result = FcResultNoMatch;
  FcPattern* font = FcFontMatch(cfg, pattern, &result);
  if (font) {
    FcChar8 *path = NULL;
    if (FcPatternGetString(font, FC_FILE, 0, &path) == FcResultMatch) {
      ret = TTF_OpenFont((char*)path, font_size);
    }
    FcPatternDestroy(font);
  }
  FcPatternDestroy(pattern);

  free(font_name);
  return ret;
}

SDL_Texture *create_chequered(SDL_Renderer *renderer)
{
  SDL_RendererInfo ri;
  SDL_GetRendererInfo(renderer, &ri);
  int width = 512;
  int height = 512;
  if(ri.max_texture_width != 0 && ri.max_texture_width < width) {
    width = ri.max_texture_width;
  }
  if(ri.max_texture_height != 0 && ri.max_texture_height < height) {
    height = ri.max_texture_height;
  }
  const int box_size = 16;
  /* Create a chequered texture */
  const unsigned char l = 196;
  const unsigned char d = 96;

  size_t pixels_len = 3 * width * height;
  unsigned char *pixels = malloc(pixels_len);
  for(int y = 0; y < height; y++) {
    for(int x = 0; x < width; x += box_size) {
      unsigned char color = l;
      if(((x/box_size) % 2 == 0) ^ ((y/box_size) % 2 == 0)) {
        color = d;
      }
      memset(pixels + 3 * x + 3 * width * y, color, 3 * box_size);
    }
  }
  SDL_Texture *ret = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB24,
    SDL_TEXTUREACCESS_STATIC,
    width, height);
  SDL_UpdateTexture(ret, NULL, pixels, 3 * width);
  free(pixels);
  return ret;
}

static int parse_hex_digit(char c) {
  if(c >= '0' && c <= '9') {
    return c - '0';
  } else if (c >= 'a' && c <= 'f') {
    return c - 'a' + 10;
  } else if (c >= 'A' && c <= 'F') {
    return c - 'A' + 10;
  }
  return -1;
}

int parse_hex_color(const char* str,
  unsigned char *r, unsigned char *g, unsigned char *b)
{
  if(str[0] == '#') {
    ++str;
  }

  if(strlen(str) != 6) {
    return 1;
  }

  for(int i = 0; i < 6; ++i) {
    if(!isxdigit(str[i])) {
      return 1;
    }
  }

  *r = (parse_hex_digit(str[0]) << 4) + parse_hex_digit(str[1]);
  *g = (parse_hex_digit(str[2]) << 4) + parse_hex_digit(str[3]);
  *b = (parse_hex_digit(str[4]) << 4) + parse_hex_digit(str[5]);
  return 0;
}

void imv_printf(SDL_Renderer *renderer, TTF_Font *font, int x, int y,
                SDL_Color *fg, SDL_Color *bg, const char *fmt, ...)
{
  char line[512];
  va_list args;
  va_start(args, fmt);
  vsnprintf(line, sizeof(line), fmt, args);

  SDL_Surface *surf = TTF_RenderUTF8_Blended(font, &line[0], *fg);
  SDL_Texture *tex = SDL_CreateTextureFromSurface(renderer, surf);

  SDL_Rect tex_rect = {0,0,0,0};
  SDL_QueryTexture(tex, NULL, NULL, &tex_rect.w, &tex_rect.h);
  tex_rect.x = x;
  tex_rect.y = y;

  /* draw bg if wanted */
  if(bg->a > 0) {
    SDL_SetRenderDrawColor(renderer, bg->r, bg->g, bg->b, bg->a);
    SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
    SDL_RenderFillRect(renderer, &tex_rect);
  }
  SDL_RenderCopy(renderer, tex, NULL, &tex_rect);

  SDL_DestroyTexture(tex);
  SDL_FreeSurface(surf);
  va_end(args);
}


/* vim:set ts=2 sts=2 sw=2 et: */