CreateFont() 从字符串 [英] CreateFont() from String

查看:103
本文介绍了CreateFont() 从字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在 Processing 或 Java 中创建一个变量,其中包含字符串格式的排版(ttf 或 otf),以便能够以与图像相同的方式加载它,因为 LoadImage ()从整数数组工作.我可以用图像做到这一点.对于排版,有没有办法做到这一点?谢谢!

int[][] var = {//R G B A{255, 0, 0, 128},{ 0, 255, 0, 128}};PImage loadPixelsArray(int xDiv, int yDiv, float[][] dots) {PImage img = createImage(xDiv, yDiv, ARGB);img.loadPixels();int size = img.pixels.length;for (int i = 0; i < size; i++) {img.pixels[i] = color(dots[i][0],dots[i][1],dots[i][2],dots[i][3]);}img.updatePixels();返回图像;}

ps:返回 2 个像素的 PImage.我需要同样的 PFont.

解决方案

在处理编辑器中转到 工具 >创建字体.. 创建一个 .vlw 文件(处理的字体格式),然后在您的代码中简单地使用

请务必针对正确的字体大小和字符进行一些测试(尤其是在使用特殊字符时).在渲染文本方面,Processing 可以根据渲染器使用

有关 PFont 的更多详细信息,请使用

此外,您可以通过 PFont 的

如果您想绘制自己的字形,您可以随时使用 beginShape()createShape() 等,并将其包装在一个函数中,该函数采用您要呈现的字符串和为每个字符绘制正确的形状.

如果您想创建自己的 ttf 字体文件,您可能需要一个外部库.查看排版处理库以获得扩展功能.

It is posible to make a variable in Processing or Java that self contain the typography (ttf or otf) in format of String to be able to load it in the same way that you can do it with images, since LoadImage () works from an array of integers. I can do it with the Images. Is there any way to do it for the case of typography? Thanks!

int[][] var = {
     // R    G    B    A
     {255,   0,   0, 128},
     {  0, 255,   0, 128}
};  

PImage loadPixelsArray(int xDiv, int yDiv, float[][] dots) {

   PImage img = createImage(xDiv, yDiv, ARGB);
   img.loadPixels();
   int size = img.pixels.length;
   for (int i = 0; i < size; i++) {
          img.pixels[i] = color(dots[i][0], dots[i][1], dots[i][2], dots[i][3]);
   }
   img.updatePixels();
   return img;
}

ps: returns PImage of 2 pixels. I need the same for PFont.

解决方案

In the Processing editor go to Tools > Create Font.. to create a .vlw file (Processing's font format) then in your code simply use loadFont() instead of createFont()

Be sure to run some tests in terms of the right font size and characters (especially if using special characters). In terms of rendering text Processing can do bitmaps or shapes using textMode() depending on the renderer.

Additionally, if you need to create images/pixels of text you can use PGraphics (which extends PImage)

Update It is still unclear what is that you're asking:

  • loading a font from a string(font filename) ?
  • render text to an image ?
  • get all the gyphs in a font as pixels ? something else ?

If you want to render text to an image, as mentioned above you can use PGraphics:

noSmooth();

PGraphics textImage = createGraphics(25,25);
textImage.beginDraw();
textImage.text("O!",10,15);
textImage.endDraw();

image(textImage,0,0);
image(textImage,0,0,textImage.width * 2.1,textImage.height * 2.1);
image(textImage,0,0,textImage.width * 4.5,textImage.height * 4.5);

For more details on PFont use the PFont javadoc. Notice you can get glyphs (which have images):

String[] fontNames = {"Monospaced","ProcessingSansPro-Regular"};
int NUM_FONTS = fontNames.length;
PFont[] fonts = new PFont[NUM_FONTS];
PFont font;

int FONT_SIZE = 30;
String message = "Hello World";

ArrayList<PImage> glyphImages;

void setup(){
  size(300,300);
  textAlign(CENTER);
  fill(0);
  
  // load fonts
  for(int i = 0 ; i < NUM_FONTS; i++){
    fonts[i] = createFont(fontNames[i],FONT_SIZE);
  }
  
  // set first font
  setFont(0);
  
}

void draw(){
  background(255);
  text(message,width * 0.5,height * 0.5);
  
  drawGlyphImages();
  
}

void keyPressed(){
  if(key == '1'){
    setFont(0);
  }
  if(key == '2'){
    setFont(1);
  }
}

void setFont(int fontIndex){
  // check font data is accessible
  if(fonts != null){
    
    if(fontIndex >= 0 && fontIndex < fonts.length){
      
      // update global reference to currently set font
      font = fonts[fontIndex];
      // tell Processing to use this font
      textFont(font,FONT_SIZE);
      // create glyph images
      glyphImages = getGlyphImages(font,message);
      
    }else{
      println("error: invalid array index",fontIndex,"valid range is",0,fonts.length-1);
    }
    
  }else{
    println("error: null fonts array");
  } 
}

ArrayList<PImage> getGlyphImages(PFont font,String message){

  ArrayList<PImage> result = new ArrayList<PImage>();
  
  // access font glyph images
  for(int i = 0 ; i < message.length(); i++){
    
    // get the each character
    char currentChar = message.charAt(i);
    // get the glyph
    PFont.Glyph glyph = font.getGlyph(currentChar);
    // if there is a glyph (space or other special character might not be encoded in the font file)
    if(glyph != null){
      // get a copy of the glyph image
      PImage glyphImage = glyph.image.get();
      // glyph PImages are in ALPHA format: for demo purposes we use a GRAY filter to set the format to RGB  
      glyphImage.filter(GRAY);
      // append the glyph image to the list
      result.add(glyphImage);
    }
    
  }
  
  return result;
}

ArrayList<PShape> getGlyphShapes(PFont font,String message){

  ArrayList<PShape> result = new ArrayList<PShape>();
  
  // access font glyph images
  for(int i = 0 ; i < message.length(); i++){
    
    // get the each character
    char currentChar = message.charAt(i);
    // get the glyph shape
    PShape glyphShape = font.getShape(currentChar);
    // if there is a glyph (space or other special character might not be encoded in the font file)
    if(glyphShape != null){
      // add the shape to the list
      result.add(glyphShape);
    }
    
  }
  
  return result;
}

void drawGlyphImages(){
  float previousGlyphsHeight = 0;
  for(int i = 0; i < glyphImages.size(); i++){
    PImage glyphImage = glyphImages.get(i);
    image(glyphImage,10,glyphImage.height + previousGlyphsHeight,glyphImage.width * 2,glyphImage.height * 2);
    previousGlyphsHeight += (glyphImage.height * 2) + 3;
  }
}

Additionally you can get the shapes via PFont's getShape():

String[] fontNames = {"Monospaced","ProcessingSansPro-Regular"};
int NUM_FONTS = fontNames.length;
PFont[] fonts = new PFont[NUM_FONTS];
PFont font;

int FONT_SIZE = 30;
String message = "Hello World";

ArrayList<PShape> glyphShapes;

void setup(){
  size(345,345);
  textAlign(CENTER);
  fill(0);
  
  // load fonts
  for(int i = 0 ; i < NUM_FONTS; i++){
    fonts[i] = createFont(fontNames[i],FONT_SIZE);
  }
  
  // set first font
  setFont(0);
  
}

void draw(){
  background(255);
  text(message,width * 0.5,height * 0.5);
  
  drawGlyphShapes();
  
}

void keyPressed(){
  if(key == '1'){
    setFont(0);
  }
  if(key == '2'){
    setFont(1);
  }
}

void setFont(int fontIndex){
  // check font data is accessible
  if(fonts != null){
    
    if(fontIndex >= 0 && fontIndex < fonts.length){
      
      // update global reference to currently set font
      font = fonts[fontIndex];
      // tell Processing to use this font
      textFont(font,FONT_SIZE);
      // update glyph shapes
      glyphShapes = getGlyphShapes(font,message);
    }else{
      println("error: invalid array index",fontIndex,"valid range is",0,fonts.length-1);
    }
    
  }else{
    println("error: null fonts array");
  } 
}

ArrayList<PShape> getGlyphShapes(PFont font,String message){

  ArrayList<PShape> result = new ArrayList<PShape>();
  
  // access font glyph images
  for(int i = 0 ; i < message.length(); i++){
    
    // get the each character
    char currentChar = message.charAt(i);
    // get the glyph shape
    PShape glyphShape = font.getShape(currentChar);
    // if there is a glyph (space or other special character might not be encoded in the font file)
    if(glyphShape != null){
      // add the shape to the list
      result.add(glyphShape);
    }
    
  }
  
  return result;
}

void drawGlyphShapes(){
  float maxGlyphHeight = 30;
  for(int i = 0; i < glyphShapes.size(); i++){
    PShape glyphShape = glyphShapes.get(i);
    glyphShape.disableStyle();
    noFill();
    beginShape();
    for(int j = 0 ; j < glyphShape.getVertexCount(); j++){
      PVector vertex = glyphShape.getVertex(j);
      vertex(10 + vertex.x,vertex.y + 30 + glyphShape.getHeight() + (maxGlyphHeight * i));
    }
    endShape(CLOSE);
  }
}

If you want to draw your own glyphs you can always use beginShape(), createShape(), etc. and wrap it in a function that takes the string you want to render and draws the correct shapes for each character.

If you want to create your own ttf font file you may need an external library. Have a look at the Typography Processing libraries for extended functionality.

这篇关于CreateFont() 从字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆