void TextViewControllerImplementor::_remeasureText(){ //get split strings m_lineBreakStrings = m_text.split( L"\\n" ); FontMetrics _metrics = FontMetrics( m_binding.getFont() ); IntegerSize _boundingTextSize; for( auto _iter = m_lineBreakStrings.begin(); _iter != m_lineBreakStrings.end(); ++_iter ){ _metrics.setMetricsText( *_iter ); const auto &_each_size = _metrics.measureSize(); m_avgHeight += _each_size.getHeight(); _boundingTextSize.joinedBy( _each_size ); } //get average height m_avgHeight /= m_lineBreakStrings.size(); m_textBoundingRect.setSize( _boundingTextSize ); //get bounding size and center pos const auto &_half_size = (m_binding.getSize() / 2); if( _half_size.getWidth() - _boundingTextSize.getWidth() > 0 && _half_size.getHeight() - _boundingTextSize.getHeight() > 0){ IntegerDyadCoordinate _translated_coord( _half_size.getWidth() - (_boundingTextSize.getWidth() / 2 ) , _half_size.getHeight() - (_boundingTextSize.getHeight() / 2 ) - (_boundingTextSize.getHeight() / 4 ) ); /* normalized value */ m_textBoundingRect.translatedBy( _translated_coord ); } }
FontMetrics FontFace::Metrics::get() { DWRITE_FONT_METRICS metrics; GetInterface<IDWriteFontFace>()->GetMetrics(&metrics); return FontMetrics(metrics); }
Font::Font(Texture *fontTexture, std::string *xmlPath) { //mCharDimensions.set(charWidth, charHeight); mpFontTexture = fontTexture; mpFontMetrics = New FontMetrics(*xmlPath); mShouldDeleteMetrics = true; }
static PyObject *Fm_FontMetrics(PyObject *_self, PyObject *_args) { PyObject *_res = NULL; FMetricRec theMetrics; #ifndef FontMetrics PyMac_PRECHECK(FontMetrics); #endif if (!PyArg_ParseTuple(_args, "")) return NULL; FontMetrics(&theMetrics); _res = Py_BuildValue("O&", FMRec_New, &theMetrics); return _res; }
GlyphCache::GlyphCache() { glyph_list.reserve(256); // Note, the user can specify a different texture group size using set_texture_group() texture_group = TextureGroup(Size(256,256)); // Set default font metrics font_metrics = FontMetrics( 0,0, 0, 0,0,0,0,0, 0,0, false, false, false, false); anti_alias = true; enable_subpixel = true; }
template<class FontMetrics> void elidedMultiLength_helper() { QString text1 = QLatin1String("Long Text 1\x9cShorter\x9csmall"); QString text1_long = "Long Text 1"; QString text1_short = "Shorter"; QString text1_small = "small"; FontMetrics fm = FontMetrics(QFont()); int width_long = fm.size(0, text1_long).width(); QCOMPARE(fm.elidedText(text1,Qt::ElideRight, 8000), text1_long); QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_long + 1), text1_long); QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_long - 1), text1_short); int width_short = fm.size(0, text1_short).width(); QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_short + 1), text1_short); QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_short - 1), text1_small); // Not even wide enough for "small" - should use ellipsis QChar ellipsisChar(0x2026); QString text1_el = QString::fromLatin1("s") + ellipsisChar; int width_small = fm.width(text1_el); QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_small + 1), text1_el); }
FontMetrics Font::get_font_metrics(Canvas &canvas) { if (impl) return impl->get_font_metrics(canvas); return FontMetrics(); }
FontMetrics get_metrics() {return FontMetrics(); }
void Font_Impl::select_font_family(Canvas &canvas) { float pixel_ratio = canvas.get_gc().get_pixel_ratio(); if (pixel_ratio == 0.0f) pixel_ratio = 1.0f; if ((!font_engine) || (pixel_ratio != selected_pixel_ratio)) { // Copy the required font, setting a scalable font size FontDescription new_selected = selected_description.clone(); if (selected_description.get_height() >= selected_height_threshold) new_selected.set_height(256.0f); // A reasonable scalable size selected_pixel_ratio = pixel_ratio; Font_Cache font_cache = font_family.impl->get_font(new_selected, pixel_ratio); if (!font_cache.engine) // Font not found font_cache = font_family.impl->copy_font(new_selected, pixel_ratio); font_engine = font_cache.engine.get(); GlyphCache *glyph_cache = font_cache.glyph_cache.get(); PathCache *path_cache = font_cache.path_cache.get(); const FontMetrics &metrics = font_engine->get_metrics(); // Determine if pathfont method is required. TODO: This feels a bit hacky selected_pathfont = font_engine->is_automatic_recreation_allowed(); if (selected_description.get_height() < selected_height_threshold) selected_pathfont = false; // Deterimine if font scaling is required float font_engine_desc_height = font_engine->get_desc().get_height(); if (font_engine_desc_height == 0.0f) font_engine_desc_height = 1.0f; scaled_height = selected_description.get_height() / font_engine_desc_height; if ((scaled_height >= 0.9999f) && (scaled_height <= 1.0001f)) // Allow for floating point accuracy issues when determining when scaling is not required scaled_height = 1.0f; // Deterimine the correct drawing engine if (selected_pathfont) { font_draw_path.init(path_cache, font_engine, scaled_height); font_draw = &font_draw_path; } else if (scaled_height == 1.0f) { if (font_engine->get_desc().get_subpixel()) { font_draw_subpixel.init(glyph_cache, font_engine); font_draw = &font_draw_subpixel; } else { font_draw_flat.init(glyph_cache, font_engine); font_draw = &font_draw_flat; } } else { font_draw_scaled.init(glyph_cache, font_engine, scaled_height); font_draw = &font_draw_scaled; } selected_metrics = FontMetrics( metrics.get_height() * scaled_height, metrics.get_ascent() * scaled_height, metrics.get_descent() * scaled_height, metrics.get_internal_leading() * scaled_height, metrics.get_external_leading() * scaled_height, selected_line_height, // Do not scale the line height pixel_ratio ); } }
void CCompass::Draw(QPainter *p_pPainter, QPoint *p_pCenter) { QBrush BkBrush, Brush[2], TextBrush; QPen Pen; QRect DrawRect; QColor Color; QPoint Points[3], TickPoints[24]; QFont Font; short i, Offset = 5, FontPixelSize = 15, TextWidth; char tstr[128]; double dCompassPosition; for(i = 0; i < 24; i += 2) { TickPoints[i].setX(p_pCenter->x() + 0); TickPoints[i].setY(p_pCenter->y() - m_nRadius); TickPoints[i + 1].setX(p_pCenter->x() + 0); TickPoints[i + 1].setY(p_pCenter->y() - m_nRadius + 2 * Offset + ((!(i % 3)) ? Offset : 0)); } DrawRect.setCoords(p_pCenter->x() - m_nRadius, p_pCenter->y() - m_nRadius, p_pCenter->x() + m_nRadius, p_pCenter->y() + m_nRadius); BkBrush = QBrush(QColor(255, 0, 0, 128)); TextBrush = QBrush(QColor(255, 255, 0, 255)); Brush[0] = QBrush(QColor(255, 0, 0, 255)); Brush[1] = QBrush(QColor(255, 255, 255, 255)); Pen = QPen(Qt::black); Pen.setWidth(1); Pen.setStyle(Qt::NoPen); p_pPainter->setPen(Pen); BkBrush.setStyle(Qt::SolidPattern); p_pPainter->setBrush(BkBrush); p_pPainter->drawEllipse(DrawRect.center(), m_nRadius - Offset, m_nRadius - Offset); BkBrush.setStyle(Qt::NoBrush); p_pPainter->setBrush(BkBrush); Pen.setStyle(Qt::SolidLine); p_pPainter->setPen(Pen); p_pPainter->drawEllipse(DrawRect.center(), m_nRadius, m_nRadius); for(i = 0; i < 12; i ++) RotatePoints(&(TickPoints[i * 2]), 2, p_pCenter, 30 * i); p_pPainter->drawLines(TickPoints, 12); Pen.setStyle(Qt::NoPen); p_pPainter->setPen(Pen); p_pPainter->setBrush(Brush[0]); Points[0].setX(p_pCenter->x() + Offset); Points[0].setY(p_pCenter->y()); Points[1].setX(p_pCenter->x() - Offset); Points[1].setY(p_pCenter->y()); Points[2].setX(p_pCenter->x()); Points[2].setY(p_pCenter->y() - m_nRadius + Offset); dCompassPosition = 360.0 - m_dHeading_deg; if(dCompassPosition < 0) dCompassPosition += 360; RotatePoints(Points, 3, p_pCenter, dCompassPosition); p_pPainter->drawConvexPolygon(Points, 3); p_pPainter->setBrush(Brush[1]); Points[0].setX(p_pCenter->x() + Offset); Points[0].setY(p_pCenter->y()); Points[1].setX(p_pCenter->x() - Offset); Points[1].setY(p_pCenter->y()); Points[2].setX(p_pCenter->x()); Points[2].setY(p_pCenter->y() + m_nRadius - Offset); RotatePoints(Points, 3, p_pCenter, dCompassPosition); p_pPainter->drawConvexPolygon(Points, 3); Pen.setColor(Qt::black); Pen.setStyle(Qt::SolidLine); p_pPainter->setPen(Pen); p_pPainter->setBrush(TextBrush); Font.setPixelSize(FontPixelSize); p_pPainter->setFont(Font); sprintf_s(tstr, sizeof(tstr), "%.0lf", m_dHeading_deg); QFontMetrics FontMetrics(Font); TextWidth = FontMetrics.width(tstr) + 4; DrawRect.setCoords(p_pCenter->x() - TextWidth / 2, p_pCenter->y() - FontPixelSize / 2, p_pCenter->x() + TextWidth / 2, p_pCenter->y() + FontPixelSize / 2 + 2); p_pPainter->drawRect(DrawRect); p_pPainter->drawText(DrawRect, Qt::AlignCenter, QString::fromLocal8Bit(tstr)); }
void FontFamily_Impl::font_face_load(Canvas &canvas, Sprite &sprite, const std::string &glyph_list, float spacelen, bool monospace, const FontMetrics &metrics) { FontMetrics font_metrics = metrics; const int length = StringHelp::utf8_length(glyph_list); if ((length > sprite.get_frame_count()) || (length == 0)) { throw Exception(string_format("Font error: Letter characters: %1, Available font glyphs: %2", length, sprite.get_frame_count())); } //If monospace font requested, find the width of the widest glyph //Then set the fixed_width var to that width //Also set space to that width, if unset int fixed_width = 0; if (monospace) { for (int i = 0; i < length; ++i) { int glyph_width = sprite.get_frame_size(i).width; if (glyph_width > fixed_width) fixed_width = glyph_width; } if (spacelen) spacelen = fixed_width; } //If not monospace, and space width not specified, then use average width as space width else if (spacelen <= 0) { std::string::size_type space_pos = glyph_list.find(' '); if (space_pos != std::string::npos) { //If there is a character for space, then use it spacelen = sprite.get_frame_size((int)space_pos).width; } else { //Make the space size the average of all character sizes spacelen = 0; for (int pos = 0; pos < length; ++pos) { spacelen += sprite.get_frame_size((int)(pos)).width; } spacelen /= length; } } int height = 0; for (int i = 0; i < length; ++i) { int glyph_height = sprite.get_frame_size(i).height; if (glyph_height > height) height = glyph_height; } if (font_metrics.get_height() == 0) { font_metrics = FontMetrics( height, font_metrics.get_ascent(), font_metrics.get_descent(), font_metrics.get_internal_leading(), font_metrics.get_external_leading(), font_metrics.get_line_height()); } FontDescription desc; desc.set_height(height); std::shared_ptr<FontEngine> engine = std::make_shared<FontEngine_Sprite>(desc, font_metrics); Font_Cache sprite_engine(engine); font_cache.push_back(sprite_engine); GlyphCache *glyph_cache = sprite_engine.glyph_cache.get(); // Setup char to glyph map: UTF8_Reader reader(glyph_list.data(), glyph_list.length()); int sprite_index = 0; while (!reader.is_end()) { unsigned int glyph = reader.get_char(); reader.next(); const Sprite_Impl::SpriteFrame &sprite_frame = sprite.impl->frames[sprite_index]; Point increment; if (fixed_width) { increment.x = fixed_width; } else { increment.x = sprite_frame.position.get_width(); } Pointf offset(sprite_frame.offset); offset.y -= font_metrics.get_ascent(); Sizef size = sprite_frame.position.get_size(); Subtexture sub_texture(sprite_frame.texture, sprite_frame.position); glyph_cache->insert_glyph(canvas, glyph, sub_texture, offset, size, GlyphMetrics(Pointf(offset.x, offset.y), Sizef(increment.x, increment.y), Sizef(increment.x, increment.y))); sprite_index++; } // Did the glyphs not contain a space? std::string::size_type space_pos = glyph_list.find(' '); if (space_pos == std::string::npos) { FontPixelBuffer pb; pb.empty_buffer = true; pb.metrics.advance.width = spacelen; pb.metrics.bbox_size.width = spacelen; pb.glyph = ' '; glyph_cache->insert_glyph(canvas, pb); } float average_character_width = 0.0f; float max_character_width = 0.0f; if (monospace) { average_character_width = fixed_width; max_character_width = fixed_width; } else { for (int i = 0; i < length; ++i) { int glyph_width = sprite.get_frame_size(i).width; average_character_width += glyph_width; if (glyph_width > max_character_width) max_character_width = glyph_width; } if (length) average_character_width /= length; } }
FontMetrics Font::get_font_metrics() { if (impl) return impl->get_font_metrics(); return FontMetrics(); }
void FontProvider_Sprite::setup_glyphs(const std::string &letters_utf8, int new_spacelen, bool monospace) { fixed_width = 0; height = 0; const int length = StringHelp::utf8_length(letters_utf8); if ((length > spr_glyphs.get_frame_count()) || (length == 0)) { throw Exception(string_format("Font error: Letter characters: %1, Available font glyphs: %2", length, spr_glyphs.get_frame_count())); } glyph_list.reserve(length + glyph_list.size()); //If monospace font requested, find the width of the widest glyph //Then set the fixed_width var to that width, which gwidth() checks for //Also set space to that width if (monospace) { for (int i=0; i < length; ++i) { int glyph_width = spr_glyphs.get_frame_size(i).width; if (glyph_width > fixed_width) fixed_width = glyph_width; } new_spacelen = fixed_width; } //If not monospace, and space width not specified, then use average width as space width else if (new_spacelen < 0) { std::string::size_type space_pos = letters_utf8.find(' '); if (space_pos != std::string::npos) { //If there is a character for space, then use it new_spacelen = spr_glyphs.get_frame_size((int)space_pos).width; } else { //Make the space size the average of all character sizes new_spacelen = 0; for (int pos = 0; pos < length; ++pos) { new_spacelen += spr_glyphs.get_frame_size((int)(pos)).width; } new_spacelen /= length; } } for (int i=0; i < length; ++i) { int glyph_height = spr_glyphs.get_frame_size(i).height; if (glyph_height > height) height = glyph_height; } spr_glyphs.set_alignment(origin_top_left, 0, 0); spr_glyphs.set_rotation_hotspot(origin_top_left, 0, 0); spacelen = new_spacelen; //If monospace font requested, find the width of the widest glyph //Then set the fixed_width var to that width, which gwidth() checks for if (monospace) { for (int i=0; i < length; ++i) { int glyph_width = spr_glyphs.get_frame_size(i).width; if (glyph_width > fixed_width) fixed_width = glyph_width; } } // Setup char to glyph map: UTF8_Reader reader(letters_utf8.data(), letters_utf8.length()); int sprite_index = 0; while(!reader.is_end()) { unsigned int glyph = reader.get_char(); reader.next(); Font_Sprite_Glyph font_glyph; font_glyph.glyph = glyph; font_glyph.sprite_index = sprite_index; sprite_index++; glyph_list.push_back(font_glyph); } float average_character_width=0.0f; float max_character_width=0.0f; if (monospace) { average_character_width = fixed_width; max_character_width = fixed_width; } else { for (int i=0; i < length; ++i) { int glyph_width = spr_glyphs.get_frame_size(i).width; average_character_width += glyph_width; if (glyph_width > max_character_width) max_character_width = glyph_width; } if (length) average_character_width /= length; } font_metrics = FontMetrics( height, // height height, // ascent 0, // descent 0, // internal_leading 0, // external_leading average_character_width, // average_character_width max_character_width, // max_character_width 400.0f, // weight 0.0f, // overhang 96.0f, // digitized_aspect_x 96.0f, // digitized_aspect_y false, // italic false, // underline false, // struck_out false); // fixed_pitch }
static void Quartz_MetricInfo(int c, R_GE_gcontext *gc, double* ascent, double* descent, double* width, NewDevDesc *dd) { FMetricRec myFMetric; QuartzDesc *xd = (QuartzDesc *) dd-> deviceSpecific; char testo[2]; char *ff; CGrafPtr savedPort; Rect bounds; CGPoint position; unsigned char tmp; testo[0] = c; testo[1] = '\0'; /* fprintf(stderr,"c=%c,>%s<\n",c,testo); */ GetPort(&savedPort); SetPort(GetWindowPort(xd->window)); Quartz_SetFont(gc->fontfamily, gc->fontface, gc->cex, gc->ps, dd); if(c==0){ FontMetrics(&myFMetric); *ascent = xd->yscale *floor(gc->cex * gc->ps + 0.5) * FixedToFloat(myFMetric.ascent); *descent = xd->yscale*floor(gc->cex * gc->ps + 0.5) * FixedToFloat(myFMetric.descent); } else { CGContextSaveGState( GetContext(xd) ); CGContextTranslateCTM( GetContext(xd), 0, 0 ); CGContextScaleCTM( GetContext(xd), -1, 1); CGContextRotateCTM( GetContext(xd), -1.0 * 3.1416); CGContextSetTextDrawingMode( GetContext(xd), kCGTextInvisible ); Quartz_SetFont(gc->fontfamily, gc->fontface, gc->cex, gc->ps, dd); ff = Quartz_TranslateFontFamily(gc->fontfamily, gc->fontface, xd->family); tmp = (unsigned char)c; if( (gc->fontface == 5) || (strcmp(ff,"Symbol")==0)){ if( (tmp>31) && IsThisASymbol(tmp)) testo[0] = (char)Lat2Uni[tmp-31-1]; else Quartz_SetFont(gc->fontfamily, -1, gc->cex, gc->ps, dd); } else { if(tmp>127) testo[0] = (char)Lat2Mac[tmp-127-1]; } CGContextShowTextAtPoint( GetContext(xd), 0, 0, testo, 1 ); position = CGContextGetTextPosition( GetContext(xd) ); CGContextRestoreGState( GetContext(xd) ); QDTextBounds(1,testo,&bounds); *ascent = -bounds.top; *descent = bounds.bottom; *width = bounds.right - bounds.left; *width = position.x; } SetPort(savedPort); /* fprintf(stderr,"ascent=%f, descent=%f,width=%f\n",*ascent, *descent, *width); */ return; }
static gx_xfont * mac_lookup_font(gx_device *dev, const byte *fname, uint len, int encoding_index, const gs_uid *puid, const gs_matrix *pmat, gs_memory_t *mem) { #pragma unused(encoding_index,puid) mac_xfont *macxf; CGrafPort *currentPort; int txFont, txSize, txMode; StyleField txFace; Fixed spExtra; /* are XFonts enabled? */ if (((gx_device_macos*) dev)->useXFonts == false) return NULL; /* we can handle only requests from these encodings */ if (encoding_index != ENCODING_INDEX_MACROMAN && encoding_index != ENCODING_INDEX_ISOLATIN1 && encoding_index != ENCODING_INDEX_STANDARD) return NULL; /* Don't render very small fonts */ if (fabs(pmat->xx * 1000.0) < 3.0) return NULL; /* Only handle simple cases for now (no transformations). */ if (fabs(pmat->xy) > 0.0001 || fabs(pmat->yx) > 0.0001 || pmat->xx <= 0) return NULL; /* allocate memory for gx_xfont */ macxf = gs_alloc_struct(mem, mac_xfont, &st_mac_xfont, "mac_lookup_font"); if (macxf == NULL) { return NULL; } /* set default values */ macxf->common.procs = &mac_xfont_procs; macxf->dev = dev; /* find the specified font */ mac_find_font_family(fname, len, &(macxf->fontID), &(macxf->fontFace)); /* no font found */ if (macxf->fontID == 0) return NULL; FMGetFontFamilyName(macxf->fontID, macxf->fontName); macxf->fontSize = (short)(pmat->xx * 1000.0); macxf->fontEncoding = mac_get_font_encoding(macxf); /* we can handle only fonts with these encodings for now (all original Mac fonts have MacRoman encoding!) */ if (macxf->fontEncoding != ENCODING_INDEX_MACROMAN && macxf->fontEncoding != ENCODING_INDEX_ISOLATIN1) return NULL; /* get font metrics */ /* save current GrafPort's font information */ GetPort(&((GrafPort*) currentPort)); txFont = currentPort->txFont; txSize = currentPort->txSize; txFace = currentPort->txFace; txMode = currentPort->txMode; spExtra = currentPort->spExtra; /* set values for measuring */ TextFont(macxf->fontID); TextSize(macxf->fontSize); TextFace(macxf->fontFace); TextMode(srcOr); SpaceExtra(0); /* measure font */ FontMetrics(&(macxf->fontMetrics)); /* restore current GrafPort's font information */ currentPort->txFont = txFont; currentPort->txSize = txSize; currentPort->txFace = txFace; currentPort->txMode = txMode; currentPort->spExtra = spExtra; return (gx_xfont*) macxf; }