bool color_from_string(const std::string& colstr, Color* color) { ASSERT_LOG(!colstr.empty(), "Empty string passed to Color constructor."); auto it = get_color_table().find(colstr); if(it == get_color_table().end()) { if(!color_from_hsv_string(colstr, color)) { if(!color_from_hex_string(colstr, color)) { if(!color_from_basic_string(colstr, color)) { ASSERT_LOG(false, "Couldn't parse color '" << colstr << "' from string value."); } } } } else { *color = it->second; } return true; }
void mdvi_shrink_glyph_grey(DviContext *dvi, DviFont *font, DviFontChar *pk, DviGlyph *dest) { int rows_left, rows; int cols_left, cols, init_cols; long sampleval, samplemax; BmUnit *old_ptr; void *image; int w, h; int x, y; DviGlyph *glyph; BITMAP *map; Ulong *pixels; int npixels; Ulong colortab[2]; int hs, vs; DviDevice *dev; hs = dvi->params.hshrink; vs = dvi->params.vshrink; dev = &dvi->device; glyph = &pk->glyph; map = (BITMAP *)glyph->data; x = (int)glyph->x / hs; init_cols = (int)glyph->x - x * hs; if(init_cols <= 0) init_cols += hs; else x++; w = x + ROUND((int)glyph->w - glyph->x, hs); cols = (int)glyph->y + 1; y = cols / vs; rows = cols - y * vs; if(rows <= 0) { rows += vs; y--; } h = y + ROUND((int)glyph->h - cols, vs) + 1; ASSERT(w && h); /* before touching anything, do this */ image = dev->create_image(dev->device_data, w, h, BITMAP_BITS); if(image == NULL) { mdvi_shrink_glyph(dvi, font, pk, dest); return; } /* save these colors */ pk->fg = MDVI_CURRFG(dvi); pk->bg = MDVI_CURRBG(dvi); samplemax = vs * hs; npixels = samplemax + 1; pixels = get_color_table(&dvi->device, npixels, pk->fg, pk->bg, dvi->params.gamma, dvi->params.density); if(pixels == NULL) { npixels = 2; colortab[0] = pk->fg; colortab[1] = pk->bg; pixels = &colortab[0]; } /* setup the new glyph */ dest->data = image; dest->x = x; dest->y = glyph->y / vs; dest->w = w; dest->h = h; y = 0; old_ptr = map->data; rows_left = glyph->h; while(rows_left && y < h) { x = 0; if(rows > rows_left) rows = rows_left; cols_left = glyph->w; cols = init_cols; while(cols_left && x < w) { if(cols > cols_left) cols = cols_left; sampleval = do_sample(old_ptr, map->stride, glyph->w - cols_left, cols, rows); /* scale the sample value by the number of grey levels */ if(npixels - 1 != samplemax) sampleval = ((npixels-1) * sampleval) / samplemax; ASSERT(sampleval < npixels); dev->put_pixel(image, x, y, pixels[sampleval]); cols_left -= cols; cols = hs; x++; } for(; x < w; x++) dev->put_pixel(image, x, y, pixels[0]); old_ptr = bm_offset(old_ptr, rows * map->stride); rows_left -= rows; rows = vs; y++; } for(; y < h; y++) { for(x = 0; x < w; x++) dev->put_pixel(image, x, y, pixels[0]); } dev->image_done(image); DEBUG((DBG_BITMAPS, "shrink_glyph_grey: (%dw,%dh,%dx,%dy) -> (%dw,%dh,%dx,%dy)\n", glyph->w, glyph->h, glyph->x, glyph->y, dest->w, dest->h, dest->x, dest->y)); }