Coder Social home page Coder Social logo

gfx_text's Introduction

gfx_text Build Status crates.io

Library for drawing text for gfx-rs graphics API. Uses freetype-rs underneath.

Usage

Basic usage:

// Initialize text renderer.
let mut text = gfx_text::new(factory).build().unwrap();

// In render loop:

// Add some text 10 pixels down and right from the top left screen corner.
text.add(
    "The quick brown fox jumps over the lazy dog",  // Text to add
    [10, 10],                                       // Position
    [0.65, 0.16, 0.16, 1.0],                        // Text color
);

// Draw text.
text.draw(&mut stream);

See API documentation for overview of all available methods.

You can skip default font by disabling include-font feature:

[dependencies.gfx_text]
version = "*"
default-features = false

Examples

See this example on how to draw text in various styles: different sizes, colors, fonts, etc.

Output:

License

gfx_text's People

Contributors

bvssvni avatar csherratt avatar emberian avatar guycook avatar kagami avatar kvark avatar nwoeanhinnogaehr avatar potpourri avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gfx_text's Issues

Outdated example

Now that gfx_window_glutin is deprecated in favour of glutin_window, the provided example in examples/styles.rs is out of date.
I would like to use gfx_text with the new glutin_window, but it seems to be somewhat involved?

Provide non-Canvas API as an alternative

Canvas should not be the only way to use this library. It would be more flexible to work with Stream and Factory types, while leaving the canvas API redirecting to them for convenience. See gfx-debug-draw for an example. Note: this will look better when factories start carrying their own capabilities.

Multiple calls to draw_end on the same text seems to erase previous calls' result partially.

The following example code:

extern crate gfx;
extern crate gfx_window_glutin;
extern crate glutin;
extern crate gfx_text;

use gfx::traits::{IntoCanvas, Stream};
use gfx_window_glutin as gfxw;
use glutin::{WindowBuilder, Event, VirtualKeyCode, GL_CORE};

const BROWN: [f32; 4] = [0.65, 0.16, 0.16, 1.0];
const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0];

fn main() {
    let mut canvas = {
        let window = WindowBuilder::new()
            .with_dimensions(640, 480)
            .with_title(format!("gfx_text example"))
            .with_gl(GL_CORE)
            .build()
            .unwrap();
        gfxw::init(window).into_canvas()
    };

    let mut normal_text = gfx_text::new(&mut canvas.factory).unwrap();

    'main: loop {
        for event in canvas.output.window.poll_events() {
            match event {
                Event::Closed => break 'main,
                Event::KeyboardInput(_, _, Some(VirtualKeyCode::Escape)) => break 'main,
                _ => {},
            }
        }
        canvas.clear(gfx::ClearData {color: [1.0, 1.0, 1.0, 1.0], depth: 1.0, stencil: 0});

        normal_text.draw("The quick brown fox jumps over the lazy dog", [10, 10], BROWN);
        normal_text.draw_end(&mut canvas).unwrap();

        normal_text.draw("The quick brown fox jumps over the lazy dog", [30, 30], BROWN);
        normal_text.draw_end(&mut canvas).unwrap();

        normal_text.draw("The quick red fox jumps over the lazy dog", [50, 50], RED);
        normal_text.draw_end(&mut canvas).unwrap();

        canvas.present();
    }
}

Will give the following result:

This causes problems in applications where multiple calls to draw_end are done for layering.

Word wrapping

Fit text into the given width, wrap by spaces and also correctly recognize line breaks.

Was requested by @LaylConway on #rust.

Add more docs

Add #![deny(missing_docs)] at crate level to make it an compiler error.

Only RGBA8 format supported

Neither HDR targets nor Srgba8 target are allowed. Draw is hardcoded on Rgba8.

What's the recommended workaround?

Avoid allocation per draw

gfx-rs itself doesn't use any allocations on a call-by-call basis, and it would be great if gfx_text didn't impose this overhead either. The allocations come from the constructed meshes and batches, even if implicit. What can be done:

  • have an OwnedBatch created once on init
  • update its Slice with updating the vertex/index buffers that the batch uses
  • send it to a stream as usual

Measuring text size

This is a feature required to do common things such as centering text and implementing a text box. Perhaps like this:

let mut text = gfx_text::new(factory).build().unwrap();
let size: [i32; 2] = text.measure("Test Value");

gfx_text 0.13.1/0.13.0 unresolved import super::gfx using rust stable 1.13

Compiling gfx_text v0.13.1 error[E0432]: unresolved import super::gfx--> C:\Users\cube\.cargo\registry\src\github.com-1ecc6299db9ec823\gfx_text-0.13.1\src\lib.rs:567:5 | 567 | gfx_pipeline_base!( pipe { | ^ nogfxinshader_structs`
|
= note: this error originates in a macro outside of the current crate

error: aborting due to previous error

error: Could not compile gfx_text.

To learn more, run the command again with --verbose.
`

rustc 1.13.0 (2c6933acc 2016-11-07)

This is similar to #53 but does not seem to be the same.

Use single-component texture for font bitmap

Since freetype provides grayscaled data, it's a waste of space and probably efficiency to store additional meanignless zeroes. I tried to use gfx::tex::R8 and t_Font_Color.r instead of .a in shader but sometimes was getting artifacts because of that.

Probably there is some error in shader and/or library code or you can't just blend R8 texture with vec4 color provided by user with default texture sampler. Needs additional investigation.

cc @kvark

Basic usage exampe not compiling

I can't make basic usage example in the Readme.md compile. The example says:

// Initialize text renderer.
let mut text = gfx_text::new(factory).build().unwrap();

// In render loop:

// Add some text 10 pixels down and right from the top left screen corner.
text.add(
    "The quick brown fox jumps over the lazy dog",  // Text to add
    [10, 10],                                       // Position
    [0.65, 0.16, 0.16, 1.0],                        // Text color
);

// Draw text.
text.draw(&mut stream);

But there is no method draw on Renderer that takes a stream as a parameter, there is only

fn draw<C: CommandBuffer<R>>(&mut self, encoder: &mut Encoder<R, C>, target: &RenderTargetView<R, Rgba8>) -> Result<(), Error>

Text Shaping Engine

Motivation

Currently the library can't work with complex text layout (ex: Arabic, Hindi, Tibetan). Hence, it is necessary to create a text shaping engine to tell how to choose and position glyphs.

Harfbuzz is a mature text shaping lib used by many opensource projects. We can add complex text shaping capability to gfx_text with it.

rusttype also plans to support complex text layout. However, for now it is not in higher priority.

alt text

Harfbuzz Examples

gfx_text couldn't compile - gfx_pipeline_base isn't in the root

Hello,

There is a futur hard error with the macro gfx_pipeline_base used at the module shader_structs.

> rustc --version
rustc 1.15.0-nightly (0bd2ce62b 2016-11-19)
> cargo build
    Updating registry `https://github.com/rust-lang/crates.io-index`
 Downloading gfx v0.12.1
 Downloading freetype-rs v0.11.0
 Downloading draw_state v0.6.0
 Downloading gfx_core v0.4.0
 Downloading bitflags v0.6.0
 Downloading freetype-sys v0.4.0
   Compiling pkg-config v0.3.8
   Compiling bitflags v0.7.0
   Compiling libc v0.2.17
   Compiling gcc v0.3.38
   Compiling bitflags v0.6.0
   Compiling log v0.3.6
   Compiling draw_state v0.6.0
   Compiling gfx_core v0.4.0
   Compiling freetype-sys v0.4.0
   Compiling libz-sys v1.0.7
   Compiling freetype-rs v0.11.0
   Compiling gfx v0.12.1
   Compiling gfx_text v0.13.1 (file:///home/adjivas/Test/gfx_text)
warning: `$crate` may not be imported
   --> src/lib.rs:567:5
    |
567 |     gfx_pipeline_base!( pipe {
    |     ^
    |
    = note: `use $crate;` was erroneously allowed and will become a hard error in a future release
    = note: this error originates in a macro outside of the current crate

error[E0432]: unresolved import ``
   --> src/lib.rs:567:5
    |
567 |     gfx_pipeline_base!( pipe {
    |     ^ no `` in the root
    |
    = note: this error originates in a macro outside of the current crate

error: aborting due to previous error

error: Could not compile `gfx_text`.

To learn more, run the command again with --verbose.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.