unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
blob 0dc122790738af2edb257f280b5e3611b42f31e6 10564 bytes (raw)
name: src/haiku_font_support.cc 	 # note: path name is non-authoritative(*)

  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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
 
/* Haiku window system support.  Hey, Emacs, this is -*- C++ -*-
   Copyright (C) 2021 Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs 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 3 of the License, or (at
your option) any later version.

GNU Emacs 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 GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */

#include <config.h>

#include <Font.h>
#include <Rect.h>

#include <cstring>
#include <cmath>

#include "haiku_support.h"

static void
estimate_font_ascii (BFont *font, int *max_width,
		     int *min_width, int *avg_width)
{
  char ch[2];
  bool tems[1];
  int total = 0;
  int count = 0;
  int min = 0;
  int max = 0;

  std::memset (ch, 0, sizeof ch);
  for (ch[0] = 1; ch[0] < 127; ++ch[0])
    {
      tems[0] = false;
      font->GetHasGlyphs (ch, 1, tems);
      if (tems[0])
	{
	  int w = font->StringWidth (ch);
	  ++count;
	  total += w;

	  if (!min || min > w)
	    min = w;
	  if (max < w)
	    max = w;
	}
    }

  *min_width = min;
  *max_width = max;
  *avg_width = total / count;
}

void *
BFont_open_default (double size, int plain_or_bold_or_fixed)
{
  BFont *ft;
  if (!plain_or_bold_or_fixed)
    ft = new BFont (be_fixed_font);
  else if (plain_or_bold_or_fixed > 0)
    ft = new BFont (be_plain_font);
  else
    ft = new BFont (be_bold_font);
  ft->SetSize (size);
  ft->SetEncoding (B_UNICODE_UTF8);
  return ft;
}

void
BFont_close (void *font)
{
  if (font != (void *) be_fixed_font &&
      font != (void *) be_plain_font &&
      font != (void *) be_bold_font)
    delete (BFont *) font;
}

void
BFont_dat (void *font, int *px_size, int *min_width, int *max_width,
	   int *avg_width, int *height, int *space_width, int *ascent,
	   int *descent, int *underline_position, int *underline_thickness)
{
  BFont *ft = (BFont *) font;
  struct font_height fheight;
  bool have_space_p;

  char atem[1];
  bool otem[1];

  ft->GetHeight (&fheight);
  atem[0] = ' ';
  otem[0] = false;
  ft->GetHasGlyphs (atem, 1, otem);
  have_space_p = otem[0];

  estimate_font_ascii (ft, max_width, min_width, avg_width);
  *ascent = std::lrint (fheight.ascent);
  *descent = std::lrint (fheight.descent);
  *height = *ascent + *descent;

  *space_width = have_space_p ? ft->StringWidth (" ") : 0;

  *px_size = std::lrint (ft->Size ());
  *underline_position = 0;
  *underline_thickness = 0;
}

int
BFont_have_char_p (void *font, int32_t chr)
{
  BFont *ft = (BFont *) font;
  return ft->IncludesBlock (chr, chr);
}

void
BFont_char_bounds (void *font, const char *mb_str, int *advance,
		   int *lb, int *rb)
{
  BRect rect;
  BFont *ft = (BFont *) font;
  edge_info edge_info;
  float size, escapement;
  size = ft->Size ();

  ft->GetEdges (mb_str, 1, &edge_info);
  *lb = (short) std::lrint (edge_info.left * size);
  *rb = (short) std::lrint (edge_info.right * size);
  ft->GetEscapements (mb_str, 1, &escapement);
  *advance = std::lrint (escapement * size);
}

void *
BFont_new (void)
{
  return new BFont ();
}

int
BFont_family_has_style_p (haiku_font_family_or_style family,
			  haiku_font_family_or_style style)
{
  int sc = count_font_styles (family);
  for (int idx = 0; idx < sc; ++idx)
    {
      font_style s;
      if (get_font_style (family, idx, &s) == B_OK)
	{
	  if (!strcmp (s, style))
	    return 1;
	}
    }
  return 0;
}

int
BFont_family (int idx, char *f, int *fixed_p)
{
  uint32_t flags;
  if (get_font_family (idx, (char (*)[64]) f, &flags) != B_OK)
    return 1;
  *fixed_p = flags & B_IS_FIXED;
  return 0;
}

int
BFont_set_family_and_style (void *font, haiku_font_family_or_style family,
			    haiku_font_family_or_style style)
{
  BFont *ft = (BFont *) font;
  return ft->SetFamilyAndStyle (family, style) != B_OK;
}

int
BFont_set_family_face (void *font, haiku_font_family_or_style family,
		       int italic_p, int bold_p)
{
  BFont *ft = (BFont *) font;
  return ft->SetFamilyAndFace (family, (italic_p ? B_ITALIC_FACE : 0) |
			       (bold_p == HAIKU_BOLD ? B_BOLD_FACE : 0));

}

static void
font_style_to_flags (char *st, struct haiku_font_pattern *pattern)
{
  char *style = strdup (st);
  char *token;
  pattern->weight = -1;
  int tok = 0;

  while ((token = std::strtok (!tok ? style : NULL, " ")) && tok < 2)
    {
      if (token && !strcmp (token, "Thin"))
	pattern->weight = HAIKU_THIN;
      else if (token && !strcmp (token, "UltraLight"))
	pattern->weight = HAIKU_ULTRALIGHT;
      else if (token && !strcmp (token, "ExtraLight"))
	pattern->weight = HAIKU_EXTRALIGHT;
      else if (token && !strcmp (token, "Light"))
	pattern->weight = HAIKU_LIGHT;
      else if (token && !strcmp (token, "SemiLight"))
	pattern->weight = HAIKU_SEMI_LIGHT;
      else if (token && !strcmp (token, "Regular"))
	pattern->weight = HAIKU_REGULAR;
      else if (token && !strcmp (token, "SemiBold"))
	pattern->weight = HAIKU_SEMI_BOLD;
      else if (token && !strcmp (token, "Bold"))
	pattern->weight = HAIKU_BOLD;
      else if (token && !strcmp (token, "ExtraBold"))
	pattern->weight = HAIKU_EXTRA_BOLD;
      else if (token && !strcmp (token, "UltraBold"))
	pattern->weight = HAIKU_ULTRA_BOLD;
      else if (token && !strcmp (token, "Oblique"))
	{
	  pattern->specified |= FSPEC_SLANT;
	  pattern->slant = SLANT_OBLIQUE;
	}
      else if (token && !strcmp (token, "Regular"))
	{
	  pattern->specified |= FSPEC_SLANT;
	  pattern->slant = SLANT_REGULAR;
	}
      else if (token && !strcmp (token, "Italic"))
	{
	  pattern->specified |= FSPEC_SLANT;
	  pattern->slant = SLANT_ITALIC;
	}
      else
	{
	  tok = 1000;
	  break;
	}
      tok++;
    }


  if (pattern->weight != -1)
    pattern->specified |= FSPEC_WEIGHT;

  if (tok > 2)
    {
      pattern->specified &= ~FSPEC_SLANT;
      pattern->specified &= ~FSPEC_WEIGHT;
      pattern->specified |= FSPEC_STYLE;
      std::strncpy ((char *) &pattern->style, style,
		    sizeof pattern->style - 1);
    }

  free (style);
}

static bool
font_family_style_matches_p (font_family family, char *style, uint32_t flags,
			     struct haiku_font_pattern *pattern)
{
  struct haiku_font_pattern m;

  if (style)
    font_style_to_flags (style, &m);

  enum haiku_font_slant slant;
  int weight;


  if (!(m.specified & FSPEC_SLANT))
    slant = SLANT_REGULAR;
  else
    slant = m.slant;

  if (!(m.specified & FSPEC_WEIGHT))
    weight = HAIKU_REGULAR;
  else
    weight = m.weight;

  if (pattern->specified & FSPEC_FAMILY &&
      strcmp ((char *) &pattern->family, family))
    return false;

  if (pattern->specified & FSPEC_SPACING &&
      !(pattern->mono_spacing_p) != !(flags & B_IS_FIXED))
    return false;

  if (pattern->specified & FSPEC_STYLE)
    {
      return style && !strcmp (style, pattern->style);
    }

  if (pattern->specified & FSPEC_WEIGHT && pattern->weight != weight)
    return false;

  if (pattern->specified & FSPEC_SLANT && pattern->slant != slant)
    return false;

  return true;
}

static void
haiku_font_fill_pattern (struct haiku_font_pattern *pattern,
			 font_family family, char *style,
			 uint32_t flags)
{
  if (style)
    font_style_to_flags (style, pattern);

  pattern->specified |= FSPEC_FAMILY;
  std::strncpy (pattern->family, family,
		sizeof pattern->family - 1);
  pattern->specified |= FSPEC_SPACING;
  pattern->mono_spacing_p = flags & B_IS_FIXED;
}

void
haiku_font_pattern_free (struct haiku_font_pattern *pt)
{
  struct haiku_font_pattern *tem = pt;
  while (tem)
    {
      struct haiku_font_pattern *t = tem;
      tem = t->next;
      delete t;
    }
}

struct haiku_font_pattern *
BFont_find (struct haiku_font_pattern *pt)
{
  struct haiku_font_pattern *r = NULL;
  font_family name;
  font_style sname;
  uint32_t flags;
  int sty_count;
  int fam_count = count_font_families ();

  for (int fi = 0; fi < fam_count; ++fi)
    {
      if (get_font_family (fi, &name, &flags) == B_OK)
	{
	  sty_count = count_font_styles (name);
	  if (!sty_count &&
	      font_family_style_matches_p (name, NULL, flags, pt))
	    {
	      struct haiku_font_pattern *p = new struct haiku_font_pattern;
	      p->specified = 0;
	      haiku_font_fill_pattern (p, name, NULL, flags);
	      p->next = r;
	      r = p;
	    }
	  else if (sty_count)
	    {
	      for (int si = 0; si < sty_count; ++si)
		{
		  if (get_font_style (name, si, &sname, &flags) == B_OK &&
		      font_family_style_matches_p (name, (char *) &sname, flags, pt))
		    {
		      struct haiku_font_pattern *p = new struct haiku_font_pattern;
		      p->specified = 0;
		      haiku_font_fill_pattern (p, name, (char *) &sname, flags);
		      p->next = r;
		      r = p;
		    }
		}
	    }
	}
    }

  return r;
}

int
BFont_open_pattern (struct haiku_font_pattern *pat, void **font, float size)
{
  int sty_count;
  font_family name;
  font_style sname;
  uint32_t flags = 0;
  if (!(pat->specified & FSPEC_FAMILY))
    return 1;
  strncpy (name, pat->family, sizeof name - 1);
  sty_count = count_font_styles (name);

  if (!sty_count &&
      font_family_style_matches_p (name, NULL, flags, pat))
    {
      BFont *ft = new BFont;
      if (ft->SetFamilyAndStyle (name, NULL) != B_OK)
	{
	  delete ft;
	  return 1;
	}
      ft->SetSize (size);
      ft->SetEncoding (B_UNICODE_UTF8);
      *font = (void *) ft;
      return 0;
    }
  else if (sty_count)
    {
      for (int si = 0; si < sty_count; ++si)
	{
	  if (get_font_style (name, si, &sname, &flags) == B_OK &&
	      font_family_style_matches_p (name, (char *) &sname, flags, pat))
	    {
	      BFont *ft = new BFont;
	      if (ft->SetFamilyAndStyle (name, sname) != B_OK)
		{
		  delete ft;
		  return 1;
		}
	      ft->SetSize (size);
	      ft->SetEncoding (B_UNICODE_UTF8);
	      *font = (void *) ft;
	      return 0;
	    }
	}
    }

  return 1;
}

void
BFont_populate_fixed_family (struct haiku_font_pattern *ptn)
{
  font_family f;
  font_style s;
  be_fixed_font->GetFamilyAndStyle (&f, &s);

  ptn->specified |= FSPEC_FAMILY;
  strncpy (ptn->family, f, sizeof ptn->family);
}

void
BFont_populate_plain_family (struct haiku_font_pattern *ptn)
{
  font_family f;
  font_style s;
  be_plain_font->GetFamilyAndStyle (&f, &s);

  ptn->specified |= FSPEC_FAMILY;
  strncpy (ptn->family, f, sizeof ptn->family);
}

debug log:

solving 0dc1227907 ...
found 0dc1227907 in https://yhetil.org/emacs-devel/87h7fa147s.fsf@yahoo.com/ ||
	https://yhetil.org/emacs-devel/874kbc7ift.fsf@yahoo.com/ ||
	https://yhetil.org/emacs-devel/87a6l47j32.fsf@yahoo.com/ ||
	https://yhetil.org/emacs-devel/874kbc5ycx.fsf@yahoo.com/ ||
	https://yhetil.org/emacs-devel/87czq04a3v.fsf@yahoo.com/ ||
	https://yhetil.org/emacs-devel/87y28o63ee.fsf@yahoo.com/

applying [1/1] https://yhetil.org/emacs-devel/87h7fa147s.fsf@yahoo.com/
diff --git a/src/haiku_font_support.cc b/src/haiku_font_support.cc
new file mode 100644
index 0000000000..0dc1227907

Checking patch src/haiku_font_support.cc...
Applied patch src/haiku_font_support.cc cleanly.

skipping https://yhetil.org/emacs-devel/874kbc7ift.fsf@yahoo.com/ for 0dc1227907
skipping https://yhetil.org/emacs-devel/87a6l47j32.fsf@yahoo.com/ for 0dc1227907
skipping https://yhetil.org/emacs-devel/874kbc5ycx.fsf@yahoo.com/ for 0dc1227907
skipping https://yhetil.org/emacs-devel/87czq04a3v.fsf@yahoo.com/ for 0dc1227907
skipping https://yhetil.org/emacs-devel/87y28o63ee.fsf@yahoo.com/ for 0dc1227907
index at:
100644 0dc122790738af2edb257f280b5e3611b42f31e6	src/haiku_font_support.cc

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).