/getbaseline.c Secret
Created
October 6, 2017 15:18
Star
You must be signed in to star a gist
Calculate baseline for outline font.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <diskfont/diskfont.h> | |
#include <diskfont/diskfonttag.h> | |
#include <diskfont/glyph.h> | |
#include <proto/diskfont.h> | |
#include <stdio.h> | |
int main(int argc, char **argv) { | |
struct OutlineFont *olf; | |
const uint32 devdpi = (72 << 16) | 72; | |
const uint32 ysize = 16; | |
uint32 charset; | |
uint32 maptable[256]; | |
uint32 code; | |
int32 error; | |
int32 yorigin; | |
int32 ascent, descent; | |
int32 baseline; | |
struct GlyphMap *gm; | |
if (argc != 2) { | |
printf("Usage: getbaseline <font>\n"); | |
return RETURN_ERROR; | |
} | |
/* Use system default charset */ | |
charset = IDiskfont->GetDiskFontCtrl(DFCTRL_CHARSET); | |
/* Get mapping table */ | |
if (charset == 4) { | |
/* ISO 8859-1 Latin1 */ | |
for (code = 0; code < 256; code++) { | |
maptable[code] = code; | |
} | |
} else { | |
const uint32 *src; | |
src = (const uint32 *)IDiskfont->ObtainCharsetInfo(DFCS_NUMBER, charset, DFCS_MAPTABLE); | |
if (src == NULL) { | |
fprintf(stderr, "Mapping table not found for charset: %lu!\n", charset); | |
return RETURN_ERROR; | |
} | |
for (code = 0; code < 256; code++) { | |
maptable[code] = src[code]; | |
} | |
} | |
/* Open outline font */ | |
olf = IDiskfont->OpenOutlineFont(argv[1], NULL, OFF_OPEN); | |
if (olf == NULL) { | |
printf("Failed to open font '%s'!\n", argv[1]); | |
return RETURN_ERROR; | |
} | |
/* Set DPI and point height */ | |
error = IDiskfont->ESetInfo(&olf->olf_EEngine, | |
OT_DeviceDPI, devdpi, | |
OT_PointHeight, ysize << 16, | |
TAG_END); | |
if (error) { | |
printf("Failed to set DPI and point height!\n"); | |
IDiskfont->CloseOutlineFont(olf, NULL); | |
return RETURN_ERROR; | |
} | |
ascent = descent = 0; | |
/* Set ascent and descent according to glyph dimensions */ | |
for (code = 0; code < 256; code++) { | |
error = IDiskfont->ESetInfo(&olf->olf_EEngine, | |
OT_GlyphCode, maptable[code], | |
TAG_END); | |
if (error) | |
continue; | |
error = IDiskfont->EObtainInfo(&olf->olf_EEngine, | |
OT_GlyphMap8Bit, &gm, | |
TAG_END); | |
if (error) | |
continue; | |
if (gm->glm_BlackWidth) { | |
yorigin = gm->glm_Y0; | |
if (ascent < (yorigin - gm->glm_BlackTop)) | |
ascent = yorigin - gm->glm_BlackTop; | |
if (descent < (gm->glm_BlackTop + gm->glm_BlackHeight - yorigin)) | |
descent = gm->glm_BlackTop + gm->glm_BlackHeight - yorigin; | |
} | |
IDiskfont->EReleaseInfo(&olf->olf_EEngine, | |
OT_GlyphMap, gm, | |
TAG_END); | |
} | |
/* Calculate baseline from ascent and descent */ | |
baseline = ascent - 1; | |
if ((ascent + descent) > ysize) | |
baseline -= (ascent + descent - ysize + 1) * 2 / 3; | |
printf("Baseline: %ld\n", baseline); | |
IDiskfont->CloseOutlineFont(olf, NULL); | |
return RETURN_OK; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment